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)
        {
            var inputRef  = NativeWrapper.AllocTensorRef(input);
            var finputRef = NativeWrapper.AllocTensorRef(finput);

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

            Marshal.StructureToPtr(inputRef, inputPtr, false);
            var 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 (var output2d = output.View(nOutputPlane, outputHeight * outputWidth))
                {
                    if (bias != null)
                    {
                        using (var 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);
            }
        }
Exemple #2
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);
            }));
        }