feat: generate dispatch entrypoints for bindings#604
Merged
Conversation
Collaborator
Author
bitzyz
previously approved these changes
May 13, 2026
bitzyz
approved these changes
May 13, 2026
Ziminli
approved these changes
May 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
generated/bindings/generated_dispatch.handgenerated/bindings/generated_dispatch.ccfromscripts/generate_wrappers.py.Make,Call, instance invocation, cache clearing, and active implementation queries through the generated dispatch entrypoints..ccvia CMake instead of compiling only the monolithicops.cc.Motivation
This change prepares the binding generator for larger generated operator sets without relying on each binding translation unit to see every backend implementation header.
The existing monolithic binding source works because all
Operator<Key, Device, Index>specializations are visible in one translation unit. A naive split binding layout changesActiveImplementationsvisibility and can produce different runtime dispatch behavior. The generated dispatch source keeps implementation visibility centralized while allowing the pybind registration code to compile in smaller per-operator translation units.Related: #593
Type of Change
feat— new feature / new operator / new platformfix— bug fixperf— performance improvement (no behavioral change)refactor— code restructuring without behavior changetest— adding or fixing tests onlydocs— documentation onlybuild/ci— build system or CI configurationchore— tooling, formatting, or other non-code changes!in the Conventional Commits prefix or aBREAKING CHANGE:footer)Platforms Affected
WITH_CPU)WITH_NVIDIA)WITH_ILUVATAR)WITH_METAX)WITH_CAMBRICON)WITH_MOORE)WITH_ASCEND)WITH_TORCH)Test Results on Supported Platforms
Latest full-platform rerun after commit
a00c8e51(refactor: rely on overloads in generated dispatch):generated-dispatch-a00c8e51-20260513-150730. Each platform usedpytest tests/from the generated-dispatch branch on GPU/NPU 6 unless noted otherwise.pytestResult4151 passed, 1375 skipped in 318.69s3651 passed, 375 skipped in 263.04s5795 passed, 1447 skipped in 365.97s12 failed, 3061 passed, 3857 skipped in 911.23stests/test_add.pyMLUint16 random_failures per prior platform runs300 failed, 5459 passed, 1483 skipped in 525.00stests/test_gemm.pyfailures per prior platform runs24 failed, 3804 passed, 138 skipped in 464.88s; job reported exit code 137 after pytest outputtests/test_linear.pyOOMs, with container-visiblenpu:0reporting only tens of MiB freeNotes on result differences from the previous table:
generated-dispatch-full-20260513; this table supersedes it for the current head commita00c8e51.pytest tests/coverage for this branch. MetaX/Moore/Cambricon match the prior known result classes.16 failedtable: this rerun used NPU 6 and failed 24tests/test_linear.pycases due OOM. Logs show PyTorch reportingNPU 0inside the container with only ~24-30 MiB free while allocating 34-66 MiB tensors, so this appears to be device-memory state/mapping rather than a generated-dispatch compile or symbol issue.Full `pytest` output summary
Benchmark / Performance Impact
This PR is expected to reduce pybind binding compilation pressure by moving operator registration into per-op translation units while keeping implementation dispatch centralized in
generated_dispatch.cc.Timing from full-platform rerun
generated-dispatch-a00c8e51-20260513-150730:Notes for Reviewers
The important invariant is that all code paths which depend on
ActiveImplementationsvisibility must remain ingenerated_dispatch.cc, because that file includes the backend implementation headers. Per-op binding files intentionally include only base/operator binding headers plus lightweight device marker headers, then call the generated dispatch entrypoints.This PR does not expose
generated_dispatchas a public C API. It is an internal generated layer that can be reused by a future C API implementation.Checklist
Title, Branch, and Commits
feat(nvidia): …,fix(cuda/gemm): …).<type>/xxx-yyyy-zzzzwhere<type>matches the PR title's Conventional Commits type and words are joined with hyphens (seeCONTRIBUTING.md§Branches).CONTRIBUTING.md§Pull Requests).master— the branch is rebased cleanly on top of the currentmaster.fixup!/squash!/wipcommits remain.Scope and Design
CONTRIBUTING.md§Code/General).printf/std::cout/print(...)left behind, orTODOwithout an owner and issue link.General Code Hygiene (applies to all languages)
CONTRIBUTING.md§Code/General).CONTRIBUTING.md§Code/General).the `seqlens_k` tensor) (CONTRIBUTING.md§Code/General).CONTRIBUTING.md§Code/General).CONTRIBUTING.md§Code/General; §Python).C++ Specific (if C++ files changed)
clang-format(version 21, per.github/workflows/clang-format.yml) has been run against all modified.h,.cc,.cuh, and.mlufiles; the diff is clean.clang-tidyconcerns (per.clang-tidy) have been reviewed — no new warnings beyond the existing baseline.new/delete; RAII / smart pointers / existing allocators are used.Python Specific (if Python files changed)
ruff checkpasses cleanly on CI (see.github/workflows/ruff.yml).ruff format --checkpasses cleanly — if not, runruff formatand commit the result.CONTRIBUTING.md§Python).pytest.skipmessages without terminal period) are honored where applicable (CONTRIBUTING.md§Python).CONTRIBUTING.md§Python).if,for, and similar control-flow statements (CONTRIBUTING.md§Python).return, except when it directly follows a control-flow statement (CONTRIBUTING.md§Python).Testing
pytestwas run locally on every supported platform that this PR can affect, and the results are recorded in the "Test Results" table above (CONTRIBUTING.md§Pull Requests).Build, CI, and Tooling
pip install .[dev]on at least one affected platform.compile_commands.jsonstill regenerates (CMake optionCMAKE_EXPORT_COMPILE_COMMANDS=ONinpyproject.toml— required by thecode-lintskill andclang-tidy -p).CMakeLists.txtis not broken.clang-format.yml,ruff.yml) are green on GitHub Actions.pyproject.toml's[project.optional-dependencies](or justified in the PR description).Documentation
README.md,CONTRIBUTING.md, or inline docs updated when behavior, build flags, or developer workflow changed.CONTRIBUTING.md§Some Code Explanations).Security and Safety