@tduncan / Nvidia-Optimization-Stack-VLLM Mirror · read-only / commits / 5223199e03
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, 19:30 UTC

[Bugfix][FP8] Fix dynamic FP8 Marlin quantization (#7219)

Michael Goin committed Aug 7, 2024 at 14:23 UTC 5223199e03ac3729eb60043a1ef57156c8af1bc9
3 files changed +33 -5
tests/quantization/test_fp8.py
+15 -4
@@ -9,6 +9,7 @@ from tests.quantization.utils import is_quant_method_supported
9 from vllm import _custom_ops as ops
10 from vllm.model_executor.layers.quantization.fp8 import (Fp8KVCacheMethod,
11 Fp8LinearMethod)
12 +from vllm.platforms import current_platform
13
14 MODELS = [
15 "neuralmagic/Meta-Llama-3-8B-Instruct-FP8-KV",
@@ -20,7 +21,12 @@ MODELS = [
21 @pytest.mark.skipif(not is_quant_method_supported("fp8"),
22 reason="FP8 is not supported on this GPU type.")
23 @pytest.mark.parametrize("model_id", MODELS)
23 -def test_model_load_and_run(vllm_runner, model_id: str):
24 +@pytest.mark.parametrize("force_marlin", [False, True])
25 +def test_model_load_and_run(vllm_runner, model_id: str, force_marlin: bool,
26 + monkeypatch) -> None:
27 + if force_marlin:
28 + monkeypatch.setenv("VLLM_TEST_FORCE_FP8_MARLIN", "1")
29 +
30 with vllm_runner(model_id) as llm:
31 # note: this does not test accuracy, just that we can run through
32 # see lm-eval tests for accuracy
@@ -61,7 +67,12 @@ def test_kv_cache_model_load_and_run(vllm_runner, model_id: str):
67 @pytest.mark.skipif(not is_quant_method_supported("fp8"),
68 reason="FP8 is not supported on this GPU type.")
69 @pytest.mark.parametrize("kv_cache_dtype", ["auto", "fp8"])
64 -def test_load_fp16_model(vllm_runner, kv_cache_dtype: str) -> None:
70 +@pytest.mark.parametrize("force_marlin", [False, True])
71 +def test_load_fp16_model(vllm_runner, kv_cache_dtype: str, force_marlin: bool,
72 + monkeypatch) -> None:
73 + if force_marlin:
74 + monkeypatch.setenv("VLLM_TEST_FORCE_FP8_MARLIN", "1")
75 +
76 with vllm_runner("facebook/opt-125m",
77 quantization="fp8",
78 kv_cache_dtype=kv_cache_dtype) as llm:
@@ -75,9 +86,9 @@ def test_load_fp16_model(vllm_runner, kv_cache_dtype: str) -> None:
86 assert attn._k_scale == 1.0
87 assert attn._v_scale == 1.0
88
78 - capability = torch.cuda.get_device_capability()
89 + capability = current_platform.get_device_capability()
90 capability = capability[0] * 10 + capability[1]
80 - if capability >= 89:
91 + if capability >= 89 and not force_marlin:
92 # For GPUs with hardware support, we keep weights in fp8
93 assert fc1.weight.dtype == torch.float8_e4m3fn
94 else:
vllm/envs.py
+8
@@ -52,6 +52,7 @@ if TYPE_CHECKING:
52 CMAKE_BUILD_TYPE: Optional[str] = None
53 VERBOSE: bool = False
54 VLLM_ALLOW_LONG_MAX_MODEL_LEN: bool = False
55 + VLLM_TEST_FORCE_FP8_MARLIN: bool = False
56
57
58 def get_default_cache_root():
@@ -342,6 +343,13 @@ environment_variables: Dict[str, Callable[[], Any]] = {
343 lambda:
344 (os.environ.get("VLLM_ALLOW_LONG_MAX_MODEL_LEN", "0").strip().lower() in
345 ("1", "true")),
346 +
347 + # If set, forces FP8 Marlin to be used for FP8 quantization regardless
348 + # of the hardware support for FP8 compute.
349 + "VLLM_TEST_FORCE_FP8_MARLIN":
350 + lambda:
351 + (os.environ.get("VLLM_TEST_FORCE_FP8_MARLIN", "0").strip().lower() in
352 + ("1", "true")),
353 }
354
355 # end-env-vars-definition
vllm/model_executor/layers/quantization/fp8.py
+10 -1
@@ -4,6 +4,7 @@ import torch
4 from torch.nn import Module
5 from torch.nn.parameter import Parameter
6
7 +import vllm.envs as envs
8 from vllm import _custom_ops as ops
9 from vllm.logger import init_logger
10 from vllm.model_executor.layers.fused_moe import FusedMoE, FusedMoEMethodBase
@@ -118,7 +119,7 @@ class Fp8LinearMethod(LinearMethodBase):
119 # kernel for fast weight-only FP8 quantization
120 capability = current_platform.get_device_capability()
121 capability = capability[0] * 10 + capability[1]
121 - self.use_marlin = capability < 89
122 + self.use_marlin = capability < 89 or envs.VLLM_TEST_FORCE_FP8_MARLIN
123
124 def create_weights(
125 self,
@@ -174,6 +175,14 @@ class Fp8LinearMethod(LinearMethodBase):
175 qweight, weight_scale = ops.scaled_fp8_quant(layer.weight,
176 scale=None)
177
178 + # If using marlin (w8a16), kernel uses channelwise weights,
179 + # so extend the weight scales to be channelwise.
180 + if self.use_marlin:
181 + assert weight_scale.numel() == 1
182 + weight_scale = convert_to_channelwise(
183 + weight_scale.expand(len(layer.logical_widths)),
184 + layer.logical_widths)
185 +
186 # Update the layer with the new values.
187 layer.weight = Parameter(qweight.t(), requires_grad=False)
188 layer.weight_scale = Parameter(weight_scale, requires_grad=False)