ollama/llm/llama.go

488 lines
12 KiB
Go
Raw Normal View History

2023-07-21 20:33:56 +00:00
package llm
2023-07-03 20:32:48 +00:00
2023-07-07 22:29:17 +00:00
/*
2023-08-13 02:47:15 +00:00
#cgo CFLAGS: -Ofast -std=c11 -fPIC
#cgo CPPFLAGS: -Ofast -Wall -Wextra -Wno-unused-function -Wno-unused-variable -DNDEBUG -DGGML_USE_K_QUANTS
#cgo CXXFLAGS: -std=c++11 -fPIC
#cgo darwin CPPFLAGS: -DGGML_USE_ACCELERATE
#cgo darwin,arm64 CPPFLAGS: -DGGML_USE_METAL -DGGML_METAL_NDEBUG
2023-07-07 22:29:17 +00:00
#cgo darwin LDFLAGS: -framework Accelerate -framework Foundation -framework Metal -framework MetalKit -framework MetalPerformanceShaders
#include <stdlib.h>
#include "llama.h"
struct llama_sample_options
{
float repeat_penalty;
float frequency_penalty;
float presence_penalty;
float temperature;
int32_t top_k;
float top_p;
float tfs_z;
float typical_p;
int mirostat;
float mirostat_tau;
float mirostat_eta;
2023-07-19 19:47:15 +00:00
bool penalize_newline;
2023-07-07 22:29:17 +00:00
};
llama_token llama_sample(
struct llama_context *ctx,
struct llama_token_data *candidates,
size_t n_candidates,
const llama_token *last_tokens,
size_t n_last_tokens,
struct llama_sample_options *opts)
{
llama_token_data_array candidates_p = {
candidates,
n_candidates,
false,
};
2023-07-19 19:47:15 +00:00
struct llama_token_data newline = candidates_p.data[llama_token_nl()];
2023-07-07 22:29:17 +00:00
llama_sample_repetition_penalty(
ctx, &candidates_p,
last_tokens, n_last_tokens,
opts->repeat_penalty);
llama_sample_frequency_and_presence_penalties(
ctx, &candidates_p,
last_tokens, n_last_tokens,
opts->frequency_penalty, opts->presence_penalty);
2023-07-19 19:47:15 +00:00
if (!opts->penalize_newline) {
candidates_p.data[llama_token_nl()] = newline;
}
2023-07-07 22:29:17 +00:00
if (opts->temperature <= 0) {
return llama_sample_token_greedy(ctx, &candidates_p);
}
2023-07-06 21:49:05 +00:00
2023-07-07 22:29:17 +00:00
if (opts->mirostat == 1) {
int mirostat_m = 100;
float mirostat_mu = 2.0f * opts->mirostat_tau;
llama_sample_temperature(ctx, &candidates_p, opts->temperature);
return llama_sample_token_mirostat(
ctx, &candidates_p,
opts->mirostat_tau, opts->mirostat_eta,
mirostat_m, &mirostat_mu);
} else if (opts->mirostat == 2) {
float mirostat_mu = 2.0f * opts->mirostat_tau;
llama_sample_temperature(ctx, &candidates_p, opts->temperature);
return llama_sample_token_mirostat_v2(
ctx, &candidates_p,
opts->mirostat_tau, opts->mirostat_eta,
&mirostat_mu);
} else {
llama_sample_top_k(ctx, &candidates_p, opts->top_k, 1);
llama_sample_tail_free(ctx, &candidates_p, opts->tfs_z, 1);
llama_sample_typical(ctx, &candidates_p, opts->typical_p, 1);
llama_sample_top_p(ctx, &candidates_p, opts->top_p, 1);
llama_sample_temperature(ctx, &candidates_p, opts->temperature);
return llama_sample_token(ctx, &candidates_p);
}
}
*/
import "C"
2023-08-04 22:56:40 +00:00
2023-07-03 20:32:48 +00:00
import (
2023-07-15 01:30:32 +00:00
"bytes"
2023-07-26 18:50:29 +00:00
"embed"
2023-07-07 22:29:17 +00:00
"errors"
2023-07-13 01:18:06 +00:00
"fmt"
2023-07-07 22:29:17 +00:00
"io"
2023-07-19 19:47:15 +00:00
"log"
2023-07-07 22:29:17 +00:00
"os"
2023-07-03 20:32:48 +00:00
"strings"
2023-07-19 22:00:28 +00:00
"sync"
2023-07-15 01:30:32 +00:00
"unicode/utf8"
2023-07-03 20:32:48 +00:00
"unsafe"
2023-07-07 22:29:17 +00:00
"github.com/jmorganca/ollama/api"
2023-07-03 20:32:48 +00:00
)
2023-07-26 18:50:29 +00:00
//go:embed ggml-metal.metal
var fs embed.FS
2023-07-21 20:33:56 +00:00
type llama struct {
2023-07-07 22:29:17 +00:00
params *C.struct_llama_context_params
model *C.struct_llama_model
ctx *C.struct_llama_context
2023-07-03 20:32:48 +00:00
2023-07-19 19:47:15 +00:00
last []C.llama_token
embd []C.llama_token
cursor int
2023-07-19 22:00:28 +00:00
mu sync.Mutex
gc bool
2023-07-07 22:29:17 +00:00
api.Options
}
2023-07-04 04:47:00 +00:00
2023-07-21 20:33:56 +00:00
type llamaHyperparameters struct {
// NumVocab is the size of the model's vocabulary.
NumVocab uint32
// NumEmbd is the size of the model's embedding layer.
NumEmbd uint32
NumMult uint32
NumHead uint32
// NumLayer is the number of layers in the model.
NumLayer uint32
NumRot uint32
// FileType describes the quantization level of the model, e.g. Q4_0, Q5_K, etc.
FileType
}
func newLlama(model string, adapters []string, opts api.Options) (*llama, error) {
2023-07-07 22:29:17 +00:00
if _, err := os.Stat(model); err != nil {
return nil, err
2023-07-03 20:32:48 +00:00
}
2023-07-21 20:33:56 +00:00
llm := llama{Options: opts}
2023-07-07 22:29:17 +00:00
2023-07-11 16:50:02 +00:00
C.llama_backend_init(C.bool(llm.UseNUMA))
2023-07-07 22:29:17 +00:00
params := C.llama_context_default_params()
params.seed = C.uint(llm.Seed)
params.n_ctx = C.int(llm.NumCtx)
params.n_batch = C.int(llm.NumBatch)
2023-07-27 21:04:30 +00:00
params.n_gqa = C.int(llm.NumGQA)
2023-07-07 22:29:17 +00:00
params.n_gpu_layers = C.int(llm.NumGPU)
params.main_gpu = C.int(llm.MainGPU)
params.low_vram = C.bool(llm.LowVRAM)
params.f16_kv = C.bool(llm.F16KV)
params.logits_all = C.bool(llm.LogitsAll)
params.vocab_only = C.bool(llm.VocabOnly)
params.use_mmap = C.bool(llm.UseMMap)
params.use_mlock = C.bool(llm.UseMLock)
params.embedding = C.bool(llm.EmbeddingOnly)
2023-08-04 05:10:49 +00:00
params.rope_freq_base = C.float(llm.RopeFrequencyBase)
params.rope_freq_scale = C.float(llm.RopeFrequencyScale)
if len(adapters) > 0 && llm.UseMMap {
log.Printf("must disable mmap to use lora adapters")
params.use_mmap = C.bool(false)
}
2023-07-07 22:29:17 +00:00
llm.params = &params
cModel := C.CString(model)
defer C.free(unsafe.Pointer(cModel))
llm.model = C.llama_load_model_from_file(cModel, params)
2023-07-12 03:32:26 +00:00
if llm.model == nil {
return nil, errors.New("failed to load model")
}
2023-07-07 22:29:17 +00:00
llm.ctx = C.llama_new_context_with_model(llm.model, params)
2023-07-12 03:32:26 +00:00
if llm.ctx == nil {
return nil, errors.New("failed to create context")
}
2023-07-07 22:29:17 +00:00
for _, adapter := range adapters {
cAdapter := C.CString(adapter)
defer C.free(unsafe.Pointer(cAdapter))
if retval := C.llama_model_apply_lora_from_file(llm.model, cAdapter, nil, C.int(llm.NumThread)); retval != 0 {
return nil, fmt.Errorf("failed to load adapter %s", adapter)
}
}
2023-07-07 22:29:17 +00:00
// warm up the model
bos := []C.llama_token{C.llama_token_bos()}
C.llama_eval(llm.ctx, unsafe.SliceData(bos), C.int(len(bos)), 0, C.int(opts.NumThread))
C.llama_reset_timings(llm.ctx)
return &llm, nil
2023-07-03 20:32:48 +00:00
}
2023-07-21 20:33:56 +00:00
func (llm *llama) Close() {
2023-07-19 22:00:28 +00:00
llm.gc = true
llm.mu.Lock()
defer llm.mu.Unlock()
2023-07-07 22:29:17 +00:00
defer C.llama_free_model(llm.model)
defer C.llama_free(llm.ctx)
C.llama_print_timings(llm.ctx)
2023-07-03 20:32:48 +00:00
}
2023-07-21 20:33:56 +00:00
func (llm *llama) SetOptions(opts api.Options) {
llm.Options = opts
}
2023-07-27 18:27:49 +00:00
var errNeedMoreData = errors.New("need more data")
2023-07-21 20:33:56 +00:00
func (llm *llama) Predict(ctx []int, prompt string, fn func(api.GenerateResponse)) error {
2023-07-19 19:47:15 +00:00
C.llama_reset_timings(llm.ctx)
2023-07-21 20:33:56 +00:00
llm.marshalPrompt(ctx, prompt)
2023-07-19 19:47:15 +00:00
C.llama_set_rng_seed(llm.ctx, C.uint(llm.Seed))
var b bytes.Buffer
for {
token, err := llm.next()
if llm.gc {
return nil
} else if errors.Is(err, io.EOF) {
2023-07-19 19:47:15 +00:00
break
} else if err != nil {
return err
}
2023-07-21 20:33:56 +00:00
b.WriteString(llm.Decode(int(token)))
2023-07-27 18:27:49 +00:00
if err := llm.checkStopConditions(b); err != nil {
if errors.Is(err, io.EOF) {
break
} else if errors.Is(err, errNeedMoreData) {
continue
}
return err
}
2023-07-19 19:47:15 +00:00
if utf8.Valid(b.Bytes()) || b.Len() >= utf8.UTFMax {
fn(api.GenerateResponse{Response: b.String()})
b.Reset()
}
2023-07-19 19:47:15 +00:00
}
embd := make([]int, len(llm.embd))
for i := range llm.embd {
embd[i] = int(llm.embd[i])
2023-07-03 20:32:48 +00:00
}
2023-07-19 19:47:15 +00:00
timings := C.llama_get_timings(llm.ctx)
fn(api.GenerateResponse{
Done: true,
Context: embd,
2023-07-25 22:51:32 +00:00
SampleCount: int(timings.n_sample),
SampleDuration: parseDurationMs(float64(timings.t_sample_ms)),
2023-07-19 19:47:15 +00:00
PromptEvalCount: int(timings.n_p_eval),
PromptEvalDuration: parseDurationMs(float64(timings.t_p_eval_ms)),
EvalCount: int(timings.n_eval),
EvalDuration: parseDurationMs(float64(timings.t_eval_ms)),
})
return nil
}
2023-07-21 20:33:56 +00:00
func (llm *llama) checkStopConditions(b bytes.Buffer) error {
for _, stopCondition := range llm.Stop {
if stopCondition == strings.TrimSpace(b.String()) {
2023-07-27 18:27:49 +00:00
return io.EOF
} else if strings.HasPrefix(stopCondition, strings.TrimSpace(b.String())) {
2023-07-27 18:27:49 +00:00
return errNeedMoreData
}
}
return nil
}
2023-07-21 20:33:56 +00:00
func (llm *llama) marshalPrompt(ctx []int, prompt string) []C.llama_token {
tokens := append(ctx, llm.Encode(prompt)...)
2023-07-19 19:47:15 +00:00
if llm.NumKeep < 0 {
llm.NumKeep = len(tokens)
}
2023-07-21 20:33:56 +00:00
cTokens := make([]C.llama_token, len(tokens))
for i := range tokens {
cTokens[i] = C.llama_token(tokens[i])
}
2023-07-19 19:47:15 +00:00
// min(llm.NumCtx - 4, llm.NumKeep)
if llm.NumCtx-4 < llm.NumKeep {
llm.NumKeep = llm.NumCtx - 4
}
if len(tokens) >= llm.NumCtx {
// truncate input
numLeft := (llm.NumCtx - llm.NumKeep) / 2
2023-07-21 20:33:56 +00:00
truncated := cTokens[:llm.NumKeep]
erasedBlocks := (len(cTokens) - llm.NumKeep - numLeft - 1) / numLeft
truncated = append(truncated, cTokens[llm.NumKeep+erasedBlocks*numLeft:]...)
copy(llm.last, cTokens[len(cTokens)-llm.NumCtx:])
2023-07-19 19:47:15 +00:00
2023-07-21 20:33:56 +00:00
cTokens = truncated
2023-07-19 19:47:15 +00:00
log.Printf("input truncated: num_ctx=%d num_keep=%d num_left=%d num_tokens=%d", llm.NumCtx, llm.NumKeep, numLeft, len(truncated))
} else {
2023-07-21 20:33:56 +00:00
llm.last = make([]C.llama_token, llm.NumCtx-len(cTokens))
llm.last = append(llm.last, cTokens...)
2023-07-19 19:47:15 +00:00
}
var i int
2023-07-21 20:33:56 +00:00
for i = 0; i < len(llm.embd) && i < len(cTokens) && llm.embd[i] == cTokens[i]; i++ {
2023-07-19 19:47:15 +00:00
// noop
}
2023-07-21 20:33:56 +00:00
llm.embd = cTokens
if i == len(cTokens) {
2023-07-19 19:47:15 +00:00
// evaluate at least one token to generate logits
i--
}
llm.cursor = i
log.Printf("prompt: num_past=%d cached=%v eval=%v", i, len(llm.embd[:i]), len(llm.embd[i:]))
2023-07-21 20:33:56 +00:00
return cTokens
2023-07-07 22:29:17 +00:00
}
2023-07-06 23:37:11 +00:00
2023-07-21 20:33:56 +00:00
func (llm *llama) Encode(prompt string) []int {
2023-07-07 22:29:17 +00:00
cPrompt := C.CString(prompt)
defer C.free(unsafe.Pointer(cPrompt))
2023-07-06 18:24:54 +00:00
2023-07-21 20:33:56 +00:00
cTokens := make([]C.llama_token, len(prompt)+1)
if n := C.llama_tokenize(llm.ctx, cPrompt, unsafe.SliceData(cTokens), C.int(len(cTokens)), true); n > 0 {
tokens := make([]int, n)
for i := range cTokens[:n] {
tokens[i] = int(cTokens[i])
}
return tokens
2023-07-03 20:32:48 +00:00
}
return nil
}
2023-07-21 20:33:56 +00:00
func (llm *llama) Decode(tokens ...int) string {
2023-07-07 22:29:17 +00:00
var sb strings.Builder
for _, token := range tokens {
2023-07-21 20:33:56 +00:00
sb.WriteString(C.GoString(C.llama_token_to_str(llm.ctx, C.llama_token(token))))
2023-07-03 20:32:48 +00:00
}
2023-07-07 22:29:17 +00:00
return sb.String()
2023-07-03 20:32:48 +00:00
}
2023-07-21 20:33:56 +00:00
func (llm *llama) next() (C.llama_token, error) {
llm.mu.Lock()
defer llm.mu.Unlock()
2023-07-19 19:47:15 +00:00
if len(llm.embd) >= llm.NumCtx {
numLeft := (llm.NumCtx - llm.NumKeep) / 2
truncated := llm.embd[:llm.NumKeep]
truncated = append(truncated, llm.embd[len(llm.embd)-numLeft:]...)
2023-07-19 19:47:15 +00:00
llm.embd = truncated
llm.cursor = llm.NumKeep
log.Printf("input truncated: num_ctx=%d num_keep=%d num_left=%d num_tokens=%d cursor=%d", llm.NumCtx, llm.NumKeep, numLeft, len(truncated), llm.cursor)
}
2023-07-07 22:29:17 +00:00
2023-07-19 19:47:15 +00:00
for {
if llm.gc {
return 0, io.EOF
}
2023-07-19 19:47:15 +00:00
if llm.cursor >= len(llm.embd) {
2023-07-13 01:18:06 +00:00
break
2023-07-07 22:29:17 +00:00
}
2023-07-19 19:47:15 +00:00
numEval := len(llm.embd) - llm.cursor
if numEval > llm.NumBatch {
numEval = llm.NumBatch
2023-07-15 01:30:32 +00:00
}
2023-07-13 01:18:06 +00:00
2023-07-19 19:47:15 +00:00
if retval := C.llama_eval(llm.ctx, unsafe.SliceData(llm.embd[llm.cursor:]), C.int(numEval), C.int(llm.cursor), C.int(llm.NumThread)); retval != 0 {
return 0, fmt.Errorf("llama_eval: %d", retval)
2023-07-13 01:18:06 +00:00
}
2023-07-07 22:29:17 +00:00
2023-07-19 19:47:15 +00:00
llm.cursor += numEval
2023-07-03 20:32:48 +00:00
}
2023-07-19 19:47:15 +00:00
var sampleOpts C.struct_llama_sample_options
sampleOpts.repeat_penalty = C.float(llm.RepeatPenalty)
sampleOpts.frequency_penalty = C.float(llm.FrequencyPenalty)
sampleOpts.presence_penalty = C.float(llm.PresencePenalty)
sampleOpts.temperature = C.float(llm.Temperature)
sampleOpts.top_k = C.int(llm.TopK)
sampleOpts.top_p = C.float(llm.TopP)
sampleOpts.tfs_z = C.float(llm.TFSZ)
sampleOpts.typical_p = C.float(llm.TypicalP)
sampleOpts.mirostat = C.int(llm.Mirostat)
sampleOpts.mirostat_tau = C.float(llm.MirostatTau)
sampleOpts.mirostat_eta = C.float(llm.MirostatEta)
sampleOpts.penalize_newline = C.bool(llm.PenalizeNewline)
numVocab := C.llama_n_vocab(llm.ctx)
2023-07-07 22:29:17 +00:00
logits := unsafe.Slice(C.llama_get_logits(llm.ctx), numVocab)
2023-07-19 19:47:15 +00:00
// TODO: logit bias
candidates := make([]C.llama_token_data, numVocab)
for i := range logits {
candidates[i] = C.llama_token_data{
2023-07-07 22:29:17 +00:00
id: C.int(i),
logit: logits[i],
p: 0,
2023-07-19 19:47:15 +00:00
}
2023-07-07 22:29:17 +00:00
}
2023-07-03 20:32:48 +00:00
2023-07-19 19:47:15 +00:00
repeatLastN := llm.RepeatLastN
if len(llm.last) < repeatLastN {
repeatLastN = len(llm.last)
}
if llm.NumCtx < repeatLastN {
repeatLastN = llm.NumCtx
}
lastN := llm.last[len(llm.last)-repeatLastN:]
2023-07-07 22:29:17 +00:00
token := C.llama_sample(
llm.ctx,
2023-07-19 19:47:15 +00:00
unsafe.SliceData(candidates), C.size_t(len(candidates)),
unsafe.SliceData(lastN), C.size_t(len(lastN)),
&sampleOpts,
)
llm.last = append(llm.last, token)
llm.embd = append(llm.embd, token)
if token == C.llama_token_eos() {
return 0, io.EOF
2023-07-03 20:32:48 +00:00
}
2023-07-07 22:29:17 +00:00
2023-07-19 19:47:15 +00:00
return token, nil
2023-07-03 20:32:48 +00:00
}
2023-08-04 22:56:40 +00:00
2023-07-21 20:33:56 +00:00
func (llm *llama) Embedding(input string) ([]float64, error) {
2023-08-04 22:56:40 +00:00
if !llm.EmbeddingOnly {
return nil, errors.New("llama: embedding not enabled")
}
2023-08-09 14:45:57 +00:00
tokens := llm.Encode(input)
2023-08-04 22:56:40 +00:00
if tokens == nil {
return nil, errors.New("llama: tokenize embedding")
}
2023-07-21 20:33:56 +00:00
cTokens := make([]C.llama_token, len(tokens))
for i := range tokens {
cTokens[i] = C.llama_token(tokens[i])
}
retval := C.llama_eval(llm.ctx, unsafe.SliceData(cTokens), C.int(len(tokens)), 0, C.int(llm.NumThread))
2023-08-04 22:56:40 +00:00
if retval != 0 {
return nil, errors.New("llama: eval")
}
2023-08-14 15:55:50 +00:00
C.llama_print_timings(llm.ctx)
2023-08-14 15:51:31 +00:00
2023-08-09 20:13:24 +00:00
n := C.llama_n_embd(llm.ctx)
2023-08-04 22:56:40 +00:00
if n <= 0 {
return nil, errors.New("llama: no embeddings generated")
}
2023-08-09 20:13:24 +00:00
cEmbeddings := unsafe.Slice(C.llama_get_embeddings(llm.ctx), n)
2023-08-04 22:56:40 +00:00
2023-08-09 20:13:24 +00:00
embeddings := make([]float64, len(cEmbeddings))
for i, v := range cEmbeddings {
embeddings[i] = float64(v)
2023-08-04 22:56:40 +00:00
}
2023-08-09 20:13:24 +00:00
return embeddings, nil
2023-08-04 22:56:40 +00:00
}