Skip to content
Open
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
16 changes: 15 additions & 1 deletion transformer_engine/pytorch/csrc/quantizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,18 @@ std::pair<TensorWrapper, py::object> NVFP4Quantizer::create_unquantized_tensor_w
return {std::move(out_cpp), std::move(out_py)};
}

std::vector<size_t> compressShapeTo2D(const std::vector<size_t>& data) {
// If 2 or fewer elements, return as-is
if (data.size() <= 2) {
return data;
}
// Multiply all elements except the last
size_t product = std::accumulate(data.begin(), data.end() - 1, static_cast<size_t>(1),
std::multiplies<size_t>());
// Return new vector of size 2: {product, last}
return std::vector<size_t>{product, data.back()};
}

std::pair<TensorWrapper, py::object> NVFP4Quantizer::convert_and_update_tensor(
py::object tensor) const {
NVTE_CHECK(detail::IsNVFP4Tensor(tensor.ptr()), "NVFP4Quantizer must output to IsNVFP4Tensor.");
Expand Down Expand Up @@ -1289,8 +1301,10 @@ std::pair<TensorWrapper, py::object> NVFP4Quantizer::convert_and_update_tensor(
shape = convert_shape_back_from_fp4(getTensorShape(*columnwise_data), true);
if (rowwise_data) {
auto expected_shape = convert_shape_back_from_fp4(getTensorShape(*rowwise_data), false);
NVTE_CHECK(shape == expected_shape, "NVFP4 row-wise data (shape=", expected_shape,
auto expected_shape_2d = compressShapeTo2D(expected_shape);
NVTE_CHECK(shape == expected_shape_2d, "NVFP4 row-wise data (2D shape=", expected_shape_2d,
") and column-wise data (shape=", shape, ") do not match");
shape = expected_shape;
}
} else { // Already checked columnwise_data_tensor == true
shape = convert_shape_back_from_fp4(getTensorShape(*rowwise_data), false);
Expand Down
Loading