# tensorplay.functional.matmul Source: https://www.tensorplay.cn/docs/generated/tensorplay.functional.matmul.html ```python tensorplay.functional.matmul(input, other, *, out=None) → Tensor ``` Matrix product of two tensors. The behavior depends on the dimensionality of the tensors as follows: - If both tensors are 1-dimensional, the dot product (scalar) is returned. - If both arguments are 2-dimensional, the matrix-matrix product is returned. - If the first argument is 1-dimensional and the second argument is 2-dimensional, a 1 is prepended to its dimension for the purpose of the matrix multiply. After the matrix multiply, the prepended dimension is removed. - If the first argument is 2-dimensional and the second argument is 1-dimensional, the matrix-vector product is returned. - If both arguments are at least 1-dimensional and at least one argument is N-dimensional (where N > 2), then a batched matrix multiply is returned. If the first argument is 1-dimensional, a 1 is prepended to its dimension for the purpose of the batched matrix multiply and removed after. If the second argument is 1-dimensional, a 1 is appended to its dimension for the purpose of the batched matrix multiply and removed after. The first N-2 dimensions of each argument, the batch dimensions, are [broadcast](/docs/notes/broadcasting.html#broadcasting-semantics) (and thus must be broadcastable). The last 2, the matrix dimensions, are handled as in the matrix-matrix product. For example, if input is a $(j \times 1 \times n \times m)$ tensor and other is a $(k \times m \times p)$ tensor, the batch dimensions are $(j \times 1)$ and $(k)$, and the matrix dimensions are $(n \times m)$ and $(m \times p)$. out will be a $(j \times k \times n \times p)$ tensor. This operation has support for arguments with sparse layouts. In particular the matrix-matrix (both arguments 2-dimensional) supports sparse arguments with the same restrictions as tensorplay.mm() > **Warning** > > Sparse support is a beta feature and some layout(s)/dtype/device combinations may not be supported, or may not have autograd support. If you notice missing functionality please open a feature request. This operator supports [TensorFloat32](/docs/notes/numerical_accuracy.html#id6). On certain ROCm devices, when using float16 inputs this module will use [different precision](/docs/notes/numerical_accuracy.html#fp16-on-mi200) for backward. > **Note** > > The 1-dimensional dot product version of this function does not support an out parameter. Parameters: - input ([Tensor](/docs/generated/tensorplay.Tensor.html#tensorplay.Tensor)) – the first tensor to be multiplied - other ([Tensor](/docs/generated/tensorplay.Tensor.html#tensorplay.Tensor)) – the second tensor to be multiplied Keyword Arguments: out ([Tensor](/docs/generated/tensorplay.Tensor.html#tensorplay.Tensor), optional) – the output tensor. Example: ``` >>> # vector x vector >>> tensor1 = tensorplay.randn(3) >>> tensor2 = tensorplay.randn(3) >>> tensorplay.matmul(tensor1, tensor2).size() tensorplay.Size([]) >>> # matrix x vector >>> tensor1 = tensorplay.randn(3, 4) >>> tensor2 = tensorplay.randn(4) >>> tensorplay.matmul(tensor1, tensor2).size() tensorplay.Size([3]) >>> # batched matrix x broadcasted vector >>> tensor1 = tensorplay.randn(10, 3, 4) >>> tensor2 = tensorplay.randn(4) >>> tensorplay.matmul(tensor1, tensor2).size() tensorplay.Size([10, 3]) >>> # batched matrix x batched matrix >>> tensor1 = tensorplay.randn(10, 3, 4) >>> tensor2 = tensorplay.randn(10, 4, 5) >>> tensorplay.matmul(tensor1, tensor2).size() tensorplay.Size([10, 3, 5]) >>> # batched matrix x broadcasted matrix >>> tensor1 = tensorplay.randn(10, 3, 4) >>> tensor2 = tensorplay.randn(4, 5) >>> tensorplay.matmul(tensor1, tensor2).size() tensorplay.Size([10, 3, 5]) ```