mhc_pre_sinkhorn
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:
Based on a series of computations, obtains the and projection matrices of the hidden layer in the MHC architecture and the input matrix of the Attention or MLP layer. Performs Sinkhorn iterative normalization on the matrix to obtain the doubly stochastic matrix . Supports outputting intermediate computation results for backward gradient computation.
Formula:
With as the input, the Sinkhorn transform performs iterations. During the iterations, intermediate normalization results and summation results are generated. The final output is from the last iteration as the transform result.
First iteration (initialization):
The -th iteration ():
Final output:
Prototype
mhc_pre_sinkhorn(x, phi, alpha, bias, hcMult, numIters, hcEps, normEps, outFlag) -> (Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor)Parameters
| Parameter | Parameter Type | Optional/Required | Description | Data Type | Dimension (shape) |
|---|---|---|---|---|---|
| x | Tensor | Required | MHC layer input data. Corresponds to x in the formula. | bfloat16 | (B, S, N, C) |
| phi | Tensor | Required | MHC parameter matrix. Corresponds to ψpre, ψpost, and ψres in the formula. Split along the N axis when used, into (N, N*C), (N, N*C), and (N*N, N*C). | float32 | (N*N+2*N, N*C) |
| alpha | Tensor | Required | Scaling parameter of MHC. Contains three elements corresponding to the scaling coefficients of Hpre, Hpost, and Hres. Corresponds to αpre, αpost, and αres in the corresponding formula. | float32 | (3) |
| bias | Tensor | Required | Bias parameter of MHC. Contains 2N+N² elements. Corresponds to βpre, βpost, and βres in the corresponding formula, which correspond to N, N, and N*N respectively. | float32 | (2N+N²) |
| hcMult | int | Required | Number of residual streams, that is, the HC dimension size. | - | - |
| numIters | int | Required | Number of Sinkhorn algorithm iterations. | - | - |
| hcEps | float | Optional | Eps parameter after sigmoid of Hpre. Corresponds to εhc in the formula. Defaults to 1e-6. | - | - |
| normEps | float | Optional | Anti-division-by-zero parameter of RmsNorm. Corresponds to εnorm in the corresponding formula. Default value: 1e-6. | - | - |
Output
| Parameter | Parameter Type | Optional/Required | Description | Data Type | Dimension (shape) |
|---|---|---|---|---|---|
| hin | Tensor | Required | Output h_in, used as the input of the Attention/MLP layer. Corresponds to hin in the formula. | bfloat16 | (B, S, C) |
| hPost | Tensor | Required | Output h_post transformation matrix of MHC. Corresponds to Hpost in the formula. | float32 | (B, S, N) |
| hRes | Tensor | Required | Output MHC h_res transformation matrix (the doubly stochastic matrix after Sinkhorn normalization). Corresponds to Hres in the corresponding formula. | float32 | (B, S, N*N) |
Constraints
This API supports training and inference use cases.
This API supports single-operator mode calls.
Specification constraints:
Specification Item Specification Description numIters 20 If the number of iterations exceeds this range, an invalid parameter error is returned. N 4 Only 4 is supported. C [1280, 1920, 2560] Only these three values are supported. Parameter constraints:
- Empty tensor not supported for x.
- Empty tensor not supported for phi.
- Empty tensor not supported for alpha.
- Empty tensor not supported for bias.
Deterministic Computation
Defaults to a deterministic computation.
Example
Single-operator mode call:
import torch import torch_npu from cann_ops_transformer.ops import mhc_pre_sinkhorn B = 1 S = 1024 N = 4 C = 1280 x = torch.randn(B, S, N, C, dtype=torch.bfloat16).npu() phi = torch.randn(N * N + 2 * N, N * C, dtype=torch.float32).npu() alpha = torch.randn(3, dtype=torch.float32).npu() bias = torch.randn(N * N + 2 * N, dtype=torch.float32).npu() hcMult = 4 numIters = 20 hcEps = 1e-6 normEps = 1e-6 outFlag = True # Set to True when backward propagation is required. hin, hPost, hRes = mhc_pre_sinkhorn( x, phi, alpha, bias, hcMult, numIters, hcEps, normEps, outFlag )