Esempio n. 1
0
        public override void DoPoolGradient(Volume <double> input, Volume <double> outputGradient,
                                            Volume <double> inputGradient, int windowWidth, int windowHeight,
                                            int horizontalPad, int verticalPad, int horizontalStride, int verticalStride)
        {
            var inputStorage          = input.Storage as VolumeStorage;
            var inputGradientStorage  = inputGradient.Storage as VolumeStorage;
            var outputStorage         = this._volumeStorage;
            var outputGradientStorage = outputGradient.Storage as VolumeStorage;

            // Copy to device if not already done
            //outputStorage.CopyToDevice();
            outputGradientStorage.CopyToDevice();
            inputStorage.CopyToDevice();
            inputGradientStorage.CopyToDevice();

            // Synchro
            this._context.DefaultStream.Synchronize();

            using (var poolingDesc = new PoolingDescriptor())
                using (var srcDesc = new TensorDescriptor())
                    using (var srcDiffDesc = new TensorDescriptor())
                        using (var destDesc = new TensorDescriptor())
                            using (var destDiffDesc = new TensorDescriptor())
                            {
                                var n = this.Shape.GetDimension(3);
                                var c = this.Shape.GetDimension(2);
                                var h = this.Shape.GetDimension(1);
                                var w = this.Shape.GetDimension(0);

                                srcDesc.SetTensor4dDescriptor(cudnnTensorFormat.NCHW, cudnnDataType.Double, n, c, h, w);
                                srcDiffDesc.SetTensor4dDescriptor(cudnnTensorFormat.NCHW, cudnnDataType.Double, n, c, h, w);

                                destDesc.SetTensor4dDescriptor(cudnnTensorFormat.NCHW, cudnnDataType.Double,
                                                               inputStorage.Shape.GetDimension(3), inputStorage.Shape.GetDimension(2),
                                                               inputStorage.Shape.GetDimension(1), inputStorage.Shape.GetDimension(0));
                                destDiffDesc.SetTensor4dDescriptor(cudnnTensorFormat.NCHW, cudnnDataType.Double,
                                                                   inputStorage.Shape.GetDimension(3), inputStorage.Shape.GetDimension(2),
                                                                   inputStorage.Shape.GetDimension(1), inputStorage.Shape.GetDimension(0));

                                poolingDesc.SetPooling2dDescriptor(cudnnPoolingMode.Max, cudnnNanPropagation.NotPropagateNan,
                                                                   windowHeight, windowWidth,
                                                                   verticalPad, horizontalPad, verticalStride, horizontalStride);

                                this._context.CudnnContext.PoolingBackward(poolingDesc, 1.0,
                                                                           srcDesc, outputStorage.DeviceBuffer,
                                                                           srcDiffDesc, outputGradientStorage.DeviceBuffer,
                                                                           destDesc, inputStorage.DeviceBuffer,
                                                                           0.0,
                                                                           destDiffDesc, inputGradientStorage.DeviceBuffer);
                            }

            inputGradientStorage.CopiedToDevice = true;
        }
Esempio n. 2
0
        public override void Pool(Tensor t, int filterSize, int stride, Tensor.PoolType type, int paddingX, int paddingY, Tensor result)
        {
            t.CopyToDevice();
            result.CopyToDevice();

            using (var poolingDesc = new PoolingDescriptor())
                using (var tDesc = new TensorDescriptor())
                    using (var resultDesc = new TensorDescriptor())
                    {
                        poolingDesc.SetPooling2dDescriptor(TensorPoolTypeToCuDNNPoolType(type), cudnnNanPropagation.NotPropagateNan, filterSize, filterSize, paddingX, paddingY, stride, stride);
                        tDesc.SetTensor4dDescriptor(cudnnTensorFormat.NCHW, cudnnDataType.Float, t.Shape.Dimensions[3], t.Shape.Dimensions[2], t.Shape.Dimensions[1], t.Shape.Dimensions[0]);
                        resultDesc.SetTensor4dDescriptor(cudnnTensorFormat.NCHW, cudnnDataType.Float, result.Shape.Dimensions[3], result.Shape.Dimensions[2], result.Shape.Dimensions[1], result.Shape.Dimensions[0]);

                        _CudnnContext.PoolingForward(poolingDesc, 1.0f, tDesc, t.GpuData.DeviceVar, 0.0f, resultDesc, result.GpuData.DeviceVar);
                    }
        }
Esempio n. 3
0
        public override void DoPool(Volume <double> result, int windowWidth, int windowHeight,
                                    int horizontalPad, int verticalPad, int horizontalStride, int verticalStride)
        {
            var resultStorage = result.Storage as VolumeStorage;

            if (resultStorage == null)
            {
                throw new ArgumentException($"{nameof(result)} storage should be VolumeStorage", nameof(result));
            }

            // Copy to device if not already done
            this._volumeStorage.CopyToDevice();
            resultStorage.CopyToDevice();

            // Synchro
            this._context.DefaultStream.Synchronize();

            // Relu
            using (var poolingDesc = new PoolingDescriptor())
                using (var srcDesc = new TensorDescriptor())
                    using (var resultDesc = new TensorDescriptor())
                    {
                        srcDesc.SetTensor4dDescriptor(cudnnTensorFormat.NCHW, cudnnDataType.Double,
                                                      this.Shape.GetDimension(3), this.Shape.GetDimension(2),
                                                      this.Shape.GetDimension(1), this.Shape.GetDimension(0));

                        resultDesc.SetTensor4dDescriptor(cudnnTensorFormat.NCHW, cudnnDataType.Double,
                                                         result.Shape.GetDimension(3), result.Shape.GetDimension(2),
                                                         result.Shape.GetDimension(1), result.Shape.GetDimension(0));

                        poolingDesc.SetPooling2dDescriptor(cudnnPoolingMode.Max, cudnnNanPropagation.NotPropagateNan,
                                                           windowHeight, windowWidth,
                                                           verticalPad, horizontalPad, verticalStride, horizontalStride);

                        this._context.CudnnContext.PoolingForward(poolingDesc, 1.0, srcDesc, this._volumeStorage.DeviceBuffer, 0.0,
                                                                  resultDesc, resultStorage.DeviceBuffer);
                    }

            resultStorage.CopiedToDevice = true;
        }