Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
Loading
Loading