Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion tests/cpp/operator/test_row_scaled_nvfp4_grouped_amax.cu
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,12 @@ void performGroupedFusedAmaxTest(const GroupedAmaxConfig& cfg) {
auto outs = make_row_scaled_outputs("gout_", Ms, K);
std::vector<NVTETensor> 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<NVTETensor> 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();
Expand Down
73 changes: 73 additions & 0 deletions tests/pytorch/nvfp4/test_nvfp4_host_split_fallback.py
Original file line number Diff line number Diff line change
@@ -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),
)
2 changes: 1 addition & 1 deletion transformer_engine/common/cast/cast_grouped.cu
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -563,11 +563,11 @@ void group_hadamard_transform_amax(const Tensor& input_, std::vector<Tensor*>& 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,
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);
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_host_split);
using namespace transformer_engine;
if (num_tensors == 0) {
return;
Expand All @@ -585,9 +585,10 @@ 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,
size_t num_tensors, cudaStream_t stream) {
NVTE_API_CALL(nvte_group_amax);
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) {
return;
Expand Down Expand Up @@ -626,3 +627,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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -1489,17 +1489,17 @@ void group_hadamard_transform_cast_fusion(const Tensor &input_, std::vector<Tens

} // namespace transformer_engine

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_API_CALL(nvte_group_hadamard_transform_cast_fusion);
void nvte_group_hadamard_transform_cast_fusion_host_split(
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_API_CALL(nvte_group_hadamard_transform_cast_fusion_host_split);
using namespace transformer_engine;
NVTE_CHECK(num_tensors > 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<Tensor *> output_list(num_tensors);
for (size_t i = 0; i < num_tensors; ++i) {
output_list[i] = convertNVTETensorCheck(outputs[i]);
Expand All @@ -1517,3 +1517,14 @@ 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,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.
Expand All @@ -493,7 +497,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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ 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,
Expand All @@ -122,6 +128,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.
Expand Down Expand Up @@ -151,6 +161,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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,18 +304,26 @@ 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.
* \param[in] split_sections Size of each tensor split along dimension 0.
* \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.
*
Expand Down
Loading
Loading