@tduncan / Nvidia-Optimization-Stack-VLLM Mirror · read-only / commits / 5985e3427d
Fork 0

A high-throughput and memory-efficient inference and serving engine for LLMs

Mirrored from https://github.com/tduncan-lum/Nvidia-Optimization-Stack---VLLM.git

Synced Nov 16, 23:15 UTC

[Kernel] Vectorized FP8 quantize kernel (#5396)

Inspired by #5146, this PR improves FP8 quantize kernel by vectorizing data transfer to better utilize memory bandwidth. Microbenchmark shows that this improved kernel can achieve 1.0x-1.5x speedup (especially when hidden size is large). In details, we applied 3 optimizations: - Use inverted scale so that most divisions are changed to multiplications. - Unroll the loop by 4 times to improve ILP. - Use vectorized 4 to transfer data between HBM and SRAM.

Cody Yu committed Jun 12, 2024 at 14:07 UTC 5985e3427dc4a10b8483fd08013fa8df563f04fb
2 files changed +94 -6
csrc/quantization/fp8/common.cu
+47 -6
@@ -23,8 +23,8 @@ __device__ __forceinline__ float atomicMaxFloat(float* addr, float value) {
23
24 template <typename scalar_t>
25 __device__ __forceinline__ c10::Float8_e4m3fn scaled_fp8_conversion(
26 - const scalar_t val, const float scale) {
27 - float x = static_cast<float>(val) / scale;
26 + const scalar_t val, const float inverted_scale) {
27 + float x = static_cast<float>(val) * inverted_scale;
28 float r = fmax(-FP8_E4M3_MAX, fmin(x, FP8_E4M3_MAX));
29 return static_cast<c10::Float8_e4m3fn>(r);
30 }
@@ -71,15 +71,56 @@ __global__ void segmented_max_reduction(float* __restrict__ scale,
71 }
72 }
73
74 +template <typename scalar_t>
75 +struct __align__(8) vec4_t {
76 + scalar_t x;
77 + scalar_t y;
78 + scalar_t z;
79 + scalar_t w;
80 +};
81 +
82 +typedef struct __align__(4) {
83 + c10::Float8_e4m3fn x;
84 + c10::Float8_e4m3fn y;
85 + c10::Float8_e4m3fn z;
86 + c10::Float8_e4m3fn w;
87 +}
88 +float8x4_t;
89 +
90 template <typename scalar_t>
91 __global__ void scaled_fp8_quant_kernel(c10::Float8_e4m3fn* __restrict__ out,
92 const scalar_t* __restrict__ input,
93 const float* __restrict__ scale,
94 int64_t num_elems) {
79 - int i = blockDim.x * blockIdx.x + threadIdx.x;
80 - while (i < num_elems) {
81 - out[i] = scaled_fp8_conversion(input[i], *scale);
82 - i += blockDim.x * gridDim.x;
95 + int tid = blockDim.x * blockIdx.x + threadIdx.x;
96 +
97 + // Invert the scale so that we can use multiplications to avoid expensive
98 + // division.
99 + const float inverted_scale = 1.0f / (*scale);
100 +
101 + // Vectorized input/output to better utilize memory bandwidth.
102 + const vec4_t<scalar_t>* vectorized_in =
103 + reinterpret_cast<const vec4_t<scalar_t>*>(input);
104 + float8x4_t* vectorized_out = reinterpret_cast<float8x4_t*>(out);
105 +
106 + int num_vec_elems = num_elems >> 2;
107 +
108 +#pragma unroll 4
109 + for (int i = tid; i < num_vec_elems; i += blockDim.x * gridDim.x) {
110 + vec4_t<scalar_t> in_vec = vectorized_in[i];
111 + float8x4_t out_vec;
112 +
113 + out_vec.x = scaled_fp8_conversion(in_vec.x, inverted_scale);
114 + out_vec.y = scaled_fp8_conversion(in_vec.y, inverted_scale);
115 + out_vec.z = scaled_fp8_conversion(in_vec.z, inverted_scale);
116 + out_vec.w = scaled_fp8_conversion(in_vec.w, inverted_scale);
117 + vectorized_out[i] = out_vec;
118 + }
119 +
120 + // Handle the remaining elements if num_elems is not divisible by 4
121 + for (int i = num_vec_elems * 4 + tid; i < num_elems;
122 + i += blockDim.x * gridDim.x) {
123 + out[i] = scaled_fp8_conversion(input[i], inverted_scale);
124 }
125 }
126
tests/quantization/test_fp8.py
+47
@@ -5,6 +5,7 @@ Run `pytest tests/quantization/test_fp8.py --forked`.
5 import pytest
6 import torch
7
8 +from vllm._custom_ops import scaled_fp8_quant
9 from vllm.model_executor.layers.quantization import QUANTIZATION_METHODS
10 from vllm.model_executor.layers.quantization.fp8 import Fp8LinearMethod
11
@@ -22,3 +23,49 @@ def test_load_fp16_model(vllm_runner) -> None:
23 fc1 = model.model.decoder.layers[0].fc1
24 assert isinstance(fc1.quant_method, Fp8LinearMethod)
25 assert fc1.weight.dtype == torch.float8_e4m3fn
26 +
27 +
28 +@pytest.mark.skipif(
29 + capability < QUANTIZATION_METHODS["fp8"].get_min_capability(),
30 + reason="FP8 is not supported on this GPU type.")
31 +@pytest.mark.parametrize("dtype", [torch.float16, torch.bfloat16])
32 +def test_scaled_fp8_quant(dtype) -> None:
33 +
34 + def quantize_ref(tensor, inv_scale):
35 + # The reference implementation that fully aligns to
36 + # the kernel being tested.
37 + finfo = torch.finfo(torch.float8_e4m3fn)
38 + scale = inv_scale.reciprocal()
39 + qweight = (tensor.to(torch.float32) * scale).clamp(min=finfo.min,
40 + max=finfo.max)
41 + qweight = qweight.to(torch.float8_e4m3fn)
42 + return qweight
43 +
44 + def per_tensor_dequantize(tensor, inv_scale, dtype):
45 + fake_qweight = tensor.to(dtype)
46 + dq_weight = fake_qweight * inv_scale
47 + return dq_weight
48 +
49 + # Note that we use a shape % 4 != 0 to cover edge cases,
50 + # because scaled_fp8_quant is vectorized by 4.
51 + x = (torch.randn(size=(11, 11), device="cuda") * 13).to(dtype)
52 +
53 + # Dynamic quantization
54 + ref_y, inv_scale = scaled_fp8_quant(x, None)
55 + ref_y = per_tensor_dequantize(ref_y, inv_scale, dtype)
56 +
57 + # Reference dynamic quantizaton
58 + y = quantize_ref(x, inv_scale)
59 + assert torch.allclose(ref_y, per_tensor_dequantize(y, inv_scale, dtype))
60 +
61 + # Static quantization
62 + y, _ = scaled_fp8_quant(x, inv_scale)
63 + assert torch.allclose(ref_y, per_tensor_dequantize(y, inv_scale, dtype))
64 +
65 + # Padding
66 + y, _ = scaled_fp8_quant(x, inv_scale, batch_dim_padding=17)
67 + assert y.shape[0] == 17
68 + assert torch.allclose(
69 + ref_y,
70 + per_tensor_dequantize(torch.narrow(y, 0, 0, x.shape[0]), inv_scale,
71 + dtype))