Ejemplo n.º 1
0
        public Tensor NormAll(Tensor result, Tensor src, float value)
        {
            var writeTarget = TensorResultBuilder.GetWriteTarget(result, src, false, 1);

            NativeWrapper.InvokeTypeMatch(normall_func, writeTarget, src, value);
            return(writeTarget);
        }
Ejemplo n.º 2
0
        public void AddLayerNormGrad(Tensor result1, Tensor result2, Tensor gradGamma_, Tensor gradBeta_, Tensor adj_, Tensor y_, Tensor x1_, Tensor x2_, Tensor gamma_, Tensor beta_, float eps)
        {
            var writeTarget1 = TensorResultBuilder.GetWriteTarget(result1, adj_, false, adj_.Sizes);
            var writeTarget2 = TensorResultBuilder.GetWriteTarget(result2, adj_, false, adj_.Sizes);

            NativeWrapper.InvokeTypeMatch(this.addlayerNormGrad_func, writeTarget1, writeTarget2, gradGamma_, gradBeta_, adj_, y_, x1_, x2_, gamma_, beta_, (int)adj_.Sizes[0], (int)adj_.Sizes[1], eps);
        }
Ejemplo n.º 3
0
        public Tensor LayerNormGrad(Tensor result, Tensor gradGamma_, Tensor gradBeta_, Tensor adj_, Tensor y_, Tensor x_, Tensor gamma_, Tensor beta_, float eps)
        {
            var writeTarget = TensorResultBuilder.GetWriteTarget(result, adj_, false, adj_.Sizes);

            NativeWrapper.InvokeTypeMatch(this.layerNormGrad_func, writeTarget, gradGamma_, gradBeta_, adj_, y_, x_, gamma_, beta_, (int)adj_.Sizes[0], (int)adj_.Sizes[1], eps);
            return(writeTarget);
        }
Ejemplo n.º 4
0
        public Tensor AddLayerNorm(Tensor result, Tensor src1, Tensor src2, Tensor gamma_, Tensor beta_, float eps)
        {
            var writeTarget = TensorResultBuilder.GetWriteTarget(result, src1, false, src1.Sizes);

            NativeWrapper.InvokeTypeMatch(this.addlayerNorm_func, writeTarget, src1, src2, gamma_, beta_, eps, (int)src1.Sizes[0], (int)src1.Sizes[1]);
            return(writeTarget);
        }
Ejemplo n.º 5
0
        public Tensor Softmax(Tensor result, Tensor src)
        {
            Tensor writeTarget = TensorResultBuilder.GetWriteTarget(result, src, false, src.Sizes);

            NativeWrapper.InvokeTypeMatch(softmax_func, writeTarget, src, (int)src.Sizes[0], (int)src.Sizes[1]);
            return(writeTarget);
        }
Ejemplo n.º 6
0
        public Tensor SoftmaxGrad(Tensor grad_, Tensor adj_, Tensor val_, bool addGrad = true)
        {
            Tensor writeTarget = TensorResultBuilder.GetWriteTarget(grad_, adj_, false, adj_.Sizes);

            NativeWrapper.InvokeTypeMatch(softmaxGrad_func, writeTarget, adj_, val_, (int)adj_.Sizes[0], (int)adj_.Sizes[1], addGrad);
            return(writeTarget);
        }
Ejemplo n.º 7
0
        public Tensor Gather(Tensor result, Tensor src, int dim, Tensor indices)
        {
            if (result != null && result.DimensionCount != src.DimensionCount)
            {
                throw new InvalidOperationException("result and src must have same number of dimensions");
            }
            if (result != null && dim < 0 && dim >= result.DimensionCount)
            {
                throw new ArgumentOutOfRangeException("dim");
            }
            if (indices.DimensionCount != src.DimensionCount)
            {
                throw new InvalidOperationException("src and indices must have same number of dimensions");
            }
            if (result != null && !result.IsSameSizeAs(indices))
            {
                throw new InvalidOperationException("result and indices must be the same size");
            }
            if (result != null && !TensorResultBuilder.ArrayEqualExcept(src.Shape, result.Shape, dim))
            {
                throw new InvalidOperationException("result and src must be the same size except in dimension dim");
            }

            var writeTarget = TensorResultBuilder.GetWriteTarget(result, indices.Allocator, src.ElementType, false, indices.Shape);

            NativeWrapper.InvokeTypeMatch(gather_func, writeTarget, src, dim, indices);
            return(writeTarget);
        }
Ejemplo n.º 8
0
        public Tensor ScatterFill(Tensor result, float value, int dim, Tensor indices)
        {
            if (result == null)
            {
                throw new ArgumentNullException("result");
            }

            if (dim < 0 && dim >= result.DimensionCount)
            {
                throw new ArgumentOutOfRangeException("dim");
            }
            if (indices.DimensionCount != result.DimensionCount)
            {
                throw new InvalidOperationException("result and indices must have same number of dimensions");
            }
            if (!TensorResultBuilder.ArrayEqualExcept(indices.Shape, result.Shape, dim))
            {
                throw new InvalidOperationException("result and indices must be the same size except in dimension dim");
            }

            var writeTarget = result;

            NativeWrapper.InvokeTypeMatch(scatterFill_func, writeTarget, value, dim, indices);
            return(writeTarget);
        }
Ejemplo n.º 9
0
        public NDArray Scatter(NDArray result, NDArray src, int dim, NDArray indices)
        {
            if (result == null)
            {
                throw new ArgumentNullException("result");
            }

            if (result.DimensionCount != src.DimensionCount)
            {
                throw new InvalidOperationException("result and src must have same number of dimensions");
            }
            if (dim < 0 && dim >= result.DimensionCount)
            {
                throw new ArgumentOutOfRangeException("dim");
            }
            if (indices.DimensionCount != src.DimensionCount)
            {
                throw new InvalidOperationException("src and indices must have same number of dimensions");
            }
            if (!src.IsSameSizeAs(indices))
            {
                throw new InvalidOperationException("src and indices must be the same size");
            }
            if (!TensorResultBuilder.ArrayEqualExcept(src.Shape, result.Shape, dim))
            {
                throw new InvalidOperationException("result and src must be the same size except in dimension dim");
            }

            var writeTarget = result;

            NativeWrapper.InvokeTypeMatch(scatter_func, writeTarget, src, dim, indices);
            return(writeTarget);
        }
Ejemplo n.º 10
0
        public Tensor StdAll(Tensor result, Tensor src)
        {
            var writeTarget = TensorResultBuilder.GetWriteTarget(result, src, false, 1);

            NativeWrapper.InvokeTypeMatch(stdall_func, writeTarget, src);
            return(writeTarget);
        }
Ejemplo n.º 11
0
        // allArgs should start with a null placeholder for the RNG object
        /// <summary>
        /// Invokes the with RNG.
        /// </summary>
        /// <param name="seed">The seed.</param>
        /// <param name="method">The method.</param>
        /// <param name="allArgs">All arguments.</param>
        private static void InvokeWithRng(int? seed, MethodInfo method, params object[] allArgs)
        {
            if (!seed.HasValue)
                seed = seedGen.Next();

            IntPtr rng;
            NativeWrapper.CheckResult(CpuOpsNative.TS_NewRNG(out rng));
            NativeWrapper.CheckResult(CpuOpsNative.TS_SetRNGSeed(rng, seed.Value));
            allArgs[0] = rng;
            NativeWrapper.InvokeTypeMatch(method, allArgs);
            NativeWrapper.CheckResult(CpuOpsNative.TS_DeleteRNG(rng));
        }
Ejemplo n.º 12
0
        public void Cols2Im(NDArray col, NDArray im, int channels, int height, int width,
                            int patch_h, int patch_w, int pad_h,
                            int pad_w, int stride_h, int stride_w,
                            int dilation_h, int dilation_w)
        {
            int height_col = (height + 2 * pad_h - (dilation_h * (patch_h - 1) + 1))
                             / stride_h + 1;
            int width_col = (width + 2 * pad_w - (dilation_w * (patch_w - 1) + 1))
                            / stride_w + 1;

            NativeWrapper.InvokeTypeMatch(cols2im_func, col, height, width, channels, patch_h, patch_w, pad_h, pad_w, stride_h, stride_w,
                                          dilation_h, dilation_w, height_col, width_col, im);
        }
Ejemplo n.º 13
0
        public void Im2Cols(NDArray im, NDArray col, int channels,
                            int height, int width,
                            int ksize_h, int ksize_w, int pad_h,
                            int pad_w, int stride_h, int stride_w,
                            int dilation_h, int dilation_w)
        {
            int height_col = (height + 2 * pad_h - (dilation_h * (ksize_h - 1) + 1))
                             / stride_h + 1;
            int width_col = (width + 2 * pad_w - (dilation_w * (ksize_w - 1) + 1))
                            / stride_w + 1;

            NativeWrapper.InvokeTypeMatch(im2cols_func, im, height, width, channels, ksize_h, ksize_w, pad_h, pad_w, stride_h, stride_w,
                                          dilation_h, dilation_w, height_col, width_col, col);
        }
Ejemplo n.º 14
0
        public Tensor SoftmaxGrad(Tensor grad_, Tensor adj_, Tensor val_, bool addGrad = true)
        {
            var ndim        = adj_.DimensionCount;
            var storageSize = TensorDimensionHelpers.GetStorageSize(adj_.Sizes, adj_.Strides);
            var cols        = adj_.Sizes[ndim - 1];

            if (storageSize % cols != 0)
            {
                throw new Exception($"Invalid tensor storage size = '{storageSize}', and cols = '{cols}'");
            }

            var rows = storageSize / cols;

            var writeTarget = TensorResultBuilder.GetWriteTarget(grad_, adj_, false, adj_.Sizes);

            NativeWrapper.InvokeTypeMatch(this.softmaxGrad_func, writeTarget, adj_, val_, (int)rows, (int)cols, addGrad);
            return(writeTarget);
        }
Ejemplo n.º 15
0
        public Tensor Softmax(Tensor result, Tensor src)
        {
            var ndim        = src.DimensionCount;
            var storageSize = TensorDimensionHelpers.GetStorageSize(src.Sizes, src.Strides);
            var cols        = src.Sizes[ndim - 1];

            if (storageSize % cols != 0)
            {
                throw new Exception($"Invalid tensor storage size = '{storageSize}', and cols = '{cols}'");
            }

            var rows = storageSize / cols;

            var writeTarget = TensorResultBuilder.GetWriteTarget(result, src, false, src.Sizes);

            NativeWrapper.InvokeTypeMatch(this.softmax_func, writeTarget, src, (int)rows, (int)cols);
            return(writeTarget);
        }
Ejemplo n.º 16
0
        public Tensor SoftmaxMask(Tensor result, Tensor src, Tensor mask)
        {
            var ndim        = src.DimensionCount;
            var storageSize = TensorDimensionHelpers.GetStorageSize(src.Sizes, src.Strides);
            var cols        = src.Sizes[ndim - 1];

            if (storageSize % cols != 0)
            {
                throw new Exception($"Invalid tensor storage size = '{storageSize}', and cols = '{cols}'");
            }

            var rows = storageSize / cols;

            var maskNdim        = mask.DimensionCount;
            var maskStorageSize = TensorDimensionHelpers.GetStorageSize(mask.Sizes, mask.Strides);
            var maskCols        = mask.Sizes[maskNdim - 1];

            if (maskStorageSize % maskCols != 0)
            {
                throw new Exception($"Invalid mask tensor storage size = '{maskStorageSize}', and cols = '{maskCols}'");
            }

            var maskRows = maskStorageSize / maskCols;

            if (rows % maskRows != 0)
            {
                throw new Exception($"Invalid tensor rows = '{rows}' and mask tensor rows = '{maskRows}'");
            }

            if (cols != maskCols)
            {
                throw new Exception($"Tensor cols = '{cols}', mask tensor cols = '{maskCols}'. They should be equal.");
            }

            var writeTarget = TensorResultBuilder.GetWriteTarget(result, src, false, src.Sizes);

            NativeWrapper.InvokeTypeMatch(this.softmaxmask_func, writeTarget, src, mask, (int)rows, (int)cols, (int)maskRows);
            return(writeTarget);
        }
Ejemplo n.º 17
0
 public void Fill(Tensor result, float value)
 {
     NativeWrapper.InvokeTypeMatch(fill_func, result, value);
 }
Ejemplo n.º 18
0
 public Tensor Adam(Tensor tw, Tensor tg, Tensor tv, Tensor tm, int batchSize, float step_size, float clipval, float regc, float decay_rate_v, float decay_rate_m, int iter, float eps)
 {
     NativeWrapper.InvokeTypeMatch(this.adam_func, tw, tg, tv, tm, (int)tw.Sizes[0], (int)tw.Sizes[1], batchSize, step_size, clipval, regc, decay_rate_v, decay_rate_m, iter, eps);
     return(tw);
 }
Ejemplo n.º 19
0
 public Tensor RMSProp(Tensor tw, Tensor tg, Tensor tc, int batchSize, float step_size, float clipval, float regc, float decay_rate, float eps)
 {
     NativeWrapper.InvokeTypeMatch(this.rmsProp_func, tw, tg, tc, (int)tw.Sizes[0], (int)tw.Sizes[1], batchSize, step_size, clipval, regc, decay_rate, eps);
     return(tw);
 }
Ejemplo n.º 20
0
 public Tensor UpdateCost(Tensor tw, Tensor tids, Tensor tc)
 {
     NativeWrapper.InvokeTypeMatch(this.updatecost_func, tw, tids, tc, (int)tw.Sizes[0], (int)tw.Sizes[1]);
     return(tw);
 }