Portable LLM
A rust library for LLM inference,which ported from llama2.c. For learning purposes only, it is currently not available for production.
Feature
- Transformer (Currently support llama2 & gemma)
- GGUF file format
- SIMD (Support x86_64 avx2, derived from candle)
- MMAP :construction: :construction:
Example
# Download testdata
make testdata
# Run example in release mode
RUSTFLAGS='-C target-cpu=native -C target-feature=+avx2' cargo run --example gemma --release
output:
a dog. She lived in a cozy hole with her family. She liked to play outside and explore new things. One day, she saw something unusual in the sky. It was a big, shiny aeroplane. Lionce had never seen anything like it before. The aeroplane was strange and bright. Lionion's family saw her looking at the aeroplane and asked her what she was doing there. Lionwn proudly said, "I found this unusual aeroplane. It's so pretty and shiny!" Lionion's family smiled and told her it was the most special thing they had ever seen. She took the aeroplane and placed it in the ground. The next day, when Liona went outside, she saw something amazing. The aeroplane had changed! It was now a big, bright orange butterfly! Lionna was so happy. She watched the butterfly fly away and smiled. With her original adventure, she decided to take her butterfly home with her. token/s: 204.37956204379563
Performance
model: tinystories15M
, prompt: a dog
version | speed | os | arch | cpu | comment |
---|---|---|---|---|---|
0.3.0 | 81 token/s | osx | x86 | 2.2 GHz Quad-Core Intel Core i7 | single thread |
0.3.1 | 140 token/s | osx | x86 | 2.2 GHz Quad-Core Intel Core i7 | multiple thread |
0.4.0 | 204 token/s | osx | x86 | 2.2 GHz Quad-Core Intel Core i7 | multiple thread, simd |
Usage
llama
use ;
use ;
// Load config from model
let f = open.unwrap;
let mut reader = new;
let config = from_reader.unwrap;
// Load weights from model
let mut weights = new;
weights.load_data.unwrap;
// Load tokenizer
let tokenizer_file = open.unwrap;
let tokenizer_reader = new;
let tokenizer = from_reader.unwrap;
// Generate text from prompts
let iterator = LLM new
.inference
.unwrap;
for i in iterator
gemma
let f = open.unwrap;
// let mmap = unsafe { Mmap::map(&f).unwrap() };
// let reader = io::Cursor::new(&mmap[..]);
let reader = new;
let mut gf = from_reader.unwrap;
let config = from_gguf.unwrap;
// println!("{:?}", config.clone());
let tokenizer = from_gguf.unwrap;
let mut weights = new;
weights.load_from_gguf.unwrap;
let args: = args.collect;
let iterator = LLM new
.inference
.unwrap;
let mut token_count = 0;
let start = now;
for in iterator.enumerate
println!
Development
# build
RUSTFLAGS='-C target-cpu=native -C target-feature=+avx2' cargo build --release
# cross build
RUSTFLAGS='-C target-cpu=native -C target-feature=+avx2' cargo zigbuild --release --target x86_64-unknown-linux-musl