Views
No views yet
llm_compressor.1from transformers import AutoTokenizer, AutoModelForCausalLM
2from llmcompressor.transformers import oneshot
3from llmcompressor.modifiers.quantization import QuantizationModifier
4
5# Define the model ID for the model you want to quantize
6MODEL_ID = "meta-llama/Llama-3.3-70B-Instruct"
7
8# Load the model and tokenizer
9model = AutoModelForCausalLM.from_pretrained(
10 MODEL_ID, device_map="auto", torch_dtype="auto"
11)
12tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
13
14# Configure the quantization recipe
15recipe = QuantizationModifier(targets="Linear", scheme="FP8_DYNAMIC", ignore=["lm_head"])
16
17# Apply the quantization algorithm
18oneshot(model=model, recipe=recipe)
19
20# Define the directory to save the quantized model
21SAVE_DIR = MODEL_ID.split("/")[1] + "-FP8-Dynamic"
22
23# Save the quantized model and tokenizer
24model.save_pretrained(SAVE_DIR)
25tokenizer.save_pretrained(SAVE_DIR)
26
27print(f"Quantized model saved to (SAVE_DIR)")