gemma3

command
v0.28.44 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 21 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

gemma3: ONNX Text Generation

Demonstrates ONNX-based text generation using the onnx-community/gemma-3-270m-it-ONNX model with GoMLX.

Given a user message, the model generates a response using Gemma3's chat template and autoregressive decoding.

Usage

go run gemma3.go

With a custom prompt:

go run gemma3.go --prompt="What is Go?"

With different generation parameters:

go run gemma3.go --max-tokens=50 --temperature=0.6 --top-k=32

Use the fp16 model variant (570MB instead of 1.14GB):

go run gemma3.go --fp16

Options

  • --prompt: User message for chat generation (default: "Write a short poem about the sea.")
  • --max-tokens: Maximum number of tokens to generate (default: 100)
  • --max-seq-len: Maximum total sequence length including prompt (default: 256)
  • --temperature: Sampling temperature; 0 = greedy (default: 0.8)
  • --top-k: Top-k sampling; 0 = disabled (default: 64)
  • --fp16: Use fp16 model variant (default: false)
  • --backend: Backend to use (default: auto-detect)

The first run downloads the ONNX model and tokenizer from HuggingFace to ~/.cache/huggingface/.

If downloads fail, consider using a mirror by setting HF_ENDPOINT.

How It Works

The example loads the Gemma3 270M instruction-tuned model in ONNX format and runs inference via model.CallGraph(). Each generation step runs the full model on the entire sequence (prompt + generated tokens so far), padded to the next power of 2 to limit JIT recompilations. KV cache inputs are provided as zero-sequence-length tensors. Sampling is performed on the CPU with temperature scaling and top-k filtering.

Documentation

Overview

gemma3 demonstrates ONNX-based text generation using GoMLX.

It downloads the onnx-community/gemma-3-270m-it-ONNX model from HuggingFace, tokenizes a chat prompt, and generates text autoregressively.

Usage:

go run gemma3.go
go run gemma3.go --prompt="What is Go?"
go run gemma3.go --max-tokens=50 --temperature=0.6
go run gemma3.go --prompts-file=prompts.txt --warmup=2

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL