Run language models in Rust on your GPU
Ferrotensor runs Llama-style language models from Hugging Face checkpoints, such as SmolLM2, TinyLlama and Qwen2, on your own GPU. It is a Rust framework with its own WGSL kernels, which run through Vulkan, Metal and DX12, on NVIDIA GPUs and Apple silicon. Sampling text and chatting are single commands, and the same binary also fine-tunes and trains models.
Install
Install the command with cargo install ferrotensor-cli (Rust 1.88 or newer). The binary is called ferrotensor. This command prints the GPU that wgpu picked and runs a kernel self-check:
ferrotensor info --check --jsonGenerate text
--model takes a Hugging Face checkpoint directory with config.json, tokenizer.json and *.safetensors. Without --raw the prompt is wrapped in ChatML. With --raw the prompt is used as written, which works with any model:
ferrotensor generate --model DIR --prompt "Give three tips for staying healthy." --temperature 0.7 --top-p 0.9
ferrotensor generate --model DIR --raw --prompt "Once upon a time"--temperature, --top-k, --top-p, --seed and --max-new-tokens control the sampling. Greedy decoding (--temperature 0 or --top-k 1) picks each token on the GPU. A prompt that encodes to no tokens, or is longer than the context of the model, exits with code 2.
Chat with a model
chat reads one user message per line of stdin and streams each reply. The KV cache is kept across turns, so a turn runs the new tokens and not the whole conversation. /reset clears the conversation, and /exit ends it:
ferrotensor chat --model DIR --system "Answer in one sentence."The model needs a tokenizer with the ChatML tokens, which SmolLM2 has and TinyLlama does not. For other models use generate --raw.
Run a fine-tuned adapter
Add --lora to generate, chat or eval to run a model with the adapter that finetune wrote. The rank, alpha and target layers are read from the file:
ferrotensor chat --model DIR --lora outputs/run/lora_adapters.safetensorsTo make the adapter part of the weights, merge writes a model directory that loads without --lora, in Ferrotensor and in Hugging Face Transformers:
ferrotensor merge --model DIR --lora outputs/run/lora_adapters.safetensors --out outputs/mergedUse the model in llama.cpp or Ollama
export-gguf writes a Llama or Qwen2 model directory, such as the output of merge, as one GGUF version 3 file:
ferrotensor export-gguf --model outputs/merged --out model.ggufTo run the file in Ollama, write a Modelfile that holds FROM ./model.gguf, then:
ollama create NAME -f Modelfile
ollama run NAMEOllama picks the chat template from the file (checked with Ollama 0.40.17).
Ferrotensor reads Hugging Face safetensors checkpoints, single or sharded. It writes GGUF files and does not load them. The CLI README lists the tokenizers that export-gguf accepts.
A Hugging Face model id
--model also takes a model id that is already in the local Hugging Face cache. Ferrotensor never downloads anything, so download the model with hf before the run:
hf download HuggingFaceTB/SmolLM2-135M
ferrotensor chat --model HuggingFaceTB/SmolLM2-135MAn existing directory of the same name wins. An id that is not in the cache exits with code 2 and names the cache and the download command.
Scripts and agents
With --json, generate prints one JSON object on stdout: text, prompt_tokens, new_tokens, tokens_per_second, prefill_seconds and kv_cache. chat prints one object per reply, one per line. Progress goes to stderr. An error prints {"error": "..."}, and the exit code says whether to retry (1) or to change the request (2). AGENTS.md lists the keys of every command.
How to run a language model in Rust?
Install the command with cargo install ferrotensor-cli, then run ferrotensor generate --model DIR --prompt "Hello", where DIR is a Hugging Face checkpoint directory of a Llama-style model such as SmolLM2. ferrotensor chat --model DIR holds a conversation.
Does it run inference on the GPU?
Yes. The kernels run through wgpu on Vulkan, Metal and DX12, on NVIDIA GPUs and Apple silicon. It has been tested on an NVIDIA A10G and a Tesla T4 through Vulkan and on an Apple M2 through Metal. AMD and Intel GPUs are untested.
Which models can it run?
Llama-style decoder models such as SmolLM2, TinyLlama and Qwen2. chat needs a ChatML tokenizer, and generate --raw takes a prompt verbatim with any model. Models larger than SmolLM2-1.7B are untested.
Can the model run in Ollama or llama.cpp?
Yes. ferrotensor export-gguf writes a GGUF file for llama.cpp and Ollama from a Llama or Qwen2 model directory, with an adapter merged in if you pass --lora.
More
- Guide: install, check the GPU and fine-tune a model, with output from real runs
- Fine-tune a language model in Rust and train one from scratch
- Questions and the docs
- Source on GitHub