public static void PoolingBackward(DNNPoolingDesc desc, Tensor x, Tensor y, Tensor dx, Tensor dy) { using (var dnn = CudaHelpers.TSContextForTensor(x).DNNForTensor(x)) { var poolingDesc = new PoolingDescriptor(); poolingDesc.SetPoolingNdDescriptor((cudnnPoolingMode)desc.Mode, cudnnNanPropagation.PropagateNan, desc.WindowDims.Length, desc.WindowDims, desc.Padding, desc.Strides); using (var xPtr = GetDeviceVar(x)) using (var yPtr = GetDeviceVar(y)) using (var dxPtr = GetDeviceVar(dx)) using (var dyPtr = GetDeviceVar(dy)) using (var xDesc = GetDescriptor(x)) using (var yDesc = GetDescriptor(y)) using (var dxDesc = GetDescriptor(dx)) using (var dyDesc = GetDescriptor(dy)) { // Note: ManagedCUDA argument names may be slightly misleading (src refers to 'y' here, and dest to 'x') dnn.Value.PoolingBackward(poolingDesc, 1, yDesc, yPtr, dyDesc, dyPtr, xDesc, xPtr, 0, dxDesc, dxPtr); } } }
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; }
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); } }
public static void PoolingForward(DNNPoolingDesc desc, Tensor x, Tensor y) { using (var dnn = CudaHelpers.TSContextForTensor(x).DNNForTensor(x)) { var poolingDesc = new PoolingDescriptor(); poolingDesc.SetPoolingNdDescriptor((cudnnPoolingMode)desc.Mode, cudnnNanPropagation.PropagateNan, desc.WindowDims.Length, desc.WindowDims, desc.Padding, desc.Strides); using (var xPtr = GetDeviceVar(x)) using (var yPtr = GetDeviceVar(y)) using (var xDesc = GetDescriptor(x)) using (var yDesc = GetDescriptor(y)) { dnn.Value.PoolingForward(poolingDesc, 1, xDesc, xPtr, 0, yDesc, yPtr); } } }
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; }
public Pooling2D(Variable <T> data, PoolingMode mode, int kernelH, int kernelW, int strideH, int strideW) { Descriptor = new PoolingDescriptor(); Descriptor.Set2D(mode, NanPropagation.NOT_PROPAGATE_NAN, kernelH, kernelW, 0, 0, strideH, strideW); var dataType = Dnn.DataTypeOf <T>(); var dataDesc = new TensorDescriptor(); dataDesc.Set4D(dataType, TensorFormat.CUDNN_TENSOR_NCHW, 10, (int)data.Shape[1], (int)data.Shape[2], (int)data.Shape[3]); int n, c, h, w; Descriptor.Get2dForwardOutputDim(dataDesc, out n, out c, out h, out w); Data = data; Output = Variable <T>(PartialShape.Create(-1, c, h, w)); AddInput(Data); AddOutput(Output); dataDesc.Dispose(); }