Views
No views yet
1
2$ pip install git+https://github.com/huggingface/transformers@v4.49.0-Gemma3
3pipeline API1from transformers import pipeline
2
3pipe = pipeline("text-generation", model="google/gemma-3-1b-it", device="cuda", torch_dtype=torch.bfloat16)
4
5messages = [
6 [
7 {
8 "role": "system",
9 "content": [{"type": "text", "text": "You are a helpful assistant."},]
10 },
11 {
12 "role": "user",
13 "content": [{"type": "text", "text": "Write a poem on Hugging Face, the company"},]
14 },
15 ],
16]
17
18output = pipe(messages, max_new_tokens=50)1from transformers import AutoTokenizer, BitsAndBytesConfig, Gemma3ForCausalLM
2import torch
3
4model_id = "google/gemma-3-1b-it"
5
6quantization_config = BitsAndBytesConfig(load_in_8bit=True)
7
8model = Gemma3ForCausalLM.from_pretrained(
9 model_id, quantization_config=quantization_config
10).eval()
11
12tokenizer = AutoTokenizer.from_pretrained(model_id)
13
14messages = [
15 [
16 {
17 "role": "system",
18 "content": [{"type": "text", "text": "You are a helpful assistant."},]
19 },
20 {
21 "role": "user",
22 "content": [{"type": "text", "text": "Write a poem on Hugging Face, the company"},]
23 },
24 ],
25]
26inputs = tokenizer.apply_chat_template(
27 messages,
28 add_generation_prompt=True,
29 tokenize=True,
30 return_dict=True,
31 return_tensors="pt",
32).to(model.device).to(torch.bfloat16)
33
34
35with torch.inference_mode():
36 outputs = model.generate(**inputs, max_new_tokens=64)
37
38outputs = tokenizer.batch_decode(outputs)