MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / matmul

Function matmul

imperative/python/megengine/functional/math.py:788–826  ·  view source on GitHub ↗

r"""Performs a matrix multiplication of the matrices ``inp1`` and ``inp2``. With different inputs dim, this function behaves differently: * Both 1-D tensor, simply forward to ``dot``. * Both 2-D tensor, normal matrix multiplication. * If one input tensor is 1-D, matrix vector multi

(
    inp1: Tensor,
    inp2: Tensor,
    transpose_a=False,
    transpose_b=False,
    compute_mode="default",
)

Source from the content-addressed store, hash-verified

786
787
788def matmul(
789 inp1: Tensor,
790 inp2: Tensor,
791 transpose_a=False,
792 transpose_b=False,
793 compute_mode="default",
794) -> Tensor:
795 r"""Performs a matrix multiplication of the matrices ``inp1`` and ``inp2``.
796
797 With different inputs dim, this function behaves differently:
798
799 * Both 1-D tensor, simply forward to ``dot``.
800 * Both 2-D tensor, normal matrix multiplication.
801 * If one input tensor is 1-D, matrix vector multiplication.
802 * If at least one tensor are 3-dimensional or >3-dimensional, the other tensor should have dim >= 2,
803 the batched matrix-matrix is returned, and the tensor with smaller dimension will be broadcasted.
804 For example:
805
806 * inp1: `(n, k, m)`, inp2: `(n, m, p)`, return: `(n, k, p)`
807 * inp1: `(n, k, m)`, inp2: `(m, p)`, return: `(n, k, p)`
808 * inp1: `(n, j, k, m)`, inp2: `(n, j, m, p)`, return: `(n, j, k, p)`
809
810 Args:
811 inp1: first matrix to be multiplied.
812 inp2: second matrix to be multiplied.
813
814 Returns:
815 output tensor.
816
817 Examples:
818 >>> import numpy as np
819 >>> data1 = Tensor(np.arange(0, 6, dtype=np.float32).reshape(2, 3))
820 >>> data2 = Tensor(np.arange(0, 6, dtype=np.float32).reshape(3, 2))
821 >>> out = F.matmul(data1, data2)
822 >>> out.numpy()
823 array([[10., 13.],
824 [28., 40.]], dtype=float32)
825 """
826 return _matmul(inp1, inp2, transpose_a, transpose_b, compute_mode)
827
828
829def dot(inp1: Tensor, inp2: Tensor) -> Tensor:

Callers 1

_calc_linearMethod · 0.50

Calls 1

_matmulFunction · 0.85

Tested by

no test coverage detected