mhc_post
Applicable Products
Ascend 950PR/Ascend 950DT : Not supportedAtlas A3 training products/Atlas A3 inference products : SupportedAtlas A2 training products/Atlas A2 inference products : SupportedAtlas 200I/500 A2 inference products : Not supportedAtlas inference products : Not supportedAtlas 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:
Where:
denotes the transpose matrix multiplication of the residual matrix on the input . For the -th row of the output (corresponding to the -th head), the computation is:
That is, the matrix is multiplied with in transposed form. is a scalar. Scalar multiplication is performed on the -th row of , and the result is accumulated into the -th row of the output.
denotes the element-wise multiplication and broadcast of the output state with the post-processing weight . For the -th head:
That is, is a scalar. Scalar multiplication is performed on the entire row of , and the result is added to the -th row of the output.
The complete computation is:
Here, denotes the parameter
x, denotes the parameterh_res, denotes the parameterh_out, denotes the parameterh_post, and denotes the outputy.
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 (), 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) -> TensorParameters
| 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 |
|
| h_res | Tensor | Required | Residual connection matrix, corresponding to Hlres in the formula. | float32 |
|
| h_out | Tensor | Required | Output state of the previous layer, corresponding to Hlout in the formula. | bfloat16, float16 |
|
| h_post | Tensor | Required | Post-processing weight matrix, corresponding to Htpost in the formula. | float32 |
|
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 |
|
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
xandh_outmust be the same. - The data type of the output
yremains consistent withx.
- The data types of
Dimension constraints:
- The dimension of
h_resmust match the dimension format ofx: (B, S, n, n) for 4D, and (T, n, n) for 3D.
- The dimension of
Shape consistency constraints:
- In the 4D (BSND) format:
- The (B, S) dimension of
h_resmust be consistent with the (B, S) dimension ofx. The last two dimensions ofh_resare (n, n), where n is consistent with the third dimension ofx. - The (B, S) dimension of
h_outmust be consistent with the (B, S) dimension ofx. The D dimension ofh_outmust be consistent with the D dimension ofx. - The (B, S) dimension of
h_postmust be consistent with the (B, S) dimension ofx. The n dimension ofh_postmust be consistent with the n dimension ofx.
- The (B, S) dimension of
- In the 3D (TND) format:
- The T dimension of
h_resmust be consistent with the T dimension ofx. The last two dimensions are (n, n). - The T dimension of
h_outmust be consistent with the T dimension ofx. The D dimension must be consistent with the D dimension ofx. - The T dimension of
h_postmust be consistent with the T dimension ofx. The n dimension must be consistent with the n dimension ofx.
- The T dimension of
- In the 4D (BSND) format:
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}")