|
| 1 | +# Copyright 2026 FlagOS Contributors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | + |
| 14 | +import pytest |
| 15 | +import torch |
| 16 | +from compressed_tensors.quantization import ( |
| 17 | + QuantizationArgs, |
| 18 | + QuantizationStrategy, |
| 19 | + QuantizationType, |
| 20 | +) |
| 21 | + |
| 22 | +from vllm_fl.quantization.w8a8 import packed |
| 23 | +from vllm_fl.quantization.w8a8.int8_mode import ( |
| 24 | + INT8_MODE_ENV, |
| 25 | + should_use_packed_w8a8, |
| 26 | +) |
| 27 | + |
| 28 | + |
| 29 | +def _weight_args(strategy: QuantizationStrategy) -> QuantizationArgs: |
| 30 | + return QuantizationArgs( |
| 31 | + num_bits=8, |
| 32 | + type=QuantizationType.INT, |
| 33 | + strategy=strategy, |
| 34 | + symmetric=True, |
| 35 | + dynamic=False, |
| 36 | + group_size=128 if strategy == QuantizationStrategy.GROUP else None, |
| 37 | + ) |
| 38 | + |
| 39 | + |
| 40 | +def test_auto_mode_maps_channelwise_packed_int8_to_w8a8(monkeypatch): |
| 41 | + monkeypatch.delenv(INT8_MODE_ENV, raising=False) |
| 42 | + assert should_use_packed_w8a8( |
| 43 | + _weight_args(QuantizationStrategy.CHANNEL), |
| 44 | + None, |
| 45 | + "pack-quantized", |
| 46 | + ) |
| 47 | + assert not should_use_packed_w8a8( |
| 48 | + _weight_args(QuantizationStrategy.GROUP), |
| 49 | + None, |
| 50 | + "pack-quantized", |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +def test_w8a16_mode_keeps_channelwise_checkpoint_weight_only(monkeypatch): |
| 55 | + monkeypatch.setenv(INT8_MODE_ENV, "w8a16") |
| 56 | + assert not should_use_packed_w8a8( |
| 57 | + _weight_args(QuantizationStrategy.CHANNEL), |
| 58 | + None, |
| 59 | + "pack-quantized", |
| 60 | + ) |
| 61 | + |
| 62 | + |
| 63 | +def test_w8a8_mode_rejects_groupwise_checkpoint(monkeypatch): |
| 64 | + monkeypatch.setenv(INT8_MODE_ENV, "w8a8") |
| 65 | + with pytest.raises(ValueError, match="--strategy channel"): |
| 66 | + should_use_packed_w8a8( |
| 67 | + _weight_args(QuantizationStrategy.GROUP), |
| 68 | + None, |
| 69 | + "pack-quantized", |
| 70 | + ) |
| 71 | + |
| 72 | + |
| 73 | +def test_packed_scheme_matches_vllm_024_layer_contract(monkeypatch): |
| 74 | + class FakeKernel: |
| 75 | + def process_weights_after_loading(self, layer): |
| 76 | + assert layer.weight.dtype == torch.int8 |
| 77 | + |
| 78 | + def apply_weights(self, layer, x, bias): |
| 79 | + raise AssertionError("not used") |
| 80 | + |
| 81 | + monkeypatch.setattr( |
| 82 | + packed, |
| 83 | + "init_int8_linear_kernel", |
| 84 | + lambda **kwargs: FakeKernel(), |
| 85 | + ) |
| 86 | + monkeypatch.setattr( |
| 87 | + "vllm.model_executor.parameter.get_tensor_model_parallel_rank", |
| 88 | + lambda: 0, |
| 89 | + ) |
| 90 | + monkeypatch.setattr( |
| 91 | + "vllm.model_executor.parameter.get_tensor_model_parallel_world_size", |
| 92 | + lambda: 1, |
| 93 | + ) |
| 94 | + |
| 95 | + scheme = packed.FLPackedW8A8Scheme(layer_name="model.linear") |
| 96 | + layer = torch.nn.Module() |
| 97 | + scheme.create_weights( |
| 98 | + layer, |
| 99 | + output_partition_sizes=[4, 4], |
| 100 | + input_size_per_partition=8, |
| 101 | + params_dtype=torch.bfloat16, |
| 102 | + weight_loader=lambda *args, **kwargs: None, |
| 103 | + ) |
| 104 | + |
| 105 | + assert layer.logical_widths == [4, 4] |
| 106 | + assert layer.weight_packed.shape == (8, 2) |
| 107 | + assert layer.weight_scale.shape == (8, 1) |
| 108 | + assert layer.weight_scale.dtype == torch.float32 |
| 109 | + |
| 110 | + values = torch.arange(-32, 32, dtype=torch.int8).reshape(8, 8) |
| 111 | + layer.weight_packed.data.copy_( |
| 112 | + (values.to(torch.int16) + 128).to(torch.uint8).contiguous().view(torch.int32) |
| 113 | + ) |
| 114 | + scheme.process_weights_after_loading(layer) |
| 115 | + |
| 116 | + assert not hasattr(layer, "weight_packed") |
| 117 | + assert torch.equal(layer.weight, values) |
0 commit comments