Skip to content

[ExecuTorch][WebGPU] Generate logical binary shader variants - #21451

Open
JCNTH wants to merge 1 commit into
gh/JCNTH/201/basefrom
gh/JCNTH/201/head
Open

[ExecuTorch][WebGPU] Generate logical binary shader variants#21451
JCNTH wants to merge 1 commit into
gh/JCNTH/201/basefrom
gh/JCNTH/201/head

Conversation

@JCNTH

@JCNTH JCNTH commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Generate byte-identical logical AND/OR variants from one shader family

Logical AND and OR duplicated the same packed-boolean WGSL structure and could drift independently. This moves them into one generated family while preserving each operator token, payload, workgroup, and host route. Mirrors Vulkan backends/vulkan/runtime/graph/ops/glsl/binary_op_buffer.{glsl,yaml}.

Key changes:

  • logical_binary.wgsl and YAML — generate AND and OR variants from the operator token.
  • Handlers and registry — relocate generated-header includes only.
  • Truth-table/native tests — lock all Boolean pairs, packed-word boundaries, aliases, and exact payloads.

Runtime bindings and dispatch remain unchanged; tests expand fixture coverage.

Co-authored-with: Claude Code.

Differential Revision: D113979789

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Jul 28, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 1 New Failure

As of commit 8635a76 with merge base 8200b9e (image):

NEW FAILURE - The following job has failed:

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

@github-actions

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.

@JCNTH

JCNTH commented Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

v

@JCNTH

JCNTH commented Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

@claude Double check for need of changes, modulariziation opportunity, etc

@claude

claude Bot commented Jul 29, 2026

Copy link
Copy Markdown

Claude finished @JCNTH's task in 1m 58s —— View job


Review: WebGPU logical binary shader variants

  • Read the full PR diff vs base
  • Compare against Vulkan binary_op_buffer reference
  • Assess correctness, modularization opportunities, drift risks
  • Post review feedback

The shader consolidation is clean and correct. The generated logical_and_wgsl.h / logical_or_wgsl.h are byte-identical to before (only the @generated from provenance line changed), the $if OP == "&" branch keeps each comment accurate, and the new roundtrip test locks the template → expansion → embedded header → registry chain. The deterministic truth-table generators are a genuine improvement over the old random-seed masks — they now cover all four Boolean pairs (FF/FT/TF/TT) in byte order plus word-boundary shapes (words63/64/65), and test_logical_binary_case_contract pins the exact payloads. No correctness issues found.

The @claude ask was "double check for need of changes, modularization opportunity" — so, focusing on that:

Main modularization opportunity: the two handler .cpp files still duplicate ~130 lines

The PR unified the shaders but left logical_and/LogicalAnd.cpp and logical_or/LogicalOr.cpp as near-identical files. A diff shows they differ only in:

  • the WGSL constant (kLogicalAndWGSL vs kLogicalOrWGSL) and workgroup constant,
  • the op-name strings in error messages / dispatch labels ("logical_and" vs "logical_or"),
  • the params struct name (LogicalAndParams vs LogicalOrParams — identical layout),
  • the two WEBGPU_REGISTER_OP lines.

Everything else — validation, uniform setup, pipeline/bind-group construction, the resize hook — is byte-for-byte the same. This is exactly the drift risk the PR set out to eliminate at the shader layer, just moved down to the host layer.

The repo already has the pattern to fix this: boolean_op/BooleanOp.cpp consolidates 7 ops (eq/ne/le/ge/lt/gt.Scalar + logical_not) behind a single dispatch_bool_op(...) helper parameterized by (wgsl, wg_size_x, op_name), with thin per-op registration shims. Applying the same shape here — a single logical_binary/LogicalBinary.cpp with dispatch_logical_binary(graph, args, wgsl, wg_size_x, op_name) and two 1-line impls — would collapse the two files into one and put the handler in the same directory as the shader it dispatches (right now shaders live in logical_binary/ while handlers stay in logical_and/ + logical_or/, which is a bit split). This also mirrors Vulkan more faithfully, where binary_op_buffer is one shader family and one host dispatch path.

If you want this, I'm happy to implement it — it's a mechanical consolidation and the existing test_logical_binary_template_roundtrip_byte_identical already validates the shader/registry side; the handler hash assertions in that test would need their paths updated to the new single file.

Minor: test-fixture module ownership

LOGICAL_BINARY_CASES, logical_binary_gen_a, and logical_binary_gen_b now live in test/ops/test_logical_and.py and are imported by test_logical_or.py, test_bitwise.py, cases.py, and test_generator.py. Having the OR / bitwise / codegen tests reach into the AND test module for shared fixtures is slightly surprising. Consider a small shared helper (e.g. test/ops/logical_binary_fixtures.py) so the shared truth-table generators have a neutral home — parallels the runtime logical_binary/ consolidation. Low priority; purely organizational.

Nit

  • test_logical_and.py _logical_binary_gen: the guard is numel % len(pattern) != 0 but the error string is hard-coded "...numel % 4 == 0". Fine today (len(pattern) == 4), just a latent inconsistency if the pattern length ever changes.

Net: no blocking issues — the shader-side work is solid and well-tested. The one worthwhile follow-up is folding the two duplicate handlers into a single dispatch_logical_binary à la BooleanOp.cpp; that completes the anti-drift goal end to end rather than stopping at the shader layer.

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant