public static InternalArray Load(byte[] bts) { ushort len = (ushort)((bts[0x6 + 0x2]) + (bts[0x6 + 0x2 + 1] << 8)); var str = Encoding.UTF8.GetString(bts, 10, len); var shp = GetProp(str, "shape"); var arr3 = shp.Split(new char[] { ',', '(', ')', ' ', }, StringSplitOptions.RemoveEmptyEntries).ToArray(); var dims = arr3.Select(z => int.Parse(z)).ToArray(); var descr = GetProp(str, "descr"); int bytesPerItem = 4; bool isFloat = descr.Contains("<f4"); InternalArray ret = new InternalArray(dims, isFloat); int cnt = 0; for (int i = 10 + len; i < bts.Length; i += bytesPerItem) { if (isFloat) { var val = BitConverter.ToSingle(bts, i); ret.Data[cnt] = val; } else { var val = BitConverter.ToUInt32(bts, i); ret.IntData[cnt] = val; } cnt++; } return(ret); }
internal static InternalArray bin_conv2d(InternalArray input, InternalArray weight, InternalArray bias, InternalArray alpha, int[] kernel_size, int[] stride, int[] padding) { var col_tensor = THWrapper.THFloatTensor_new(); var output = THWrapper.THFloatTensor_new(); var _alpha = alpha.ToTHTensor(); var _input = input.ToTHTensor(); var _weight = weight.ToTHTensor(); IntPtr _bias; if (bias == null) { _bias = THWrapper.THFloatTensor_new(); } else { _bias = bias.ToTHTensor(); } binop.THNN_Bin_SpatialConvolutionMM_updateOutput(_input, output, _weight, _bias, col_tensor, _alpha, kernel_size[0], kernel_size[1], stride[0], stride[1], padding[0], padding[1]); THWrapper.THFloatTensor_free(col_tensor); var ret = InternalArray.FromTHFloatTensor(output); THWrapper.THFloatTensor_free(output); THWrapper.THFloatTensor_free(_bias); THWrapper.THFloatTensor_free(_input); THWrapper.THFloatTensor_free(_alpha); return(ret); }
public override InternalArray Forward(InternalArray ar) { if (ar.QIntData != null) { ar.Data = new float[ar.QIntData.Length]; for (int i = 0; i < ar.QIntData.Length; i++) { ar.Data[i] = ar.QIntData[i] / 256f; } ar.QIntData = null; } InternalArray ret = new InternalArray(new int[] { 1, Weight.Shape[0] }); for (int j = 0; j < Weight.Shape[0]; j++) { float acc = 0; for (int i = 0; i < Weight.Shape[1]; i++) { acc += Weight.Get2D(j, i) * ar.Data[i]; } ret.Data[j] = acc; if (Bias != null) { ret.Data[j] += Bias.Data[j]; } } return(ret); }
public static void THFloatTensor_zero(InternalArray ar) { for (int i = 0; i < ar.Data.Length; i++) { ar.Data[i] = 0; } }
internal InternalArray Unsqueeze(int v) { InternalArray ret = new InternalArray(new int[] { 1, Shape[0], Shape[1], Shape[2] }); ret.Data = Data.ToArray(); return(ret); }
public void PrepareData() { InternalArray qint = RunningMean.GetQInt(256); RunningMean = qint; var cln = RunningVar.Clone(); for (int i = 0; i < cln.Data.Length; i++) { cln.Data[i] = (float)Math.Sqrt(cln.Data[i] + eps); } InternalArray qint2 = cln.GetQInt(256); for (int i = 0; i < qint2.QIntData.Length; i++) { if (qint2.QIntData[i] == 0) { qint2.QIntData[i] = 1; } var q1 = qint2.Unquant(i); var err = Math.Abs(q1 - Math.Sqrt(RunningVar.Data[i] + eps)); if (err > 0.1) { } } RunningVar = qint2; InternalArray qint3 = Bias.GetQInt(256); Bias = qint3; InternalArray qint4 = Weight.GetQInt(256); Weight = qint4; }
public void CalcRegionsHashes(InternalArray ar) { int ww = RegionSize;//region size int rw = ar.Shape[1] / ww; int rh = ar.Shape[2] / ww; for (int i = 0; i < rw; i++) { for (int j = 0; j < rh; j++) { int pointer = 0; pointer = 0;//zero channel var xx = i * ww; var yy = j * ww; pointer = xx + yy * ar.offsets[1]; int hash = 0; for (int i2 = 0; i2 < ww; i2++) { for (int j2 = 0; j2 < ww; j2++) { hash ^= (int)(ar.Data[pointer] * 1000); pointer++; } pointer += ar.offsets[1]; pointer -= ww; } } } }
public static void THFloatTensor_fill(InternalArray ar, float val) { for (int i = 0; i < ar.Data.Length; i++) { ar.Data[i] = val; } }
internal InternalArray Transpose2D() { if (Shape.Length != 2) { throw new Exception(); } InternalArray ret = new InternalArray(new int[] { Shape[1], Shape[0] }); if (QIntData != null) { ret.Data = null; ret.QIntData = new short[QIntData.Length]; for (int i = 0; i < Shape[0]; i++) { for (int j = 0; j < Shape[1]; j++) { var val = Get2DQuant(i, j); ret.Set2DQuant(j, i, val); } } return(ret); } for (int i = 0; i < Shape[0]; i++) { for (int j = 0; j < Shape[1]; j++) { var val = Get2D(i, j); ret.Set2D(j, i, val); } } return(ret); }
public int CalcSubRegionHash(InternalArray ar, int sx, int sy, int ww, int hout, int wout) { int pointer = 0; int hash = 0; for (int ch = 0; ch < ar.Shape[0]; ch++) { var xx = sx * ww; var yy = sy * ww; pointer = xx + yy * ar.offsets[1]; for (int i2 = 0; i2 < ww; i2++) { for (int j2 = 0; j2 < ww; j2++) { hash ^= (int)(ar.Data[pointer] * 1000); pointer++; } pointer += ar.offsets[1]; pointer -= ww; } pointer += ar.offsets[0]; } return(hash); }
public override InternalArray Forward(InternalArray ar) { #if PROFILER LogItem = new CalcLogItem(this, "batchNorm2d"); var sw = Stopwatch.StartNew(); #endif InternalArray ret = new InternalArray(ar.Shape); var n = ar.Shape[0]; var c = ar.Shape[1]; List <float> data = new List <float>(); for (int i = 0; i < ar.Data.Length; i++) { var tt = (float)(((ar.Data[i] - RunningMean.Data[i]) / Math.Sqrt(RunningVar.Data[i] + eps)) * Weight.Data[i] + Bias.Data[i]); data.Add(tt); } ret.Data = data.ToArray(); #if PROFILER sw.Stop(); if (Parent != null) { Profiler.AddLog(Parent.LogItem, LogItem); } #endif return(ret); }
public override InternalArray Forward(InternalArray ar) { InternalArray ret = new InternalArray(ar.Shape); var n = ar.Shape[0]; var c = ar.Shape[1]; List <short> data = new List <short>(); InternalArray ret2 = new InternalArray(ar.Shape); ret2.Data = null; for (int i = 0; i < n; i++) { for (int j = 0; j < c; j++) { var img = Helpers.Get2DImageFrom4DArray(ar, i, j); int len = 0; if (img.QIntData != null) { len = img.QIntData.Length; } else { len = img.Data.Length; } for (int zi = 0; zi < len; zi++) { // var orig = (float)(((img.Data[zi] - RunningMean.Data[j]) / Math.Sqrt(RunningVar.Data[j] + eps)) * Weight.Data[j] + Bias.Data[j]); //img.Data[zi] = (float)(((img.Data[zi] - RunningMean.Data[j]) / Math.Sqrt(RunningVar.Data[j] + eps)) * Weight.Data[j] + Bias.Data[j]); short val = 0; if (img.QIntData != null) { val = img.QIntData[zi]; } else { val = (short)(img.Data[zi] * 256); } var val2 = val - RunningMean.QIntData[j]; var val3 = (short)((short)((int)(val2 << 8) / RunningVar.QIntData[j])); var val4 = (short)((int)(val3 * Weight.QIntData[j]) >> 8); var val5 = (short)((val4 + Bias.QIntData[j])); //var res = val5 / 256f; var res = val5; //var input = img.Data[zi]; //var res2 = (float)(((input - (qint.QIntData[j] / 256f)) / (qint2.QIntData[j] / 256f)) * qint4.QIntData[j] / 256f + qint3.QIntData[j] / 256f); //img.Data[zi] = res; data.Add(res); //img.Data[zi] = res; } //data.AddRange(img.Data); } } ret2.QIntData = data.ToArray(); return(ret2); }
public void Sub(InternalArray ar) { for (int i = 0; i < Data.Length; i++) { Data[i] -= ar.Data[i]; } }
public static InternalArray ParseFromString(string str) { List <int> dims = new List <int>(); int dim = 0; int maxdim = 0; StringBuilder sb = new StringBuilder(); List <int> cnt = new List <int>(); List <float> data = new List <float>(); str = str.Substring(str.IndexOf('[')); for (int i = 0; i < str.Length; i++) { if (str[i] == '[') { dim++; maxdim = Math.Max(dim, maxdim); if (dims.Count < maxdim) { dims.Add(0); cnt.Add(0); } cnt[dim - 1] = 1; sb.Clear(); continue; } if (str[i] == ']') { if (dim == maxdim) { data.Add(float.Parse(sb.ToString())); } dims[dim - 1] = Math.Max(dims[dim - 1], cnt[dim - 1]); dim--; sb.Clear(); if (dim == 0) { break; } continue; } if (str[i] == ',') { if (dim == maxdim) { data.Add(float.Parse(sb.ToString())); } sb.Clear(); cnt[dim - 1]++; continue; } sb.Append(str[i]); } InternalArray ret = new InternalArray(dims.ToArray()); ret.Data = data.ToArray(); return(ret); }
public static InternalArray GetNext2dImageFrom4dArray(this InternalArray array, ref int pos0) { InternalArray ret = new InternalArray(new int[] { array.Shape[2], array.Shape[3] }); Array.Copy(array.Data, pos0, ret.Data, 0, ret.Data.Length); pos0 += array.offsets[1]; return(ret); }
public static InternalArray Get3DImageFrom4DArray(this InternalArray array, int ind1) { int pos = ind1 * array.offsets[0]; InternalArray ret = new InternalArray(new int[] { array.Shape[1], array.Shape[2], array.Shape[3] }); Array.Copy(array.Data, pos, ret.Data, 0, ret.Data.Length); return(ret); }
public BinLinear(int inFeatures, int outFeatures, bool bias, bool fpBn = false) { bn = fpBn ? new FPBatchNorm1d(inFeatures) : new BatchNorm1d(inFeatures); if (bias) { Bias = new InternalArray(new int[] { outFeatures }); } }
public Linear(int inFeatures, int outFeratures, bool bias) { Weight = new InternalArray(new int[] { outFeratures, inFeatures }); if (bias) { Bias = new InternalArray(new int[] { inFeatures }); } }
public static InternalArray FromTHIntTensor(IntPtr tensor) { var dims = THWrapper.THIntTensor_nDimension(tensor); int[] d = new int[dims]; for (int i = 0; i < dims; i++) { d[i] = (int)THWrapper.THIntTensor_size(tensor, i); } InternalArray ret = new InternalArray(d, false); if (dims == 2) { for (int i = 0; i < d[0]; i++) { for (int j = 0; j < d[1]; j++) { var val = THWrapper.THIntTensor_get2d(tensor, i, j); if (val != 0) { } ret.Set2DInt(i, j, (uint)val); } } } if (dims == 3) { for (int k = 0; k < d[0]; k++) { for (int i = 0; i < d[1]; i++) { for (int j = 0; j < d[2]; j++) { ret.Set3DInt(k, i, j, (uint)THWrapper.THIntTensor_get3d(tensor, k, i, j)); } } } } if (dims == 4) { for (int k1 = 0; k1 < d[0]; k1++) { for (int k = 0; k < d[1]; k++) { for (int i = 0; i < d[2]; i++) { for (int j = 0; j < d[3]; j++) { ret.Set4DInt(k1, k, i, j, (uint)THWrapper.THIntTensor_get4d(tensor, k1, k, i, j)); } } } } } ret.UpdateOffsets(); return(ret); }
public override InternalArray Forward(InternalArray ar) { #if PROFILER LogItem = new CalcLogItem(this, "avgPool2D"); var sw = Stopwatch.StartNew(); #endif var hin = ar.Shape[2]; var win = ar.Shape[3]; var n = ar.Shape[0]; var c = ar.Shape[1]; var hout = ((hin + 2 * padding[0] - kernelSize[0]) / stride[0]) + 1; var wout = ((win + 2 * padding[1] - kernelSize[1]) / stride[1]) + 1; InternalArray ret = new InternalArray(new int[] { n, c, hout, wout }); //get all 2d images //append them together to [n,c] if (ar.QIntData != null) { List <short> data = new List <short>(); ret.QIntData = new short[ret.Data.Length]; ret.Data = null; for (int i = 0; i < n; i++) { for (int j = 0; j < c; j++) { var img = Helpers.Get2DImageFrom4DArray(ar, i, j); var img2 = Process2DImage(img); data.AddRange(img2.QIntData); } } ret.QIntData = data.ToArray(); } else { List <float> data = new List <float>(); for (int i = 0; i < n; i++) { for (int j = 0; j < c; j++) { var img = Helpers.Get2DImageFrom4DArray(ar, i, j); var img2 = Process2DImage(img); data.AddRange(img2.Data); } } ret.Data = data.ToArray(); } #if PROFILER sw.Stop(); Profiler.AddLog(LogItem, this, "exec", sw.ElapsedMilliseconds, true); #endif return(ret); }
public void ProcessSubRegion(InternalArray ar, InternalArray[,] filters, InternalArray ret, int sx, int sy, int ww, int hout, int wout) { var hin = ar.Shape[1]; var win = ar.Shape[2]; var c = ar.Shape[0]; int shiftx = padding[0] - kernelSize[0] / 2; int shifty = padding[1] - kernelSize[1] / 2; var maxw = Math.Min(wout, (sy + 1) * ww); var maxh = Math.Min(hout, (sx + 1) * ww); var starth = sx * ww; var startw = sy * ww; for (int i = starth; i < maxh; i++) { for (int j = startw; j < maxw; j++) { for (int ch = 0; ch < outChannels; ch++) { float val = 0; for (int zz = 0; zz < c; zz++) { var kernel = filters[ch, zz]; for (int i1 = 0; i1 < kernelSize[0]; i1++) { for (int j1 = 0; j1 < kernelSize[0]; j1++) { //var x = i * stride[0] - kernelSize[0] / 2 + i1; //var y = j * stride[1] - kernelSize[1] / 2 + j1; //outspace var xout = (i) * stride[0] - kernelSize[0] / 2 + i1 * dilation[0]; var yout = (j) * stride[1] - kernelSize[1] / 2 + j1 * dilation[1]; //inspace var xin = xout - shiftx; var yin = yout - shifty; if (!ar.WithIn(zz, xin, yin)) { continue; } //var y=jmul+j1 var ii1 = i1 * kernel.Shape[1] + j1; var ii2 = zz * ar.offsets[0] + xin * ar.offsets[1] + yin; val += (float)(kernel.Get2D(i1, j1) * ar.Get3D(zz, xin, yin)); var ii3 = j + ch * ret.offsets[0] + i * ret.offsets[1]; } } } ret.Set3D(ch, i, j, val); } } } }
public static void fpbinary_gemm_cpu(IntPtr a, IntPtr b, IntPtr c, int m, int nn, int k, int transb, int beta, int alpha, IntPtr alphas) { if (THWrapper.THFloatTensor_nDimension(c) != 2 || THWrapper.THFloatTensor_size(c, 0) * THWrapper.THFloatTensor_size(c, 1) < m * k) { THWrapper.THFloatTensor_resize2d(c, m, k); //THFloatTensor_resize2d(c, m, k); } /* * uint32_t* A = (uint32_t*)THIntTensor_data(a); * uint32_t* B = (uint32_t*)THIntTensor_data(b); * float* C = THFloatTensor_data(c); * float* D = THFloatTensor_data(alphas); */ var A = THWrapper.THIntTensor_data(a); var B = THWrapper.THIntTensor_data(b); var C = THWrapper.THFloatTensor_data(c); var D = THWrapper.THFloatTensor_data(alphas); var aa = InternalArray.FromTHIntTensor(a); var bb = InternalArray.FromTHIntTensor(b); var cc = InternalArray.FromTHFloatTensor(c); var dd = InternalArray.FromTHFloatTensor(alphas); int n = 1 + (nn - 1) / matmul.ENCODE_BIT; int brow = transb != 0 ? 1 : k; int bcol = transb != 0 ? n : 1; //matmul.dgemm_nn(m, k, nn, A, n, 1, B, brow, bcol, C, k, 1, beta, alpha, D); //matmul.dgemm_nn(m, k, nn, A, n, 1, B, brow, bcol, C, k, 1, beta, alpha, D); matmul.fpdgemm_nn(m, k, nn, A, n, 1, B, brow, bcol, C, k, 1, beta); if (alpha != 0) { for (int i = 0; i < m; i++) { for (int j = 0; j < k; j++) { //C[i * n + j] *= alphas[i]; var aa1 = matmul.GetFloat(C, i * k + j); short aq1 = (short)(aa1 * 256); var aa2 = matmul.GetFloat(D, i); var aq2 = (short)(aa2 * 256); var val4 = (short)((int)(aq1 * aq2) >> 8); var quant_res = val4 / 256f; var orig = aa1 * aa2; //matmul.SetFloat(C, i * k + j, aa1 * aa2); matmul.SetFloat(C, i * k + j, val4); //C[i * n + j] = (float)(C[i * n + j] * alphas[i]); } } } }
public void ProcessSubRegionOptimized(InternalArray ar, InternalArray[,] filters, InternalArray ret, int sx, int sy, int ww, int hout, int wout) { var hin = ar.Shape[1]; var win = ar.Shape[2]; var c = ar.Shape[0]; int shiftx = padding[0] - kernelSize[0] / 2; int shifty = padding[1] - kernelSize[1] / 2; var maxw = Math.Min(wout, (sy + 1) * ww); var maxh = Math.Min(hout, (sx + 1) * ww); var starth = sx * ww; var startw = sy * ww; for (int i = starth; i < maxh; i++) { var imul = (i) * stride[0] - kernelSize[0] / 2 - shiftx; var maxi1 = Math.Min((ar.Shape[1] - imul) / dilation[0], kernelSize[0]); var mini1 = Math.Max((int)Math.Ceiling(-(double)imul / dilation[0]), 0); for (int j = startw; j < maxw; j++) { var jmul = (j) * stride[1] - kernelSize[1] / 2 - shifty; var minj1 = Math.Max((int)Math.Ceiling(-(double)jmul / dilation[1]), 0); var maxj1 = Math.Min((ar.Shape[2] - jmul) / dilation[1], kernelSize[1]); for (int ch = 0; ch < outChannels; ch++) { float val = 0; for (int zz = 0; zz < c; zz++) { var kernel = filters[ch, zz]; var offset1 = zz * ar.offsets[0]; int kindex = 0; for (int i1 = mini1; i1 < maxi1; i1++) { var x = imul + i1 * dilation[0]; for (int j1 = minj1; j1 < maxj1; j1++) { var y = jmul + j1 * dilation[1]; var index = offset1 + x * ar.offsets[1] + y; val += kernel.Data[kindex] * ar.Data[index]; kindex++; } } } ret.Set3D(ch, i, j, val); } } } }
public override InternalArray Forward(InternalArray ar1) { InternalArray ar = ar1.Clone(); for (int i = 0; i < ar.Data.Length; i++) { ar.Data[i] = Math.Max(0, ar.Data[i]); } return(ar); }
public static InternalArray Relu(InternalArray ar) { InternalArray ar1 = new InternalArray(ar.Shape); for (int i = 0; i < ar.Data.Length; i++) { ar1.Data[i] = Math.Max(0, ar.Data[i]); } return(ar1); }
public static InternalArray GetNext3DImageFrom4DArrayQuant(this InternalArray array, ref int pos) { InternalArray ret = new InternalArray(new int[] { array.Shape[1], array.Shape[2], array.Shape[3] }); ret.QIntData = new short[ret.Data.Length]; ret.Data = null; Array.Copy(array.QIntData, pos, ret.QIntData, 0, ret.QIntData.Length); pos += array.offsets[0]; return(ret); }
public static InternalArray Randn(this Random r, int[] dims) { var ar = new InternalArray(dims); for (int i = 0; i < ar.Data.Length; i++) { ar.Data[i] = (float)r.NextGaussian(); } return(ar); }
public BatchNorm2d(int nOut, double eps = 1e-5) { this.eps = eps; Bias = new InternalArray(new int[] { nOut }); Weight = new InternalArray(new int[] { nOut }); for (int i = 0; i < Weight.Data.Length; i++) { Weight.Data[i] = 1; } }
public void PrepareData() { if (Bias != null) { InternalArray qint3 = Bias.GetQInt(256); Bias = qint3; } InternalArray qint4 = Weight.GetQInt(256); Weight = qint4; }
public Conv2d(int inChannels, int outChannels, int kSize, int stride, int padding, bool bias = false, int dilation = 1) { this.inChannels = inChannels; this.outChannels = outChannels; Weight = new InternalArray(new int[] { outChannels, inChannels, kSize, kSize }); this.padding = new int[] { padding, padding }; this.stride = new[] { stride, stride }; this.kernelSize = new[] { kSize, kSize }; this.dilation = new[] { dilation, dilation }; }