Instructions to use meta-llama/Llama-3.2-3B-Instruct with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use meta-llama/Llama-3.2-3B-Instruct with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="meta-llama/Llama-3.2-3B-Instruct") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.2-3B-Instruct") model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.2-3B-Instruct", device_map="auto") 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]:])) - Inference
- Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use meta-llama/Llama-3.2-3B-Instruct with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "meta-llama/Llama-3.2-3B-Instruct" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "meta-llama/Llama-3.2-3B-Instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/meta-llama/Llama-3.2-3B-Instruct
- SGLang
How to use meta-llama/Llama-3.2-3B-Instruct with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "meta-llama/Llama-3.2-3B-Instruct" \ --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": "meta-llama/Llama-3.2-3B-Instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker images
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 "meta-llama/Llama-3.2-3B-Instruct" \ --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": "meta-llama/Llama-3.2-3B-Instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use meta-llama/Llama-3.2-3B-Instruct with Docker Model Runner:
docker model run hf.co/meta-llama/Llama-3.2-3B-Instruct
Token indices sequence length is longer than the specified maximum sequence length for this model (269923 > 131072)
I am getting the above-mentioned error while using llama3.2 for vision. If I use an image URL, it works fine. However, if I upload the same image from a local directory, I get an error. I am using transformed from hugging face. My device is a Mac M1 pro, with 16 GB RAM.
There is also a runtime error "RuntimeError: MPS backend out of memory (MPS allocated: 17.08 GB, other allocations: 672.00 KB, max allowed: 18.13 GB). Tried to allocate 1.54 GB on private pool. Use PYTORCH_MPS_HIGH_WATERMARK_RATIO=0.0 to disable upper limit for memory allocations (may cause system failure)."
I searched Google and people said we need to set "PYTORCH_MPS_HIGH_WATERMARK_RATIO" to 0.7. However, I do not know how to set it in PyTorch. Most answers are related to Stable diffusion webui.
This is my code.
import warnings
from transformers import pipeline
import torch
access_token = "hf_...."
warnings.filterwarnings('ignore')
model_id = "meta-llama/Llama-3.2-3B-Instruct"
pipe = pipeline(
"text-generation",
model=model_id,
token=access_token,
torch_dtype=torch.bfloat16,
device_map="auto",
)
import base64
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
base64_image = encode_image("/Users/mac/Downloads/Llama_Repo.jpeg")
messages = [
{"role": "user",
"content": [
{"type": "text",
"text": "describe the image in one sentence"
},
{"type": "image_url",
"image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}
}
]
},
]
outputs = pipe(
messages,
max_new_tokens=256,
)
response = outputs[0]["generated_text"][-1]["content"]
print(response)
Please guide me on how to solve it in Pytorch. Thanks
I have nothing to do with this project, just thought I would mention that your posting a problem about a vision model. This is not a vision model. This is 3B instruct. Vision model is 11B.
@jdc4429 You are right. This is not a vision model but it is fined-tuned to reason images and lighter weight than heavier vision model. I understood the issue. It was related to Python configuration. Setting PYTORCH_MPS_HIGH_WATERMARK_RATIO=0.0 resolves the issue.