| Files | Last commit | Last update |
|---|---|---|
FuseLayerNormBroadcastToPass
1. Function Description
FuseLayerNormBroadcastToPass is a custom graph compilation fusion pass for Ascend CANN (registered as FuseLayerNormBroadcastToPass, executed at the kAfterInferShape stage). It fuses LayerNorm together with its surrounding BroadcastTo, Add, Adds, and Rsqrt operators into a single AddLayerNorm or LayerNormV3 operator:
Before fusion (AddLayerNorm mode):
x1 ──┐
├──→ Add ──→ Identity ──┐
x2 ──┘ │
▼
gamma ──→ BroadcastTo ──→ LayerNorm ──→ y
beta ──→ BroadcastTo ────→ ↑
└── output2 → Adds → Rsqrt (removed in inference)
Before fusion (LayerNormV3 mode):
x ──→ LayerNorm ←── BroadcastTo ←── gamma
↑
BroadcastTo ←── beta
After fusion:
x1 ──┐
├──→ AddLayerNorm / LayerNormV3 ──→ y
x2 ──┘ (gamma, beta connected directly; broadcast handled inside the operator)
Key capabilities:
- Automatic fusion mode selection: if an Add exists on the x input path (
Add → Identity → LayerNormand the BroadcastTo shape input is associated with that Add), it fuses intoAddLayerNorm; otherwise it fuses intoLayerNormV3 - Removes the
BroadcastTobefore gamma and beta; the broadcast is performed inside the fused operator - In inference scenarios, removes the useless
Adds → Rsqrtchain on LayerNorm output 2 and the edges of output 1 (mean) - Supports both static shapes (BroadcastTo shape input is a Const) and dynamic shapes (shape input is
Shape → Identity → Add) - Inherits attributes such as
epsilonandbegin_norm_axisfrom the original LayerNorm (begin_params_axisis set to the last dim of x)
Supported versions: Ascend910_93, Ascend950.
2. Build and Deployment
2.1 Build
Depends on the CANN environment (the CANN install path is specified by the ASCEND_HOME_PATH environment variable; if unset, it defaults to /usr/local/Ascend/ascend-toolkit/latest):
mkdir build && cd build && cmake .. && make
The build artifact is libfuse_layernorm_broadcastto_pass.so (located in the build directory).
2.2 Deployment
Copy the build artifact to the custom fusion pass directory to take effect:
cp libfuse_layernorm_broadcastto_pass.so ${ASCEND_HOME_PATH}/opp/vendors/xx/custom_fusion_passes/
where xx is the custom vendor path (a custom directory under vendors).
3. Fusion Limitations
- Only applicable to inference scenarios, i.e., where LayerNorm outputs 2 and 3 (mean, rstd) have no practical use
- Only supports gamma and beta being 1-dimensional with the same shape, and equal to the last dim of the x input
- The Add on the x input path is optional: if present, it fuses into
AddLayerNorm; otherwise it fuses intoLayerNormV3