参考:
https://developers.googleblog.com/en/introducing-gemma-3n-developer-guide/
下载:
https://modelscope.cn/models/google/gemma-3n-E2B-it 模型下载
运行代码:
https://github.com/huggingface/huggingface-gemma-recipes
微调:
https://docs.unsloth.ai/basics/gemma-3n-how-to-run-and-fine-tune#fine-tuning-gemma-3n-with-unsloth
代码
报错:Set TORCHDYNAMO_VERBOSE=1 for the internal stack trace (please do this especially if you’re reporting a bug to PyTorch). For even more developer context, set TORCH_LOGS=“+dynamo”
解决:
import torch._dynamo
torch._dynamo.config.suppress_errors = True
torch._dynamo.disable()
import os
os.environ["TORCH_COMPILE"] = "0"
os.environ["TORCHDYNAMO_DISABLE"] = "1"
os.environ["DISABLE_TORCH_COMPILE"] = "1"
完整代码
from transformers import AutoProcessor, AutoModelForImageTextToText
import torchdevice = torch.device("cuda" if torch.cuda.is_available() else "cpu")import torch._dynamo
torch._dynamo.config.suppress_errors = True
torch._dynamo.disable()
import os
os.environ["TORCH_COMPILE"] = "0"
os.environ["TORCHDYNAMO_DISABLE"] = "1"
os.environ["DISABLE_TORCH_COMPILE"] = "1"model_id = "./gemma-3n-E2B-it" # google/gemma-3n-e2b-it
processor = AutoProcessor.from_pretrained(model_id)
model = AutoModelForImageTextToText.from_pretrained(model_id).to(device)def model_generation(model, messages):inputs = processor.apply_chat_template(messages,add_generation_prompt=True,tokenize=True,return_dict=True,return_tensors="pt",)input_len = inputs["input_ids"].shape[-1]inputs = inputs.to(model.device, dtype=model.dtype)with torch.inference_mode():generation = model.generate(**inputs, max_new_tokens=32)generation = generation[:, input_len:]decoded = processor.batch_decode(generation, skip_special_tokens=True)print(decoded[0])
文本推理
# Text
messages = [{"role": "user","content": [{"type": "text", "text": "你是谁"}]}
]
model_generation(model, messages)
图像+文本推理
# Image Onlymessages = [{"role": "user","content": [{ "type": "image", "image" : "./下载.jpg" },{"type": "text", "text": "详细描述这张图片"}]}
]
model_generation(model, messages)
语音+文本推理
# Interleaved with Audiomessages = [{"role": "user","content": [{"type": "text", "text": "Transcribe the following speech segment in English:"},{"type": "audio", "audio": "test-16b-caps.wav"},]}
]
model_generation(model, messages)