Skip to content

[mkldnn][12/N] Port ideep matmul op into ATen - #202547

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

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

Conversation

@frost-intel

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

Copy link
Copy Markdown
Collaborator

Linked issue or supporting maintainer

Part of #154444

BC-breaking?

No

Summary (human written only)

Port of the matmul op from ideep to ATen. Unused params and functions were dropped, but otherwise this is a line-for-line port with identical functionality, verified by tests and onednn_verbose log checking.

The int8 branches were dropped because all calls in ATen do not use these.

Depends on #201055

Fake stack

Agent details (if applicable)

Developed with assistance from Claude Opus 5.5.

Part of #154444. Depends on #201055.

Ports the floating-point matmul path that ATen uses from ideep into aten/src/ATen/native/mkldnn/detail/Matmul.{h,cpp} (namespace at::native::mkldnn). The port is line-for-line and changes no behavior: it still takes ideep::tensor / ideep::attr_t and uses ideep's engine, stream and primitive cache.

Ported

ATen's three matmul calls all resolve to ideep's deprecated no-bias matmul_forward::compute. This PR ports exactly that path and keeps ideep's deprecation comment.

Public (detail/Matmul.h):

  • matmul_forward(src, weights, dst, dst_coeff, sum_coeff, attr)

File-local (detail/Matmul.cpp), same names as in ideep:

  • matmul_forward_params
  • do_prepare<with_bias> (fp32)
  • do_compute<with_bias, reorder_src, reorder_weight>
  • compute_impl<with_bias, reorder_src, reorder_weight>: fp32 branch

The call chain is the same as ideep's: compute_impl<false, true, true> → do_prepare<false> + do_compute<false, true, true>. The aarch64 path (uncached primitive descriptor, format_tag::any weights) is kept.

Call sites switched

  • mkldnn/Matmul.cpp: all three ideep::matmul_forward::compute calls: the bf16/fp16/bf32/tf32 mkldnn_gemm, bf16→fp32 mkldnn_gemm, and mkldnn_matmul. The three empty std::vector<float>() scale arguments are dropped. alpha and beta map to dst_coeff and sum_coeff as before.

Still on ideep (quantized; follow-up): qlinear.cpp, qlinear_dynamic.cpp and qlinear_prepack.cpp prepare / compute / expected_weights_desc, and the LinearParams alias.

Dropped (unreachable from ATen's callers)

  • compute_impl's int8 branches (dynamic and static quant). They are taken only when weights.has_scale() or weights_scales is non-empty. All three callers pass empty scales, and their weights come from make_ideep_tensor / itensor_view_from_dense, which never set a scale.
  • Unused parameters:
    • the three scale parameters
    • bin_post_params (always {})
    • dst_type (always undef, which made its override line in do_prepare a no-op)
    • lowp_kind and engine
  • Unused fields and overloads:
    • matmul_forward_params::all_zero_points (never set on the fp32 path) and its loop in do_compute
    • the unused bias_attr field and references
    • dq_param_ptr and the struct's constructors
    • the with-bias deprecated overload (no callers)
  • Comments describing the removed int8 / zero-point / binary-post-op code. They were removed or shortened along with that code.

Other changes

  • IDEEP_ENFORCE becomes a debug-only dnnl::error::wrap_c_api(...) under #ifndef NDEBUG, as in ideep. The weights/bias dtype || chains are written with one_of from detail/Utils.h, which computes the same thing.
  • Called directly from ideep for now: ideep::utils::create_key, ideep::utils::computation_cache, ideep::utils::op_scale_mask.
  • The primitive cache is ideep's thread-local computation_cache<dnnl::matmul::primitive_desc>, the same store ideep's own matmul_forward (still used by quantized linear) uses. The key fields and their order are unchanged.

Testing

oneDNN verbose logs (ONEDNN_VERBOSE=1, timings stripped, sorted counts) are identical before and after:

  • test_mkldnn.py -k "matmul or mm or bmm or dot or mv or bf32 or tf32 or reduced" plus test_linalg.py -k "cpu and (addmm or bmm or matmul or addmv or mv or dot or _mm_ or linear) and (bfloat16 or float16 or half)": 524 distinct lines, 521 of them matmul (bf16/fp16, alpha source scales, beta sum post-op).
  • A probe of fp32 addmm/mm/bmm/baddbmm/mv with torch.backends.mkldnn.matmul.fp32_precision set to bf16 and tf32 (paths no existing test exercises): logs identical, outputs bitwise equal.
  • Full test_mkldnn.py plus test_freezing.py -k mkldnn: 6,572 distinct lines.

(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 29, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ No Failures

As of commit 2cf6c97 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.

Port the floating-point matmul path ATen uses (ideep's deprecated
no-bias matmul_forward::compute) into
aten/src/ATen/native/mkldnn/detail/Matmul.{h,cpp}. The port is
line-for-line, still takes ideep::tensor/attr_t, and shares ideep's
primitive cache, so behavior is unchanged.

Switch the three call sites in mkldnn/Matmul.cpp. Quantized linear
remains on ideep for now.

Drop code ATen never reaches: compute_impl's int8 branches (callers
always pass empty scales and unscaled weights), the scale,
bin_post_params, dst_type, lowp_kind and engine parameters, and the
unused zero-point, bias-attr and dynamic-quant fields.
@frost-intel
frost-intel force-pushed the ideep-removal/11-port-matmul branch from 8614ffc to 2cf6c97 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
pytorchmergebot pushed a commit that referenced this pull request Nov 25, 2026
)

## Linked issue or supporting maintainer
Part of #154444
Supported by @albanD

## BC-breaking?
No

## Summary (human written only)
Port of the matmul op from ideep to ATen. Unused params and functions were dropped, but otherwise this is a line-for-line port with identical functionality, verified by tests and onednn_verbose log checking.

Ports int8 matmul_forward used by quantized linear into ATen. This is a line-by-line copy, no behavioral change. Functionality not used in ATen was dropped, along with unused params.

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

>
> Ports the matmul paths that ATen uses from ideep (`ideep::matmul_forward`) into `aten/src/ATen/native/mkldnn/detail/Matmul.{h,cpp}` (namespace `at::native::mkldnn`): the floating-point path used by `mkldnn/Matmul.cpp` and the int8 paths used by quantized linear. The port is line-for-line and changes no behavior: it still takes `ideep::tensor` / `ideep::attr_t` and uses ideep's engine, stream and primitive cache.
>
> This combines two earlier PRs, the fp32 port (#202547) and the quantized port (#204617). Code that #202547 had removed as unused on the fp path (`do_prepare`'s `dst_type` override, `do_compute`'s zero-point arguments, `compute_impl`'s int8 branches) is kept, because the quantized paths reach it.
>
> ### Ported
> Public (`detail/Matmul.h`), each with ideep's comments:
> - `matmul_forward_params` and `matmul_forward_dyn_quant_params`, with their default constructors only. ideep's member-wise constructors are unused and are dropped (the `matmul_forward_params` one also read `pd` after moving from it), as is the `bias_attr` member, which only that constructor set. `LinearParams` in `OnednnUtils.h` now points at the ported struct.
> - `matmul_forward(src, weights, dst, dst_coeff, sum_coeff, attr)`: ideep's **deprecated** no-bias 2-in-1 `compute`, which all three fp calls in ATen resolve to. ideep's deprecation comment is kept.
> - `matmul_forward_prepare<is_dynamic>`: static and dynamic int8 `prepare`.
> - `matmul_forward_compute`: with pre-prepared params (fp32 and static int8), with pre-prepared params (dynamic int8), and the int8 2-in-1 compute.
> - Both `matmul_forward_expected_weights_desc` overloads.
>
> File-local (`detail/Matmul.cpp`), same names as in ideep:
> - `do_prepare<with_bias>` (fp32)
> - `do_prepare_static_quant`, `do_prepare_dynamic_quant`
> - `do_compute<with_bias, reorder_src, reorder_weight>`, `do_compute_dynamic_quant`
> - `compute_impl<with_bias, reorder_src, reorder_weight>`: all three branches (fp32, static and dynamic int8)
>
> The call chains are the same as ideep's. The fp path is `compute_impl<false, true, true>` with ideep's empty scales and zero points, so only its fp32 branch runs. The aarch64 path (uncached primitive descriptor, `format_tag::any` weights) is kept.
>
> ### Call sites switched
> - `mkldnn/Matmul.cpp`: all three `ideep::matmul_forward::compute` calls: the bf16/fp16/bf32/tf32 `mkldnn_gemm`, bf16→fp32 `mkldnn_gemm`, and `mkldnn_matmul`. The three empty `std::vector<float>()` scale arguments are dropped. `alpha` and `beta` map to `dst_coeff` and `sum_coeff` as before.
> - `quantized/cpu/qlinear.cpp`, `qlinear_dynamic.cpp` and `qlinear_prepack.cpp`: `prepare`, `compute` and `expected_weights_desc`.
>
> ### Not ported, because ATen never reaches them
> - `compute_binary` and its helpers
> - `compute_v2`
> - `compute_dynamic`
> - the deprecated `compute`/`prepare` overloads that take `bin_post_params` or put `dst_coeff` first, and the deprecated with-bias fp overload
> - the fp32 with-bias `prepare`/`compute` overloads
> - the no-bias int8 overloads
>
> ### Deviations from ideep
> - The `engine` parameters are dropped; ATen only ever passes `ideep::engine::cpu_engine()`, which the ported code uses directly.
> - The deprecated fp `matmul_forward` drops the three scale parameters, `bin_post_params` (always `{}`), `dst_type` (always `undef`) and `lowp_kind`; it passes ideep's defaults to `compute_impl`.
> - Unused local references are dropped (`bias_attr`, `wei_zero_point`, and `dst_scales_in` in the dynamic compute) because they fail `-Werror`.
> - In `do_prepare_static_quant`, the zero-point size variables are declared inside `#ifndef NDEBUG`, since only that check uses them, and the unused `src_strides` vector is dropped (the src descriptor uses `format_tag::any`).
> - `IDEEP_ENFORCE` becomes a debug-only `dnnl::error::wrap_c_api(...)` under `#ifndef NDEBUG`, as in ideep. The weights/bias dtype `||` chains are written with `one_of` from `mkldnn/Utils.h`, which computes the same thing.
>
> ### Other changes
> - Called directly from ideep for now: `ideep::utils::create_key`, `ideep::utils::computation_cache`, `ideep::utils::op_scale_mask`, `ideep::utils::tensor_scale_mask`, `ideep::utils::tensor_zp_mask`, and the `IDEEP_DEF_SCALE` / `IDEEP_DEF_ZP` / `IDEEP_EMPTY_SCALE` / `IDEEP_EMPTY_ZP` constants.
> - The primitive cache is ideep's thread-local `computation_cache<dnnl::matmul::primitive_desc>`. The key fields and their order are unchanged.
>
> ### Testing
> oneDNN verbose logs (`ONEDNN_VERBOSE=1`, timings stripped, sorted counts) are identical before and after:
> - `test_mkldnn.py -k "matmul or mm or bmm or dot or mv or bf32 or tf32 or reduced"` plus `test_linalg.py -k "cpu and (addmm or bmm or matmul or addmv or mv or dot or _mm_ or linear) and (bfloat16 or float16 or half)"`: 524 distinct lines, 521 of them matmul (bf16/fp16, `alpha` source scales, `beta` sum post-op).
> - A probe of fp32 `addmm`/`mm`/`bmm`/`baddbmm`/`mv` with `torch.backends.mkldnn.matmul.fp32_precision` set to `bf16` and `tf32`: logs identical, outputs bitwise equal. On bf16/AMX hardware these paths are also covered by the `@reduced_f32_on_and_off` float32 tests in `test_linalg.py` (`addmm`, `mm`, `bmm`, `addbmm`, `baddbmm`, `inner`, `tensordot`), which the A/B filter above leaves out; `mv` has no such test.
> - `test_quantized_op -k linear` (fixed test seeds).
> - Full `test_mkldnn.py` plus `test_freezing.py -k mkldnn`: 6,572 distinct lines.
> - Re-run against `main` including the `test_linalg.py` `*_cpu_float32` tests (`addmm`, `mm`, `bmm`, `baddbmm`, `addbmm`, `inner`, `tensordot`) that `@reduced_f32_on_and_off` runs with bf16 and tf32 fp32-precision: oneDNN logs identical (1,015 distinct lines, 237 matmul each with `fpmath:bf16` and `fpmath:tf32`).
>
> A probe that reuses packed quantized weights with new input scales and shapes, to reach the cache-miss paths, gives identical logs and bitwise-equal outputs.
>
> (`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 #202547 and #204617.

Pull Request resolved: #205976
Approved by: https://github.com/albanD
ghstack dependencies: #205972, #205973, #205974, #205975
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