コード例 #1
0
ファイル: NativeWrapper.cs プロジェクト: SciSharp/TensorSharp
        /// <summary>
        /// Allocs the tensor reference.
        /// </summary>
        /// <param name="tensor">The tensor.</param>
        /// <returns>TensorRef64.</returns>
        public static TensorRef64 AllocTensorRef(NDArray tensor)
        {
            var tensorRef = new TensorRef64();

            tensorRef.buffer      = CpuNativeHelpers.GetBufferStart(tensor);
            tensorRef.dimCount    = tensor.Shape.Length;
            tensorRef.sizes       = AllocArray(tensor.Shape);
            tensorRef.strides     = AllocArray(tensor.Strides);
            tensorRef.elementType = (CpuDType)tensor.ElementType;
            return(tensorRef);
        }
コード例 #2
0
        private static void Conv2ForwardFrame(Tensor input, Tensor output, Tensor weight, Tensor bias, Tensor finput,
                                              int kW,
                                              int kH,
                                              int dW,
                                              int dH,
                                              int padW,
                                              int padH,
                                              long nInputPlane,
                                              long inputWidth,
                                              long inputHeight,
                                              long nOutputPlane,
                                              long outputWidth,
                                              long outputHeight)
        {
            TensorRef64 inputRef  = NativeWrapper.AllocTensorRef(input);
            TensorRef64 finputRef = NativeWrapper.AllocTensorRef(finput);

            IntPtr inputPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TensorRef64)));

            Marshal.StructureToPtr(inputRef, inputPtr, false);
            IntPtr finputPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TensorRef64)));

            Marshal.StructureToPtr(finputRef, finputPtr, false);

            try
            {
                CpuOpsNative.TS_Unfolded_Copy(finputPtr, inputPtr, kW, kH, dW, dH, padW, padH, (int)nInputPlane, (int)inputWidth, (int)inputHeight, (int)outputWidth, (int)outputHeight);

                using (Tensor output2d = output.View(nOutputPlane, outputHeight * outputWidth))
                {
                    if (bias != null)
                    {
                        using (Tensor biasExp = bias.Expand(nOutputPlane, output2d.Sizes[1]))
                        {
                            Ops.Copy(output2d, biasExp);
                        }
                    }
                    else
                    {
                        Ops.Fill(output, 0);
                    }

                    Ops.Addmm(output2d, 1, output2d, 1, weight, finput);
                }
            }
            finally
            {
                Marshal.FreeHGlobal(inputPtr);
                Marshal.FreeHGlobal(finputPtr);
                NativeWrapper.FreeTensorRef(inputRef);
                NativeWrapper.FreeTensorRef(finputRef);
            }
        }
コード例 #3
0
        public static TensorRef64 AllocTensorRef(Tensor tensor)
        {
            TensorRef64 tensorRef = new TensorRef64
            {
                buffer      = CpuNativeHelpers.GetBufferStart(tensor),
                dimCount    = tensor.Sizes.Length,
                sizes       = AllocArray(tensor.Sizes),
                strides     = AllocArray(tensor.Strides),
                elementType = (CpuDType)tensor.ElementType
            };

            return(tensorRef);
        }
コード例 #4
0
        public static IDisposable BuildTensorRefPtr(Tensor tensor, out IntPtr tensorRefPtr)
        {
            TensorRef64 tensorRef = NativeWrapper.AllocTensorRef(tensor);
            IntPtr      tensorPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TensorRef64)));

            Marshal.StructureToPtr(tensorRef, tensorPtr, false);

            tensorRefPtr = tensorPtr;

            return(new DelegateDisposable(() =>
            {
                Marshal.FreeHGlobal(tensorPtr);
                NativeWrapper.FreeTensorRef(tensorRef);
            }));
        }
コード例 #5
0
        public static void Invoke(MethodInfo method, params object[] args)
        {
            List <TensorRef64> freeListTensor = new List <TensorRef64>();
            List <IntPtr>      freeListPtr    = new List <IntPtr>();

            try
            {
                for (int i = 0; i < args.Length; ++i)
                {
                    if (args[i] is Tensor)
                    {
                        Tensor tensor = (Tensor)args[i];
                        if (!(tensor.Storage is CpuStorage))
                        {
                            throw new InvalidOperationException("Argument " + i + " is not a Cpu tensor");
                        }

                        TensorRef64 tensorRef = AllocTensorRef(tensor);
                        IntPtr      tensorPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TensorRef64)));
                        Marshal.StructureToPtr(tensorRef, tensorPtr, false);

                        args[i] = tensorPtr;

                        freeListTensor.Add(tensorRef);
                        freeListPtr.Add(tensorPtr);
                    }
                }

                //return method.Invoke(null, args);
                int result = (int)method.Invoke(null, args);
                if (result != 0)
                {
                    throw new ApplicationException(GetLastError());
                }
            }
            finally
            {
                foreach (TensorRef64 tensorRef in freeListTensor)
                {
                    FreeTensorRef(tensorRef);
                }

                foreach (IntPtr tensorPtr in freeListPtr)
                {
                    Marshal.FreeHGlobal(tensorPtr);
                }
            }
        }
コード例 #6
0
 public static void FreeTensorRef(TensorRef64 tensorRef)
 {
     Marshal.FreeHGlobal(tensorRef.sizes);
     Marshal.FreeHGlobal(tensorRef.strides);
 }