mhc_post

Applicable Products

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

Function

  • Description:

    Implements the forward computation of the MHC Post component for the post-processing stage of multi-layer residual connections in Transformer models. This operator fuses the residual matrix transformation and the output state projection into a single computation, avoiding the overhead of multiple independent operator calls.

  • Formula:

    • The core formula of the MHC Post operator is:

      xl+1=(Hlres)T⋅xl+hlout⋅Htpostx_{l+1} = (H_{l}^{res})^{T} \cdot x_{l} + h_{l}^{out} \cdot H_{t}^{post}

      Where:

      • (Hlres)T⋅xl(H_{l}^{res})^{T} \cdot x_{l} denotes the transpose matrix multiplication of the residual matrix on the input xlx_{l}. For the ii-th row of the output (corresponding to the ii-th head), the computation is:

        xl+1[i]=∑j=0n−1Hlres[j,i]⋅xl[j]x_{l+1}[i] = \sum_{j=0}^{n-1} H_{l}^{res}[j, i] \cdot x_{l}[j]

        That is, the HlresH_{l}^{res} matrix is multiplied with xlx_{l} in transposed form. Hlres[j,i]H_{l}^{res}[j, i] is a scalar. Scalar multiplication is performed on the jj-th row of xlx_{l}, and the result is accumulated into the ii-th row of the output.

      • hlout⋅Htposth_{l}^{out} \cdot H_{t}^{post} denotes the element-wise multiplication and broadcast of the output state hlouth_{l}^{out} with the post-processing weight HtpostH_{t}^{post}. For the ii-th head:

        xl+1[i]+=Htpost[i]⋅hloutx_{l+1}[i] += H_{t}^{post}[i] \cdot h_{l}^{out}

        That is, Htpost[i]H_{t}^{post}[i] is a scalar. Scalar multiplication is performed on the entire row of hlouth_{l}^{out}, and the result is added to the ii-th row of the output.

    • The complete computation is:

      xl+1[i,:]=Htpost[i]⋅hlout[:]+∑j=0n−1Hlres[j,i]⋅xl[j,:]x_{l+1}[i, :] = H_{t}^{post}[i] \cdot h_{l}^{out}[:] + \sum_{j=0}^{n-1} H_{l}^{res}[j, i] \cdot x_{l}[j, :]

      Here, xlx_{l} denotes the parameter x, HlresH_{l}^{res} denotes the parameter h_res, hlouth_{l}^{out} denotes the parameter h_out, HtpostH_{t}^{post} denotes the parameter h_post, and xl+1x_{l+1} denotes the output y.

    Note

    The input supports two dimension formats: BSND (4D) and TND (3D). B (Batch) denotes the batch size, S (Seq-Length) denotes the sequence length, T denotes the sum of sequence lengths across all batches (T=B×ST = B \times S), n denotes the number of heads, and D denotes the hidden dimension size of each head (headdim).

Prototype

mhc_post(x, h_res, h_out, h_post) -> Tensor

Parameters

Parameter Parameter Type Optional/Required Description Data Type Dimension (shape)
x Tensor Required Input token feature of the current layer, corresponding to xl in the formula. bfloat16, float16
  • (B, S, n, D)
  • (T, n, D)
h_res Tensor Required Residual connection matrix, corresponding to Hlres in the formula. float32
  • (B, S, n, n)
  • (T, n, n)
h_out Tensor Required Output state of the previous layer, corresponding to Hlout in the formula. bfloat16, float16
  • (B, S, D)
  • (T, D)
h_post Tensor Required Post-processing weight matrix, corresponding to Htpost in the formula. float32
  • (B, S, n)
  • (T, n)

Return Value

Parameter Parameter Type Optional/Required Description Data Type Dimension (shape)
y Tensor Required MHC Post computation output, corresponding to xl+1 in the formula. The data type remains consistent with the input `x`, and the shape remains consistent with the input `x`. bfloat16, float16
  • (B, S, n, D)
  • (T, n, D)

Constraints

  • This API supports use in training and inference use cases.

  • This API supports single-operator mode calls.

  • Data type constraints:

    • The data types of x and h_out must be the same.
    • The data type of the output y remains consistent with x.
  • Dimension constraints:

    • The dimension of h_res must match the dimension format of x: (B, S, n, n) for 4D, and (T, n, n) for 3D.
  • Shape consistency constraints:

    • In the 4D (BSND) format:
      • The (B, S) dimension of h_res must be consistent with the (B, S) dimension of x. The last two dimensions of h_res are (n, n), where n is consistent with the third dimension of x.
      • The (B, S) dimension of h_out must be consistent with the (B, S) dimension of x. The D dimension of h_out must be consistent with the D dimension of x.
      • The (B, S) dimension of h_post must be consistent with the (B, S) dimension of x. The n dimension of h_post must be consistent with the n dimension of x.
    • In the 3D (TND) format:
      • The T dimension of h_res must be consistent with the T dimension of x. The last two dimensions are (n, n).
      • The T dimension of h_out must be consistent with the T dimension of x. The D dimension must be consistent with the D dimension of x.
      • The T dimension of h_post must be consistent with the T dimension of x. The n dimension must be consistent with the n dimension of x.
  • The value of each dimension in the shape of all input tensors must be a positive number (greater than 0).

Deterministic Computation

Defaults to a deterministic computation.

Example

  • Single-operator mode call:

    import torch
    import torch_npu
    from cann_ops_transformer.ops import mhc_post
    
    B = 2
    S = 8
    n = 4
    D = 128
    
    x = torch.randn(B, S, n, D, dtype=torch.bfloat16).npu()
    h_res = torch.randn(B, S, n, n, dtype=torch.float32).npu()
    h_out = torch.randn(B, S, D, dtype=torch.bfloat16).npu()
    h_post = torch.randn(B, S, n, dtype=torch.float32).npu()
    
    y = mhc_post(x, h_res, h_out, h_post)
    print(f"output shape: {y.shape}")