public Tensor Addmm(Tensor result, float beta, Tensor src, float alpha, Tensor m1, Tensor m2) { //Console.WriteLine($"src0 = {src.Sizes[0]}, src1 = {src.Sizes[1]}, m1_0 = {m1.Sizes[0]}, m1_1 = {m1.Sizes[1]}, m2_0 = {m2.Sizes[0]}, m2_1 = {m2.Sizes[1]}"); // ReSharper disable once ArrangeRedundantParentheses if (src.ElementType != m1.ElementType || src.ElementType != m2.ElementType || (result != null && result.ElementType != src.ElementType)) { throw new InvalidOperationException("All tensors must have the same element type"); } if (result != null && !(result.Storage is CpuStorage)) { throw new ArgumentException("result must be a CPU tensor", nameof(result)); } if (!(m1.Storage is CpuStorage)) { throw new ArgumentException("m1 must be a CPU tensor", nameof(m1)); } if (!(m2.Storage is CpuStorage)) { throw new ArgumentException("m2 must be a CPU tensor", nameof(m2)); } if (src.DimensionCount != 2) { throw new ArgumentException("src must be a matrix", nameof(src)); } if (m1.DimensionCount != 2) { throw new ArgumentException("m1 must be a matrix", nameof(m1)); } if (m2.DimensionCount != 2) { throw new ArgumentException("m2 must be a matrix", nameof(m2)); } if (src.Sizes[0] != m1.Sizes[0] || src.Sizes[1] != m2.Sizes[1] || m1.Sizes[1] != m2.Sizes[0]) { throw new InvalidOperationException("Size mismatch"); } var writeTarget = TensorResultBuilder.GetWriteTarget(result, src, true, src.Sizes); if (writeTarget != src) { Ops.Copy(writeTarget, src); } MatrixMultiplication.Gemm(alpha, m1, m2, beta, writeTarget); return(writeTarget); }
public Tensor Addmm(Tensor result, float beta, Tensor src, float alpha, Tensor m1, Tensor m2) { if (src.ElementType != m1.ElementType || src.ElementType != m2.ElementType || (result != null && result.ElementType != src.ElementType)) { throw new InvalidOperationException("All tensors must have the same element type"); } if (result != null && !(result.Storage is CpuStorage)) { throw new ArgumentException("result must be a CPU tensor", "result"); } if (!(m1.Storage is CpuStorage)) { throw new ArgumentException("m1 must be a CPU tensor", "m1"); } if (!(m2.Storage is CpuStorage)) { throw new ArgumentException("m2 must be a CPU tensor", "m2"); } if (src.DimensionCount != 2) { throw new ArgumentException("src must be a matrix", "src"); } if (m1.DimensionCount != 2) { throw new ArgumentException("m1 must be a matrix", "m1"); } if (m2.DimensionCount != 2) { throw new ArgumentException("m2 must be a matrix", "m2"); } if (src.Sizes[0] != m1.Sizes[0] || src.Sizes[1] != m2.Sizes[1] || m1.Sizes[1] != m2.Sizes[0]) { throw new InvalidOperationException("Size mismatch"); } var writeTarget = TensorResultBuilder.GetWriteTarget(result, src, true, src.Sizes); if (writeTarget != src) { Ops.Copy(writeTarget, src); } MatrixMultiplication.Gemm(alpha, m1, m2, beta, writeTarget); return(writeTarget); }
public Tensor Dot(Tensor result, Tensor lhs, Tensor rhs) { if (lhs.DimensionCount == 1 && rhs.DimensionCount == 1) { return(MatrixMultiplication.Dot(result, lhs, rhs)); } if (lhs.DimensionCount == 2 && rhs.DimensionCount == 1) { return(MatrixMultiplication.Mul_M_V(result, lhs, rhs)); } if (lhs.DimensionCount == 2 && rhs.DimensionCount == 2) { return(MatrixMultiplication.Mul_M_M(result, lhs, rhs)); } throw new NotSupportedException(string.Format("Multiplication of {0}D with {1}D tensor is not supported")); }
public Tensor AddmmBatch(Tensor result, float beta, Tensor src, float alpha, Tensor m1, Tensor m2) { // ReSharper disable once ArrangeRedundantParentheses if (src.ElementType != m1.ElementType || src.ElementType != m2.ElementType || (result != null && result.ElementType != src.ElementType)) { throw new InvalidOperationException("All tensors must have the same element type"); } if (result != null && !(result.Storage is CpuStorage)) { throw new ArgumentException("result must be a CPU tensor", nameof(result)); } if (!(m1.Storage is CpuStorage)) { throw new ArgumentException("m1 must be a CPU tensor", nameof(m1)); } if (!(m2.Storage is CpuStorage)) { throw new ArgumentException("m2 must be a CPU tensor", nameof(m2)); } if (src.DimensionCount != 3) { throw new ArgumentException("src must be a matrix", nameof(src)); } if (m1.DimensionCount != 3) { throw new ArgumentException("m1 must be a matrix", nameof(m1)); } if (m2.DimensionCount != 3) { throw new ArgumentException("m2 must be a matrix", nameof(m2)); } if (src.Sizes[1] != m1.Sizes[1] || src.Sizes[2] != m2.Sizes[2] || m1.Sizes[2] != m2.Sizes[1]) { throw new InvalidOperationException($"Size mismatch, srcSize0 = {src.Sizes[0]}, m1Size0 = {m1.Sizes[0]}, srcSize1 = {src.Sizes[1]}, m2Size1 = {m2.Sizes[1]}, m1Size1 = '{m1.Sizes[1]}', m2Size0 = '{m2.Sizes[0]}'"); } var writeTarget = TensorResultBuilder.GetWriteTarget(result, src, true, src.Sizes); if (writeTarget != src) { Ops.Copy(writeTarget, src); } var batchSize = (int)src.Sizes[0]; for (var i = 0; i < batchSize; i++) { var a = m1.Select(0, i); // m1.Narrow(0, i, 1).View(m1.Sizes[1], m1.Sizes[2]); var b = m2.Select(0, i); // m2.Narrow(0, i, 1).View(m2.Sizes[1], m2.Sizes[2]); var r = writeTarget.Select(0, i); // writeTarget.Narrow(0, i, 1).View(writeTarget.Sizes[1], writeTarget.Sizes[2]); MatrixMultiplication.Gemm(alpha, a, b, beta, r); } //MatrixMultiplication.Gemm(alpha, m1, m2, beta, writeTarget); return(writeTarget); }
public static Tensor Addmm(Tensor result, float beta, Tensor src, float alpha, Tensor m1, Tensor m2) { try { if (src.ElementType != m1.ElementType || src.ElementType != m2.ElementType || (result != null && result.ElementType != src.ElementType)) { throw new InvalidOperationException("All tensors must have the same element type"); } if (result != null && !(result.Storage is CpuStorage)) { throw new ArgumentException("result must be a CPU tensor", nameof(result)); } if (!(m1.Storage is CpuStorage)) { throw new ArgumentException("m1 must be a CPU tensor", nameof(m1)); } if (!(m2.Storage is CpuStorage)) { throw new ArgumentException("m2 must be a CPU tensor", nameof(m2)); } if (src.DimensionCount != 2) { throw new ArgumentException("src must be a matrix", nameof(src)); } if (m1.DimensionCount != 2) { throw new ArgumentException("m1 must be a matrix", nameof(m1)); } if (m2.DimensionCount != 2) { throw new ArgumentException("m2 must be a matrix", nameof(m2)); } if (src.Sizes[0] != m1.Sizes[0] || src.Sizes[1] != m2.Sizes[1] || m1.Sizes[1] != m2.Sizes[0]) { throw new InvalidOperationException("Size mismatch"); } Tensor writeTarget = TensorResultBuilder.GetWriteTarget(result, src, false, src.Sizes); if (writeTarget != src) { Ops.Copy(writeTarget, src); } MatrixMultiplication.Gemm(alpha, m1, m2, beta, writeTarget); return(writeTarget); } catch (Exception err) { Logger.WriteLine(Logger.Level.err, $"Exception = '{err.Message}', Call stack = '{err.StackTrace}'"); throw; } }