Skip to content

refactor: compile L3 host wrappers via signature mode, drop dummy args - #146

Merged
superxf merged 1 commit into
hw-native-sys:mainfrom
ndleslx:worktree-qwen-host-wrappers
Aug 11, 2026
Merged

superxf merged 1 commit into
hw-native-sys:mainfrom
ndleslx:worktree-qwen-host-wrappers

Conversation

@ndleslx

@ndleslx ndleslx commented Aug 7, 2026

Copy link
Copy Markdown
Collaborator

Summary

Compile both the Qwen3-14B and DeepSeek-V4 L3 host wrappers in annotation-driven signature mode — no positional dummy tensors — reading shapes from the wrappers' pl.Tensor[[...], dtype] annotations. DeepSeek's runtime scalars (mtp_num_tokens / num_tokens) are passed as pl.RUNTIME (unspecialized).

Rebased onto main (which extracted the shared KernelCompiler and removed fingerprinting — the cache is now name-slotted).

Changes

  • compiler.py (KernelCompiler.compile): dummy_args is removed entirely; it always compiles in signature mode and forwards **compile_kwargs to jit_fn.compile (e.g. name=pl.RUNTIME).
  • qwen npu_executor.py: read the host wrappers off the already-loaded kernel modules (qwen3_decode_fwd.qwen3_decode_host, etc.); delete the three per-callable dummy_args builders and the qwen3_l3_dispatch injection/file.
  • deepseek npu_executor.py: pass pl.RUNTIME for each runtime scalar instead of building meta-tensor/ctypes sample args; drop _runtime_scalar_compile_args, _PYPTO_TORCH_DTYPES, and the ctypes/inspect imports.
  • tests: update the KernelCompiler/qwen/deepseek mocks + tests to the signature-mode + RUNTIME-kwarg API; replace the dummy-args-forwarding test with a kwargs-forwarding one.
  • Bump the pypto-lib submodule gitlink to b6c34f3 (qwen3_14b @pl.jit.host wrappers #914 + rope-dim fix #916).

Net: 9 files, +51 / −433.

Verification (all on this branch, pypto-lib b6c34f3)

Device guards:

  • qwen3 accuracy — 1 passed; serving (prefix cache + chunked prefill + multi-batch) — 4 passed
  • deepseek-v4 accuracy (8-device TP) — 4 passed

Unit: 72 passed (kernel_compiler + qwen + deepseek); ruff clean.

DeepSeek's pl.RUNTIME requires pypto ≥ #2300 (merged to pypto main, which CI clones).

@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Qwen kernel caching now fingerprints PyPTO kernel sources and static module metadata. NPU executor compilation uses a shared JIT path with annotated host wrappers. The serving-owned dispatch module and dummy tensor inputs are removed. Tests use lightweight kernel metadata objects.

Changes

Qwen kernel compilation and caching

Layer / File(s) Summary
Kernel cache fingerprint contract
pypto-lib, pypto_serving/model/qwen/kernel_cache.py
The PyPTO submodule reference changed. Code fingerprints now cover Qwen3-14B kernel sources. Parameter fingerprints use kernel names, platforms, and static module attributes.
Executor JIT integration
pypto_serving/model/qwen/npu_executor.py, pypto_serving/model/qwen/qwen3_l3_dispatch.py
The executor uses a shared JIT helper with loaded kernel modules and host-wrapper annotations. The serving-owned dispatch module and runtime function mutation were removed.
Fingerprint and executor validation
tests/unit/model/qwen/test_kernel_cache.py, tests/unit/model/qwen/test_npu_executor.py, tests/unit/model/qwen/test_npu_runner_inputs.py
Tests use lightweight kernel metadata objects and validate static specialization, platform, and kernel-name fingerprint changes. Obsolete dispatch test constants were removed.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

Poem

I hop through kernels, light and quick,
Static stars now name each trick.
No dummy tensors block the way,
Host wrappers guide the JIT today.
Cache and Qwen sing as one.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly summarizes the main refactor to signature-mode compilation and removal of dummy arguments.
Description check ✅ Passed The description directly explains the signature-mode migration, removed dummy arguments, affected components, tests, and verification.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/unit/model/qwen/test_kernel_cache.py`:
- Around line 93-101: Parameterize
test_params_fingerprint_tracks_every_distinguishing_dimension over
_QWEN3_14B_FINGERPRINT_CONSTS, varying each constant from its baseline value and
asserting _pf produces a different fingerprint. Preserve the existing platform
and kernel-name assertions so every listed static fingerprint constant is
covered.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 5fa68a5f-7c95-4849-bec5-a3961b8c8c39

📥 Commits

Reviewing files that changed from the base of the PR and between 272b874 and 00a4f2e.

📒 Files selected for processing (7)
  • pypto-lib
  • pypto_serving/model/qwen/kernel_cache.py
  • pypto_serving/model/qwen/npu_executor.py
  • pypto_serving/model/qwen/qwen3_l3_dispatch.py
  • tests/unit/model/qwen/test_kernel_cache.py
  • tests/unit/model/qwen/test_npu_executor.py
  • tests/unit/model/qwen/test_npu_runner_inputs.py
💤 Files with no reviewable changes (2)
  • pypto_serving/model/qwen/qwen3_l3_dispatch.py
  • tests/unit/model/qwen/test_npu_runner_inputs.py

Comment on lines +93 to +101
def test_params_fingerprint_tracks_every_distinguishing_dimension():
args = [_FakeTensor((16, 512), "bfloat16")]
base = _pf("decode_fwd", args)
# max_seq (shape) change -> the exact bug the name-only key missed
assert base != _pf("decode_fwd", [_FakeTensor((16, 2048), "bfloat16")])
# dtype, platform, and kernel name all distinguish a binary
assert base != _pf("decode_fwd", [_FakeTensor((16, 512), "float32")])
assert base != _pf("decode_fwd", args, platform="a2a3sim")
assert base != _pf("prefill_fwd", args)
base = _pf("decode_fwd", types.SimpleNamespace(MAX_SEQ=2048, VOCAB=512))
# MAX_SEQ change -> different binary (the exact bug a name-only key missed)
assert base != _pf("decode_fwd", types.SimpleNamespace(MAX_SEQ=4096, VOCAB=512))
# VOCAB change -> different binary
assert base != _pf("decode_fwd", types.SimpleNamespace(MAX_SEQ=2048, VOCAB=152064))
# platform and kernel name also distinguish a binary
assert base != _pf("decode_fwd", types.SimpleNamespace(MAX_SEQ=2048, VOCAB=512), platform="a2a3sim")
assert base != _pf("prefill_fwd", types.SimpleNamespace(MAX_SEQ=2048, VOCAB=512))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win

Cover every static fingerprint constant.

Lines 94-101 change only MAX_SEQ and VOCAB. A regression that omits BATCH_PAD, NUM_LAYERS, TOPK, or another listed constant will keep this test green and can reuse an incompatible cached binary. Parameterize the test over _QWEN3_14B_FINGERPRINT_CONSTS.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/unit/model/qwen/test_kernel_cache.py` around lines 93 - 101,
Parameterize test_params_fingerprint_tracks_every_distinguishing_dimension over
_QWEN3_14B_FINGERPRINT_CONSTS, varying each constant from its baseline value and
asserting _pf produces a different fingerprint. Preserve the existing platform
and kernel-name assertions so every listed static fingerprint constant is
covered.

@ndleslx
ndleslx force-pushed the worktree-qwen-host-wrappers branch 3 times, most recently from 141288a to 6585fe2 Compare August 10, 2026 09:54
Compile both the Qwen3-14B and DeepSeek-V4 L3 host wrappers in annotation-driven
signature mode -- no positional dummy tensors -- reading shapes from the
wrappers' pl.Tensor[[...], dtype] annotations.

- compiler.py: KernelCompiler.compile drops ``dummy_args`` entirely; it always
  compiles in signature mode and forwards ``**compile_kwargs`` to
  ``jit_fn.compile`` (e.g. ``name=pl.RUNTIME`` for runtime scalars).
- qwen npu_executor.py: read the host wrappers off the already-loaded kernel
  modules (qwen3_decode_fwd.qwen3_decode_host, etc.); delete the three
  per-callable dummy_args builders and the qwen3_l3_dispatch injection/file.
- deepseek npu_executor.py: pass pl.RUNTIME for each runtime scalar
  (mtp_num_tokens / num_tokens) instead of building meta-tensor/ctypes sample
  args; drop _runtime_scalar_compile_args, _PYPTO_TORCH_DTYPES, and the
  ctypes/inspect imports.
- tests: update the KernelCompiler/qwen/deepseek mocks and tests to the
  signature-mode + RUNTIME-kwarg API; drop the dummy-args-forwarding test.
- Bump the pypto-lib submodule gitlink to b6c34f3 (qwen3_14b @pl.jit.host
  wrappers #914 + rope-dim fix #916).

Rebased onto main (which extracted the shared KernelCompiler and removed
fingerprinting -- the cache is name-slotted, so the earlier qwen params-
fingerprint rework is dropped).

Verified: qwen3 accuracy + serving device guards pass locally; 72 unit tests
(kernel_compiler + qwen + deepseek); ruff clean.
@ndleslx
ndleslx force-pushed the worktree-qwen-host-wrappers branch from 6585fe2 to b93d345 Compare August 10, 2026 12:29
@ndleslx ndleslx changed the title refactor(qwen): compile host wrappers via signature mode, drop dummy args refactor: compile L3 host wrappers via signature mode, drop dummy args Aug 10, 2026
@superxf
superxf merged commit 3f6a7ff into hw-native-sys:main Aug 11, 2026
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants