Skip to content

[ET-VK][gemm] Nest linear and matmul sources by scheme - #23493

Open
SS-JIA wants to merge 4 commits into
gh/SS-JIA/660/basefrom
gh/SS-JIA/660/head
Open

SS-JIA wants to merge 4 commits into
gh/SS-JIA/660/basefrom
gh/SS-JIA/660/head

Conversation

@SS-JIA

@SS-JIA SS-JIA commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Ulterior Motive

Organize Vulkan operators by implementation family so operators with multiple routes can use clear, scalable directory layouts.

Rationale

What: Move the linear and matrix multiplication C++ implementations, GLSL templates, YAML variant definitions, and GEMM tile headers under gemm/ in both impl/ and glsl/, split into linear/ (an activation times a usually prepacked weight) and matmul/ (multiplication between live activations). Quantized linear sources are grouped by how they are quantized, and the multi-scheme QuantizedLinear.cpp and the shared GemmCoopmat.cpp are split accordingly.

Why: Linear and matmul sources were interleaved by name in the flat directories, quantized linear variants for different schemes shared one file, and the shared GEMM tile headers were scattered across the shader root, which obscured which sources implement which operator, route, and scheme.

Details

  • Layout (impl/ and glsl/ mirror each other)
    • gemm/: GemmCommon.{h,cpp} (fp weight prepack, resize helpers, and the cooperative-matrix tile constants and is_coopmat_eligible() shared by linear and matmul) and coopmat_mm, the cooperative-matrix shader shared by both.
    • gemm/tile_utils/: all GEMM tile headers (tile structs, tile load/store/compute, block packing, accumulators, and scale/sum/bias loaders), also used by convolution, SDPA, and quantize shaders.
    • gemm/linear/: Linear.{h,cpp} (aten.linear), LinearCoopmat.{h,cpp}, and backward/LinearDw.cpp (et_vk.linear_dW).
    • gemm/linear/q8ta/: Q8taLinear and Q8taLinearGemv; activations and weights are quantized ahead of time with static parameters (primarily statically quantized convolutional models).
    • gemm/linear/qw/: weight-only quantization, with activations kept in floating point or quantized dynamically (primarily LLMs and transformers). QuantizedLinear.{h,cpp} hold the shared weight prepacking, workgroup sizing, the weight-only node, and quantized_linear_impl; schemes live in q4gsw/ (with backward/ for et_vk.linear_q4gsw_backward and et_vk.q4gsw_requant), dq8ca_q4gsw/, q8csw/, and legacy/ (QuantizedLinearQCSNW and the q8ta_q8csw implementation that runs through quantized_linear_impl).
    • gemm/matmul/: MatMul.{h,cpp} (aten.mm, aten.bmm, aten.addmm; renamed from Matmul.cpp to match its header) and MatmulCoopmat.{h,cpp}.
    • Weight packing shaders sit in pack_weights/ subdirectories of the folder that dispatches them (linear/, linear/qw/, linear/qw/q4gsw/, and the shader root for pack_int4_linear_weight_transposed_interleaved).
  • Splits
    • QuantizedLinear.cpp: q8csw/Q8cswLinear.cpp (et_vk.linear_q8csw, aten._weight_int8pack_mm), legacy/Q8taQ8cswLinear.{h,cpp} (add_linear_qa_qw_node, et_vk.linear_q8ta_q8csw), and dq8ca_q4gsw/Dq8caQ4gswLinear.{h,cpp} (add_linear_dqa_qw_node, et_vk.linear_dq8ca_q4gsw). can_use_q4gsw_coopmat and the coopmat tile constants are now declared in QuantizedLinear.h because the shared and dq8ca_q4gsw files both use them.
    • GemmCoopmat.cpp: linear/LinearCoopmat.cpp and matmul/MatmulCoopmat.cpp.
    • Functions move unchanged.
  • Includes
    • Shaders inside gemm/ include tile headers as tile_utils/<header>; shaders outside gemm/ include them as gemm/tile_utils/<header>.
    • C++ include paths and op_registry.py section labels are updated.
  • Moved files are recorded as renames, and split files as copies of their source.
  • Code that moves within or between files is re-linted, so its CLANGTIDY findings are fixed: (void) for unused parameters, utils::safe_downcast for narrowing in quantized_linear_gwg and the coopmat size reads, bounds-checked .at() in place of .back() in LinearCoopmat.cpp, and removal of unused includes (Softmax.h in SDPA.cpp, Linear.h in Conv1dPW.cpp, and two utility headers in the coopmat files).

Generated shader names and runtime dispatch behavior are unchanged.

Differential Revision: D123523650

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ No Failures

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

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

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 6, 2026
@github-actions

github-actions Bot commented Oct 6, 2026

Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

[ghstack-poisoned]
SS-JIA added a commit that referenced this pull request Oct 9, 2026
Pull Request resolved: #23493

## Ulterior Motive

Organize Vulkan operators by implementation family so operators with multiple routes can use clear, scalable directory layouts.

## Rationale

**What**: Move the linear and matrix multiplication C++ implementations, GLSL templates, YAML variant definitions, and GEMM tile headers under `gemm/` in both `impl/` and `glsl/`, split into `linear/` (an activation times a usually prepacked weight) and `matmul/` (multiplication between live activations). Quantized linear sources are grouped by how they are quantized, and the multi-scheme `QuantizedLinear.cpp` and the shared `GemmCoopmat.cpp` are split accordingly.

**Why**: Linear and matmul sources were interleaved by name in the flat directories, quantized linear variants for different schemes shared one file, and the shared GEMM tile headers were scattered across the shader root, which obscured which sources implement which operator, route, and scheme.

## Details

- Layout (`impl/` and `glsl/` mirror each other)
  - `gemm/`: `GemmCommon.{h,cpp}` (fp weight prepack, resize helpers, and the cooperative-matrix tile constants and `is_coopmat_eligible()` shared by linear and matmul) and `coopmat_mm`, the cooperative-matrix shader shared by both.
  - `gemm/tile_utils/`: all GEMM tile headers (tile structs, tile load/store/compute, block packing, accumulators, and scale/sum/bias loaders), also used by convolution, SDPA, and quantize shaders.
  - `gemm/linear/`: `Linear.{h,cpp}` (`aten.linear`), `LinearCoopmat.{h,cpp}`, and `backward/LinearDw.cpp` (`et_vk.linear_dW`).
  - `gemm/linear/q8ta/`: `Q8taLinear` and `Q8taLinearGemv`; activations and weights are quantized ahead of time with static parameters (primarily statically quantized convolutional models).
  - `gemm/linear/qw/`: weight-only quantization, with activations kept in floating point or quantized dynamically (primarily LLMs and transformers). `QuantizedLinear.{h,cpp}` hold the shared weight prepacking, workgroup sizing, the weight-only node, and `quantized_linear_impl`; schemes live in `q4gsw/` (with `backward/` for `et_vk.linear_q4gsw_backward` and `et_vk.q4gsw_requant`), `dq8ca_q4gsw/`, `q8csw/`, and `legacy/` (`QuantizedLinearQCSNW` and the `q8ta_q8csw` implementation that runs through `quantized_linear_impl`).
  - `gemm/matmul/`: `MatMul.{h,cpp}` (`aten.mm`, `aten.bmm`, `aten.addmm`; renamed from `Matmul.cpp` to match its header) and `MatmulCoopmat.{h,cpp}`.
  - Weight packing shaders sit in `pack_weights/` subdirectories of the folder that dispatches them (`linear/`, `linear/qw/`, `linear/qw/q4gsw/`, and the shader root for `pack_int4_linear_weight_transposed_interleaved`).
- Splits
  - `QuantizedLinear.cpp`: `q8csw/Q8cswLinear.cpp` (`et_vk.linear_q8csw`, `aten._weight_int8pack_mm`), `legacy/Q8taQ8cswLinear.{h,cpp}` (`add_linear_qa_qw_node`, `et_vk.linear_q8ta_q8csw`), and `dq8ca_q4gsw/Dq8caQ4gswLinear.{h,cpp}` (`add_linear_dqa_qw_node`, `et_vk.linear_dq8ca_q4gsw`). `can_use_q4gsw_coopmat` and the coopmat tile constants are now declared in `QuantizedLinear.h` because the shared and `dq8ca_q4gsw` files both use them.
  - `GemmCoopmat.cpp`: `linear/LinearCoopmat.cpp` and `matmul/MatmulCoopmat.cpp`.
  - Functions move unchanged.
- Includes
  - Shaders inside `gemm/` include tile headers as `tile_utils/<header>`; shaders outside `gemm/` include them as `gemm/tile_utils/<header>`.
  - C++ include paths and `op_registry.py` section labels are updated.
- Moved files are recorded as renames, and split files as copies of their source.
- Code that moves within or between files is re-linted, so its CLANGTIDY findings are fixed: `(void)` for unused parameters, `utils::safe_downcast` for narrowing in `quantized_linear_gwg` and the coopmat size reads, bounds-checked `.at()` in place of `.back()` in `LinearCoopmat.cpp`, and removal of unused includes (`Softmax.h` in `SDPA.cpp`, `Linear.h` in `Conv1dPW.cpp`, and two utility headers in the coopmat files).

Generated shader names and runtime dispatch behavior are unchanged.
ghstack-source-id: 445310033
@exported-using-ghexport

Differential Revision: [D123523650](https://our.internmc.facebook.com/intern/diff/D123523650/)
[ghstack-poisoned]
[ghstack-poisoned]

This branch was successfully deployed

1 active deployment
cadence — 57c3330d Deployed Oct 10, 2026 by SS-JIA via hifi-op-test / hifi4 #32856
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. meta-exported

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant