Esempio n. 1
0
        protected override void DoDot2D(Tensor b, int hA, int wA, int hB, int wB, Shape resultShape, Tensor c)
        {
            int m = hA;
            int n = wB;
            int k = wA;

            int alpha = 1;
            int beta  = 0;

            int lda = k;
            int ldb = n;
            int ldc = n;

            Blas.gemm(Layout.RowMajor, Trans.No, Trans.No, m, n, k, alpha, Storage.Data, lda, b.Storage.Data, ldb, beta, c.Storage.Data, ldc);
        }
Esempio n. 2
0
        protected override void DoDot2D(Tensor b, Tensor c)
        {
            int m = Height;
            int n = b.Width;
            int k = Width;

            int alpha = 1;
            int beta  = 0;

            int lda = k;
            int ldb = n;
            int ldc = n;

            //MKL row major DGEMM
            Blas.gemm(Layout.RowMajor, Trans.No, Trans.No, m, n, k, alpha, Storage.Data, lda, b.Storage.Data, ldb, beta, c.Storage.Data, ldc);
        }
Esempio n. 3
0
 /// <summary>
 /// Compute the outer product of two vectors: result[i, j] = alpha * a[i] * b[j] + beta * result[i, j]
 /// </summary>
 /// <param name="a"></param>
 /// <param name="b"></param>
 /// <param name="result"></param>
 /// <param name="alpha"></param>
 /// <param name="beta"></param>
 /// <returns></returns>
 public static Array <Real> Outer(this Array <Real> a, Array <Real> b, Array <Real> result = null, Real alpha = 1, Real beta = 0)
 {
     if (a.Shape.Length != 1 && (a.Shape.Length != 2 || a.Shape[1] != 1))
     {
         a = a.Reshape(a.Size);
     }
     if (b.Shape.Length != 1 && (b.Shape.Length != 2 || b.Shape[1] != 1))
     {
         b = b.Reshape(b.Size);
     }
     if (result == null)
     {
         result = new Array <Real>(a.Shape[0], b.Shape[0]);
     }
     else
     {
         if (result.Shape.Length != 2)
         {
             throw new RankException("objects are not aligned");
         }
         if (result.Shape[0] != a.Shape[0])
         {
             throw new RankException("objects are not aligned");
         }
         if (result.Shape[1] != b.Shape[0])
         {
             throw new RankException("objects are not aligned");
         }
         // TODO: check strides ?
     }
     Blas.gemm(Order.RowMajor, Transpose.NoTrans, Transpose.Trans, result.Shape[0], result.Shape[1], 1,
               alpha,
               a.Values, a.Offset, a.Stride[0],
               b.Values, b.Offset, b.Stride[0],
               beta,
               result.Values, result.Offset, result.Stride[0]);
     return(result);
 }
Esempio n. 4
0
        /// <summary>
        /// Matrix multiplication.
        /// Returns: alpha * dot(a, b) + beta * result
        /// Ie with default value: dot(a, b)
        /// </summary>
        /// <remarks>
        /// For 2-D arrays it is equivalent to matrix multiplication,
        /// and for 1-D arrays to inner product of vectors (without complex conjugation).
        /// For N dimensions it is a sum product over the last axis of a and the second-to-last of b:
        /// dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])
        /// `TensorDot` provides more control for N-dim array multiplication.
        /// </remarks>
        public static Array <Real> Dot(this Array <Real> a, Array <Real> b, Array <Real> result = null, Real alpha = 1, Real beta = 0, bool transA = false, bool transB = false)
        {
            if (transA)
            {
                if (a.Shape.Length == 1)
                {
                    if (b.Shape.Length == 1)
                    {
                        if (transB)
                        {
                            return(a.Dot(b, result, alpha, beta));
                        }
                        else
                        {
                            return(a.VectorDot(b));
                        }
                    }
                    if (b.Shape.Length != 2)
                    {
                        throw new NotImplementedException();
                    }
                    return(b.Dot(a, result, alpha, beta, transA: !transB, transB: false));
                }
                else
                {
                    a = a.T;    // TODO: optimize => avoid creating new shape, stride, ...
                    //if (b.Shape.Length == 1 && !transB) b = b.Reshape(1, b.Shape[0]);
                }
                transA = false;
            }

            if (transB)
            {
                if (b.Shape.Length == 1)
                {
                    if (a.Shape.Length == 1)
                    {
                        if (transA)
                        {
                            throw new NotImplementedException();
                        }
                        return(a.Outer(b, result: result, alpha: alpha, beta: 0));
                    }
                    throw new NotImplementedException();
                    //if (a.Shape.Length != 2) throw new NotImplementedException();
                    //if (a.IsTransposed())
                    //{
                    //    a = a.T;
                    //    transA = !transA;
                    //}
                    //result = new Array<Real>(transA ? a.Shape[1] : a.Shape[0], b.Shape[0]);   // TODO: result != null
                    //Blas.gemm(Order.RowMajor, transA ? Transpose.Trans : Transpose.NoTrans, Transpose.NoTrans,
                    //    result.Shape[0], result.Shape[1], 1,
                    //    alpha,
                    //    a.Values, a.Offset, a.Stride[0],
                    //    b.Values, b.Offset, b.Stride[0],
                    //    beta,
                    //    result.Values, result.Offset, result.Stride[0]);
                    //return result;
                }
                else
                {
                    b = b.T;    // TODO: optimize => avoid creating new shape, stride, ...
                }
                transB = false;
            }

            // TODO: check alpha & beta
            if (a.Shape.Length == 0)
            {
                return(b.Scale(a.Values[a.Offset]));
            }
            if (b.Shape.Length == 0)
            {
                return(a.Scale(b.Values[b.Offset]));
            }
            if (a.Shape.Length == 1)     // vector x tensor
            {
                if (b.Shape.Length == 1) // vector x vector
                {
                    if (a.Shape[0] != b.Shape[0])
                    {
                        throw AssertArray.BadRank("objects are not aligned: [{0}] dot [{1}]", a.Shape[0], b.Shape[0]);
                    }
                    if (result == null)
                    {
                        result = new Array <Real>();
                    }
                    else
                    {
                        if (result.Shape.Length != 0)
                        {
                            throw AssertArray.BadRank("objects are not aligned");
                        }
                    }

                    result.Values[result.Offset] = beta * result.Values[result.Offset] + alpha * Blas.dot(a.Shape[0], a.Values, a.Offset, a.Stride[0], b.Values, b.Offset, b.Stride[0]);
                    return(result);
                }
                else if (b.Shape.Length == 2)   // vector x matrix
                {
                    if (a.Shape[0] != b.Shape[0])
                    {
                        throw new RankException("objects are not aligned");
                    }
                    if (result == null)
                    {
                        result = new Array <Real>(b.Shape[1]);
                    }
                    else
                    {
                        if (result.Shape.Length != 1)
                        {
                            throw new RankException("objects are not aligned");
                        }
                        if (result.Shape[0] != b.Shape[1])
                        {
                            throw new RankException("objects are not aligned");
                        }
                    }

                    // dgemv computes matrix x vector => result = M.T.dot(v.T).T
                    transB = !transB;
                    if (b.IsTransposed())
                    {
                        transB = !transB;
                        b      = b.T;
                    }
                    // y:= alpha * A' * x + beta * y
                    Blas.gemv(Order.RowMajor, transB ? Transpose.Trans : Transpose.NoTrans, b.Shape[0], b.Shape[1], alpha,
                              b.Values, b.Offset, b.Stride[0],
                              a.Values, a.Offset, a.Stride[0],
                              beta,
                              result.Values, result.Offset, result.Stride[0]);
                    return(result);
                }
                else if (b.Shape.Length == 3) // vector x tensor3
                {
                    // TODO: beta ?
                    if (a.Shape[0] != b.Shape[1])
                    {
                        throw new RankException("objects are not aligned");
                    }
                    if (result == null)
                    {
                        result = new Array <Real>(b.Shape[0], b.Shape[2]);
                    }
                    else
                    {
                        if (result.Shape[0] != b.Shape[0])
                        {
                            throw new RankException("objects are not aligned");
                        }
                        if (result.Shape[1] != b.Shape[2])
                        {
                            throw new RankException("objects are not aligned");
                        }
                    }

                    var offsetk = b.Offset;
                    var k_0     = result.Offset;
                    for (var k = 0; k < result.Shape[0]; k++)       // result.Shape[0] == b.Shape[0]
                    {
                        var offsetm = offsetk;
                        var k_m     = k_0;
                        for (var m = 0; m < result.Shape[1]; m++)                                                                               // result.Shape[1] == b.Shape[2]
                        {
                            result.Values[k_m] = alpha * Blas.dot(a.Shape[0], a.Values, a.Offset, a.Stride[0], b.Values, offsetm, b.Stride[1]); // a.Shape[axis] == b.Shape[1];
                            offsetm           += b.Stride[2];
                            k_m += result.Stride[1];
                        }
                        offsetk += b.Stride[0];
                        k_0     += result.Stride[0];
                    }

                    return(result);
                }
                throw new NotImplementedException();
            }
            else if (b.Shape.Length == 1)   // tensor x vector
            {
                if (a.Shape.Length == 2)    // matrix x vector
                {
                    if (a.Shape[1] != b.Shape[0])
                    {
                        throw new RankException("objects are not aligned");
                    }
                    if (result == null)
                    {
                        result = new Array <Real>(a.Shape[0]);
                    }
                    else
                    {
                        if (result.Shape.Length != b.Shape.Length)
                        {
                            throw new RankException("objects are not aligned");
                        }
                        if (result.Shape[0] != a.Shape[0])
                        {
                            throw new RankException("objects are not aligned");
                        }
                        // TODO: check strides
                    }
                    if ((a.Flags & Flags.Transposed) != 0)
                    {
                        transA = !transA;
                        a      = a.T;
                    }
                    // y:= A*x + beta*y
                    if (a.Stride[1] == 1)
                    {
                        Blas.gemv(Order.RowMajor, transA ? Transpose.Trans : Transpose.NoTrans, a.Shape[0], a.Shape[1], alpha,
                                  a.Values, a.Offset, a.Stride[0],
                                  b.Values, b.Offset, b.Stride[0],
                                  beta,
                                  result.Values, result.Offset, result.Stride[0]);
                    }
                    else
                    {
                        // y *= beta
                        if (beta != 1)
                        {
                            result.Scale(beta, result: result);
                        }

                        int offB = b.Offset;
                        int offA = a.Offset;
                        for (int j = 0; j < b.Shape[0]; ++j)
                        {
                            Blas.axpy(a.Shape[0], alpha * b.Values[offB],
                                      a.Values, offA, a.Stride[0],
                                      result.Values, result.Offset, result.Stride[0]);
                            offB += b.Stride[0];
                            offA += a.Stride[1];
                        }
                    }

                    return(result);
                }
                else if (a.Shape.Length == 3)    // tensor x vector = mat
                {
                    if (a.Shape[2] != b.Shape[0])
                    {
                        throw new RankException("objects are not aligned");
                    }
                    if (result == null)
                    {
                        result = new Array <Real>(a.Shape[0], a.Shape[1]);
                    }
                    else if (result.Shape[0] != a.Shape[0] || result.Shape[1] != a.Shape[1])
                    {
                        throw new RankException("objects are not aligned");
                    }

                    var offsetk   = a.Offset;
                    var offsetRes = result.Offset;

                    for (var k = 0; k < result.Shape[0]; k++)
                    {
                        var offsetj = offsetk;
                        for (var j = 0; j < result.Shape[1]; j++)
                        {
                            result.Values[offsetRes] = alpha * Blas.dot(a.Shape[2], a.Values, offsetj, a.Stride[2], b.Values, b.Offset, b.Stride[0]);
                            offsetj   += a.Stride[1];
                            offsetRes += result.Stride[1];
                        }
                        offsetk += a.Stride[0];
                    }
                    return(result);
                }
                throw new NotImplementedException();
            }
            else if (a.Shape.Length == 2 && b.Shape.Length == 2)    // matrix x matrix
            {
                if (a.Shape[1] != b.Shape[0])
                {
                    throw AssertArray.BadRank("objects are not aligned: [{0}, {1}] dot [{2}, {3}]", a.Shape[0], a.Shape[1], b.Shape[0], b.Shape[1]);
                }
                if (result == null)
                {
                    result = new Array <Real>(a.Shape[0], b.Shape[1]);
                }
                else
                {
                    if (result.Shape[0] != a.Shape[0] || result.Shape[1] != b.Shape[1])
                    {
                        throw AssertArray.BadRank("result target have incorrect shape: [{0}, {1}] instead of [{2}, {3}].", result.Shape[0], result.Shape[1], a.Shape[0], b.Shape[1]);
                    }
                    // TODO: check strides
                }
                var m = a.Shape[0];
                var n = b.Shape[1];
                var k = a.Shape[1];
                if ((a.Flags & Flags.Transposed) != 0)
                {
                    transA = !transA;
                    a      = a.T;
                }
                if ((b.Flags & Flags.Transposed) != 0)
                {
                    transB = !transB;
                    b      = b.T;
                }
                // C:= alpha * op(A) * op(B) + beta * C
                Blas.gemm(Order.RowMajor, transA ? Transpose.Trans : Transpose.NoTrans, transB ? Transpose.Trans : Transpose.NoTrans,
                          m, n, k,
                          alpha,
                          a.Values, a.Offset, a.Stride[0],
                          b.Values, b.Offset, b.Stride[0],
                          beta,
                          result.Values, result.Offset, result.Stride[0]);
                return(result);
            }
            // tensor x tensor
            throw new NotImplementedException();
        }