Instructions to use google/gemma-3-270m with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use google/gemma-3-270m with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="google/gemma-3-270m")# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("google/gemma-3-270m") model = AutoModelForCausalLM.from_pretrained("google/gemma-3-270m") - Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use google/gemma-3-270m with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "google/gemma-3-270m" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "google/gemma-3-270m", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/google/gemma-3-270m
- SGLang
How to use google/gemma-3-270m 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-3-270m" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "google/gemma-3-270m", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'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-3-270m" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "google/gemma-3-270m", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use google/gemma-3-270m with Docker Model Runner:
docker model run hf.co/google/gemma-3-270m
`tokenizer.model_max_length` of 1000000000000000019884624838656 ?
Hi there,
I'm sorry if this ought to be obvious (and yes, I haven't looked in detail into the positional embedding mechanism for Gemma yet), but is that number... accurate (found here)? I'm looking into this as this used to be a reliable number to format the dataset for finetuning for older models. I see in some of your tutorials that you have max_seq_length=512, or gemma_lm.preprocessor.sequence_length = 256, and I'm curious if by any chance you have some guidelines regarding that particular parameter when fine-tuning (beyond simple demos)...
Thanks in advance!
Hi @jchwenger ,
The tokenizer.model_max_length value is a HF sentinel artifact, effectively representing an unbounded default. it is populated by HF transformers library when the tokenizer config doesn't explicitly inherit a maximum length during export. As a result, the tokenizer will technically continue encoding sequences until memory is exhausted, rather than enforcing a hard cutoff.
The actual context length limit is determined by the model architecture (see config.max_position_embeddings), not by the tokenizer. The Gemma-3-270M model is text-only, and its usable context window is bounded by this architectural limit as well as practical memory constraints.
For fine tuning, it is therefore safe to ignore the tokenizer's placeholder model_max_length and instead explicitly clamp the sequence length in your tokenizer or preprocessing pipeline, based on available VRAM and the length distribution of your dataset. This helps maximize training throughput and avoids unnecessary padding.
Thanks!
Hi @srikanta-221 ,
Thanks so much for the clarification! Indeed that makes far more sense to fetch the maximum context length in the model config rather than in the tokenizer, I wonder where I got this idea from in the first place. Very useful, cheers!