Iker/calibration-dataset
Viewer • Updated • 2.47k • 10
How to use Iker/Latxa-Llama-3.1-70B-Instruct-w4a4_nvfp4 with Transformers:
# Use a pipeline as a high-level helper
from transformers import pipeline
pipe = pipeline("text-generation", model="Iker/Latxa-Llama-3.1-70B-Instruct-w4a4_nvfp4")
messages = [
{"role": "user", "content": "Who are you?"},
]
pipe(messages) # Load model directly
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("Iker/Latxa-Llama-3.1-70B-Instruct-w4a4_nvfp4")
model = AutoModelForCausalLM.from_pretrained("Iker/Latxa-Llama-3.1-70B-Instruct-w4a4_nvfp4")
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 Iker/Latxa-Llama-3.1-70B-Instruct-w4a4_nvfp4 with vLLM:
# Install vLLM from pip:
pip install vllm
# Start the vLLM server:
vllm serve "Iker/Latxa-Llama-3.1-70B-Instruct-w4a4_nvfp4"
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:8000/v1/chat/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "Iker/Latxa-Llama-3.1-70B-Instruct-w4a4_nvfp4",
"messages": [
{
"role": "user",
"content": "What is the capital of France?"
}
]
}'docker model run hf.co/Iker/Latxa-Llama-3.1-70B-Instruct-w4a4_nvfp4
How to use Iker/Latxa-Llama-3.1-70B-Instruct-w4a4_nvfp4 with SGLang:
# Install SGLang from pip:
pip install sglang
# Start the SGLang server:
python3 -m sglang.launch_server \
--model-path "Iker/Latxa-Llama-3.1-70B-Instruct-w4a4_nvfp4" \
--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": "Iker/Latxa-Llama-3.1-70B-Instruct-w4a4_nvfp4",
"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 "Iker/Latxa-Llama-3.1-70B-Instruct-w4a4_nvfp4" \
--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": "Iker/Latxa-Llama-3.1-70B-Instruct-w4a4_nvfp4",
"messages": [
{
"role": "user",
"content": "What is the capital of France?"
}
]
}'How to use Iker/Latxa-Llama-3.1-70B-Instruct-w4a4_nvfp4 with Docker Model Runner:
docker model run hf.co/Iker/Latxa-Llama-3.1-70B-Instruct-w4a4_nvfp4
AWQ Quantization using https://github.com/vllm-project/llm-compressor. Compatible with vLLM and huggingface transformers.
import os
from datasets import load_dataset
from llmcompressor import oneshot
from llmcompressor.modifiers.quantization import QuantizationModifier
from llmcompressor.utils import dispatch_for_generation
from transformers import AutoModelForCausalLM, AutoTokenizer
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
MODEL_ID = "HiTZ/Latxa-Llama-3.1-70B-Instruct"
# Load model.
model = AutoModelForCausalLM.from_pretrained(MODEL_ID, torch_dtype="auto")
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
DATASET_ID = "Iker/calibration-dataset"
DATASET_SPLIT = "train"
# Select number of samples. 512 samples is a good place to start.
# Increasing the number of samples can improve accuracy.
NUM_CALIBRATION_SAMPLES = 2048
MAX_SEQUENCE_LENGTH = 8192
# Load dataset and preprocess.
ds = load_dataset(DATASET_ID, split="train")
ds = ds.shuffle(seed=42)
ds = ds.select(range(NUM_CALIBRATION_SAMPLES))
def preprocess(example):
return {
"text": tokenizer.apply_chat_template(
example["messages"],
tokenize=False,
)
}
ds = ds.map(preprocess)
# Tokenize inputs.
def tokenize(sample):
return tokenizer(
sample["text"],
padding=False,
max_length=MAX_SEQUENCE_LENGTH,
truncation=True,
add_special_tokens=False,
)
ds = ds.map(tokenize, remove_columns=ds.column_names)
# Configure the quantization algorithm and scheme.
# In this case, we:
# * quantize the weights to fp4 with per group 16 via ptq
# * calibrate a global_scale for activations, which will be used to
# quantize activations to fp4 on the fly
recipe = QuantizationModifier(targets="Linear", scheme="NVFP4", ignore=["lm_head"])
# Apply quantization.
oneshot(
model=model,
dataset=ds,
recipe=recipe,
max_seq_length=MAX_SEQUENCE_LENGTH,
num_calibration_samples=NUM_CALIBRATION_SAMPLES,
)
print("\n\n")
print("========== SAMPLE GENERATION ==============")
dispatch_for_generation(model)
model_input = tokenizer.apply_chat_template(
[{"role": "user", "content": "Who are you?"}],
tokenize=True,
return_tensors="pt",
)
#output = model.generate(model_input.to(model.device), max_new_tokens=100, temperature=0.6, do_sample=True)
#print(tokenizer.decode(output[0]))
print("==========================================\n\n")
# Save to disk in compressed-tensors format.
SAVE_DIR = MODEL_ID.rstrip("/").split("/")[-1] + "-w4a4_nvfp4"
model.save_pretrained(SAVE_DIR, save_compressed=True)
tokenizer.save_pretrained(SAVE_DIR)
model.push_to_hub("Iker/Latxa-Llama-3.1-70B-Instruct-w4a4_nvfp4")
tokenizer.push_to_hub("Iker/Latxa-Llama-3.1-70B-Instruct-w4a4_nvfp4")
Base model
meta-llama/Llama-3.1-70B