Instructions to use google/gemma-3n-E4B-it with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use google/gemma-3n-E4B-it with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="google/gemma-3n-E4B-it") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] pipe(text=messages)# Load model directly from transformers import AutoProcessor, AutoModelForImageTextToText processor = AutoProcessor.from_pretrained("google/gemma-3n-E4B-it") model = AutoModelForImageTextToText.from_pretrained("google/gemma-3n-E4B-it") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] inputs = processor.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(processor.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Inference
- HuggingChat
- Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use google/gemma-3n-E4B-it with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "google/gemma-3n-E4B-it" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "google/gemma-3n-E4B-it", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker
docker model run hf.co/google/gemma-3n-E4B-it
- SGLang
How to use google/gemma-3n-E4B-it 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 "google/gemma-3n-E4B-it" \ --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": "google/gemma-3n-E4B-it", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'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 "google/gemma-3n-E4B-it" \ --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": "google/gemma-3n-E4B-it", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }' - Docker Model Runner
How to use google/gemma-3n-E4B-it with Docker Model Runner:
docker model run hf.co/google/gemma-3n-E4B-it
Cannot copy out of meta tensor; no data!
First in order to avoid unknown mobilenetv5_300m_enc errors:
!pip uninstall -y timm
!pip install timm --upgrade
Then run the inference example of the model card and got: NotImplementedError: Cannot copy out of meta tensor; no data!
Full log:
The following generation flags are not valid and may be ignored: ['top_p', 'top_k']. Set TRANSFORMERS_VERBOSITY=info for more details.
NotImplementedError Traceback (most recent call last)
/tmp/ipython-input-1-875231982.py in <cell line: 0>()
35
36 with torch.inference_mode():
---> 37 generation = model.generate(**inputs, max_new_tokens=100, do_sample=False)
38 generation = generation[0][input_len:]
39
21 frames
/usr/local/lib/python3.11/dist-packages/transformers/models/gemma3n/modeling_gemma3n.py in scale_corrected_output(self, corrected)
1138 def scale_corrected_output(self, corrected: torch.Tensor) -> torch.Tensor:
1139 """Scales the provided 3D tensor of shape [batch_size, num_tokens, hidden_size]."""
-> 1140 return (corrected.type_as(self.correct_output_scale) * self.correct_output_scale).type_as(corrected)
1141
1142
NotImplementedError: Cannot copy out of meta tensor; no data!
Hi @Amit5674 ,
RuntimeError: Unknown model (mobilenetv5_300m_enc) is an error from timm it means they are not up to date. Gemma 3n requires at least timm v1.0.16. Please upgrade avoid the above issue, and also please upgrade the transformer to the latest the timm to latest version or at least to v1.0.16 to version to work with Gemma 3n models. I have successfully able to load the model after upgrading the timm to v1.0.16.
Please find the following gist file where I have successfully able to get the output from the model.
Thanks.