Views
No views yet
llm_compressor.⚠️ This is a quantization pipeline, not a pre-quantized checkpoint.
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 = "perplexity-ai/r1-1776-distill-llama-70b"
7
8# Load the model and tokenizer with appropriate parameters
9model = AutoModelForCausalLM.from_pretrained(
10 MODEL_ID,
11 device_map="auto",
12 torch_dtype="auto",
13 trust_remote_code=True, # Add this to automatically trust remote code
14 low_cpu_mem_usage=True, # Help with memory issues during loading
15 offload_folder="offload" # Use disk offloading for large models
16)
17
18tokenizer = AutoTokenizer.from_pretrained(
19 MODEL_ID,
20 trust_remote_code=True # Also need this for tokenizer
21)
22
23# Configure the quantization recipe
24recipe = QuantizationModifier(targets="Linear", scheme="FP8_DYNAMIC", ignore=["lm_head"])
25
26# Apply the quantization algorithm
27oneshot(model=model, recipe=recipe)
28
29# Define the directory to save the quantized model
30SAVE_DIR = MODEL_ID.split("/")[1] + "-FP8-Dynamic"
31
32# Save the quantized model and tokenizer
33model.save_pretrained(SAVE_DIR)
34tokenizer.save_pretrained(SAVE_DIR)
35
36print(f"Quantized model saved to {SAVE_DIR}")