Skip to content

[mkldnn][8/N] Port LSTM form ideep to ATen - #201126

Closed
frost-intel wants to merge 3 commits into
pytorch:mainfrom
frost-intel:ideep-removal/08-port-lstm
Closed

frost-intel wants to merge 3 commits into
pytorch:mainfrom
frost-intel:ideep-removal/08-port-lstm

Conversation

@frost-intel

@frost-intel frost-intel commented Oct 16, 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 LSTM from ideep. Not all of ideep functionality is included, only what is used in ATen currently. Note that we continue to use ideep::tensor until all ops are ported over, then we will transition to the ported class.

Depends on #201055

Fake stack

Agent details (if applicable)

Developed with assistance from Claude Opus 5.5.

What changed (5 files, +460/−7):

  • detail/Lstm.{h,cpp}: the five LSTM functions ATen calls, ported line for line from ideep's lstm.hpp:
    • lstm_forward_inference
    • lstm_forward_inference_expected_weights_desc: the packed weight layout used when weights are prepacked
    • lstm_forward_training_prepare and lstm_forward_training
    • lstm_backward
  • Kept as in ideep:
    • "any" layouts for the weights, the weight reorders, buffer replacement on the outputs, the workspace argument, and the user-managed scratchpad. The packed-layout lookup keeps ideep's default scratchpad mode.
    • Backward computes gradients in f32, zero-initializes the weight and bias gradients before running (oneDNN adds into them), and copies the results back into the caller's tensors.
    • The avx2_vnni_2 bf16/f16 check on backward.
    • ideep's 8-bit-input branch (s8 weights plus the RNN quantization attributes). An 8-bit input can reach it through mkldnn_rnn_layer. ATen never passes quantization parameters, so it keeps ideep's defaults (all −1).
  • Simplified:
    • Removed parameters ATen never sets or ideep never reads: the attribute and quantization parameters, and reverse/prop_kind on training compute, where ideep ignored them.
    • ideep's lstm_forward_training::expected_weights_desc has no ATen callers and is not ported.
  • Call sites: RNN.cpp (5) and MKLDNNConversions.cpp (1, the LSTM weight prepack). No ideep::lstm_* calls remain.
  • build_variables.bzl: 1 entry.

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.
    • LSTM runs 36,864 times there, in f32 and bf16: 156 distinct lines for inference, 104 for training forward and 104 for backward.
  • Weight prepack (mkldnn._reorder_mkldnn_rnn_layer_weight). Inductor's freezing pass uses this op, and it's the only caller of expected_weights_desc. I called it for 24 configurations (f32/bf16, two sizes, both directions, three input-size settings) on builds from before and after this change:
    • The oneDNN logs are identical.
    • The serialized layout of all 48 packed weights is byte-identical.
    • Their values are bitwise identical.
  • Tests:
    • test_mkldnn.py: 92 passed, 1 skipped.
    • test_nn.py -k "lstm and cpu": 8 passed.
    • test/inductor/test_cpu_repro.py -k lstm: 514 passed. This exercises the prepacked-weight path end to end.
  • Lint: clean.

(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

@pytorch-bot

pytorch-bot Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ No Failures

As of commit d10df2b with merge base 780bf8e (image):
💚 Looks good so far! There are no failures yet. 💚

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

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 lstm_forward_inference (compute and expected_weights_desc),
lstm_forward_training (prepare and compute) and lstm_backward operators
used by ATen into aten/src/ATen/native/mkldnn/detail/Lstm.{h,cpp} and
switch the callers in RNN.cpp and MKLDNNConversions.cpp. The port is
line-for-line for the code paths ATen uses, including ideep's u8 branch;
parameters that ATen never passed or ideep ignored are dropped.

Verified with ONEDNN_VERBOSE=1 over test_mkldnn.py and the mkldnn freezing
tests (all primitive create/exec lines identical before and after), and
by comparing the output of mkldnn._reorder_mkldnn_rnn_layer_weight across
24 configurations (byte-identical packed layouts and values).
@frost-intel
frost-intel force-pushed the ideep-removal/08-port-lstm branch from 57b9fe8 to d10df2b Compare November 10, 2026 14:44
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 LSTM from ideep. Not all of ideep functionality is included, only what is used in ATen currently. Note that we continue to use `ideep::tensor` until all ops are ported over, then we will transition to the ported class.

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

> **What changed** (5 files, +464/−7):
>
> - **`detail/Lstm.{h,cpp}`:** the five LSTM functions ATen calls, ported line for line from ideep's `lstm.hpp`:
>   - `lstm_forward_inference`
>   - `lstm_forward_inference_expected_weights_desc`: the packed weight layout used when weights are prepacked
>   - `lstm_forward_training_prepare` and `lstm_forward_training`
>   - `lstm_backward`
> - **Kept as in ideep:**
>   - "any" layouts for the weights, the weight reorders, buffer replacement on the outputs, the workspace argument, and the user-managed scratchpad. The packed-layout lookup keeps ideep's default scratchpad mode.
>   - Backward computes gradients in f32, zero-initializes the weight and bias gradients before running (oneDNN adds into them), and copies the results back into the caller's tensors.
>   - The avx2_vnni_2 bf16/f16 check on backward.
>   - ideep's 8-bit-input branch (s8 weights plus the RNN quantization attributes), kept line for line. ATen cannot reach it today: every LSTM entry point builds its input with `itensor_view_from_const_dense`, which accepts only float, bfloat16 and half. ATen never passes quantization parameters, so it would keep ideep's defaults (all −1).
> - **Simplified:**
>   - Removed parameters ATen never sets or ideep never reads: the attribute and quantization parameters, and `reverse`/`prop_kind` on training `compute`, where ideep ignored them.
>   - ideep's `lstm_forward_training::expected_weights_desc` has no ATen callers and is not ported.
> - **Call sites:** `RNN.cpp` (5) and `MKLDNNConversions.cpp` (1, the LSTM weight prepack). No `ideep::lstm_*` calls remain.
> - **`build_variables.bzl`:** 1 entry.
>
> **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.
>   - LSTM runs 36,864 times there, in f32 and bf16: 156 distinct lines for inference, 104 for training forward and 104 for backward.
> - **Weight prepack (`mkldnn._reorder_mkldnn_rnn_layer_weight`).** Inductor's freezing pass uses this op, and it's the only caller of `expected_weights_desc`. I called it for 24 configurations (f32/bf16, two sizes, both directions, three input-size settings) on builds from before and after this change:
>   - The oneDNN logs are identical.
>   - The serialized layout of all 48 packed weights is byte-identical.
>   - Their values are bitwise identical.
> - **Tests:**
>   - `test_mkldnn.py`: 92 passed, 1 skipped.
>   - `test_nn.py -k "lstm and cpu"`: 8 passed.
>   - `test/inductor/test_cpu_repro.py -k lstm`: 514 passed. This exercises the prepacked-weight path end to end.
> - **Lint:** clean.
>
> (`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.)
>
> Previously submitted as #201126.

Pull Request resolved: #205714
Approved by: https://github.com/albanD
ghstack dependencies: #205709, #205710, #205711, #205712, #205713
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) open source release notes: quantization release notes category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants