94057c3d3e
PR Test (NPU) / check-changes (push) Has been cancelled
PR Test (NPU) / pr-gate (push) Has been cancelled
PR Test (NPU) / set-image-config (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-1-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (0) (push) Has been cancelled
PR Test (NPU) / stage-b-test-2-npu-a2 (1) (push) Has been cancelled
PR Test (NPU) / stage-b-test-4-npu-a3 (push) Has been cancelled
PR Test (NPU) / stage-b-test-16-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-1-npu-a3 (push) Has been cancelled
PR Test (NPU) / multimodal-gen-test-2-npu-a3 (push) Has been cancelled
PR Test (Arm64) / pr-gate (push) Has been cancelled
PR Test (Arm64) / check-changes (push) Has been cancelled
PR Test (Arm64) / build-test (push) Has been cancelled
PR Test (sgl-router) / gate (push) Has been cancelled
PR Test (sgl-router) / tier-1 — lint (push) Has been cancelled
PR Test (sgl-router) / tier-2 — build + test (push) Has been cancelled
PR Test (sgl-router) / tier-3 — docker (placeholder) (push) Has been cancelled
PR Test (sgl-router) / tier-3 — k8s integration (push) Has been cancelled
PR Test (sgl-router) / tier-3 — e2e (push) Has been cancelled
PR Test (sgl-router) / finish (push) Has been cancelled
PR Test (NPU) / single-node-poc (map[name:qwen3_6_27b_w8a8_1p_in64k_out1k_50ms runner:linux-aarch64-a3-2 test_case:test/registered/ascend/performance/qwen3_6_27b/test_npu_qwen3_6_27b_w8a8_1p_in64k_out1k_50ms.py test_type:perf]) (push) Has been cancelled
PR Test (NPU) / pr-test-npu-finish (push) Has been cancelled
PR Test (Xeon) / pr-gate (push) Has been cancelled
PR Test (Xeon) / check-changes (push) Has been cancelled
PR Test (Xeon) / build-test (, xeon-gnr, base-b-test-cpu) (push) Has been cancelled
PR Test (XPU) / check-changes (push) Has been cancelled
PR Test (XPU) / pr-gate (push) Has been cancelled
PR Test (XPU) / stage-a-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / wait-for-stage-a (push) Has been cancelled
PR Test (XPU) / stage-b-test-1-gpu-xpu (push) Has been cancelled
PR Test (XPU) / finish (push) Has been cancelled
CI Model Inventory / build-inventory (push) Has been cancelled
Lint / lint (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Compilation Check (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Manual Policy (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark - Request Processing (push) Has been cancelled
PR Benchmark (SMG Components) / Benchmark Summary (push) Has been cancelled
PR Test (SMG) / build-wheel (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on windows (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (x86_64 - auto) (push) Has been cancelled
PR Test (SMG) / python-unit-tests (push) Has been cancelled
PR Test (SMG) / unit-tests (push) Has been cancelled
PR Test (SMG) / benchmarks (push) Has been cancelled
PR Test (SMG) / chat-completions (push) Has been cancelled
PR Test (SMG) / chat-completions-4gpu (push) Has been cancelled
PR Test (SMG) / e2e (push) Has been cancelled
PR Test (SMG) / docker-build-test (push) Has been cancelled
PR Test (SMG) / k8s-integration (push) Has been cancelled
PR Test (SMG) / finish (push) Has been cancelled
PR Test (SMG) / summarize-benchmarks (push) Has been cancelled
Release SGLang Model Gateway Docker Image / publish (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on macos (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - auto) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (aarch64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / build on linux (x86_64 - musllinux_1_1) (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Build SDist (push) Has been cancelled
Release SGLang Model Gateway to PyPI / Upload to PyPI (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (aarch64, 12.9, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu129-matrix (x86_64, 12.9, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu129 (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (aarch64, 13.0, 3.10, arm-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / build-cu130-matrix (x86_64, 13.0, 3.10, x64-kernel-build-node) (push) Has been cancelled
Release SGLang Kernels / release-cu130 (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 700) (push) Has been cancelled
Release SGLang Kernels / build-rocm-matrix (3.10, 720) (push) Has been cancelled
Release SGLang Kernels / release-rocm700 (push) Has been cancelled
Release SGLang Kernels / release-rocm720 (push) Has been cancelled
Release SGLang Kernels / build-musa43 (43, 3.10) (push) Has been cancelled
Release SGLang Kernels / release-musa43 (push) Has been cancelled
218 lines
9.6 KiB
Plaintext
218 lines
9.6 KiB
Plaintext
#include <ATen/cuda/CUDAContext.h>
|
|
#include <cuda_fp8.h>
|
|
|
|
#include <cmath>
|
|
#include <flashinfer/vec_dtypes.cuh>
|
|
|
|
#include "utils.h"
|
|
|
|
__device__ __forceinline__ float GroupReduceMax(float val, const int tid) {
|
|
unsigned mask = threadIdx.x % 32 >= 16 ? 0xffff0000 : 0x0000ffff;
|
|
|
|
val = fmaxf(val, __shfl_xor_sync(mask, val, 8));
|
|
val = fmaxf(val, __shfl_xor_sync(mask, val, 4));
|
|
val = fmaxf(val, __shfl_xor_sync(mask, val, 2));
|
|
val = fmaxf(val, __shfl_xor_sync(mask, val, 1));
|
|
return val;
|
|
}
|
|
|
|
template <
|
|
typename T,
|
|
typename DST_DTYPE,
|
|
bool IS_COLUMN_MAJOR = false,
|
|
bool SCALE_UE8M0 = false,
|
|
typename scale_packed_t = std::conditional_t<SCALE_UE8M0, uint32_t, float>>
|
|
__global__ void per_token_group_quant_8bit_kernel(
|
|
const T* __restrict__ input,
|
|
void* __restrict__ output_q,
|
|
scale_packed_t* __restrict__ output_s,
|
|
const int group_size,
|
|
const int num_groups,
|
|
const int groups_per_block,
|
|
const float eps,
|
|
const float min_8bit,
|
|
const float max_8bit,
|
|
const int num_groups_per_row = 0,
|
|
const int scale_stride = 0) {
|
|
const int threads_per_group = 16;
|
|
const int64_t local_group_id = threadIdx.x / threads_per_group;
|
|
const int lane_id = threadIdx.x % threads_per_group;
|
|
|
|
const int64_t block_group_id = blockIdx.x * groups_per_block;
|
|
const int64_t global_group_id = block_group_id + local_group_id;
|
|
const int64_t block_group_offset = global_group_id * group_size;
|
|
|
|
float local_absmax = eps;
|
|
|
|
using scale_element_t = std::conditional_t<SCALE_UE8M0, uint8_t, float>;
|
|
static_assert(sizeof(scale_packed_t) % sizeof(scale_element_t) == 0);
|
|
|
|
const T* group_input = input + block_group_offset;
|
|
DST_DTYPE* group_output = static_cast<DST_DTYPE*>(output_q) + block_group_offset;
|
|
scale_element_t* scale_output;
|
|
|
|
if constexpr (IS_COLUMN_MAJOR) {
|
|
const int num_elems_per_pack = static_cast<int>(sizeof(scale_packed_t) / sizeof(scale_element_t));
|
|
const int row_idx = global_group_id / num_groups_per_row;
|
|
const int col_idx_unpacked = global_group_id % num_groups_per_row;
|
|
const int col_idx = col_idx_unpacked / num_elems_per_pack;
|
|
const int pack_idx = col_idx_unpacked % num_elems_per_pack;
|
|
scale_output = reinterpret_cast<scale_element_t*>(output_s) +
|
|
(col_idx * scale_stride * num_elems_per_pack + row_idx * num_elems_per_pack + pack_idx);
|
|
} else {
|
|
static_assert(!SCALE_UE8M0);
|
|
scale_output = output_s + global_group_id;
|
|
}
|
|
|
|
constexpr uint32_t vec_size = 16 / sizeof(T);
|
|
using vec_t = flashinfer::vec_t<T, vec_size>;
|
|
|
|
const int32_t num_vec_elems = group_size / vec_size;
|
|
|
|
for (int32_t i = lane_id; i < num_vec_elems; i += 16) {
|
|
vec_t input_vec;
|
|
input_vec.cast_load(group_input + i * vec_size);
|
|
|
|
#pragma unroll
|
|
for (uint32_t j = 0; j < vec_size; ++j) {
|
|
float val = static_cast<float>(input_vec[j]);
|
|
float abs_val = fabsf(val);
|
|
local_absmax = fmaxf(local_absmax, abs_val);
|
|
}
|
|
}
|
|
|
|
local_absmax = GroupReduceMax(local_absmax, lane_id);
|
|
|
|
float y_s = local_absmax / max_8bit;
|
|
if constexpr (SCALE_UE8M0) {
|
|
y_s = exp2f(ceilf(log2f(fmaxf(y_s, 1e-10f))));
|
|
}
|
|
|
|
// TODO can optimize
|
|
scale_element_t y_s_quant;
|
|
if constexpr (SCALE_UE8M0) {
|
|
y_s_quant = (uint8_t)(((int)log2f(y_s)) + 127);
|
|
} else {
|
|
y_s_quant = y_s;
|
|
}
|
|
|
|
if (lane_id == 0) {
|
|
*scale_output = y_s_quant;
|
|
}
|
|
|
|
for (int32_t i = lane_id; i < num_vec_elems; i += 16) {
|
|
vec_t input_vec;
|
|
input_vec.cast_load(group_input + i * vec_size);
|
|
|
|
#pragma unroll
|
|
for (uint32_t j = 0; j < vec_size; ++j) {
|
|
float val = static_cast<float>(input_vec[j]);
|
|
float q_val = fminf(fmaxf(val / y_s, min_8bit), max_8bit);
|
|
group_output[i * vec_size + j] = DST_DTYPE(q_val);
|
|
}
|
|
}
|
|
}
|
|
|
|
void sgl_per_token_group_quant_8bit(
|
|
torch::Tensor input,
|
|
torch::Tensor output_q,
|
|
torch::Tensor output_s,
|
|
int64_t group_size,
|
|
double eps,
|
|
double min_8bit,
|
|
double max_8bit,
|
|
bool scale_ue8m0) {
|
|
CHECK_INPUT(input);
|
|
CHECK_INPUT(output_q);
|
|
|
|
const int num_groups = input.numel() / group_size;
|
|
|
|
CHECK_EQ(input.numel() % group_size, 0);
|
|
CHECK_EQ(output_s.dim(), 2);
|
|
|
|
cudaStream_t stream = at::cuda::getCurrentCUDAStream();
|
|
|
|
constexpr int THREADS_PER_GROUP = 16;
|
|
|
|
int groups_per_block = 1;
|
|
|
|
if (num_groups % 16 == 0) {
|
|
groups_per_block = 16;
|
|
} else if (num_groups % 8 == 0) {
|
|
groups_per_block = 8;
|
|
} else if (num_groups % 4 == 0) {
|
|
groups_per_block = 4;
|
|
} else if (num_groups % 2 == 0) {
|
|
groups_per_block = 2;
|
|
}
|
|
|
|
auto dst_type = output_q.scalar_type();
|
|
const int num_blocks = num_groups / groups_per_block;
|
|
const int num_threads = groups_per_block * THREADS_PER_GROUP;
|
|
|
|
const bool is_column_major = output_s.stride(0) < output_s.stride(1);
|
|
const int hidden_dim = input.size(input.dim() - 1);
|
|
const int num_groups_per_row = hidden_dim / group_size;
|
|
const int scale_stride = output_s.stride(1);
|
|
|
|
#define LAUNCH_KERNEL(T, DST_DTYPE) \
|
|
do { \
|
|
dim3 grid(num_blocks); \
|
|
dim3 block(num_threads); \
|
|
if (is_column_major) { \
|
|
if (scale_ue8m0) { \
|
|
per_token_group_quant_8bit_kernel<T, DST_DTYPE, true, true><<<grid, block, 0, stream>>>( \
|
|
static_cast<T*>(input.data_ptr()), \
|
|
output_q.data_ptr(), \
|
|
static_cast<uint32_t*>(output_s.data_ptr()), \
|
|
group_size, \
|
|
num_groups, \
|
|
groups_per_block, \
|
|
(float)eps, \
|
|
(float)min_8bit, \
|
|
(float)max_8bit, \
|
|
num_groups_per_row, \
|
|
scale_stride); \
|
|
} else { \
|
|
per_token_group_quant_8bit_kernel<T, DST_DTYPE, true, false><<<grid, block, 0, stream>>>( \
|
|
static_cast<T*>(input.data_ptr()), \
|
|
output_q.data_ptr(), \
|
|
static_cast<float*>(output_s.data_ptr()), \
|
|
group_size, \
|
|
num_groups, \
|
|
groups_per_block, \
|
|
(float)eps, \
|
|
(float)min_8bit, \
|
|
(float)max_8bit, \
|
|
num_groups_per_row, \
|
|
scale_stride); \
|
|
} \
|
|
} else { \
|
|
assert(!scale_ue8m0); \
|
|
per_token_group_quant_8bit_kernel<T, DST_DTYPE, false><<<grid, block, 0, stream>>>( \
|
|
static_cast<T*>(input.data_ptr()), \
|
|
output_q.data_ptr(), \
|
|
static_cast<float*>(output_s.data_ptr()), \
|
|
group_size, \
|
|
num_groups, \
|
|
groups_per_block, \
|
|
(float)eps, \
|
|
(float)min_8bit, \
|
|
(float)max_8bit); \
|
|
} \
|
|
} while (0)
|
|
|
|
DISPATCH_PYTORCH_DTYPE_TO_CTYPE_FLOAT_FP16(input.scalar_type(), scalar_t, [&] {
|
|
if (dst_type == at::ScalarType::Char) {
|
|
LAUNCH_KERNEL(scalar_t, int8_t);
|
|
return true;
|
|
} else if (dst_type == at::ScalarType::Float8_e4m3fn) {
|
|
LAUNCH_KERNEL(scalar_t, __nv_fp8_e4m3);
|
|
return true;
|
|
}
|
|
return false;
|
|
});
|
|
|
|
#undef LAUNCH_KERNEL
|
|
}
|