AutoAWQ
AutoAWQ
모델을 더 가볍고 빠르게 만들고 싶다면, 가장 확실한 방법 중 하나가 양자화(quantization) 예요. 그중에서도 AutoAWQ는 4비트 양자화 모델을 만들 때 흔히 쓰이던 도구 중 하나였죠. 이 페이지에서는 AutoAWQ로 모델을 양자화하고, 그 결과를 vLLM에서 실행하는 방법을 살펴볼게요.
⚠️ 경고:
AutoAWQ라이브러리는 더 이상 사용되지 않습니다(deprecated). 이 기능은 vLLM 프로젝트의 llm-compressor로 이관되었어요. 권장되는 양자화 워크플로는 llm-compressor의 AWQ 예시를, 더 자세한 폐기(deprecation) 정보는 원래 AutoAWQ 저장소를 참고하세요. (GPU 추론 자체는 vLLM에서 계속 지원돼요.)
AutoAWQ로 4비트 양자화 모델 만들기
새로운 4비트 양자화 모델을 만들려면 AutoAWQ를 활용할 수 있어요. 양자화는 모델의 정밀도를 BF16/FP16 → INT4로 낮춰서 전체 모델 메모리 사용량을 효과적으로 줄여줍니다. 주요 이점은 더 낮은 지연 시간과 메모리 사용이에요.
AutoAWQ를 설치하거나 HuggingFace의 6500+ 모델 중 하나를 골라서 직접 양자화할 수 있습니다.
pip install autoawq
AutoAWQ를 설치하면 모델을 양자화할 준비가 된 거예요. 자세한 내용은 AutoAWQ 문서를 참고하세요. 다음은 mistralai/Mistral-7B-Instruct-v0.2를 양자화하는 예시입니다.
from awq import AutoAWQForCausalLM
from transformers import AutoTokenizer
model_path = "mistralai/Mistral-7B-Instruct-v0.2"
quant_path = "mistral-instruct-v0.2-awq"
quant_config = {"zero_point": True, "q_group_size": 128, "w_bit": 4, "version": "GEMM"}
# Load model
model = AutoAWQForCausalLM.from_pretrained(
model_path,
low_cpu_mem_usage=True,
use_cache=False,
)
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
# Quantize
model.quantize(tokenizer, quant_config=quant_config)
# Save quantized model
model.save_quantized(quant_path)
tokenizer.save_pretrained(quant_path)
print(f'Model is quantized and saved at "{quant_path}"')
주요 양자화 설정을 보면 zero_point, q_group_size, w_bit: 4, version: "GEMM" 같은 값들이 보여요. 이들이 4비트 양자화의 세부 사항을 결정합니다.
vLLM에서 AWQ 모델 실행하기 (Run an AWQ model with vLLM)
AWQ 모델을 vLLM으로 실행하려면 --quantization awq를 지정하면 돼요. 예를 들어 TheBloke/Llama-2-7b-Chat-AWQ 모델을 쓴다면:
python examples/offline_inference/llm_engine_example.py \
--model TheBloke/Llama-2-7b-Chat-AWQ \
--quantization awq
AWQ 모델은 LLM 엔트리포인트로도 바로 지원됩니다.
from vllm import LLM, SamplingParams
# Sample prompts.
prompts = [
"Hello, my name is",
"The president of the United States is",
"The capital of France is",
"The future of AI is",
]
# Create a sampling params object.
sampling_params = SamplingParams(temperature=0.8, top_p=0.95)
# Create an LLM.
llm = LLM(model="TheBloke/Llama-2-7b-Chat-AWQ", quantization="AWQ")
# Generate texts from the prompts. The output is a list of RequestOutput objects
# that contain the prompt, generated text, and other information.
outputs = llm.generate(prompts, sampling_params)
# Print the outputs.
for output in outputs:
prompt = output.prompt
generated_text = output.outputs[0].text
print(f"Prompt: {prompt!r}, Generated text: {generated_text!r}")
이렇게 quantization="AWQ"로 지정하기만 하면 vLLM이 AWQ 양자화 모델을 바로 로드해서 추론할 수 있어요.