Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
c55753c
Clarify NVFP4 host-split APIs and retire frontend split fusions
zhongbozhu Nov 25, 2026
7ab1535
Format NVFP4 host-split API migration
zhongbozhu Nov 25, 2026
40e26cd
Add graph-safe grouped NVFP4 2D quantization with per-expert amax
zhongbozhu Nov 25, 2026
1d98ca7
Document grouped 2D contract and add common API oracle test
zhongbozhu Nov 25, 2026
0ca25e6
Fix grouped 2D CUDA launch dimension type
zhongbozhu Nov 25, 2026
df333ee
Flatten grouped CUDA grids for large single-weight capacities
zhongbozhu Nov 25, 2026
5f82d7a
Tile grouped 2D quantization and fix common test shape constructors
zhongbozhu Nov 25, 2026
7dc47f7
Include NVFP4 conversion primitives without legacy static kernels
zhongbozhu Nov 25, 2026
588ccee
Validate grouped 2D inputs and cover constant blocks
zhongbozhu Nov 25, 2026
018cf2c
Exercise larger randomized expert shapes in grouped 2D quantization
zhongbozhu Nov 25, 2026
cab0fb4
Refresh debug scale views across graph replay and test single NVFP4 w…
zhongbozhu Nov 25, 2026
49b0220
Supply a process group when testing grouped amax reduction rejection
zhongbozhu Nov 25, 2026
92b0835
Vectorize packed FP4 stores in grouped 2D quantization
zhongbozhu Nov 25, 2026
5a4fb1e
Validate dynamic CUDA graph replay directly through the common API
zhongbozhu Nov 25, 2026
c842f34
Pipeline grouped NVFP4 2D quantization with TMA and packed transpose
zhongbozhu Nov 25, 2026
32133fd
Clarify NVFP4 2D scales use the 1D GEMM storage layout
zhongbozhu Nov 26, 2026
88b0433
Support NVFP4 grouped scale swizzle before GEMM
zhongbozhu Nov 25, 2026
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
5 changes: 5 additions & 0 deletions docs/api/c/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
38 changes: 38 additions & 0 deletions docs/api/c/nvfp4_host_split.rst
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions tests/cpp/operator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ add_executable(test_operator
test_cast_mxfp8_grouped.cu
test_cast_mxfp8_grouped_scaled_swiglu.cu
test_cast_nvfp4_transpose.cu
test_cast_nvfp4_grouped_2d.cu
test_row_scaled_nvfp4_grouped_amax.cu
test_row_scaled_nvfp4_grouped_cast.cu
test_cast_float8blockwise.cu
Expand Down
135 changes: 135 additions & 0 deletions tests/cpp/operator/test_cast_nvfp4_grouped_2d.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*************************************************************************
* Copyright (c) 2022-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* See LICENSE for license information.
************************************************************************/
#include <gtest/gtest.h>
#include <transformer_engine/cast.h>
#include <transformer_engine/recipe.h>

#include <algorithm>
#include <cstring>
#include <vector>

#include "../test_common.h"

using namespace transformer_engine;
using namespace test;

class NVFP4Grouped2D : public ::testing::TestWithParam<int> {};

TEST_P(NVFP4Grouped2D, MatchesSingleTensorWithEmptyExpertAndCapacityTail) {
if (getDeviceComputeCapability() < blackwellComputeCapability) GTEST_SKIP();
constexpr size_t capacity = 512, k = 128, count = 3;
const bool rowwise = GetParam() != 2;
const bool columnwise = GetParam() != 1;
std::vector<int64_t> rows{128, 0, 256};
std::vector<int64_t> offsets{0, 128 * k, 128 * k, 384 * k};
Tensor source("source", std::vector<size_t>{capacity, k}, DType::kBFloat16);
auto* host = source.rowwise_cpu_dptr<bf16>();
for (size_t i = 0; i < capacity * k; ++i) {
host[i] = bf16(i < 384 * k ? (static_cast<int>(i % 97) - 48) * 0.125f : 10000.0f);
}
source.from_cpu();
auto device_rows = cuda_alloc<int64_t>(rows.size() * sizeof(int64_t));
auto device_offsets = cuda_alloc<int64_t>(offsets.size() * sizeof(int64_t));
NVTE_CHECK_CUDA(cudaMemcpy(device_rows.get(), rows.data(), rows.size() * sizeof(int64_t),
cudaMemcpyHostToDevice));
NVTE_CHECK_CUDA(cudaMemcpy(device_offsets.get(), offsets.data(), offsets.size() * sizeof(int64_t),
cudaMemcpyHostToDevice));
auto data_r = cuda_alloc<uint8_t>(capacity * k / 2);
auto data_c = cuda_alloc<uint8_t>(capacity * k / 2);
auto scale_r = cuda_alloc<uint8_t>(capacity * k / 16);
auto scale_c = cuda_alloc<uint8_t>(capacity * k / 16);
auto amax_r = cuda_alloc<float>(count * sizeof(float));
auto amax_c = cuda_alloc<float>(count * sizeof(float));
GroupedTensorWrapper input(count, std::vector<size_t>{capacity, k});
input.set_rowwise_data(source.rowwise_dptr(), DType::kBFloat16, std::vector<size_t>{capacity, k});
GroupedTensorWrapper output(count, std::vector<size_t>{capacity, k}, NVTE_NVFP4_1D_SCALING);
output.set_first_dims(device_rows.get(), DType::kInt64, std::vector<size_t>{count});
output.set_tensor_offsets(device_offsets.get(), DType::kInt64, std::vector<size_t>{count + 1});
if (rowwise) {
output.set_rowwise_data(data_r.get(), DType::kFloat4E2M1, std::vector<size_t>{capacity, k});
output.set_rowwise_scale_inv(scale_r.get(), DType::kFloat8E4M3,
std::vector<size_t>{capacity, k / 16});
output.set_amax(amax_r.get(), DType::kFloat32, std::vector<size_t>{count});
}
if (columnwise) {
output.set_columnwise_data(data_c.get(), DType::kFloat4E2M1, std::vector<size_t>{k, capacity});
output.set_columnwise_scale_inv(scale_c.get(), DType::kFloat8E4M3,
std::vector<size_t>{k, capacity / 16});
output.set_columnwise_amax(amax_c.get(), DType::kFloat32, std::vector<size_t>{count});
}
QuantizationConfigWrapper config;
config.set_nvfp4_2d_quantization(true);
nvte_group_quantize(input.data(), output.data(), config, 0);
NVTE_CHECK_CUDA(cudaDeviceSynchronize());
cudaStream_t stream;
cudaGraph_t graph;
cudaGraphExec_t executable;
NVTE_CHECK_CUDA(cudaStreamCreate(&stream));
NVTE_CHECK_CUDA(cudaStreamBeginCapture(stream, cudaStreamCaptureModeGlobal));
nvte_group_quantize(input.data(), output.data(), config, stream);
NVTE_CHECK_CUDA(cudaStreamEndCapture(stream, &graph));
NVTE_CHECK_CUDA(cudaGraphInstantiate(&executable, graph, 0));
const std::vector<std::vector<int64_t>> cases{
{128, 0, 256}, {256, 128, 128}, {0, 0, 0}, {128, 256, 0}};
for (const auto& current_rows : cases) {
rows = current_rows;
offsets[0] = 0;
for (size_t i = 0; i < count; ++i) offsets[i + 1] = offsets[i] + rows[i] * k;
NVTE_CHECK_CUDA(cudaMemcpyAsync(device_rows.get(), rows.data(), rows.size() * sizeof(int64_t),
cudaMemcpyHostToDevice, stream));
NVTE_CHECK_CUDA(cudaMemcpyAsync(device_offsets.get(), offsets.data(),
offsets.size() * sizeof(int64_t), cudaMemcpyHostToDevice,
stream));
NVTE_CHECK_CUDA(cudaGraphLaunch(executable, stream));
NVTE_CHECK_CUDA(cudaStreamSynchronize(stream));
for (size_t i = 0; i < count; ++i) {
if (rows[i] == 0) {
float amax;
if (rowwise) {
NVTE_CHECK_CUDA(
cudaMemcpy(&amax, amax_r.get() + i, sizeof(float), cudaMemcpyDeviceToHost));
EXPECT_EQ(amax, 0.0f);
}
if (columnwise) {
NVTE_CHECK_CUDA(
cudaMemcpy(&amax, amax_c.get() + i, sizeof(float), cudaMemcpyDeviceToHost));
EXPECT_EQ(amax, 0.0f);
}
continue;
}
const size_t m = rows[i];
Tensor expert("expert", std::vector<size_t>{m, k}, DType::kBFloat16);
std::copy_n(host + offsets[i], m * k, expert.rowwise_cpu_dptr<bf16>());
expert.from_cpu();
Tensor reference("reference", std::vector<size_t>{m, k}, DType::kFloat4E2M1, true, true,
NVTE_NVFP4_1D_SCALING);
nvte_compute_amax(expert.data(), reference.data(), 0);
nvte_quantize_v2(expert.data(), reference.data(), config, 0);
NVTE_CHECK_CUDA(cudaDeviceSynchronize());
reference.to_cpu();
for (bool col : {false, true}) {
if (col ? !columnwise : !rowwise) continue;
std::vector<uint8_t> actual(m * k / 2), scales(m * k / 16);
NVTE_CHECK_CUDA(cudaMemcpy(actual.data(),
(col ? data_c.get() : data_r.get()) + offsets[i] / 2,
actual.size(), cudaMemcpyDeviceToHost));
NVTE_CHECK_CUDA(cudaMemcpy(scales.data(),
(col ? scale_c.get() : scale_r.get()) + offsets[i] / 16,
scales.size(), cudaMemcpyDeviceToHost));
const auto* expected =
col ? reference.columnwise_cpu_dptr<fp4e2m1>() : reference.rowwise_cpu_dptr<fp4e2m1>();
const auto* expected_scales = col ? reference.columnwise_cpu_scale_inv_ptr<fp8e4m3>()
: reference.rowwise_cpu_scale_inv_ptr<fp8e4m3>();
EXPECT_EQ(std::memcmp(actual.data(), expected, actual.size()), 0);
EXPECT_EQ(std::memcmp(scales.data(), expected_scales, scales.size()), 0);
}
}
}
NVTE_CHECK_CUDA(cudaGraphExecDestroy(executable));
NVTE_CHECK_CUDA(cudaGraphDestroy(graph));
NVTE_CHECK_CUDA(cudaStreamDestroy(stream));
}

INSTANTIATE_TEST_SUITE_P(Directions, NVFP4Grouped2D, ::testing::Values(0, 1, 2));
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
101 changes: 101 additions & 0 deletions tests/cpp/operator/test_swizzle.cu
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,107 @@ void performTestGroupedSwizzleMXFP8(const int num_tensors, const size_t M, const
num_tensors * col_numel);
}

void performTestGroupedSwizzleNVFP4(const std::vector<std::pair<size_t, size_t>>& shapes,
bool rowwise, bool columnwise) {
using namespace test;
SCOPED_TRACE(::testing::PrintToString(shapes));
std::vector<std::unique_ptr<Tensor>> inputs, outputs;
std::vector<Tensor*> input_ptrs, output_ptrs;
std::vector<uint8_t> expected_row, expected_col;
for (const auto& shape : shapes) {
const std::vector<size_t> dims{shape.first, shape.second};
auto input = std::make_unique<Tensor>("input", dims, DType::kFloat4E2M1, rowwise, columnwise,
NVTE_NVFP4_1D_SCALING);
auto output = std::make_unique<Tensor>("output", dims, DType::kFloat4E2M1, rowwise, columnwise,
NVTE_NVFP4_1D_SCALING);
for (const bool row : {true, false}) {
if (!(row ? rowwise : columnwise)) continue;
if (row) {
input->fill_uniform_rowwise_scale_inv();
} else {
input->fill_uniform_columnwise_scale_inv();
}
const auto s = row ? input->rowwise_scale_inv_shape() : input->columnwise_scale_inv_shape();
const size_t count = s.data[0] * s.data[1];
if (count == 0) continue;
auto* values = row ? input->rowwise_cpu_scale_inv_ptr<uint8_t>()
: input->columnwise_cpu_scale_inv_ptr<uint8_t>();
const size_t m = row ? shape.first : shape.second;
const size_t k = divide_round_up(row ? shape.second : shape.first, size_t{16});
// Poison the padding to catch reads outside the logical scale matrix.
set_scale_inv_padding(values, s.data[0], s.data[1], m, k, 0xFF);
std::vector<uint8_t> padded(values, values + count);
zero_scale_inv_padding(padded.data(), s.data[0], s.data[1], m, k);
auto& expected = row ? expected_row : expected_col;
const size_t start = expected.size();
expected.resize(start + count);
compute_ref_swizzle<128, 4, true>(padded.data(), expected.data() + start, s.data[0],
s.data[1]);
}
input->from_cpu();
input_ptrs.push_back(input.get());
output_ptrs.push_back(output.get());
inputs.emplace_back(std::move(input));
outputs.emplace_back(std::move(output));
}
auto input = build_grouped_tensor(input_ptrs, NVTE_NVFP4_1D_SCALING);
auto output = build_grouped_tensor(output_ptrs, NVTE_NVFP4_1D_SCALING);
// The GEMM fixture adds random gaps between payloads. Swizzle needs only scale
// storage, so describe a logical, pointer-free data extent as the PyTorch wrapper does.
size_t logical_numel = 0;
for (const auto& shape : shapes) logical_numel += shape.first * shape.second;
NVTEBasicTensor data{nullptr, kNVTEFloat4E2M1, nvte_make_shape(&logical_numel, 1)};
for (auto handle : {input.get_handle(), output.get_handle()}) {
if (rowwise) {
nvte_set_grouped_tensor_param(handle, kNVTEGroupedRowwiseData, &data, sizeof(data));
}
if (columnwise) {
nvte_set_grouped_tensor_param(handle, kNVTEGroupedColumnwiseData, &data, sizeof(data));
}
}
const uint8_t compact = 0;
nvte_set_grouped_tensor_param(input.get_handle(), kNVTEGroupedWithGEMMSwizzledScales,
&compact, sizeof(compact));
const uint8_t swizzled = 1;
nvte_set_grouped_tensor_param(output.get_handle(), kNVTEGroupedWithGEMMSwizzledScales, &swizzled,
sizeof(swizzled));
nvte_swizzle_grouped_scaling_factors(input.get_handle(), output.get_handle(), 0);
NVTE_CHECK_CUDA(cudaDeviceSynchronize());
for (const bool row : {true, false}) {
const auto& expected = row ? expected_row : expected_col;
if (expected.empty()) continue;
std::vector<uint8_t> actual(expected.size());
NVTE_CHECK_CUDA(cudaMemcpy(actual.data(),
row ? output.scale_inv.get() : output.columnwise_scale_inv.get(),
actual.size(), cudaMemcpyDeviceToHost));
compareResults(row ? "nvfp4_grouped_row" : "nvfp4_grouped_col", actual.data(), expected.data(),
expected.size());
}
}

TEST(SwizzleTest, GroupedNVFP4UniformAndVariable) {
for (const auto& directions :
{std::make_pair(true, false), std::make_pair(false, true), std::make_pair(true, true)}) {
for (const auto& shape :
{std::make_pair<size_t, size_t>(128, 128), {160, 96}, {128, 3584}, {2176, 4032}}) {
performTestGroupedSwizzleNVFP4({shape, shape, shape}, directions.first, directions.second);
}
performTestGroupedSwizzleNVFP4({{128, 128}, {256, 128}, {384, 128}}, directions.first,
directions.second);
performTestGroupedSwizzleNVFP4({{128, 128}, {128, 256}, {128, 384}}, directions.first,
directions.second);
performTestGroupedSwizzleNVFP4({{160, 96}, {128, 3584}, {384, 192}}, directions.first,
directions.second);
performTestGroupedSwizzleNVFP4({{0, 128}, {128, 128}, {0, 128}, {256, 128}, {0, 128}},
directions.first, directions.second);
// Exceed the persistent grid size so a CTA must reuse shared memory for multiple tiles.
performTestGroupedSwizzleNVFP4({{65536, 128}, {128, 128}, {0, 128}, {256, 128}},
directions.first, directions.second);
performTestGroupedSwizzleNVFP4({{128, 65536}, {128, 128}, {128, 0}, {128, 256}},
directions.first, directions.second);
}
}

class SwizzleTestSuite
: public ::testing::TestWithParam<
std::tuple<std::pair<int, int>, std::pair<bool, bool>, bool>> {};
Expand Down
Loading
Loading