public static void AddFloat32_GetGradients(Tensor[] gradients, Tensor s, Tensor[] tensors) { for (int j = 0; j < gradients.Length; j++) { Tensor gradient = gradients[j]; //gradient.SetValue(0); long go = s.Shape.TotalSize / gradient.Shape.TotalSize * gradient.Shape.TotalSize; for (long i = 0; i < go; i += gradient.Shape.TotalSize) { if (go == 0) { VectorizationFloat.ElementWiseAssignAVX((float *)gradient.Base.Array, (float *)s.Base.Array + i, gradient.Shape.TotalSize); } else { VectorizationFloat.ElementWiseAddAVX((float *)s.Base.Array + i, (float *)gradient.Base.Array, (float *)gradient.Base.Array, gradient.Shape.TotalSize); } } if (go < s.Shape.TotalSize) { VectorizationFloat.ElementWiseAddAVX((float *)s.Base.Array + go, (float *)gradient.Base.Array, (float *)gradient.Base.Array, s.Shape.TotalSize - go); } } }
public unsafe void Add() { int l = 10000; float[] v1 = new float[l]; for (int i = 0; i < l; i++) { v1[i] = i; } float[] v2 = new float[l]; for (int i = 0; i < l; i++) { v2[i] = i; } float[] res = new float[l]; fixed(float *a = v1, b = v2, y = res) VectorizationFloat.ElementWiseAddAVX(a, b, y, res.Length); float[] res2 = new float[l]; for (int i = 0; i < l; i++) { res2[i] = i * 2; } Assert.IsTrue(ArrayEqual(res, res2)); }
public unsafe void Add2() { float[] v1 = { 1, 2, 3 }; float v2 = 2; float[] res = new float[v1.Length]; fixed(float *a = v1, y = res) VectorizationFloat.ElementWiseAddAVX(a, v2, y, res.Length); float[] res2 = { 3, 4, 5 }; Assert.IsTrue(ArrayEqual(res, res2)); }
public static Tensor SoftmaxFloat32_GetGradient_0(Tensor s, Tensor sm) { Tensor combined = Tensor.Clone(s); long groupsize = sm.Shape[sm.Shape.N - 1]; for (long start = 0; start < combined.Shape.TotalSize; start += groupsize) { float averageK = VectorizationFloat.SumOfProduction((float *)s.Base.Array + start, (float *)sm.Base.Array + start, groupsize); VectorizationFloat.ElementWiseAddAVX((float *)combined.Base.Array + start, -averageK, (float *)combined.Base.Array + start, groupsize); } VectorizationFloat.ElementWiseMultiplyAVX((float *)combined.Base.Array, (float *)sm.Base.Array, (float *)combined.Base.Array, combined.Shape.TotalSize); return(combined); }
public static Tensor[] AddFloat32_GetGradients(Tensor s, Tensor[] tensors, bool generateseperately = false) { Tensor[] gradients = new Tensor[tensors.Length]; Dictionary <long, Tensor> dict = new Dictionary <long, Tensor>(); dict[s.Shape.TotalSize] = s; for (int j = 0; j < gradients.Length; j++) { Tensor a = tensors[j]; if (!generateseperately && dict.ContainsKey(a.Shape.TotalSize)) { gradients[j] = dict[a.Shape.TotalSize]; } else { Tensor gradient = new Tensor(a.Shape.Clone(), TensorConfig.Host_Float32); //gradient.SetValue(0); long go = s.Shape.TotalSize / gradient.Shape.TotalSize * gradient.Shape.TotalSize; for (long i = 0; i < go; i += gradient.Shape.TotalSize) { if (go == 0) { VectorizationFloat.ElementWiseAssignAVX((float *)gradient.Base.Array, (float *)s.Base.Array + i, gradient.Shape.TotalSize); } else { VectorizationFloat.ElementWiseAddAVX((float *)s.Base.Array + i, (float *)gradient.Base.Array, (float *)gradient.Base.Array, gradient.Shape.TotalSize); } } if (go < s.Shape.TotalSize) { VectorizationFloat.ElementWiseAddAVX((float *)s.Base.Array + go, (float *)gradient.Base.Array, (float *)gradient.Base.Array, s.Shape.TotalSize - go); } } } return(gradients); }
public static void SubtractFloat32_GetGradientA(Tensor gradienta, Tensor s, Tensor a) { long go = s.Shape.TotalSize / gradienta.Shape.TotalSize * gradienta.Shape.TotalSize; for (long i = 0; i < go; i += gradienta.Shape.TotalSize) { if (i == 0) { VectorizationFloat.ElementWiseAssignAVX((float *)gradienta.Base.Array, (float *)s.Base.Array + i, gradienta.Shape.TotalSize); } else { VectorizationFloat.ElementWiseAddAVX((float *)s.Base.Array + i, (float *)gradienta.Base.Array, (float *)gradienta.Base.Array, gradienta.Shape.TotalSize); } } if (go < s.Shape.TotalSize) { VectorizationFloat.ElementWiseAddAVX((float *)s.Base.Array + go, (float *)gradienta.Base.Array, (float *)gradienta.Base.Array, s.Shape.TotalSize - go); } }
public static void AddFloat32(Tensor res, Tensor a, Tensor b) { if (a.Shape.TotalSize > b.Shape.TotalSize) { Tensor temp = a; a = b; b = temp; } long go = res.Shape.TotalSize / a.Shape.TotalSize * a.Shape.TotalSize; for (long i = 0; i < go; i += a.Shape.TotalSize) { VectorizationFloat.ElementWiseAddAVX((float *)a.Base.Array, (float *)b.Base.Array + i, (float *)res.Base.Array + i, a.Shape.TotalSize); } if (go < res.Shape.TotalSize) { VectorizationFloat.ElementWiseAddAVX((float *)a.Base.Array, (float *)b.Base.Array + go, (float *)res.Base.Array + go, res.Shape.TotalSize - go); } }
public static void ExpandFloat32_GetGradient_0(Tensor res, Tensor s, Shape thisShape, Shape term0, Shape Multiplier) { res.SetValue(0); float *ptrcombined = (float *)res.Base.Array; float *ptrs = (float *)s.Base.Array; if (Multiplier.N == 2 && Multiplier[1] == 1) { for (int i = 0; i < Multiplier[0]; i++) { float *me = ((float *)s.Base.Array) + i * term0.TotalSize; VectorizationFloat.ElementWiseAddAVX((float *)res.Base.Array, me, (float *)res.Base.Array, term0.TotalSize); } } else { Index iterator = new Index(thisShape); iterator.SetZero(); for (int h = 0; h < thisShape.TotalSize; h++) { long indexs = 0; for (int i = iterator.N - 1; i >= 0; i--) { if (iterator.Indices[i] == thisShape[i]) { iterator.Indices[i] = 0; iterator.Indices[i - 1]++; } indexs += (iterator.Indices[i] / Multiplier[i]) * term0.Multiplied[i + 1]; } ptrcombined[indexs] += ptrs[h]; iterator.Indices[iterator.N - 1]++; } } }