megengine.functional.matmul#

matmul(inp1, inp2, transpose_a=False, transpose_b=False, compute_mode='default')[source]#

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 multiplication.

  • If at least one tensor are 3-dimensional or >3-dimensional, the other tensor should have dim >= 2, the batched matrix-matrix is returned, and the tensor with smaller dimension will be broadcasted. For example:

    • inp1: (n, k, m), inp2: (n, m, p), return: (n, k, p)

    • inp1: (n, k, m), inp2: (m, p), return: (n, k, p)

    • inp1: (n, j, k, m), inp2: (n, j, m, p), return: (n, j, k, p)

Parameters:
  • inp1 (Tensor) – first matrix to be multiplied.

  • inp2 (Tensor) – second matrix to be multiplied.

Return type:

Tensor

Returns:

output tensor.

Examples

>>> import numpy as np
>>> data1 = Tensor(np.arange(0, 6, dtype=np.float32).reshape(2, 3))
>>> data2 = Tensor(np.arange(0, 6, dtype=np.float32).reshape(3, 2))
>>> out = F.matmul(data1, data2)
>>> out.numpy()
array([[10., 13.],
       [28., 40.]], dtype=float32)