private static NDArray ViewReplace(NDArray old, params long[] sizes) { var result = old.View(sizes); old.Dispose(); return(result); }
public override NDArray Forward(NDArray input, ModelMode mode) { if (activation != null) { activation.Dispose(); } activation = input.View(resultSize); lastInputSize = input.Shape; return(activation); }
public static void TestCreate() { NDArray ndCPU = new NDArray(new Shape(3, 2, 5), Context.Cpu()); ndCPU.SetValue(1.0f); ndCPU.WaitToWrite(); Assert.AreEqual(ndCPU.At(1, 1, 1), 1.0f, "NDArray TestSetValue"); ndCPU.Plus(2.0f); ndCPU.WaitToWrite(); Assert.AreEqual(ndCPU.At(1, 1, 1), 3.0f, "NDArray TestPlus"); ndCPU.Minus(1.0f); ndCPU.WaitToWrite(); Assert.AreEqual(ndCPU.At(1, 1, 1), 2.0f, "NDArray TestMinus"); ndCPU.Mul(2.0f); ndCPU.WaitToWrite(); Assert.AreEqual(ndCPU.At(1, 1, 1), 4.0f, "NDArray TestMul"); ndCPU.Dispose(); }
// Computes c := alpha * a * b + beta * c /// <summary> /// Gemms the specified context. /// </summary> /// <param name="context">The context.</param> /// <param name="alpha">The alpha.</param> /// <param name="a">a.</param> /// <param name="b">The b.</param> /// <param name="beta">The beta.</param> /// <param name="c">The c.</param> /// <exception cref="InvalidOperationException">Size mismatch</exception> public static void Gemm(TSCudaContext context, float alpha, NDArray a, NDArray b, float beta, NDArray c) { if (a.Shape[0] != c.Shape[0] || b.Shape[1] != c.Shape[1] || a.Shape[1] != b.Shape[0]) { throw new InvalidOperationException("Size mismatch"); } BlasOp aOp = default(BlasOp); BlasOp bOp = default(BlasOp); bool copyC = false; NDArray aClone = null; NDArray bClone = null; NDArray cClone = null; if (c.Strides[0] == 1 && c.Strides[1] != 0) { // If c is contiguous in dimension 0 (column-major) aClone = a.CopyRef(); bClone = b.CopyRef(); cClone = c.CopyRef(); } else if (c.Strides[1] == 1 && c.Strides[0] != 0) { // If c is contiguous in dimension 1 (row-major) // using (a * b)' == b' * a' // we can pass row-major matrices to BLAS functions that expect column-major by swapping A and B, // and transposing all 3 matrices cClone = c.IntTranspose(); aClone = b.IntTranspose(); // Note swap of a and b bClone = a.IntTranspose(); } else { var cNew = new NDArray(c.Allocator, c.ElementType, c.Shape[1], c.Shape[0]); cClone = cNew.IntTranspose(); Ops.Copy(cClone, c); cNew.Dispose(); copyC = true; aClone = a.CopyRef(); bClone = b.CopyRef(); } try { if (aClone.Strides[0] == 1 && aClone.Strides[1] != 0) { // If a is contiguous in dimension 0 (column-major) aOp = BlasOp.NonTranspose; } else if (aClone.Strides[1] == 1 && aClone.Strides[0] != 0) { aOp = BlasOp.Transpose; var aNew = aClone.IntTranspose(); aClone.Dispose(); aClone = aNew; } else { var aNew = new NDArray(aClone.Allocator, aClone.ElementType, aClone.Shape[1], aClone.Shape[0]); var aClone2 = aNew.IntTranspose(); Ops.Copy(aClone2, aClone); aClone.Dispose(); aClone = aClone2; aNew.Dispose(); } if (bClone.Strides[0] == 1 && bClone.Strides[1] != 0) { // If a is contiguous in dimension 0 (column-major) bOp = BlasOp.NonTranspose; } else if (bClone.Strides[1] == 1 && bClone.Strides[0] != 0) { bOp = BlasOp.Transpose; var bNew = bClone.IntTranspose(); bClone.Dispose(); bClone = bNew; } else { var bNew = new NDArray(bClone.Allocator, bClone.ElementType, bClone.Shape[1], bClone.Shape[0]); var bClone2 = bNew.IntTranspose(); Ops.Copy(bClone2, bClone); bClone.Dispose(); bClone = bClone2; bNew.Dispose(); } GemmOp(context, aOp, bOp, alpha, aClone, bClone, beta, cClone); if (copyC) { Ops.Copy(c, cClone); } } finally { aClone.Dispose(); bClone.Dispose(); cClone.Dispose(); } }