Views
No views yet
[!NOTE] zentorch v2.11.0.2 for PyTorch v2.11.0 has to be built from source.
1import torch
2from transformers import TorchAoConfig, AutoModelForCausalLM, AutoTokenizer
3from torchao.quantization import Int8DynamicActivationInt8WeightConfig
4from torchao.quantization.quant_primitives import MappingType
5
6MODEL_ID = "mistralai/Mistral-7B-v0.1"
7OUTPUT_DIR = "amd/Mistral-7B-v0.1-da8w8-torchao-v0.17.0"
8modules_to_skip = ["lm_head"]
9
10quantization_config = TorchAoConfig(
11 Int8DynamicActivationInt8WeightConfig(
12 version=2,
13 act_mapping_type=MappingType.SYMMETRIC,
14 ),
15 modules_to_not_convert=modules_to_skip,
16)
17
18model = AutoModelForCausalLM.from_pretrained(
19 MODEL_ID,
20 dtype=torch.bfloat16,
21 device_map="cpu",
22 quantization_config=quantization_config,
23 trust_remote_code=True,
24)
25model.save_pretrained(OUTPUT_DIR)
26
27tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
28tokenizer.save_pretrained(OUTPUT_DIR)
29
30# Smoke test
31inputs = tokenizer("What are we having for dinner?", return_tensors="pt")
32out = model.generate(**inputs, max_new_tokens=30, cache_implementation="static")
33print(tokenizer.decode(out[0], skip_special_tokens=True))1pip install --extra-index-url https://download.pytorch.org/whl/cpu \
2 --extra-index-url https://wheels.vllm.ai/cpu/ \
3 torch==2.11.0+cpu \
4 vllm==0.23.0 \
5 torchao==0.17.0 \
6 "lm-eval[vllm]==0.4.12" \
7 huggingface_hubconda install -c conda-forge gperftools=2.17.2 llvm-openmp=18.1.8 --no-deps -y1# TorchInductor + zentorch
2export TORCHINDUCTOR_FREEZING=1
3export TORCHINDUCTOR_AUTOGRAD_CACHE=0
4export VLLM_USE_AOT_COMPILE=0
5export ZENDNNL_MATMUL_ALGO=1
6
7# Required CPU runtime libraries
8export LD_PRELOAD="<path to lib>/libtcmalloc_minimal.so.4:<path to lib>/libiomp5.so${LD_PRELOAD:+:$LD_PRELOAD}"find / -name 'libtcmalloc_minimal.so.4' and find / -name 'libiomp5.so', then substitute the resulting directory for <path to lib>.| Benchmark | BF16 Baseline | DA8W8 (this model) | Dynamic Quant Difference (baseline: BF16) |
|---|---|---|---|
| GSM8K (5-shot, exact-match strict) | 0.3844 | 0.3768 | -1.98% |
1lm_eval \
2 --model vllm \
3 --model_args pretrained=amd/Mistral-7B-v0.1-da8w8-torchao-v0.17.0,tokenizer=mistralai/Mistral-7B-v0.1,dtype=bfloat16 \
4 --tasks gsm8k \
5 --batch_size auto \
6 --trust_remote_code \
7 --num_fewshot 5 \
8 --log_samples \
9 --gen_kwargs "max_gen_toks=2048" \
10 --output_path .