llama2

package module
v0.0.0-...-8c1a6d3 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2023 License: MIT Imports: 10 Imported by: 0

README

llama2.go

Cute Llama

This is a Go port of llama2.c.

Setup

  1. Download a model:

  2. Download tokenizer.bin

  3. go install github.com/saracen/llama2.go/cmd/llama2go@latest

  4. Do things:

    ./llama2go --help
    llama2go: <checkpoint>
      -cpuprofile string
             write cpu profile to file
      -prompt string
             prompt
      -steps int
             max number of steps to run for, 0: use seq_len (default 256)
      -temperature float
             temperature for sampling (default 0.9)
    
    ./llama2go -prompt "Cute llamas are" -steps 38 --temperature 0 stories110M.bin
    <s>
    Cute llamas are two friends who love to play together. They have a special game that they play every day. They pretend to be superheroes and save the world.
    achieved tok/s: 43.268528
    

Performance

Tokens per second:

system model llama2.c llama2.go (no cgo) llama2.go (cgo)
M1 Max, 10-Core, 32 GB stories15M 676.392573 246.885611 473.840849
M1 Max, 10-Core, 32 GB stories42M 267.295597 98.165245 151.396638
M1 Max, 10-Core, 32 GB stories110M 100.671141 42.592345 69.804907

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Argmax

func Argmax(v []float32) int

func Sample

func Sample(seed uint64, probabilities []float32) int

func Softmax

func Softmax(x []float32)

func Transformer

func Transformer(token, pos int, c Checkpoint, s *RunState)

Types

type Checkpoint

type Checkpoint interface {
	Error() error
	Close() error

	Dim() int
	HiddenDim() int
	NLayers() int
	NHeads() int
	NKvHeads() int
	VocabSize() int
	SeqLen() int

	TokenEmbeddingTable(token int) []float32
	RmsAttWeight(layer int) []float32
	RmsFfnWeight(layer int) []float32
	Wq(layer int) []float32
	Wk(layer int) []float32
	Wv(layer int) []float32
	Wo(layer int) []float32
	W1(layer int) []float32
	W2(layer int) []float32
	W3(layer int) []float32
	RmsFinalWeight() []float32
	FreqCisReal(pos int) []float32
	FreqCisImag(pos int) []float32
	Wcls() []float32
}

type Config

type Config struct {
	Dim       int
	HiddenDim int
	NLayers   int
	NHeads    int
	NKvHeads  int
	VocabSize int
	SeqLen    int
}

type MmapCheckpoint

type MmapCheckpoint struct {
	// contains filtered or unexported fields
}

func NewMmapCheckpoint

func NewMmapCheckpoint(pathname string) (*MmapCheckpoint, error)

func (*MmapCheckpoint) Close

func (c *MmapCheckpoint) Close() error

func (*MmapCheckpoint) Dim

func (c *MmapCheckpoint) Dim() int

func (*MmapCheckpoint) Error

func (c *MmapCheckpoint) Error() error

func (*MmapCheckpoint) FreqCisImag

func (c *MmapCheckpoint) FreqCisImag(pos int) []float32

func (*MmapCheckpoint) FreqCisReal

func (c *MmapCheckpoint) FreqCisReal(pos int) []float32

func (*MmapCheckpoint) HiddenDim

func (c *MmapCheckpoint) HiddenDim() int

func (*MmapCheckpoint) NHeads

func (c *MmapCheckpoint) NHeads() int

func (*MmapCheckpoint) NKvHeads

func (c *MmapCheckpoint) NKvHeads() int

func (*MmapCheckpoint) NLayers

func (c *MmapCheckpoint) NLayers() int

func (*MmapCheckpoint) RmsAttWeight

func (c *MmapCheckpoint) RmsAttWeight(layer int) []float32

func (*MmapCheckpoint) RmsFfnWeight

func (c *MmapCheckpoint) RmsFfnWeight(layer int) []float32

func (*MmapCheckpoint) RmsFinalWeight

func (c *MmapCheckpoint) RmsFinalWeight() []float32

func (*MmapCheckpoint) SeqLen

func (c *MmapCheckpoint) SeqLen() int

func (*MmapCheckpoint) TokenEmbeddingTable

func (c *MmapCheckpoint) TokenEmbeddingTable(token int) []float32

func (*MmapCheckpoint) VocabSize

func (c *MmapCheckpoint) VocabSize() int

func (*MmapCheckpoint) W1

func (c *MmapCheckpoint) W1(layer int) []float32

func (*MmapCheckpoint) W2

func (c *MmapCheckpoint) W2(layer int) []float32

func (*MmapCheckpoint) W3

func (c *MmapCheckpoint) W3(layer int) []float32

func (*MmapCheckpoint) Wcls

func (c *MmapCheckpoint) Wcls() []float32

func (*MmapCheckpoint) Wk

func (c *MmapCheckpoint) Wk(layer int) []float32

func (*MmapCheckpoint) Wo

func (c *MmapCheckpoint) Wo(layer int) []float32

func (*MmapCheckpoint) Wq

func (c *MmapCheckpoint) Wq(layer int) []float32

func (*MmapCheckpoint) Wv

func (c *MmapCheckpoint) Wv(layer int) []float32

type RunState

type RunState struct {
	X          []float32
	Xb         []float32
	Xb2        []float32
	Hb         []float32
	Hb2        []float32
	Q          []float32
	K          []float32
	V          []float32
	Att        []float32
	Logits     []float32
	KeyCache   []float32
	ValueCache []float32
}

func NewRunState

func NewRunState(c Checkpoint) *RunState

type Vocab

type Vocab struct {
	// contains filtered or unexported fields
}

func LoadTokenizer

func LoadTokenizer(pathname string, size int) (Vocab, error)

func (Vocab) BPEEncode

func (v Vocab) BPEEncode(text string) ([]int, error)

func (Vocab) String

func (v Vocab) String(id int) string

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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