Repository navigation
Conversation
… 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]>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 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; |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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.pySo 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
constexprfolding oftotal_q_bits - mantisa_bits - 1yields 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.
Patch based on deepspeedai/DeepSpeed#8808 probably superseded by deepspeedai/DeepSpeed#8950?
* 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]>
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()andapply_selective_dequantization()incsrc/fp_quantizer/fp_quantize_impl.cuboth declare a localconstexpr int q_exponent_bitsthat shadows the template parameterint q_exponent_bits, so buildingfp_quantizer_opon CUDA 13+ fails with: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++20on 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, breakingop_builder/python setup.py install/python -m buildwhenDS_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 theclip<...>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/412quantized_bits = q_mantisa_bits + q_exponent_bits + 1already implicitly relied on the shadowed value.Testing
-std=c++20): 2 errors, build fails.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.