Skip to content

fix(fp_quantizer): rename local shadowing template param for CUDA 13+ nvcc - #8808

Closed
bheu wants to merge 1 commit into
deepspeedai:masterfrom
bheu:fix/fp-quantizer-cuda13-qe-bits
Closed

bheu wants to merge 1 commit into
deepspeedai:masterfrom
bheu:fix/fp-quantizer-cuda13-qe-bits

Conversation

@bheu

@bheu bheu commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

nvcc 13.x (CUDA 13+) rejects redeclaring a template parameter name as a local variable inside the function body. apply_dequantization() and apply_selective_dequantization() in csrc/fp_quantizer/fp_quantize_impl.cu both declare a local constexpr int q_exponent_bits that shadows the template parameter int q_exponent_bits, so building fp_quantizer_op on CUDA 13+ fails with:

csrc/fp_quantizer/fp_quantize_impl.cu(234): error: template parameter "q_exponent_bits" may not be redeclared in this scope
csrc/fp_quantizer/fp_quantize_impl.cu(413): error: template parameter "q_exponent_bits" may not be redeclared in this scope
2 errors detected in the compilation of "csrc/fp_quantizer/fp_quantize_impl.cu".

CUDA 12.x compilers accept the shadowing, which is why upstream CI has not caught it.

Why is this PR needed?

PyTorch >= 2.12 headers require C++20, and DeepSpeed's nvcc_args() now passes -std=c++20 on CUDA 12.5+/13+. On a CUDA 13 + recent torch stack (e.g. CUDA 13.4 / torch 2.14) the fp_quantizer kernel no longer compiles at all, breaking op_builder / python setup.py install / python -m build when DS_BUILD_FP_QUANTIZER=1.

Design

Rename the two shadowing locals to new_qeb. The template parameter name is kept because it is part of the kernel signature and referenced by call sites (including the clip<...> template arguments).

Behavior is unchanged: the local always equals total_q_bits - mantisa_bits - 1 (e.g. 3+2=6), which differs from the template default (4) anyway — the existing line 233/412 quantized_bits = q_mantisa_bits + q_exponent_bits + 1 already implicitly relied on the shadowed value.

Testing

  • Reproduced the failure with the pre-fix file on CUDA 13.4 + torch 2.14.1 (-std=c++20): 2 errors, build fails.
  • With the fix, the file compiles cleanly (0 errors) under the same toolchain.
  • Full wheel build succeeds: python -m build --wheel --no-isolation --config-setting="--build-option=build_ext" on an A100 (sm_80/86/89/120) host, CUDA 13.4, Python 3.12, torch 2.14.1.

Hardware: 2x NVIDIA A100-SXM4-80GB (sm_80) host, compile targets sm_80/86/89/120.

… nvcc

nvcc 13.x (CUDA 13+) rejects redeclaring a template parameter name as a
local variable inside the function body. apply_dequantization() and
apply_selective_dequantization() both declare a local
'constexpr int q_exponent_bits' that shadows the template parameter
'int q_exponent_bits', so the fp_quantizer_op build fails on CUDA 13+:

  error: template parameter "q_exponent_bits" may not be redeclared in
  this scope

CUDA 12.x compilers accept the shadowing, which is why upstream CI
never caught it. The local is always total_q_bits - mantisa_bits - 1
(e.g. 6 = 3 + 2) and differs from the template parameter default (4),
so the rename is behavior-neutral. Rename the local to 'new_qeb' in
both kernels; the template parameter name is kept since it is part of
the kernel signature.

Signed-off-by: Frank Tin <[email protected]>
@bheu
bheu requested a review from tjruwase as a code owner October 12, 2026 09:31
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-12T09:35:21.776483Z c4ab93d PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c4ab93d584

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

constexpr int quantized_bits = q_mantisa_bits + q_exponent_bits + 1;
constexpr int q_exponent_bits = total_q_bits - mantisa_bits - 1;
constexpr int new_qeb = total_q_bits - mantisa_bits - 1;
constexpr int quantized_bits = q_mantisa_bits + new_qeb + 1;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Keep packed width based on the source exponent

When T is BF16 (the Python API's supported dtype), new_qeb is 8, the destination BF16 exponent width, whereas an E4M3 input has q_exponent_bits == 4. This changes quantized_bits from 8 to 12 (and similarly mis-sizes q6/q12), so group offsets and scale loads no longer match the buffer produced and allocated using q_bits; later groups are read out of bounds and dequantization returns corrupted values. Keep this expression based on the template q_exponent_bits; the same regression is duplicated in apply_selective_dequantization.

AGENTS.md reference: AGENTS.md:L19-L19

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This P1 does not apply — I traced the full call chain and verified the shadowing is intentional and correct.

How q_exponent_bits actually gets bound in this PR

apply_dequantization / apply_selective_dequantization are only reached via:

// csrc/fp_quantizer/fp_quantize_impl.cu (DEQUANT_SWITCH, applied to both kernels)
#define DEQUANT_SWITCH(q_bits)                                                    \
    switch (q_bits) {                                                            \
    case 8: /* E4M3 */                                                            \
        apply_dequantization<T, mantisa_bits, 3, 4>...                            \
    case 6: /* E3M2 */                                                            \
        apply_dequantization<T, mantisa_bits, 2, 3>...                            \
    ...

which is invoked as:

DEQUANT_SWITCH(q_mantisa_bits * q_exponent_bits)   // q_mantisa_bits, q_exponent_bits are the
                                                   // SOURCE quantization format's bit widths,
                                                   // passed in from deepspeed/ops/fp_quantizer/quantize.py

So the template q_exponent_bits is always the source quantization format's exponent width (4 for E4M3, 3 for E3M2, ...), never the destination dtype's width. It is not a free parameter — there is no call site that binds it to 8 for a BF16 destination.

Why the local re-derivation is correct for the destination

Inside the kernel, q_buf/q_buf1 are the destination-format packed values (shifted by mantisa_bits, i.e. 7 for BF16, then repacked into uint16_t). The masks that read them therefore need the destination-format bit widths:

constexpr uint16_t _exponent_mask = ((1 << new_qeb) - 1) << q_mantisa_bits;
constexpr uint16_t _sign_mask = 1U << (q_mantisa_bits + new_qeb);

where new_qeb == total_q_bits - mantisa_bits - 1 == 16 - 7 - 1 == 8 for a BF16 destination. Using the source q_exponent_bits (4) there instead would mask only 4 bits of a field that actually occupies 8 — the subsequent re-biasing (dst_exponent - bias_q + bias_dst, where bias_q is derived from the source format) and repacking into dst_data[j] with << (mantisa_bits - q_mantisa_bits) shifts would then operate on a truncated/misplaced exponent field. That matches the existing (pre-PR) behavior exactly: on every CUDA version that compiles this code today (CUDA <=12, where the shadow is legal C++), the local variable was always the value in use, and the FP8-GEMM tests (tests/unit/ops/fp_quantizer/test_fp8_gemm.py) pass upstream with that behavior.

Net effect of the PR

  • CUDA <=12: identical generated code to today (local variable was always shadowing; nothing changes).
  • CUDA 13+: compiles at all (currently a hard error). Generated code is bit-for-bit the same as the CUDA <=12 behavior above, since constexpr folding of total_q_bits - mantisa_bits - 1 yields the same value the shadowed local always had.

So this is a pure build-fix, not a behavior change, and the "mis-sized quantized_bits" concern would actually introduce a behavior change relative to every release shipped so far. Happy to add a short comment to the code documenting why new_qeb must track the destination format (not the source q_exponent_bits) so this doesn't have to be re-derived next time — let me know if you'd like that added to this PR.

@bheu bheu closed this Nov 5, 2026
@auto-pr-labeler-with-jev auto-pr-labeler-with-jev Bot added the skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. label Nov 14, 2026
weiji14 added a commit to regro-cf-autotick-bot/deepspeed-feedstock that referenced this pull request Nov 15, 2026
weiji14 added a commit to conda-forge/deepspeed-feedstock that referenced this pull request Nov 16, 2026
* updated v0.20.0

* MNT: Re-rendered with conda-smithy 2026.11.6 and conda-forge-pinning 2026.11.13.06.22.1

* Remove 0002-fp_quantizer-do-not-redeclare-template-parameter.patch

Patch based on deepspeedai/DeepSpeed#8808 probably superseded by deepspeedai/DeepSpeed#8950?

---------

Co-authored-by: Wei Ji <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant