Skip to content

[mkldnn][21/N] Port ideep::tensor, engine and stream into ATen - #206276

Open
frost-intel wants to merge 5 commits into
gh/frost-intel/19/basefrom
gh/frost-intel/19/head
Open

frost-intel wants to merge 5 commits into
gh/frost-intel/19/basefrom
gh/frost-intel/19/head

Conversation

@frost-intel

@frost-intel frost-intel commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Stack from ghstack (oldest at bottom):

Linked issue or supporting maintainer

Part of #154444

BC-breaking?

No

Summary (human written only)

Ports tensor, engine, and stream from ideep to ATen, as well as replacing aliases.

After this PR, ATen no longer calls any ideep code.

Agent details (if applicable)

Developed with assistance from Claude Opus 5.5.

Part of #154444 (remove the ideep submodule).

This ports ideep's tensor (with its nested desc), engine, stream and allocator into aten/src/ATen/native/mkldnn/detail/Memory.h, as at::native::mkldnn::{MkldnnMemory, Engine, IStream}. They replace the aliases added earlier in this series. The code is ported line-for-line from ideep's tensor.hpp, abstract_types.hpp and allocators.hpp, and ideep's comments are kept. After this PR, ATen no longer calls any ideep code. The only thing left is the #include, which the next PR removes along with the submodule.

Changes

  • detail/Memory.h:
    • utils::allocator;
    • Engine, a subclass of dnnl::engine with cpu_engine() and the allocator hooks;
    • IStream::default_stream();
    • MkldnnMemory, a subclass of dnnl::memory, with its nested desc.
  • detail/Attr.h: no longer includes ideep, and operator ideep::attr_t is removed (nothing needs to convert back to ideep anymore). Adds the get_scales / has_scales_for / get_zero_points / has_zero_points_for helpers that the ported tensor code uses.
  • detail/IdeepUtils.h: adds utils::array_cmp.
  • IDeepRegistration.cpp: defines Engine::cpu_engine() and registers the c10 CPU allocator with it, together with the MKLDNN_* -> DNNL_* environment-variable translation. This replaces the setup that ideep's ideep_pin_singletons.hpp did before.

Deviations from ideep

  • Members of tensor and desc that ATen cannot reach are dropped: the descriptor typedef; desc::is_iohw, is_limited_blockable, is_nchw and to_dims; and get_descriptor, dup_desc, dup_descriptor, reinit_like (both overloads), get_public_format_dims, reorder_from, extract_submemory (both overloads), insert_submemory, need_reorder, get_hash and resize.
  • IDEEP_ENFORCE on oneDNN C API calls becomes dnnl::error::wrap_c_api under #ifndef NDEBUG, which matches ideep's behavior in release builds.
  • The loop index in feed_from, and in array_cmp, is now size_t instead of int, so they compile under -Wsign-compare -Werror. In-tree code is checked with those warnings, while ideep was compiled as a system header.
  • Engine::cpu_engine() is defined out of line in IDeepRegistration.cpp and marked TORCH_API, replacing ideep's IDEEP_EXPORT declaration and its definition in ideep_pin_singletons.hpp. This keeps a single engine instance across shared libraries, as before.
  • ideep's GPU engine path is not ported. ATen only uses the CPU engine here; XPU has its own oneDNN integration.

Testing

No behavior change:

  • oneDNN verbose primitive logs are identical before and after for test_mkldnn, quantized conv, conv-transpose and linear, and matmul, as are the outputs of targeted probe scripts.
  • Primitive-cache miss counts are identical at the default LRU_CACHE_CAPACITY and at LRU_CACHE_CAPACITY=1.
  • to_mkldnn buffers are still allocated through c10's CPU allocator (they show up in the memory profiler), and MKLDNN_VERBOSE=1 still turns on oneDNN verbose output.
  • The memory-descriptor blob returned by mkldnn::_get_mkldnn_serialized_md is unchanged apart from uninitialized struct padding. That padding varies from process to process on main too, and will be reported in a separate issue.
  • Compiles cleanly with -Wall -Wextra -Wsign-compare -Werror in release and debug builds.

These measurements were taken when this PR was first submitted. Since then the series has only had review changes (rewritten comments, removed dead code and unused includes, a typo rename); none change behavior. The rebuilt stack passes -Wall -Wextra -Wsign-compare -Werror per commit, and test_mkldnn, the mkldnn conv tests and the quantized conv/linear tests were rerun at the tip.

Exported symbols

libtorch_cpu.so no longer exports ideep::engine::cpu_engine() and ideep::engine::gpu_engine(). Three
at::native helpers that took ideep types now take MkldnnMemory / MkldnnMemory::desc, so their mangled names change:
itensor_view_from_dense, itensor_view_from_const_dense and new_with_itensor_mkldnn. These are internal
ATen functions, and ideep headers were never shipped in torch/include, so out-of-tree code could not use them
through public headers.

Previously submitted as #204807.

Differential Revision: D135688229

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ No Failures

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

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

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@frost-intel frost-intel added the topic: not user facing topic category label Nov 25, 2026

@albanD albanD left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Starting merge as part of PR stack under #206630

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

## BC-breaking?
No

## Summary (human written only)
Removes the ideep submodule, including `#include` statements, references in comments, build setup, and related files.

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

> Part of #154444 (remove the ideep submodule).
>
> ATen no longer calls into ideep: its operators, tensor, engine, stream, attributes, primitive cache and utilities now live in `aten/src/ATen/native/mkldnn/detail/`. This PR pins oneDNN as a direct submodule, `third_party/oneDNN`, drops the last ideep includes, and deletes `third_party/ideep`. The pin was #201052; maintainers asked for it to land last, so it is part of this PR.
>
> ### oneDNN pin
> - `third_party/oneDNN` is added at `a190d4ab` (v3.14.1), the same commit ideep's nested `mkl-dnn` pins today, so the compiled oneDNN is unchanged. `FindMKLDNN.cmake` builds the CPU library and oneDNN Graph from it. The XPU build is unchanged.
> - `FindMKLDNN.cmake` drops a cached `MKLDNN_INCLUDE_DIR` / `LLGA_INCLUDE_DIR` only when it points into `third_party/ideep`, which older build directories cached. A value passed with `-D` is kept. Its submodule-init fallback is removed; `cmake/PreBuildSteps.cmake` already initializes every `.gitmodules` path.
> - oneDNN's licenses ship from `third_party/oneDNN` (`pyproject.toml`, license audit manifest). OWNERSHIP/CODEOWNERS list `/third_party/oneDNN`; `labeler.yml` and `merge_rules.yaml` drop the nonexistent `third_party/mkl-dnn.BUILD`; comments in `jit/codegen/onednn` point at `third_party/oneDNN`.
>
> ### Changes
> - The 10 remaining `#include <ideep...>` lines are removed.
> - ideep's version gates become oneDNN version gates: `IDEEP_VERSION_*` -> `DNNL_VERSION_*`, and `IDEEP_PREREQ(a, b, c, r)` -> `DNNL_PREREQ(a, b, c)`. `IDEEP_PREREQ` also checked ideep's own API revision, which is 4, and every call site asked for revision 4 or lower. So that check was always true, and each gate takes the same branch as before.
> - `detail/IdeepUtils.h` takes over ideep's `<omp.h>` include and its `omp_get_max_threads()` fallback for builds without OpenMP. The primitive cache keys use it.
> - `FindMKLDNN.cmake` no longer looks for ideep headers, and no longer defines `IDEEP_USE_MKL`, which only ideep read.
> - ideep is removed from `.gitmodules`, the `pyproject.toml` license-files list, the license audit manifest, `labeler.yml`, `merge_rules.yaml` and OWNERSHIP/CODEOWNERS; `third_party/oneDNN` takes its place.
> - `DNNL_PREREQ` moves from `MKLDNNCommon.h` to a new `detail/Version.h` that includes only `<oneapi/dnnl/dnnl_version.h>`. `CPUBlas.cpp` uses it for its oneDNN version gates without pulling the mkldnn headers into the core BLAS code.
>
> - Keeps ideep's MIT license next to the ported code as `aten/src/ATen/native/mkldnn/detail/LICENSE.ideep`, listed in `pyproject.toml` license-files and the license audit manifest, and adds that directory to the audit's discovery globs.
>
> ### Deviation
> - In `qconv.cpp` and `qconv_prepack.cpp`, the `#elif IDEEP_PREREQ(3, 1, 0, 0)` branches are removed. They targeted ideep API revision 0, which used reciprocal weight scales (`1 / scale`). That revision was replaced by revision 1, and these branches could not be compiled in with the bundled ideep (revision 4). Keeping them under a plain oneDNN version check would be wrong: if one were ever selected, every int8 conv would get inverted weight scales. The gates that depend only on the oneDNN version (< 3.1, 3.5 vs 3.6+, 3.9+, 3.13+) are kept as `DNNL_PREREQ` checks. The removed revision-0 branch in `qconv.cpp` carried a TODO (leslie) about removing the weight-scale reciprocal and two copies of the scales; it goes with that branch. The revision-1 branch that remains already passes the scales without the reciprocal.
> - The unreachable `TORCH_CHECK(false, "Unexpected IDeep version ...")` messages now say "oneDNN".
> - `test_mkldnn.py::test_mkldnn_layer_norm` and its `FileCheck` import are removed. It only exercised the TorchScript frozen-graph pass, and TorchScript is being deleted.
> - Leftover ideep mentions are updated: the unsupported-dtype error and two comments in `MKLDNNConversions.cpp`, the `rnn_kind` reference in `torch/_decomp/decompositions.py` (now `detail/IdeepUtils.h`), and an outdated ideep-version comment in `Version.cpp`.
>
> ### Notes for reviewers
> - PyTorch has no system-oneDNN build path: `FindMKLDNN.cmake` always builds `third_party/oneDNN`, so there is no separate minimum-version check to enforce.
> - Existing checkouts keep an untracked `third_party/ideep` directory after updating. It is ignored by the build and the license audit; to remove it: `rm -rf third_party/ideep`, plus `.git/modules/third_party/ideep` in the git directory.
>
> ### Testing
> This PR:
> - Builds from a fresh CMake configure with `third_party/ideep` absent. No compile command references ideep, and `libtorch_cpu.so` defines no `ideep::` symbols.
> - 21/N, the PR before this one, builds from scratch with oneDNN still taken from ideep's nested copy and passes `test_mkldnn.py`, so nothing earlier in the series needs the pin.
> - Compiles cleanly with `-Wall -Wextra -Wsign-compare -Werror` in release and debug builds. The license audit and lintrunner are clean.
> - Upstream wheels never shipped ideep headers in `torch/include`, so C++ extensions are not affected.
>
> Whole series, compared with the `main` commit it is based on: both builds were made with identical settings (only the commit differs) and tested on an Intel Xeon 6960P.
> - **Numerics:** 202 fixed-seed outputs of oneDNN-backed ops are bitwise identical. They cover mkldnn tensors; fp32/bf16/fp16 convolution and backward; LSTM; matmul with bf16/tf32 fp32-math modes; the Inductor `mkldnn::*_pointwise` and `onednn::q*_pointwise` ops; quantized linear, conv and conv-transpose; and `torch.compile` with freezing. The oneDNN primitives they run are identical too.
> - **Primitive cache:** the number of oneDNN primitive creations is identical (465 vs 465) for a workload that changes shapes, scales and thread counts, at the default `LRU_CACHE_CAPACITY` and at `LRU_CACHE_CAPACITY=1`.
> - **Tests:** per-test outcomes and executed oneDNN primitives are identical across 13 suite runs (about 1,250 tests). The suites are `test_mkldnn`, `nn/test_convolution -k mkldnn`, `quantization/core/test_quantized_op`, bf16/fp16 `test_linalg`, the CPU LSTM tests in `test_nn`, `inductor/test_mkldnn_pattern_matcher`, `inductor/test_cpu_repro` (conv/linear) and the mkldnn tests in `test_aot_inductor`. `test_mkldnn` and `test_quantized_op` were also run with `ONEDNN_MAX_CPU_ISA` set to `AVX2` and to `AVX512_CORE`. The failures that do occur happen identically on `main` and are tracked separately.
> - **Performance:** all 16 op microbenchmarks are within ±3%, both with primitives cached and when a new primitive is created on every call. Measured over 7 alternating rounds, pinned to 16 cores on one NUMA node.
> - **Binary size:** `libtorch_cpu.so` `.text` +2.9 KB. The exported-symbol changes are listed in 21/N.
>
> These measurements were taken when this PR was first submitted. Since then the series has only had review changes (rewritten comments, removed dead code and unused includes, a typo rename); none change behavior. The rebuilt stack passes `-Wall -Wextra -Wsign-compare -Werror` per commit, and `test_mkldnn`, the mkldnn conv tests and the quantized conv/linear tests were rerun at the tip.
>
> Previously submitted as #204808.
Pull Request resolved: #206277
Approved by: https://github.com/albanD
ghstack dependencies: #205978, #205979, #205980, #206276
pytorchmergebot pushed a commit that referenced this pull request Nov 25, 2026
## Linked issue or supporting maintainer
Part of #154444

## BC-breaking?
No

## Summary (human written only)
Replaces any references to ideep in comments with mkldnn references reflecting the current code.

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

> Part of #154444 (remove the ideep submodule).
>
> Comment-only. After ideep is removed, comments in `aten/src/ATen/native/mkldnn/` and `aten/src/ATen/native/quantized/cpu/` still describe ideep doing the work. They now name what does it: `MkldnnMemory` for ideep tensors, `matmul_forward` / `mkldnn::inner_product_forward` for ideep operators, the oneDNN wrappers for "ideep (mkldnn bridge)", and oneDNN where the comment means the library.
>
> Left as-is on purpose: "Ported from ideep" provenance notes, ideep comments that were ported with the code in `detail/`, and identifiers that still say ideep (`DeviceType::IDEEP`, `IDeepRegistration.cpp`, `make_ideep_tensor`).
>
> What changed: 10 files, +33/−33, all comment lines.

Pull Request resolved: #206278
Approved by: https://github.com/albanD
ghstack dependencies: #205978, #205979, #205980, #206276, #206277
pytorchmergebot pushed a commit that referenced this pull request Nov 25, 2026
…n Attr (#206279)

## Linked issue or supporting maintainer
Part of #154444

## BC-breaking?
No

## Summary (human written only)
Changing `inner_product_forward_expected_weights_desc` to take an `Attr` parameter as its last argument, allowing the packed weight layout to depend on primitive attributes. This was requested by @mriganks-amd for upcoming ZenDNN fp16 accumulation support.

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

> Part of #154444. Requested by AMD for ZenDNN fp16 accumulation (#204794).
>
> `inner_product_forward_expected_weights_desc` now takes `const Attr& attr = Attr()` as its last parameter and passes it to the `dnnl::inner_product_forward::primitive_desc`, as `matmul_forward_expected_weights_desc` already does. The packed weight layout can depend on primitive attributes. For example, with ZenDNN's fp16 "any" accumulation mode, a weight packed with default attributes runs on brgemm even with the mode set. Callers can now pack with the attributes they compute with.
>
> No behavior change: the only caller, `mkldnn_reorder_linear_weight` in `MKLDNNConversions.cpp`, does not pass `attr`, and a default `Attr` is an empty `dnnl::primitive_attr`, which is what oneDNN uses when no attributes are given. This is an API addition on top of the port; ideep's version takes no attributes.
>
> Testing: compiles with `-Wall -Wextra -Wsign-compare -Werror` in release and debug builds; `test_mkldnn`, the mkldnn conv tests and the quantized conv/linear tests pass at the tip.

Pull Request resolved: #206279
Approved by: https://github.com/albanD
ghstack dependencies: #205978, #205979, #205980, #206276, #206277, #206278
pytorchmergebot pushed a commit that referenced this pull request Nov 25, 2026
## Linked issue or supporting maintainer
Part of #154444

## BC-breaking?
No

## Summary (human written only)
Rename all itensor functions to `memory_*` to eliminate all itensor references in ideep port.

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

> Part of #154444 (remove the ideep submodule).
>
> Follows the `ITensor` -> `MkldnnMemory` rename. The helpers that convert between `at::Tensor` and `MkldnnMemory` drop their `itensor` prefix. This is a mechanical rename with no behavior change:
> - `itensor_from_mkldnn` -> `memory_from_mkldnn`
> - `itensor_from_tensor` -> `memory_from_tensor`
> - `itensor_from_const_tensor` -> `memory_from_const_tensor`
> - `itensor_view_from_dense` -> `memory_view_from_dense`
> - `itensor_view_from_const_dense` -> `memory_view_from_const_dense`
> - `itensor_view_from_dense_impl` -> `memory_view_from_dense_impl` (file-local)
> - `new_with_itensor_mkldnn` -> `new_with_mkldnn_memory`
> - `typed_itensor_view_from_dense` -> `typed_mkldnn_memory_view_from_dense` (file-local)
>
> These are internal ATen helpers declared in `MKLDNNCommon.h`, so their exported symbol names change, as with the helpers in 21/N.
Pull Request resolved: #206630
Approved by: https://github.com/albanD
ghstack dependencies: #205978, #205979, #205980, #206276, #206277, #206278, #206279
@izaitsevfb

Copy link
Copy Markdown
Contributor

✴️ iz2: Reverting this PR because it breaks Meta-internal tests that OSS CI does not run. @frost-intel, to reland it, please reach out to @albanD, who reviewed it, about the fix it needs.
@pytorchbot revert -m "Breaks Meta-internal tests that OSS CI does not run" -c ghfirst

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

@pytorchbot successfully started a revert job. Check the current status here.
Questions? Feedback? Please reach out to the PyTorch DevX Team

pytorchmergebot added a commit that referenced this pull request Nov 27, 2026
pytorchmergebot added a commit that referenced this pull request Nov 27, 2026
…c take an Attr (#206279)"

This reverts commit c067e37.

Reverted #206279 on behalf of https://github.com/izaitsevfb due to Breaks Meta-internal tests that OSS CI does not run ([comment](#206276 (comment)))
pytorchmergebot added a commit that referenced this pull request Nov 27, 2026
pytorchmergebot added a commit that referenced this pull request Nov 27, 2026
pytorchmergebot added a commit that referenced this pull request Nov 27, 2026
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

@frost-intel your PR has been successfully reverted.

[ghstack-poisoned]
@soulitzer

Copy link
Copy Markdown
Contributor

@soulitzer has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Dec 2, 2026

This branch was previously deployed

1 inactive (outdated) deployment
bedrock — fe66b3cc Deployed Nov 27, 2026 by pytorchmergebot via analyze #43237
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-no-td Do not run TD on this PR ciflow/trunk Trigger trunk jobs on your pull request Merged module: cpu CPU specific problem (e.g., perf, algorithm) open source Reverted topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants