• Stars
    star
    998
  • Rank 45,938 (Top 1.0 %)
  • Language
    Rust
  • License
    MIT License
  • Created over 1 year ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A fast llama2 decoder in pure Rust.

llama2.rs ๐Ÿค—

This is a Rust implementation of Llama2 inference on CPU

The goal is to be as fast as possible.

It has the following features:

  • Support for 4-bit GPT-Q Quantization
  • Batched prefill of prompt tokens
  • SIMD support for fast CPU inference
  • Memory mapping, loads 70B instantly.
  • Static size checks for safety
  • Support for Grouped Query Attention (needed for big Llamas)
  • Python calling API

Can run up on 1 tok/s 70B Llama2 and 9 tok/s 7B Llama2. (on my intel i9 desktop)

To build, you'll need the nightly toolchain, which is used by default:

> rustup toolchain install nightly # to get nightly
> ulimit -s 10000000 # Increase your stack memory limit. 

You can load models from the Hugging Face hub. For example this creates a version of a 70B quantized) model with 4 bit quant and 64 sized groups:

> pip install -r requirements.export.txt
> python export.py l70b.act64.bin TheBloke/llama-2-70b-Guanaco-QLoRA-GPTQ gptq-4bit-64g-actorder_True

The library needs to be recompiled to match the model. You can do this with cargo.

To run:

> cargo run --release --features 70B,group_64 -- -c llama2-70b-q.bin -t 0.0 -s 11 -p "The only thing"                                                                                                                                 
The only thing that I can think of is that the          
achieved tok/s: 0.89155835

Honestly, not so bad for running on my GPU machine, significantly faster than llama.c.

Here's a run of 13B quantized:

> cargo run --release --features 13B,group_128 -- -c l13orca.act.bin -t 0.0 -s 25 -p "Hello to all the cool people out there who "
Hello to all the cool people out there who are reading this. I hope you are having a great day. I am here
achieved tok/s: 5.1588936

Here's a run of 7B quantized:

cargo run --release --features 7B,group_128 -- -c l7.ack.bin -t 0.0 -s 25 -p "Hello to all the cool people out there who "
> Hello to all the cool people out there who are reading this. I am a newbie here and I am looking for some
achieved tok/s: 9.048136

Python

To run in Python, you need to first compile from the main directory with the python flag.

cargo build --release --features 7B,group_128,python
pip install .

You can then run the following code.

import llama2_rs

def test_llama2_13b_4_128act_can_generate():
    model = llama2_rs.LlamaModel("lorca13b.act132.bin", False)
    tokenizer = llama2_rs.Tokenizer("tokenizer.bin")
    random = llama2_rs.Random()
    response = llama2_rs.generate(
        model,
        tokenizer,
        "Tell me zero-cost abstractions in Rust ",
        50,
        random, 
        0.0
    )

Todos

Configuration

In order to make the model as fast as possible, you need to compile a new version to adapt to other Llama versions. Currently in .cargo/config. The model will fail if these disagree with the binary model that is being loaded. To turn quantization off set it to quant="no".

See Also

Originally, a Rust port of Karpathy's llama2.c but now has a bunch more features to make it scale to 70B.

Also check out:

How does it work?

Started as a port of the original code, with extra type information to make it easier to extend.

There are some dependencies:

  • memmap2for memory mapping
  • rayon for parallel computation.
  • clap for command-line args.
  • pyO3 for python calling
  • SIMD enabled support with portable_simd

Authors

Llama2.rs is written by @srush and @rachtsingh.

More Repositories

1

GPU-Puzzles

Solve puzzles. Learn CUDA.
Jupyter Notebook
5,464
star
2

Tensor-Puzzles

Solve puzzles. Improve your pytorch.
Jupyter Notebook
2,967
star
3

MiniChain

A tiny library for coding with large language models.
Python
1,197
star
4

Triton-Puzzles

Puzzles for learning Triton
Jupyter Notebook
895
star
5

LLM-Training-Puzzles

What would you do with 1000 H100s...
Jupyter Notebook
797
star
6

annotated-s4

Implementation of https://srush.github.io/annotated-s4
Python
451
star
7

annotated-mamba

Annotated version of the Mamba paper
Jupyter Notebook
438
star
8

Autodiff-Puzzles

Jupyter Notebook
317
star
9

Transformer-Puzzles

Puzzles for exploring transformers
Jupyter Notebook
281
star
10

streambook

Live Python Notebooks with any Editor
Jupyter Notebook
277
star
11

raspy

An interactive exploration of Transformer programming.
Jupyter Notebook
239
star
12

do-we-need-attention

TeX
159
star
13

parallax

Python
157
star
14

awesome-o1

TeX
119
star
15

GPTWorld

A puzzle to learn about prompting
Jupyter Notebook
104
star
16

awesome-ml-tracking

102
star
17

triton-autodiff

Experiment of using Tangent to autodiff triton
Python
66
star
18

torch-queue

Python
64
star
19

LLM-Talk

45
star
20

torch-golf

Silly twitter torch implementations.
Python
44
star
21

PyDecode

A dynamic programming toolkit.
C++
39
star
22

VirtualTeaching

DIY setup for virtual teaching on ubuntu
39
star
23

mamba-primer

34
star
24

learns-dex

33
star
25

text2table

Python
31
star
26

jax-lda

Python
31
star
27

ProbTalk

HTML
29
star
28

Hierarchical-Bayes-Compiler

Hal Daume's hbc
Haskell
20
star
29

g9py

HTML
18
star
30

drop7

Jupyter Notebook
18
star
31

Tensor-Puzzles-Penzai

HTML
17
star
32

mamba-scans

Blog post
16
star
33

anynp

Proof-of-concept of global switching between numpy/jax/pytorch in a library.
Python
16
star
34

transformers-bet

HTML
12
star
35

relax-decode

Java
10
star
36

aima-arguments

7
star
37

torch-mechanics

Amateur experiments with autodiff mechanics simulators
7
star
38

minitorch-rust

7
star
39

cs5781

Machine Learning Engineering
6
star
40

postgres-provanence

C
6
star
41

PowerEdit

A super-minimal Python-based video editor โšก
Python
6
star
42

SemiRings

Holder for a bunch of semirings used in ChartParsing
Haskell
6
star
43

DiffRast

HTML
6
star
44

MRF-LM

Shell
5
star
45

TextBook

Command-line Facebook
Haskell
5
star
46

hsNLP-

Combined repo for nlp libs
Haskell
4
star
47

provenance

4
star
48

icfp2009

when I was 4 years old I was maimed by a giant pig
Haskell
4
star
49

configure

some configuration file
Emacs Lisp
3
star
50

clustering

C++
3
star
51

annotated-transformer.github.io

Annotated Transformer Blog Post
3
star
52

BT-AI

Jupyter Notebook
3
star
53

srush-blog

Haskell
2
star
54

Eisner-Parser

An implementation of the Eisner Parser (described in "Bilexical Grammars and a Cubic-time parsing algorithm" ) in Haskell
Haskell
2
star
55

FSM

Finite State Machine lib for haskell
Haskell
2
star
56

hplay

2
star
57

opennmt-gen

Shell
2
star
58

PhraseDep

C++
2
star
59

triton

2
star
60

tf-fork

Python
2
star
61

srush-wiki

2
star
62

icfp2003

Race Car
Haskell
2
star
63

icfp2008

2
star
64

hypergraph

Hypergraph specification
Python
2
star
65

learns-triton

2
star
66

Training

Haskell
1
star
67

bipartite-sampler

Implementation of Huber-Law rejection sampling for bipartite graphs
C
1
star
68

ezTVM

1
star
69

test_grade

Python
1
star
70

Chart-Parsing-

haskell library for basic chart parsers
Haskell
1
star
71

blog-twitter

1
star
72

sigmoidfit

Jupyter Notebook
1
star
73

evernote

Command line bindings for evernote
1
star
74

prof8

Experimental paper writing linter.
TeX
1
star
75

transforest

transforest
Python
1
star
76

decoding-methods

1
star
77

blog

Jupyter Notebook
1
star
78

nlp-course

Go
1
star
79

beamer-animation

Create animations for LaTeX Beamer presentations.
Python
1
star
80

Duel

Python
1
star
81

nlp

1
star
82

twitter-simmons-sports

1
star
83

monadnack-project

Art project for monadnack
1
star
84

peoplesounds

Python
1
star
85

CutParse

C++
1
star
86

Lattice

lattice protobuffer
Python
1
star
87

Penn-Treebank

Haskell library for the penn treebank management
1
star
88

icfp2020

Python
1
star
89

twittersports

1
star
90

ProbDist

Tools for probabality distributions focusing on estimation, conditioning, and smoothing
Haskell
1
star
91

osgai

JavaScript
1
star