Skip to content

[mkldnn][7/N] Port batchnorm and layernorm from ideep to ATen - #201083

Closed
frost-intel wants to merge 5 commits into
pytorch:mainfrom
frost-intel:ideep-removal/07-port-batchnorm-layernorm
Closed

frost-intel wants to merge 5 commits into
pytorch:mainfrom
frost-intel:ideep-removal/07-port-batchnorm-layernorm

Conversation

@frost-intel

@frost-intel frost-intel commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

Linked issue or supporting maintainer

Part of #154444

BC-breaking?

No

Summary (human written only)

Line-for-line ports of layernorm and batchnorm from ideep to ATen.

Layernorm drops an empty stub that wasn't ported from ideep.

Batchnorm includes the inference, training, and backward versions. Some args that had no effect were removed.

Depends on #201055

Fake stack

Agent details (if applicable)

Developed with assistance from Claude Opus 5.5.

What changed (6 files, +296/−5):

  • detail/BatchNorm.{h,cpp}: the three batchnorm functions ATen calls, ported line for line from ideep's batchnorm.hpp:
    • batch_normalization_forward_inference: the version that takes the running mean and variance.
    • batch_normalization_forward_training: the version without running stats. ATen updates the running stats itself.
    • batch_normalization_backward.
    • Kept: the workaround that reads src's layout through _get_unblocked_desc_if_4c_blocked() (for oneDNN issue Pooling and BN fallback to ref implementation uxlfoundation/oneDNN#588), the input/mean/variance reorders, buffer replacement on the outputs, the user-managed scratchpad, and the avx2_vnni_2 bf16/f16 check on backward.
    • The flags are fixed to use_scale | use_shift (plus use_global_stats for inference). ATen always used ideep's default flags, so ideep's fused-relu path and its workspace never ran. I also removed the parameters ideep took but ATen never set or ideep never used: flags, dst on backward, and momentum on training. In ideep's training function momentum had no effect.
    • Not ported because ATen never calls them: the inference version without stats, and the training version that updates running stats with ideep::sum.
  • detail/LayerNorm.{h,cpp}: layer_normalization_forward, ported line for line. ideep's layer_normalization_backward is an empty stub and isn't ported.
  • Call sites: Normalization.cpp (4). The two ideep::sum calls for the running stats are ported separately in the sum/binary PR.
  • build_variables.bzl: 2 entries.

Checks:

  • Same oneDNN calls before and after. I ran all of test_mkldnn.py plus test_freezing.py -k mkldnn with ONEDNN_VERBOSE=1 and compared the logged primitive creations and executions, with timings stripped. All 6,572 distinct primitive log lines are identical.
    • Batchnorm runs 87 times: 17 distinct lines for inference, 1 for training forward and 1 for backward.
    • The oneDNN logs are identical, including the layer_normalization call.
    • The outputs are bitwise identical.
    • The output matches eager mode to within 2.4e-7.
  • Tests:
    • test_mkldnn.py: 92 passed, 1 skipped.
    • Freezing tests: 4 passed, 5 skipped.
    • test_mkldnn_fusion.py: OK.
    • test_nn.py -k "batchnorm or layer_norm or layernorm": 24 passed.
  • Lint: clean.

Update: test_mkldnn_layer_norm was originally added to test/jit/test_freezing.py. Upstream
#203621 deleted that file along with the public torch.jit.script / torch.jit.freeze, so the test now
lives in test/test_mkldnn.py. It drives the frozen-graph pass through torch.jit._script.script,
torch.jit._freeze.freeze and torch._C._jit_pass_convert_frozen_ops_to_mkldnn, checks for
prim::MKLDNNLayerNorm, and compares against eager. It passes before and after this PR and executes the
oneDNN layer_normalization primitive.

(test/jit/test_freezing.py was later deleted upstream by #203621. The final A/B comparison of the whole series against main, described in #204808, re-checks these paths without it.)

cc @jgong5 @mingfeima @XiaobingSuper @sanchitintel @ashokei @jingxu10 @aditew01 @gujinghui @PenghuiCheng @jianyuh @min-jean-cho @yanbing-j @Guobing-Chen @Xia-Weiwen @snadampal

@pytorch-bot

pytorch-bot Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/201083

Note: Links to docs will display an error until the docs builds have been completed.

❌ 1 Unclassified Failure

As of commit 4e92f3f with merge base 780bf8e (image):

UNCLASSIFIED FAILURE - DrCI could not classify the following job because the workflow did not run on the merge base. The failure may be pre-existing on trunk or introduced by this PR:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorch-bot pytorch-bot Bot added module: cpu CPU specific problem (e.g., perf, algorithm) release notes: quantization release notes category labels Oct 15, 2026
@frost-intel
frost-intel force-pushed the ideep-removal/07-port-batchnorm-layernorm branch from 18be5bc to fed1c1c Compare October 29, 2026 16:04
Part of pytorch#154444.

Replace ideep type aliases with the oneDNN (or std) types they name.
Each is a plain `using` in ideep, so this is a pure spelling change
with no effect on generated code. It removes about a third of the
remaining ideep references from ATen CPU code.
Part of pytorch#154444. Move ideep::eltwise_forward/backward into aten.
Switch all CPU callers (relu, sigmoid, tanh, gelu, scalar mul,
dense GELU, JIT frozen scalar mul). Line-for-line port.
Part of pytorch#154444. Depends on pytorch#201055.

Move the ideep batch_normalization_forward_inference/forward_training/
backward and layer_normalization_forward operators used by ATen into
aten/src/ATen/native/mkldnn/detail/{BatchNorm,LayerNorm}.{h,cpp} and switch
the callers in Normalization.cpp. The port is line-for-line for the
code paths ATen uses; flags are fixed to ideep's defaults (the only values
ATen ever passed), so ideep's unused fused-relu/workspace branches and the
ignored momentum parameter are dropped.

Verified with ONEDNN_VERBOSE=1: the primitive create/exec log of
test_mkldnn.py and the mkldnn freezing tests is identical before and
after. A new test_mkldnn_layer_norm in test_freezing.py is added, since
not previously covered by tests.
Replace the kScaleShift constant with ideep's per-function pd_flags in
the batchnorm forward inference, forward training and backward ports so
they read line-for-line against ideep's batchnorm.hpp. No behavior change;
ONEDNN_VERBOSE logs are unchanged.
This test was added to test/jit/test_freezing.py earlier in this PR, but
pytorch#203621 deleted that file together with the public torch.jit.script and
torch.jit.freeze. The MKLDNN layer norm port is only reachable through
the frozen-graph pass (prim::MKLDNNLayerNorm), so move the test here and
drive the pass through the private entry points that remain.
@pytorch-bot pytorch-bot Bot added the module: mkldnn Related to Intel IDEEP or oneDNN (a.k.a. mkldnn) integration label Nov 13, 2026
pytorchmergebot pushed a commit that referenced this pull request Nov 19, 2026
…1053)

## Linked issue or supporting maintainer
Part of #154444

## BC-breaking?
No

## Summary (human written only)
See #154444 for ideep removal discussion.
Replace ideep type aliases with the oneDNN or std types they name. Each is a plain `using` in ideep, so this is a pure spelling change with no effect on generated code. It removes about a third of the remaining ideep references from ATen CPU code.

#### Fake Stack
* #201052
* -> #201053
* Port ops:
* #201055
* #201059
* #201062
* #201080
* #201083
* #201126
* #201194
* #201224
* #201229
* #202547
* #202611
* #202622
* #203645
* #204617
* #204615
* Port classes and utils:
* #204801
* #204805
* #204806
* #204807
* Final removal:
* #204808

## Agent details (if applicable)

Developed with assistance from Claude Opus 5.5
Summary:
> Replace ideep type aliases with the oneDNN (or std) types they name:
> data_type, dims, format_tag/tag, prop_kind, algorithm, scale_t,
> zero_point_t and exec_args. Each is a plain `using` in ideep, so this is
> a pure spelling change with no effect on generated code. It removes
> about a third of the remaining ideep references from ATen CPU code
> without touching any ideep-specific behavior.

Pull Request resolved: #201053
Approved by: https://github.com/albanD, https://github.com/malfet
@frost-intel

Copy link
Copy Markdown
Collaborator Author

Moved to ghstack:
#205709

pytorchmergebot pushed a commit that referenced this pull request Nov 20, 2026
## Linked issue or supporting maintainer
Part of #154444

## BC-breaking?
No

## Summary (human written only)
Line-for-line ports of layernorm and batchnorm from ideep to ATen.

Layernorm drops an empty stub that wasn't ported from ideep.

Batchnorm includes the inference, training, and backward versions. Some args that had no effect were removed.

## Agent details (if applicable)
Developed with assistance from Claude Opus 5.5.

> **What changed** (7 files, +318/−5):
>
> - **`detail/BatchNorm.{h,cpp}`:** the three batchnorm functions ATen calls, ported line for line from ideep's `batchnorm.hpp`:
>   - `batch_normalization_forward_inference`: the version that takes the running mean and variance.
>   - `batch_normalization_forward_training`: the version without running stats. ATen updates the running stats itself.
>   - `batch_normalization_backward`.
>   - Kept: the workaround that reads `src`'s layout through `_get_unblocked_desc_if_4c_blocked()` (for oneDNN issue uxlfoundation/oneDNN#588), the input/mean/variance reorders, buffer replacement on the outputs, the user-managed scratchpad, and the avx2_vnni_2 bf16/f16 check on backward.
>   - The flags are fixed to `use_scale | use_shift` (plus `use_global_stats` for inference). ATen always used ideep's default flags, so ideep's fused-relu path and its workspace never ran. I also removed the parameters ideep took but ATen never set or ideep never used: `flags`, `dst` on backward, and `momentum` on training. In ideep's training function `momentum` had no effect.
>   - Not ported because ATen never calls them: the inference version without stats, and the training version that updates running stats with `ideep::sum`.
> - **`detail/LayerNorm.{h,cpp}`:** `layer_normalization_forward`, ported line for line. ideep's `layer_normalization_backward` is an empty stub and isn't ported.
> - **Call sites:** `Normalization.cpp` (4). The two `ideep::sum` calls for the running stats are ported separately in the sum/binary PR.
> - **`build_variables.bzl`:** 2 entries.
>
> **Checks:**
>
> - **Same oneDNN calls before and after.** I ran all of `test_mkldnn.py` plus `test_freezing.py -k mkldnn` with `ONEDNN_VERBOSE=1` and compared the logged primitive creations and executions, with timings stripped. All 6,572 distinct primitive log lines are identical.
>   - Batchnorm runs 87 times: 17 distinct lines for inference, 1 for training forward and 1 for backward.
>   - The oneDNN logs are identical, including the `layer_normalization` call.
>   - The outputs are bitwise identical.
>   - The output matches eager mode to within 2.4e-7.
> - **Tests:**
>   - `test_mkldnn.py`: 92 passed, 1 skipped.
>   - Freezing tests: 4 passed, 5 skipped.
>   - `test_mkldnn_fusion.py`: OK.
>   - `test_nn.py -k "batchnorm or layer_norm or layernorm"`: 24 passed.
> - **Lint:** clean.
>
> **Update:** `test_mkldnn_layer_norm` was originally added to `test/jit/test_freezing.py`. Upstream
> #203621 deleted that file along with the public `torch.jit.script` / `torch.jit.freeze`, so the test now
> lives in `test/test_mkldnn.py`. It drives the frozen-graph pass through `torch.jit._script.script`,
> `torch.jit._freeze.freeze` and `torch._C._jit_pass_convert_frozen_ops_to_mkldnn`, checks for
> `prim::MKLDNNLayerNorm`, and compares against eager with a random LayerNorm weight and bias. It passes before and after this PR and executes the
> oneDNN `layer_normalization` primitive.
>
> (`test/jit/test_freezing.py` and `test/test_mkldnn_fusion.py` were later deleted upstream by #203621. The final A/B comparison of the whole series against `main`, described in #204808, re-checks these paths without it.)
>
> Previously submitted as #201083.

Pull Request resolved: #205713
Approved by: https://github.com/albanD
ghstack dependencies: #205709, #205710, #205711, #205712
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module: cpu CPU specific problem (e.g., perf, algorithm) module: mkldnn Related to Intel IDEEP or oneDNN (a.k.a. mkldnn) integration open source release notes: quantization release notes category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants