Skip to content

fix(qwen3-14b): accept pypto-lib's renamed BATCH_PAD constant - #119

Open
lyfne123 wants to merge 1 commit into
hw-native-sys:mainfrom
lyfne123:fix/qwen3-decode-batch-pad-compat
Open

fix(qwen3-14b): accept pypto-lib's renamed BATCH_PAD constant#119
lyfne123 wants to merge 1 commit into
hw-native-sys:mainfrom
lyfne123:fix/qwen3-decode-batch-pad-compat

Conversation

@lyfne123

Copy link
Copy Markdown
Contributor

Summary

pypto-lib split the conflated BATCH constant when decode's public batch became dynamic (hw-native-sys/pypto-lib#841): BATCH_PAD is the padded pipeline width (the M of every matmul), while the public batch is now a runtime value read from the seq_lens descriptor. Against a post-rename pypto-lib the executor dies during validation with:

AttributeError: module '_pypto_lib_qwen3_14b_decode_fwd' has no attribute 'BATCH'
pypto_serving/model/qwen/npu_executor.py:171
  • Resolve the constant through _kernel_batch_pad, which accepts either spelling, so this file works against a pypto-lib from either side of the rename and the two repos can be updated independently. This has to land first: pypto-lib CI checks out pypto-serving at ref: main and swaps in the pypto-lib under test, so Add: dynamic public batch (1..16) for Qwen3-14B decode pypto-lib#841 cannot go green until main here tolerates the new name.
  • Decode's check becomes an upper bound only when the kernel advertises BATCH_PAD. A pre-rename decode writes exactly BATCH rows whatever the runtime batch, so relaxing it unconditionally would admit a smaller max_batch_size and overrun the buffers sized from it; that case keeps the exact-match check.
  • greedy_sample_fwd was not converted and stays an exact match either way. It carries the new spelling but is still fixed-batch, so the spelling alone must not be read as "takes a dynamic batch" — the helper's docstring says so explicitly.

No behaviour change against the pypto-lib pinned here: decode still receives batch-padded tensors. Exploiting the dynamic batch — dropping the row-0 replication padding in _prepare_decode_inputs and sizing block_table by the active batch — is left to a follow-up.

Verification

_kernel_batch_pad resolved against the real kernel modules of both generations, each in its own process:

pypto-lib decode_fwd greedy_sample
pre-rename (this submodule, 306519d) BATCH -> 16, exact-match path BATCH -> 16
post-rename (pypto-lib#841) BATCH_PAD -> 16, bound path BATCH_PAD -> 16

tests/test_qwen3_accuracy.py gets past executor validation (the AttributeError/ValueError are gone) but cannot complete on my box: both local pypto environments are independently broken right now — one has a Python/native skew (DataType.FP8E8M0 missing from a freshly rebuilt pypto_core.so), the other has pto-isa header drift in build_output/_deps (TStore.hpp: template parameter redefines default argument). Neither is related to this change; the remaining failure is in prefill kernel compilation. CI runs the accuracy test in its own pinned environment.

pypto-lib split the conflated `BATCH` constant when decode's public batch
became dynamic: `BATCH_PAD` is the padded pipeline width (the M of every
matmul), while the public batch is now a runtime value read from the
`seq_lens` descriptor. Against a post-rename pypto-lib the executor dies
during validation with

  AttributeError: module '_pypto_lib_qwen3_14b_decode_fwd'
                  has no attribute 'BATCH'

Resolve the constant through `_kernel_batch_pad`, which accepts either
spelling, so this file works against a pypto-lib from either side of the
rename and the two repos can be updated independently.

Decode's check becomes an upper bound *only* when the kernel advertises
`BATCH_PAD`. A pre-rename decode writes exactly `BATCH` rows whatever the
runtime batch, so relaxing it unconditionally would admit a smaller
max_batch_size and overrun the buffers sized from it; that case keeps the
exact-match check. greedy_sample_fwd was not converted and stays an exact
match either way -- it carries the new spelling but is still fixed-batch,
so the spelling alone must not be read as "takes a dynamic batch".

No behaviour change against the pypto-lib pinned here: decode still
receives batch-padded tensors. Exploiting the dynamic batch -- dropping the
row-0 replication padding and sizing block_table by the active batch -- is
left to a follow-up.
@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The Qwen NPU executor now detects kernel batch widths from either BATCH_PAD or legacy BATCH constants. Compilation validation applies bounded checks for dynamic decode kernels and exact checks for fixed decode and greedy-sampling kernels.

Changes

Qwen kernel batch validation

Layer / File(s) Summary
Batch-width compatibility helper
pypto_serving/model/qwen/npu_executor.py
Adds _kernel_batch_pad to read BATCH_PAD or fall back to BATCH, while reporting which constant was detected.
Compilation batch checks
pypto_serving/model/qwen/npu_executor.py
Validates dynamic decode kernels against a maximum padded width and requires exact batch matches for fixed decode and greedy-sampling kernels.

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

Possibly related PRs

Poem

A bunny checks the batch with care,
BATCH_PAD or BATCH hidden there.
Decode bounds now fit just right,
Greedy samples match in sight.
Hop, hop—kernels compile bright!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: accepting the renamed BATCH_PAD constant in Qwen3-14B validation.
Description check ✅ Passed The description is directly related to the code changes and explains the compatibility and validation behavior.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
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.

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

🧹 Nitpick comments (1)
pypto_serving/model/qwen/npu_executor.py (1)

217-232: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Add regression coverage for the batch-boundary contract.

Please test BATCH_PAD preference, legacy BATCH fallback, max_batch_size == padded_width, max_batch_size > padded_width, and legacy exact-match rejection. These cases protect the compatibility behavior and the distinction between dynamic decode and fixed-batch kernels.

🤖 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 `@pypto_serving/model/qwen/npu_executor.py` around lines 217 - 232, Add
regression tests covering the batch-validation logic around
_kernel_batch_pad/qwen3_decode_fwd: verify BATCH_PAD takes precedence, legacy
BATCH is used when padding is absent, dynamic kernels accept max_batch_size
equal to padded width but reject larger values, and fixed-batch kernels reject
non-exact max_batch_size values while accepting an exact match.
🤖 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 `@pypto_serving/model/qwen/npu_executor.py`:
- Around line 44-45: Update the _kernel_batch_pad docstring so its boolean
return value is documented as indicating whether BATCH_PAD was found, matching
the helper’s True/False behavior; retain the padded row count description.

---

Nitpick comments:
In `@pypto_serving/model/qwen/npu_executor.py`:
- Around line 217-232: Add regression tests covering the batch-validation logic
around _kernel_batch_pad/qwen3_decode_fwd: verify BATCH_PAD takes precedence,
legacy BATCH is used when padding is absent, dynamic kernels accept
max_batch_size equal to padded width but reject larger values, and fixed-batch
kernels reject non-exact max_batch_size values while accepting an exact match.
🪄 Autofix (Beta)

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: fcffe7be-2d47-4bef-832f-52f954da2566

📥 Commits

Reviewing files that changed from the base of the PR and between 627df90 and 6d8cad7.

📒 Files selected for processing (1)
  • pypto_serving/model/qwen/npu_executor.py

Comment on lines +44 to +45
def _kernel_batch_pad(kernel_module: object) -> tuple[int, bool]:
"""Return ``(padded row count, module predates the BATCH_PAD rename)``.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Correct the documented boolean polarity.

The helper returns True when BATCH_PAD is present and False when it falls back to legacy BATCH, but the docstring says the boolean indicates that the module predates the rename. Update the contract to state that it reports whether BATCH_PAD was found.

Proposed fix
 def _kernel_batch_pad(kernel_module: object) -> tuple[int, bool]:
-    """Return ``(padded row count, module predates the BATCH_PAD rename)``.
+    """Return ``(padded row count, whether the module exposes BATCH_PAD)``.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def _kernel_batch_pad(kernel_module: object) -> tuple[int, bool]:
"""Return ``(padded row count, module predates the BATCH_PAD rename)``.
def _kernel_batch_pad(kernel_module: object) -> tuple[int, bool]:
"""Return ``(padded row count, whether the module exposes BATCH_PAD)``.
🤖 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 `@pypto_serving/model/qwen/npu_executor.py` around lines 44 - 45, Update the
_kernel_batch_pad docstring so its boolean return value is documented as
indicating whether BATCH_PAD was found, matching the helper’s True/False
behavior; retain the padded row count description.

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.

1 participant