Quantized LLama-based Models
Collection
llama based models quantized to various precisions • 6 items • Updated
How to use jsbaicenter/Llama-3.3-70b-Instruct-AWQ-4BIT-GEMM with Transformers:
# Use a pipeline as a high-level helper
from transformers import pipeline
pipe = pipeline("text-generation", model="jsbaicenter/Llama-3.3-70b-Instruct-AWQ-4BIT-GEMM")
messages = [
{"role": "user", "content": "Who are you?"},
]
pipe(messages) # Load model directly
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("jsbaicenter/Llama-3.3-70b-Instruct-AWQ-4BIT-GEMM")
model = AutoModelForCausalLM.from_pretrained("jsbaicenter/Llama-3.3-70b-Instruct-AWQ-4BIT-GEMM")
messages = [
{"role": "user", "content": "Who are you?"},
]
inputs = tokenizer.apply_chat_template(
messages,
add_generation_prompt=True,
tokenize=True,
return_dict=True,
return_tensors="pt",
).to(model.device)
outputs = model.generate(**inputs, max_new_tokens=40)
print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:]))How to use jsbaicenter/Llama-3.3-70b-Instruct-AWQ-4BIT-GEMM with vLLM:
# Install vLLM from pip:
pip install vllm
# Start the vLLM server:
vllm serve "jsbaicenter/Llama-3.3-70b-Instruct-AWQ-4BIT-GEMM"
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:8000/v1/chat/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "jsbaicenter/Llama-3.3-70b-Instruct-AWQ-4BIT-GEMM",
"messages": [
{
"role": "user",
"content": "What is the capital of France?"
}
]
}'docker model run hf.co/jsbaicenter/Llama-3.3-70b-Instruct-AWQ-4BIT-GEMM
How to use jsbaicenter/Llama-3.3-70b-Instruct-AWQ-4BIT-GEMM with SGLang:
# Install SGLang from pip:
pip install sglang
# Start the SGLang server:
python3 -m sglang.launch_server \
--model-path "jsbaicenter/Llama-3.3-70b-Instruct-AWQ-4BIT-GEMM" \
--host 0.0.0.0 \
--port 30000
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:30000/v1/chat/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "jsbaicenter/Llama-3.3-70b-Instruct-AWQ-4BIT-GEMM",
"messages": [
{
"role": "user",
"content": "What is the capital of France?"
}
]
}'docker run --gpus all \
--shm-size 32g \
-p 30000:30000 \
-v ~/.cache/huggingface:/root/.cache/huggingface \
--env "HF_TOKEN=<secret>" \
--ipc=host \
lmsysorg/sglang:latest \
python3 -m sglang.launch_server \
--model-path "jsbaicenter/Llama-3.3-70b-Instruct-AWQ-4BIT-GEMM" \
--host 0.0.0.0 \
--port 30000
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:30000/v1/chat/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "jsbaicenter/Llama-3.3-70b-Instruct-AWQ-4BIT-GEMM",
"messages": [
{
"role": "user",
"content": "What is the capital of France?"
}
]
}'How to use jsbaicenter/Llama-3.3-70b-Instruct-AWQ-4BIT-GEMM with Docker Model Runner:
docker model run hf.co/jsbaicenter/Llama-3.3-70b-Instruct-AWQ-4BIT-GEMM
Simple compression of llama-3.3-70B-instruct model using AWQ method.
from transformers import AutoModelForCausalLM
model_name = "uyiosa/Llama-3.3-70b-Instruct-AWQ-4bit-GEMM"
model = AutoModelForCausalLM.from_pretrained(model_name)
print(model)
docker run --runtime nvidia --gpus all \
--env "HUGGING_FACE_HUB_TOKEN = .........." \
-p 8000:8000 \
--ipc=host --model jsbaicenter/Llama-3.3-70b-Instruct-AWQ-4BIT-GEMM \
--gpu-memory-utilization 0.9 \
--swap-space 0 \
--max-seq-len-to-capture 512 \
--max-num-seqs 1 \
--api-key "token-abc123" \
--max-model-len 8000 \
--trust-remote-code --enable-chunked-prefill \
--max_num_batched_tokens 1024
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel
import os
from awq import AutoAWQForCausalLM
import gc
def clear_gpu_memory():
"""Clear GPU memory and cache"""
if torch.cuda.is_available():
torch.cuda.empty_cache()
gc.collect()
def merge_model(base_model_path: str, adapter_path: str, merged_path: str, device: str = "cuda"):
"""Merge adapter with base model and save"""
print("Loading base model...")
base_model = AutoModelForCausalLM.from_pretrained(
base_model_path,
torch_dtype=torch.float16,
device_map=device
)
print("Loading adapter...")
adapter_model = PeftModel.from_pretrained(
base_model,
adapter_path,
device_map=device
)
print("Merging adapter with base model...")
merged_model = adapter_model.merge_and_unload()
print("Saving merged model...")
merged_model.save_pretrained(merged_path)
# Clear model from GPU memory
del base_model
del adapter_model
del merged_model
clear_gpu_memory()
print("Cleared GPU memory after merge")
def quantize_model(merged_path: str, quantized_path: str, device: str = "cuda"):
"""Quantize the merged model"""
print("Starting quantization...")
quant_config = {
"bits": 4,
"group_size": 128,
"zero_point": True,
"modules_to_not_convert": [
"attention", # keep attention in fp16
"rotary_emb", # keep embeddings in fp16
"norm", # keep normalization in fp16
"adapter", # keep adapter weights in fp16
"lora" # keep any remaining LoRA weights in fp16
]
}
# Load and quantize
print("Loading merged model for quantization...")
quantizer = AutoAWQForCausalLM.from_pretrained(
merged_path,
**quant_config,
device_map=device
)
quantized_model = quantizer.quantize(
examples=128,
verify_loading=True
)
print("Saving quantized model...")
quantized_model.save_pretrained(quantized_path)
# Clear GPU memory again
del quantizer
del quantized_model
clear_gpu_memory()
print("Cleared GPU memory after quantization")
def process_model(base_model_path: str, adapter_path: str, output_dir: str):
"""Main processing function"""
os.makedirs(output_dir, exist_ok=True)
merged_path = os.path.join(output_dir, "merged_model")
quantized_path = os.path.join(output_dir, "quantized_model")
try:
# Step 1: Merge
merge_model(base_model_path, adapter_path, merged_path)
# Step 2: Quantize
quantize_model(merged_path, quantized_path)
print("Process completed successfully!")
return True
except Exception as e:
print(f"Error during processing: {str(e)}")
clear_gpu_memory() # Clear memory if there's an error
return False
if __name__ == "__main__":
# Configuration
BASE_MODEL_PATH = "meta-llama/Llama-3.3-70B-Instruct"
ADAPTER_PATH = "./checkpoint-781" # Directory with adapter_config.json
OUTPUT_DIR = "llama-3.3-70b-FT781-AWQ-GEMM"
# Run the process
success = process_model(
base_model_path=BASE_MODEL_PATH,
adapter_path=ADAPTER_PATH,
output_dir=OUTPUT_DIR
)
Base model
meta-llama/Llama-3.1-70B