Skip to content

[mkldnn][9/N] Port inner product from ideep to ATen - #201194

Closed
frost-intel wants to merge 4 commits into
pytorch:mainfrom
frost-intel:ideep-removal/09-port-inner-product
Closed

frost-intel wants to merge 4 commits into
pytorch:mainfrom
frost-intel:ideep-removal/09-port-inner-product

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)

Ports inner-product operators from ideep to ATen. Some ideep debug settings are changed to use torch debug handles. A few unused parameters are removed.

One non-impactful code change: previously, calls within mkldnn_linear calls a function marked "DEPRECATED" in ideep. These deprecated functions were superseded by a templatized version, but the mkldnn_linear calls hadn't been updated to match those in mkldnn_linear_pointwise which do use the templateized version. I did not port the deprecated calls over, so the calls in mlkdnn_linear now fallback to the templatized version.

Depends on #201055

Fake stack

Agent details (if applicable)

Developed with assistance from Claude Opus 5.5.

What changed (5 files, +837/−10):

  • detail/InnerProduct.{h,cpp}: the inner-product code ATen calls, ported line for line from ideep's inner_product.hpp:
    • inner_product_forward (with and without bias) and inner_product_forward_binary (with and without bias), keeping ideep's reorder_src/reorder_weight template parameters. Only the combinations ATen uses are compiled: <true, true> and <false, false> for forward, <false, false> for binary.
    • inner_product_forward_expected_weights_desc, inner_product_backward_data, and inner_product_backward_weights (with and without bias).
    • ideep's private functions and its inner_product_forward_params struct are ported under their ideep names, local to the .cpp. ideep's backward-weights compute_impl becomes backward_weights_compute_impl, because it would otherwise collide with the forward compute_impl.
  • Primitive cache: on x86, primitive descriptors are still fetched from ideep's cache (ideep::utils::computation_cache<dnnl::inner_product_forward::primitive_desc> with ideep::utils::create_key). This is the same cache ideep's own operators use, with the same key fields, capacity, and LRU_CACHE_CAPACITY handling. Porting the cache itself is a later PR. The aarch64 path, which bypasses the cache, is kept under #ifdef __aarch64__ as in ideep.
  • Kept as in ideep: input/weight/bias reorders, the dst-buffer reuse and copy-back logic, the sum post-op copy in do_compute_, the Caffe2 int8 source-scale handling, the src/weights reshape workaround, the avx2_vnni_2 bf16/f16 check on both backward functions, and ideep's debug-only argument checks. Those are written out as #ifndef NDEBUG dnnl::error::wrap_c_api(...), so they still only run in debug builds and throw the same error.
  • Not ported, because ATen never calls them: the prepare/compute/compute_binary overloads that take a precomputed params object, plus do_prepare and do_compute, which only those overloads use.
  • Removed, because ATen never set them or they did nothing:
    • The unused scale and lowp_kind parameters of ideep's deprecated compute overloads. These were the overloads ATen's plain linear actually resolved to; they called the same compute_impl as the templated overloads.
    • The attr parameters on both backward functions (ATen always used the default).
    • diff_weight_type on backward weights (always the default, so the gradient type is diff_dst's), along with the type-conversion branch that only ran for other values.
    • An unused op_attr local in do_compute_binary.
  • Call sites: Linear.cpp (9) and MKLDNNConversions.cpp (1, the linear weight prepack). The only remaining ideep::inner_product_forward text is a comment in qlinear_dynamic.cpp.
  • build_variables.bzl: 1 entry.

Checks:

  • Same oneDNN calls before and after, with ONEDNN_VERBOSE=1 and timings stripped:
    • test_mkldnn.py plus test_freezing.py -k mkldnn: all 6,572 distinct primitive log lines are identical. These include 32 inner-product executions, some of them backward.
    • test_mkldnn_fusion.py plus test/inductor/test_mkldnn_pattern_matcher.py -k "linear and not qlinear and not quant and not woq and not da8w8": all 892 distinct primitive log lines are identical. These cover 254 distinct inner-product lines (the fused linear and binary-fusion paths with prepacked weights) and 61 distinct reorder lines, including the weight-prepack reorders.
  • Tests:
    • test_mkldnn.py: 92 passed, 1 skipped.
    • Freezing tests: 4 passed, 5 skipped.
    • test_mkldnn_fusion.py: 9 passed.
    • test_mkldnn_pattern_matcher.py (linear subset): 12 passed.
  • 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/201194

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

❌ 1 New Failure

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

NEW FAILURE - The following job has failed:

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 (only the top commit is new).

Move the ideep inner_product_forward (compute, compute_binary,
expected_weights_desc), inner_product_backward_data and
inner_product_backward_weights code paths used by ATen into
aten/src/ATen/native/mkldnn/detail/InnerProduct.{h,cpp} and switch the
callers in Linear.cpp and MKLDNNConversions.cpp. The port is
line-for-line; ideep's private helpers keep their names. Primitive
descriptors still come from ideep's computation_cache with the same keys,
so the cache is shared with ideep until it is ported separately.

Verified with ONEDNN_VERBOSE=1: the primitive create/exec logs of
test_mkldnn.py, the mkldnn freezing tests, test_mkldnn_fusion.py and the
Inductor mkldnn linear pattern tests are identical before and after.
Cast weights.ndims() to size_t when comparing with diff_src_dims.size().
The comparison is copied from ideep, where it is hidden by the system-header
include; WERROR builds reject it here.
dnnl::prop_kind prop_kind = dnnl::prop_kind::forward);

// Ported from ideep::inner_product_forward::expected_weights_desc.
ideep::tensor::desc inner_product_forward_expected_weights_desc(

@mriganks-amd mriganks-amd Nov 19, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hi @frost-intel, Can the port take const Attr& attr = Attr() argument and pass it to the primitive_desc like the compute functions above?

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)
Ports inner-product operators from ideep to ATen. A few unused parameters are removed.

One non-impactful code change: previously, calls within `mkldnn_linear` calls a function marked "DEPRECATED" in ideep. These deprecated functions were superseded by a templatized version, but the `mkldnn_linear` calls hadn't been updated to match those in `mkldnn_linear_pointwise` which do use the templateized version. I did not port the deprecated calls over, so the calls in `mlkdnn_linear` now fallback to the templatized version.

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

> **What changed** (5 files, +838/−10):
> - **`detail/InnerProduct.{h,cpp}`:** the inner-product code ATen calls, ported line for line from ideep's `inner_product.hpp`:
>   - `inner_product_forward` (with and without bias) and `inner_product_forward_binary` (with and without bias), keeping ideep's `reorder_src`/`reorder_weight` template parameters. Only the combinations ATen uses are compiled: `<true, true>` and `<false, false>` for forward, `<false, false>` for binary.
>   - `inner_product_forward_expected_weights_desc`, `inner_product_backward_data`, and `inner_product_backward_weights` (with and without bias).
>   - ideep's private functions and its `inner_product_forward_params` struct are ported under their ideep names, local to the `.cpp`. ideep's backward-weights `compute_impl` becomes `backward_weights_compute_impl`, because it would otherwise collide with the forward `compute_impl`.
> - **Primitive cache:** on x86, primitive descriptors are still fetched from ideep's cache (`ideep::utils::computation_cache<dnnl::inner_product_forward::primitive_desc>` with `ideep::utils::create_key`). This is the same cache ideep's own operators use, with the same key fields, capacity, and `LRU_CACHE_CAPACITY` handling. Porting the cache itself is a later PR. The aarch64 path, which bypasses the cache, is kept under `#ifdef __aarch64__` as in ideep.
> - **Kept as in ideep:** input/weight/bias reorders, the dst-buffer reuse and copy-back logic, the sum post-op copy in `do_compute_`, the Caffe2 int8 source-scale handling, the src/weights reshape workaround, the avx2_vnni_2 bf16/f16 check on both backward functions, and ideep's debug-only argument checks. Those are written out as `#ifndef NDEBUG dnnl::error::wrap_c_api(...)`, so they still only run in debug builds and throw the same error.
> - **Not ported, because ATen never calls them:** the `prepare`/`compute`/`compute_binary` overloads that take a precomputed `params` object, plus `do_prepare` and `do_compute`, which only those overloads use.
> - **Removed, because ATen never set them or they did nothing:**
>   - The unused scale and `lowp_kind` parameters of ideep's deprecated `compute` overloads. These were the overloads ATen's plain linear actually resolved to; they called the same `compute_impl` as the templated overloads.
>   - The `attr` parameters on both backward functions (ATen always used the default).
>   - `diff_weight_type` on backward weights (always the default, so the gradient type is `diff_dst`'s), along with the type-conversion branch that only ran for other values.
>   - An unused `op_attr` local in `do_compute_binary`.
> - **Call sites:** `Linear.cpp` (9) and `MKLDNNConversions.cpp` (1, the linear weight prepack). The only remaining `ideep::inner_product_forward` text is a comment in `qlinear_dynamic.cpp`.
> - **`build_variables.bzl`:** 1 entry.
>
> **Checks:**
>
> - **Same oneDNN calls before and after**, with `ONEDNN_VERBOSE=1` and timings stripped:
>   - `test_mkldnn.py` plus `test_freezing.py -k mkldnn`: all 6,572 distinct primitive log lines are identical. These include 32 inner-product executions, some of them backward.
>   - `test_mkldnn_fusion.py` plus `test/inductor/test_mkldnn_pattern_matcher.py -k "linear and not qlinear and not quant and not woq and not da8w8"`: all 892 distinct primitive log lines are identical. These cover 254 distinct inner-product lines (the fused linear and binary-fusion paths with prepacked weights) and 61 distinct reorder lines, including the weight-prepack reorders.
> - **Tests:**
>   - `test_mkldnn.py`: 92 passed, 1 skipped.
>   - Freezing tests: 4 passed, 5 skipped.
>   - `test_mkldnn_fusion.py`: 9 passed.
>   - `test_mkldnn_pattern_matcher.py` (linear subset): 12 passed.
> - **Lint:** clean.
>
> (`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 #201194.

Pull Request resolved: #205715
Approved by: https://github.com/albanD
ghstack dependencies: #205709, #205710, #205711, #205712, #205713, #205714
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.

3 participants