TensorRT-LLMs/tests/model_api/test_model_quantization.py
2024-03-19 17:36:42 +08:00

96 lines
3.6 KiB
Python

import os
import sys
import tempfile
from pathlib import Path
import tensorrt_llm
import tensorrt_llm.quantization.mode as quant_algo
from tensorrt_llm.builder import BuildConfig, build
from tensorrt_llm.executor import GenerationExecutor
from tensorrt_llm.models import LLaMAForCausalLM
from tensorrt_llm.models.modeling_utils import QuantizationConfig
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from utils.llm_data import llm_models_root
from utils.util import force_ampere, skip_no_ammo, skip_pre_ada
tensorrt_llm.logger.set_level('info')
@force_ampere
@skip_no_ammo
def test_int4_awq_quantization():
input_text = [
'Born in north-east France, Soyer trained as a',
"What is large language model?"
]
max_batch_size, max_isl, max_osl = 8, 256, 256
hf_model_dir = llm_models_root() / "llama-models/llama-7b-hf"
tokenizer_dir = hf_model_dir
checkpoint_dir = tempfile.TemporaryDirectory("llama-checkpoint").name
quant_config = QuantizationConfig(quant_algo.W4A16_AWQ)
LLaMAForCausalLM.quantize(hf_model_dir,
checkpoint_dir,
quant_config=quant_config,
calib_batches=32,
calib_batch_size=32)
llama = LLaMAForCausalLM.from_checkpoint(checkpoint_dir)
engine = build(
llama,
BuildConfig(max_batch_size=max_batch_size,
max_input_len=max_isl,
max_output_len=max_osl))
engine_dir = "llama-awq-quantized"
engine_temp = tempfile.TemporaryDirectory(engine_dir)
engine_dir = engine_temp.name
engine.save(engine_dir)
with GenerationExecutor.create(Path(engine_dir), tokenizer_dir) as executor:
for idx, output in enumerate(
executor.generate(input_text, max_new_tokens=10)):
print(f"Input: {input_text[idx]}")
print(f'Output: {output.text}')
# TODO: TRTLLM-185, check the score when the test infra is ready, hard coded value is not stable, cause flaky tests in L0
@skip_pre_ada
@skip_no_ammo
def test_fp8_quantization():
input_text = [
'Born in north-east France, Soyer trained as a',
"What is large language model?"
]
max_batch_size, max_isl, max_osl = 8, 256, 256
hf_model_dir = llm_models_root() / "llama-models/llama-7b-hf"
tokenizer_dir = hf_model_dir
checkpoint_dir = tempfile.TemporaryDirectory("llama-checkpoint").name
quant_config = QuantizationConfig(quant_algo.FP8)
LLaMAForCausalLM.quantize(hf_model_dir,
checkpoint_dir,
quant_config=quant_config,
calib_batches=32)
llama = LLaMAForCausalLM.from_checkpoint(checkpoint_dir)
engine = build(
llama,
BuildConfig(max_batch_size=max_batch_size,
max_input_len=max_isl,
max_output_len=max_osl,
strongly_typed=True))
engine_dir = "llama-fp8-quantized"
engine_temp = tempfile.TemporaryDirectory(engine_dir)
engine_dir = engine_temp.name
engine.save(engine_dir)
with GenerationExecutor.create(Path(engine_dir), tokenizer_dir) as executor:
for idx, output in enumerate(
executor.generate(input_text, max_new_tokens=10)):
print(f"Input: {input_text[idx]}")
print(f'Output: {output.text}')
# TODO: TRTLLM-185, check the score when the test infra is ready, hard coded value is not stable, cause flaky tests in L0
if __name__ == "__main__":
test_int4_awq_quantization()
test_fp8_quantization()