From c55753c3e40ca92f4c2ff00048159a937519ff79 Mon Sep 17 00:00:00 2001 From: Zhongbo Zhu Date: Wed, 25 Nov 2026 02:24:21 -0800 Subject: [PATCH 1/4] Clarify NVFP4 host-split APIs and retire frontend split fusions Signed-off-by: Zhongbo Zhu --- docs/api/c/index.rst | 5 + docs/api/c/nvfp4_host_split.rst | 38 ++++ .../test_row_scaled_nvfp4_grouped_amax.cu | 3 +- .../nvfp4/test_nvfp4_host_split_fallback.py | 73 ++++++ .../common/cast/cast_grouped.cu | 2 +- .../group_hadamard_transform.cu | 21 +- ...cast_col_hadamard_transform_cast_fusion.cu | 15 +- .../common/include/transformer_engine/cast.h | 6 +- .../transformer_engine/hadamard_transform.h | 15 ++ .../include/transformer_engine/multi_tensor.h | 10 +- .../pytorch/csrc/extensions/cast.cpp | 212 +++--------------- 11 files changed, 211 insertions(+), 189 deletions(-) create mode 100644 docs/api/c/nvfp4_host_split.rst create mode 100644 tests/pytorch/nvfp4/test_nvfp4_host_split_fallback.py diff --git a/docs/api/c/index.rst b/docs/api/c/index.rst index 82024e25b02..a0e9eb7d16d 100644 --- a/docs/api/c/index.rst +++ b/docs/api/c/index.rst @@ -9,6 +9,11 @@ C/C++ API The C/C++ API allows you to access the custom kernels defined in `libtransformer_engine.so` library directly from C/C++, without Python. +.. toctree:: + :caption: Migration + + nvfp4_host_split + .. toctree:: :caption: Headers diff --git a/docs/api/c/nvfp4_host_split.rst b/docs/api/c/nvfp4_host_split.rst new file mode 100644 index 00000000000..054eddeb7a4 --- /dev/null +++ b/docs/api/c/nvfp4_host_split.rst @@ -0,0 +1,38 @@ +.. + Copyright (c) 2022-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + + See LICENSE for license information. + +NVFP4 Host-Split API Migration +============================= + +Host-split APIs consume a host array of expert lengths. Their new names make +that ownership explicit: + +* ``nvte_group_amax_host_split`` +* ``nvte_group_hadamard_transform_amax_host_split`` +* ``nvte_group_hadamard_transform_cast_fusion_host_split`` + +The previous names remain deprecated forwarding symbols. Renaming does not +change amax semantics or alignment. Reading dynamic expert lengths back from +the device is not CUDA-graph safe; fixed host splits do not require that copy. + +The fused host-split RHT quantizer requires each expert's row count and the +shared hidden dimension to be divisible by 128. Empty experts are allowed. +The PyTorch split-quantize dispatcher checks this before selecting the fusion. +For example, splits ``[64, 64]`` cannot use it even though the total is 128; +they use per-expert quantization with the same RHT recipe instead. Scale layout +conversion follows the selected implementation. + +``nvte_group_nvfp4_quantize_with_amax`` and +``nvte_group_hadamard_transform_cast_fusion_columnwise`` are deprecated. +PyTorch no longer uses these single-direction grouped kernels. Their common +implementations remain available for compatibility, including their existing +alignment and supplied-amax contracts. A 2D quantizer is not a replacement for +the former's 1D recipe. + +``nvte_group_nvfp4_compute_amax`` remains a deprecated host-split alias. +``nvte_group_amax_graph_safe`` is also deprecated as a standalone public entry; +its internal implementations remain available. Ordinary GroupedTensor +quantization uses ``nvte_group_quantize`` for supported recipes. The existing +GroupedTensor RHT APIs are unchanged. diff --git a/tests/cpp/operator/test_row_scaled_nvfp4_grouped_amax.cu b/tests/cpp/operator/test_row_scaled_nvfp4_grouped_amax.cu index 226e5c4c2ed..9a5021fc1a6 100644 --- a/tests/cpp/operator/test_row_scaled_nvfp4_grouped_amax.cu +++ b/tests/cpp/operator/test_row_scaled_nvfp4_grouped_amax.cu @@ -80,11 +80,12 @@ void performGroupedFusedAmaxTest(const GroupedAmaxConfig& cfg) { auto outs = make_row_scaled_outputs("gout_", Ms, K); std::vector out_handles; for (auto& t : outs) out_handles.push_back(t->data()); - nvte_group_amax(packed.data(), out_handles.data(), splits.data(), num_tensors, 0); + nvte_group_amax_host_split(packed.data(), out_handles.data(), splits.data(), num_tensors, 0); auto outs2 = make_row_scaled_outputs("gout2_", Ms, K); std::vector out_handles2; for (auto& t : outs2) out_handles2.push_back(t->data()); + // The deprecated symbol must remain ABI-compatible and numerically identical. nvte_group_amax(packed.data(), out_handles2.data(), splits.data(), num_tensors, 0); cudaDeviceSynchronize(); diff --git a/tests/pytorch/nvfp4/test_nvfp4_host_split_fallback.py b/tests/pytorch/nvfp4/test_nvfp4_host_split_fallback.py new file mode 100644 index 00000000000..951a9958dc1 --- /dev/null +++ b/tests/pytorch/nvfp4/test_nvfp4_host_split_fallback.py @@ -0,0 +1,73 @@ +# Copyright (c) 2022-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# +# See LICENSE for license information. + +import pytest +import torch + +import transformer_engine.pytorch as te +import transformer_engine_torch as tex +from transformer_engine.pytorch import NVFP4Quantizer + + +def _valid_scales(output, direction, rows, cols): + scales = getattr(output, f"_{direction}_scale_inv").view(torch.uint8) + if output._with_gemm_swizzled_scales: + padded_rows, padded_cols = scales.shape + scales = ( + scales.reshape(padded_rows // 128, padded_cols // 4, 32, 4, 4) + .permute(0, 3, 2, 1, 4) + .reshape(padded_rows, padded_cols) + ) + return scales[:rows, : cols // 16] + + +@pytest.mark.skipif(not te.is_nvfp4_available(), reason="NVFP4 requires Blackwell") +@pytest.mark.parametrize("splits", [(128, 128), (64, 64), (0, 64, 192), (128, 0, 128), (0, 0)]) +@pytest.mark.parametrize("directions", [(True, True), (True, False), (False, True)]) +@pytest.mark.parametrize("optimize_for_gemm", [False, True]) +@pytest.mark.parametrize("disable_fusion", [False, True]) +def test_host_split_rht_fallback( + splits, directions, optimize_for_gemm, disable_fusion, monkeypatch +): + monkeypatch.setenv("NVTE_NVFP4_DISABLE_RHT_CAST_FUSION", str(int(disable_fusion))) + torch.manual_seed(123) + x = torch.randn(sum(splits), 128, dtype=torch.bfloat16, device="cuda") + quantizers = [ + NVFP4Quantizer( + fp4_dtype=te.DType.kFloat4E2M1, + rowwise=directions[0], + columnwise=directions[1], + with_rht=True, + with_post_rht_amax=True, + with_random_sign_mask=False, + ) + for _ in splits + ] + for quantizer in quantizers: + quantizer.optimize_for_gemm = optimize_for_gemm + expected = [q(part) for q, part in zip(quantizers, x.split(splits))] + with torch.profiler.profile(activities=[torch.profiler.ProfilerActivity.CUDA]) as prof: + actual = tex.split_quantize(x, splits, quantizers) + # Neither the full fusion nor its per-expert fallback may call retired kernels. + names = [event.name for event in prof.events()] + assert not any("group_quantize_transpose_nvfp4_kernel" in name for name in names) + assert not any("group_rht_gemm_device" in name for name in names) + for rows, result, reference in zip(splits, actual, expected): + for enabled, direction, amax, shape in ( + (directions[0], "rowwise", "_amax_rowwise", (rows, 128)), + (directions[1], "columnwise", "_amax_columnwise", (128, rows)), + ): + if not enabled: + continue + data = getattr(result, f"_{direction}_data") + ref_data = getattr(reference, f"_{direction}_data") + assert data.shape == ref_data.shape + if rows == 0: + continue # Empty-expert storage is not initialized by either path. + assert torch.equal(data.view(torch.uint8), ref_data.view(torch.uint8)) + assert torch.equal(getattr(result, amax), getattr(reference, amax)) + assert torch.equal( + _valid_scales(result, direction, *shape), + _valid_scales(reference, direction, *shape), + ) diff --git a/transformer_engine/common/cast/cast_grouped.cu b/transformer_engine/common/cast/cast_grouped.cu index 0a33f62d5fb..89229af0829 100644 --- a/transformer_engine/common/cast/cast_grouped.cu +++ b/transformer_engine/common/cast/cast_grouped.cu @@ -102,5 +102,5 @@ void nvte_group_nvfp4_quantize_with_amax(const NVTETensor input, NVTETensor *out void nvte_group_nvfp4_compute_amax(const NVTETensor input, NVTETensor *outputs, const size_t *split_sections, const size_t num_tensors, cudaStream_t stream) { - nvte_group_amax(input, outputs, split_sections, num_tensors, stream); + nvte_group_amax_host_split(input, outputs, split_sections, num_tensors, stream); } diff --git a/transformer_engine/common/hadamard_transform/group_hadamard_transform.cu b/transformer_engine/common/hadamard_transform/group_hadamard_transform.cu index f8c04bea0cb..80fe6b0be85 100644 --- a/transformer_engine/common/hadamard_transform/group_hadamard_transform.cu +++ b/transformer_engine/common/hadamard_transform/group_hadamard_transform.cu @@ -563,11 +563,11 @@ void group_hadamard_transform_amax(const Tensor& input_, std::vector& o // in dimension 0, we can treat the entire input as a single tensor. // Although mathmatically 16 multple is enough for this function to be correct, // for this kernel, we required 64 multiple of 16 in dimension 0 for better performance. -void nvte_group_hadamard_transform_amax(const NVTETensor input, NVTETensor* outputs, +void nvte_group_hadamard_transform_amax_host_split(const NVTETensor input, NVTETensor* outputs, const size_t* split_sections, size_t num_tensors, int random_sign_mask, int random_sign_mask_t, cudaStream_t stream) { - NVTE_API_CALL(nvte_group_hadamard_transform_amax); + NVTE_API_CALL(nvte_group_hadamard_transform_amax_host_split); using namespace transformer_engine; if (num_tensors == 0) { return; @@ -585,9 +585,9 @@ void nvte_group_hadamard_transform_amax(const NVTETensor input, NVTETensor* outp } // Grouped-tensor amax -void nvte_group_amax(const NVTETensor input, NVTETensor* outputs, const size_t* split_sections, +void nvte_group_amax_host_split(const NVTETensor input, NVTETensor* outputs, const size_t* split_sections, size_t num_tensors, cudaStream_t stream) { - NVTE_API_CALL(nvte_group_amax); + NVTE_API_CALL(nvte_group_amax_host_split); using namespace transformer_engine; if (num_tensors == 0) { return; @@ -626,3 +626,16 @@ void nvte_group_amax(const NVTETensor input, NVTETensor* outputs, const size_t* group_hadamard_transform_amax(*input_tensor, output_list, split_sections, num_tensors, 0, 0, true, stream); } + +void nvte_group_amax(const NVTETensor input, NVTETensor* outputs, const size_t* split_sections, + size_t num_tensors, cudaStream_t stream) { + nvte_group_amax_host_split(input, outputs, split_sections, num_tensors, stream); +} + +void nvte_group_hadamard_transform_amax(const NVTETensor input, NVTETensor* outputs, + const size_t* split_sections, size_t num_tensors, + int random_sign_mask, int random_sign_mask_t, + cudaStream_t stream) { + nvte_group_hadamard_transform_amax_host_split(input, outputs, split_sections, num_tensors, + random_sign_mask, random_sign_mask_t, stream); +} diff --git a/transformer_engine/common/hadamard_transform/group_row_cast_col_hadamard_transform_cast_fusion.cu b/transformer_engine/common/hadamard_transform/group_row_cast_col_hadamard_transform_cast_fusion.cu index 67452c6aa75..8bf3c2fd902 100644 --- a/transformer_engine/common/hadamard_transform/group_row_cast_col_hadamard_transform_cast_fusion.cu +++ b/transformer_engine/common/hadamard_transform/group_row_cast_col_hadamard_transform_cast_fusion.cu @@ -1489,17 +1489,19 @@ void group_hadamard_transform_cast_fusion(const Tensor &input_, std::vector 0, "Number of tensors should be greater than 0."); Tensor *input_tensor = convertNVTETensorCheck(input); + NVTE_CHECK(input_tensor->flat_last_dim() % 128 == 0, + "Grouped RHT quantization requires the hidden dimension to be a multiple of 128."); std::vector output_list(num_tensors); for (size_t i = 0; i < num_tensors; ++i) { output_list[i] = convertNVTETensorCheck(outputs[i]); @@ -1517,3 +1519,12 @@ void nvte_group_hadamard_transform_cast_fusion(const NVTETensor input, NVTETenso *convertNVTETensorCheck(hadamard_matrix), quant_config_cpp, *quant_workspace_tensor, stream); } + +void nvte_group_hadamard_transform_cast_fusion( + const NVTETensor input, NVTETensor *outputs, const NVTETensor hadamard_matrix, + const size_t *split_sections, const size_t num_tensors, + const NVTEQuantizationConfig quant_config, NVTETensor quant_workspace, cudaStream_t stream) { + nvte_group_hadamard_transform_cast_fusion_host_split( + input, outputs, hadamard_matrix, split_sections, num_tensors, quant_config, quant_workspace, + stream); +} diff --git a/transformer_engine/common/include/transformer_engine/cast.h b/transformer_engine/common/include/transformer_engine/cast.h index 1af93d42919..b29332276b9 100644 --- a/transformer_engine/common/include/transformer_engine/cast.h +++ b/transformer_engine/common/include/transformer_engine/cast.h @@ -467,6 +467,10 @@ void nvte_multi_tensor_quantize(const NVTETensor *inputs, NVTETensor *outputs, cudaStream_t stream); /*! \brief Casts grouped input tensor to quantized output tensors. + * + * \deprecated Legacy host-split rowwise-only NVFP4 quantization with supplied amax. + * Retained for compatibility. Use single-tensor nvte_quantize_v2 for ordinary + * 1D quantization; ordinary grouped 2D quantization is not a replacement. * * \param[in] input Input tensor to be cast. * \param[in,out] outputs Output quantized tensors. @@ -480,7 +484,7 @@ void nvte_group_nvfp4_quantize_with_amax(const NVTETensor input, NVTETensor *out const NVTEQuantizationConfig quant_config, cudaStream_t stream); -/*! \deprecated Use nvte_group_amax instead. */ +/*! \deprecated Use nvte_group_amax_host_split instead. */ void nvte_group_nvfp4_compute_amax(const NVTETensor input, NVTETensor *outputs, const size_t *split_sections, size_t num_tensors, cudaStream_t stream); diff --git a/transformer_engine/common/include/transformer_engine/hadamard_transform.h b/transformer_engine/common/include/transformer_engine/hadamard_transform.h index 8f1a213cec1..7cf87e115d9 100644 --- a/transformer_engine/common/include/transformer_engine/hadamard_transform.h +++ b/transformer_engine/common/include/transformer_engine/hadamard_transform.h @@ -96,6 +96,11 @@ void nvte_quantize_with_hadamard_transform(const NVTETensor input, NVTETensor ou * \param[in] random_sign_mask_t 16-bit sign mask for transposed RHT. * \param[in] stream CUDA stream used for the operation. */ +void nvte_group_hadamard_transform_amax_host_split( + const NVTETensor input, NVTETensor* outputs, const size_t* split_sections, size_t num_tensors, + int random_sign_mask, int random_sign_mask_t, cudaStream_t stream); + +/*! \deprecated Use nvte_group_hadamard_transform_amax_host_split. */ void nvte_group_hadamard_transform_amax(const NVTETensor input, NVTETensor* outputs, const size_t* split_sections, size_t num_tensors, int random_sign_mask, int random_sign_mask_t, @@ -122,6 +127,10 @@ void nvte_group_hadamard_transform_amax_graph_safe(const NVTEGroupedTensor input /*! * \brief Perform the grouped-tensor columnwise Hadamard transform cast fusion operation. * + * \deprecated Legacy host-split columnwise-only quantization. The implementation is retained + * for compatibility. Use the fused host-split entry for eligible shapes or a + * single-tensor RHT path otherwise; their alignment requirements differ. + * * This function is experimental and the API is not stable. Group_ prefix means contiguous input concatenated * * \param[in] input Input tensor to apply Hadamard transform. @@ -151,6 +160,12 @@ void nvte_group_hadamard_transform_cast_fusion_columnwise( * \param[in] quant_workspace Workspace buffer. Must be at least 4 bytes. * \param[in] stream CUDA stream used for the operation. */ +void nvte_group_hadamard_transform_cast_fusion_host_split( + const NVTETensor input, NVTETensor* outputs, const NVTETensor hadamard_matrix, + const size_t* split_sections, size_t num_tensors, const NVTEQuantizationConfig quant_config, + NVTETensor quant_workspace, cudaStream_t stream); + +/*! \deprecated Use nvte_group_hadamard_transform_cast_fusion_host_split. */ void nvte_group_hadamard_transform_cast_fusion(const NVTETensor input, NVTETensor* outputs, const NVTETensor hadamard_matrix, const size_t* split_sections, size_t num_tensors, diff --git a/transformer_engine/common/include/transformer_engine/multi_tensor.h b/transformer_engine/common/include/transformer_engine/multi_tensor.h index 30f7669392e..364b991f379 100644 --- a/transformer_engine/common/include/transformer_engine/multi_tensor.h +++ b/transformer_engine/common/include/transformer_engine/multi_tensor.h @@ -304,7 +304,7 @@ void nvte_multi_tensor_compute_scale_inv_e8m0_cuda(int chunk_size, NVTETensor ** * For each tensor split, compute the maximum absolute value (amax) * and populate the amax buffers of the corresponding output tensor. * All output tensors must have the same tensor format, and the - * tensor format determines which amaxes are computed. + * tensor format determines which amaxes are computed. Split lengths reside on the host. * * \param[in] input Input tensor. * \param[in,out] outputs Array of output tensors. Only amax buffers are updated. @@ -312,10 +312,18 @@ void nvte_multi_tensor_compute_scale_inv_e8m0_cuda(int chunk_size, NVTETensor ** * \param[in] num_tensors Number of tensor splits. * \param[in] stream CUDA stream used for the operation. */ +void nvte_group_amax_host_split(const NVTETensor input, NVTETensor *outputs, + const size_t *split_sections, size_t num_tensors, + cudaStream_t stream); + +/*! \deprecated Use nvte_group_amax_host_split. The signature and behavior are unchanged. */ void nvte_group_amax(const NVTETensor input, NVTETensor *outputs, const size_t *split_sections, size_t num_tensors, cudaStream_t stream); /*! \brief Graph-safe variant of nvte_group_amax for grouped tensors. + * + * \deprecated Standalone grouped amax entry. Existing behavior is retained for compatibility; + * quantization paths should use nvte_group_quantize when supported. * * This function is experimental and the API is not stable. * diff --git a/transformer_engine/pytorch/csrc/extensions/cast.cpp b/transformer_engine/pytorch/csrc/extensions/cast.cpp index 13dd26f6259..4107432286c 100644 --- a/transformer_engine/pytorch/csrc/extensions/cast.cpp +++ b/transformer_engine/pytorch/csrc/extensions/cast.cpp @@ -1854,190 +1854,45 @@ static StochasticRngStateResources setup_stochastic_rounding_rng_states_helper( // Implements split-quantize NVFP4 with Row/Column-wise Hadamard Transform (RHT) void split_quantize_nvfp4_impl_with_rht_helper(const TensorWrapper &input, - const std::vector &input_list, std::vector &output_list, const std::vector &split_sections, const std::vector &quantizers, cudaStream_t stream) { const size_t num_tensors = split_sections.size(); const auto &quantizer = *quantizers.front(); - const bool nvfp4_use_4over6 = quantizer.nvfp4_4over6_mode != kNVTENVFP44Over6Disabled; - NVTE_CHECK(!nvfp4_use_4over6, + NVTE_CHECK(quantizer.nvfp4_4over6_mode == kNVTENVFP44Over6Disabled, "NVFP4 4over6 quantization is not supported with RHT split quantization."); - - std::vector nvte_tensor_input_list; - std::vector nvte_tensor_output_list; - for (size_t i = 0; i < num_tensors; ++i) { - nvte_tensor_input_list.push_back(input_list[i].data()); - nvte_tensor_output_list.push_back(output_list[i].data()); - } - - // trigger the row-col fusion when the split-sections shapes are all 128 aligned for max performance - bool all_aligned_token_dim = - std::all_of(split_sections.begin(), split_sections.end(), - [](size_t split_section) { return split_section % 128 == 0; }); - - // in the case when rowwise and colwise cannot be fused, we have to generate the RNG states twice - // so that rowwise and colwise will have different random numbers - bool need_separate_rng_states = - (!all_aligned_token_dim) && quantizer.rowwise_usage && quantizer.columnwise_usage; - - // Objects for TE C API - std::vector quant_config_list; - std::vector quant_config_list_colwise; - for (size_t i = 0; i < num_tensors; ++i) { - quant_config_list.emplace_back(QuantizationConfigWrapper()); - quant_config_list_colwise.emplace_back(QuantizationConfigWrapper()); - } - - // this is true because we have already built grouped kernels for rowwise and colwise quantization with RHT - bool with_bulk_generate_rng_states = true; - - // Stochastic rounding - bool need_stochastic_rounding = quantizer.stochastic_rounding; - auto stochastic_rng_state_resources = setup_stochastic_rounding_rng_states_helper( - num_tensors, need_stochastic_rounding, with_bulk_generate_rng_states, - need_separate_rng_states, quant_config_list, quant_config_list_colwise); - - for (auto &config : quant_config_list) { - config.set_nvfp4_4over6_mode(quantizer.nvfp4_4over6_mode); - } - for (auto &config : quant_config_list_colwise) { - config.set_nvfp4_4over6_mode(quantizer.nvfp4_4over6_mode); - } - - // Enable NVFP4 kernels to use math operations that sacrifice - // accuracy for performance. These optimizations are experimental - // and inconsistently implemented. - // What math is accelerated? Only the high precision math, so numerical impact is minimal - // 1. replace 1 / x by reciprocal_approximate_ftz(x) - // 2. when RHT cast fusion is available, fusion allows cast to be performed on FP32 data, - // this will essentially remove a round trip between FP32 to BF16 then FP32 - // NVFP4 4over6 candidate error math is controlled separately by - // NVTE_NVFP4_4OVER6_ERR_USE_FAST_MATH. - const auto use_fast_math = transformer_engine::getenv("NVTE_USE_FAST_MATH"); - if (use_fast_math && !nvfp4_use_4over6) { - for (auto &config : quant_config_list) { - config.set_use_fast_math(true); - } - for (auto &config : quant_config_list_colwise) { - config.set_use_fast_math(true); - } + std::vector output_handles; + for (const auto &output : output_list) { + output_handles.push_back(output.data()); } - const auto use_4over6_err_use_fast_math = - transformer_engine::getenv("NVTE_NVFP4_4OVER6_ERR_USE_FAST_MATH"); - if (use_4over6_err_use_fast_math) { - for (auto &config : quant_config_list) { - config.set_nvfp4_4over6_err_use_fast_math(true); - } - for (auto &config : quant_config_list_colwise) { - config.set_nvfp4_4over6_err_use_fast_math(true); - } + std::vector configs(num_tensors); + std::vector unused_columnwise_configs; + auto rng_resources = setup_stochastic_rounding_rng_states_helper( + num_tensors, quantizer.stochastic_rounding, /*with_bulk_generate_rng_states=*/true, + /*need_separate_rng_states=*/false, configs, unused_columnwise_configs); + if (transformer_engine::getenv("NVTE_USE_FAST_MATH")) { + configs[0].set_use_fast_math(true); } - auto &quant_config_list_colwise_to_use = - need_separate_rng_states ? quant_config_list_colwise : quant_config_list; - - // Compute amaxes - if (quantizer.disable_second_level_scale) { - // A null amax tells common NVFP4 kernels to use a unit global scale. - } else if (quantizer.with_post_rht_amax) { - // We need: - // 1. Rowwise amax = amax for input - // 2. Columnwise amax = amax for RHT(input.t) - nvte_group_hadamard_transform_amax( - input.data(), reinterpret_cast(nvte_tensor_output_list.data()), - split_sections.data(), num_tensors, 0, quantizer.rht_matrix_random_sign_mask_t, stream); - } else { - // RHT is enabled, but amax is pre-RHT amax - NVTE_ERROR("NVFP4 split-quantize does not yet support pre-RHT amax"); + if (!quantizer.disable_second_level_scale) { + NVTE_CHECK(quantizer.with_post_rht_amax, + "NVFP4 split-quantize does not yet support pre-RHT amax"); + nvte_group_hadamard_transform_amax_host_split( + input.data(), output_handles.data(), split_sections.data(), num_tensors, 0, + quantizer.rht_matrix_random_sign_mask_t, stream); } - // Check that RHT matrix is available NVTE_CHECK(quantizer.rht_matrix.defined() && quantizer.rht_matrix.numel() > 0, "RHT matrix is not available."); - auto rht_matrix_nvte = makeTransformerEngineTensor(quantizer.rht_matrix); - - if (all_aligned_token_dim) { - // allocate a tile scheduler workspace - auto tile_scheduler_workspace_torch = - at::empty({1}, at::device(at::kCUDA).dtype(torch::kInt32)); - auto nvte_tile_scheduler_workspace = - makeTransformerEngineTensor(tile_scheduler_workspace_torch); - // call the fully-fused grouped kernel for rowwise quantization & colwise RHT quantization transpose - nvte_group_hadamard_transform_cast_fusion( - input.data(), reinterpret_cast(nvte_tensor_output_list.data()), - rht_matrix_nvte.data(), split_sections.data(), num_tensors, quant_config_list[0], - nvte_tile_scheduler_workspace.data(), stream); - } else { - // Separate quantization for rowwise usage and columnwise usage - // Rowwise quantization fusion with grouped version - if (quantizer.rowwise_usage) { - std::vector out_identity_list; - std::vector nvte_tensor_out_identity_list; - for (size_t i = 0; i < num_tensors; i++) { - bool is_empty_split = input_list[i].numel() == 0; - TensorWrapper out_identity(output_list[i].scaling_mode()); - auto out_identity_data = output_list[i].get_rowwise_data(); - auto out_identity_scale_inv = output_list[i].get_rowwise_scale_inv(); - auto out_identity_amax = output_list[i].get_amax(); - if (!is_empty_split) { - out_identity.set_rowwise_data(out_identity_data.data_ptr, - static_cast(out_identity_data.dtype), - out_identity_data.shape); - out_identity.set_rowwise_scale_inv(out_identity_scale_inv.data_ptr, - static_cast(out_identity_scale_inv.dtype), - out_identity_scale_inv.shape); - out_identity.set_amax(out_identity_amax.data_ptr, - static_cast(out_identity_amax.dtype), - out_identity_amax.shape); - } - out_identity_list.emplace_back(std::move(out_identity)); - nvte_tensor_out_identity_list.push_back(out_identity_list.back().data()); - } - nvte_group_nvfp4_quantize_with_amax(input.data(), nvte_tensor_out_identity_list.data(), - split_sections.data(), num_tensors, quant_config_list[0], - stream); - } - - // Columnwise RHT quantization fusion with grouped version - if (quantizer.columnwise_usage) { - std::vector out_transpose_list; - std::vector nvte_tensor_out_transpose_list; - for (size_t i = 0; i < num_tensors; i++) { - bool is_empty_split = input_list[i].numel() == 0; - auto out_columnwise_data = output_list[i].get_columnwise_data(); - auto out_columnwise_scale_inv = output_list[i].get_columnwise_scale_inv(); - auto out_columnwise_amax = output_list[i].get_columnwise_amax(); - - // Create a wrapper for the columnwise output, as the rowwise output. Input is in transposed layout. - TensorWrapper out_transpose(output_list[i].scaling_mode()); - if (!is_empty_split) { - auto [cw_first, cw_last] = get_2d_dims(out_columnwise_data.shape, true); - std::vector colwise_data_shape_2d = {cw_first, cw_last}; - - out_transpose.set_rowwise_data(out_columnwise_data.data_ptr, - static_cast(out_columnwise_data.dtype), - colwise_data_shape_2d); - out_transpose.set_rowwise_scale_inv(out_columnwise_scale_inv.data_ptr, - static_cast(out_columnwise_scale_inv.dtype), - out_columnwise_scale_inv.shape); - out_transpose.set_amax(out_columnwise_amax.data_ptr, - static_cast(out_columnwise_amax.dtype), - out_columnwise_amax.shape); - } - out_transpose_list.emplace_back(std::move(out_transpose)); - nvte_tensor_out_transpose_list.push_back(out_transpose_list.back().data()); - } - nvte_group_hadamard_transform_cast_fusion_columnwise( - input.data(), reinterpret_cast(nvte_tensor_out_transpose_list.data()), - rht_matrix_nvte.data(), split_sections.data(), num_tensors, - quant_config_list_colwise_to_use[0], stream); - } - } + auto rht_matrix = makeTransformerEngineTensor(quantizer.rht_matrix); + auto workspace = at::empty({1}, at::device(at::kCUDA).dtype(torch::kInt32)); + auto workspace_tensor = makeTransformerEngineTensor(workspace); + nvte_group_hadamard_transform_cast_fusion_host_split( + input.data(), output_handles.data(), rht_matrix.data(), split_sections.data(), num_tensors, + configs[0], workspace_tensor.data(), stream); } - void split_quantize_nvfp4_impl_helper(const TensorWrapper &input, const std::vector &input_list, std::vector &output_list, @@ -2118,7 +1973,7 @@ void split_quantize_nvfp4_impl_helper(const TensorWrapper &input, NVTE_CHECK(amax_ptr != nullptr, "Could not find amax pointer"); output_list[i].set_amax(amax_ptr, DType::kFloat32, std::vector{1}); } - nvte_group_amax(input.data(), reinterpret_cast(nvte_tensor_output_list.data()), + nvte_group_amax_host_split(input.data(), reinterpret_cast(nvte_tensor_output_list.data()), split_sections.data(), num_tensors, stream); for (size_t i = 0; i < num_tensors; i++) { output_list[i].set_amax(orig_amax_ptr_list[i], DType::kFloat32, std::vector{1}); @@ -2188,22 +2043,21 @@ void split_quantize_nvfp4_impl(const TensorWrapper &input, // CUDA stream auto stream = at::cuda::getCurrentCUDAStream(); - // The grouped Hadamard transform kernels are implemented for the SM100 family - // only. On other architectures, where - // NVFP4Quantizer::is_eligible_for_rht_cast_fusion is false as well, quantize - // each split on its own instead. That takes the generic unfused RHT path. - const int sm = transformer_engine::cuda::sm_arch(); - const bool grouped_rht_supported = sm >= 100 && sm <= 110; - - const bool emits_gemm_swizzled_scales = + // Check each expert, not just the packed input. The grouped fusion requires + // 128-row alignment; the per-tensor path also supports 64-row splits and + // handles disabled fusion or unsupported architectures without changing RHT. + const bool grouped_rht_supported = quantizer.with_rht && std::all_of(input_list.begin(), input_list.end(), [](const auto &input) { return NVFP4Quantizer::is_eligible_for_rht_cast_fusion(get_tensor_shape(input), /*for_grouped_kernel=*/true); }); + // Per-tensor RHT quantizers own their scale conversion, including shape fallbacks. + const bool per_tensor_rht = quantizer.with_rht && !grouped_rht_supported; + const bool emits_gemm_swizzled_scales = grouped_rht_supported; std::vector needs_swizzle(num_tensors); for (size_t i = 0; i < num_tensors; ++i) { - needs_swizzle[i] = outputs[i].get_with_gemm_swizzled_scales() && !emits_gemm_swizzled_scales && + needs_swizzle[i] = !per_tensor_rht && outputs[i].get_with_gemm_swizzled_scales() && !emits_gemm_swizzled_scales && input_list[i].numel() != 0; } std::vector scale_buffers; @@ -2218,7 +2072,7 @@ void split_quantize_nvfp4_impl(const TensorWrapper &input, NVTE_CHECK(input.dtype() == DType::kBFloat16, "RHT is only supported for bfloat16 input"); if (grouped_rht_supported) { // Fuse the rowwise and colwise into one when the kernel is ready - split_quantize_nvfp4_impl_with_rht_helper(input, input_list, output_list, split_sections, + split_quantize_nvfp4_impl_with_rht_helper(input, output_list, split_sections, quantizers, stream); } else { for (size_t i = 0; i < num_tensors; ++i) { From 7ab1535f55036fe69d28f6a0841b662b38962035 Mon Sep 17 00:00:00 2001 From: Zhongbo Zhu Date: Wed, 25 Nov 2026 02:31:25 -0800 Subject: [PATCH 2/4] Format NVFP4 host-split API migration Signed-off-by: Zhongbo Zhu --- .../group_hadamard_transform.cu | 13 +++++----- ...cast_col_hadamard_transform_cast_fusion.cu | 26 +++++++++---------- .../transformer_engine/hadamard_transform.h | 7 ++--- .../pytorch/csrc/extensions/cast.cpp | 19 +++++++------- 4 files changed, 34 insertions(+), 31 deletions(-) diff --git a/transformer_engine/common/hadamard_transform/group_hadamard_transform.cu b/transformer_engine/common/hadamard_transform/group_hadamard_transform.cu index 80fe6b0be85..e9e18048c96 100644 --- a/transformer_engine/common/hadamard_transform/group_hadamard_transform.cu +++ b/transformer_engine/common/hadamard_transform/group_hadamard_transform.cu @@ -564,9 +564,9 @@ void group_hadamard_transform_amax(const Tensor& input_, std::vector& o // Although mathmatically 16 multple is enough for this function to be correct, // for this kernel, we required 64 multiple of 16 in dimension 0 for better performance. void nvte_group_hadamard_transform_amax_host_split(const NVTETensor input, NVTETensor* outputs, - const size_t* split_sections, size_t num_tensors, - int random_sign_mask, int random_sign_mask_t, - cudaStream_t stream) { + const size_t* split_sections, size_t num_tensors, + int random_sign_mask, int random_sign_mask_t, + cudaStream_t stream) { NVTE_API_CALL(nvte_group_hadamard_transform_amax_host_split); using namespace transformer_engine; if (num_tensors == 0) { @@ -585,8 +585,9 @@ void nvte_group_hadamard_transform_amax_host_split(const NVTETensor input, NVTET } // Grouped-tensor amax -void nvte_group_amax_host_split(const NVTETensor input, NVTETensor* outputs, const size_t* split_sections, - size_t num_tensors, cudaStream_t stream) { +void nvte_group_amax_host_split(const NVTETensor input, NVTETensor* outputs, + const size_t* split_sections, size_t num_tensors, + cudaStream_t stream) { NVTE_API_CALL(nvte_group_amax_host_split); using namespace transformer_engine; if (num_tensors == 0) { @@ -637,5 +638,5 @@ void nvte_group_hadamard_transform_amax(const NVTETensor input, NVTETensor* outp int random_sign_mask, int random_sign_mask_t, cudaStream_t stream) { nvte_group_hadamard_transform_amax_host_split(input, outputs, split_sections, num_tensors, - random_sign_mask, random_sign_mask_t, stream); + random_sign_mask, random_sign_mask_t, stream); } diff --git a/transformer_engine/common/hadamard_transform/group_row_cast_col_hadamard_transform_cast_fusion.cu b/transformer_engine/common/hadamard_transform/group_row_cast_col_hadamard_transform_cast_fusion.cu index 8bf3c2fd902..e86c8ae6166 100644 --- a/transformer_engine/common/hadamard_transform/group_row_cast_col_hadamard_transform_cast_fusion.cu +++ b/transformer_engine/common/hadamard_transform/group_row_cast_col_hadamard_transform_cast_fusion.cu @@ -1489,12 +1489,10 @@ void group_hadamard_transform_cast_fusion(const Tensor &input_, std::vector 0, "Number of tensors should be greater than 0."); @@ -1520,11 +1518,13 @@ void nvte_group_hadamard_transform_cast_fusion_host_split(const NVTETensor input *quant_workspace_tensor, stream); } -void nvte_group_hadamard_transform_cast_fusion( - const NVTETensor input, NVTETensor *outputs, const NVTETensor hadamard_matrix, - const size_t *split_sections, const size_t num_tensors, - const NVTEQuantizationConfig quant_config, NVTETensor quant_workspace, cudaStream_t stream) { - nvte_group_hadamard_transform_cast_fusion_host_split( - input, outputs, hadamard_matrix, split_sections, num_tensors, quant_config, quant_workspace, - stream); +void nvte_group_hadamard_transform_cast_fusion(const NVTETensor input, NVTETensor *outputs, + const NVTETensor hadamard_matrix, + const size_t *split_sections, + const size_t num_tensors, + const NVTEQuantizationConfig quant_config, + NVTETensor quant_workspace, cudaStream_t stream) { + nvte_group_hadamard_transform_cast_fusion_host_split(input, outputs, hadamard_matrix, + split_sections, num_tensors, quant_config, + quant_workspace, stream); } diff --git a/transformer_engine/common/include/transformer_engine/hadamard_transform.h b/transformer_engine/common/include/transformer_engine/hadamard_transform.h index 7cf87e115d9..00e2d4c1da6 100644 --- a/transformer_engine/common/include/transformer_engine/hadamard_transform.h +++ b/transformer_engine/common/include/transformer_engine/hadamard_transform.h @@ -96,9 +96,10 @@ void nvte_quantize_with_hadamard_transform(const NVTETensor input, NVTETensor ou * \param[in] random_sign_mask_t 16-bit sign mask for transposed RHT. * \param[in] stream CUDA stream used for the operation. */ -void nvte_group_hadamard_transform_amax_host_split( - const NVTETensor input, NVTETensor* outputs, const size_t* split_sections, size_t num_tensors, - int random_sign_mask, int random_sign_mask_t, cudaStream_t stream); +void nvte_group_hadamard_transform_amax_host_split(const NVTETensor input, NVTETensor* outputs, + const size_t* split_sections, size_t num_tensors, + int random_sign_mask, int random_sign_mask_t, + cudaStream_t stream); /*! \deprecated Use nvte_group_hadamard_transform_amax_host_split. */ void nvte_group_hadamard_transform_amax(const NVTETensor input, NVTETensor* outputs, diff --git a/transformer_engine/pytorch/csrc/extensions/cast.cpp b/transformer_engine/pytorch/csrc/extensions/cast.cpp index 4107432286c..6b446f811a9 100644 --- a/transformer_engine/pytorch/csrc/extensions/cast.cpp +++ b/transformer_engine/pytorch/csrc/extensions/cast.cpp @@ -1879,9 +1879,9 @@ void split_quantize_nvfp4_impl_with_rht_helper(const TensorWrapper &input, if (!quantizer.disable_second_level_scale) { NVTE_CHECK(quantizer.with_post_rht_amax, "NVFP4 split-quantize does not yet support pre-RHT amax"); - nvte_group_hadamard_transform_amax_host_split( - input.data(), output_handles.data(), split_sections.data(), num_tensors, 0, - quantizer.rht_matrix_random_sign_mask_t, stream); + nvte_group_hadamard_transform_amax_host_split(input.data(), output_handles.data(), + split_sections.data(), num_tensors, 0, + quantizer.rht_matrix_random_sign_mask_t, stream); } NVTE_CHECK(quantizer.rht_matrix.defined() && quantizer.rht_matrix.numel() > 0, @@ -1973,8 +1973,9 @@ void split_quantize_nvfp4_impl_helper(const TensorWrapper &input, NVTE_CHECK(amax_ptr != nullptr, "Could not find amax pointer"); output_list[i].set_amax(amax_ptr, DType::kFloat32, std::vector{1}); } - nvte_group_amax_host_split(input.data(), reinterpret_cast(nvte_tensor_output_list.data()), - split_sections.data(), num_tensors, stream); + nvte_group_amax_host_split(input.data(), + reinterpret_cast(nvte_tensor_output_list.data()), + split_sections.data(), num_tensors, stream); for (size_t i = 0; i < num_tensors; i++) { output_list[i].set_amax(orig_amax_ptr_list[i], DType::kFloat32, std::vector{1}); } @@ -2057,8 +2058,8 @@ void split_quantize_nvfp4_impl(const TensorWrapper &input, const bool emits_gemm_swizzled_scales = grouped_rht_supported; std::vector needs_swizzle(num_tensors); for (size_t i = 0; i < num_tensors; ++i) { - needs_swizzle[i] = !per_tensor_rht && outputs[i].get_with_gemm_swizzled_scales() && !emits_gemm_swizzled_scales && - input_list[i].numel() != 0; + needs_swizzle[i] = !per_tensor_rht && outputs[i].get_with_gemm_swizzled_scales() && + !emits_gemm_swizzled_scales && input_list[i].numel() != 0; } std::vector scale_buffers; auto compact_outputs = @@ -2072,8 +2073,8 @@ void split_quantize_nvfp4_impl(const TensorWrapper &input, NVTE_CHECK(input.dtype() == DType::kBFloat16, "RHT is only supported for bfloat16 input"); if (grouped_rht_supported) { // Fuse the rowwise and colwise into one when the kernel is ready - split_quantize_nvfp4_impl_with_rht_helper(input, output_list, split_sections, - quantizers, stream); + split_quantize_nvfp4_impl_with_rht_helper(input, output_list, split_sections, quantizers, + stream); } else { for (size_t i = 0; i < num_tensors; ++i) { if (input_list[i].numel() == 0) { From ce6afdd2628a4d240b9988a2393c23db6d129b8f Mon Sep 17 00:00:00 2001 From: Tim Moon Date: Fri, 4 Dec 2026 18:24:03 -0800 Subject: [PATCH 3/4] Review comments Delete unnecessary docs. Handle edge case with NVFP4 quant with no second scaling and pre-RHT amax config. Signed-off-by: Tim Moon --- docs/api/c/index.rst | 5 --- docs/api/c/nvfp4_host_split.rst | 38 ---------------- transformer_engine/pytorch/csrc/quantizer.cpp | 43 +++++++++---------- 3 files changed, 20 insertions(+), 66 deletions(-) delete mode 100644 docs/api/c/nvfp4_host_split.rst diff --git a/docs/api/c/index.rst b/docs/api/c/index.rst index a0e9eb7d16d..82024e25b02 100644 --- a/docs/api/c/index.rst +++ b/docs/api/c/index.rst @@ -9,11 +9,6 @@ C/C++ API The C/C++ API allows you to access the custom kernels defined in `libtransformer_engine.so` library directly from C/C++, without Python. -.. toctree:: - :caption: Migration - - nvfp4_host_split - .. toctree:: :caption: Headers diff --git a/docs/api/c/nvfp4_host_split.rst b/docs/api/c/nvfp4_host_split.rst deleted file mode 100644 index 054eddeb7a4..00000000000 --- a/docs/api/c/nvfp4_host_split.rst +++ /dev/null @@ -1,38 +0,0 @@ -.. - Copyright (c) 2022-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. - - See LICENSE for license information. - -NVFP4 Host-Split API Migration -============================= - -Host-split APIs consume a host array of expert lengths. Their new names make -that ownership explicit: - -* ``nvte_group_amax_host_split`` -* ``nvte_group_hadamard_transform_amax_host_split`` -* ``nvte_group_hadamard_transform_cast_fusion_host_split`` - -The previous names remain deprecated forwarding symbols. Renaming does not -change amax semantics or alignment. Reading dynamic expert lengths back from -the device is not CUDA-graph safe; fixed host splits do not require that copy. - -The fused host-split RHT quantizer requires each expert's row count and the -shared hidden dimension to be divisible by 128. Empty experts are allowed. -The PyTorch split-quantize dispatcher checks this before selecting the fusion. -For example, splits ``[64, 64]`` cannot use it even though the total is 128; -they use per-expert quantization with the same RHT recipe instead. Scale layout -conversion follows the selected implementation. - -``nvte_group_nvfp4_quantize_with_amax`` and -``nvte_group_hadamard_transform_cast_fusion_columnwise`` are deprecated. -PyTorch no longer uses these single-direction grouped kernels. Their common -implementations remain available for compatibility, including their existing -alignment and supplied-amax contracts. A 2D quantizer is not a replacement for -the former's 1D recipe. - -``nvte_group_nvfp4_compute_amax`` remains a deprecated host-split alias. -``nvte_group_amax_graph_safe`` is also deprecated as a standalone public entry; -its internal implementations remain available. Ordinary GroupedTensor -quantization uses ``nvte_group_quantize`` for supported recipes. The existing -GroupedTensor RHT APIs are unchanged. diff --git a/transformer_engine/pytorch/csrc/quantizer.cpp b/transformer_engine/pytorch/csrc/quantizer.cpp index eccebf62acb..a7870b16219 100644 --- a/transformer_engine/pytorch/csrc/quantizer.cpp +++ b/transformer_engine/pytorch/csrc/quantizer.cpp @@ -2724,7 +2724,9 @@ void NVFP4Quantizer::quantize_impl(const TensorWrapper& input, TensorWrapper& ou } // Compute amax. - if (this->with_rht) { + if (!compute_amax || this->disable_second_level_scale || row_scaled_nvfp4) { + // Amax compute isn't needed + } else if (this->with_rht) { if (input.dtype() != DType::kBFloat16) { NVTE_ERROR("RHT is only supported for bfloat16 input, got dtype enum value ", static_cast(input.dtype())); @@ -2733,12 +2735,10 @@ void NVFP4Quantizer::quantize_impl(const TensorWrapper& input, TensorWrapper& ou // We need: // 1. Rowwise amax = amax for input // 2. Columnwise amax = amax for RHT(input.t) - if (compute_amax && !this->disable_second_level_scale) { - NVTE_SCOPED_GIL_RELEASE({ - nvte_hadamard_transform_amax(input.data(), kernel_out.data(), 0, - this->rht_matrix_random_sign_mask_t, stream); - }); - } + NVTE_SCOPED_GIL_RELEASE({ + nvte_hadamard_transform_amax(input.data(), kernel_out.data(), 0, + this->rht_matrix_random_sign_mask_t, stream); + }); } else { // raise error since it's not supported yet NVTE_ERROR( @@ -2746,23 +2746,20 @@ void NVFP4Quantizer::quantize_impl(const TensorWrapper& input, TensorWrapper& ou "Use with_post_rht_amax=true instead."); } } else { // Without RHT - if (compute_amax && !row_scaled_nvfp4 && !this->disable_second_level_scale) { - // Amax pointers - auto rowwise_amax_ptr = kernel_out.get_amax().data_ptr; - auto columnwise_amax_ptr = kernel_out.get_columnwise_amax().data_ptr; - void* amax_ptr = rowwise_amax_ptr != nullptr ? rowwise_amax_ptr : columnwise_amax_ptr; - NVTE_CHECK(amax_ptr != nullptr, "Could not find amax pointer"); - - // Compute amax of input tensor. When both the row-wise and the column-wise amax are - // allocated, the amax kernel updates both (identical values) - kernel_out.set_amax(amax_ptr, DType::kFloat32, std::vector{1}); - NVTE_SCOPED_GIL_RELEASE({ - nvte_compute_amax_with_config(input.data(), kernel_out.data(), quant_config, stream); - }); - kernel_out.set_amax(rowwise_amax_ptr, DType::kFloat32, std::vector{1}); - } + // Amax pointers + auto rowwise_amax_ptr = kernel_out.get_amax().data_ptr; + auto columnwise_amax_ptr = kernel_out.get_columnwise_amax().data_ptr; + void* amax_ptr = rowwise_amax_ptr != nullptr ? rowwise_amax_ptr : columnwise_amax_ptr; + NVTE_CHECK(amax_ptr != nullptr, "Could not find amax pointer"); + + // Compute amax of input tensor. When both the row-wise and the column-wise amax are + // allocated, the amax kernel updates both (identical values) + kernel_out.set_amax(amax_ptr, DType::kFloat32, std::vector{1}); + NVTE_SCOPED_GIL_RELEASE({ + nvte_compute_amax_with_config(input.data(), kernel_out.data(), quant_config, stream); + }); + kernel_out.set_amax(rowwise_amax_ptr, DType::kFloat32, std::vector{1}); } - reduce_amaxes(); // Fast math toggle: RHT transform can be accelerated From 0f3ea9b9aa09e95dbd778942ab7499e37d626ef8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 5 Dec 2026 02:31:49 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- transformer_engine/pytorch/csrc/quantizer.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/transformer_engine/pytorch/csrc/quantizer.cpp b/transformer_engine/pytorch/csrc/quantizer.cpp index 68f8c48b997..cbcce9bf118 100644 --- a/transformer_engine/pytorch/csrc/quantizer.cpp +++ b/transformer_engine/pytorch/csrc/quantizer.cpp @@ -2749,9 +2749,8 @@ void NVFP4Quantizer::quantize_impl(const TensorWrapper& input, TensorWrapper& ou // Compute amax of input tensor. When both the row-wise and the column-wise amax are // allocated, the amax kernel updates both (identical values) kernel_out.set_amax(amax_ptr, DType::kFloat32, std::vector{1}); - NVTE_SCOPED_GIL_RELEASE({ - nvte_compute_amax_with_config(input.data(), kernel_out.data(), quant_config, stream); - }); + NVTE_SCOPED_GIL_RELEASE( + { nvte_compute_amax_with_config(input.data(), kernel_out.data(), quant_config, stream); }); kernel_out.set_amax(rowwise_amax_ptr, DType::kFloat32, std::vector{1}); } reduce_amaxes();