test_compressed_tensors.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. """Test model set-up and weight loading for llmcompressor-quantized models.
  2. Run `pytest tests/quantization/test_compressed_tensors.py`.
  3. """
  4. import pytest
  5. import torch
  6. from aphrodite.quantization.compressed_tensors.compressed_tensors import ( # noqa: E501
  7. CompressedTensorsLinearMethod, CompressedTensorsW4A16Sparse24,
  8. CompressedTensorsW8A8Fp8, CompressedTensorsW8A8Int8,
  9. CompressedTensorsW8A16Fp8, CompressedTensorsWNA16)
  10. from aphrodite.quantization.compressed_tensors.utils import QuantizationType
  11. @pytest.mark.parametrize("model_args", [
  12. ("nm-testing/tinyllama-oneshot-w8w8-test-static-shape-change", "tensor",
  13. QuantizationType.INT, 2560),
  14. ("nm-testing/tinyllama-oneshot-w8-channel-a8-tensor", "channel",
  15. QuantizationType.INT, 2560),
  16. ])
  17. def test_compressed_tensors_w8a8_static_setup(aphrodite_runner, model_args):
  18. model_path, strategy, quant_type, shape_0 = model_args
  19. with aphrodite_runner(model_path, enforce_eager=True) as llm:
  20. model = llm.model.llm_engine.model_executor.driver_worker.model_runner.model # noqa: E501
  21. layer = model.model.layers[0]
  22. qkv_proj = layer.self_attn.qkv_proj
  23. o_proj = layer.self_attn.o_proj
  24. gate_up_proj = layer.mlp.gate_up_proj
  25. down_proj = layer.mlp.down_proj
  26. assert isinstance(qkv_proj.quant_method, CompressedTensorsLinearMethod)
  27. assert isinstance(o_proj.quant_method, CompressedTensorsLinearMethod)
  28. assert isinstance(gate_up_proj.quant_method,
  29. CompressedTensorsLinearMethod)
  30. assert isinstance(down_proj.quant_method,
  31. CompressedTensorsLinearMethod)
  32. assert isinstance(qkv_proj.scheme, CompressedTensorsW8A8Int8)
  33. assert qkv_proj.scheme.strategy == strategy
  34. assert qkv_proj.scheme.is_static_input_scheme
  35. expected_type = torch.int8
  36. assert qkv_proj.weight.dtype is expected_type
  37. assert o_proj.weight.dtype is expected_type
  38. assert gate_up_proj.weight.dtype is expected_type
  39. if qkv_proj.scheme.strategy == "tensor":
  40. # Make sure it is a channelwise buffer
  41. # After running process_weights_after_loading
  42. assert len(qkv_proj.weight_scale.shape) == 2
  43. assert qkv_proj.weight_scale.shape[0] == shape_0
  44. assert qkv_proj.weight_scale.shape[1] == 1
  45. assert qkv_proj.weight_scale.dtype is torch.float32
  46. assert qkv_proj.input_scale.dtype is torch.float32
  47. output = llm.generate_greedy("Hello my name is", max_tokens=20)
  48. assert output
  49. def test_compressed_tensors_no_enforce_eager(aphrodite_runner):
  50. model_path = "nm-testing/tinyllama-oneshot-w8w8-test-static-shape-change"
  51. with aphrodite_runner(model_path) as llm:
  52. output = llm.generate_greedy("Hello my name is", max_tokens=20)
  53. assert output
  54. @pytest.mark.parametrize("model_args", [
  55. ("nm-testing/tinyllama-oneshot-w8a8-dynamic-token-v2", "tensor"),
  56. ("nm-testing/tinyllama-oneshot-w8a8-channel-dynamic-token-v2", "channel"),
  57. ])
  58. def test_compressed_tensors_w8a8_dynanmic_per_token(aphrodite_runner,
  59. model_args):
  60. model_path, strategy = model_args
  61. with aphrodite_runner(model_path, dtype=torch.float16) as llm:
  62. model = llm.model.llm_engine.model_executor.driver_worker.model_runner.model # noqa: E501
  63. layer = model.model.layers[0]
  64. qkv_proj = layer.self_attn.qkv_proj
  65. assert isinstance(qkv_proj.quant_method, CompressedTensorsLinearMethod)
  66. assert isinstance(qkv_proj.scheme, CompressedTensorsW8A8Int8)
  67. assert not qkv_proj.scheme.is_static_input_scheme
  68. assert qkv_proj.scheme.strategy == strategy
  69. assert qkv_proj.weight.dtype is torch.int8
  70. output = llm.generate_greedy("Hello my name is", max_tokens=20)
  71. assert output
  72. @pytest.mark.parametrize(
  73. "wNa16_args",
  74. [("nm-testing/tinyllama-oneshot-w4a16-channel-v2", "channel", None, 8),
  75. ("nm-testing/tinyllama-oneshot-w4a16-group128-v2", "group", 128, 8),
  76. ("nm-testing/tinyllama-oneshot-w8a16-per-channel", "channel", None, 4)])
  77. def test_compressed_tensors_wNa16(aphrodite_runner, wNa16_args):
  78. model, strategy, group, pack_factor = wNa16_args
  79. with aphrodite_runner(model) as llm:
  80. model = llm.model.llm_engine.model_executor.driver_worker.model_runner.model # noqa: E501
  81. layer = model.model.layers[0]
  82. qkv_proj = layer.self_attn.qkv_proj
  83. assert isinstance(qkv_proj.quant_method, CompressedTensorsLinearMethod)
  84. assert isinstance(qkv_proj.scheme, CompressedTensorsWNA16)
  85. assert qkv_proj.scheme.strategy == strategy
  86. assert qkv_proj.scheme.group_size == (-1 if group is None else group)
  87. assert qkv_proj.weight_packed.dtype is torch.int32
  88. assert qkv_proj.weight_scale.dtype is torch.float16
  89. assert qkv_proj.scheme.pack_factor == pack_factor
  90. output = llm.generate_greedy("Hello my name is", max_tokens=20)
  91. assert output
  92. def test_compressed_tensors_w4a16_marlin24(aphrodite_runner):
  93. model_path = "nm-testing/llama7b-one-shot-2_4-w4a16-marlin24-t"
  94. with aphrodite_runner(model_path) as llm:
  95. model = llm.model.llm_engine.model_executor.driver_worker.model_runner.model # noqa: E501
  96. layer = model.model.layers[0]
  97. qkv_proj = layer.self_attn.qkv_proj
  98. assert isinstance(qkv_proj.quant_method, CompressedTensorsLinearMethod)
  99. assert isinstance(qkv_proj.scheme, CompressedTensorsW4A16Sparse24)
  100. assert qkv_proj.weight_packed.dtype is torch.int32
  101. output = llm.generate_greedy("Hello my name is", max_tokens=20)
  102. assert output
  103. def test_compressed_tensors_fp8(aphrodite_runner):
  104. model_path = "nm-testing/Meta-Llama-3-8B-FP8-compressed-tensors-test"
  105. with aphrodite_runner(model_path) as llm:
  106. model = llm.model.llm_engine.model_executor.driver_worker.model_runner.model # noqa: E501
  107. layer = model.model.layers[0]
  108. qkv_proj = layer.self_attn.qkv_proj
  109. assert isinstance(qkv_proj.quant_method, CompressedTensorsLinearMethod)
  110. assert isinstance(
  111. qkv_proj.scheme,
  112. (CompressedTensorsW8A8Fp8, CompressedTensorsW8A16Fp8))
  113. assert qkv_proj.input_scale.dtype is torch.float32
  114. if isinstance(qkv_proj.scheme, CompressedTensorsW8A8Fp8):
  115. assert len(qkv_proj.input_scale.shape) == 0
  116. assert qkv_proj.weight.dtype is torch.float8_e4m3fn
  117. assert qkv_proj.weight_scale.dtype is torch.float32
  118. assert len(qkv_proj.weight_scale.shape) == 0
  119. output = llm.generate_greedy("Hello my name is", max_tokens=20)
  120. assert output
  121. def test_compressed_tensors_kv_cache(aphrodite_runner):
  122. model_path = "nm-testing/TinyLlama-1.1B-compressed-tensors-kv-cache-scheme"
  123. with aphrodite_runner(model_path, kv_cache_dtype="fp8") as llm:
  124. output = llm.generate_greedy("Hello world!", max_tokens=20)
  125. assert output