// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #include #include "core/providers/cuda/cuda_common.h" #include "contrib_ops/cuda/quantization/gather_block_quantized.h" #include "contrib_ops/cuda/quantization/gather_block_quantized.cuh" namespace onnxruntime { namespace contrib { namespace cuda { using namespace onnxruntime::cuda; #define REGISTER_GATHERBLOCKQUANTIZED(T1, T2, Tind) \ ONNX_OPERATOR_THREE_TYPED_KERNEL_EX( \ GatherBlockQuantized, \ kMSDomain, 1, \ T1, T2, Tind, \ kCudaExecutionProvider, \ (*KernelDefBuilder::Create()) \ .TypeConstraint("T1", DataTypeImpl::GetTensorType()) \ .TypeConstraint("T2", DataTypeImpl::GetTensorType()) \ .TypeConstraint("Tind", DataTypeImpl::GetTensorType()), \ GatherBlockQuantized); REGISTER_GATHERBLOCKQUANTIZED(uint8_t, float, int32_t); REGISTER_GATHERBLOCKQUANTIZED(uint8_t, float, int64_t); REGISTER_GATHERBLOCKQUANTIZED(UInt4x2, float, int32_t); REGISTER_GATHERBLOCKQUANTIZED(UInt4x2, float, int64_t); REGISTER_GATHERBLOCKQUANTIZED(Int4x2, float, int32_t); REGISTER_GATHERBLOCKQUANTIZED(Int4x2, float, int64_t); REGISTER_GATHERBLOCKQUANTIZED(uint8_t, MLFloat16, int64_t); REGISTER_GATHERBLOCKQUANTIZED(uint8_t, MLFloat16, int32_t); REGISTER_GATHERBLOCKQUANTIZED(UInt4x2, MLFloat16, int32_t); REGISTER_GATHERBLOCKQUANTIZED(UInt4x2, MLFloat16, int64_t); REGISTER_GATHERBLOCKQUANTIZED(Int4x2, MLFloat16, int32_t); REGISTER_GATHERBLOCKQUANTIZED(Int4x2, MLFloat16, int64_t); REGISTER_GATHERBLOCKQUANTIZED(UInt4x2, BFloat16, int32_t); REGISTER_GATHERBLOCKQUANTIZED(UInt4x2, BFloat16, int64_t); REGISTER_GATHERBLOCKQUANTIZED(uint8_t, BFloat16, int32_t); REGISTER_GATHERBLOCKQUANTIZED(uint8_t, BFloat16, int64_t); REGISTER_GATHERBLOCKQUANTIZED(Int4x2, BFloat16, int32_t); REGISTER_GATHERBLOCKQUANTIZED(Int4x2, BFloat16, int64_t); #if !defined(DISABLE_FLOAT8_TYPES) #define REGISTER_GATHERBLOCKQUANTIZED_FP8(T1) \ REGISTER_GATHERBLOCKQUANTIZED(T1, float, int32_t); \ REGISTER_GATHERBLOCKQUANTIZED(T1, float, int64_t); \ REGISTER_GATHERBLOCKQUANTIZED(T1, MLFloat16, int32_t); \ REGISTER_GATHERBLOCKQUANTIZED(T1, MLFloat16, int64_t); \ REGISTER_GATHERBLOCKQUANTIZED(T1, BFloat16, int32_t); \ REGISTER_GATHERBLOCKQUANTIZED(T1, BFloat16, int64_t); REGISTER_GATHERBLOCKQUANTIZED_FP8(Float8E4M3FN); REGISTER_GATHERBLOCKQUANTIZED_FP8(Float8E4M3FNUZ); REGISTER_GATHERBLOCKQUANTIZED_FP8(Float8E5M2); REGISTER_GATHERBLOCKQUANTIZED_FP8(Float8E5M2FNUZ); #undef REGISTER_GATHERBLOCKQUANTIZED_FP8 #endif // !defined(DISABLE_FLOAT8_TYPES) #if !defined(DISABLE_FLOAT4_TYPES) REGISTER_GATHERBLOCKQUANTIZED(Float4E2M1x2, float, int32_t); REGISTER_GATHERBLOCKQUANTIZED(Float4E2M1x2, float, int64_t); REGISTER_GATHERBLOCKQUANTIZED(Float4E2M1x2, MLFloat16, int32_t); REGISTER_GATHERBLOCKQUANTIZED(Float4E2M1x2, MLFloat16, int64_t); REGISTER_GATHERBLOCKQUANTIZED(Float4E2M1x2, BFloat16, int32_t); REGISTER_GATHERBLOCKQUANTIZED(Float4E2M1x2, BFloat16, int64_t); #endif // !defined(DISABLE_FLOAT4_TYPES) template GatherBlockQuantized::GatherBlockQuantized(const OpKernelInfo& info) : CudaKernel(info) { if constexpr (IsFpQuantizedV) { bits_ = 0; // Not applicable for FP8/FP4 data. } else { ORT_ENFORCE(info.GetAttr("bits", &bits_).IsOK()); } block_size_ = info.GetAttrOrDefault("block_size", 128); gather_axis_ = info.GetAttrOrDefault("gather_axis", 0); quantize_axis_ = info.GetAttrOrDefault("quantize_axis", 1); // If block size is set, it has to be no smaller than 16 and must be power of 2. // block_size_ & (block_size_ - 1) == 0 checks if block_size_ only has 1 bit set. // block_size_ == 0 is only valid for FP8/FP4 data, meaning the whole quantize_axis dimension // is a single block (one scale per row). if (block_size_ == 0) { ORT_ENFORCE(IsFpQuantizedV, "block_size must be a power of 2 and not smaller than 16."); } else { ORT_ENFORCE(block_size_ >= 16 && ((block_size_ & (block_size_ - 1)) == 0)); } } template Status GatherBlockQuantized::ComputeInternal(OpKernelContext* ctx) const { const Tensor* data = ctx->Input(0); const Tensor* indices = ctx->Input(1); const Tensor* scales = ctx->Input(2); const Tensor* zero_points = ctx->Input(3); auto data_shape = data->Shape().GetDims(); int64_t data_rank = data->Shape().NumDimensions(); const int64_t gather_axis = HandleNegativeAxis(gather_axis_, data_rank); const int64_t quantize_axis = HandleNegativeAxis(quantize_axis_, data_rank); auto indices_shape = indices->Shape().GetDims(); int64_t indices_rank = static_cast(indices->Shape().NumDimensions()); ORT_ENFORCE(quantize_axis == data_rank - 1, "GatherBlockQuantized CUDA requires quantize_axis to be the last axis."); TensorShapeVector output_shape; output_shape.reserve(data_rank - 1 + indices_rank); // Dimension after gather axis int64_t after_gather_dim = 1; // Dimension of indices int64_t ind_dim = 1; // 1) dims before gather_axis for (int64_t i = 0; i < gather_axis; ++i) { output_shape.push_back(data_shape[i]); } // 2) all of indices.shape for (auto dim : indices_shape) { output_shape.push_back(dim); ind_dim *= dim; } // 3) dims after gather_axis for (int64_t i = gather_axis + 1; i < static_cast(data_rank); ++i) { output_shape.push_back(data_shape[i]); after_gather_dim *= data_shape[i]; } // Special int4‐in‐uint8 packing tweak: expand the last dim by components if constexpr (std::is_same_v) { uint32_t components = 8 / static_cast(bits_); if (components > 1) { output_shape.back() *= components; } } Tensor* output = ctx->Output(0, TensorShape(output_shape)); int64_t N = 1; for (auto dim : output_shape) { N *= dim; } if (N == 0) { return Status::OK(); } const auto* data_ptr = data->Data(); const auto* indices_ptr = indices->Data(); const T1* zero_points_ptr = nullptr; if (zero_points != nullptr) { ORT_ENFORCE(!IsFpQuantizedV, "zero_points must not be provided when data is an FP8 or FP4 type."); zero_points_ptr = zero_points->Data(); } // For packed uint8_t with bits < 8, // after_gather_dim has to be adjusted to match // the unpacked output dims for correct kernel indexing int64_t after_gather_dim_unpacked = after_gather_dim; if constexpr (std::is_same_v) { uint32_t components = 8 / static_cast(bits_); if (components > 1) { after_gather_dim_unpacked *= components; } } // block_size_ == 0 (FP8/FP4 only) means the whole quantize_axis dimension is a single block. // Clamp to at least 1 so an empty (0-sized) quantize axis doesn't divide by zero below. int64_t effective_block_size = block_size_ == 0 ? std::max(data_shape[quantize_axis], 1) : block_size_; GatherBlockQuantizedParam param; param.stream = Stream(ctx); param.after_gather_dim = after_gather_dim_unpacked; param.gather_axis_dim = data_shape[gather_axis]; param.ind_dim = ind_dim; param.bits = bits_; param.block_size = effective_block_size; param.gather_axis = gather_axis; param.N = N; param.max_blocks_per_grid = GetDeviceProp().maxGridSize[0]; if constexpr (IsFpQuantizedV) { // Build a generic per-axis description of `scales` so the kernel can (a) reset the block // index at every quantize-axis row boundary, even when data_shape[quantize_axis_] isn't a // multiple of effective_block_size, and (b) support broadcasting on any individual axis // (scales dim == 1 while the corresponding data/block dim isn't), matching the CPU kernel. const auto scales_shape = scales->Shape().GetDims(); ORT_ENFORCE(static_cast(scales_shape.size()) == data_rank, "'scales' must have the same rank as 'data'."); ORT_RETURN_IF_NOT(data_rank <= 8, "GatherBlockQuantized CUDA supports FP8/FP4 data with rank at most 8."); TArray data_dims(static_cast(data_rank)); TArray scale_strides(static_cast(data_rank)); TArray scale_broadcast_axis(static_cast(data_rank)); int64_t stride = 1; for (int64_t i = data_rank - 1; i >= 0; --i) { data_dims[static_cast(i)] = data_shape[i]; const int64_t expected_dim = (i == quantize_axis) ? (data_shape[i] + effective_block_size - 1) / effective_block_size : data_shape[i]; const int64_t actual_dim = scales_shape[i]; const bool is_broadcast = i != quantize_axis && actual_dim == 1 && actual_dim != expected_dim; ORT_ENFORCE(is_broadcast || actual_dim == expected_dim, "'scales' shape does not match 'data' shape (and is not broadcastable) at axis ", i, "."); scale_broadcast_axis[static_cast(i)] = is_broadcast ? 1 : 0; scale_strides[static_cast(i)] = stride; stride *= actual_dim; } param.rank = static_cast(data_rank); param.quantize_axis = quantize_axis; param.data_dims = data_dims; param.scale_strides = scale_strides; param.scale_broadcast_axis = scale_broadcast_axis; } const auto dequantized_type = scales->GetElementType(); if (dequantized_type == ONNX_NAMESPACE::TensorProto_DataType_FLOAT) { const auto* scales_ptr = static_cast(scales->DataRaw()); auto* output_ptr = static_cast(output->MutableDataRaw()); ORT_RETURN_IF_ERROR( LaunchGatherBlockQuantizedKernel(data_ptr, indices_ptr, scales_ptr, zero_points_ptr, output_ptr, param)); } else if (dequantized_type == ONNX_NAMESPACE::TensorProto_DataType_FLOAT16) { const auto* scales_ptr = static_cast(scales->DataRaw()); auto* output_ptr = static_cast(output->MutableDataRaw()); ORT_RETURN_IF_ERROR( LaunchGatherBlockQuantizedKernel(data_ptr, indices_ptr, scales_ptr, zero_points_ptr, output_ptr, param)); } else if (dequantized_type == ONNX_NAMESPACE::TensorProto_DataType_BFLOAT16) { const auto* scales_ptr = static_cast(scales->DataRaw()); auto* output_ptr = static_cast(output->MutableDataRaw()); ORT_RETURN_IF_ERROR( LaunchGatherBlockQuantizedKernel(data_ptr, indices_ptr, scales_ptr, zero_points_ptr, output_ptr, param)); } return Status::OK(); } } // namespace cuda } // namespace contrib } // namespace onnxruntime