fused_matmul

Applicable Products

  • Ascend 950PR&950DT products: Supported
  • Atlas A3 products: Not supported
  • Atlas A2 products: Not supported
  • Atlas 200I/500 A2 inference products: Not supported
  • Atlas inference products: Not supported
  • Atlas training products: Not supported

Function

  • Description: fuses matrix multiplication with general vector computation. The underlying implementation encapsulates aclnnFusedMatmulV2.

  • Formula:

    $$ y = OP((x @ x2 + bias), x3) $$

    The computations supported by fused_op_type are as follows:

    • "": $y = x @ x2 + bias$.
    • "add": $y = (x @ x2 + bias) + x3$.
    • "mul": $y = (x @ x2 + bias) * x3$.
    • "gelu_erf": $y = gelu\_erf(x @ x2)$.
    • "gelu_tanh": $y = gelu\_tanh(x @ x2)$.
    • "relu": $y = relu(x @ x2 + bias)$.
    • "16cast32": $y = cast\_float32(x @ x2 + bias)$.

    When fused_op_type="add" and alpha or beta is not 1:

    $$ y = alpha * (x @ x2) + beta * x3 $$

    This use case does not support bias. The operator internally completes the computation in scale_add fusion mode.

Prototype

cann_ops_nn.fused_matmul(
    x,
    x2,
    *,
    bias=None,
    x3=None,
    alpha=None,
    beta=None,
    fused_op_type="",
) -> Tensor

Parameters

NameTypeRequired/OptionalDescriptionData TypeDimension (Shape)
xTensorRequiredFirst input matrix of the matrix multiplication, with the last two dimensions (M, K).torch.float16, torch.bfloat16, torch.float32Supports 2-6 dimensions when fused_op_type is "" or "relu"; 2-3 dimensions when it is "add" or "mul"; 2 dimensions for other values.
x2TensorRequiredSecond input matrix of the matrix multiplication, with the last two dimensions (K, N). The data type and the length of dimension K must be the same as those of x.Same as xSupports 2-6 dimensions when fused_op_type is "" or "relu"; 2-3 dimensions when it is "add" or "mul"; 2 dimensions for other values.
biasTensorOptionalBias of the matrix multiplication. Takes effect only when fused_op_type is "", "16cast32", "relu", "add", or "mul".torch.float16, torch.bfloat16, torch.float321-2 dimensions
x3TensorOptionalInput matrix of the fused operation. Required when fused_op_type is "add" or "mul"; must be None in other use cases.Same as x2-3 dimensions
alphafloatOptionalScaling coefficient of the matrix multiplication result. Defaults to 1.0 when None is passed in; the value passed in is converted to FP32 internally by the API.--
betafloatOptionalScaling coefficient of x3. Defaults to 1.0 when None is passed in; the value passed in is converted to FP32 internally by the API.--
fused_op_typestrOptionalFusion mode. Supported values are "", "16cast32", "add", "mul", "gelu_erf", "gelu_tanh", and "relu". The default value is "". "scale_add" is reserved for internal operator use only and does not support user input.--

Return Values

OutputOutput TypeDescriptionData TypeDimension (Shape)
yTensorOutput of the fused matrix multiplication.torch.float32 when fused_op_type is "16cast32", otherwise the same as x(..., M, N)

Constraints

  • This API currently supports calls in single-operator mode.
  • x, x2, bias, and x3 must be NPU tensors. None can be passed in for optional tensors.
  • x and x2 must have the same data type, and their shapes must satisfy the matrix multiplication relationship. In multi-dimensional use cases, the batch dimensions of x and x2 must be the same, and broadcasting along the batch axis is not supported.
  • When x is torch.float16 or torch.bfloat16, the data type of bias must be the same as that of x or torch.float32. When x is torch.float32, bias must be torch.float32.
  • When fused_op_type is "add" or "mul", the data type of x3 must be the same as that of x.
  • When fused_op_type is "add" or "mul", in BMM (3D) use cases, x3 supports 2-3 dimensions. A 2D x3 can be broadcast as a matrix for a 3D output, and the batch axis of a 3D x3 must be the same as that of y or 1.
  • bias cannot be passed in when fused_op_type is "gelu_erf" or "gelu_tanh".
  • When alpha or beta is not 1, only the 3D non-transposed use case with fused_op_type="add" is supported. Broadcasting of bias and the batch axis is not supported. x, x2, x3, and y must be of the same data type, either torch.float16 or torch.bfloat16.
  • The string length of fused_op_type cannot exceed 100.
  • torch_npu.npu.matmul.cube_math_type has the highest priority. If the value is not None, the API directly uses the Cube computation mode set by it, and torch.npu.matmul.allow_hf32 does not take effect. If the value is None, the API selects USE_HF32 or KEEP_DTYPE based on torch.npu.matmul.allow_hf32.

Deterministic Computation

Defaults to a deterministic implementation.

Example

  • Single-operator mode call (eager)

    import torch
    import torch_npu
    import cann_ops_nn
    
    x = torch.randn((32, 21, 9), dtype=torch.bfloat16).npu()
    x2 = torch.randn((32, 9, 16), dtype=torch.bfloat16).npu()
    x3 = torch.randn((32, 21, 16), dtype=torch.bfloat16).npu()
    
    y = cann_ops_nn.fused_matmul(
        x,
        x2,
        x3=x3,
        alpha=1.5,
        beta=0.5,
        fused_op_type="add",
    )
    print(y.shape, y.dtype)