private static void Conv2BackwardFilterFrame(Tensor gradOutput, Tensor gradWeight, Tensor gradBias, Tensor finput, ConvolutionDesc2d cd) { if (gradOutput is null) { throw new ArgumentNullException(nameof(gradOutput)); } if (gradWeight is null) { throw new ArgumentNullException(nameof(gradWeight)); } if (gradBias is null) { throw new ArgumentNullException(nameof(gradBias)); } if (finput is null) { throw new ArgumentNullException(nameof(finput)); } if (cd is null) { throw new ArgumentNullException(nameof(cd)); } using Tensor gradOutput2d = gradOutput.View(gradOutput.Sizes[0], gradOutput.Sizes[1] * gradOutput.Sizes[2]); using Tensor finputT = finput.Transpose(); Ops.Addmm(gradWeight, 1, gradWeight, 1, gradOutput2d, finputT); Ops.Sum(gradBias, gradOutput2d, 1); }
public override Tensor Evaluate(Tensor writeTarget) { using var s = this.src.Evaluate(null); using var m1Val = this.m1.Evaluate(null); using var m2Val = this.m2.Evaluate(null); return(Ops.Addmm(writeTarget, this.beta, s, this.alpha, m1Val, m2Val)); }
public IWeightMatrix MulAdd2(IWeightMatrix m1, IWeightMatrix m2, IWeightMatrix m3) { WeightTensor t1 = m1 as WeightTensor; WeightTensor t2 = m2 as WeightTensor; WeightTensor t3 = m3 as WeightTensor; var n = t1.Rows; var d = t2.Columns; WeightTensor res = weightTensorFactory.CreateWeightTensor(n, d, deviceId); Ops.Addmm(res.TWeight, 1.0f, t3.TWeight, 1.0f, t1.TWeight, t2.TWeight); if (this.needs_backprop) { Action backward = () => { Ops.Add(t3.TGradient, t3.TGradient, res.TGradient); var tW2 = t2.TWeight.Transpose(); Ops.Addmm(t1.TGradient, 1.0f, t1.TGradient, 1.0f, res.TGradient, tW2); var tW1 = t1.TWeight.Transpose(); Ops.Addmm(t2.TGradient, 1.0f, t2.TGradient, 1.0f, tW1, res.TGradient); tW1.Dispose(); tW2.Dispose(); }; this.backprop.Add(backward); } return(res); }
public void Conv2BackwardInput(Tensor input, Tensor gradOutput, Tensor gradInput, Tensor weight, Tensor finput, Tensor fgradInput, ConvolutionDesc2d cd) { var nOutputPlane = weight.Sizes[0]; var batchSize = input.Sizes[0]; var nInputPlane = input.Sizes[1]; var inputWidth = input.Sizes[3]; var inputHeight = input.Sizes[2]; var outputWidth = (inputWidth + 2 * cd.padW - cd.kW) / cd.dW + 1; var outputHeight = (inputHeight + 2 * cd.padH - cd.kH) / cd.dH + 1; for (long i = 0; i < batchSize; ++i) { using (var gradInput_i = gradInput.Select(0, i)) using (var gradOutput_i = gradOutput.Select(0, i)) using (var gradOutput_i2d = gradOutput_i.View(nOutputPlane, outputHeight * outputWidth)) using (var weightT = weight.Transpose()) { Ops.Addmm(fgradInput, 0, fgradInput, 1, weightT, gradOutput_i2d); im2colKernels.Col2Im(fgradInput, gradInput_i, (int)nInputPlane, (int)inputHeight, (int)inputWidth, cd.kH, cd.kW, cd.padH, cd.padW, cd.dH, cd.dW, 1, 1); } } }
public void Conv2BackwardFilter(Tensor input, Tensor gradOutput, Tensor gradWeight, Tensor gradBias, Tensor finput, Tensor fgradInput, ConvolutionDesc2d cd) { var nOutputPlane = gradWeight.Sizes[0]; var batchSize = input.Sizes[0]; var nInputPlane = input.Sizes[1]; var inputWidth = input.Sizes[3]; var inputHeight = input.Sizes[2]; var outputWidth = (inputWidth + 2 * cd.padW - cd.kW) / cd.dW + 1; var outputHeight = (inputHeight + 2 * cd.padH - cd.kH) / cd.dH + 1; for (long i = 0; i < batchSize; ++i) { using (var input_i = input.Select(0, i)) using (var gradOutput_i = gradOutput.Select(0, i)) { im2colKernels.Im2Col(input_i, finput, (int)nInputPlane, (int)inputHeight, (int)inputWidth, cd.kH, cd.kW, cd.padH, cd.padW, cd.dH, cd.dW, 1, 1); using (var gradOutput2d = gradOutput_i.View(gradOutput_i.Sizes[0], gradOutput_i.Sizes[1] * gradOutput_i.Sizes[2])) using (var finputT = finput.Transpose()) { Ops.Addmm(gradWeight, 1, gradWeight, 1, gradOutput2d, finputT); Ops.Sum(gradBias, gradOutput2d, 1); } } } }
public IWeightTensor Affine(IWeightTensor m1, IWeightTensor m2, IWeightTensor mbias) { if (m1 == null) { throw new ArgumentNullException($"m1 tensor is null"); } if (m2 == null) { throw new ArgumentNullException($"m2 tensor is null"); } if (mbias == null) { throw new ArgumentNullException($"mbias tensor is null"); } WeightTensor t1 = m1 as WeightTensor; WeightTensor t2 = m2 as WeightTensor; WeightTensor t3 = mbias as WeightTensor; var n = t1.Rows; var d = t2.Columns; WeightTensor res = m_weightTensorFactory.CreateWeightTensor(n, d, m_deviceId, name: $"{GetHashString(m1.Name, m2.Name, mbias.Name)}.Affine"); VisualizeNodes(new IWeightTensor[] { m1, m2, mbias }, res); using (var t3WExp = t3.TWeight.Expand(n, d)) { Ops.Addmm(res.TWeight, 1.0f, t3WExp, 1.0f, t1.TWeight, t2.TWeight); } if (m_needsBackprop) { Action backward = () => { res.ReleaseWeight(); using (var t3G = t3.TGradient.Expand(n, d)) { Ops.Add(t3G, t3G, res.TGradient); } using (var tW2 = t2.TWeight.Transpose()) { Ops.Addmm(t1.TGradient, 1.0f, t1.TGradient, 1.0f, res.TGradient, tW2); } using (var tW1 = t1.TWeight.Transpose()) { Ops.Addmm(t2.TGradient, 1.0f, t2.TGradient, 1.0f, tW1, res.TGradient); } res.Dispose(); }; this.m_backprop.Add(backward); } return(res); }
public IWeightTensor Mul(IWeightTensor m1, IWeightTensor m2) { WeightTensor t1 = m1 as WeightTensor; WeightTensor t2 = m2 as WeightTensor; var n = t1.Rows; var d = t2.Columns; WeightTensor res; res = m_weightTensorFactory.CreateWeightTensor(n, d, m_deviceId, name: $"{GetHashString(m1.Name, m2.Name)}.Mul"); VisualizeNodes(new IWeightTensor[] { m1, m2 }, res); Ops.Addmm(res.TWeight, 0.0f, res.TWeight, 1.0f, t1.TWeight, t2.TWeight); if (m_needsBackprop) { Action backward = () => { res.ReleaseWeight(); using (var tW2 = t2.TWeight.Transpose()) { Ops.Addmm(t1.TGradient, 1.0f, t1.TGradient, 1.0f, res.TGradient, tW2); } using (var tW1 = t1.TWeight.Transpose()) { Ops.Addmm(t2.TGradient, 1.0f, t2.TGradient, 1.0f, tW1, res.TGradient); } res.Dispose(); }; this.m_backprop.Add(backward); } return(res); }
private static void Conv2BackwardFilterFrame(Tensor gradOutput, Tensor gradWeight, Tensor gradBias, Tensor finput, ConvolutionDesc2d cd) { using var gradOutput2d = gradOutput.View(gradOutput.Sizes[0], gradOutput.Sizes[1] * gradOutput.Sizes[2]); using var finputT = finput.Transpose(); Ops.Addmm(gradWeight, 1, gradWeight, 1, gradOutput2d, finputT); Ops.Sum(gradBias, gradOutput2d, 1); }
public override Tensor Evaluate(Tensor writeTarget) { using (var s = src.Evaluate(null)) using (var m1Val = m1.Evaluate(null)) using (var m2Val = m2.Evaluate(null)) { return(Ops.Addmm(writeTarget, beta, s, alpha, m1Val, m2Val)); } }
/// <summary> /// Conv2s the backward filter frame. /// </summary> /// <param name="gradOutput">The grad output.</param> /// <param name="gradWeight">The grad weight.</param> /// <param name="gradBias">The grad bias.</param> /// <param name="finput">The finput.</param> /// <param name="cd">The cd.</param> private static void Conv2BackwardFilterFrame(NDArray gradOutput, NDArray gradWeight, NDArray gradBias, NDArray finput, ConvolutionDesc2d cd) { using (var gradOutput2d = gradOutput.View(gradOutput.Shape[0], gradOutput.Shape[1] * gradOutput.Shape[2])) using (var finputT = finput.Transpose()) { Ops.Addmm(gradWeight, 1, gradWeight, 1, gradOutput2d, finputT); Ops.Sum(gradBias, gradOutput2d, 1); } }
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); } }
private static void Conv2BackwardInputFrame(Tensor gradOutput, Tensor gradInput, Tensor weight, Tensor fgradInput, ConvolutionDesc2d cd) { using (Tensor gradOutput2d = gradOutput.View(gradOutput.Sizes[0], gradOutput.Sizes[1] * gradOutput.Sizes[2])) { Ops.Addmm(fgradInput, 0, fgradInput, 1, weight, gradOutput2d); } Ops.Fill(gradInput, 0); using (NativeWrapper.BuildTensorRefPtr(fgradInput, out IntPtr fgradInputPtr)) using (NativeWrapper.BuildTensorRefPtr(gradInput, out IntPtr gradInputPtr)) { CpuOpsNative.TS_Unfolded_Acc(fgradInputPtr, gradInputPtr, cd.kW, cd.kH, cd.dW, cd.dH, cd.padW, cd.padH, (int)gradInput.Sizes[0], (int)gradInput.Sizes[2], (int)gradInput.Sizes[1], (int)gradOutput.Sizes[2], (int)gradOutput.Sizes[1]); } }
public void Conv2Forward(Tensor input, Tensor output, Tensor weight, Tensor bias, Tensor finput, ConvolutionDesc2d cd) { var batchSize = input.Sizes[0]; var nInputPlane = input.Sizes[1]; var inputWidth = input.Sizes[3]; var inputHeight = input.Sizes[2]; var nOutputPlane = weight.Sizes[0]; var outputWidth = (inputWidth + 2 * cd.padW - cd.kW) / cd.dW + 1; var outputHeight = (inputHeight + 2 * cd.padH - cd.kH) / cd.dH + 1; for (long i = 0; i < batchSize; ++i) { using (var input_i = input.Select(0, i)) using (var output_i = output.Select(0, i)) { using (var output2d = output_i.View(nOutputPlane, outputHeight * outputWidth)) { if (bias != null) { using (var biasExp = bias.Expand(nOutputPlane, output2d.Sizes[1])) { Ops.Copy(output2d, biasExp); } } else { Ops.Fill(output_i, 0); } im2colKernels.Im2Col(input_i, finput, (int)nInputPlane, (int)inputHeight, (int)inputWidth, cd.kH, cd.kW, cd.padH, cd.padW, cd.dH, cd.dW, 1, 1); Ops.Addmm(output2d, 1, output2d, 1, weight, finput); } } } }
public IWeightTensor Mul(IWeightTensor m1, IWeightTensor m2, float alpha = 1.0f) { WeightTensor t1 = m1 as WeightTensor; WeightTensor t2 = m2 as WeightTensor; int n = t1.Rows; int d = t2.Columns; WeightTensor res; res = m_weightTensorFactory.CreateWeightTensor(n, d, m_deviceId, name: $"{GetHashString(m1.Name, m2.Name)}.Mul", graphToBind: this); VisualizeNodes(new IWeightTensor[] { m1, m2 }, res); Ops.Addmm(res.TWeight, 0.0f, res.TWeight, alpha, t1.TWeight, t2.TWeight); if (m_needsBackprop) { Action backward = () => { res.ReleaseWeight(); using (Tensor tW2 = t2.TWeight.Transpose()) { Ops.Addmm(t1.TGradient, 1.0f, t1.TGradient, alpha, res.TGradient, tW2); } using (Tensor tW1 = t1.TWeight.Transpose()) { Ops.Addmm(t2.TGradient, 1.0f, t2.TGradient, alpha, tW1, res.TGradient); } res.Dispose(); }; m_backprop.Add(backward); t1.UnbindFromComputeGraph(); t2.UnbindFromComputeGraph(); } return(res); }