repo
stringlengths
6
47
file_url
stringlengths
77
269
file_path
stringlengths
5
186
content
stringlengths
0
32.8k
language
stringclasses
1 value
license
stringclasses
7 values
commit_sha
stringlengths
40
40
retrieved_at
stringdate
2026-01-07 08:35:43
2026-01-07 08:55:24
truncated
bool
2 classes
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/server/sparse_common.go
server/sparse_common.go
//go:build !windows package server import "os" func setSparse(*os.File) { }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/server/routes_generate_test.go
server/routes_generate_test.go
package server import ( "bytes" "context" "encoding/json" "io" "net/http" "net/http/httptest" "net/url" "strings" "sync" "testing" "time" "github.com/gin-gonic/gin" "github.com/google/go-cmp/cmp" "github.com/ollama/ollama/api" "github.com/ollama/ollama/fs/ggml" "github.com/ollama/ollama/llm" "github.com/ollama/ollama/ml" ) // testPropsMap creates a ToolPropertiesMap from a map (convenience function for tests) func testPropsMap(m map[string]api.ToolProperty) *api.ToolPropertiesMap { props := api.NewToolPropertiesMap() for k, v := range m { props.Set(k, v) } return props } // testArgs creates ToolCallFunctionArguments from a map (convenience function for tests) func testArgs(m map[string]any) api.ToolCallFunctionArguments { args := api.NewToolCallFunctionArguments() for k, v := range m { args.Set(k, v) } return args } // argsComparer provides cmp options for comparing ToolCallFunctionArguments by value var argsComparer = cmp.Comparer(func(a, b api.ToolCallFunctionArguments) bool { return cmp.Equal(a.ToMap(), b.ToMap()) }) type mockRunner struct { llm.LlamaServer // CompletionRequest is only valid until the next call to Completion llm.CompletionRequest llm.CompletionResponse CompletionFn func(context.Context, llm.CompletionRequest, func(llm.CompletionResponse)) error } func (m *mockRunner) Completion(ctx context.Context, r llm.CompletionRequest, fn func(r llm.CompletionResponse)) error { m.CompletionRequest = r if m.CompletionFn != nil { return m.CompletionFn(ctx, r, fn) } fn(m.CompletionResponse) return nil } func (mockRunner) Tokenize(_ context.Context, s string) (tokens []int, err error) { for range strings.Fields(s) { tokens = append(tokens, len(tokens)) } return } func newMockServer(mock *mockRunner) func(ml.SystemInfo, []ml.DeviceInfo, string, *ggml.GGML, []string, []string, api.Options, int) (llm.LlamaServer, error) { return func(_ ml.SystemInfo, _ []ml.DeviceInfo, _ string, _ *ggml.GGML, _, _ []string, _ api.Options, _ int) (llm.LlamaServer, error) { return mock, nil } } func TestGenerateChatRemote(t *testing.T) { gin.SetMode(gin.TestMode) rs := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { t.Errorf("Expected POST request, got %s", r.Method) } if r.URL.Path != "/api/chat" { t.Errorf("Expected path '/api/chat', got %s", r.URL.Path) } w.WriteHeader(http.StatusOK) w.Header().Set("Content-Type", "application/json") resp := api.ChatResponse{ Model: "test", Done: true, DoneReason: "load", } if err := json.NewEncoder(w).Encode(&resp); err != nil { t.Fatal(err) } })) defer rs.Close() p, err := url.Parse(rs.URL) if err != nil { t.Fatal(err) } t.Setenv("OLLAMA_REMOTES", p.Hostname()) s := Server{} w := createRequest(t, s.CreateHandler, api.CreateRequest{ Model: "test-cloud", RemoteHost: rs.URL, From: "test", Info: map[string]any{ "capabilities": []string{"completion", "thinking"}, }, Stream: &stream, }) if w.Code != http.StatusOK { t.Fatalf("expected status 200, got %d", w.Code) } t.Run("missing messages", func(t *testing.T) { w := createRequest(t, s.ChatHandler, api.ChatRequest{ Model: "test-cloud", }) if w.Code != http.StatusOK { t.Errorf("expected status 200, got %d", w.Code) } var actual api.ChatResponse if err := json.NewDecoder(w.Body).Decode(&actual); err != nil { t.Fatal(err) } if actual.Model != "test-cloud" { t.Errorf("expected model test-cloud, got %s", actual.Model) } if actual.RemoteModel != "test" { t.Errorf("expected remote model test, got %s", actual.RemoteModel) } if actual.RemoteHost != rs.URL { t.Errorf("expected remote host '%s', got %s", rs.URL, actual.RemoteHost) } if !actual.Done { t.Errorf("expected done true, got false") } if actual.DoneReason != "load" { t.Errorf("expected done reason load, got %s", actual.DoneReason) } }) } func TestGenerateChat(t *testing.T) { gin.SetMode(gin.TestMode) mock := mockRunner{ CompletionResponse: llm.CompletionResponse{ Done: true, DoneReason: llm.DoneReasonStop, PromptEvalCount: 1, PromptEvalDuration: 1, EvalCount: 1, EvalDuration: 1, }, } s := Server{ sched: &Scheduler{ pendingReqCh: make(chan *LlmRequest, 1), finishedReqCh: make(chan *LlmRequest, 1), expiredCh: make(chan *runnerRef, 1), unloadedCh: make(chan any, 1), loaded: make(map[string]*runnerRef), newServerFn: newMockServer(&mock), getGpuFn: getGpuFn, getSystemInfoFn: getSystemInfoFn, waitForRecovery: 250 * time.Millisecond, loadFn: func(req *LlmRequest, _ *ggml.GGML, _ ml.SystemInfo, _ []ml.DeviceInfo, _ bool) bool { // add small delay to simulate loading time.Sleep(time.Millisecond) req.successCh <- &runnerRef{ llama: &mock, } return false }, }, } go s.sched.Run(t.Context()) _, digest := createBinFile(t, ggml.KV{ "general.architecture": "llama", "llama.block_count": uint32(1), "llama.context_length": uint32(8192), "llama.embedding_length": uint32(4096), "llama.attention.head_count": uint32(32), "llama.attention.head_count_kv": uint32(8), "tokenizer.ggml.tokens": []string{""}, "tokenizer.ggml.scores": []float32{0}, "tokenizer.ggml.token_type": []int32{0}, }, []*ggml.Tensor{ {Name: "token_embd.weight", Shape: []uint64{1}, WriterTo: bytes.NewReader(make([]byte, 4))}, {Name: "blk.0.attn_norm.weight", Shape: []uint64{1}, WriterTo: bytes.NewReader(make([]byte, 4))}, {Name: "blk.0.ffn_down.weight", Shape: []uint64{1}, WriterTo: bytes.NewReader(make([]byte, 4))}, {Name: "blk.0.ffn_gate.weight", Shape: []uint64{1}, WriterTo: bytes.NewReader(make([]byte, 4))}, {Name: "blk.0.ffn_up.weight", Shape: []uint64{1}, WriterTo: bytes.NewReader(make([]byte, 4))}, {Name: "blk.0.ffn_norm.weight", Shape: []uint64{1}, WriterTo: bytes.NewReader(make([]byte, 4))}, {Name: "blk.0.attn_k.weight", Shape: []uint64{1}, WriterTo: bytes.NewReader(make([]byte, 4))}, {Name: "blk.0.attn_output.weight", Shape: []uint64{1}, WriterTo: bytes.NewReader(make([]byte, 4))}, {Name: "blk.0.attn_q.weight", Shape: []uint64{1}, WriterTo: bytes.NewReader(make([]byte, 4))}, {Name: "blk.0.attn_v.weight", Shape: []uint64{1}, WriterTo: bytes.NewReader(make([]byte, 4))}, {Name: "output.weight", Shape: []uint64{1}, WriterTo: bytes.NewReader(make([]byte, 4))}, }) w := createRequest(t, s.CreateHandler, api.CreateRequest{ Model: "test", Files: map[string]string{"file.gguf": digest}, Template: ` {{- if .Tools }} {{ .Tools }} {{ end }} {{- range .Messages }} {{- .Role }}: {{ .Content }} {{- range .ToolCalls }}{"name": "{{ .Function.Name }}", "arguments": {{ .Function.Arguments }}} {{- end }} {{ end }}`, Stream: &stream, }) if w.Code != http.StatusOK { t.Fatalf("expected status 200, got %d", w.Code) } t.Run("missing body", func(t *testing.T) { w := createRequest(t, s.ChatHandler, nil) if w.Code != http.StatusBadRequest { t.Errorf("expected status 400, got %d", w.Code) } if diff := cmp.Diff(w.Body.String(), `{"error":"model is required"}`); diff != "" { t.Errorf("mismatch (-got +want):\n%s", diff) } }) t.Run("missing thinking capability", func(t *testing.T) { think := true w := createRequest(t, s.ChatHandler, api.ChatRequest{ Model: "test", Messages: []api.Message{ {Role: "user", Content: "Hello!"}, }, Think: &api.ThinkValue{Value: think}, }) if w.Code != http.StatusBadRequest { t.Errorf("expected status 400, got %d", w.Code) } if diff := cmp.Diff(w.Body.String(), `{"error":"\"test\" does not support thinking"}`); diff != "" { t.Errorf("mismatch (-got +want):\n%s", diff) } }) t.Run("model can't think but think set false", func(t *testing.T) { think := false w := createRequest(t, s.ChatHandler, api.ChatRequest{ Model: "test", Messages: []api.Message{ {Role: "user", Content: "Hello!"}, }, Think: &api.ThinkValue{Value: think}, }) if w.Code != http.StatusOK { t.Errorf("expected status 200, got %d", w.Code) } }) t.Run("missing model", func(t *testing.T) { w := createRequest(t, s.ChatHandler, api.ChatRequest{}) if w.Code != http.StatusBadRequest { t.Errorf("expected status 400, got %d", w.Code) } if diff := cmp.Diff(w.Body.String(), `{"error":"model is required"}`); diff != "" { t.Errorf("mismatch (-got +want):\n%s", diff) } }) t.Run("missing capabilities chat", func(t *testing.T) { _, digest := createBinFile(t, ggml.KV{ "general.architecture": "bert", "bert.pooling_type": uint32(0), }, []*ggml.Tensor{}) w := createRequest(t, s.CreateHandler, api.CreateRequest{ Model: "bert", Files: map[string]string{"bert.gguf": digest}, Stream: &stream, }) if w.Code != http.StatusOK { t.Fatalf("expected status 200, got %d", w.Code) } w = createRequest(t, s.ChatHandler, api.ChatRequest{ Model: "bert", }) if w.Code != http.StatusBadRequest { t.Errorf("expected status 400, got %d", w.Code) } if diff := cmp.Diff(w.Body.String(), `{"error":"\"bert\" does not support chat"}`); diff != "" { t.Errorf("mismatch (-got +want):\n%s", diff) } }) t.Run("load model", func(t *testing.T) { w := createRequest(t, s.ChatHandler, api.ChatRequest{ Model: "test", }) if w.Code != http.StatusOK { t.Errorf("expected status 200, got %d", w.Code) } var actual api.ChatResponse if err := json.NewDecoder(w.Body).Decode(&actual); err != nil { t.Fatal(err) } if actual.Model != "test" { t.Errorf("expected model test, got %s", actual.Model) } if !actual.Done { t.Errorf("expected done true, got false") } if actual.DoneReason != "load" { t.Errorf("expected done reason load, got %s", actual.DoneReason) } }) checkChatResponse := func(t *testing.T, body io.Reader, model, content string) { t.Helper() var actual api.ChatResponse if err := json.NewDecoder(body).Decode(&actual); err != nil { t.Fatal(err) } if actual.Model != model { t.Errorf("expected model test, got %s", actual.Model) } if !actual.Done { t.Errorf("expected done false, got true") } if actual.DoneReason != "stop" { t.Errorf("expected done reason stop, got %s", actual.DoneReason) } if diff := cmp.Diff(actual.Message, api.Message{ Role: "assistant", Content: content, }); diff != "" { t.Errorf("mismatch (-got +want):\n%s", diff) } if actual.PromptEvalCount == 0 { t.Errorf("expected prompt eval count > 0, got 0") } if actual.PromptEvalDuration == 0 { t.Errorf("expected prompt eval duration > 0, got 0") } if actual.EvalCount == 0 { t.Errorf("expected eval count > 0, got 0") } if actual.EvalDuration == 0 { t.Errorf("expected eval duration > 0, got 0") } if actual.LoadDuration == 0 { t.Errorf("expected load duration > 0, got 0") } if actual.TotalDuration == 0 { t.Errorf("expected total duration > 0, got 0") } } mock.CompletionResponse.Content = "Hi!" t.Run("messages", func(t *testing.T) { w := createRequest(t, s.ChatHandler, api.ChatRequest{ Model: "test", Messages: []api.Message{ {Role: "user", Content: "Hello!"}, }, Stream: &stream, }) if w.Code != http.StatusOK { t.Errorf("expected status 200, got %d", w.Code) } if diff := cmp.Diff(mock.CompletionRequest.Prompt, "user: Hello!\n"); diff != "" { t.Errorf("mismatch (-got +want):\n%s", diff) } checkChatResponse(t, w.Body, "test", "Hi!") }) w = createRequest(t, s.CreateHandler, api.CreateRequest{ Model: "test-system", From: "test", System: "You are a helpful assistant.", }) if w.Code != http.StatusOK { t.Fatalf("expected status 200, got %d", w.Code) } t.Run("messages with model system", func(t *testing.T) { w := createRequest(t, s.ChatHandler, api.ChatRequest{ Model: "test-system", Messages: []api.Message{ {Role: "user", Content: "Hello!"}, }, Stream: &stream, }) if w.Code != http.StatusOK { t.Errorf("expected status 200, got %d", w.Code) } if diff := cmp.Diff(mock.CompletionRequest.Prompt, "system: You are a helpful assistant.\nuser: Hello!\n"); diff != "" { t.Errorf("mismatch (-got +want):\n%s", diff) } checkChatResponse(t, w.Body, "test-system", "Hi!") }) mock.CompletionResponse.Content = "Abra kadabra!" t.Run("messages with system", func(t *testing.T) { w := createRequest(t, s.ChatHandler, api.ChatRequest{ Model: "test-system", Messages: []api.Message{ {Role: "system", Content: "You can perform magic tricks."}, {Role: "user", Content: "Hello!"}, }, Stream: &stream, }) if w.Code != http.StatusOK { t.Errorf("expected status 200, got %d", w.Code) } if diff := cmp.Diff(mock.CompletionRequest.Prompt, "system: You can perform magic tricks.\nuser: Hello!\n"); diff != "" { t.Errorf("mismatch (-got +want):\n%s", diff) } checkChatResponse(t, w.Body, "test-system", "Abra kadabra!") }) t.Run("messages with interleaved system", func(t *testing.T) { w := createRequest(t, s.ChatHandler, api.ChatRequest{ Model: "test-system", Messages: []api.Message{ {Role: "user", Content: "Hello!"}, {Role: "assistant", Content: "I can help you with that."}, {Role: "system", Content: "You can perform magic tricks."}, {Role: "user", Content: "Help me write tests."}, }, Stream: &stream, }) if w.Code != http.StatusOK { t.Errorf("expected status 200, got %d", w.Code) } if diff := cmp.Diff(mock.CompletionRequest.Prompt, "system: You are a helpful assistant.\nuser: Hello!\nassistant: I can help you with that.\nsystem: You can perform magic tricks.\nuser: Help me write tests.\n"); diff != "" { t.Errorf("mismatch (-got +want):\n%s", diff) } checkChatResponse(t, w.Body, "test-system", "Abra kadabra!") }) t.Run("messages with tools (non-streaming)", func(t *testing.T) { if w.Code != http.StatusOK { t.Fatalf("failed to create test-system model: %d", w.Code) } tools := []api.Tool{ { Type: "function", Function: api.ToolFunction{ Name: "get_weather", Description: "Get the current weather", Parameters: api.ToolFunctionParameters{ Type: "object", Required: []string{"location"}, Properties: testPropsMap(map[string]api.ToolProperty{ "location": { Type: api.PropertyType{"string"}, Description: "The city and state", }, "unit": { Type: api.PropertyType{"string"}, Enum: []any{"celsius", "fahrenheit"}, }, }), }, }, }, } mock.CompletionResponse = llm.CompletionResponse{ Content: `{"name":"get_weather","arguments":{"location":"Seattle, WA","unit":"celsius"}}`, Done: true, DoneReason: llm.DoneReasonStop, PromptEvalCount: 1, PromptEvalDuration: 1, EvalCount: 1, EvalDuration: 1, } streamRequest := true w := createRequest(t, s.ChatHandler, api.ChatRequest{ Model: "test-system", Messages: []api.Message{ {Role: "user", Content: "What's the weather in Seattle?"}, }, Tools: tools, Stream: &streamRequest, }) if w.Code != http.StatusOK { var errResp struct { Error string `json:"error"` } if err := json.NewDecoder(w.Body).Decode(&errResp); err != nil { t.Logf("Failed to decode error response: %v", err) } else { t.Logf("Error response: %s", errResp.Error) } } if w.Code != http.StatusOK { t.Errorf("expected status 200, got %d", w.Code) } var resp api.ChatResponse if err := json.NewDecoder(w.Body).Decode(&resp); err != nil { t.Fatal(err) } if resp.Message.ToolCalls == nil { t.Error("expected tool calls, got nil") } gotToolCall := resp.Message.ToolCalls[0] if gotToolCall.ID == "" { t.Error("expected tool call ID to be populated") } if !strings.HasPrefix(gotToolCall.ID, "call_") { t.Errorf("expected tool call ID to have call_ prefix, got %q", gotToolCall.ID) } expectedToolCall := api.ToolCall{ Function: api.ToolCallFunction{ Name: "get_weather", Arguments: testArgs(map[string]any{ "location": "Seattle, WA", "unit": "celsius", }), }, } expectedToolCall.ID = gotToolCall.ID if diff := cmp.Diff(gotToolCall, expectedToolCall, argsComparer); diff != "" { t.Errorf("tool call mismatch (-got +want):\n%s", diff) } }) t.Run("messages with tools (streaming)", func(t *testing.T) { tools := []api.Tool{ { Type: "function", Function: api.ToolFunction{ Name: "get_weather", Description: "Get the current weather", Parameters: api.ToolFunctionParameters{ Type: "object", Required: []string{"location"}, Properties: testPropsMap(map[string]api.ToolProperty{ "location": { Type: api.PropertyType{"string"}, Description: "The city and state", }, "unit": { Type: api.PropertyType{"string"}, Enum: []any{"celsius", "fahrenheit"}, }, }), }, }, }, } // Simulate streaming response with multiple chunks var wg sync.WaitGroup wg.Add(1) mock.CompletionFn = func(ctx context.Context, r llm.CompletionRequest, fn func(r llm.CompletionResponse)) error { defer wg.Done() // Send chunks with small delays to simulate streaming responses := []llm.CompletionResponse{ { Content: `{"name":"get_`, Done: false, PromptEvalCount: 1, PromptEvalDuration: 1, }, { Content: `weather","arguments":{"location":"Seattle`, Done: false, PromptEvalCount: 2, PromptEvalDuration: 1, }, { Content: `, WA","unit":"celsius"}}`, Done: true, DoneReason: llm.DoneReasonStop, PromptEvalCount: 3, PromptEvalDuration: 1, }, } for _, resp := range responses { select { case <-ctx.Done(): return ctx.Err() default: fn(resp) time.Sleep(10 * time.Millisecond) // Small delay between chunks } } return nil } w := createRequest(t, s.ChatHandler, api.ChatRequest{ Model: "test-system", Messages: []api.Message{ {Role: "user", Content: "What's the weather in Seattle?"}, }, Tools: tools, Stream: &stream, }) wg.Wait() if w.Code != http.StatusOK { t.Errorf("expected status 200, got %d", w.Code) } // Read and validate the streamed responses decoder := json.NewDecoder(w.Body) var finalToolCall api.ToolCall for { var resp api.ChatResponse if err := decoder.Decode(&resp); err == io.EOF { break } else if err != nil { t.Fatal(err) } if len(resp.Message.ToolCalls) > 0 { for _, call := range resp.Message.ToolCalls { if call.ID == "" { t.Fatal("expected streaming tool call to have an ID") } if !strings.HasPrefix(call.ID, "call_") { t.Fatalf("expected streaming tool call ID to have call_ prefix, got %q", call.ID) } } } if resp.Done { if len(resp.Message.ToolCalls) != 1 { t.Errorf("expected 1 tool call in final response, got %d", len(resp.Message.ToolCalls)) } finalToolCall = resp.Message.ToolCalls[0] } } expectedToolCall := api.ToolCall{ Function: api.ToolCallFunction{ Name: "get_weather", Arguments: testArgs(map[string]any{ "location": "Seattle, WA", "unit": "celsius", }), }, } if finalToolCall.ID == "" { t.Fatal("expected final tool call to have an ID") } if !strings.HasPrefix(finalToolCall.ID, "call_") { t.Fatalf("expected final tool call ID to have call_ prefix, got %q", finalToolCall.ID) } expectedToolCall.ID = finalToolCall.ID if diff := cmp.Diff(finalToolCall, expectedToolCall, argsComparer); diff != "" { t.Errorf("final tool call mismatch (-got +want):\n%s", diff) } }) t.Run("messages with tools and logprobs (streaming)", func(t *testing.T) { tools := []api.Tool{ { Type: "function", Function: api.ToolFunction{ Name: "get_weather", Parameters: api.ToolFunctionParameters{ Type: "object", Properties: testPropsMap(map[string]api.ToolProperty{ "location": {Type: api.PropertyType{"string"}}, }), }, }, }, } var wg sync.WaitGroup wg.Add(1) mock.CompletionFn = func(ctx context.Context, r llm.CompletionRequest, fn func(r llm.CompletionResponse)) error { defer wg.Done() // Simulate a response where logprobs are sent while the tool call is being buffered responses := []llm.CompletionResponse{ { Content: `{ "name": "get_weather"`, Done: false, Logprobs: []llm.Logprob{{}}, }, { Content: `,"arguments":{"location":"Seattle, WA","unit":"celsius"}}`, Done: false, Logprobs: []llm.Logprob{{}}, }, { Content: ``, Done: true, DoneReason: llm.DoneReasonStop, Logprobs: nil, }, } for _, resp := range responses { select { case <-ctx.Done(): return ctx.Err() default: fn(resp) time.Sleep(10 * time.Millisecond) } } return nil } w := createRequest(t, s.ChatHandler, api.ChatRequest{ Model: "test-system", Messages: []api.Message{ {Role: "user", Content: "Weather?"}, }, Tools: tools, Stream: &stream, }) wg.Wait() if w.Code != http.StatusOK { t.Errorf("expected status 200, got %d", w.Code) } decoder := json.NewDecoder(w.Body) var totalLogprobs int for { var resp api.ChatResponse if err := decoder.Decode(&resp); err == io.EOF { break } else if err != nil { t.Fatal(err) } totalLogprobs += len(resp.Logprobs) } expectedLogprobs := 2 if totalLogprobs != expectedLogprobs { t.Errorf("expected %d logprobs, got %d", expectedLogprobs, totalLogprobs) } }) t.Run("status error non-streaming", func(t *testing.T) { mock.CompletionFn = func(ctx context.Context, r llm.CompletionRequest, fn func(r llm.CompletionResponse)) error { return api.StatusError{ StatusCode: http.StatusServiceUnavailable, Status: "Service Unavailable", ErrorMessage: "model is overloaded", } } stream := false w := createRequest(t, s.ChatHandler, api.ChatRequest{ Model: "test", Messages: []api.Message{ {Role: "user", Content: "Hello!"}, }, Stream: &stream, }) if w.Code != http.StatusServiceUnavailable { t.Errorf("expected status 503, got %d", w.Code) } if diff := cmp.Diff(w.Body.String(), `{"error":"model is overloaded"}`); diff != "" { t.Errorf("mismatch (-got +want):\n%s", diff) } }) t.Run("status error streaming", func(t *testing.T) { mock.CompletionFn = func(ctx context.Context, r llm.CompletionRequest, fn func(r llm.CompletionResponse)) error { return api.StatusError{ StatusCode: http.StatusTooManyRequests, Status: "Too Many Requests", ErrorMessage: "rate limit exceeded", } } w := createRequest(t, s.ChatHandler, api.ChatRequest{ Model: "test", Messages: []api.Message{ {Role: "user", Content: "Hello!"}, }, }) if w.Code != http.StatusTooManyRequests { t.Errorf("expected status 429, got %d", w.Code) } if diff := cmp.Diff(w.Body.String(), `{"error":"rate limit exceeded"}`); diff != "" { t.Errorf("mismatch (-got +want):\n%s", diff) } }) } func TestGenerate(t *testing.T) { gin.SetMode(gin.TestMode) mock := mockRunner{ CompletionResponse: llm.CompletionResponse{ Done: true, DoneReason: llm.DoneReasonStop, PromptEvalCount: 1, PromptEvalDuration: 1, EvalCount: 1, EvalDuration: 1, }, } s := Server{ sched: &Scheduler{ pendingReqCh: make(chan *LlmRequest, 1), finishedReqCh: make(chan *LlmRequest, 1), expiredCh: make(chan *runnerRef, 1), unloadedCh: make(chan any, 1), loaded: make(map[string]*runnerRef), newServerFn: newMockServer(&mock), getGpuFn: getGpuFn, getSystemInfoFn: getSystemInfoFn, waitForRecovery: 250 * time.Millisecond, loadFn: func(req *LlmRequest, _ *ggml.GGML, _ ml.SystemInfo, _ []ml.DeviceInfo, _ bool) bool { // add small delay to simulate loading time.Sleep(time.Millisecond) req.successCh <- &runnerRef{ llama: &mock, } return false }, }, } go s.sched.Run(t.Context()) _, digest := createBinFile(t, ggml.KV{ "general.architecture": "llama", "llama.block_count": uint32(1), "llama.context_length": uint32(8192), "llama.embedding_length": uint32(4096), "llama.attention.head_count": uint32(32), "llama.attention.head_count_kv": uint32(8), "tokenizer.ggml.tokens": []string{""}, "tokenizer.ggml.scores": []float32{0}, "tokenizer.ggml.token_type": []int32{0}, }, []*ggml.Tensor{ {Name: "token_embd.weight", Shape: []uint64{1}, WriterTo: bytes.NewReader(make([]byte, 4))}, {Name: "blk.0.attn_norm.weight", Shape: []uint64{1}, WriterTo: bytes.NewReader(make([]byte, 4))}, {Name: "blk.0.ffn_down.weight", Shape: []uint64{1}, WriterTo: bytes.NewReader(make([]byte, 4))}, {Name: "blk.0.ffn_gate.weight", Shape: []uint64{1}, WriterTo: bytes.NewReader(make([]byte, 4))}, {Name: "blk.0.ffn_up.weight", Shape: []uint64{1}, WriterTo: bytes.NewReader(make([]byte, 4))}, {Name: "blk.0.ffn_norm.weight", Shape: []uint64{1}, WriterTo: bytes.NewReader(make([]byte, 4))}, {Name: "blk.0.attn_k.weight", Shape: []uint64{1}, WriterTo: bytes.NewReader(make([]byte, 4))}, {Name: "blk.0.attn_output.weight", Shape: []uint64{1}, WriterTo: bytes.NewReader(make([]byte, 4))}, {Name: "blk.0.attn_q.weight", Shape: []uint64{1}, WriterTo: bytes.NewReader(make([]byte, 4))}, {Name: "blk.0.attn_v.weight", Shape: []uint64{1}, WriterTo: bytes.NewReader(make([]byte, 4))}, {Name: "output.weight", Shape: []uint64{1}, WriterTo: bytes.NewReader(make([]byte, 4))}, }) w := createRequest(t, s.CreateHandler, api.CreateRequest{ Model: "test", Files: map[string]string{"file.gguf": digest}, Template: ` {{- if .System }}System: {{ .System }} {{ end }} {{- if .Prompt }}User: {{ .Prompt }} {{ end }} {{- if .Response }}Assistant: {{ .Response }} {{ end }} `, Stream: &stream, }) if w.Code != http.StatusOK { t.Fatalf("expected status 200, got %d", w.Code) } t.Run("missing body", func(t *testing.T) { w := createRequest(t, s.GenerateHandler, nil) if w.Code != http.StatusNotFound { t.Errorf("expected status 404, got %d", w.Code) } if diff := cmp.Diff(w.Body.String(), `{"error":"model '' not found"}`); diff != "" { t.Errorf("mismatch (-got +want):\n%s", diff) } }) t.Run("missing model", func(t *testing.T) { w := createRequest(t, s.GenerateHandler, api.GenerateRequest{}) if w.Code != http.StatusNotFound { t.Errorf("expected status 404, got %d", w.Code) } if diff := cmp.Diff(w.Body.String(), `{"error":"model '' not found"}`); diff != "" { t.Errorf("mismatch (-got +want):\n%s", diff) } }) t.Run("missing capabilities generate", func(t *testing.T) { _, digest := createBinFile(t, ggml.KV{ "general.architecture": "bert", "bert.pooling_type": uint32(0), }, []*ggml.Tensor{}) w := createRequest(t, s.CreateHandler, api.CreateRequest{ Model: "bert", Files: map[string]string{"file.gguf": digest}, Stream: &stream, }) if w.Code != http.StatusOK { t.Fatalf("expected status 200, got %d", w.Code) } w = createRequest(t, s.GenerateHandler, api.GenerateRequest{ Model: "bert", }) if w.Code != http.StatusBadRequest { t.Errorf("expected status 400, got %d", w.Code) } if diff := cmp.Diff(w.Body.String(), `{"error":"\"bert\" does not support generate"}`); diff != "" { t.Errorf("mismatch (-got +want):\n%s", diff) } }) t.Run("missing capabilities suffix", func(t *testing.T) { w := createRequest(t, s.GenerateHandler, api.GenerateRequest{ Model: "test", Prompt: "def add(", Suffix: " return c", }) if w.Code != http.StatusBadRequest { t.Errorf("expected status 400, got %d", w.Code) } if diff := cmp.Diff(w.Body.String(), `{"error":"registry.ollama.ai/library/test:latest does not support insert"}`); diff != "" { t.Errorf("mismatch (-got +want):\n%s", diff) } }) t.Run("load model", func(t *testing.T) { w := createRequest(t, s.GenerateHandler, api.GenerateRequest{ Model: "test", }) if w.Code != http.StatusOK { t.Errorf("expected status 200, got %d", w.Code) } var actual api.GenerateResponse if err := json.NewDecoder(w.Body).Decode(&actual); err != nil { t.Fatal(err) } if actual.Model != "test" { t.Errorf("expected model test, got %s", actual.Model) } if !actual.Done { t.Errorf("expected done true, got false") } if actual.DoneReason != "load" { t.Errorf("expected done reason load, got %s", actual.DoneReason) } }) checkGenerateResponse := func(t *testing.T, body io.Reader, model, content string) { t.Helper() var actual api.GenerateResponse if err := json.NewDecoder(body).Decode(&actual); err != nil { t.Fatal(err) } if actual.Model != model { t.Errorf("expected model test, got %s", actual.Model) } if !actual.Done { t.Errorf("expected done false, got true") } if actual.DoneReason != "stop" { t.Errorf("expected done reason stop, got %s", actual.DoneReason) } if actual.Response != content { t.Errorf("expected response %s, got %s", content, actual.Response) } if actual.Context == nil { t.Errorf("expected context not nil") } if actual.PromptEvalCount == 0 { t.Errorf("expected prompt eval count > 0, got 0") } if actual.PromptEvalDuration == 0 { t.Errorf("expected prompt eval duration > 0, got 0") } if actual.EvalCount == 0 { t.Errorf("expected eval count > 0, got 0") } if actual.EvalDuration == 0 { t.Errorf("expected eval duration > 0, got 0") } if actual.LoadDuration == 0 { t.Errorf("expected load duration > 0, got 0") } if actual.TotalDuration == 0 { t.Errorf("expected total duration > 0, got 0") } } mock.CompletionResponse.Content = "Hi!" t.Run("prompt", func(t *testing.T) { w := createRequest(t, s.GenerateHandler, api.GenerateRequest{ Model: "test", Prompt: "Hello!", Stream: &stream, }) if w.Code != http.StatusOK { t.Errorf("expected status 200, got %d", w.Code) } if diff := cmp.Diff(mock.CompletionRequest.Prompt, "User: Hello! "); diff != "" { t.Errorf("mismatch (-got +want):\n%s", diff) } checkGenerateResponse(t, w.Body, "test", "Hi!") }) w = createRequest(t, s.CreateHandler, api.CreateRequest{ Model: "test-system", From: "test", System: "You are a helpful assistant.", }) if w.Code != http.StatusOK { t.Fatalf("expected status 200, got %d", w.Code) } t.Run("prompt with model system", func(t *testing.T) { w := createRequest(t, s.GenerateHandler, api.GenerateRequest{ Model: "test-system", Prompt: "Hello!", Stream: &stream, }) if w.Code != http.StatusOK { t.Errorf("expected status 200, got %d", w.Code) } if diff := cmp.Diff(mock.CompletionRequest.Prompt, "System: You are a helpful assistant. User: Hello! "); diff != "" { t.Errorf("mismatch (-got +want):\n%s", diff) } checkGenerateResponse(t, w.Body, "test-system", "Hi!") }) mock.CompletionResponse.Content = "Abra kadabra!" t.Run("prompt with system", func(t *testing.T) { w := createRequest(t, s.GenerateHandler, api.GenerateRequest{ Model: "test-system", Prompt: "Hello!", System: "You can perform magic tricks.", Stream: &stream, }) if w.Code != http.StatusOK { t.Errorf("expected status 200, got %d", w.Code) } if diff := cmp.Diff(mock.CompletionRequest.Prompt, "System: You can perform magic tricks. User: Hello! "); diff != "" { t.Errorf("mismatch (-got +want):\n%s", diff) } checkGenerateResponse(t, w.Body, "test-system", "Abra kadabra!") }) t.Run("prompt with template", func(t *testing.T) { w := createRequest(t, s.GenerateHandler, api.GenerateRequest{ Model: "test-system", Prompt: "Help me write tests.", System: "You can perform magic tricks.", Template: `{{- if .System }}{{ .System }} {{ end }} {{- if .Prompt }}### USER {{ .Prompt }} {{ end }} {{- if .Response }}### ASSISTANT {{ .Response }} {{ end }}`, Stream: &stream, }) if w.Code != http.StatusOK { t.Errorf("expected status 200, got %d", w.Code) }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
true
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/server/prompt.go
server/prompt.go
package server import ( "bytes" "context" "errors" "fmt" "log/slog" "slices" "strings" "github.com/ollama/ollama/api" "github.com/ollama/ollama/llm" "github.com/ollama/ollama/model/renderers" "github.com/ollama/ollama/template" ) type tokenizeFunc func(context.Context, string) ([]int, error) // chatPrompt accepts a list of messages and returns the prompt and images that should be used for the next chat turn. // chatPrompt truncates any messages that exceed the context window of the model, making sure to always include 1) the // latest message and 2) system messages func chatPrompt(ctx context.Context, m *Model, tokenize tokenizeFunc, opts *api.Options, msgs []api.Message, tools []api.Tool, think *api.ThinkValue, truncate bool) (prompt string, images []llm.ImageData, _ error) { var system []api.Message // TODO: Ideally we would compute this from the projector metadata but some pieces are implementation dependent // Clip images are represented as 768 tokens, each an embedding imageNumTokens := 768 n := len(msgs) - 1 // in reverse, find all messages that fit into context window for i := n; i >= 0; i-- { // always include the last message if i == n { continue } system = make([]api.Message, 0) for j := range i { if msgs[j].Role == "system" { system = append(system, msgs[j]) } } p, err := renderPrompt(m, append(system, msgs[i:]...), tools, think) if err != nil { return "", nil, err } s, err := tokenize(ctx, p) if err != nil { return "", nil, err } ctxLen := len(s) if m.ProjectorPaths != nil { for _, m := range msgs[i:] { ctxLen += imageNumTokens * len(m.Images) } } if truncate && ctxLen > opts.NumCtx { slog.Debug("truncating input messages which exceed context length", "truncated", len(msgs[i:])) break } else { n = i } } currMsgIdx := n for cnt, msg := range msgs[currMsgIdx:] { if slices.Contains(m.Config.ModelFamilies, "mllama") && len(msg.Images) > 1 { return "", nil, errors.New("this model only supports one image while more than one image requested") } var prefix string prompt := msg.Content for _, i := range msg.Images { imgData := llm.ImageData{ ID: len(images), Data: i, } imgTag := fmt.Sprintf("[img-%d]", imgData.ID) if !strings.Contains(prompt, "[img]") { prefix += imgTag } else { prompt = strings.Replace(prompt, "[img]", imgTag, 1) } images = append(images, imgData) } msgs[currMsgIdx+cnt].Content = prefix + prompt } // truncate any messages that do not fit into the context window p, err := renderPrompt(m, append(system, msgs[currMsgIdx:]...), tools, think) if err != nil { return "", nil, err } return p, images, nil } func renderPrompt(m *Model, msgs []api.Message, tools []api.Tool, think *api.ThinkValue) (string, error) { if m.Config.Renderer != "" { rendered, err := renderers.RenderWithRenderer(m.Config.Renderer, msgs, tools, think) if err != nil { return "", err } return rendered, nil } var b bytes.Buffer thinkVal := false thinkLevel := "" if think != nil { thinkVal = think.Bool() thinkLevel = think.String() } if err := m.Template.Execute(&b, template.Values{Messages: msgs, Tools: tools, Think: thinkVal, ThinkLevel: thinkLevel, IsThinkSet: think != nil}); err != nil { return "", err } return b.String(), nil }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/server/sparse_windows.go
server/sparse_windows.go
package server import ( "os" "golang.org/x/sys/windows" ) func setSparse(file *os.File) { // exFat (and other FS types) don't support sparse files, so ignore errors windows.DeviceIoControl( //nolint:errcheck windows.Handle(file.Fd()), windows.FSCTL_SET_SPARSE, nil, 0, nil, 0, nil, nil, ) }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/server/routes.go
server/routes.go
package server import ( "bytes" "cmp" "context" "encoding/base64" "encoding/json" "errors" "fmt" "image" "io" "io/fs" "log/slog" "math" "math/rand" "net" "net/http" "net/netip" "net/url" "os" "os/signal" "slices" "strings" "sync/atomic" "syscall" "time" "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" "golang.org/x/image/webp" "golang.org/x/sync/errgroup" "github.com/ollama/ollama/api" "github.com/ollama/ollama/auth" "github.com/ollama/ollama/discover" "github.com/ollama/ollama/envconfig" "github.com/ollama/ollama/format" "github.com/ollama/ollama/fs/ggml" "github.com/ollama/ollama/llm" "github.com/ollama/ollama/logutil" "github.com/ollama/ollama/middleware" "github.com/ollama/ollama/model/parsers" "github.com/ollama/ollama/model/renderers" "github.com/ollama/ollama/server/internal/client/ollama" "github.com/ollama/ollama/server/internal/registry" "github.com/ollama/ollama/template" "github.com/ollama/ollama/thinking" "github.com/ollama/ollama/tools" "github.com/ollama/ollama/types/errtypes" "github.com/ollama/ollama/types/model" "github.com/ollama/ollama/version" ) const signinURLStr = "https://ollama.com/connect?name=%s&key=%s" func shouldUseHarmony(model *Model) bool { if slices.Contains([]string{"gptoss", "gpt-oss"}, model.Config.ModelFamily) { // heuristic to check whether the template expects to be parsed via harmony: // search for harmony tags that are nearly always used if model.Template.Contains("<|start|>") && model.Template.Contains("<|end|>") { return true } } return false } func experimentEnabled(name string) bool { return slices.Contains(strings.Split(os.Getenv("OLLAMA_EXPERIMENT"), ","), name) } var useClient2 = experimentEnabled("client2") // Low VRAM mode is based on the sum of total VRAM (not free) and triggers // reduced context length on some models var lowVRAMThreshold uint64 = 20 * format.GibiByte var mode string = gin.DebugMode type Server struct { addr net.Addr sched *Scheduler lowVRAM bool } func init() { switch mode { case gin.DebugMode: case gin.ReleaseMode: case gin.TestMode: default: mode = gin.DebugMode } gin.SetMode(mode) // Tell renderers to use [img] tags renderers.RenderImgTags = true } var ( errRequired = errors.New("is required") errBadTemplate = errors.New("template error") ) func modelOptions(model *Model, requestOpts map[string]any) (api.Options, error) { opts := api.DefaultOptions() if err := opts.FromMap(model.Options); err != nil { return api.Options{}, err } if err := opts.FromMap(requestOpts); err != nil { return api.Options{}, err } return opts, nil } // scheduleRunner schedules a runner after validating inputs such as capabilities and model options. // It returns the allocated runner, model instance, and consolidated options if successful and error otherwise. func (s *Server) scheduleRunner(ctx context.Context, name string, caps []model.Capability, requestOpts map[string]any, keepAlive *api.Duration) (llm.LlamaServer, *Model, *api.Options, error) { if name == "" { return nil, nil, nil, fmt.Errorf("model %w", errRequired) } model, err := GetModel(name) if err != nil { return nil, nil, nil, err } if slices.Contains(model.Config.ModelFamilies, "mllama") && len(model.ProjectorPaths) > 0 { return nil, nil, nil, fmt.Errorf("'llama3.2-vision' is no longer compatible with your version of Ollama and has been replaced by a newer version. To re-download, run 'ollama pull llama3.2-vision'") } if err := model.CheckCapabilities(caps...); err != nil { return nil, nil, nil, fmt.Errorf("%s %w", name, err) } opts, err := modelOptions(model, requestOpts) if err != nil { return nil, nil, nil, err } // This model is much more capable with a larger context, so set that // unless it would penalize performance too much if !s.lowVRAM && slices.Contains([]string{ "gptoss", "gpt-oss", "qwen3vl", "qwen3vlmoe", }, model.Config.ModelFamily) { opts.NumCtx = max(opts.NumCtx, 8192) } runnerCh, errCh := s.sched.GetRunner(ctx, model, opts, keepAlive) var runner *runnerRef select { case runner = <-runnerCh: case err = <-errCh: return nil, nil, nil, err } return runner.llama, model, &opts, nil } func signinURL() (string, error) { pubKey, err := auth.GetPublicKey() if err != nil { return "", err } encKey := base64.RawURLEncoding.EncodeToString([]byte(pubKey)) h, _ := os.Hostname() return fmt.Sprintf(signinURLStr, url.PathEscape(h), encKey), nil } func (s *Server) GenerateHandler(c *gin.Context) { checkpointStart := time.Now() var req api.GenerateRequest if err := c.ShouldBindJSON(&req); errors.Is(err, io.EOF) { c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "missing request body"}) return } else if err != nil { c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } if req.TopLogprobs < 0 || req.TopLogprobs > 20 { c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "top_logprobs must be between 0 and 20"}) return } name := model.ParseName(req.Model) if !name.IsValid() { // Ideally this is "invalid model name" but we're keeping with // what the API currently returns until we can change it. c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("model '%s' not found", req.Model)}) return } // We cannot currently consolidate this into GetModel because all we'll // induce infinite recursion given the current code structure. name, err := getExistingName(name) if err != nil { c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("model '%s' not found", req.Model)}) return } m, err := GetModel(name.String()) if err != nil { switch { case errors.Is(err, fs.ErrNotExist): c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("model '%s' not found", req.Model)}) case err.Error() == errtypes.InvalidModelNameErrMsg: c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) default: c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) } return } if req.TopLogprobs < 0 || req.TopLogprobs > 20 { c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "top_logprobs must be between 0 and 20"}) return } if m.Config.RemoteHost != "" && m.Config.RemoteModel != "" { origModel := req.Model remoteURL, err := url.Parse(m.Config.RemoteHost) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } if !slices.Contains(envconfig.Remotes(), remoteURL.Hostname()) { slog.Info("remote model", "remotes", envconfig.Remotes(), "remoteURL", m.Config.RemoteHost, "hostname", remoteURL.Hostname()) c.JSON(http.StatusBadRequest, gin.H{"error": "this server cannot run this remote model"}) return } req.Model = m.Config.RemoteModel if req.Template == "" && m.Template.String() != "" { req.Template = m.Template.String() } if req.Options == nil { req.Options = map[string]any{} } for k, v := range m.Options { if _, ok := req.Options[k]; !ok { req.Options[k] = v } } // update the system prompt from the model if one isn't already specified if req.System == "" && m.System != "" { req.System = m.System } if len(m.Messages) > 0 { slog.Warn("embedded messages in the model not supported with '/api/generate'; try '/api/chat' instead") } contentType := "application/x-ndjson" if req.Stream != nil && !*req.Stream { contentType = "application/json; charset=utf-8" } c.Header("Content-Type", contentType) fn := func(resp api.GenerateResponse) error { resp.Model = origModel resp.RemoteModel = m.Config.RemoteModel resp.RemoteHost = m.Config.RemoteHost data, err := json.Marshal(resp) if err != nil { return err } if _, err = c.Writer.Write(append(data, '\n')); err != nil { return err } c.Writer.Flush() return nil } client := api.NewClient(remoteURL, http.DefaultClient) err = client.Generate(c, &req, fn) if err != nil { var authError api.AuthorizationError if errors.As(err, &authError) { sURL, sErr := signinURL() if sErr != nil { slog.Error(sErr.Error()) c.JSON(http.StatusInternalServerError, gin.H{"error": "error getting authorization details"}) return } c.JSON(authError.StatusCode, gin.H{"error": "unauthorized", "signin_url": sURL}) return } var apiError api.StatusError if errors.As(err, &apiError) { c.JSON(apiError.StatusCode, apiError) return } c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } return } // expire the runner if req.Prompt == "" && req.KeepAlive != nil && req.KeepAlive.Duration == 0 { s.sched.expireRunner(m) c.JSON(http.StatusOK, api.GenerateResponse{ Model: req.Model, CreatedAt: time.Now().UTC(), Response: "", Done: true, DoneReason: "unload", }) return } if req.Raw && (req.Template != "" || req.System != "" || len(req.Context) > 0) { c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "raw mode does not support template, system, or context"}) return } var builtinParser parsers.Parser if shouldUseHarmony(m) && m.Config.Parser == "" { m.Config.Parser = "harmony" } if !req.Raw && m.Config.Parser != "" { builtinParser = parsers.ParserForName(m.Config.Parser) if builtinParser != nil { // no tools or last message for generate endpoint builtinParser.Init(nil, nil, req.Think) } } // Validate Think value: string values currently only allowed for harmony/gptoss models if req.Think != nil && req.Think.IsString() && m.Config.Parser != "harmony" { c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("think value %q is not supported for this model", req.Think.String())}) return } caps := []model.Capability{model.CapabilityCompletion} if req.Suffix != "" { caps = append(caps, model.CapabilityInsert) } modelCaps := m.Capabilities() if slices.Contains(modelCaps, model.CapabilityThinking) { caps = append(caps, model.CapabilityThinking) if req.Think == nil { req.Think = &api.ThinkValue{Value: true} } } else { if req.Think != nil && req.Think.Bool() { c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("%q does not support thinking", req.Model)}) return } } r, m, opts, err := s.scheduleRunner(c.Request.Context(), name.String(), caps, req.Options, req.KeepAlive) if errors.Is(err, errCapabilityCompletion) { c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("%q does not support generate", req.Model)}) return } else if err != nil { handleScheduleError(c, req.Model, err) return } checkpointLoaded := time.Now() // load the model if req.Prompt == "" { c.JSON(http.StatusOK, api.GenerateResponse{ Model: req.Model, CreatedAt: time.Now().UTC(), Done: true, DoneReason: "load", }) return } if slices.Contains(m.Config.ModelFamilies, "mllama") && len(req.Images) > 1 { c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "this model only supports one image while more than one image requested"}) return } images := make([]llm.ImageData, len(req.Images)) for i := range req.Images { images[i] = llm.ImageData{ID: i, Data: req.Images[i]} } prompt := req.Prompt if !req.Raw { tmpl := m.Template if req.Template != "" { tmpl, err = template.Parse(req.Template) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } } var values template.Values if req.Suffix != "" { values.Prompt = prompt values.Suffix = req.Suffix } else { var msgs []api.Message if req.System != "" { msgs = append(msgs, api.Message{Role: "system", Content: req.System}) } else if m.System != "" { msgs = append(msgs, api.Message{Role: "system", Content: m.System}) } if req.Context == nil { msgs = append(msgs, m.Messages...) } userMsg := api.Message{Role: "user", Content: req.Prompt} for _, i := range images { userMsg.Images = append(userMsg.Images, i.Data) } values.Messages = append(msgs, userMsg) } values.Think = req.Think != nil && req.Think.Bool() values.ThinkLevel = "" if req.Think != nil { values.ThinkLevel = req.Think.String() } values.IsThinkSet = req.Think != nil var b bytes.Buffer if req.Context != nil { slog.Warn("the context field is deprecated and will be removed in a future version of Ollama") s, err := r.Detokenize(c.Request.Context(), req.Context) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } b.WriteString(s) } // check that we're in the `api/chat`-like flow, and if so, generate the // prompt the same way // TEMP(drifkin): we should really just detect the chat-like flow and call // the real chat handler, but doing this as a stopgap to get renderer // support for generate if values.Messages != nil && values.Suffix == "" && req.Template == "" { prompt, images, err = chatPrompt(c.Request.Context(), m, r.Tokenize, opts, values.Messages, []api.Tool{}, req.Think, req.Truncate == nil || *req.Truncate) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } // TEMP(drifkin): req.Context will be removed very soon, but we're temporarily supporting it in this flow here if req.Context != nil { b.WriteString(prompt) prompt = b.String() } } else { // legacy flow if err := tmpl.Execute(&b, values); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } prompt = b.String() } } // If debug mode is enabled, return the rendered template instead of calling the model if req.DebugRenderOnly { c.JSON(http.StatusOK, api.GenerateResponse{ Model: req.Model, CreatedAt: time.Now().UTC(), DebugInfo: &api.DebugInfo{ RenderedTemplate: prompt, ImageCount: len(images), }, }) return } var thinkingState *thinking.Parser if builtinParser == nil { openingTag, closingTag := thinking.InferTags(m.Template.Template) if req.Think != nil && req.Think.Bool() && openingTag != "" && closingTag != "" { thinkingState = &thinking.Parser{ OpeningTag: openingTag, ClosingTag: closingTag, } if strings.HasSuffix(strings.TrimSpace(prompt), openingTag) { thinkingState.AddContent(openingTag) } } } ch := make(chan any) go func() { // TODO (jmorganca): avoid building the response twice both here and below var sb strings.Builder defer close(ch) if err := r.Completion(c.Request.Context(), llm.CompletionRequest{ Prompt: prompt, Images: images, Format: req.Format, Options: opts, Shift: req.Shift == nil || *req.Shift, Truncate: req.Truncate == nil || *req.Truncate, Logprobs: req.Logprobs, TopLogprobs: req.TopLogprobs, }, func(cr llm.CompletionResponse) { res := api.GenerateResponse{ Model: req.Model, CreatedAt: time.Now().UTC(), Response: cr.Content, Done: cr.Done, Metrics: api.Metrics{ PromptEvalCount: cr.PromptEvalCount, PromptEvalDuration: cr.PromptEvalDuration, EvalCount: cr.EvalCount, EvalDuration: cr.EvalDuration, }, Logprobs: toAPILogprobs(cr.Logprobs), } if builtinParser != nil { content, thinking, toolCalls, err := builtinParser.Add(cr.Content, cr.Done) if err != nil { ch <- gin.H{"error": err.Error()} return } res.Response = content res.Thinking = thinking if cr.Done && len(toolCalls) > 0 { res.ToolCalls = toolCalls } } else if thinkingState != nil { thinking, content := thinkingState.AddContent(cr.Content) res.Thinking = thinking res.Response = content } if _, err := sb.WriteString(cr.Content); err != nil { ch <- gin.H{"error": err.Error()} } if cr.Done { res.DoneReason = cr.DoneReason.String() res.TotalDuration = time.Since(checkpointStart) res.LoadDuration = checkpointLoaded.Sub(checkpointStart) if !req.Raw { tokens, err := r.Tokenize(c.Request.Context(), prompt+sb.String()) if err != nil { ch <- gin.H{"error": err.Error()} return } res.Context = tokens } } if builtinParser != nil { // only send messages with meaningful content (empty messages confuse clients) if res.Response != "" || res.Thinking != "" || res.Done || len(res.ToolCalls) > 0 { ch <- res } return } ch <- res }); err != nil { var serr api.StatusError if errors.As(err, &serr) { ch <- gin.H{"error": serr.ErrorMessage, "status": serr.StatusCode} } else { ch <- gin.H{"error": err.Error()} } } }() if req.Stream != nil && !*req.Stream { var r api.GenerateResponse var allLogprobs []api.Logprob var sbThinking strings.Builder var sbContent strings.Builder for rr := range ch { switch t := rr.(type) { case api.GenerateResponse: sbThinking.WriteString(t.Thinking) sbContent.WriteString(t.Response) r = t // Accumulate logprobs from all chunks for non-streaming response if len(t.Logprobs) > 0 { allLogprobs = append(allLogprobs, t.Logprobs...) } case gin.H: msg, ok := t["error"].(string) if !ok { msg = "unexpected error format in response" } status, ok := t["status"].(int) if !ok { status = http.StatusInternalServerError } c.JSON(status, gin.H{"error": msg}) return default: c.JSON(http.StatusInternalServerError, gin.H{"error": "unexpected response"}) return } } r.Thinking = sbThinking.String() r.Response = sbContent.String() r.Logprobs = allLogprobs c.JSON(http.StatusOK, r) return } streamResponse(c, ch) } func (s *Server) EmbedHandler(c *gin.Context) { checkpointStart := time.Now() var req api.EmbedRequest err := c.ShouldBindJSON(&req) switch { case errors.Is(err, io.EOF): c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "missing request body"}) return case err != nil: c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } var input []string switch i := req.Input.(type) { case string: if len(i) > 0 { input = append(input, i) } case []any: for _, v := range i { if _, ok := v.(string); !ok { c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "invalid input type"}) return } input = append(input, v.(string)) } default: if req.Input != nil { c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "invalid input type"}) return } } name, err := getExistingName(model.ParseName(req.Model)) if err != nil { c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("model '%s' not found", req.Model)}) return } r, m, opts, err := s.scheduleRunner(c.Request.Context(), name.String(), []model.Capability{}, req.Options, req.KeepAlive) if err != nil { handleScheduleError(c, req.Model, err) return } checkpointLoaded := time.Now() if len(input) == 0 { c.JSON(http.StatusOK, api.EmbedResponse{Model: req.Model, Embeddings: [][]float32{}}) return } kvData, _, err := getModelData(m.ModelPath, false) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } ctx := c.Request.Context() embedWithRetry := func(text string) ([]float32, int, error) { emb, tokCount, err := r.Embedding(ctx, text) if err == nil { return emb, tokCount, nil } var serr api.StatusError if !errors.As(err, &serr) || serr.StatusCode != http.StatusBadRequest { return nil, 0, err } if req.Truncate != nil && !*req.Truncate { return nil, 0, err } tokens, err := r.Tokenize(ctx, text) if err != nil { return nil, 0, err } // TODO @nicolepardal: avoid reaching into kvData here; pass required tokenizer metadata via model/options instead ctxLen := min(opts.NumCtx, int(kvData.ContextLength())) if bos := kvData.Uint("tokenizer.ggml.bos_token_id"); len(tokens) > 0 && tokens[0] != int(bos) && kvData.Bool("add_bos_token", true) { ctxLen-- } if eos := kvData.Uint("tokenizer.ggml.eos_token_id"); len(tokens) > 0 && tokens[len(tokens)-1] != int(eos) && kvData.Bool("add_eos_token", true) { ctxLen-- } if len(tokens) <= ctxLen { return nil, 0, fmt.Errorf("input exceeds maximum context length and cannot be truncated further") } if ctxLen <= 0 { return nil, 0, fmt.Errorf("input after truncation exceeds maximum context length") } truncatedTokens := tokens[:ctxLen] truncated, err := r.Detokenize(ctx, truncatedTokens) if err != nil { return nil, 0, err } return r.Embedding(ctx, truncated) } var g errgroup.Group embeddings := make([][]float32, len(input)) var totalTokens uint64 for i, text := range input { g.Go(func() error { embedding, tokenCount, err := embedWithRetry(text) if err != nil { return err } // TODO: this first normalization should be done by the model embedding, err = normalize(embedding) if err != nil { return err } if req.Dimensions > 0 && req.Dimensions < len(embedding) { embedding, err = normalize(embedding[:req.Dimensions]) if err != nil { return err } } embeddings[i] = embedding atomic.AddUint64(&totalTokens, uint64(tokenCount)) return nil }) } if err := g.Wait(); err != nil { var serr api.StatusError if errors.As(err, &serr) { c.AbortWithStatusJSON(serr.StatusCode, gin.H{ "error": strings.TrimSpace(serr.ErrorMessage), }) return } c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{ "error": strings.TrimSpace(err.Error()), }) return } resp := api.EmbedResponse{ Model: req.Model, Embeddings: embeddings, TotalDuration: time.Since(checkpointStart), LoadDuration: checkpointLoaded.Sub(checkpointStart), PromptEvalCount: int(totalTokens), } c.JSON(http.StatusOK, resp) } func normalize(vec []float32) ([]float32, error) { var sum float32 for _, v := range vec { if math.IsNaN(float64(v)) || math.IsInf(float64(v), 0) { return nil, errors.New("embedding contains NaN or Inf values") } sum += v * v } norm := float32(1.0 / max(math.Sqrt(float64(sum)), 1e-12)) for i := range vec { vec[i] *= norm } return vec, nil } func (s *Server) EmbeddingsHandler(c *gin.Context) { var req api.EmbeddingRequest if err := c.ShouldBindJSON(&req); errors.Is(err, io.EOF) { c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "missing request body"}) return } else if err != nil { c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } name := model.ParseName(req.Model) if !name.IsValid() { c.JSON(http.StatusBadRequest, gin.H{"error": "model is required"}) return } r, _, _, err := s.scheduleRunner(c.Request.Context(), name.String(), []model.Capability{}, req.Options, req.KeepAlive) if err != nil { handleScheduleError(c, req.Model, err) return } // an empty request loads the model if req.Prompt == "" { c.JSON(http.StatusOK, api.EmbeddingResponse{Embedding: []float64{}}) return } embedding, _, err := r.Embedding(c.Request.Context(), req.Prompt) if err != nil { c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": strings.TrimSpace(err.Error())}) return } var e []float64 for _, v := range embedding { e = append(e, float64(v)) } resp := api.EmbeddingResponse{ Embedding: e, } c.JSON(http.StatusOK, resp) } func (s *Server) PullHandler(c *gin.Context) { var req api.PullRequest err := c.ShouldBindJSON(&req) switch { case errors.Is(err, io.EOF): c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "missing request body"}) return case err != nil: c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } name := model.ParseName(cmp.Or(req.Model, req.Name)) if !name.IsValid() { c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": errtypes.InvalidModelNameErrMsg}) return } name, err = getExistingName(name) if err != nil { c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } ch := make(chan any) go func() { defer close(ch) fn := func(r api.ProgressResponse) { ch <- r } regOpts := &registryOptions{ Insecure: req.Insecure, } ctx, cancel := context.WithCancel(c.Request.Context()) defer cancel() if err := PullModel(ctx, name.DisplayShortest(), regOpts, fn); err != nil { ch <- gin.H{"error": err.Error()} } }() if req.Stream != nil && !*req.Stream { waitForStream(c, ch) return } streamResponse(c, ch) } func (s *Server) PushHandler(c *gin.Context) { var req api.PushRequest err := c.ShouldBindJSON(&req) switch { case errors.Is(err, io.EOF): c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "missing request body"}) return case err != nil: c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } var mname string if req.Model != "" { mname = req.Model } else if req.Name != "" { mname = req.Name } else { c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "model is required"}) return } ch := make(chan any) go func() { defer close(ch) fn := func(r api.ProgressResponse) { ch <- r } regOpts := &registryOptions{ Insecure: req.Insecure, } ctx, cancel := context.WithCancel(c.Request.Context()) defer cancel() name, err := getExistingName(model.ParseName(mname)) if err != nil { ch <- gin.H{"error": err.Error()} return } if err := PushModel(ctx, name.DisplayShortest(), regOpts, fn); err != nil { ch <- gin.H{"error": err.Error()} } }() if req.Stream != nil && !*req.Stream { waitForStream(c, ch) return } streamResponse(c, ch) } // getExistingName searches the models directory for the longest prefix match of // the input name and returns the input name with all existing parts replaced // with each part found. If no parts are found, the input name is returned as // is. func getExistingName(n model.Name) (model.Name, error) { var zero model.Name existing, err := Manifests(true) if err != nil { return zero, err } var set model.Name // tracks parts already canonicalized for e := range existing { if set.Host == "" && strings.EqualFold(e.Host, n.Host) { n.Host = e.Host } if set.Namespace == "" && strings.EqualFold(e.Namespace, n.Namespace) { n.Namespace = e.Namespace } if set.Model == "" && strings.EqualFold(e.Model, n.Model) { n.Model = e.Model } if set.Tag == "" && strings.EqualFold(e.Tag, n.Tag) { n.Tag = e.Tag } } return n, nil } func (s *Server) DeleteHandler(c *gin.Context) { var r api.DeleteRequest if err := c.ShouldBindJSON(&r); errors.Is(err, io.EOF) { c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "missing request body"}) return } else if err != nil { c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } n := model.ParseName(cmp.Or(r.Model, r.Name)) if !n.IsValid() { c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("name %q is invalid", cmp.Or(r.Model, r.Name))}) return } n, err := getExistingName(n) if err != nil { c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("model '%s' not found", cmp.Or(r.Model, r.Name))}) return } m, err := ParseNamedManifest(n) if err != nil { switch { case os.IsNotExist(err): c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("model '%s' not found", cmp.Or(r.Model, r.Name))}) default: c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) } return } if err := m.Remove(); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } if err := m.RemoveLayers(); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } } func (s *Server) ShowHandler(c *gin.Context) { var req api.ShowRequest err := c.ShouldBindJSON(&req) switch { case errors.Is(err, io.EOF): c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "missing request body"}) return case err != nil: c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } if req.Model != "" { // noop } else if req.Name != "" { req.Model = req.Name } else { c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "model is required"}) return } resp, err := GetModelInfo(req) if err != nil { switch { case os.IsNotExist(err): c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("model '%s' not found", req.Model)}) case err.Error() == errtypes.InvalidModelNameErrMsg: c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) default: c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) } return } c.JSON(http.StatusOK, resp) } func GetModelInfo(req api.ShowRequest) (*api.ShowResponse, error) { name := model.ParseName(req.Model) if !name.IsValid() { return nil, ErrModelPathInvalid } name, err := getExistingName(name) if err != nil { return nil, err } m, err := GetModel(name.String()) if err != nil { return nil, err } modelDetails := api.ModelDetails{ ParentModel: m.ParentModel, Format: m.Config.ModelFormat, Family: m.Config.ModelFamily, Families: m.Config.ModelFamilies, ParameterSize: m.Config.ModelType, QuantizationLevel: m.Config.FileType, } if req.System != "" { m.System = req.System } msgs := make([]api.Message, len(m.Messages)) for i, msg := range m.Messages { msgs[i] = api.Message{Role: msg.Role, Content: msg.Content} } manifest, err := ParseNamedManifest(name) if err != nil { return nil, err } resp := &api.ShowResponse{ License: strings.Join(m.License, "\n"), System: m.System, Template: m.Template.String(), Details: modelDetails, Messages: msgs, Capabilities: m.Capabilities(), ModifiedAt: manifest.fi.ModTime(), Requires: m.Config.Requires, } if m.Config.RemoteHost != "" { resp.RemoteHost = m.Config.RemoteHost resp.RemoteModel = m.Config.RemoteModel if m.Config.ModelFamily != "" { resp.ModelInfo = make(map[string]any) resp.ModelInfo["general.architecture"] = m.Config.ModelFamily if m.Config.BaseName != "" { resp.ModelInfo["general.basename"] = m.Config.BaseName } if m.Config.ContextLen > 0 { resp.ModelInfo[fmt.Sprintf("%s.context_length", m.Config.ModelFamily)] = m.Config.ContextLen } if m.Config.EmbedLen > 0 { resp.ModelInfo[fmt.Sprintf("%s.embedding_length", m.Config.ModelFamily)] = m.Config.EmbedLen } } } var params []string cs := 30 for k, v := range m.Options { switch val := v.(type) { case []any: for _, nv := range val { params = append(params, fmt.Sprintf("%-*s %#v", cs, k, nv)) } default: params = append(params, fmt.Sprintf("%-*s %#v", cs, k, v)) } } resp.Parameters = strings.Join(params, "\n") if len(req.Options) > 0 { if m.Options == nil { m.Options = make(map[string]any) } for k, v := range req.Options { m.Options[k] = v } } var sb strings.Builder fmt.Fprintln(&sb, "# Modelfile generated by \"ollama show\"") fmt.Fprintln(&sb, "# To build a new Modelfile based on this, replace FROM with:") fmt.Fprintf(&sb, "# FROM %s\n\n", m.ShortName) fmt.Fprint(&sb, m.String()) resp.Modelfile = sb.String() // skip loading tensor information if this is a remote model if m.Config.RemoteHost != "" && m.Config.RemoteModel != "" { return resp, nil } kvData, tensors, err := getModelData(m.ModelPath, req.Verbose) if err != nil { return nil, err } delete(kvData, "general.name") delete(kvData, "tokenizer.chat_template") resp.ModelInfo = kvData tensorData := make([]api.Tensor, len(tensors.Items())) for cnt, t := range tensors.Items() { tensorData[cnt] = api.Tensor{Name: t.Name, Type: t.Type(), Shape: t.Shape} } resp.Tensors = tensorData if len(m.ProjectorPaths) > 0 { projectorData, _, err := getModelData(m.ProjectorPaths[0], req.Verbose) if err != nil { return nil, err } resp.ProjectorInfo = projectorData } return resp, nil } func getModelData(digest string, verbose bool) (ggml.KV, ggml.Tensors, error) { maxArraySize := 0 if verbose { maxArraySize = -1 } data, err := llm.LoadModel(digest, maxArraySize) if err != nil { return nil, ggml.Tensors{}, err } kv := data.KV() if !verbose { for k := range kv { if t, ok := kv[k].([]any); len(t) > 5 && ok { kv[k] = []any{} } } } return kv, data.Tensors(), nil } func (s *Server) ListHandler(c *gin.Context) { ms, err := Manifests(true) if err != nil {
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
true
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/server/download.go
server/download.go
package server import ( "context" "encoding/json" "errors" "fmt" "io" "log/slog" "math" "math/rand/v2" "net/http" "net/url" "os" "path/filepath" "strconv" "strings" "sync" "sync/atomic" "syscall" "time" "golang.org/x/sync/errgroup" "github.com/ollama/ollama/api" "github.com/ollama/ollama/format" ) const maxRetries = 6 var ( errMaxRetriesExceeded = errors.New("max retries exceeded") errPartStalled = errors.New("part stalled") errMaxRedirectsExceeded = errors.New("maximum redirects exceeded (10) for directURL") ) var blobDownloadManager sync.Map type blobDownload struct { Name string Digest string Total int64 Completed atomic.Int64 Parts []*blobDownloadPart context.CancelFunc done chan struct{} err error references atomic.Int32 } type blobDownloadPart struct { N int Offset int64 Size int64 Completed atomic.Int64 lastUpdatedMu sync.Mutex lastUpdated time.Time *blobDownload `json:"-"` } type jsonBlobDownloadPart struct { N int Offset int64 Size int64 Completed int64 } func (p *blobDownloadPart) MarshalJSON() ([]byte, error) { return json.Marshal(jsonBlobDownloadPart{ N: p.N, Offset: p.Offset, Size: p.Size, Completed: p.Completed.Load(), }) } func (p *blobDownloadPart) UnmarshalJSON(b []byte) error { var j jsonBlobDownloadPart if err := json.Unmarshal(b, &j); err != nil { return err } *p = blobDownloadPart{ N: j.N, Offset: j.Offset, Size: j.Size, } p.Completed.Store(j.Completed) return nil } const ( numDownloadParts = 16 minDownloadPartSize int64 = 100 * format.MegaByte maxDownloadPartSize int64 = 1000 * format.MegaByte ) func (p *blobDownloadPart) Name() string { return strings.Join([]string{ p.blobDownload.Name, "partial", strconv.Itoa(p.N), }, "-") } func (p *blobDownloadPart) StartsAt() int64 { return p.Offset + p.Completed.Load() } func (p *blobDownloadPart) StopsAt() int64 { return p.Offset + p.Size } func (p *blobDownloadPart) Write(b []byte) (n int, err error) { n = len(b) p.blobDownload.Completed.Add(int64(n)) p.lastUpdatedMu.Lock() p.lastUpdated = time.Now() p.lastUpdatedMu.Unlock() return n, nil } func (b *blobDownload) Prepare(ctx context.Context, requestURL *url.URL, opts *registryOptions) error { partFilePaths, err := filepath.Glob(b.Name + "-partial-*") if err != nil { return err } b.done = make(chan struct{}) for _, partFilePath := range partFilePaths { part, err := b.readPart(partFilePath) if err != nil { return err } b.Total += part.Size b.Completed.Add(part.Completed.Load()) b.Parts = append(b.Parts, part) } if len(b.Parts) == 0 { resp, err := makeRequestWithRetry(ctx, http.MethodHead, requestURL, nil, nil, opts) if err != nil { return err } defer resp.Body.Close() b.Total, _ = strconv.ParseInt(resp.Header.Get("Content-Length"), 10, 64) size := b.Total / numDownloadParts switch { case size < minDownloadPartSize: size = minDownloadPartSize case size > maxDownloadPartSize: size = maxDownloadPartSize } var offset int64 for offset < b.Total { if offset+size > b.Total { size = b.Total - offset } if err := b.newPart(offset, size); err != nil { return err } offset += size } } if len(b.Parts) > 0 { slog.Info(fmt.Sprintf("downloading %s in %d %s part(s)", b.Digest[7:19], len(b.Parts), format.HumanBytes(b.Parts[0].Size))) } return nil } func (b *blobDownload) Run(ctx context.Context, requestURL *url.URL, opts *registryOptions) { defer close(b.done) b.err = b.run(ctx, requestURL, opts) } func newBackoff(maxBackoff time.Duration) func(ctx context.Context) error { var n int return func(ctx context.Context) error { if ctx.Err() != nil { return ctx.Err() } n++ // n^2 backoff timer is a little smoother than the // common choice of 2^n. d := min(time.Duration(n*n)*10*time.Millisecond, maxBackoff) // Randomize the delay between 0.5-1.5 x msec, in order // to prevent accidental "thundering herd" problems. d = time.Duration(float64(d) * (rand.Float64() + 0.5)) t := time.NewTimer(d) defer t.Stop() select { case <-ctx.Done(): return ctx.Err() case <-t.C: return nil } } } func (b *blobDownload) run(ctx context.Context, requestURL *url.URL, opts *registryOptions) error { defer blobDownloadManager.Delete(b.Digest) ctx, b.CancelFunc = context.WithCancel(ctx) file, err := os.OpenFile(b.Name+"-partial", os.O_CREATE|os.O_RDWR, 0o644) if err != nil { return err } defer file.Close() setSparse(file) _ = file.Truncate(b.Total) directURL, err := func() (*url.URL, error) { ctx, cancel := context.WithTimeout(ctx, 30*time.Second) defer cancel() backoff := newBackoff(10 * time.Second) for { // shallow clone opts to be used in the closure // without affecting the outer opts. newOpts := new(registryOptions) *newOpts = *opts newOpts.CheckRedirect = func(req *http.Request, via []*http.Request) error { if len(via) > 10 { return errMaxRedirectsExceeded } // if the hostname is the same, allow the redirect if req.URL.Hostname() == requestURL.Hostname() { return nil } // stop at the first redirect that is not // the same hostname as the original // request. return http.ErrUseLastResponse } resp, err := makeRequestWithRetry(ctx, http.MethodGet, requestURL, nil, nil, newOpts) if err != nil { slog.Warn("failed to get direct URL; backing off and retrying", "err", err) if err := backoff(ctx); err != nil { return nil, err } continue } defer resp.Body.Close() if resp.StatusCode != http.StatusTemporaryRedirect && resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected status code %d", resp.StatusCode) } return resp.Location() } }() if err != nil { return err } g, inner := errgroup.WithContext(ctx) g.SetLimit(numDownloadParts) for i := range b.Parts { part := b.Parts[i] if part.Completed.Load() == part.Size { continue } g.Go(func() error { var err error for try := 0; try < maxRetries; try++ { w := io.NewOffsetWriter(file, part.StartsAt()) err = b.downloadChunk(inner, directURL, w, part) switch { case errors.Is(err, context.Canceled), errors.Is(err, syscall.ENOSPC): // return immediately if the context is canceled or the device is out of space return err case errors.Is(err, errPartStalled): try-- continue case err != nil: sleep := time.Second * time.Duration(math.Pow(2, float64(try))) slog.Info(fmt.Sprintf("%s part %d attempt %d failed: %v, retrying in %s", b.Digest[7:19], part.N, try, err, sleep)) time.Sleep(sleep) continue default: return nil } } return fmt.Errorf("%w: %w", errMaxRetriesExceeded, err) }) } if err := g.Wait(); err != nil { return err } // explicitly close the file so we can rename it if err := file.Close(); err != nil { return err } for i := range b.Parts { if err := os.Remove(file.Name() + "-" + strconv.Itoa(i)); err != nil { return err } } if err := os.Rename(file.Name(), b.Name); err != nil { return err } return nil } func (b *blobDownload) downloadChunk(ctx context.Context, requestURL *url.URL, w io.Writer, part *blobDownloadPart) error { g, ctx := errgroup.WithContext(ctx) g.Go(func() error { req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestURL.String(), nil) if err != nil { return err } req.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", part.StartsAt(), part.StopsAt()-1)) resp, err := http.DefaultClient.Do(req) if err != nil { return err } defer resp.Body.Close() n, err := io.CopyN(w, io.TeeReader(resp.Body, part), part.Size-part.Completed.Load()) if err != nil && !errors.Is(err, context.Canceled) && !errors.Is(err, io.ErrUnexpectedEOF) { // rollback progress b.Completed.Add(-n) return err } part.Completed.Add(n) if err := b.writePart(part.Name(), part); err != nil { return err } // return nil or context.Canceled or UnexpectedEOF (resumable) return err }) g.Go(func() error { ticker := time.NewTicker(time.Second) for { select { case <-ticker.C: if part.Completed.Load() >= part.Size { return nil } part.lastUpdatedMu.Lock() lastUpdated := part.lastUpdated part.lastUpdatedMu.Unlock() if !lastUpdated.IsZero() && time.Since(lastUpdated) > 30*time.Second { const msg = "%s part %d stalled; retrying. If this persists, press ctrl-c to exit, then 'ollama pull' to find a faster connection." slog.Info(fmt.Sprintf(msg, b.Digest[7:19], part.N)) // reset last updated part.lastUpdatedMu.Lock() part.lastUpdated = time.Time{} part.lastUpdatedMu.Unlock() return errPartStalled } case <-ctx.Done(): return ctx.Err() } } }) return g.Wait() } func (b *blobDownload) newPart(offset, size int64) error { part := blobDownloadPart{blobDownload: b, Offset: offset, Size: size, N: len(b.Parts)} if err := b.writePart(part.Name(), &part); err != nil { return err } b.Parts = append(b.Parts, &part) return nil } func (b *blobDownload) readPart(partName string) (*blobDownloadPart, error) { var part blobDownloadPart partFile, err := os.Open(partName) if err != nil { return nil, err } defer partFile.Close() if err := json.NewDecoder(partFile).Decode(&part); err != nil { return nil, err } part.blobDownload = b return &part, nil } func (b *blobDownload) writePart(partName string, part *blobDownloadPart) error { partFile, err := os.OpenFile(partName, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0o644) if err != nil { return err } defer partFile.Close() return json.NewEncoder(partFile).Encode(part) } func (b *blobDownload) acquire() { b.references.Add(1) } func (b *blobDownload) release() { if b.references.Add(-1) == 0 { b.CancelFunc() } } func (b *blobDownload) Wait(ctx context.Context, fn func(api.ProgressResponse)) error { b.acquire() defer b.release() ticker := time.NewTicker(60 * time.Millisecond) for { select { case <-b.done: return b.err case <-ticker.C: fn(api.ProgressResponse{ Status: fmt.Sprintf("pulling %s", b.Digest[7:19]), Digest: b.Digest, Total: b.Total, Completed: b.Completed.Load(), }) case <-ctx.Done(): return ctx.Err() } } } type downloadOpts struct { mp ModelPath digest string regOpts *registryOptions fn func(api.ProgressResponse) } // downloadBlob downloads a blob from the registry and stores it in the blobs directory func downloadBlob(ctx context.Context, opts downloadOpts) (cacheHit bool, _ error) { if opts.digest == "" { return false, fmt.Errorf(("%s: %s"), opts.mp.GetNamespaceRepository(), "digest is empty") } fp, err := GetBlobsPath(opts.digest) if err != nil { return false, err } fi, err := os.Stat(fp) switch { case errors.Is(err, os.ErrNotExist): case err != nil: return false, err default: opts.fn(api.ProgressResponse{ Status: fmt.Sprintf("pulling %s", opts.digest[7:19]), Digest: opts.digest, Total: fi.Size(), Completed: fi.Size(), }) return true, nil } data, ok := blobDownloadManager.LoadOrStore(opts.digest, &blobDownload{Name: fp, Digest: opts.digest}) download := data.(*blobDownload) if !ok { requestURL := opts.mp.BaseURL() requestURL = requestURL.JoinPath("v2", opts.mp.GetNamespaceRepository(), "blobs", opts.digest) if err := download.Prepare(ctx, requestURL, opts.regOpts); err != nil { blobDownloadManager.Delete(opts.digest) return false, err } //nolint:contextcheck go download.Run(context.Background(), requestURL, opts.regOpts) } return false, download.Wait(ctx, opts.fn) }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/server/routes_list_test.go
server/routes_list_test.go
package server import ( "encoding/json" "net/http" "slices" "testing" "github.com/gin-gonic/gin" "github.com/ollama/ollama/api" ) func TestList(t *testing.T) { gin.SetMode(gin.TestMode) t.Setenv("OLLAMA_MODELS", t.TempDir()) expectNames := []string{ "mistral:7b-instruct-q4_0", "zephyr:7b-beta-q5_K_M", "apple/OpenELM:latest", "boreas:2b-code-v1.5-q6_K", "notus:7b-v1-IQ2_S", // TODO: host:port currently fails on windows (#4107) // "localhost:5000/library/eurus:700b-v0.5-iq3_XXS", "mynamespace/apeliotes:latest", "myhost/mynamespace/lips:code", } var s Server for _, n := range expectNames { _, digest := createBinFile(t, nil, nil) createRequest(t, s.CreateHandler, api.CreateRequest{ Name: n, Files: map[string]string{"test.gguf": digest}, }) } w := createRequest(t, s.ListHandler, nil) if w.Code != http.StatusOK { t.Fatalf("expected status code 200, actual %d", w.Code) } var resp api.ListResponse if err := json.NewDecoder(w.Body).Decode(&resp); err != nil { t.Fatal(err) } if len(resp.Models) != len(expectNames) { t.Fatalf("expected %d models, actual %d", len(expectNames), len(resp.Models)) } actualNames := make([]string, len(resp.Models)) for i, m := range resp.Models { actualNames[i] = m.Name } slices.Sort(actualNames) slices.Sort(expectNames) if !slices.Equal(actualNames, expectNames) { t.Fatalf("expected slices to be equal %v", actualNames) } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/server/routes_harmony_streaming_test.go
server/routes_harmony_streaming_test.go
package server // this test file is to test integration of harmony parser into routes.go (as // opposed to harmonyparser_test.go, which tests the parser in isolation) import ( "bytes" "context" "encoding/json" "net/http" "strings" "testing" "time" "github.com/gin-gonic/gin" "github.com/ollama/ollama/api" "github.com/ollama/ollama/fs/ggml" "github.com/ollama/ollama/llm" "github.com/ollama/ollama/ml" ) func getTestTools() []api.Tool { return []api.Tool{ { Type: "function", Function: api.ToolFunction{ Name: "get_weather", Description: "Get the current weather in a given location", Parameters: api.ToolFunctionParameters{ Type: "object", Required: []string{"location"}, Properties: testPropsMap(map[string]api.ToolProperty{ "location": { Type: api.PropertyType{"string"}, Description: "The city and state, e.g. San Francisco, CA", }, }), }, }, }, { Type: "function", Function: api.ToolFunction{ Name: "calculate", Description: "Calculate a mathematical expression", Parameters: api.ToolFunctionParameters{ Type: "object", Required: []string{"expression"}, Properties: testPropsMap(map[string]api.ToolProperty{ "expression": { Type: api.PropertyType{"string"}, Description: "The mathematical expression to calculate", }, }), }, }, }, } } func createHarmonyTestModel(t *testing.T) (string, string) { t.Helper() return createBinFile(t, ggml.KV{ "general.architecture": "gptoss", "llama.block_count": uint32(1), "llama.context_length": uint32(8192), "llama.embedding_length": uint32(4096), "llama.attention.head_count": uint32(32), "llama.attention.head_count_kv": uint32(8), "tokenizer.ggml.tokens": []string{""}, "tokenizer.ggml.scores": []float32{0}, "tokenizer.ggml.token_type": []int32{0}, }, []*ggml.Tensor{ {Name: "token_embd.weight", Shape: []uint64{1}, WriterTo: bytes.NewReader(make([]byte, 4))}, {Name: "blk.0.attn_norm.weight", Shape: []uint64{1}, WriterTo: bytes.NewReader(make([]byte, 4))}, {Name: "blk.0.ffn_down.weight", Shape: []uint64{1}, WriterTo: bytes.NewReader(make([]byte, 4))}, {Name: "blk.0.ffn_gate.weight", Shape: []uint64{1}, WriterTo: bytes.NewReader(make([]byte, 4))}, {Name: "blk.0.ffn_up.weight", Shape: []uint64{1}, WriterTo: bytes.NewReader(make([]byte, 4))}, {Name: "blk.0.ffn_norm.weight", Shape: []uint64{1}, WriterTo: bytes.NewReader(make([]byte, 4))}, {Name: "blk.0.attn_k.weight", Shape: []uint64{1}, WriterTo: bytes.NewReader(make([]byte, 4))}, {Name: "blk.0.attn_output.weight", Shape: []uint64{1}, WriterTo: bytes.NewReader(make([]byte, 4))}, {Name: "blk.0.attn_q.weight", Shape: []uint64{1}, WriterTo: bytes.NewReader(make([]byte, 4))}, {Name: "blk.0.attn_v.weight", Shape: []uint64{1}, WriterTo: bytes.NewReader(make([]byte, 4))}, {Name: "output.weight", Shape: []uint64{1}, WriterTo: bytes.NewReader(make([]byte, 4))}, }) } // TestChatHarmonyParserStreamingRealtime verifies that chunks are emitted as soon as they're available func TestChatHarmonyParserStreamingRealtime(t *testing.T) { gin.SetMode(gin.TestMode) type step struct { input llm.CompletionResponse wantContent string wantThinking string wantToolCalls []api.ToolCall } testCases := []struct { name string steps []step only bool }{ { name: "content streams as it arrives", steps: []step{ { input: llm.CompletionResponse{Content: "<|message|>Hello", Done: false}, wantContent: "Hello", }, { input: llm.CompletionResponse{Content: ", world", Done: false}, wantContent: ", world", }, { input: llm.CompletionResponse{Content: "!<|end|>", Done: true, DoneReason: llm.DoneReasonStop}, wantContent: "!", }, }, }, { name: "thinking streams separately from content", steps: []step{ { input: llm.CompletionResponse{Content: "<|channel|>analysis<|message|>Thinking...", Done: false}, wantThinking: "Thinking...", }, { input: llm.CompletionResponse{Content: "<|end|>", Done: false}, // No output expected - just closes the analysis message and resets state to normal }, { input: llm.CompletionResponse{Content: "<|start|>assistant<|message|>Answer", Done: false}, wantContent: "Answer", // After message end, state is reset to normal }, { input: llm.CompletionResponse{Content: "<|end|>", Done: true, DoneReason: llm.DoneReasonStop}, // No output expected - just closes the assistant message }, }, }, { name: "partial tags buffer until complete", steps: []step{ { input: llm.CompletionResponse{Content: "<|chan", Done: false}, // No output - partial tag }, { input: llm.CompletionResponse{Content: "nel|>analysis<|mess", Done: false}, // No output - still building tags }, { input: llm.CompletionResponse{Content: "age|>Deep ", Done: false}, wantThinking: "Deep ", }, { input: llm.CompletionResponse{Content: "thought<|end|>", Done: false}, wantThinking: "thought", }, { input: llm.CompletionResponse{Content: "<|start|>assistant<|message|>Done<|end|>", Done: true, DoneReason: llm.DoneReasonStop}, wantContent: "Done", // After message end, state is reset to normal }, }, }, { name: "simple assistant after analysis", steps: []step{ { input: llm.CompletionResponse{Content: "<|channel|>analysis<|message|>Think<|end|><|start|>assistant<|message|>Answer<|end|>", Done: true, DoneReason: llm.DoneReasonStop}, wantContent: "Answer", wantThinking: "Think", }, }, }, { name: "tool call parsed and returned correctly", steps: []step{ { input: llm.CompletionResponse{Content: "<|channel|>commentary to=functions.get_weather<|message|>{\"location\":\"San Francisco\"}<|end|><|start|>assistant<|message|>The weather is sunny<|end|>", Done: true, DoneReason: llm.DoneReasonStop}, wantContent: "The weather is sunny", wantToolCalls: []api.ToolCall{ { Function: api.ToolCallFunction{ Name: "get_weather", Arguments: testArgs(map[string]any{ "location": "San Francisco", }), }, }, }, }, }, }, { name: "tool call with streaming JSON across chunks", steps: []step{ { input: llm.CompletionResponse{Content: "<|channel|>commentary to=functions.calculate<|message|>{\"expr", Done: false}, // No output yet - incomplete JSON }, { input: llm.CompletionResponse{Content: "ession\":\"2+", Done: false}, // Still no output - incomplete JSON }, { input: llm.CompletionResponse{Content: "2\"}", Done: true}, wantToolCalls: []api.ToolCall{ { Function: api.ToolCallFunction{ Name: "calculate", Arguments: testArgs(map[string]any{ "expression": "2+2", }), }, }, }, }, }, }, } anyOnlies := false for _, tc := range testCases { if tc.only { anyOnlies = true } } for _, tc := range testCases { if anyOnlies && !tc.only { continue } t.Run(tc.name, func(t *testing.T) { var chunks []api.ChatResponse chunkIdx := 0 mockResponses := make([]llm.CompletionResponse, len(tc.steps)) for i, step := range tc.steps { mockResponses[i] = step.input } mock := mockRunner{ CompletionFn: func(ctx context.Context, r llm.CompletionRequest, fn func(llm.CompletionResponse)) error { for _, resp := range mockResponses { fn(resp) // Give the handler time to process each response time.Sleep(30 * time.Millisecond) } return nil }, } s := Server{ sched: &Scheduler{ pendingReqCh: make(chan *LlmRequest, 1), finishedReqCh: make(chan *LlmRequest, 1), expiredCh: make(chan *runnerRef, 1), unloadedCh: make(chan any, 1), loaded: make(map[string]*runnerRef), newServerFn: newMockServer(&mock), getGpuFn: getGpuFn, getSystemInfoFn: getSystemInfoFn, waitForRecovery: 100 * time.Millisecond, loadFn: func(req *LlmRequest, _ *ggml.GGML, _ ml.SystemInfo, _ []ml.DeviceInfo, _ bool) bool { req.successCh <- &runnerRef{ llama: &mock, } return false }, }, } go s.sched.Run(t.Context()) // Create a simple test model _, digest := createHarmonyTestModel(t) streamFalse := false w := createRequest(t, s.CreateHandler, api.CreateRequest{ Model: "harmony-test-streaming", Files: map[string]string{"test.gguf": digest}, Template: `<|start|><|end|>{{ with .Tools }}{{ end }}{{ .Prompt }}`, Stream: &streamFalse, }) if w.Code != 200 { t.Fatalf("failed to create model: %d", w.Code) } // Test chat endpoint with streaming streamTrue := true w = createRequest(t, s.ChatHandler, api.ChatRequest{ Model: "harmony-test-streaming", Messages: []api.Message{{Role: "user", Content: "Hello"}}, Stream: &streamTrue, Tools: getTestTools(), }) if w.Code != 200 { t.Fatalf("chat request failed: %d - %s", w.Code, w.Body.String()) } // Parse all chunks decoder := json.NewDecoder(w.Body) for decoder.More() { var chunk api.ChatResponse if err := decoder.Decode(&chunk); err != nil { t.Fatalf("failed to decode chunk: %v", err) } if chunk.Message.Content != "" || chunk.Message.Thinking != "" || len(chunk.Message.ToolCalls) > 0 { chunks = append(chunks, chunk) } } // Log received chunks for debugging if t.Failed() || len(chunks) == 0 { t.Logf("Received %d chunks:", len(chunks)) for i, chunk := range chunks { t.Logf(" Chunk %d: content=%q thinking=%q", i, chunk.Message.Content, chunk.Message.Thinking) } } // Verify chunks match expected steps for i, step := range tc.steps { // Skip steps that don't expect any output if step.wantContent == "" && step.wantThinking == "" && len(step.wantToolCalls) == 0 { continue } if chunkIdx >= len(chunks) { t.Errorf("step %d: expected chunk not received (wanted content=%q thinking=%q)", i, step.wantContent, step.wantThinking) continue } chunk := chunks[chunkIdx] if chunk.Message.Content != step.wantContent || chunk.Message.Thinking != step.wantThinking { t.Errorf("step %d: chunk mismatch: got (content=%q, thinking=%q), want (content=%q, thinking=%q)", i, chunk.Message.Content, chunk.Message.Thinking, step.wantContent, step.wantThinking) } // Check tool calls if expected if len(step.wantToolCalls) > 0 { if len(chunk.Message.ToolCalls) != len(step.wantToolCalls) { t.Errorf("step %d: tool calls count mismatch: got %d, want %d", i, len(chunk.Message.ToolCalls), len(step.wantToolCalls)) } else { for j, wantCall := range step.wantToolCalls { if j >= len(chunk.Message.ToolCalls) { break } gotCall := chunk.Message.ToolCalls[j] if gotCall.Function.Name != wantCall.Function.Name { t.Errorf("step %d, tool call %d: name mismatch: got %q, want %q", i, j, gotCall.Function.Name, wantCall.Function.Name) } // Compare arguments as JSON strings for simplicity gotArgs, _ := json.Marshal(gotCall.Function.Arguments) wantArgs, _ := json.Marshal(wantCall.Function.Arguments) if string(gotArgs) != string(wantArgs) { t.Errorf("step %d, tool call %d: arguments mismatch: got %s, want %s", i, j, string(gotArgs), string(wantArgs)) } } } } chunkIdx++ } // Check if we have extra chunks if chunkIdx < len(chunks) { t.Errorf("received %d extra chunks", len(chunks)-chunkIdx) for i := chunkIdx; i < len(chunks); i++ { t.Logf(" extra chunk %d: content=%q thinking=%q", i-chunkIdx, chunks[i].Message.Content, chunks[i].Message.Thinking) } } }) } } // TestChatHarmonyParserStreamingSimple is a simpler test that just verifies basic streaming func TestChatHarmonyParserStreamingSimple(t *testing.T) { gin.SetMode(gin.TestMode) mockResponses := []llm.CompletionResponse{ {Content: "<|message|>First ", Done: false}, {Content: "chunk ", Done: false}, {Content: "here<|end|>", Done: true, DoneReason: llm.DoneReasonStop}, } mock := mockRunner{ CompletionFn: func(ctx context.Context, r llm.CompletionRequest, fn func(llm.CompletionResponse)) error { t.Logf("Mock received prompt: %q", r.Prompt) t.Logf("Mock sending %d responses", len(mockResponses)) for i, resp := range mockResponses { t.Logf("Sending response %d: %q", i, resp.Content) fn(resp) } return nil }, } s := Server{ sched: &Scheduler{ pendingReqCh: make(chan *LlmRequest, 1), finishedReqCh: make(chan *LlmRequest, 1), expiredCh: make(chan *runnerRef, 1), unloadedCh: make(chan any, 1), loaded: make(map[string]*runnerRef), newServerFn: newMockServer(&mock), getGpuFn: getGpuFn, getSystemInfoFn: getSystemInfoFn, waitForRecovery: 100 * time.Millisecond, loadFn: func(req *LlmRequest, _ *ggml.GGML, _ ml.SystemInfo, _ []ml.DeviceInfo, _ bool) bool { req.successCh <- &runnerRef{ llama: &mock, } return false }, }, } go s.sched.Run(t.Context()) // Create model _, digest := createHarmonyTestModel(t) streamFalse := false w := createRequest(t, s.CreateHandler, api.CreateRequest{ Model: "gpt-oss", Files: map[string]string{"test.gguf": digest}, Template: `<|start|><|end|>{{ .Tools }}{{ .Prompt }}`, Stream: &streamFalse, }) if w.Code != 200 { t.Fatalf("failed to create model: %d", w.Code) } // Test streaming streamTrue := true w = createRequest(t, s.ChatHandler, api.ChatRequest{ Model: "gpt-oss", Messages: []api.Message{{Role: "user", Content: "Hello"}}, Stream: &streamTrue, Tools: getTestTools(), }) if w.Code != 200 { t.Fatalf("chat request failed: %d - %s", w.Code, w.Body.String()) } // Parse chunks var chunks []api.ChatResponse decoder := json.NewDecoder(w.Body) for decoder.More() { var chunk api.ChatResponse if err := decoder.Decode(&chunk); err != nil { t.Fatalf("failed to decode chunk: %v", err) } chunks = append(chunks, chunk) t.Logf("Received chunk %d: content=%q thinking=%q done=%v", len(chunks), chunk.Message.Content, chunk.Message.Thinking, chunk.Done) } // Verify we got chunks if len(chunks) == 0 { t.Fatal("expected streaming chunks, got none") } // Verify content var content strings.Builder for _, chunk := range chunks { content.WriteString(chunk.Message.Content) } expectedContent := "First chunk here" if content.String() != expectedContent { t.Errorf("content mismatch: got %q, want %q", content.String(), expectedContent) } // Verify we got multiple chunks (streaming) contentChunks := 0 for _, chunk := range chunks { if chunk.Message.Content != "" { contentChunks++ } } if contentChunks < 2 { t.Errorf("expected at least 2 content chunks for streaming, got %d", contentChunks) } } func TestChatHarmonyParserStreaming(t *testing.T) { gin.SetMode(gin.TestMode) type expectedChunk struct { afterResponse int // Which mock response this chunk should appear after content string // Expected content in this chunk thinking string // Expected thinking in this chunk } testCases := []struct { name string mockResponses []llm.CompletionResponse expectedChunks []expectedChunk wantContent string wantThinking string }{ { name: "simple message without thinking", mockResponses: []llm.CompletionResponse{ {Content: "<|start|>assistant<|message|>Hello, ", Done: false}, {Content: "how can I help?", Done: false}, {Content: "<|end|>", Done: true, DoneReason: llm.DoneReasonStop}, }, expectedChunks: []expectedChunk{ {afterResponse: 1, content: "Hello, "}, {afterResponse: 2, content: "how can I help?"}, }, wantContent: "Hello, how can I help?", }, { name: "message with analysis channel for thinking", mockResponses: []llm.CompletionResponse{ {Content: "<|channel|>analysis<|message|>", Done: false}, {Content: "Let me think ", Done: false}, {Content: "about this problem...", Done: false}, {Content: "<|end|>", Done: false}, {Content: "<|start|>assistant<|message|>", Done: false}, {Content: "The answer ", Done: false}, {Content: "is 42", Done: false}, {Content: "<|end|>", Done: true, DoneReason: llm.DoneReasonStop}, }, expectedChunks: []expectedChunk{ {afterResponse: 2, thinking: "Let me think "}, {afterResponse: 3, thinking: "about this problem..."}, {afterResponse: 6, content: "The answer "}, {afterResponse: 7, content: "is 42"}, }, wantContent: "The answer is 42", wantThinking: "Let me think about this problem...", }, { name: "streaming with partial tags across boundaries", mockResponses: []llm.CompletionResponse{ {Content: "<|chan", Done: false}, {Content: "nel|>analy", Done: false}, {Content: "sis<|mess", Done: false}, {Content: "age|>Think", Done: false}, {Content: "ing deeply...<|end|>", Done: false}, {Content: "<|start|>assi", Done: false}, {Content: "stant<|message|>Result ", Done: false}, {Content: "computed<|e", Done: false}, {Content: "nd|>", Done: true, DoneReason: llm.DoneReasonStop}, }, expectedChunks: []expectedChunk{ {afterResponse: 4, thinking: "Think"}, {afterResponse: 5, thinking: "ing deeply..."}, {afterResponse: 7, content: "Result "}, {afterResponse: 8, content: "computed"}, }, wantContent: "Result computed", wantThinking: "Thinking deeply...", }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { // Channel to synchronize mock responses with chunk verification responsesSent := make(chan int, len(tc.mockResponses)) mock := mockRunner{ CompletionFn: func(ctx context.Context, r llm.CompletionRequest, fn func(llm.CompletionResponse)) error { // Send mock responses one at a time, notifying when each is sent for i, resp := range tc.mockResponses { fn(resp) responsesSent <- i + 1 } close(responsesSent) return nil }, } s := Server{ sched: &Scheduler{ pendingReqCh: make(chan *LlmRequest, 1), finishedReqCh: make(chan *LlmRequest, 1), expiredCh: make(chan *runnerRef, 1), unloadedCh: make(chan any, 1), loaded: make(map[string]*runnerRef), newServerFn: newMockServer(&mock), getGpuFn: getGpuFn, getSystemInfoFn: getSystemInfoFn, waitForRecovery: 250 * time.Millisecond, loadFn: func(req *LlmRequest, _ *ggml.GGML, _ ml.SystemInfo, _ []ml.DeviceInfo, _ bool) bool { req.successCh <- &runnerRef{ llama: &mock, } return false }, }, } go s.sched.Run(t.Context()) // Create a minimal model _, digest := createHarmonyTestModel(t) // Create model with passthrough template stream := false w := createRequest(t, s.CreateHandler, api.CreateRequest{ Model: "harmony-test", Files: map[string]string{"file.gguf": digest}, Template: `<|start|><|end|>{{ with .Tools }}{{ end }}{{ .Prompt }}`, Stream: &stream, }) if w.Code != http.StatusOK { t.Fatalf("failed to create model: %d", w.Code) } // Test chat endpoint with streaming streamTrue := true w = createRequest(t, s.ChatHandler, api.ChatRequest{ Model: "harmony-test", Messages: []api.Message{{Role: "user", Content: "Hello"}}, Stream: &streamTrue, Tools: getTestTools(), }) if w.Code != http.StatusOK { t.Fatalf("chat request failed: %d - %s", w.Code, w.Body.String()) } // Parse streaming response var chunks []api.ChatResponse var content, thinking strings.Builder decoder := json.NewDecoder(w.Body) for decoder.More() { var chunk api.ChatResponse if err := decoder.Decode(&chunk); err != nil { t.Fatalf("failed to decode chunk: %v", err) } chunks = append(chunks, chunk) // Accumulate content and thinking from each chunk content.WriteString(chunk.Message.Content) thinking.WriteString(chunk.Message.Thinking) // Debug output t.Logf("Chunk %d: content=%q thinking=%q done=%v", len(chunks), chunk.Message.Content, chunk.Message.Thinking, chunk.Done) } // Verify we got streaming chunks if len(chunks) == 0 { t.Fatal("expected streaming chunks, got none") } gotContent := content.String() gotThinking := thinking.String() if gotContent != tc.wantContent { t.Errorf("content mismatch: got %q, want %q", gotContent, tc.wantContent) } if gotThinking != tc.wantThinking { t.Errorf("thinking mismatch: got %q, want %q", gotThinking, tc.wantThinking) } // Verify last chunk has done=true lastChunk := chunks[len(chunks)-1] if !lastChunk.Done { t.Error("expected last chunk to have done=true") } }) } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/server/images_test.go
server/images_test.go
package server import ( "strings" "testing" "github.com/ollama/ollama/fs/ggml" "github.com/ollama/ollama/template" "github.com/ollama/ollama/types/model" ) func TestModelCapabilities(t *testing.T) { // Create completion model (llama architecture without vision) completionModelPath, _ := createBinFile(t, ggml.KV{ "general.architecture": "llama", }, []*ggml.Tensor{}) // Create vision model (llama architecture with vision block count) visionModelPath, _ := createBinFile(t, ggml.KV{ "general.architecture": "llama", "llama.vision.block_count": uint32(1), }, []*ggml.Tensor{}) // Create embedding model (bert architecture with pooling type) embeddingModelPath, _ := createBinFile(t, ggml.KV{ "general.architecture": "bert", "bert.pooling_type": uint32(1), }, []*ggml.Tensor{}) toolsInsertTemplate, err := template.Parse("{{ .prompt }}{{ if .tools }}{{ .tools }}{{ end }}{{ if .suffix }}{{ .suffix }}{{ end }}") if err != nil { t.Fatalf("Failed to parse template: %v", err) } chatTemplate, err := template.Parse("{{ .prompt }}") if err != nil { t.Fatalf("Failed to parse template: %v", err) } toolsTemplate, err := template.Parse("{{ .prompt }}{{ if .tools }}{{ .tools }}{{ end }}") if err != nil { t.Fatalf("Failed to parse template: %v", err) } testModels := []struct { name string model Model expectedCaps []model.Capability }{ { name: "model with completion capability", model: Model{ ModelPath: completionModelPath, Template: chatTemplate, }, expectedCaps: []model.Capability{model.CapabilityCompletion}, }, { name: "model with completion, tools, and insert capability", model: Model{ ModelPath: completionModelPath, Template: toolsInsertTemplate, }, expectedCaps: []model.Capability{model.CapabilityCompletion, model.CapabilityTools, model.CapabilityInsert}, }, { name: "model with tools capability", model: Model{ ModelPath: completionModelPath, Template: toolsTemplate, }, expectedCaps: []model.Capability{model.CapabilityCompletion, model.CapabilityTools}, }, { name: "model with vision capability", model: Model{ ModelPath: visionModelPath, Template: chatTemplate, }, expectedCaps: []model.Capability{model.CapabilityCompletion, model.CapabilityVision}, }, { name: "model with vision, tools, and insert capability", model: Model{ ModelPath: visionModelPath, Template: toolsInsertTemplate, }, expectedCaps: []model.Capability{model.CapabilityCompletion, model.CapabilityVision, model.CapabilityTools, model.CapabilityInsert}, }, { name: "model with embedding capability", model: Model{ ModelPath: embeddingModelPath, Template: chatTemplate, }, expectedCaps: []model.Capability{model.CapabilityEmbedding}, }, } // compare two slices of model.Capability regardless of order compareCapabilities := func(a, b []model.Capability) bool { if len(a) != len(b) { return false } aCount := make(map[model.Capability]int) for _, cap := range a { aCount[cap]++ } bCount := make(map[model.Capability]int) for _, cap := range b { bCount[cap]++ } for cap, count := range aCount { if bCount[cap] != count { return false } } return true } for _, tt := range testModels { t.Run(tt.name, func(t *testing.T) { // Test Capabilities method caps := tt.model.Capabilities() if !compareCapabilities(caps, tt.expectedCaps) { t.Errorf("Expected capabilities %v, got %v", tt.expectedCaps, caps) } }) } } func TestModelCheckCapabilities(t *testing.T) { // Create simple model file for tests that don't depend on GGUF content completionModelPath, _ := createBinFile(t, ggml.KV{ "general.architecture": "llama", }, []*ggml.Tensor{}) // Create vision model (llama architecture with vision block count) visionModelPath, _ := createBinFile(t, ggml.KV{ "general.architecture": "llama", "llama.vision.block_count": uint32(1), }, []*ggml.Tensor{}) // Create embedding model (bert architecture with pooling type) embeddingModelPath, _ := createBinFile(t, ggml.KV{ "general.architecture": "bert", "bert.pooling_type": uint32(1), }, []*ggml.Tensor{}) toolsInsertTemplate, err := template.Parse("{{ .prompt }}{{ if .tools }}{{ .tools }}{{ end }}{{ if .suffix }}{{ .suffix }}{{ end }}") if err != nil { t.Fatalf("Failed to parse template: %v", err) } chatTemplate, err := template.Parse("{{ .prompt }}") if err != nil { t.Fatalf("Failed to parse template: %v", err) } toolsTemplate, err := template.Parse("{{ .prompt }}{{ if .tools }}{{ .tools }}{{ end }}") if err != nil { t.Fatalf("Failed to parse template: %v", err) } tests := []struct { name string model Model checkCaps []model.Capability expectedErrMsg string }{ { name: "completion model without tools capability", model: Model{ ModelPath: completionModelPath, Template: chatTemplate, }, checkCaps: []model.Capability{model.CapabilityTools}, expectedErrMsg: "does not support tools", }, { name: "model with all needed capabilities", model: Model{ ModelPath: completionModelPath, Template: toolsInsertTemplate, }, checkCaps: []model.Capability{model.CapabilityTools, model.CapabilityInsert}, }, { name: "model missing insert capability", model: Model{ ModelPath: completionModelPath, Template: toolsTemplate, }, checkCaps: []model.Capability{model.CapabilityInsert}, expectedErrMsg: "does not support insert", }, { name: "model missing vision capability", model: Model{ ModelPath: completionModelPath, Template: toolsTemplate, }, checkCaps: []model.Capability{model.CapabilityVision}, expectedErrMsg: "does not support vision", }, { name: "model with vision capability", model: Model{ ModelPath: visionModelPath, Template: chatTemplate, }, checkCaps: []model.Capability{model.CapabilityVision}, }, { name: "model with embedding capability", model: Model{ ModelPath: embeddingModelPath, Template: chatTemplate, }, checkCaps: []model.Capability{model.CapabilityEmbedding}, }, { name: "unknown capability", model: Model{ ModelPath: completionModelPath, Template: chatTemplate, }, checkCaps: []model.Capability{"unknown"}, expectedErrMsg: "unknown capability", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { // Test CheckCapabilities method err := tt.model.CheckCapabilities(tt.checkCaps...) if tt.expectedErrMsg == "" { if err != nil { t.Errorf("Expected no error, got: %v", err) } } else { if err == nil { t.Errorf("Expected error containing %q, got nil", tt.expectedErrMsg) } else if !strings.Contains(err.Error(), tt.expectedErrMsg) { t.Errorf("Expected error containing %q, got: %v", tt.expectedErrMsg, err) } } }) } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/server/quantization_test.go
server/quantization_test.go
package server import ( "bytes" "fmt" "math" "os" "strings" "testing" fsggml "github.com/ollama/ollama/fs/ggml" "github.com/ollama/ollama/ml/backend/ggml" ) func TestGetTensorNewType(t *testing.T) { cases := []struct { name string kv map[string]any qs quantizeState newType fsggml.TensorType tensor_name string shape []uint64 ftype fsggml.FileType expected fsggml.TensorType expectedPanic string }{ { name: "output_unsupported", kv: map[string]any{}, newType: fsggml.TensorTypeQ4_0, tensor_name: "output.weight", shape: []uint64{100, 100}, ftype: fsggml.FileTypeF32, expected: fsggml.TensorTypeF16, }, { name: "output_Q8", kv: map[string]any{}, newType: fsggml.TensorTypeQ4_0, tensor_name: "output.weight", shape: []uint64{1024, 1024}, ftype: fsggml.FileTypeF32, expected: fsggml.TensorTypeQ6_K, }, { name: "attn_v.weight_q4_k_m", qs: quantizeState{ iAttnV: 2, nAttnV: 3 * 8, }, kv: map[string]any{}, newType: fsggml.TensorTypeQ4_0, tensor_name: "blk.0.attn_v.weight", shape: []uint64{256}, ftype: fsggml.FileTypeQ4_K_M, expected: fsggml.TensorTypeQ6_K, }, { name: "attn_v.weight_q4_k_s", qs: quantizeState{}, kv: map[string]any{}, newType: fsggml.TensorTypeQ4_0, tensor_name: "blk.0.attn_v.weight", shape: []uint64{256}, ftype: fsggml.FileTypeQ4_K_S, expected: fsggml.TensorTypeQ5_K, }, { name: "attn_v.weight_8_expert", qs: quantizeState{}, kv: map[string]any{ "general.architecture": "foo", "foo.expert_count": uint32(8), }, newType: fsggml.TensorTypeQ4_0, tensor_name: "blk.0.attn_v.weight", shape: []uint64{256}, ftype: fsggml.FileTypeF32, expected: fsggml.TensorTypeQ8_0, }, { name: "attn_k.weight_8_expert", qs: quantizeState{}, kv: map[string]any{ "general.architecture": "foo", "foo.expert_count": uint32(8), }, newType: fsggml.TensorTypeQ4_0, tensor_name: "blk.0.attn_k.weight", shape: []uint64{256}, ftype: fsggml.FileTypeF32, expected: fsggml.TensorTypeQ8_0, }, { name: "ffn_down_q4_k_m", qs: quantizeState{ iFfnDown: 1, nFfnDown: 8, }, kv: map[string]any{}, newType: fsggml.TensorTypeQ4_0, tensor_name: "ffn_down", shape: []uint64{256}, ftype: fsggml.FileTypeQ4_K_M, expected: fsggml.TensorTypeQ4_0, }, { name: "ffn_down_q4_k_m_6", qs: quantizeState{ iFfnDown: 2, nFfnDown: 3 * 8, }, kv: map[string]any{}, newType: fsggml.TensorTypeQ4_0, tensor_name: "ffn_down", shape: []uint64{256}, ftype: fsggml.FileTypeQ4_K_M, expected: fsggml.TensorTypeQ6_K, }, { name: "ffn_down_q4_k_s", qs: quantizeState{ iFfnDown: 2, nFfnDown: 3 * 8, }, kv: map[string]any{}, newType: fsggml.TensorTypeQ4_0, tensor_name: "ffn_down", shape: []uint64{256}, ftype: fsggml.FileTypeQ4_K_S, expected: fsggml.TensorTypeQ5_K, }, { name: "attn_qkv.weight_q4_k_m", qs: quantizeState{}, kv: map[string]any{}, newType: fsggml.TensorTypeQ4_0, tensor_name: "blk.0.attn_qkv.weight", shape: []uint64{256}, ftype: fsggml.FileTypeQ4_K_M, expected: fsggml.TensorTypeQ5_K, }, } for _, tt := range cases { t.Run(tt.name, func(t *testing.T) { if tt.expectedPanic != "" { defer func() { e := recover() if !strings.Contains(fmt.Sprintf("%v", e), tt.expectedPanic) { t.Fatalf("incorrect panic\ngot: %v\nexpected: %s", e, tt.expectedPanic) } }() } else { defer func() { e := recover() if e != nil { t.Fatalf("hit unexpected panic %v", e) } }() } ret := getTensorNewType(tt.kv, &tt.qs, tt.newType, tt.tensor_name, tt.shape, tt.ftype) if ret != tt.expected { t.Fatalf("incorrect type returned\ngot: %d\nexpected: %d", ret, tt.expected) } }) } } func TestQuantizeModel(t *testing.T) { cases := []struct { name string kv map[string]any tensors []*fsggml.Tensor newType string expectedTensorTypes map[string]fsggml.TensorType }{ { name: "f16_q4_k", kv: map[string]any{ "general.architecture": "foo", }, tensors: []*fsggml.Tensor{ { Name: "blk.0.attn.weight", Kind: uint32(fsggml.TensorTypeF16), Offset: uint64(0), Shape: []uint64{512, 2}, WriterTo: bytes.NewReader( append(append(append(quantBytes[fsggml.TensorTypeF16], quantBytes[fsggml.TensorTypeF16]...), quantBytes[fsggml.TensorTypeF16]...), quantBytes[fsggml.TensorTypeF16]...), ), }, { Name: "output.weight", Kind: uint32(fsggml.TensorTypeF16), Offset: uint64(0), Shape: []uint64{256, 4}, WriterTo: bytes.NewReader( append(append(append(quantBytes[fsggml.TensorTypeF16], quantBytes[fsggml.TensorTypeF16]...), quantBytes[fsggml.TensorTypeF16]...), quantBytes[fsggml.TensorTypeF16]...), ), }, }, newType: "Q4_K", expectedTensorTypes: map[string]fsggml.TensorType{ "blk.0.attn.weight": fsggml.TensorTypeQ4_K, "output.weight": fsggml.TensorTypeQ6_K, }, }, { name: "f32_q4_k", kv: map[string]any{ "general.architecture": "foo", }, tensors: []*fsggml.Tensor{ { Name: "blk.0.attn_v.weight", Kind: uint32(fsggml.TensorTypeF32), Offset: uint64(0), Shape: []uint64{512, 2}, WriterTo: bytes.NewReader( append(append(append(quantBytes[fsggml.TensorTypeF32], quantBytes[fsggml.TensorTypeF32]...), quantBytes[fsggml.TensorTypeF32]...), quantBytes[fsggml.TensorTypeF32]...), ), }, { Name: "output.weight", Kind: uint32(fsggml.TensorTypeF32), Offset: uint64(0), Shape: []uint64{512}, WriterTo: bytes.NewReader(append(quantBytes[fsggml.TensorTypeF32], quantBytes[fsggml.TensorTypeF32]...)), }, }, newType: "Q4_K", expectedTensorTypes: map[string]fsggml.TensorType{ "blk.0.attn_v.weight": fsggml.TensorTypeQ6_K, "output.weight": fsggml.TensorTypeF32, }, }, { name: "f16_q8_0", kv: map[string]any{ "general.architecture": "foo", }, tensors: []*fsggml.Tensor{ { Name: "blk.0.attn.weight", Kind: uint32(fsggml.TensorTypeF16), Offset: uint64(0), Shape: []uint64{32, 16, 2}, WriterTo: bytes.NewReader( append(append(append(quantBytes[fsggml.TensorTypeF16], quantBytes[fsggml.TensorTypeF16]...), quantBytes[fsggml.TensorTypeF16]...), quantBytes[fsggml.TensorTypeF16]...), ), }, { Name: "output.weight", Kind: uint32(fsggml.TensorTypeF16), Offset: uint64(0), Shape: []uint64{256, 4}, WriterTo: bytes.NewReader( append(append(append(quantBytes[fsggml.TensorTypeF16], quantBytes[fsggml.TensorTypeF16]...), quantBytes[fsggml.TensorTypeF16]...), quantBytes[fsggml.TensorTypeF16]...), ), }, }, newType: "Q8_0", expectedTensorTypes: map[string]fsggml.TensorType{ "blk.0.attn.weight": fsggml.TensorTypeQ8_0, "output.weight": fsggml.TensorTypeQ8_0, }, }, } for _, tt := range cases { t.Run(tt.name, func(t *testing.T) { p, _ := createBinFile(t, tt.kv, tt.tensors) fp, err := os.Open(p) if err != nil { t.Fatal(err.Error()) } defer fp.Close() meta, err := fsggml.Decode(fp, -1) if err != nil { t.Fatal(err.Error()) } progressCalled := false progress := func(n uint64) { // fmt.Fprintf(os.Stderr, "progress: %f\n", p) progressCalled = true } tmp, err := os.CreateTemp(t.TempDir(), tt.name+".out") if err != nil { t.Fatal(err.Error()) } defer tmp.Close() ftype, err := fsggml.ParseFileType(tt.newType) if err != nil { t.Fatal(err.Error()) } err = quantize(fp, tmp, meta, ftype, progress) if err != nil { t.Fatalf("error during quantize: %s", err) } if !progressCalled { t.Fatalf("progress was not reported") } // Now attempt to load it back and make sure types match expected fpNew, err := os.Open(tmp.Name()) if err != nil { t.Fatalf("failed to load the quantized model %s: %s", tmp.Name(), err) } defer fpNew.Close() newMeta, err := fsggml.Decode(fpNew, -1) if err != nil { t.Fatalf("failed to load the quantized model %s: %s", tmp.Name(), err) } tensors := newMeta.Tensors() for _, l := range tensors.GroupLayers() { for _, tensor := range l { if fsggml.TensorType(tensor.Kind) != tt.expectedTensorTypes[tensor.Name] { t.Fatalf("incorrect output type for %s\ngot:%s\nexpected:%s", tensor.Name, fsggml.TensorType(tensor.Kind), tt.expectedTensorTypes[tensor.Name]) } } } }) } } func TestConvertToF32(t *testing.T) { expected := make([]float32, 256) for i := range expected { expected[i] = float32(i) } for dtype, data := range quantBytes { // Skip the no-op if dtype == fsggml.TensorTypeF32 { continue } t.Run(dtype.String(), func(t *testing.T) { fp32 := ggml.ConvertToF32(data, uint32(dtype), 256) similarity := cosineSimilarity(expected, fp32) if similarity < 0.999 { t.Fatalf("Results not similar enough: %s %f", dtype.String(), similarity) } }) } } func dotProduct[V float32 | float64](v1, v2 []V) V { var result V = 0 for i := range v1 { result += v1[i] * v2[i] } return result } func magnitude[V float32 | float64](v []V) V { var result V = 0 for _, val := range v { result += val * val } return V(math.Sqrt(float64(result))) } func cosineSimilarity[V float32 | float64](v1, v2 []V) V { return dotProduct(v1, v2) / (magnitude(v1) * magnitude(v2)) } // Precomputed quantized data - arange 256 // # For gguf-py supported types // import gguf // import numpy as np // print(repr(gguf.quantize(np.arange(256, dtype=np.float16), gguf.GGMLQuantizationType.Q4_0))) // // For types not supported by gguf-py converted via ggml_fp32_to_fp16_row and quantize_XXX // // data := make([]byte, 256*2) // fp32 := make([]float32, 256) // for i := range 256 { // fp32[i] = float32(i) // } // l := C.quantize_q6_K((*C.float)(&fp32[0]), unsafe.Pointer(&data[0]), 1, 256, nil) // for i := range data[:int(l)] { // fmt.Printf("%d, ", data[i]) // } var ( quantBytes = map[fsggml.TensorType][]byte{ fsggml.TensorTypeQ4_0: { 192, 195, 72, 72, 55, 55, 55, 55, 38, 38, 38, 38, 21, 21, 21, 21, 4, 4, 224, 199, 36, 36, 36, 36, 19, 19, 19, 19, 19, 19, 19, 19, 2, 2, 2, 2, 240, 201, 19, 19, 18, 18, 18, 18, 18, 18, 18, 18, 2, 2, 2, 2, 1, 1, 240, 203, 18, 18, 18, 18, 18, 18, 18, 18, 1, 1, 1, 1, 1, 1, 1, 1, 248, 204, 18, 18, 17, 17, 17, 17, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 248, 205, 17, 17, 17, 17, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 248, 206, 17, 17, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 248, 207, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, }, fsggml.TensorTypeQ4_1: { 34, 64, 0, 0, 128, 128, 145, 145, 162, 162, 179, 179, 196, 196, 213, 213, 230, 230, 247, 247, 34, 64, 0, 80, 128, 128, 145, 145, 162, 162, 179, 179, 196, 196, 213, 213, 230, 230, 247, 247, 34, 64, 0, 84, 128, 128, 145, 145, 162, 162, 179, 179, 196, 196, 213, 213, 230, 230, 247, 247, 34, 64, 0, 86, 128, 128, 145, 145, 162, 162, 179, 179, 196, 196, 213, 213, 230, 230, 247, 247, 34, 64, 0, 88, 128, 128, 145, 145, 162, 162, 179, 179, 196, 196, 213, 213, 230, 230, 247, 247, 34, 64, 0, 89, 128, 128, 145, 145, 162, 162, 179, 179, 196, 196, 213, 213, 230, 230, 247, 247, 34, 64, 0, 90, 128, 128, 145, 145, 162, 162, 179, 179, 196, 196, 213, 213, 230, 230, 247, 247, 34, 64, 0, 91, 128, 128, 145, 145, 162, 162, 179, 179, 196, 196, 213, 213, 230, 230, 247, 247, }, fsggml.TensorTypeQ5_0: { 192, 191, 1, 0, 0, 0, 128, 127, 127, 110, 110, 93, 93, 76, 76, 59, 59, 42, 42, 25, 25, 8, 224, 195, 0, 0, 0, 0, 72, 72, 55, 55, 55, 55, 38, 38, 38, 38, 21, 21, 21, 21, 4, 4, 240, 197, 0, 0, 0, 0, 53, 37, 37, 37, 37, 36, 36, 20, 20, 20, 20, 19, 19, 3, 3, 3, 240, 199, 0, 0, 0, 0, 36, 36, 36, 36, 19, 19, 19, 19, 19, 19, 19, 19, 2, 2, 2, 2, 248, 200, 0, 0, 0, 0, 35, 19, 19, 19, 19, 19, 19, 18, 18, 18, 18, 2, 2, 2, 2, 2, 248, 201, 0, 0, 0, 0, 19, 19, 18, 18, 18, 18, 18, 18, 18, 18, 2, 2, 2, 2, 1, 1, 248, 202, 0, 0, 0, 0, 18, 18, 18, 18, 18, 18, 18, 18, 18, 2, 2, 1, 1, 1, 1, 1, 248, 203, 0, 0, 0, 0, 18, 18, 18, 18, 18, 18, 18, 18, 1, 1, 1, 1, 1, 1, 1, 1, }, fsggml.TensorTypeQ5_1: { 0, 60, 0, 0, 0, 0, 255, 255, 0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255, 0, 60, 0, 80, 0, 0, 255, 255, 0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255, 0, 60, 0, 84, 0, 0, 255, 255, 0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255, 0, 60, 0, 86, 0, 0, 255, 255, 0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255, 0, 60, 0, 88, 0, 0, 255, 255, 0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255, 0, 60, 0, 89, 0, 0, 255, 255, 0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255, 0, 60, 0, 90, 0, 0, 255, 255, 0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255, 0, 60, 0, 91, 0, 0, 255, 255, 0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255, }, fsggml.TensorTypeQ8_0: { 208, 51, 0, 4, 8, 12, 16, 20, 25, 29, 33, 37, 41, 45, 49, 53, 57, 61, 66, 70, 74, 78, 82, 86, 90, 94, 98, 102, 107, 111, 115, 119, 123, 127, 240, 55, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99, 101, 103, 105, 107, 109, 111, 113, 115, 117, 119, 121, 123, 125, 127, 252, 57, 86, 87, 88, 90, 91, 92, 94, 95, 96, 98, 99, 100, 102, 103, 104, 106, 107, 108, 110, 111, 112, 114, 115, 116, 118, 119, 120, 122, 123, 124, 126, 127, 0, 60, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 2, 61, 102, 103, 104, 105, 105, 106, 107, 108, 109, 109, 110, 111, 112, 113, 113, 114, 115, 116, 117, 117, 118, 119, 120, 121, 121, 122, 123, 124, 125, 125, 126, 127, 4, 62, 106, 107, 108, 108, 109, 110, 110, 111, 112, 112, 113, 114, 114, 115, 116, 116, 117, 118, 118, 119, 120, 120, 121, 122, 122, 123, 124, 124, 125, 126, 126, 127, 6, 63, 109, 110, 110, 111, 112, 112, 113, 113, 114, 114, 115, 116, 116, 117, 117, 118, 118, 119, 120, 120, 121, 121, 122, 122, 123, 124, 124, 125, 125, 126, 126, 127, 4, 64, 112, 112, 113, 113, 114, 114, 115, 115, 116, 116, 117, 117, 118, 118, 119, 119, 120, 120, 121, 121, 122, 122, 123, 123, 124, 124, 125, 125, 126, 126, 127, 127, }, fsggml.TensorTypeBF16: { 0, 0, 128, 63, 0, 64, 64, 64, 128, 64, 160, 64, 192, 64, 224, 64, 0, 65, 16, 65, 32, 65, 48, 65, 64, 65, 80, 65, 96, 65, 112, 65, 128, 65, 136, 65, 144, 65, 152, 65, 160, 65, 168, 65, 176, 65, 184, 65, 192, 65, 200, 65, 208, 65, 216, 65, 224, 65, 232, 65, 240, 65, 248, 65, 0, 66, 4, 66, 8, 66, 12, 66, 16, 66, 20, 66, 24, 66, 28, 66, 32, 66, 36, 66, 40, 66, 44, 66, 48, 66, 52, 66, 56, 66, 60, 66, 64, 66, 68, 66, 72, 66, 76, 66, 80, 66, 84, 66, 88, 66, 92, 66, 96, 66, 100, 66, 104, 66, 108, 66, 112, 66, 116, 66, 120, 66, 124, 66, 128, 66, 130, 66, 132, 66, 134, 66, 136, 66, 138, 66, 140, 66, 142, 66, 144, 66, 146, 66, 148, 66, 150, 66, 152, 66, 154, 66, 156, 66, 158, 66, 160, 66, 162, 66, 164, 66, 166, 66, 168, 66, 170, 66, 172, 66, 174, 66, 176, 66, 178, 66, 180, 66, 182, 66, 184, 66, 186, 66, 188, 66, 190, 66, 192, 66, 194, 66, 196, 66, 198, 66, 200, 66, 202, 66, 204, 66, 206, 66, 208, 66, 210, 66, 212, 66, 214, 66, 216, 66, 218, 66, 220, 66, 222, 66, 224, 66, 226, 66, 228, 66, 230, 66, 232, 66, 234, 66, 236, 66, 238, 66, 240, 66, 242, 66, 244, 66, 246, 66, 248, 66, 250, 66, 252, 66, 254, 66, 0, 67, 1, 67, 2, 67, 3, 67, 4, 67, 5, 67, 6, 67, 7, 67, 8, 67, 9, 67, 10, 67, 11, 67, 12, 67, 13, 67, 14, 67, 15, 67, 16, 67, 17, 67, 18, 67, 19, 67, 20, 67, 21, 67, 22, 67, 23, 67, 24, 67, 25, 67, 26, 67, 27, 67, 28, 67, 29, 67, 30, 67, 31, 67, 32, 67, 33, 67, 34, 67, 35, 67, 36, 67, 37, 67, 38, 67, 39, 67, 40, 67, 41, 67, 42, 67, 43, 67, 44, 67, 45, 67, 46, 67, 47, 67, 48, 67, 49, 67, 50, 67, 51, 67, 52, 67, 53, 67, 54, 67, 55, 67, 56, 67, 57, 67, 58, 67, 59, 67, 60, 67, 61, 67, 62, 67, 63, 67, 64, 67, 65, 67, 66, 67, 67, 67, 68, 67, 69, 67, 70, 67, 71, 67, 72, 67, 73, 67, 74, 67, 75, 67, 76, 67, 77, 67, 78, 67, 79, 67, 80, 67, 81, 67, 82, 67, 83, 67, 84, 67, 85, 67, 86, 67, 87, 67, 88, 67, 89, 67, 90, 67, 91, 67, 92, 67, 93, 67, 94, 67, 95, 67, 96, 67, 97, 67, 98, 67, 99, 67, 100, 67, 101, 67, 102, 67, 103, 67, 104, 67, 105, 67, 106, 67, 107, 67, 108, 67, 109, 67, 110, 67, 111, 67, 112, 67, 113, 67, 114, 67, 115, 67, 116, 67, 117, 67, 118, 67, 119, 67, 120, 67, 121, 67, 122, 67, 123, 67, 124, 67, 125, 67, 126, 67, 127, 67, }, fsggml.TensorTypeF16: { 0, 0, 0, 60, 0, 64, 0, 66, 0, 68, 0, 69, 0, 70, 0, 71, 0, 72, 128, 72, 0, 73, 128, 73, 0, 74, 128, 74, 0, 75, 128, 75, 0, 76, 64, 76, 128, 76, 192, 76, 0, 77, 64, 77, 128, 77, 192, 77, 0, 78, 64, 78, 128, 78, 192, 78, 0, 79, 64, 79, 128, 79, 192, 79, 0, 80, 32, 80, 64, 80, 96, 80, 128, 80, 160, 80, 192, 80, 224, 80, 0, 81, 32, 81, 64, 81, 96, 81, 128, 81, 160, 81, 192, 81, 224, 81, 0, 82, 32, 82, 64, 82, 96, 82, 128, 82, 160, 82, 192, 82, 224, 82, 0, 83, 32, 83, 64, 83, 96, 83, 128, 83, 160, 83, 192, 83, 224, 83, 0, 84, 16, 84, 32, 84, 48, 84, 64, 84, 80, 84, 96, 84, 112, 84, 128, 84, 144, 84, 160, 84, 176, 84, 192, 84, 208, 84, 224, 84, 240, 84, 0, 85, 16, 85, 32, 85, 48, 85, 64, 85, 80, 85, 96, 85, 112, 85, 128, 85, 144, 85, 160, 85, 176, 85, 192, 85, 208, 85, 224, 85, 240, 85, 0, 86, 16, 86, 32, 86, 48, 86, 64, 86, 80, 86, 96, 86, 112, 86, 128, 86, 144, 86, 160, 86, 176, 86, 192, 86, 208, 86, 224, 86, 240, 86, 0, 87, 16, 87, 32, 87, 48, 87, 64, 87, 80, 87, 96, 87, 112, 87, 128, 87, 144, 87, 160, 87, 176, 87, 192, 87, 208, 87, 224, 87, 240, 87, 0, 88, 8, 88, 16, 88, 24, 88, 32, 88, 40, 88, 48, 88, 56, 88, 64, 88, 72, 88, 80, 88, 88, 88, 96, 88, 104, 88, 112, 88, 120, 88, 128, 88, 136, 88, 144, 88, 152, 88, 160, 88, 168, 88, 176, 88, 184, 88, 192, 88, 200, 88, 208, 88, 216, 88, 224, 88, 232, 88, 240, 88, 248, 88, 0, 89, 8, 89, 16, 89, 24, 89, 32, 89, 40, 89, 48, 89, 56, 89, 64, 89, 72, 89, 80, 89, 88, 89, 96, 89, 104, 89, 112, 89, 120, 89, 128, 89, 136, 89, 144, 89, 152, 89, 160, 89, 168, 89, 176, 89, 184, 89, 192, 89, 200, 89, 208, 89, 216, 89, 224, 89, 232, 89, 240, 89, 248, 89, 0, 90, 8, 90, 16, 90, 24, 90, 32, 90, 40, 90, 48, 90, 56, 90, 64, 90, 72, 90, 80, 90, 88, 90, 96, 90, 104, 90, 112, 90, 120, 90, 128, 90, 136, 90, 144, 90, 152, 90, 160, 90, 168, 90, 176, 90, 184, 90, 192, 90, 200, 90, 208, 90, 216, 90, 224, 90, 232, 90, 240, 90, 248, 90, 0, 91, 8, 91, 16, 91, 24, 91, 32, 91, 40, 91, 48, 91, 56, 91, 64, 91, 72, 91, 80, 91, 88, 91, 96, 91, 104, 91, 112, 91, 120, 91, 128, 91, 136, 91, 144, 91, 152, 91, 160, 91, 168, 91, 176, 91, 184, 91, 192, 91, 200, 91, 208, 91, 216, 91, 224, 91, 232, 91, 240, 91, 248, 91, }, fsggml.TensorTypeF32: { 0, 0, 0, 0, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64, 0, 0, 128, 64, 0, 0, 160, 64, 0, 0, 192, 64, 0, 0, 224, 64, 0, 0, 0, 65, 0, 0, 16, 65, 0, 0, 32, 65, 0, 0, 48, 65, 0, 0, 64, 65, 0, 0, 80, 65, 0, 0, 96, 65, 0, 0, 112, 65, 0, 0, 128, 65, 0, 0, 136, 65, 0, 0, 144, 65, 0, 0, 152, 65, 0, 0, 160, 65, 0, 0, 168, 65, 0, 0, 176, 65, 0, 0, 184, 65, 0, 0, 192, 65, 0, 0, 200, 65, 0, 0, 208, 65, 0, 0, 216, 65, 0, 0, 224, 65, 0, 0, 232, 65, 0, 0, 240, 65, 0, 0, 248, 65, 0, 0, 0, 66, 0, 0, 4, 66, 0, 0, 8, 66, 0, 0, 12, 66, 0, 0, 16, 66, 0, 0, 20, 66, 0, 0, 24, 66, 0, 0, 28, 66, 0, 0, 32, 66, 0, 0, 36, 66, 0, 0, 40, 66, 0, 0, 44, 66, 0, 0, 48, 66, 0, 0, 52, 66, 0, 0, 56, 66, 0, 0, 60, 66, 0, 0, 64, 66, 0, 0, 68, 66, 0, 0, 72, 66, 0, 0, 76, 66, 0, 0, 80, 66, 0, 0, 84, 66, 0, 0, 88, 66, 0, 0, 92, 66, 0, 0, 96, 66, 0, 0, 100, 66, 0, 0, 104, 66, 0, 0, 108, 66, 0, 0, 112, 66, 0, 0, 116, 66, 0, 0, 120, 66, 0, 0, 124, 66, 0, 0, 128, 66, 0, 0, 130, 66, 0, 0, 132, 66, 0, 0, 134, 66, 0, 0, 136, 66, 0, 0, 138, 66, 0, 0, 140, 66, 0, 0, 142, 66, 0, 0, 144, 66, 0, 0, 146, 66, 0, 0, 148, 66, 0, 0, 150, 66, 0, 0, 152, 66, 0, 0, 154, 66, 0, 0, 156, 66, 0, 0, 158, 66, 0, 0, 160, 66, 0, 0, 162, 66, 0, 0, 164, 66, 0, 0, 166, 66, 0, 0, 168, 66, 0, 0, 170, 66, 0, 0, 172, 66, 0, 0, 174, 66, 0, 0, 176, 66, 0, 0, 178, 66, 0, 0, 180, 66, 0, 0, 182, 66, 0, 0, 184, 66, 0, 0, 186, 66, 0, 0, 188, 66, 0, 0, 190, 66, 0, 0, 192, 66, 0, 0, 194, 66, 0, 0, 196, 66, 0, 0, 198, 66, 0, 0, 200, 66, 0, 0, 202, 66, 0, 0, 204, 66, 0, 0, 206, 66, 0, 0, 208, 66, 0, 0, 210, 66, 0, 0, 212, 66, 0, 0, 214, 66, 0, 0, 216, 66, 0, 0, 218, 66, 0, 0, 220, 66, 0, 0, 222, 66, 0, 0, 224, 66, 0, 0, 226, 66, 0, 0, 228, 66, 0, 0, 230, 66, 0, 0, 232, 66, 0, 0, 234, 66, 0, 0, 236, 66, 0, 0, 238, 66, 0, 0, 240, 66, 0, 0, 242, 66, 0, 0, 244, 66, 0, 0, 246, 66, 0, 0, 248, 66, 0, 0, 250, 66, 0, 0, 252, 66, 0, 0, 254, 66, 0, 0, 0, 67, 0, 0, 1, 67, 0, 0, 2, 67, 0, 0, 3, 67, 0, 0, 4, 67, 0, 0, 5, 67, 0, 0, 6, 67, 0, 0, 7, 67, 0, 0, 8, 67, 0, 0, 9, 67, 0, 0, 10, 67, 0, 0, 11, 67, 0, 0, 12, 67, 0, 0, 13, 67, 0, 0, 14, 67, 0, 0, 15, 67, 0, 0, 16, 67, 0, 0, 17, 67, 0, 0, 18, 67, 0, 0, 19, 67, 0, 0, 20, 67, 0, 0, 21, 67, 0, 0, 22, 67, 0, 0, 23, 67, 0, 0, 24, 67, 0, 0, 25, 67, 0, 0, 26, 67, 0, 0, 27, 67, 0, 0, 28, 67, 0, 0, 29, 67, 0, 0, 30, 67, 0, 0, 31, 67, 0, 0, 32, 67, 0, 0, 33, 67, 0, 0, 34, 67, 0, 0, 35, 67, 0, 0, 36, 67, 0, 0, 37, 67, 0, 0, 38, 67, 0, 0, 39, 67, 0, 0, 40, 67, 0, 0, 41, 67, 0, 0, 42, 67, 0, 0, 43, 67, 0, 0, 44, 67, 0, 0, 45, 67, 0, 0, 46, 67, 0, 0, 47, 67, 0, 0, 48, 67, 0, 0, 49, 67, 0, 0, 50, 67, 0, 0, 51, 67, 0, 0, 52, 67, 0, 0, 53, 67, 0, 0, 54, 67, 0, 0, 55, 67, 0, 0, 56, 67, 0, 0, 57, 67, 0, 0, 58, 67, 0, 0, 59, 67, 0, 0, 60, 67, 0, 0, 61, 67, 0, 0, 62, 67, 0, 0, 63, 67, 0, 0, 64, 67, 0, 0, 65, 67, 0, 0, 66, 67, 0, 0, 67, 67, 0, 0, 68, 67, 0, 0, 69, 67, 0, 0, 70, 67, 0, 0, 71, 67, 0, 0, 72, 67, 0, 0, 73, 67, 0, 0, 74, 67, 0, 0, 75, 67, 0, 0, 76, 67, 0, 0, 77, 67, 0, 0, 78, 67, 0, 0, 79, 67, 0, 0, 80, 67, 0, 0, 81, 67, 0, 0, 82, 67, 0, 0, 83, 67, 0, 0, 84, 67, 0, 0, 85, 67, 0, 0, 86, 67, 0, 0, 87, 67, 0, 0, 88, 67, 0, 0, 89, 67, 0, 0, 90, 67, 0, 0, 91, 67, 0, 0, 92, 67, 0, 0, 93, 67, 0, 0, 94, 67, 0, 0, 95, 67, 0, 0, 96, 67, 0, 0, 97, 67, 0, 0, 98, 67, 0, 0, 99, 67, 0, 0, 100, 67, 0, 0, 101, 67, 0, 0, 102, 67, 0, 0, 103, 67, 0, 0, 104, 67, 0, 0, 105, 67, 0, 0, 106, 67, 0, 0, 107, 67, 0, 0, 108, 67, 0, 0, 109, 67, 0, 0, 110, 67, 0, 0, 111, 67, 0, 0, 112, 67, 0, 0, 113, 67, 0, 0, 114, 67, 0, 0, 115, 67, 0, 0, 116, 67, 0, 0, 117, 67, 0, 0, 118, 67, 0, 0, 119, 67, 0, 0, 120, 67, 0, 0, 121, 67, 0, 0, 122, 67, 0, 0, 123, 67, 0, 0, 124, 67, 0, 0, 125, 67, 0, 0, 126, 67, 0, 0, 127, 67, }, fsggml.TensorTypeQ4_K: { 52, 52, 0, 0, 136, 208, 216, 223, 0, 0, 0, 0, 8, 0, 8, 15, 128, 128, 129, 129, 146, 146, 147, 147, 164, 164, 165, 165, 166, 182, 183, 183, 184, 200, 201, 201, 202, 218, 218, 219, 219, 236, 236, 237, 237, 254, 254, 255, 202, 202, 202, 203, 203, 203, 219, 219, 219, 220, 220, 220, 220, 220, 236, 237, 237, 237, 237, 237, 237, 237, 238, 254, 254, 254, 254, 254, 255, 255, 255, 255, 220, 220, 220, 220, 221, 221, 221, 221, 221, 221, 221, 237, 237, 237, 238, 238, 238, 238, 238, 238, 238, 238, 238, 254, 254, 255, 255, 255, 255, 255, 255, 255, 237, 237, 237, 237, 237, 237, 237, 238, 238, 238, 238, 238, 238, 238, 238, 238, 254, 254, 254, 254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }, fsggml.TensorTypeQ2_K: { 1, 2, 3, 3, 4, 5, 7, 7, 8, 9, 10, 11, 12, 13, 14, 15, 184, 184, 184, 185, 249, 249, 249, 249, 249, 250, 250, 254, 254, 254, 254, 255, 253, 253, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 171, 69, 0, 0, }, fsggml.TensorTypeQ5_K: { 32, 48, 0, 0, 136, 208, 216, 223, 0, 0, 0, 0, 8, 0, 7, 15, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 1, 2, 19, 20, 37, 38, 55, 56, 73, 74, 91, 92, 109, 110, 127, 112, 128, 129, 146, 147, 164, 165, 182, 183, 200, 201, 218, 219, 236, 237, 254, 133, 133, 149, 150, 150, 150, 167, 167, 167, 168, 184, 184, 185, 185, 201, 202, 202, 202, 219, 219, 219, 219, 236, 236, 236, 237, 253, 253, 254, 254, 254, 255, 169, 169, 169, 169, 186, 186, 186, 186, 186, 187, 187, 203, 203, 203, 204, 204, 204, 220, 220, 221, 221, 221, 221, 237, 237, 238, 238, 238, 238, 254, 255, 255, 203, 203, 203, 204, 204, 204, 204, 204, 220, 220, 220, 221, 221, 221, 221, 221, 237, 237, 238, 238, 238, 238, 238, 238, 254, 255, 255, 255, 255, 255, 255, 255, }, fsggml.TensorTypeQ6_K: { 96, 110, 92, 90, 88, 70, 68, 50, 48, 46, 44, 42, 24, 22, 4, 2, 80, 95, 78, 77, 76, 59, 58, 57, 40, 39, 38, 21, 20, 19, 2, 1, 75, 75, 74, 57, 57, 56, 55, 39, 38, 37, 21, 20, 20, 19, 2, 2, 72, 55, 55, 54, 54, 37, 37, 36, 36, 19, 19, 18, 18, 1, 1, 0, 35, 35, 35, 35, 34, 18, 18, 18, 17, 17, 17, 1, 1, 0, 0, 0, 35, 35, 34, 34, 18, 18, 18, 17, 17, 17, 17, 1, 0, 0, 0, 0, 35, 35, 35, 19, 19, 18, 18, 18, 18, 18, 1, 1, 1, 1, 1, 1, 34, 34, 18, 18, 18, 18, 17, 17, 17, 17, 1, 1, 0, 0, 0, 0, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 248, 240, 231, 224, 216, 208, 200, 192, 184, 176, 166, 160, 152, 144, 136, 128, 235, 43, }, fsggml.TensorTypeQ3_K: { 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 20, 23, 23, 7, 7, 6, 6, 6, 2, 1, 1, 1, 1, 0, 0, 22, 22, 6, 6, 5, 5, 5, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 238, 204, 170, 136, 102, 68, 34, 1, 5, 5, 5, 5, 189, 63, }, } )
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/server/internal/cache/blob/cache.go
server/internal/cache/blob/cache.go
// Package blob implements a content-addressable disk cache for blobs and // manifests. package blob import ( "bytes" "crypto/sha256" "errors" "fmt" "hash" "io" "io/fs" "iter" "os" "path/filepath" "strings" "time" "github.com/ollama/ollama/server/internal/internal/names" ) // Entry contains metadata about a blob in the cache. type Entry struct { Digest Digest Size int64 Time time.Time // when added to the cache } // DiskCache caches blobs and manifests on disk. // // The cache is rooted at a directory, which is created if it does not exist. // // Blobs are stored in the "blobs" subdirectory, and manifests are stored in the // "manifests" subdirectory. A example directory structure might look like: // // <dir>/ // blobs/ // sha256-<digest> - <blob data> // manifests/ // <host>/ // <namespace>/ // <name>/ // <tag> - <manifest data> // // The cache is safe for concurrent use. // // Name casing is preserved in the cache, but is not significant when resolving // names. For example, "Foo" and "foo" are considered the same name. // // The cache is not safe for concurrent use. It guards concurrent writes, but // does not prevent duplicated effort. Because blobs are immutable, duplicate // writes should result in the same file being written to disk. type DiskCache struct { // Dir specifies the top-level directory where blobs and manifest // pointers are stored. dir string now func() time.Time testHookBeforeFinalWrite func(f *os.File) } // PutBytes is a convenience function for c.Put(d, strings.NewReader(s), int64(len(s))). func PutBytes[S string | []byte](c *DiskCache, d Digest, data S) error { return c.Put(d, bytes.NewReader([]byte(data)), int64(len(data))) } // Open opens a cache rooted at the given directory. If the directory does not // exist, it is created. If the directory is not a directory, an error is // returned. func Open(dir string) (*DiskCache, error) { if dir == "" { return nil, errors.New("blob: empty directory name") } info, err := os.Stat(dir) if err == nil && !info.IsDir() { return nil, fmt.Errorf("%q is not a directory", dir) } if err := os.MkdirAll(dir, 0o777); err != nil { return nil, err } subdirs := []string{"blobs", "manifests"} for _, subdir := range subdirs { if err := os.MkdirAll(filepath.Join(dir, subdir), 0o777); err != nil { return nil, err } } // TODO(bmizerany): support shards c := &DiskCache{ dir: dir, now: time.Now, } return c, nil } func readAndSum(filename string, limit int64) (data []byte, _ Digest, err error) { f, err := os.Open(filename) if err != nil { return nil, Digest{}, err } defer f.Close() h := sha256.New() r := io.TeeReader(f, h) data, err = io.ReadAll(io.LimitReader(r, limit)) if err != nil { return nil, Digest{}, err } var d Digest h.Sum(d.sum[:0]) return data, d, nil } //lint:ignore U1000 used for debugging purposes as needed in tests var debug = false // debugger returns a function that can be used to add a step to the error message. // The error message will be a list of steps that were taken before the error occurred. // The steps are added in the order they are called. // // To set the error message, call the returned function with an empty string. // //lint:ignore U1000 used for debugging purposes as needed in tests func debugger(err *error) func(step string) { if !debug { return func(string) {} } var steps []string return func(step string) { if step == "" && *err != nil { *err = fmt.Errorf("%q: %w", steps, *err) return } steps = append(steps, step) if len(steps) > 100 { // shift hints in case of a bug that causes a lot of hints copy(steps, steps[1:]) steps = steps[:100] } } } // Resolve resolves a name to a digest. The name is expected to // be in either of the following forms: // // @<digest> // <name>@<digest> // <name> // // If a digest is provided, it is returned as is and nothing else happens. // // If a name is provided for a manifest that exists in the cache, the digest // of the manifest is returned. If there is no manifest in the cache, it // returns [fs.ErrNotExist]. // // To cover the case where a manifest may change without the cache knowing // (e.g. it was reformatted or modified by hand), the manifest data read and // hashed is passed to a PutBytes call to ensure that the manifest is in the // blob store. This is done to ensure that future calls to [Get] succeed in // these cases. func (c *DiskCache) Resolve(name string) (Digest, error) { name, digest := splitNameDigest(name) if digest != "" { return ParseDigest(digest) } // We want to address manifests files by digest using Get. That requires // them to be blobs. This cannot be directly accomplished by looking in // the blob store because manifests can change without Ollama knowing // (e.g. a user modifies a manifests by hand then pushes it to update // their model). We also need to support the blob caches inherited from // older versions of Ollama, which do not store manifests in the blob // store, so for these cases, we need to handle adding the manifests to // the blob store, just in time. // // So now we read the manifests file, hash it, and copy it to the blob // store if it's not already there. // // This should be cheap because manifests are small, and accessed // infrequently. file, err := c.manifestPath(name) if err != nil { return Digest{}, err } data, d, err := readAndSum(file, 1<<20) if err != nil { return Digest{}, err } // Ideally we'd read the "manifest" file as a manifest to the blob file, // but we are not changing this yet, so copy the manifest to the blob // store so it can be addressed by digest subsequent calls to Get. if err := PutBytes(c, d, data); err != nil { return Digest{}, err } return d, nil } // Put writes a new blob to the cache, identified by its digest. The operation // reads content from r, which must precisely match both the specified size and // digest. // // Concurrent write safety is achieved through file locking. The implementation // guarantees write integrity by enforcing size limits and content validation // before allowing the file to reach its final state. func (c *DiskCache) Put(d Digest, r io.Reader, size int64) error { return c.copyNamedFile(c.GetFile(d), r, d, size) } // Import imports a blob from the provided reader into the cache. It reads the // entire content of the reader, calculates its digest, and stores it in the // cache. // // Import should be considered unsafe for use with untrusted data, such as data // read from a network. The caller is responsible for ensuring the integrity of // the data being imported. func (c *DiskCache) Import(r io.Reader, size int64) (Digest, error) { // users that want to change the temp dir can set TEMPDIR. f, err := os.CreateTemp("", "blob-") if err != nil { return Digest{}, err } defer os.Remove(f.Name()) // Copy the blob to a temporary file. h := sha256.New() r = io.TeeReader(r, h) n, err := io.Copy(f, r) if err != nil { return Digest{}, err } if n != size { return Digest{}, fmt.Errorf("blob: expected %d bytes, got %d", size, n) } // Check the digest. var d Digest h.Sum(d.sum[:0]) if err := f.Close(); err != nil { return Digest{}, err } name := c.GetFile(d) // Rename the temporary file to the final file. if err := os.Rename(f.Name(), name); err != nil { return Digest{}, err } os.Chtimes(name, c.now(), c.now()) // mainly for tests return d, nil } // Get retrieves a blob from the cache using the provided digest. The operation // fails if the digest is malformed or if any errors occur during blob // retrieval. func (c *DiskCache) Get(d Digest) (Entry, error) { name := c.GetFile(d) info, err := os.Stat(name) if err != nil { return Entry{}, err } if info.Size() == 0 { return Entry{}, fs.ErrNotExist } return Entry{ Digest: d, Size: info.Size(), Time: info.ModTime(), }, nil } // Link creates a symbolic reference in the cache that maps the provided name // to a blob identified by its digest, making it retrievable by name using // [Resolve]. // // It returns an error if either the name or digest is invalid, or if link // creation encounters any issues. func (c *DiskCache) Link(name string, d Digest) error { manifest, err := c.manifestPath(name) if err != nil { return err } f, err := os.OpenFile(c.GetFile(d), os.O_RDONLY, 0) if err != nil { return err } defer f.Close() // TODO(bmizerany): test this happens only if the blob was found to // avoid leaving debris if err := os.MkdirAll(filepath.Dir(manifest), 0o777); err != nil { return err } info, err := f.Stat() if err != nil { return err } // Copy manifest to cache directory. return c.copyNamedFile(manifest, f, d, info.Size()) } // Unlink unlinks the manifest by name from the cache. If the name is not // found. If a manifest is removed ok will be true, otherwise false. If an // error occurs, it returns ok false, and the error. func (c *DiskCache) Unlink(name string) (ok bool, _ error) { manifest, err := c.manifestPath(name) if err != nil { return false, err } err = os.Remove(manifest) if errors.Is(err, fs.ErrNotExist) { return false, nil } return true, err } // GetFile returns the absolute path to the file, in the cache, for the given // digest. It does not check if the file exists. // // The returned path should not be stored, used outside the lifetime of the // cache, or interpreted in any way. func (c *DiskCache) GetFile(d Digest) string { filename := fmt.Sprintf("sha256-%x", d.sum) return absJoin(c.dir, "blobs", filename) } // Links returns a sequence of link names. The sequence is in lexical order. // Names are converted from their relative path form to their name form but are // not guaranteed to be valid. Callers should validate the names before using. func (c *DiskCache) Links() iter.Seq2[string, error] { return func(yield func(string, error) bool) { for path, err := range c.links() { if err != nil { yield("", err) return } if !yield(pathToName(path), nil) { return } } } } // pathToName converts a path to a name. It is the inverse of nameToPath. The // path is assumed to be in filepath.ToSlash format. func pathToName(s string) string { s = strings.TrimPrefix(s, "manifests/") rr := []rune(s) for i := len(rr) - 1; i > 0; i-- { if rr[i] == '/' { rr[i] = ':' return string(rr) } } return s } // manifestPath finds the first manifest file on disk that matches the given // name using a case-insensitive comparison. If no manifest file is found, it // returns the path where the manifest file would be if it existed. // // If two manifest files exists on disk that match the given name using a // case-insensitive comparison, the one that sorts first, lexically, is // returned. func (c *DiskCache) manifestPath(name string) (string, error) { np, err := nameToPath(name) if err != nil { return "", err } maybe := filepath.Join("manifests", np) for l, err := range c.links() { if err != nil { return "", err } if strings.EqualFold(maybe, l) { return filepath.Join(c.dir, l), nil } } return filepath.Join(c.dir, maybe), nil } // links returns a sequence of links in the cache in lexical order. func (c *DiskCache) links() iter.Seq2[string, error] { // TODO(bmizerany): reuse empty dirnames if exist return func(yield func(string, error) bool) { fsys := os.DirFS(c.dir) manifests, err := fs.Glob(fsys, "manifests/*/*/*/*") if err != nil { yield("", err) return } for _, manifest := range manifests { if !yield(manifest, nil) { return } } } } type checkWriter struct { size int64 d Digest f *os.File h hash.Hash w io.Writer // underlying writer; set by creator n int64 err error testHookBeforeFinalWrite func(*os.File) } func (w *checkWriter) seterr(err error) error { if w.err == nil { w.err = err } return err } // Write writes p to the underlying hash and writer. The last write to the // underlying writer is guaranteed to be the last byte of p as verified by the // hash. func (w *checkWriter) Write(p []byte) (int, error) { if w.err != nil { return 0, w.err } _, err := w.h.Write(p) if err != nil { return 0, w.seterr(err) } nextSize := w.n + int64(len(p)) if nextSize == w.size { // last write. check hash. sum := w.h.Sum(nil) if !bytes.Equal(sum, w.d.sum[:]) { return 0, w.seterr(fmt.Errorf("file content changed underfoot")) } if w.testHookBeforeFinalWrite != nil { w.testHookBeforeFinalWrite(w.f) } } if nextSize > w.size { return 0, w.seterr(fmt.Errorf("content exceeds expected size: %d > %d", nextSize, w.size)) } n, err := w.w.Write(p) w.n += int64(n) return n, w.seterr(err) } // copyNamedFile copies file into name, expecting it to have the given Digest // and size, if that file is not present already. func (c *DiskCache) copyNamedFile(name string, file io.Reader, out Digest, size int64) error { info, err := os.Stat(name) if err == nil && info.Size() == size { // File already exists with correct size. This is good enough. // We can skip expensive hash checks. // // TODO: Do the hash check, but give caller a way to skip it. return nil } // Copy file to cache directory. mode := os.O_RDWR | os.O_CREATE if err == nil && info.Size() > size { // shouldn't happen but fix in case mode |= os.O_TRUNC } f, err := os.OpenFile(name, mode, 0o666) if err != nil { return err } defer f.Close() if size == 0 { // File now exists with correct size. // Only one possible zero-length file, so contents are OK too. // Early return here makes sure there's a "last byte" for code below. return nil } // From here on, if any of the I/O writing the file fails, // we make a best-effort attempt to truncate the file f // before returning, to avoid leaving bad bytes in the file. // Copy file to f, but also into h to double-check hash. cw := &checkWriter{ d: out, size: size, h: sha256.New(), f: f, w: f, testHookBeforeFinalWrite: c.testHookBeforeFinalWrite, } n, err := io.Copy(cw, file) if err != nil { f.Truncate(0) return err } if n < size { f.Truncate(0) return io.ErrUnexpectedEOF } if err := f.Close(); err != nil { // Data might not have been written, // but file may look like it is the right size. // To be extra careful, remove cached file. os.Remove(name) return err } os.Chtimes(name, c.now(), c.now()) // mainly for tests return nil } func splitNameDigest(s string) (name, digest string) { i := strings.LastIndexByte(s, '@') if i < 0 { return s, "" } return s[:i], s[i+1:] } var errInvalidName = errors.New("invalid name") func nameToPath(name string) (_ string, err error) { n := names.Parse(name) if !n.IsFullyQualified() { return "", errInvalidName } return filepath.Join(n.Host(), n.Namespace(), n.Model(), n.Tag()), nil } func absJoin(pp ...string) string { abs, err := filepath.Abs(filepath.Join(pp...)) if err != nil { panic(err) // this should never happen } return abs }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/server/internal/cache/blob/cache_test.go
server/internal/cache/blob/cache_test.go
package blob import ( "crypto/sha256" "errors" "fmt" "io" "io/fs" "os" "path/filepath" "slices" "strings" "testing" "time" "github.com/ollama/ollama/server/internal/testutil" ) func init() { debug = true } var epoch = func() time.Time { d := time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC) if d.IsZero() { panic("time zero") } return d }() func TestOpenErrors(t *testing.T) { exe, err := os.Executable() if err != nil { panic(err) } cases := []struct { dir string err string }{ {t.TempDir(), ""}, {"", "empty directory name"}, {exe, "not a directory"}, } for _, tt := range cases { t.Run(tt.dir, func(t *testing.T) { _, err := Open(tt.dir) if tt.err == "" { if err != nil { t.Fatal(err) } return } if err == nil { t.Fatal("expected error") } if !strings.Contains(err.Error(), tt.err) { t.Fatalf("err = %v, want %q", err, tt.err) } }) } } func TestGetFile(t *testing.T) { t.Chdir(t.TempDir()) c, err := Open(".") if err != nil { t.Fatal(err) } d := mkdigest("1") got := c.GetFile(d) cleaned := filepath.Clean(got) if cleaned != got { t.Fatalf("got is unclean: %q", got) } if !filepath.IsAbs(got) { t.Fatal("got is not absolute") } abs, _ := filepath.Abs(c.dir) if !strings.HasPrefix(got, abs) { t.Fatalf("got is not local to %q", c.dir) } } func TestBasic(t *testing.T) { c, err := Open(t.TempDir()) if err != nil { t.Fatal(err) } now := epoch c.now = func() time.Time { return now } checkEntry := entryChecker(t, c) checkFailed := func(err error) { if err == nil { t.Helper() t.Fatal("expected error") } } _, err = c.Resolve("invalid") checkFailed(err) _, err = c.Resolve("h/n/m:t") checkFailed(err) dx := mkdigest("x") d, err := c.Resolve(fmt.Sprintf("h/n/m:t@%s", dx)) if err != nil { t.Fatal(err) } if d != dx { t.Fatalf("d = %v, want %v", d, dx) } _, err = c.Get(Digest{}) checkFailed(err) // not committed yet _, err = c.Get(dx) checkFailed(err) err = PutBytes(c, dx, "!") checkFailed(err) err = PutBytes(c, dx, "x") if err != nil { t.Fatal(err) } checkEntry(dx, 1, now) t0 := now now = now.Add(1*time.Hour + 1*time.Minute) err = PutBytes(c, dx, "x") if err != nil { t.Fatal(err) } // check not updated checkEntry(dx, 1, t0) } type sleepFunc func(d time.Duration) time.Time func openTester(t *testing.T) (*DiskCache, sleepFunc) { t.Helper() c, err := Open(t.TempDir()) if err != nil { t.Fatal(err) } now := epoch c.now = func() time.Time { return now } return c, func(d time.Duration) time.Time { now = now.Add(d) return now } } func TestManifestPath(t *testing.T) { check := testutil.Checker(t) c, sleep := openTester(t) d1 := mkdigest("1") err := PutBytes(c, d1, "1") check(err) err = c.Link("h/n/m:t", d1) check(err) t0 := sleep(0) sleep(1 * time.Hour) err = c.Link("h/n/m:t", d1) // nop expected check(err) file := must(c.manifestPath("h/n/m:t")) info, err := os.Stat(file) check(err) testutil.CheckTime(t, info.ModTime(), t0) } func TestManifestExistsWithoutBlob(t *testing.T) { t.Chdir(t.TempDir()) check := testutil.Checker(t) c, err := Open(".") check(err) checkEntry := entryChecker(t, c) man := must(c.manifestPath("h/n/m:t")) os.MkdirAll(filepath.Dir(man), 0o777) testutil.WriteFile(t, man, "1") got, err := c.Resolve("h/n/m:t") check(err) want := mkdigest("1") if got != want { t.Fatalf("got = %v, want %v", got, want) } e, err := c.Get(got) check(err) checkEntry(got, 1, e.Time) } func TestPut(t *testing.T) { c, sleep := openTester(t) check := testutil.Checker(t) checkEntry := entryChecker(t, c) d := mkdigest("hello, world") err := PutBytes(c, d, "hello") if err == nil { t.Fatal("expected error") } got, err := c.Get(d) if !errors.Is(err, fs.ErrNotExist) { t.Fatalf("expected error, got %v", got) } // Put a valid blob err = PutBytes(c, d, "hello, world") check(err) checkEntry(d, 12, sleep(0)) // Put a blob with content that does not hash to the digest err = PutBytes(c, d, "hello") if err == nil { t.Fatal("expected error") } checkNotExists(t, c, d) // Put the valid blob back and check it err = PutBytes(c, d, "hello, world") check(err) checkEntry(d, 12, sleep(0)) // Put a blob that errors during Read err = c.Put(d, &errOnBangReader{s: "!"}, 1) if err == nil { t.Fatal("expected error") } checkNotExists(t, c, d) // Put valid blob back and check it err = PutBytes(c, d, "hello, world") check(err) checkEntry(d, 12, sleep(0)) // Put a blob with mismatched size err = c.Put(d, strings.NewReader("hello, world"), 11) if err == nil { t.Fatal("expected error") } checkNotExists(t, c, d) // Final byte does not match the digest (testing commit phase) err = PutBytes(c, d, "hello, world$") if err == nil { t.Fatal("expected error") } checkNotExists(t, c, d) reset := c.setTestHookBeforeFinalWrite(func(f *os.File) { // change mode to read-only f.Truncate(0) f.Chmod(0o400) f.Close() f1, err := os.OpenFile(f.Name(), os.O_RDONLY, 0) if err != nil { t.Fatal(err) } t.Cleanup(func() { f1.Close() }) *f = *f1 }) defer reset() err = PutBytes(c, d, "hello, world") if err == nil { t.Fatal("expected error") } checkNotExists(t, c, d) reset() } func TestImport(t *testing.T) { c, _ := openTester(t) checkEntry := entryChecker(t, c) want := mkdigest("x") got, err := c.Import(strings.NewReader("x"), 1) if err != nil { t.Fatal(err) } if want != got { t.Fatalf("digest = %v, want %v", got, want) } checkEntry(want, 1, epoch) got, err = c.Import(strings.NewReader("x"), 1) if err != nil { t.Fatal(err) } if want != got { t.Fatalf("digest = %v, want %v", got, want) } checkEntry(want, 1, epoch) } func (c *DiskCache) setTestHookBeforeFinalWrite(h func(*os.File)) (reset func()) { old := c.testHookBeforeFinalWrite c.testHookBeforeFinalWrite = h return func() { c.testHookBeforeFinalWrite = old } } func TestPutGetZero(t *testing.T) { c, sleep := openTester(t) check := testutil.Checker(t) checkEntry := entryChecker(t, c) d := mkdigest("x") err := PutBytes(c, d, "x") check(err) checkEntry(d, 1, sleep(0)) err = os.Truncate(c.GetFile(d), 0) check(err) _, err = c.Get(d) if !errors.Is(err, fs.ErrNotExist) { t.Fatalf("err = %v, want fs.ErrNotExist", err) } } func TestPutZero(t *testing.T) { c, _ := openTester(t) d := mkdigest("x") err := c.Put(d, strings.NewReader("x"), 0) // size == 0 (not size of content) testutil.Check(t, err) checkNotExists(t, c, d) } func TestCommit(t *testing.T) { check := testutil.Checker(t) c, err := Open(t.TempDir()) if err != nil { t.Fatal(err) } checkEntry := entryChecker(t, c) now := epoch c.now = func() time.Time { return now } d1 := mkdigest("1") err = c.Link("h/n/m:t", d1) if !errors.Is(err, fs.ErrNotExist) { t.Fatalf("err = %v, want fs.ErrNotExist", err) } err = PutBytes(c, d1, "1") check(err) err = c.Link("h/n/m:t", d1) check(err) got, err := c.Resolve("h/n/m:t") check(err) if got != d1 { t.Fatalf("d = %v, want %v", got, d1) } // commit again, more than 1 byte d2 := mkdigest("22") err = PutBytes(c, d2, "22") check(err) err = c.Link("h/n/m:t", d2) check(err) checkEntry(d2, 2, now) filename := must(c.manifestPath("h/n/m:t")) data, err := os.ReadFile(filename) check(err) if string(data) != "22" { t.Fatalf("data = %q, want %q", data, "22") } t0 := now now = now.Add(1 * time.Hour) err = c.Link("h/n/m:t", d2) // same contents; nop check(err) info, err := os.Stat(filename) check(err) testutil.CheckTime(t, info.ModTime(), t0) } func TestManifestInvalidBlob(t *testing.T) { c, _ := openTester(t) d := mkdigest("1") err := c.Link("h/n/m:t", d) if err == nil { t.Fatal("expected error") } checkNotExists(t, c, d) err = PutBytes(c, d, "1") testutil.Check(t, err) err = os.WriteFile(c.GetFile(d), []byte("invalid"), 0o666) if err != nil { t.Fatal(err) } err = c.Link("h/n/m:t", d) if !strings.Contains(err.Error(), "underfoot") { t.Fatalf("err = %v, want error to contain %q", err, "underfoot") } } func TestManifestNameReuse(t *testing.T) { t.Run("case-insensitive", func(t *testing.T) { // This should run on all file system types. testManifestNameReuse(t) }) t.Run("case-sensitive", func(t *testing.T) { useCaseInsensitiveTempDir(t) testManifestNameReuse(t) }) } func testManifestNameReuse(t *testing.T) { check := testutil.Checker(t) c, _ := openTester(t) d1 := mkdigest("1") err := PutBytes(c, d1, "1") check(err) err = c.Link("h/n/m:t", d1) check(err) d2 := mkdigest("22") err = PutBytes(c, d2, "22") check(err) err = c.Link("H/N/M:T", d2) check(err) var g [2]Digest g[0], err = c.Resolve("h/n/m:t") check(err) g[1], err = c.Resolve("H/N/M:T") check(err) w := [2]Digest{d2, d2} if g != w { t.Fatalf("g = %v, want %v", g, w) } var got []string for l, err := range c.links() { if err != nil { t.Fatal(err) } got = append(got, l) } want := []string{"manifests/h/n/m/t"} if !slices.Equal(got, want) { t.Fatalf("got = %v, want %v", got, want) } // relink with different case unlinked, err := c.Unlink("h/n/m:t") check(err) if !unlinked { t.Fatal("expected unlinked") } err = c.Link("h/n/m:T", d1) check(err) got = got[:0] for l, err := range c.links() { if err != nil { t.Fatal(err) } got = append(got, l) } // we should have only one link that is same case as the last link want = []string{"manifests/h/n/m/T"} if !slices.Equal(got, want) { t.Fatalf("got = %v, want %v", got, want) } } func TestManifestFile(t *testing.T) { cases := []struct { in string want string }{ {"", ""}, // valid names {"h/n/m:t", "/manifests/h/n/m/t"}, {"hh/nn/mm:tt", "/manifests/hh/nn/mm/tt"}, {"%/%/%/%", ""}, // already a path {"h/n/m/t", ""}, // refs are not names {"h/n/m:t@sha256-1", ""}, {"m@sha256-1", ""}, {"n/m:t@sha256-1", ""}, } c, _ := openTester(t) for _, tt := range cases { t.Run(tt.in, func(t *testing.T) { got, err := c.manifestPath(tt.in) if err != nil && tt.want != "" { t.Fatalf("unexpected error: %v", err) } if err == nil && tt.want == "" { t.Fatalf("expected error") } dir := filepath.ToSlash(c.dir) got = filepath.ToSlash(got) got = strings.TrimPrefix(got, dir) if got != tt.want { t.Fatalf("got = %q, want %q", got, tt.want) } }) } } func TestNames(t *testing.T) { c, _ := openTester(t) check := testutil.Checker(t) check(PutBytes(c, mkdigest("1"), "1")) check(PutBytes(c, mkdigest("2"), "2")) check(c.Link("h/n/m:t", mkdigest("1"))) check(c.Link("h/n/m:u", mkdigest("2"))) var got []string for l, err := range c.Links() { if err != nil { t.Fatal(err) } got = append(got, l) } want := []string{"h/n/m:t", "h/n/m:u"} if !slices.Equal(got, want) { t.Fatalf("got = %v, want %v", got, want) } } func mkdigest(s string) Digest { return Digest{sha256.Sum256([]byte(s))} } func checkNotExists(t *testing.T, c *DiskCache, d Digest) { t.Helper() _, err := c.Get(d) if !errors.Is(err, fs.ErrNotExist) { t.Fatalf("err = %v, want fs.ErrNotExist", err) } } func entryChecker(t *testing.T, c *DiskCache) func(Digest, int64, time.Time) { t.Helper() return func(d Digest, size int64, mod time.Time) { t.Helper() t.Run("checkEntry:"+d.String(), func(t *testing.T) { t.Helper() defer func() { if t.Failed() { dumpCacheContents(t, c) } }() e, err := c.Get(d) if size == 0 && errors.Is(err, fs.ErrNotExist) { err = nil } if err != nil { t.Fatal(err) } if e.Digest != d { t.Errorf("e.Digest = %v, want %v", e.Digest, d) } if e.Size != size { t.Fatalf("e.Size = %v, want %v", e.Size, size) } testutil.CheckTime(t, e.Time, mod) info, err := os.Stat(c.GetFile(d)) if err != nil { t.Fatal(err) } if info.Size() != size { t.Fatalf("info.Size = %v, want %v", info.Size(), size) } testutil.CheckTime(t, info.ModTime(), mod) }) } } func must[T any](v T, err error) T { if err != nil { panic(err) } return v } func TestNameToPath(t *testing.T) { _, err := nameToPath("h/n/m:t") if err != nil { t.Fatal(err) } } type errOnBangReader struct { s string n int } func (e *errOnBangReader) Read(p []byte) (int, error) { if len(p) < 1 { return 0, io.ErrShortBuffer } if e.n >= len(p) { return 0, io.EOF } if e.s[e.n] == '!' { return 0, errors.New("bang") } p[0] = e.s[e.n] e.n++ return 1, nil } func dumpCacheContents(t *testing.T, c *DiskCache) { t.Helper() var b strings.Builder fsys := os.DirFS(c.dir) fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error { t.Helper() if err != nil { return err } info, err := d.Info() if err != nil { return err } // Format like ls: // // ; ls -la // drwxr-xr-x 224 Jan 13 14:22 blob/sha256-123 // drwxr-xr-x 224 Jan 13 14:22 manifest/h/n/m fmt.Fprintf(&b, " %s % 4d %s %s\n", info.Mode(), info.Size(), info.ModTime().Format("Jan 2 15:04"), path, ) return nil }) t.Log() t.Logf("cache contents:\n%s", b.String()) }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/server/internal/cache/blob/casecheck_test.go
server/internal/cache/blob/casecheck_test.go
package blob import ( "fmt" "os" "path/filepath" "runtime" "strings" "testing" ) func isCaseSensitive(dir string) bool { defer func() { os.Remove(filepath.Join(dir, "_casecheck")) }() exists := func(file string) bool { _, err := os.Stat(file) return err == nil } file := filepath.Join(dir, "_casecheck") FILE := filepath.Join(dir, "_CASECHECK") if exists(file) || exists(FILE) { panic(fmt.Sprintf("_casecheck already exists in %q; remove and try again.", dir)) } err := os.WriteFile(file, nil, 0o666) if err != nil { panic(err) } return !exists(FILE) } func isCI() bool { return os.Getenv("CI") != "" } const volumeHint = ` Unable to locate case-insensitive TMPDIR on darwin. To run tests, create the case-insensitive volume /Volumes/data: $ sudo diskutil apfs addVolume disk1 APFSX data -mountpoint /Volumes/data or run with: CI=1 go test ./... ` // useCaseInsensitiveTempDir sets TMPDIR to a case-insensitive directory // can find one, otherwise it skips the test if the CI environment variable is // set, or GOOS is not darwin. func useCaseInsensitiveTempDir(t *testing.T) bool { if isCaseSensitive(os.TempDir()) { // Use the default temp dir if it is already case-sensitive. return true } if runtime.GOOS == "darwin" { // If darwin, check for the special case-sensitive volume and // use it if available. const volume = "/Volumes/data" _, err := os.Stat(volume) if err == nil { tmpdir := filepath.Join(volume, "tmp") os.MkdirAll(tmpdir, 0o700) t.Setenv("TMPDIR", tmpdir) return true } if isCI() { // Special case darwin in CI; it is not case-sensitive // by default, and we will be testing other platforms // that are case-sensitive, so we'll have the test // being skipped covered there. t.Skip("Skipping test in CI for darwin; TMPDIR is not case-insensitive.") } } if !isCI() { // Require devs to always tests with a case-insensitive TMPDIR. // TODO(bmizerany): Print platform-specific instructions or // link to docs on that topic. lines := strings.Split(volumeHint, "\n") for _, line := range lines { t.Skip(line) } } return false }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/server/internal/cache/blob/chunked.go
server/internal/cache/blob/chunked.go
package blob import ( "crypto/sha256" "errors" "io" "os" ) // Chunk represents a range of bytes in a blob. type Chunk struct { Start int64 End int64 } // Size returns end minus start plus one. func (c Chunk) Size() int64 { return c.End - c.Start + 1 } // Chunker writes to a blob in chunks. // Its zero value is invalid. Use [DiskCache.Chunked] to create a new Chunker. type Chunker struct { digest Digest size int64 f *os.File // nil means pre-validated } // Chunked returns a new Chunker, ready for use storing a blob of the given // size in chunks. // // Use [Chunker.Put] to write data to the blob at specific offsets. func (c *DiskCache) Chunked(d Digest, size int64) (*Chunker, error) { name := c.GetFile(d) info, err := os.Stat(name) if err == nil && info.Size() == size { return &Chunker{}, nil } f, err := os.OpenFile(name, os.O_CREATE|os.O_WRONLY, 0o666) if err != nil { return nil, err } return &Chunker{digest: d, size: size, f: f}, nil } // Put copies chunk.Size() bytes from r to the blob at the given offset, // merging the data with the existing blob. It returns an error if any. As a // special case, if r has less than chunk.Size() bytes, Put returns // io.ErrUnexpectedEOF. func (c *Chunker) Put(chunk Chunk, d Digest, r io.Reader) error { if c.f == nil { return nil } cw := &checkWriter{ d: d, size: chunk.Size(), h: sha256.New(), f: c.f, w: io.NewOffsetWriter(c.f, chunk.Start), } _, err := io.CopyN(cw, r, chunk.Size()) if err != nil && errors.Is(err, io.EOF) { return io.ErrUnexpectedEOF } return err } // Close closes the underlying file. func (c *Chunker) Close() error { return c.f.Close() }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/server/internal/cache/blob/digest.go
server/internal/cache/blob/digest.go
package blob import ( "crypto/sha256" "encoding/hex" "errors" "fmt" "slices" "strings" ) var ErrInvalidDigest = errors.New("invalid digest") // Digest is a blob identifier that is the SHA-256 hash of a blob's content. // // It is comparable and can be used as a map key. type Digest struct { sum [32]byte } // ParseDigest parses a digest from a string. If the string is not a valid // digest, a call to the returned digest's IsValid method will return false. // // The input string may be in one of two forms: // // - ("sha256-<hex>"), where <hex> is a 64-character hexadecimal string. // - ("sha256:<hex>"), where <hex> is a 64-character hexadecimal string. // // The [Digest.String] method will return the canonical form of the // digest, "sha256:<hex>". func ParseDigest[S ~[]byte | ~string](v S) (Digest, error) { s := string(v) i := strings.IndexAny(s, ":-") var zero Digest if i < 0 { return zero, ErrInvalidDigest } prefix, sum := s[:i], s[i+1:] if prefix != "sha256" || len(sum) != 64 { return zero, ErrInvalidDigest } var d Digest _, err := hex.Decode(d.sum[:], []byte(sum)) if err != nil { return zero, ErrInvalidDigest } return d, nil } func DigestFromBytes[S ~[]byte | ~string](v S) Digest { return Digest{sha256.Sum256([]byte(v))} } // String returns the string representation of the digest in the conventional // form "sha256:<hex>". func (d Digest) String() string { return fmt.Sprintf("sha256:%x", d.sum[:]) } func (d Digest) Short() string { return fmt.Sprintf("%x", d.sum[:4]) } func (d Digest) Sum() [32]byte { return d.sum } func (d Digest) Compare(other Digest) int { return slices.Compare(d.sum[:], other.sum[:]) } // IsValid returns true if the digest is valid, i.e. if it is the SHA-256 hash // of some content. func (d Digest) IsValid() bool { return d != (Digest{}) } // MarshalText implements the encoding.TextMarshaler interface. It returns an // error if [Digest.IsValid] returns false. func (d Digest) MarshalText() ([]byte, error) { return []byte(d.String()), nil } // UnmarshalText implements the encoding.TextUnmarshaler interface, and only // works for a zero digest. If [Digest.IsValid] returns true, it returns an // error. func (d *Digest) UnmarshalText(text []byte) error { if *d != (Digest{}) { return errors.New("digest: illegal UnmarshalText on valid digest") } v, err := ParseDigest(string(text)) if err != nil { return err } *d = v return nil }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/server/internal/cache/blob/digest_test.go
server/internal/cache/blob/digest_test.go
package blob import ( "encoding/json" "testing" ) func TestParseDigest(t *testing.T) { cases := []struct { in string valid bool }{ {"sha256-0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", true}, {"sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", true}, // too short {"sha256-0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde", false}, {"sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde", false}, // too long {"sha256-0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0", false}, {"sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0", false}, // invalid prefix {"sha255-0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", false}, {"sha255:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", false}, {"sha256!0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", false}, // invalid hex {"sha256-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", false}, {"sha256:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", false}, } for _, tt := range cases { got, err := ParseDigest(tt.in) if tt.valid && err != nil { t.Errorf("ParseDigest(%q) = %v, %v; want valid", tt.in, got, err) } want := "sha256:" + tt.in[7:] if tt.valid && got.String() != want { t.Errorf("ParseDigest(%q).String() = %q, want %q", tt.in, got.String(), want) } } } func TestDigestMarshalText(t *testing.T) { const s = `"sha256-0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"` var d Digest if err := json.Unmarshal([]byte(s), &d); err != nil { t.Errorf("json.Unmarshal: %v", err) } out, err := json.Marshal(d) if err != nil { t.Errorf("json.Marshal: %v", err) } want := `"sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"` if string(out) != want { t.Errorf("json.Marshal: got %s, want %s", out, want) } if err := json.Unmarshal([]byte(`"invalid"`), &Digest{}); err == nil { t.Errorf("json.Unmarshal: expected error") } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/server/internal/internal/backoff/backoff_synctest_test.go
server/internal/internal/backoff/backoff_synctest_test.go
//go:build goexperiment.synctest package backoff import ( "context" "errors" "testing" "testing/synctest" "time" ) func TestLoop(t *testing.T) { synctest.Run(func() { last := -1 ctx, cancel := context.WithCancel(t.Context()) defer cancel() for n, err := range Loop(ctx, 100*time.Millisecond) { if !errors.Is(err, ctx.Err()) { t.Errorf("err = %v, want nil", err) } if err != nil { break } if n != last+1 { t.Errorf("n = %d, want %d", n, last+1) } last = n if n > 5 { cancel() } } if last != 6 { t.Errorf("last = %d, want 6", last) } }) }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/server/internal/internal/backoff/backoff_test.go
server/internal/internal/backoff/backoff_test.go
//go:build goexperiment.synctest package backoff import ( "testing" "testing/synctest" "time" ) func TestLoopAllocs(t *testing.T) { for i := range 3 { got := testing.AllocsPerRun(1000, func() { for tick := range Loop(t.Context(), 1) { if tick >= i { break } } }) want := float64(0) if i > 0 { want = 3 // due to time.NewTimer } if got > want { t.Errorf("[%d ticks]: allocs = %v, want 0", i, want) } } } func BenchmarkLoop(b *testing.B) { ctx := b.Context() synctest.Run(func() { for n := range Loop(ctx, 100*time.Millisecond) { if n == b.N { break } } }) }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/server/internal/internal/backoff/backoff.go
server/internal/internal/backoff/backoff.go
package backoff import ( "context" "iter" "math/rand/v2" "time" ) func Loop(ctx context.Context, maxBackoff time.Duration) iter.Seq2[int, error] { var n int return func(yield func(int, error) bool) { var t *time.Timer for { if ctx.Err() != nil { yield(n, ctx.Err()) return } if !yield(n, nil) { return } n++ // n^2 backoff timer is a little smoother than the // common choice of 2^n. d := min(time.Duration(n*n)*10*time.Millisecond, maxBackoff) // Randomize the delay between 0.5-1.5 x msec, in order // to prevent accidental "thundering herd" problems. d = time.Duration(float64(d) * (rand.Float64() + 0.5)) if t == nil { t = time.NewTimer(d) } else { t.Reset(d) } select { case <-ctx.Done(): t.Stop() case <-t.C: } } } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/server/internal/internal/names/name.go
server/internal/internal/names/name.go
package names import ( "cmp" "fmt" "strings" "github.com/ollama/ollama/server/internal/internal/stringsx" ) const MaxNameLength = 350 + 1 + 80 + 1 + 80 + 1 + 80 // <host>/<namespace>/<model>:<tag> type Name struct { // Make incomparable to enfoce use of Compare / Equal for // case-insensitive comparisons. _ [0]func() h string n string m string t string } // Parse parses and assembles a Name from a name string. The // format of a valid name string is: // // s: // { host } "/" { namespace } "/" { model } ":" { tag } // { host } "/" { namespace } "/" { model } // { namespace } "/" { model } ":" { tag } // { namespace } "/" { model } // { model } ":" { tag } // { model } // host: // pattern: { alphanum | "_" } { alphanum | "_" | "-" | "." | ":" }* // length: [1, 350] // namespace: // pattern: { alphanum | "_" } { alphanum | "-" | "_" }* // length: [1, 80] // model: // pattern: { alphanum | "_" } { alphanum | "-" | "_" | "." }* // length: [1, 80] // tag: // pattern: { alphanum | "_" } { alphanum | "-" | "_" | "." }* // length: [1, 80] // // The name returned is not guaranteed to be valid. If it is not valid, the // field values are left in an undefined state. Use [Name.IsValid] to check // if the name is valid. func Parse(s string) Name { if len(s) > MaxNameLength { return Name{} } var n Name var tail string var c byte for { s, tail, c = cutLastAny(s, "/:") switch c { case ':': n.t = tail continue // look for model case '/': n.h, n.n, _ = cutLastAny(s, "/") n.m = tail return n case 0: n.m = tail return n } } } // Split splits an extended name string into its scheme, name, and digest // parts. // // Examples: // // http://ollama.com/bmizerany/smol:latest@digest // https://ollama.com/bmizerany/smol:latest // ollama.com/bmizerany/smol:latest@digest // returns "https" scheme. // model@digest // @digest func Split(s string) (scheme, name, digest string) { i := strings.Index(s, "://") if i >= 0 { scheme = s[:i] s = s[i+3:] } i = strings.LastIndex(s, "@") if i >= 0 { digest = s[i+1:] s = s[:i] } return scheme, s, digest } // Merge merges two names into a single name. Non-empty host, namespace, and // tag parts of a take precedence over fields in b. The model field is left as // is. // // The returned name is not guaranteed to be valid. Use [Name.IsValid] to check // if the name is valid. func Merge(a, b Name) Name { a.h = cmp.Or(a.h, b.h) a.n = cmp.Or(a.n, b.n) a.t = cmp.Or(a.t, b.t) return a } // IsValid returns true if the name is valid. func (n Name) IsValid() bool { if n.h != "" && !isValidPart(partHost, n.h) { return false } if n.n != "" && !isValidPart(partNamespace, n.n) { return false } if n.t != "" && !isValidPart(partTag, n.t) { return false } // at bare minimum, model must be present and valid return n.m != "" && isValidPart(partModel, n.m) } func (n Name) IsFullyQualified() bool { return n.IsValid() && n.h != "" && n.n != "" && n.m != "" && n.t != "" } const ( partHost = iota partNamespace partModel partTag ) func isValidPart(kind int, s string) bool { maxlen := 80 if kind == partHost { maxlen = 350 } if len(s) > maxlen { return false } for i := range s { if i == 0 { if !isAlphanumericOrUnderscore(s[i]) { return false } continue } switch s[i] { case '_', '-': case '.': if kind == partNamespace { return false } case ':': if kind != partHost { return false } default: if !isAlphanumericOrUnderscore(s[i]) { return false } } } return true } func isAlphanumericOrUnderscore(c byte) bool { return c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' || c >= '0' && c <= '9' || c == '_' } func (n Name) Host() string { return n.h } func (n Name) Namespace() string { return n.n } func (n Name) Model() string { return n.m } func (n Name) Tag() string { return n.t } // Compare compares n and o case-insensitively. It returns 0 if n and o are // equal, -1 if n sorts before o, and 1 if n sorts after o. func (n Name) Compare(o Name) int { return cmp.Or( stringsx.CompareFold(n.h, o.h), stringsx.CompareFold(n.n, o.n), stringsx.CompareFold(n.m, o.m), stringsx.CompareFold(n.t, o.t), ) } // String returns the fully qualified name in the format // <namespace>/<model>:<tag>. func (n Name) String() string { var b strings.Builder if n.h != "" { b.WriteString(n.h) b.WriteByte('/') } if n.n != "" { b.WriteString(n.n) b.WriteByte('/') } b.WriteString(n.m) if n.t != "" { b.WriteByte(':') b.WriteString(n.t) } return b.String() } func (n Name) GoString() string { return fmt.Sprintf("<Name %q %q %q %q>", n.h, n.n, n.m, n.t) } // cutLastAny is like strings.Cut but scans in reverse for the last character // in chars. If no character is found, before is the empty string and after is // s. The returned sep is the byte value of the character in chars if one was // found; otherwise it is 0. func cutLastAny(s, chars string) (before, after string, sep byte) { i := strings.LastIndexAny(s, chars) if i >= 0 { return s[:i], s[i+1:], s[i] } return "", s, 0 }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/server/internal/internal/names/name_test.go
server/internal/internal/names/name_test.go
package names import ( "strings" "testing" ) func TestParseName(t *testing.T) { cases := []struct { in string want Name }{ {"", Name{}}, {"m:t", Name{m: "m", t: "t"}}, {"m", Name{m: "m"}}, {"/m", Name{m: "m"}}, {"/n/m:t", Name{n: "n", m: "m", t: "t"}}, {"n/m", Name{n: "n", m: "m"}}, {"n/m:t", Name{n: "n", m: "m", t: "t"}}, {"n/m", Name{n: "n", m: "m"}}, {"n/m", Name{n: "n", m: "m"}}, {strings.Repeat("m", MaxNameLength+1), Name{}}, {"h/n/m:t", Name{h: "h", n: "n", m: "m", t: "t"}}, {"ollama.com/library/_:latest", Name{h: "ollama.com", n: "library", m: "_", t: "latest"}}, // Invalids // TODO: {"n:t/m:t", Name{}}, // TODO: {"/h/n/m:t", Name{}}, } for _, tt := range cases { t.Run(tt.in, func(t *testing.T) { got := Parse(tt.in) if got.Compare(tt.want) != 0 { t.Errorf("parseName(%q) = %#v, want %q", tt.in, got, tt.want) } }) } } func TestString(t *testing.T) { cases := []string{ "", "m:t", "m:t", "m", "n/m", "n/m:t", "n/m", "n/m", "h/n/m:t", "ollama.com/library/_:latest", // Special cased to "round trip" without the leading slash. "/m", "/n/m:t", } for _, s := range cases { t.Run(s, func(t *testing.T) { s = strings.TrimPrefix(s, "/") if g := Parse(s).String(); g != s { t.Errorf("parse(%q).String() = %q", s, g) } }) } } func TestParseExtended(t *testing.T) { cases := []struct { in string wantScheme string wantName Name wantDigest string }{ {"", "", Name{}, ""}, {"m", "", Name{m: "m"}, ""}, {"http://m", "http", Name{m: "m"}, ""}, {"http+insecure://m", "http+insecure", Name{m: "m"}, ""}, {"http://m@sha256:deadbeef", "http", Name{m: "m"}, "sha256:deadbeef"}, } for _, tt := range cases { t.Run(tt.in, func(t *testing.T) { scheme, name, digest := Split(tt.in) n := Parse(name) if scheme != tt.wantScheme || n.Compare(tt.wantName) != 0 || digest != tt.wantDigest { t.Errorf("ParseExtended(%q) = %q, %#v, %q, want %q, %#v, %q", tt.in, scheme, name, digest, tt.wantScheme, tt.wantName, tt.wantDigest) } }) } } func TestMerge(t *testing.T) { cases := []struct { a, b string want string }{ {"", "", ""}, {"m", "", "m"}, {"", "m", ""}, {"x", "y", "x"}, {"o.com/n/m:t", "o.com/n/m:t", "o.com/n/m:t"}, {"o.com/n/m:t", "o.com/n/_:t", "o.com/n/m:t"}, {"bmizerany/smol", "ollama.com/library/_:latest", "ollama.com/bmizerany/smol:latest"}, {"localhost:8080/bmizerany/smol", "ollama.com/library/_:latest", "localhost:8080/bmizerany/smol:latest"}, } for _, tt := range cases { t.Run("", func(t *testing.T) { a, b := Parse(tt.a), Parse(tt.b) got := Merge(a, b) if got.Compare(Parse(tt.want)) != 0 { t.Errorf("merge(%q, %q) = %#v, want %q", tt.a, tt.b, got, tt.want) } }) } } func TestParseStringRoundTrip(t *testing.T) { cases := []string{ "", "m", "m:t", "n/m", "n/m:t", "n/m:t", "n/m", "n/m", "h/n/m:t", "ollama.com/library/_:latest", } for _, s := range cases { t.Run(s, func(t *testing.T) { if got := Parse(s).String(); got != s { t.Errorf("parse(%q).String() = %q", s, got) } }) } } var junkName Name func BenchmarkParseName(b *testing.B) { b.ReportAllocs() for range b.N { junkName = Parse("h/n/m:t") } } const ( part80 = "88888888888888888888888888888888888888888888888888888888888888888888888888888888" part350 = "33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333" ) var testCases = map[string]bool{ // name -> valid "": false, "_why/_the/_lucky:_stiff": true, // minimal "h/n/m:t": true, "host/namespace/model:tag": true, "host/namespace/model": true, "namespace/model": true, "model": true, // long (but valid) part80 + "/" + part80 + "/" + part80 + ":" + part80: true, part350 + "/" + part80 + "/" + part80 + ":" + part80: true, // too long part80 + "/" + part80 + "/" + part80 + ":" + part350: false, "x" + part350 + "/" + part80 + "/" + part80 + ":" + part80: false, "h/nn/mm:t": true, // bare minimum part sizes // unqualified "m": true, "n/m:": true, "h/n/m": true, "@t": false, "m@d": false, // invalids "^": false, "mm:": true, "/nn/mm": true, "//": false, // empty model "//mm": true, "hh//": false, // empty model "//mm:@": false, "00@": false, "@": false, // not starting with alphanum "-hh/nn/mm:tt": false, "hh/-nn/mm:tt": false, "hh/nn/-mm:tt": false, "hh/nn/mm:-tt": false, // smells like a flag "-h": false, // hosts "host:https/namespace/model:tag": true, // colon in non-host part before tag "host/name:space/model:tag": false, } func TestParseNameValidation(t *testing.T) { for s, valid := range testCases { got := Parse(s) if got.IsValid() != valid { t.Logf("got: %v", got) t.Errorf("Parse(%q).IsValid() = %v; want !%[2]v", s, got.IsValid()) } } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/server/internal/internal/stringsx/stringsx.go
server/internal/internal/stringsx/stringsx.go
// Copyright (c) Tailscale Inc & AUTHORS // SPDX-License-Identifier: BSD-3-Clause // Package stringsx provides additional string manipulation functions // that aren't in the standard library's strings package or go4.org/mem. package stringsx import ( "unicode" "unicode/utf8" ) // CompareFold returns -1, 0, or 1 depending on whether a < b, a == b, or a > b, // like cmp.Compare, but case insensitively. func CompareFold(a, b string) int { // Track our position in both strings ia, ib := 0, 0 for ia < len(a) && ib < len(b) { ra, wa := nextRuneLower(a[ia:]) rb, wb := nextRuneLower(b[ib:]) if ra < rb { return -1 } if ra > rb { return 1 } ia += wa ib += wb if wa == 0 || wb == 0 { break } } // If we've reached here, one or both strings are exhausted // The shorter string is "less than" if they match up to this point switch { case ia == len(a) && ib == len(b): return 0 case ia == len(a): return -1 default: return 1 } } // nextRuneLower returns the next rune in the string, lowercased, along with its // original (consumed) width in bytes. If the string is empty, it returns // (utf8.RuneError, 0) func nextRuneLower(s string) (r rune, width int) { r, width = utf8.DecodeRuneInString(s) return unicode.ToLower(r), width }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/server/internal/internal/stringsx/stringsx_test.go
server/internal/internal/stringsx/stringsx_test.go
// Copyright (c) Tailscale Inc & AUTHORS // SPDX-License-Identifier: BSD-3-Clause package stringsx import ( "cmp" "strings" "testing" ) func TestCompareFold(t *testing.T) { tests := []struct { a, b string }{ // Basic ASCII cases {"", ""}, {"a", "a"}, {"a", "A"}, {"A", "a"}, {"a", "b"}, {"b", "a"}, {"abc", "ABC"}, {"ABC", "abc"}, {"abc", "abd"}, {"abd", "abc"}, // Length differences {"abc", "ab"}, {"ab", "abc"}, // Unicode cases {"世界", "世界"}, {"Hello世界", "hello世界"}, {"世界Hello", "世界hello"}, {"世界", "世界x"}, {"世界x", "世界"}, // Special case folding examples {"ß", "ss"}, // German sharp s {"fi", "fi"}, // fi ligature {"Σ", "σ"}, // Greek sigma {"İ", "i\u0307"}, // Turkish dotted I // Mixed cases {"HelloWorld", "helloworld"}, {"HELLOWORLD", "helloworld"}, {"helloworld", "HELLOWORLD"}, {"HelloWorld", "helloworld"}, {"helloworld", "HelloWorld"}, // Edge cases {" ", " "}, {"1", "1"}, {"123", "123"}, {"!@#", "!@#"}, } wants := []int{} for _, tt := range tests { got := CompareFold(tt.a, tt.b) want := cmp.Compare(strings.ToLower(tt.a), strings.ToLower(tt.b)) if got != want { t.Errorf("CompareFold(%q, %q) = %v, want %v", tt.a, tt.b, got, want) } wants = append(wants, want) } if n := testing.AllocsPerRun(1000, func() { for i, tt := range tests { if CompareFold(tt.a, tt.b) != wants[i] { panic("unexpected") } } }); n > 0 { t.Errorf("allocs = %v; want 0", int(n)) } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/server/internal/internal/syncs/line.go
server/internal/internal/syncs/line.go
// Package syncs provides synchronization primitives. package syncs import ( "cmp" "io" "sync" ) var closedChan = func() chan struct{} { ch := make(chan struct{}) close(ch) return ch }() // Ticket represents a ticket in a sequence of tickets. The zero value is // invalid. Use [Line.Take] to get a valid ticket. // // A Ticket is not safe for concurrent use. type Ticket struct { ahead chan struct{} // ticket ahead of this one ch chan struct{} } // Ready returns a channel that is closed when the ticket before this one is // done. // // It is incorrect to wait on Ready after the ticket is done. func (t *Ticket) Ready() chan struct{} { return cmp.Or(t.ahead, closedChan) } // Done signals that this ticket is done and that the next ticket in line can // proceed. // // The first call to [Done] unblocks the ticket after it, if any. Subsequent // calls are no-ops. func (t *Ticket) Done() { if t.ch != nil { close(t.ch) } t.ch = nil } // Line is an ordered sequence of tickets waiting for their turn to proceed. // // To get a ticket use [Line.Take]. // To signal that a ticket is done use [Ticket.Done]. // To wait your turn use [Ticket.Ready]. // // A Line is not safe for concurrent use. type Line struct { last chan struct{} // last ticket in line } func (q *Line) Take() *Ticket { t := &Ticket{ ahead: q.last, ch: make(chan struct{}), } q.last = t.ch return t } // RelayReader implements an [io.WriterTo] that yields the passed // writer to its [WriteTo] method each [io.WriteCloser] taken from [Take], in // the order they are taken. Each [io.WriteCloser] blocks until the previous // one is closed, or a call to [RelayReader.CloseWithError] is made. // // The zero value is invalid. Use [NewWriteToLine] to get a valid RelayReader. // // It is not safe for concurrent use. type RelayReader struct { line Line t *Ticket w io.Writer n int64 mu sync.Mutex err error // set by CloseWithError closedCh chan struct{} // closed if err is set } var ( _ io.Closer = (*RelayReader)(nil) _ io.WriterTo = (*RelayReader)(nil) _ io.Reader = (*RelayReader)(nil) ) func NewRelayReader() *RelayReader { var q RelayReader q.closedCh = make(chan struct{}) q.t = q.line.Take() return &q } // CloseWithError terminates the line, unblocking any writer waiting for its // turn with the error, or [io.EOF] if err is nil. It is safe to call // [CloseWithError] multiple times and across multiple goroutines. // // If the line is already closed, [CloseWithError] is a no-op. // // It never returns an error. func (q *RelayReader) CloseWithError(err error) error { q.mu.Lock() defer q.mu.Unlock() if q.err == nil { q.err = cmp.Or(q.err, err, io.EOF) close(q.closedCh) } return nil } // Close closes the line. Any writer waiting for its turn will be unblocked // with an [io.ErrClosedPipe] error. // // It never returns an error. func (q *RelayReader) Close() error { return q.CloseWithError(nil) } func (q *RelayReader) closed() <-chan struct{} { q.mu.Lock() defer q.mu.Unlock() return q.closedCh } func (q *RelayReader) Read(p []byte) (int, error) { panic("RelayReader.Read is for show only; use WriteTo") } // WriteTo yields the writer w to the first writer in line and blocks until the // first call to [Close]. // // It is safe to call [Take] concurrently with [WriteTo]. func (q *RelayReader) WriteTo(dst io.Writer) (int64, error) { select { case <-q.closed(): return 0, io.ErrClosedPipe default: } // We have a destination writer; let the relay begin. q.w = dst q.t.Done() <-q.closed() return q.n, nil } // Take returns a writer that will be passed to the next writer in line. // // It is not safe for use across multiple goroutines. func (q *RelayReader) Take() io.WriteCloser { return &relayWriter{q: q, t: q.line.Take()} } type relayWriter struct { q *RelayReader t *Ticket ready bool } var _ io.StringWriter = (*relayWriter)(nil) // Write writes to the writer passed to [RelayReader.WriteTo] as soon as the // writer is ready. It returns io.ErrClosedPipe if the line is closed before // the writer is ready. func (w *relayWriter) Write(p []byte) (int, error) { if !w.awaitTurn() { return 0, w.q.err } n, err := w.q.w.Write(p) w.q.n += int64(n) return n, err } func (w *relayWriter) WriteString(s string) (int, error) { if !w.awaitTurn() { return 0, w.q.err } return io.WriteString(w.q.w, s) } // Close signals that the writer is done, unblocking the next writer in line. func (w *relayWriter) Close() error { w.t.Done() return nil } func (t *relayWriter) awaitTurn() (ok bool) { if t.ready { return true } select { case <-t.t.Ready(): t.ready = true return true case <-t.q.closed(): return false } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/server/internal/internal/syncs/line_test.go
server/internal/internal/syncs/line_test.go
//go:build goexperiment.synctest package syncs import ( "bytes" "io" "math/rand/v2" "testing" "testing/synctest" ) func TestPipelineReadWriterTo(t *testing.T) { for range 10 { synctest.Run(func() { q := NewRelayReader() tickets := []struct { io.WriteCloser s string }{ {q.Take(), "you"}, {q.Take(), " say hi,"}, {q.Take(), " and "}, {q.Take(), "I say "}, {q.Take(), "hello"}, } rand.Shuffle(len(tickets), func(i, j int) { tickets[i], tickets[j] = tickets[j], tickets[i] }) var g Group for i, t := range tickets { g.Go(func() { defer t.Close() if i%2 == 0 { // Use [relayWriter.WriteString] io.WriteString(t.WriteCloser, t.s) } else { t.Write([]byte(t.s)) } }) } var got bytes.Buffer var copyErr error // checked at end g.Go(func() { _, copyErr = io.Copy(&got, q) }) synctest.Wait() q.Close() g.Wait() if copyErr != nil { t.Fatal(copyErr) } want := "you say hi, and I say hello" if got.String() != want { t.Fatalf("got %q, want %q", got.String(), want) } }) } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/server/internal/internal/syncs/syncs.go
server/internal/internal/syncs/syncs.go
package syncs import ( "sync" "sync/atomic" ) // Group is a [sync.WaitGroup] with a Go method. type Group struct { wg sync.WaitGroup n atomic.Int64 } func (g *Group) Go(f func()) { g.wg.Add(1) go func() { g.n.Add(1) // Now we are running defer func() { g.wg.Done() g.n.Add(-1) // Now we are done }() f() }() } // Running returns the number of goroutines that are currently running. // // If a call to [Running] returns zero, and a call to [Wait] is made without // any calls to [Go], then [Wait] will return immediately. This is true even if // a goroutine is started and finishes between the two calls. // // It is possible for [Running] to return non-zero and for [Wait] to return // immediately. This can happen if the all running goroutines finish between // the two calls. func (g *Group) Running() int64 { return g.n.Load() } func (g *Group) Wait() { g.wg.Wait() }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/server/internal/client/ollama/registry_synctest_test.go
server/internal/client/ollama/registry_synctest_test.go
// TODO: go:build goexperiment.synctest package ollama import ( "context" "errors" "io" "net/http" "strings" "testing" "time" ) func TestPullDownloadTimeout(t *testing.T) { rc, ctx := newRegistryClient(t, func(w http.ResponseWriter, r *http.Request) { defer t.Log("upstream", r.Method, r.URL.Path) switch { case strings.HasPrefix(r.URL.Path, "/v2/library/smol/manifests/"): io.WriteString(w, `{ "layers": [{"digest": "sha256:1111111111111111111111111111111111111111111111111111111111111111", "size": 3}] }`) case strings.HasPrefix(r.URL.Path, "/v2/library/smol/blobs/sha256:1111111111111111111111111111111111111111111111111111111111111111"): // Get headers out to client and then hang on the response w.WriteHeader(200) w.(http.Flusher).Flush() // Hang on the response and unblock when the client // gives up <-r.Context().Done() default: t.Fatalf("unexpected request: %s", r.URL.Path) } }) rc.ReadTimeout = 100 * time.Millisecond done := make(chan error, 1) go func() { done <- rc.Pull(ctx, "http://example.com/library/smol") }() select { case err := <-done: want := context.DeadlineExceeded if !errors.Is(err, want) { t.Errorf("err = %v, want %v", err, want) } case <-time.After(3 * time.Second): t.Error("timeout waiting for Pull to finish") } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/server/internal/client/ollama/registry.go
server/internal/client/ollama/registry.go
// Package ollama provides a client for interacting with an Ollama registry // which pushes and pulls model manifests and layers as defined by the // [ollama.com/manifest]. package ollama import ( "bufio" "bytes" "cmp" "context" "crypto" "crypto/ed25519" "crypto/sha256" "crypto/tls" "encoding/base64" "encoding/hex" "encoding/json" "errors" "fmt" "io" "io/fs" "iter" "log/slog" "net/http" "os" "path/filepath" "runtime" "runtime/debug" "slices" "strconv" "strings" "sync" "sync/atomic" "time" "golang.org/x/crypto/ssh" "golang.org/x/sync/errgroup" "github.com/ollama/ollama/server/internal/cache/blob" "github.com/ollama/ollama/server/internal/internal/names" _ "embed" ) // Errors var ( // ErrModelNotFound is returned when a manifest is not found in the // cache or registry. ErrModelNotFound = errors.New("model not found") // ErrManifestInvalid is returned when a manifest found in a local or // remote cache is invalid. ErrManifestInvalid = errors.New("invalid manifest") // ErrMissingModel is returned when the model part of a name is missing // or invalid. ErrNameInvalid = errors.New("invalid or missing name") // ErrCached is passed to [Trace.PushUpdate] when a layer already // exists. It is a non-fatal error and is never returned by [Registry.Push]. ErrCached = errors.New("cached") // ErrIncomplete is returned by [Registry.Pull] when a model pull was // incomplete due to one or more layer download failures. Users that // want specific errors should use [WithTrace]. ErrIncomplete = errors.New("incomplete") ) // Defaults const ( // DefaultChunkingThreshold is the threshold at which a layer should be // split up into chunks when downloading. DefaultChunkingThreshold = 64 << 20 ) var defaultCache = sync.OnceValues(func() (*blob.DiskCache, error) { dir := os.Getenv("OLLAMA_MODELS") if dir == "" { home, _ := os.UserHomeDir() home = cmp.Or(home, ".") dir = filepath.Join(home, ".ollama", "models") } return blob.Open(dir) }) // DefaultCache returns the default cache used by the registry. It is // configured from the OLLAMA_MODELS environment variable, or defaults to // $HOME/.ollama/models, or, if an error occurs obtaining the home directory, // it uses the current working directory. func DefaultCache() (*blob.DiskCache, error) { return defaultCache() } // Error is the standard error returned by Ollama APIs. It can represent a // single or multiple error response. // // Single error responses have the following format: // // {"code": "optional_code","error":"error message"} // // Multiple error responses have the following format: // // {"errors": [{"code": "optional_code","message":"error message"}]} // // Note, that the error field is used in single error responses, while the // message field is used in multiple error responses. // // In both cases, the code field is optional and may be empty. type Error struct { status int `json:"-"` // TODO(bmizerany): remove this Code string `json:"code"` Message string `json:"message"` } // Temporary reports if the error is temporary (e.g. 5xx status code). func (e *Error) Temporary() bool { return e.status >= 500 } func (e *Error) Error() string { var b strings.Builder b.WriteString("registry responded with status ") b.WriteString(strconv.Itoa(e.status)) if e.Code != "" { b.WriteString(": code ") b.WriteString(e.Code) } if e.Message != "" { b.WriteString(": ") b.WriteString(e.Message) } return b.String() } func (e *Error) LogValue() slog.Value { return slog.GroupValue( slog.Int("status", e.status), slog.String("code", e.Code), slog.String("message", e.Message), ) } // UnmarshalJSON implements json.Unmarshaler. func (e *Error) UnmarshalJSON(b []byte) error { type E Error var v struct { // Single error Code string Error string // Multiple errors Errors []E } if err := json.Unmarshal(b, &v); err != nil { return err } if v.Error != "" { // Single error case e.Code = v.Code e.Message = v.Error return nil } if len(v.Errors) == 0 { return fmt.Errorf("no messages in error response: %s", string(b)) } *e = Error(v.Errors[0]) // our registry only returns one error. return nil } const DefaultMask = "registry.ollama.ai/library/_:latest" var defaultMask = func() names.Name { n := names.Parse(DefaultMask) if !n.IsFullyQualified() { panic("default mask is not fully qualified") } return n }() // CompleteName returns a fully qualified name by merging the given name with // the default mask. If the name is already fully qualified, it is returned // unchanged. func CompleteName(name string) string { return names.Merge(names.Parse(name), defaultMask).String() } // Registry is a client for performing push and pull operations against an // Ollama registry. type Registry struct { // Cache is the cache used to store models. If nil, [DefaultCache] is // used. Cache *blob.DiskCache // UserAgent is the User-Agent header to send with requests to the // registry. If empty, the User-Agent is determined by HTTPClient. UserAgent string // Key is the key used to authenticate with the registry. // // Currently, only Ed25519 keys are supported. Key crypto.PrivateKey // HTTPClient is the HTTP client used to make requests to the registry. // // If nil, [http.DefaultClient] is used. // // As a quick note: If a Registry function that makes a call to a URL // with the "https+insecure" scheme, the client will be cloned and the // transport will be set to skip TLS verification, unless the client's // Transport done not have a Clone method with the same signature as // [http.Transport.Clone], which case, the call will fail. HTTPClient *http.Client // MaxStreams is the maximum number of concurrent streams to use when // pushing or pulling models. If zero, the number of streams is // determined by [runtime.GOMAXPROCS]. // // A negative value means no limit. MaxStreams int // ChunkingThreshold is the maximum size of a layer to download in a single // request. If zero, [DefaultChunkingThreshold] is used. ChunkingThreshold int64 // Mask, if set, is the name used to convert non-fully qualified names // to fully qualified names. // If empty, [DefaultMask] is used. Mask string // ReadTimeout is the maximum duration for reading the entire request, // including the body. // A zero or negative value means there will be no timeout. ReadTimeout time.Duration } func (r *Registry) readTimeout() time.Duration { if r.ReadTimeout > 0 { return r.ReadTimeout } return 1<<63 - 1 // no timeout, max int } func (r *Registry) cache() (*blob.DiskCache, error) { if r.Cache != nil { return r.Cache, nil } return defaultCache() } func (r *Registry) parseName(name string) (names.Name, error) { mask := defaultMask if r.Mask != "" { mask = names.Parse(r.Mask) } n := names.Merge(names.Parse(name), mask) if !n.IsFullyQualified() { return names.Name{}, fmt.Errorf("%w: %q", ErrNameInvalid, name) } return n, nil } // DefaultRegistry returns a new Registry configured from the environment. The // key is read from $HOME/.ollama/id_ed25519, MaxStreams is set to the // value of OLLAMA_REGISTRY_MAXSTREAMS, and ReadTimeout is set to 30 seconds. // // It returns an error if any configuration in the environment is invalid. func DefaultRegistry() (*Registry, error) { home, err := os.UserHomeDir() if err != nil { return nil, err } keyPEM, err := os.ReadFile(filepath.Join(home, ".ollama/id_ed25519")) if err != nil && errors.Is(err, fs.ErrNotExist) { return nil, err } var rc Registry rc.ReadTimeout = 30 * time.Second rc.UserAgent = UserAgent() rc.Key, err = ssh.ParseRawPrivateKey(keyPEM) if err != nil { return nil, err } maxStreams := os.Getenv("OLLAMA_REGISTRY_MAXSTREAMS") if maxStreams != "" { var err error rc.MaxStreams, err = strconv.Atoi(maxStreams) if err != nil { return nil, fmt.Errorf("invalid OLLAMA_REGISTRY_MAXSTREAMS: %w", err) } } return &rc, nil } func UserAgent() string { buildinfo, _ := debug.ReadBuildInfo() version := buildinfo.Main.Version if version == "(devel)" { // When using `go run .` the version is "(devel)". This is seen // as an invalid version by ollama.com and so it defaults to // "needs upgrade" for some requests, such as pulls. These // checks can be skipped by using the special version "v0.0.0", // so we set it to that here. version = "v0.0.0" } return fmt.Sprintf("ollama/%s (%s %s) Go/%s", version, runtime.GOARCH, runtime.GOOS, runtime.Version(), ) } func (r *Registry) maxStreams() int { return cmp.Or(r.MaxStreams, runtime.GOMAXPROCS(0)) } func (r *Registry) maxChunkingThreshold() int64 { return cmp.Or(r.ChunkingThreshold, DefaultChunkingThreshold) } type PushParams struct { // From is an optional destination name for the model. If empty, the // destination name is the same as the source name. From string } // Push pushes the model with the name in the cache to the remote registry. func (r *Registry) Push(ctx context.Context, name string, p *PushParams) error { if p == nil { p = &PushParams{} } c, err := r.cache() if err != nil { return err } m, err := r.ResolveLocal(cmp.Or(p.From, name)) if err != nil { return err } // Before much else happens, check layers at not null, the blobs exist, // and the sizes match. This prevents long uploads followed by // disappointment. for _, l := range m.Layers { if l == nil { return fmt.Errorf("%w: null layer", ErrManifestInvalid) } info, err := c.Get(l.Digest) if err != nil { return fmt.Errorf("error getting %s: %w", l.Digest.Short(), err) } if info.Size != l.Size { return fmt.Errorf("size mismatch for %s: %d != %d", l.Digest.Short(), info.Size, l.Size) } } t := traceFromContext(ctx) scheme, n, _, err := r.parseNameExtended(name) if err != nil { // This should never happen since ResolveLocal should have // already validated the name. panic(err) } ctx, cancel := context.WithCancel(ctx) defer cancel() var g errgroup.Group g.SetLimit(r.maxStreams()) for _, l := range m.Layers { var progress atomic.Int64 g.Go(func() (err error) { defer func() { t.update(l, progress.Load(), err) }() t.update(l, 0, nil) startURL := fmt.Sprintf("%s://%s/v2/%s/%s/blobs/uploads/?digest=%s", scheme, n.Host(), n.Namespace(), n.Model(), l.Digest, ) res, err := r.send(ctx, "POST", startURL, nil) if err != nil { return err } res.Body.Close() f, err := os.Open(c.GetFile(l.Digest)) if err != nil { return err } defer f.Close() uploadURL := res.Header.Get("Location") if uploadURL == "" { t.update(l, l.Size, ErrCached) return nil } req, err := r.newRequest(ctx, "PUT", uploadURL, f) if err != nil { return fmt.Errorf("invalid upload URL returned from registry: %q: %w", uploadURL, err) } req.ContentLength = l.Size res, err = sendRequest(r.client(), req) if err == nil { res.Body.Close() } return err }) } if err := g.Wait(); err != nil { return err } // Commit path := fmt.Sprintf("%s://%s/v2/%s/%s/manifests/%s", scheme, n.Host(), n.Namespace(), n.Model(), n.Tag(), ) res, err := r.send(ctx, "PUT", path, bytes.NewReader(m.Data)) if err == nil { res.Body.Close() } // TODO(bmizerany): add a "commit" trace event return err } // trackingReader is an io.Reader that tracks the number of bytes read and // calls the update function with the layer, the number of bytes read. // // It always calls update with a nil error. type trackingReader struct { r io.Reader update func(n int64, err error) // err is always nil } func (r *trackingReader) Read(p []byte) (n int, err error) { n, err = r.r.Read(p) r.update(int64(n), nil) return } // Pull pulls the model with the given name from the remote registry into the // cache. // // For layers larger then [Registry.MaxChunkSize], the layer is downloaded in // chunks of the specified size, and then reassembled and verified. This is // typically slower than splitting the model up across layers, and is mostly // utilized for layers of type equal to "application/vnd.ollama.image". func (r *Registry) Pull(ctx context.Context, name string) error { m, err := r.Resolve(ctx, name) if err != nil { return err } // TODO(bmizerany): decide if this should be considered valid. Maybe // server-side we special case '{}' to have some special meaning? Maybe // "archiving" a tag (which is how we reason about it in the registry // already, just with a different twist). if len(m.Layers) == 0 { return fmt.Errorf("%w: no layers", ErrManifestInvalid) } c, err := r.cache() if err != nil { return err } // TODO(bmizerany): work to remove the need to do this layers := m.Layers if m.Config != nil && m.Config.Digest.IsValid() { layers = append(layers, m.Config) } // Send initial layer trace events to allow clients to have an // understanding of work to be done before work starts. var expected int64 t := traceFromContext(ctx) for _, l := range layers { t.update(l, 0, nil) expected += l.Size } var g errgroup.Group g.SetLimit(r.maxStreams()) var completed atomic.Int64 for _, l := range layers { var received atomic.Int64 update := func(n int64, err error) { if n == 0 && err == nil { // Clients expect an update with no progress and no error to mean "starting download". // This is not the case here, // so we don't want to send an update in this case. return } completed.Add(n) t.update(l, received.Add(n), err) } info, err := c.Get(l.Digest) if err == nil && info.Size == l.Size { update(l.Size, ErrCached) continue } func() (err error) { defer func() { if err != nil { update(0, err) } }() var wg sync.WaitGroup chunked, err := c.Chunked(l.Digest, l.Size) if err != nil { return err } defer func() { // Close the chunked writer when all chunks are // downloaded. // // This is done as a background task in the // group to allow the next layer to start while // we wait for the final chunk in this layer to // complete. It also ensures this is done // before we exit Pull. g.Go(func() error { wg.Wait() chunked.Close() return nil }) }() for cs, err := range r.chunksums(ctx, name, l) { if err != nil { // Note the chunksum stream // interruption, but do not cancel // in-flight downloads. We can still // make progress on them. Once they are // done, ErrIncomplete will be returned // below. update(0, err) break } cacheKey := fmt.Sprintf( "v1 pull chunksum %s %s %d-%d", l.Digest, cs.Digest, cs.Chunk.Start, cs.Chunk.End, ) cacheKeyDigest := blob.DigestFromBytes(cacheKey) _, err := c.Get(cacheKeyDigest) if err == nil { update(cs.Chunk.Size(), ErrCached) continue } wg.Add(1) g.Go(func() (err error) { defer func() { defer wg.Done() if err != nil { update(0, err) } }() ctx, cancel := context.WithCancelCause(ctx) defer cancel(nil) timer := time.AfterFunc(r.readTimeout(), func() { cancel(fmt.Errorf("%w: downloading %s %d-%d/%d", context.DeadlineExceeded, cs.Digest.Short(), cs.Chunk.Start, cs.Chunk.End, l.Size, )) }) defer timer.Stop() req, err := http.NewRequestWithContext(ctx, "GET", cs.URL, nil) if err != nil { return err } req.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", cs.Chunk.Start, cs.Chunk.End)) res, err := sendRequest(r.client(), req) if err != nil { return err } defer res.Body.Close() tr := &trackingReader{ r: res.Body, update: func(n int64, err error) { timer.Reset(r.readTimeout()) update(n, err) }, } if err := chunked.Put(cs.Chunk, cs.Digest, tr); err != nil { return err } // Record the downloading of this chunk. return blob.PutBytes(c, cacheKeyDigest, cacheKey) }) } return nil }() } if err := g.Wait(); err != nil { return err } if recv := completed.Load(); recv != expected { return fmt.Errorf("%w: received %d/%d bytes", ErrIncomplete, recv, expected) } md := blob.DigestFromBytes(m.Data) if err := blob.PutBytes(c, md, m.Data); err != nil { return err } return c.Link(m.Name, md) } // Unlink is like [blob.DiskCache.Unlink], but makes name fully qualified // before attempting to unlink the model. func (r *Registry) Unlink(name string) (ok bool, _ error) { n, err := r.parseName(name) if err != nil { return false, err } c, err := r.cache() if err != nil { return false, err } return c.Unlink(n.String()) } // Manifest represents a [ollama.com/manifest]. type Manifest struct { Name string `json:"-"` // the canonical name of the model Data []byte `json:"-"` // the raw data of the manifest Layers []*Layer `json:"layers"` // For legacy reasons, we still have to download the config layer. Config *Layer `json:"config"` } // Layer returns the layer with the given // digest, or nil if not found. func (m *Manifest) Layer(d blob.Digest) *Layer { for _, l := range m.Layers { if l.Digest == d { return l } } return nil } func (m *Manifest) All() iter.Seq[*Layer] { return func(yield func(*Layer) bool) { if !yield(m.Config) { return } for _, l := range m.Layers { if !yield(l) { return } } } } func (m *Manifest) Size() int64 { var size int64 if m.Config != nil { size += m.Config.Size } for _, l := range m.Layers { size += l.Size } return size } // MarshalJSON implements json.Marshaler. // // NOTE: It adds an empty config object to the manifest, which is required by // the registry, but not used by the client. In the future, the config object // will not be required by the registry and this will should be removed. func (m Manifest) MarshalJSON() ([]byte, error) { type M Manifest v := struct { M // This is ignored, mostly, by the registry But, if not // present, it will cause an error to be returned during the // last phase of the commit which expects it, but does nothing // with it. This will be fixed in a future release of // ollama.com. Config Layer `json:"config"` }{ M: M(m), } return json.Marshal(v) } // unmarshalManifest unmarshals the data into a manifest, and sets the name // field to the string representation of the name. // // It panics if the name is not fully qualified. Callers should ensure the name // is fully qualified before calling this function. func unmarshalManifest(n names.Name, data []byte) (*Manifest, error) { if !n.IsFullyQualified() { panic(fmt.Sprintf("unmarshalManifest: name is not fully qualified: %s", n.String())) } var m Manifest if err := json.Unmarshal(data, &m); err != nil { return nil, err } m.Name = n.String() m.Data = data return &m, nil } // Layer is a layer in a model. type Layer struct { Digest blob.Digest `json:"digest"` MediaType string `json:"mediaType"` Size int64 `json:"size"` } // ResolveLocal resolves a name to a Manifest in the local cache. func (r *Registry) ResolveLocal(name string) (*Manifest, error) { _, n, d, err := r.parseNameExtended(name) if err != nil { return nil, err } c, err := r.cache() if err != nil { return nil, err } if !d.IsValid() { // No digest, so resolve the manifest by name. d, err = c.Resolve(n.String()) if err != nil { return nil, err } } data, err := os.ReadFile(c.GetFile(d)) if err != nil { if errors.Is(err, fs.ErrNotExist) { return nil, fmt.Errorf("%w: %s", ErrModelNotFound, name) } return nil, err } m, err := unmarshalManifest(n, data) if err != nil { return nil, fmt.Errorf("%s: %w", name, errors.Join(ErrManifestInvalid, err)) } return m, nil } // Resolve resolves a name to a Manifest in the remote registry. func (r *Registry) Resolve(ctx context.Context, name string) (*Manifest, error) { scheme, n, d, err := r.parseNameExtended(name) if err != nil { return nil, err } manifestURL := fmt.Sprintf("%s://%s/v2/%s/%s/manifests/%s", scheme, n.Host(), n.Namespace(), n.Model(), n.Tag()) if d.IsValid() { manifestURL = fmt.Sprintf("%s://%s/v2/%s/%s/blobs/%s", scheme, n.Host(), n.Namespace(), n.Model(), d) } res, err := r.send(ctx, "GET", manifestURL, nil) if err != nil { return nil, err } defer res.Body.Close() data, err := io.ReadAll(res.Body) if err != nil { return nil, err } // TODO(bmizerany): return digest here m, err := unmarshalManifest(n, data) if err != nil { return nil, fmt.Errorf("%s: %w", name, errors.Join(ErrManifestInvalid, err)) } return m, nil } type chunksum struct { URL string Chunk blob.Chunk Digest blob.Digest } // chunksums returns a sequence of chunksums for the given layer. If the layer is under the // chunking threshold, a single chunksum is returned that covers the entire layer. If the layer // is over the chunking threshold, the chunksums are read from the chunksums endpoint. func (r *Registry) chunksums(ctx context.Context, name string, l *Layer) iter.Seq2[chunksum, error] { return func(yield func(chunksum, error) bool) { scheme, n, _, err := r.parseNameExtended(name) if err != nil { yield(chunksum{}, err) return } if l.Size < r.maxChunkingThreshold() { // any layer under the threshold should be downloaded // in one go. cs := chunksum{ URL: fmt.Sprintf("%s://%s/v2/%s/%s/blobs/%s", scheme, n.Host(), n.Namespace(), n.Model(), l.Digest, ), Chunk: blob.Chunk{Start: 0, End: l.Size - 1}, Digest: l.Digest, } yield(cs, nil) return } // The response is a sequence of chunksums. // // Chunksums are chunks of a larger blob that can be // downloaded and verified independently. // // The chunksums endpoint is a GET request that returns a // sequence of chunksums in the following format: // // > GET /v2/<namespace>/<model>/chunksums/<digest> // // < HTTP/1.1 200 OK // < Content-Location: <blobURL> // < // < <digest> <start>-<end> // < ... // // The <blobURL> is the URL to download the chunks from and // each <digest> is the digest of the chunk, and <start>-<end> // is the range the chunk in the blob. // // Ranges may be used directly in Range headers like // "bytes=<start>-<end>". // // The chunksums returned are guaranteed to be contiguous and // include all bytes of the layer. If the stream is cut short, // clients should retry. chunksumsURL := fmt.Sprintf("%s://%s/v2/%s/%s/chunksums/%s", scheme, n.Host(), n.Namespace(), n.Model(), l.Digest, ) req, err := r.newRequest(ctx, "GET", chunksumsURL, nil) if err != nil { yield(chunksum{}, err) return } res, err := sendRequest(r.client(), req) if err != nil { yield(chunksum{}, err) return } defer res.Body.Close() if res.StatusCode != 200 { err := fmt.Errorf("chunksums: unexpected status code %d", res.StatusCode) yield(chunksum{}, err) return } blobURL := res.Header.Get("Content-Location") s := bufio.NewScanner(res.Body) s.Split(bufio.ScanWords) for { if !s.Scan() { if s.Err() != nil { yield(chunksum{}, s.Err()) } return } d, err := blob.ParseDigest(s.Bytes()) if err != nil { yield(chunksum{}, fmt.Errorf("invalid digest: %q", s.Bytes())) return } if !s.Scan() { err := s.Err() if err == nil { err = fmt.Errorf("missing chunk range for digest %s", d) } yield(chunksum{}, err) return } chunk, err := parseChunk(s.Bytes()) if err != nil { yield(chunksum{}, fmt.Errorf("invalid chunk range for digest %s: %q", d, s.Bytes())) return } cs := chunksum{ URL: blobURL, Chunk: chunk, Digest: d, } if !yield(cs, nil) { return } } } } func (r *Registry) client() *http.Client { if r.HTTPClient != nil { return r.HTTPClient } return http.DefaultClient } // newRequest constructs a new request, ready to use, with the given method, // url, and body, pre-signed with client [Key] and [UserAgent]. func (r *Registry) newRequest(ctx context.Context, method, url string, body io.Reader) (*http.Request, error) { req, err := http.NewRequestWithContext(ctx, method, url, body) if err != nil { return nil, err } if r.UserAgent != "" { req.Header.Set("User-Agent", r.UserAgent) } if r.Key != nil { token, err := makeAuthToken(r.Key) if err != nil { return nil, err } req.Header.Set("Authorization", "Bearer "+token) } return req, nil } // sendRequest makes a request with the given client and request, and returns the // response if the status code is 200. If the status code is not 200, an Error // is parsed from the response body and returned. If any other error occurs, it // is returned. func sendRequest(c *http.Client, r *http.Request) (_ *http.Response, err error) { if r.URL.Scheme == "https+insecure" { // TODO(bmizerany): clone client.Transport, set // InsecureSkipVerify, etc. type cloner interface { Clone() *http.Transport } // Attempt to configure the transport to skip TLS verification // if we can clone it, otherwise fall through and let the http // client complain and the scheme being invalid. x, ok := cmp.Or(c.Transport, http.DefaultTransport).(cloner) if ok { tr := x.Clone() tr.TLSClientConfig = cmp.Or(tr.TLSClientConfig, &tls.Config{}) tr.TLSClientConfig.InsecureSkipVerify = true cc := *c // shallow copy cc.Transport = tr c = &cc r = r.Clone(r.Context()) r.URL.Scheme = "https" // fall through } } res, err := c.Do(r) if err != nil { return nil, err } if res.StatusCode/100 != 2 { out, err := io.ReadAll(res.Body) if err != nil { return nil, err } var re Error if err := json.Unmarshal(out, &re); err != nil { // Use the raw body if we can't parse it as an error object. re.Message = string(out) } // coerce MANIFEST_UNKNOWN to ErrManifestNotFound if strings.EqualFold(re.Code, "MANIFEST_UNKNOWN") { return nil, ErrModelNotFound } re.status = res.StatusCode return nil, &re } return res, nil } // send is a convenience method for making a request with newRequest and // passing it to send with r.client(). func (r *Registry) send(ctx context.Context, method, path string, body io.Reader) (*http.Response, error) { req, err := r.newRequest(ctx, method, path, body) if err != nil { return nil, err } return sendRequest(r.client(), req) } // makeAuthToken creates an Ollama auth token for the given private key. // // NOTE: This format is OLD, overly complex, and should be replaced. We're // inheriting it from the original Ollama client and ollama.com // implementations, so we need to support it for now. func makeAuthToken(key crypto.PrivateKey) (string, error) { privKey, _ := key.(*ed25519.PrivateKey) if privKey == nil { return "", fmt.Errorf("unsupported private key type: %T", key) } url := fmt.Sprintf("https://ollama.com?ts=%d", time.Now().Unix()) // Part 1: the checkData (e.g. the URL with a timestamp) // Part 2: the public key pubKeyShort, err := func() ([]byte, error) { sshPubKey, err := ssh.NewPublicKey(privKey.Public()) if err != nil { return nil, err } pubKeyParts := bytes.Fields(ssh.MarshalAuthorizedKey(sshPubKey)) if len(pubKeyParts) < 2 { return nil, fmt.Errorf("malformed public key: %q", pubKeyParts) } pubKeyShort := pubKeyParts[1] return pubKeyShort, nil }() if err != nil { return "", err } // Part 3: the signature sig := ed25519.Sign(*privKey, []byte(checkData(url))) // Assemble the token: <checkData>:<pubKey>:<signature> var b strings.Builder io.WriteString(&b, base64.StdEncoding.EncodeToString([]byte(url))) b.WriteByte(':') b.Write(pubKeyShort) b.WriteByte(':') io.WriteString(&b, base64.StdEncoding.EncodeToString(sig)) return b.String(), nil } // The original spec for Ollama tokens was to use the SHA256 of the zero // string as part of the signature. I'm not sure why that was, but we still // need it to verify the signature. var zeroSum = func() string { sha256sum := sha256.Sum256(nil) x := base64.StdEncoding.EncodeToString([]byte(hex.EncodeToString(sha256sum[:]))) return x }() // checkData takes a URL and creates the original string format of the // data signature that is used by the ollama client to sign requests func checkData(url string) string { return fmt.Sprintf("GET,%s,%s", url, zeroSum) } type publicError struct { wrapped error message string } func withPublicMessagef(err error, message string, args ...any) error { return publicError{wrapped: err, message: fmt.Sprintf(message, args...)} } func (e publicError) Error() string { return e.message } func (e publicError) Unwrap() error { return e.wrapped } var supportedSchemes = []string{ "http", "https", "https+insecure", } var supportedSchemesMessage = fmt.Sprintf("supported schemes are %v", strings.Join(supportedSchemes, ", ")) // parseNameExtended parses and validates an extended name, returning the scheme, name, // and digest. // // If the scheme is empty, scheme will be "https". If an unsupported scheme is // given, [ErrNameInvalid] wrapped with a display friendly message is returned. // // If the digest is invalid, [ErrNameInvalid] wrapped with a display friendly // message is returned. // // If the name is not, once merged with the mask, fully qualified, // [ErrNameInvalid] wrapped with a display friendly message is returned. func (r *Registry) parseNameExtended(s string) (scheme string, _ names.Name, _ blob.Digest, _ error) { scheme, name, digest := splitExtended(s) scheme = cmp.Or(scheme, "https") if !slices.Contains(supportedSchemes, scheme) { err := withPublicMessagef(ErrNameInvalid, "unsupported scheme: %q: %s", scheme, supportedSchemesMessage) return "", names.Name{}, blob.Digest{}, err } var d blob.Digest if digest != "" { var err error d, err = blob.ParseDigest(digest) if err != nil { err = withPublicMessagef(ErrNameInvalid, "invalid digest: %q", digest) return "", names.Name{}, blob.Digest{}, err } if name == "" { // We have can resolve a manifest from a digest only, // so skip name validation and return the scheme and // digest. return scheme, names.Name{}, d, nil } } n, err := r.parseName(name) if err != nil { return "", names.Name{}, blob.Digest{}, err } return scheme, n, d, nil } // splitExtended splits an extended name string into its scheme, name, and digest // parts. // // Examples: // // http://ollama.com/bmizerany/smol:latest@digest // https://ollama.com/bmizerany/smol:latest // ollama.com/bmizerany/smol:latest@digest // returns "https" scheme. // model@digest // @digest func splitExtended(s string) (scheme, name, digest string) { i := strings.Index(s, "://") if i >= 0 { scheme = s[:i] s = s[i+3:] } i = strings.LastIndex(s, "@") if i >= 0 { digest = s[i+1:] s = s[:i] } return scheme, s, digest } // parseChunk parses a string in the form "start-end" and returns the Chunk. func parseChunk[S ~string | ~[]byte](s S) (blob.Chunk, error) { startPart, endPart, found := strings.Cut(string(s), "-") if !found { return blob.Chunk{}, fmt.Errorf("chunks: invalid range %q: missing '-'", s) } start, err := strconv.ParseInt(startPart, 10, 64) if err != nil { return blob.Chunk{}, fmt.Errorf("chunks: invalid start to %q: %v", s, err) } end, err := strconv.ParseInt(endPart, 10, 64) if err != nil { return blob.Chunk{}, fmt.Errorf("chunks: invalid end to %q: %v", s, err) } if start > end { return blob.Chunk{}, fmt.Errorf("chunks: invalid range %q: start > end", s) } return blob.Chunk{Start: start, End: end}, nil }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/server/internal/client/ollama/trace.go
server/internal/client/ollama/trace.go
package ollama import ( "context" ) // Trace is a set of functions that are called to report progress during blob // downloads and uploads. // // Use [WithTrace] to attach a Trace to a context for use with [Registry.Push] // and [Registry.Pull]. type Trace struct { // Update is called during [Registry.Push] and [Registry.Pull] to // report the progress of blob uploads and downloads. // // The n argument is the number of bytes transferred so far, and err is // any error that has occurred. If n == 0, and err is nil, the download // or upload has just started. If err is [ErrCached], the download or // upload has been skipped because the blob is already present in the // local cache or remote registry, respectively. Otherwise, if err is // non-nil, the download or upload has failed. When l.Size == n, and // err is nil, the download or upload has completed. // // A function assigned must be safe for concurrent use. The function is // called synchronously and so should not block or take long to run. Update func(_ *Layer, n int64, _ error) } func (t *Trace) update(l *Layer, n int64, err error) { if t.Update != nil { t.Update(l, n, err) } } type traceKey struct{} // WithTrace adds a trace to the context for transfer progress reporting. func WithTrace(ctx context.Context, t *Trace) context.Context { old := traceFromContext(ctx) if old == t { // No change, return the original context. This also prevents // infinite recursion below, if the caller passes the same // Trace. return ctx } // Create a new Trace that wraps the old one, if any. If we used the // same pointer t, we end up with a recursive structure. composed := &Trace{ Update: func(l *Layer, n int64, err error) { if old != nil { old.update(l, n, err) } t.update(l, n, err) }, } return context.WithValue(ctx, traceKey{}, composed) } var emptyTrace = &Trace{} // traceFromContext returns the Trace associated with ctx, or an empty Trace if // none is found. // // It never returns nil. func traceFromContext(ctx context.Context) *Trace { t, _ := ctx.Value(traceKey{}).(*Trace) if t == nil { return emptyTrace } return t }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/server/internal/client/ollama/registry_test.go
server/internal/client/ollama/registry_test.go
package ollama import ( "bytes" "cmp" "context" "encoding/json" "errors" "fmt" "io" "io/fs" "net" "net/http" "net/http/httptest" "os" "reflect" "strings" "sync/atomic" "testing" "github.com/ollama/ollama/server/internal/cache/blob" "github.com/ollama/ollama/server/internal/testutil" ) func ExampleRegistry_cancelOnFirstError() { ctx, cancel := context.WithCancel(context.Background()) defer cancel() ctx = WithTrace(ctx, &Trace{ Update: func(l *Layer, n int64, err error) { if err != nil { // Discontinue pulling layers if there is an // error instead of continuing to pull more // data. cancel() } }, }) var r Registry if err := r.Pull(ctx, "model"); err != nil { // panic for demo purposes panic(err) } } func TestManifestMarshalJSON(t *testing.T) { // All manifests should contain an "empty" config object. var m Manifest data, err := json.Marshal(m) if err != nil { t.Fatal(err) } if !bytes.Contains(data, []byte(`"config":{"digest":"sha256:`)) { t.Error("expected manifest to contain empty config") t.Fatalf("got:\n%s", string(data)) } } var errRoundTrip = errors.New("forced roundtrip error") type recordRoundTripper http.HandlerFunc func (rr recordRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { w := httptest.NewRecorder() rr(w, req) if w.Code == 499 { return nil, errRoundTrip } resp := w.Result() // For some reason, Response.Request is not set by httptest.NewRecorder, so we // set it manually. resp.Request = req return w.Result(), nil } // newClient constructs a cache with predefined manifests for testing. The manifests are: // // empty: no data // zero: no layers // single: one layer with the contents "exists" // multiple: two layers with the contents "exists" and "here" // notfound: a layer that does not exist in the cache // null: one null layer (e.g. [null]) // sizemismatch: one valid layer, and one with a size mismatch (file size is less than the reported size) // invalid: a layer with invalid JSON data // // Tests that want to ensure the client does not communicate with the upstream // registry should pass a nil handler, which will cause a panic if // communication is attempted. // // To simulate a network error, pass a handler that returns a 499 status code. func newClient(t *testing.T, upstreamRegistry http.HandlerFunc) (*Registry, *blob.DiskCache) { t.Helper() c, err := blob.Open(t.TempDir()) if err != nil { t.Fatal(err) } mklayer := func(data string) *Layer { return &Layer{ Digest: importBytes(t, c, data), Size: int64(len(data)), } } r := &Registry{ Cache: c, HTTPClient: &http.Client{ Transport: recordRoundTripper(upstreamRegistry), }, } link := func(name string, manifest string) { n, err := r.parseName(name) if err != nil { panic(err) } d, err := c.Import(bytes.NewReader([]byte(manifest)), int64(len(manifest))) if err != nil { panic(err) } if err := c.Link(n.String(), d); err != nil { panic(err) } } commit := func(name string, layers ...*Layer) { t.Helper() data, err := json.Marshal(&Manifest{Layers: layers}) if err != nil { t.Fatal(err) } link(name, string(data)) } link("empty", "") commit("zero") commit("single", mklayer("exists")) commit("multiple", mklayer("exists"), mklayer("present")) commit("notfound", &Layer{Digest: blob.DigestFromBytes("notfound"), Size: int64(len("notfound"))}) commit("null", nil) commit("sizemismatch", mklayer("exists"), &Layer{Digest: blob.DigestFromBytes("present"), Size: 499}) link("invalid", "!!!!!") return r, c } func okHandler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) } func checkErrCode(t *testing.T, err error, status int, code string) { t.Helper() var e *Error if !errors.As(err, &e) || e.status != status || e.Code != code { t.Errorf("err = %v; want %v %v", err, status, code) } } func importBytes(t *testing.T, c *blob.DiskCache, data string) blob.Digest { d, err := c.Import(strings.NewReader(data), int64(len(data))) if err != nil { t.Fatal(err) } return d } func withTraceUnexpected(ctx context.Context) (context.Context, *Trace) { t := &Trace{Update: func(*Layer, int64, error) { panic("unexpected") }} return WithTrace(ctx, t), t } func TestPushZero(t *testing.T) { rc, _ := newClient(t, okHandler) err := rc.Push(t.Context(), "empty", nil) if !errors.Is(err, ErrManifestInvalid) { t.Errorf("err = %v; want %v", err, ErrManifestInvalid) } } func TestPushSingle(t *testing.T) { rc, _ := newClient(t, okHandler) err := rc.Push(t.Context(), "single", nil) testutil.Check(t, err) } func TestPushMultiple(t *testing.T) { rc, _ := newClient(t, okHandler) err := rc.Push(t.Context(), "multiple", nil) testutil.Check(t, err) } func TestPushNotFound(t *testing.T) { rc, _ := newClient(t, func(w http.ResponseWriter, r *http.Request) { t.Errorf("unexpected request: %v", r) }) err := rc.Push(t.Context(), "notfound", nil) if !errors.Is(err, fs.ErrNotExist) { t.Errorf("err = %v; want %v", err, fs.ErrNotExist) } } func TestPushNullLayer(t *testing.T) { rc, _ := newClient(t, nil) err := rc.Push(t.Context(), "null", nil) if err == nil || !strings.Contains(err.Error(), "invalid manifest") { t.Errorf("err = %v; want invalid manifest", err) } } func TestPushSizeMismatch(t *testing.T) { rc, _ := newClient(t, nil) ctx, _ := withTraceUnexpected(t.Context()) got := rc.Push(ctx, "sizemismatch", nil) if got == nil || !strings.Contains(got.Error(), "size mismatch") { t.Errorf("err = %v; want size mismatch", got) } } func TestPushInvalid(t *testing.T) { rc, _ := newClient(t, nil) err := rc.Push(t.Context(), "invalid", nil) if err == nil || !strings.Contains(err.Error(), "invalid manifest") { t.Errorf("err = %v; want invalid manifest", err) } } func TestPushExistsAtRemote(t *testing.T) { var pushed bool rc, _ := newClient(t, func(w http.ResponseWriter, r *http.Request) { if strings.Contains(r.URL.Path, "/uploads/") { if !pushed { // First push. Return an uploadURL. pushed = true w.Header().Set("Location", "http://blob.store/blobs/123") return } w.WriteHeader(http.StatusAccepted) return } io.Copy(io.Discard, r.Body) w.WriteHeader(http.StatusOK) }) rc.MaxStreams = 1 // prevent concurrent uploads var errs []error ctx := WithTrace(t.Context(), &Trace{ Update: func(_ *Layer, n int64, err error) { // uploading one at a time so no need to lock errs = append(errs, err) }, }) check := testutil.Checker(t) err := rc.Push(ctx, "single", nil) check(err) if !errors.Is(errors.Join(errs...), nil) { t.Errorf("errs = %v; want %v", errs, []error{ErrCached}) } err = rc.Push(ctx, "single", nil) check(err) } func TestPushRemoteError(t *testing.T) { rc, _ := newClient(t, func(w http.ResponseWriter, r *http.Request) { if strings.Contains(r.URL.Path, "/blobs/") { w.WriteHeader(500) io.WriteString(w, `{"errors":[{"code":"blob_error"}]}`) return } }) got := rc.Push(t.Context(), "single", nil) checkErrCode(t, got, 500, "blob_error") } func TestPushLocationError(t *testing.T) { rc, _ := newClient(t, func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Location", ":///x") w.WriteHeader(http.StatusAccepted) }) got := rc.Push(t.Context(), "single", nil) wantContains := "invalid upload URL" if got == nil || !strings.Contains(got.Error(), wantContains) { t.Errorf("err = %v; want to contain %v", got, wantContains) } } func TestPushUploadRoundtripError(t *testing.T) { rc, _ := newClient(t, func(w http.ResponseWriter, r *http.Request) { if r.Host == "blob.store" { w.WriteHeader(499) // force RoundTrip error on upload return } w.Header().Set("Location", "http://blob.store/blobs/123") }) got := rc.Push(t.Context(), "single", nil) if !errors.Is(got, errRoundTrip) { t.Errorf("got = %v; want %v", got, errRoundTrip) } } func TestPushUploadFileOpenError(t *testing.T) { rc, c := newClient(t, okHandler) ctx := WithTrace(t.Context(), &Trace{ Update: func(l *Layer, _ int64, err error) { // Remove the file just before it is opened for upload, // but after the initial Stat that happens before the // upload starts os.Remove(c.GetFile(l.Digest)) }, }) got := rc.Push(ctx, "single", nil) if !errors.Is(got, fs.ErrNotExist) { t.Errorf("got = %v; want fs.ErrNotExist", got) } } func TestPushCommitRoundtripError(t *testing.T) { rc, _ := newClient(t, func(w http.ResponseWriter, r *http.Request) { if strings.Contains(r.URL.Path, "/blobs/") { panic("unexpected") } w.WriteHeader(499) // force RoundTrip error }) err := rc.Push(t.Context(), "zero", nil) if !errors.Is(err, errRoundTrip) { t.Errorf("err = %v; want %v", err, errRoundTrip) } } func TestRegistryPullInvalidName(t *testing.T) { rc, _ := newRegistryClient(t, nil) err := rc.Pull(t.Context(), "://") if !errors.Is(err, ErrNameInvalid) { t.Errorf("err = %v; want %v", err, ErrNameInvalid) } } func TestRegistryPullInvalidManifest(t *testing.T) { cases := []string{ "", "null", "!!!", `{"layers":[]}`, } for _, resp := range cases { rc, _ := newRegistryClient(t, func(w http.ResponseWriter, r *http.Request) { io.WriteString(w, resp) }) err := rc.Pull(t.Context(), "http://example.com/a/b") if !errors.Is(err, ErrManifestInvalid) { t.Errorf("err = %v; want invalid manifest", err) } } } func TestRegistryResolveByDigest(t *testing.T) { check := testutil.Checker(t) exists := blob.DigestFromBytes("exists") rc, _ := newClient(t, func(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/v2/alice/palace/blobs/"+exists.String() { w.WriteHeader(499) // should not hit manifest endpoint } fmt.Fprintf(w, `{"layers":[{"digest":%q,"size":5}]}`, exists) }) _, err := rc.Resolve(t.Context(), "alice/palace@"+exists.String()) check(err) } func TestInsecureSkipVerify(t *testing.T) { exists := blob.DigestFromBytes("exists") s := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, `{"layers":[{"digest":%q,"size":5}]}`, exists) })) defer s.Close() const name = "library/insecure" var rc Registry url := fmt.Sprintf("https://%s/%s", s.Listener.Addr(), name) _, err := rc.Resolve(t.Context(), url) if err == nil || !strings.Contains(err.Error(), "failed to verify") { t.Errorf("err = %v; want cert verification failure", err) } url = fmt.Sprintf("https+insecure://%s/%s", s.Listener.Addr(), name) _, err = rc.Resolve(t.Context(), url) testutil.Check(t, err) } func TestErrorUnmarshal(t *testing.T) { cases := []struct { name string data string want *Error wantErr bool }{ { name: "errors empty", data: `{"errors":[]}`, wantErr: true, }, { name: "errors empty", data: `{"errors":[]}`, wantErr: true, }, { name: "errors single", data: `{"errors":[{"code":"blob_unknown"}]}`, want: &Error{Code: "blob_unknown", Message: ""}, }, { name: "errors multiple", data: `{"errors":[{"code":"blob_unknown"},{"code":"blob_error"}]}`, want: &Error{Code: "blob_unknown", Message: ""}, }, { name: "error empty", data: `{"error":""}`, wantErr: true, }, { name: "error very empty", data: `{}`, wantErr: true, }, { name: "error message", data: `{"error":"message", "code":"code"}`, want: &Error{Code: "code", Message: "message"}, }, { name: "invalid value", data: `{"error": 1}`, wantErr: true, }, } for _, tt := range cases { t.Run(tt.name, func(t *testing.T) { var got Error err := json.Unmarshal([]byte(tt.data), &got) if err != nil { if tt.wantErr { return } t.Errorf("Unmarshal() error = %v", err) // fallthrough and check got } if tt.want == nil { tt.want = &Error{} } if !reflect.DeepEqual(got, *tt.want) { t.Errorf("got = %v; want %v", got, *tt.want) } }) } } // TestParseNameExtendedErrors tests that parseName returns errors messages with enough // detail for users to debug naming issues they may encounter. Previous to this // test, the error messages were not very helpful and each problem was reported // as the same message. // // It is only for testing error messages, not that all invalids and valids are // covered. Those are in other tests for names.Name and blob.Digest. func TestParseNameExtendedErrors(t *testing.T) { cases := []struct { name string err error want string }{} var r Registry for _, tt := range cases { _, _, _, err := r.parseNameExtended(tt.name) if !errors.Is(err, tt.err) { t.Errorf("[%s]: err = %v; want %v", tt.name, err, tt.err) } if err != nil && !strings.Contains(err.Error(), tt.want) { t.Errorf("[%s]: err =\n\t%v\nwant\n\t%v", tt.name, err, tt.want) } } } func TestParseNameExtended(t *testing.T) { cases := []struct { in string scheme string name string digest string err string }{ {in: "http://m", scheme: "http", name: "m"}, {in: "https+insecure://m", scheme: "https+insecure", name: "m"}, {in: "http+insecure://m", err: "unsupported scheme"}, {in: "http://m@sha256:1111111111111111111111111111111111111111111111111111111111111111", scheme: "http", name: "m", digest: "sha256:1111111111111111111111111111111111111111111111111111111111111111"}, {in: "", err: "invalid or missing name"}, {in: "m", scheme: "https", name: "m"}, {in: "://", err: "invalid or missing name"}, {in: "@sha256:deadbeef", err: "invalid digest"}, {in: "@sha256:deadbeef@sha256:deadbeef", err: "invalid digest"}, } for _, tt := range cases { t.Run(tt.in, func(t *testing.T) { var r Registry scheme, n, digest, err := r.parseNameExtended(tt.in) if err != nil { if tt.err == "" { t.Errorf("err = %v; want nil", err) } else if !strings.Contains(err.Error(), tt.err) { t.Errorf("err = %v; want %q", err, tt.err) } } else if tt.err != "" { t.Errorf("err = nil; want %q", tt.err) } if err == nil && !n.IsFullyQualified() { t.Errorf("name = %q; want fully qualified", n) } if scheme != tt.scheme { t.Errorf("scheme = %q; want %q", scheme, tt.scheme) } // smoke-test name is superset of tt.name if !strings.Contains(n.String(), tt.name) { t.Errorf("name = %q; want %q", n, tt.name) } tt.digest = cmp.Or(tt.digest, (&blob.Digest{}).String()) if digest.String() != tt.digest { t.Errorf("digest = %q; want %q", digest, tt.digest) } }) } } func TestUnlink(t *testing.T) { t.Run("found by name", func(t *testing.T) { check := testutil.Checker(t) rc, _ := newRegistryClient(t, nil) // make a blob and link it d := blob.DigestFromBytes("{}") err := blob.PutBytes(rc.Cache, d, "{}") check(err) err = rc.Cache.Link("registry.ollama.ai/library/single:latest", d) check(err) // confirm linked _, err = rc.ResolveLocal("single") check(err) // unlink _, err = rc.Unlink("single") check(err) // confirm unlinked _, err = rc.ResolveLocal("single") if !errors.Is(err, fs.ErrNotExist) { t.Errorf("err = %v; want fs.ErrNotExist", err) } }) t.Run("not found by name", func(t *testing.T) { rc, _ := newRegistryClient(t, nil) ok, err := rc.Unlink("manifestNotFound") if err != nil { t.Fatal(err) } if ok { t.Error("expected not found") } }) } // Many tests from here out, in this file are based on a single blob, "abc", // with the checksum of its sha256 hash. The checksum is: // // "abc" -> sha256:ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad // // Using the literal value instead of a constant with fmt.Xprintf calls proved // to be the most readable and maintainable approach. The sum is consistently // used in the tests and unique so searches do not yield false positives. func checkRequest(t *testing.T, req *http.Request, method, path string) { t.Helper() if got := req.URL.Path; got != path { t.Errorf("URL = %q, want %q", got, path) } if req.Method != method { t.Errorf("Method = %q, want %q", req.Method, method) } } func newRegistryClient(t *testing.T, upstream http.HandlerFunc) (*Registry, context.Context) { s := httptest.NewServer(upstream) t.Cleanup(s.Close) cache, err := blob.Open(t.TempDir()) if err != nil { t.Fatal(err) } ctx := WithTrace(t.Context(), &Trace{ Update: func(l *Layer, n int64, err error) { t.Log("trace:", l.Digest.Short(), n, err) }, }) rc := &Registry{ Cache: cache, HTTPClient: &http.Client{Transport: &http.Transport{ Dial: func(network, addr string) (net.Conn, error) { return net.Dial(network, s.Listener.Addr().String()) }, }}, } return rc, ctx } func TestPullChunked(t *testing.T) { var steps atomic.Int64 c, ctx := newRegistryClient(t, func(w http.ResponseWriter, r *http.Request) { switch steps.Add(1) { case 1: checkRequest(t, r, "GET", "/v2/library/abc/manifests/latest") io.WriteString(w, `{"layers":[{"size":3,"digest":"sha256:ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"}]}`) case 2: checkRequest(t, r, "GET", "/v2/library/abc/chunksums/sha256:ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad") w.Header().Set("Content-Location", "http://blob.store/v2/library/abc/blobs/sha256:ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad") fmt.Fprintf(w, "%s 0-1\n", blob.DigestFromBytes("ab")) fmt.Fprintf(w, "%s 2-2\n", blob.DigestFromBytes("c")) case 3, 4: checkRequest(t, r, "GET", "/v2/library/abc/blobs/sha256:ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad") switch rng := r.Header.Get("Range"); rng { case "bytes=0-1": io.WriteString(w, "ab") case "bytes=2-2": t.Logf("writing c") io.WriteString(w, "c") default: t.Errorf("unexpected range %q", rng) } default: t.Errorf("unexpected steps %d: %v", steps.Load(), r) http.Error(w, "unexpected steps", http.StatusInternalServerError) } }) c.ChunkingThreshold = 1 // force chunking err := c.Pull(ctx, "http://o.com/library/abc") testutil.Check(t, err) _, err = c.Cache.Resolve("o.com/library/abc:latest") testutil.Check(t, err) if g := steps.Load(); g != 4 { t.Fatalf("got %d steps, want 4", g) } } func TestPullCached(t *testing.T) { c, ctx := newRegistryClient(t, func(w http.ResponseWriter, r *http.Request) { checkRequest(t, r, "GET", "/v2/library/abc/manifests/latest") io.WriteString(w, `{"layers":[{"size":3,"digest":"sha256:ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"}]}`) }) check := testutil.Checker(t) // Premeptively cache the blob d, err := blob.ParseDigest("sha256:ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad") check(err) err = blob.PutBytes(c.Cache, d, []byte("abc")) check(err) // Pull only the manifest, which should be enough to resolve the cached blob err = c.Pull(ctx, "http://o.com/library/abc") check(err) } func TestPullManifestError(t *testing.T) { c, ctx := newRegistryClient(t, func(w http.ResponseWriter, r *http.Request) { checkRequest(t, r, "GET", "/v2/library/abc/manifests/latest") w.WriteHeader(http.StatusNotFound) io.WriteString(w, `{"errors":[{"code":"MANIFEST_UNKNOWN"}]}`) }) err := c.Pull(ctx, "http://o.com/library/abc") if err == nil { t.Fatalf("expected error") } var got *Error if !errors.Is(err, ErrModelNotFound) { t.Fatalf("err = %v, want %v", got, ErrModelNotFound) } } func TestPullLayerError(t *testing.T) { c, ctx := newRegistryClient(t, func(w http.ResponseWriter, r *http.Request) { checkRequest(t, r, "GET", "/v2/library/abc/manifests/latest") io.WriteString(w, `!`) }) err := c.Pull(ctx, "http://o.com/library/abc") if err == nil { t.Fatalf("expected error") } var want *json.SyntaxError if !errors.As(err, &want) { t.Fatalf("err = %T, want %T", err, want) } } func TestPullLayerChecksumError(t *testing.T) { var step atomic.Int64 c, _ := newRegistryClient(t, func(w http.ResponseWriter, r *http.Request) { switch step.Add(1) { case 1: checkRequest(t, r, "GET", "/v2/library/abc/manifests/latest") io.WriteString(w, `{"layers":[{"size":3,"digest":"sha256:ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"}]}`) case 2: checkRequest(t, r, "GET", "/v2/library/abc/chunksums/sha256:ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad") w.Header().Set("Content-Location", "http://blob.store/v2/library/abc/blobs/sha256:ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad") fmt.Fprintf(w, "%s 0-1\n", blob.DigestFromBytes("ab")) fmt.Fprintf(w, "%s 2-2\n", blob.DigestFromBytes("c")) case 3: w.WriteHeader(http.StatusNotFound) io.WriteString(w, `{"errors":[{"code":"BLOB_UNKNOWN"}]}`) case 4: io.WriteString(w, "c") default: t.Errorf("unexpected steps %d: %v", step.Load(), r) http.Error(w, "unexpected steps", http.StatusInternalServerError) } }) c.MaxStreams = 1 c.ChunkingThreshold = 1 // force chunking var written atomic.Int64 ctx := WithTrace(t.Context(), &Trace{ Update: func(l *Layer, n int64, err error) { t.Log("trace:", l.Digest.Short(), n, err) written.Add(n) }, }) err := c.Pull(ctx, "http://o.com/library/abc") var got *Error if !errors.As(err, &got) || got.Code != "BLOB_UNKNOWN" { t.Fatalf("err = %v, want %v", err, got) } if g := written.Load(); g != 1 { t.Fatalf("wrote %d bytes, want 1", g) } } func TestPullChunksumStreamError(t *testing.T) { var step atomic.Int64 c, ctx := newRegistryClient(t, func(w http.ResponseWriter, r *http.Request) { switch step.Add(1) { case 1: checkRequest(t, r, "GET", "/v2/library/abc/manifests/latest") io.WriteString(w, `{"layers":[{"size":3,"digest":"sha256:ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"}]}`) case 2: w.Header().Set("Content-Location", "http://blob.store/v2/library/abc/blobs/sha256:ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad") // Write one valid chunksum and one invalid chunksum fmt.Fprintf(w, "%s 0-1\n", blob.DigestFromBytes("ab")) // valid fmt.Fprint(w, "sha256:!") // invalid case 3: io.WriteString(w, "ab") default: t.Errorf("unexpected steps %d: %v", step.Load(), r) http.Error(w, "unexpected steps", http.StatusInternalServerError) } }) c.ChunkingThreshold = 1 // force chunking got := c.Pull(ctx, "http://o.com/library/abc") if !errors.Is(got, ErrIncomplete) { t.Fatalf("err = %v, want %v", got, ErrIncomplete) } } type flushAfterWriter struct { w io.Writer } func (f *flushAfterWriter) Write(p []byte) (n int, err error) { n, err = f.w.Write(p) f.w.(http.Flusher).Flush() // panic if not a flusher return } func TestPullChunksumStreaming(t *testing.T) { csr, csw := io.Pipe() defer csw.Close() var step atomic.Int64 c, _ := newRegistryClient(t, func(w http.ResponseWriter, r *http.Request) { switch step.Add(1) { case 1: checkRequest(t, r, "GET", "/v2/library/abc/manifests/latest") io.WriteString(w, `{"layers":[{"size":3,"digest":"sha256:ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"}]}`) case 2: w.Header().Set("Content-Location", "http://blob.store/v2/library/abc/blobs/sha256:ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad") fw := &flushAfterWriter{w} // ensure client gets data as it arrives by aggressively flushing _, err := io.Copy(fw, csr) if err != nil { t.Errorf("copy: %v", err) } case 3: io.WriteString(w, "ab") case 4: io.WriteString(w, "c") default: t.Errorf("unexpected steps %d: %v", step.Load(), r) http.Error(w, "unexpected steps", http.StatusInternalServerError) } }) c.ChunkingThreshold = 1 // force chunking update := make(chan int64, 1) ctx := WithTrace(t.Context(), &Trace{ Update: func(l *Layer, n int64, err error) { t.Log("trace:", l.Digest.Short(), n, err) if n > 0 { update <- n } }, }) errc := make(chan error, 1) go func() { errc <- c.Pull(ctx, "http://o.com/library/abc") }() // Send first chunksum and ensure it kicks off work immediately fmt.Fprintf(csw, "%s 0-1\n", blob.DigestFromBytes("ab")) if g := <-update; g != 2 { t.Fatalf("got %d, want 2", g) } // now send the second chunksum and ensure it kicks off work immediately fmt.Fprintf(csw, "%s 2-2\n", blob.DigestFromBytes("c")) if g := <-update; g != 3 { t.Fatalf("got %d, want 3", g) } csw.Close() testutil.Check(t, <-errc) } func TestPullChunksumsCached(t *testing.T) { var step atomic.Int64 c, _ := newRegistryClient(t, func(w http.ResponseWriter, r *http.Request) { switch step.Add(1) { case 1: checkRequest(t, r, "GET", "/v2/library/abc/manifests/latest") io.WriteString(w, `{"layers":[{"size":3,"digest":"sha256:ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"}]}`) case 2: w.Header().Set("Content-Location", "http://blob.store/v2/library/abc/blobs/sha256:ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad") fmt.Fprintf(w, "%s 0-1\n", blob.DigestFromBytes("ab")) fmt.Fprintf(w, "%s 2-2\n", blob.DigestFromBytes("c")) case 3, 4: switch rng := r.Header.Get("Range"); rng { case "bytes=0-1": io.WriteString(w, "ab") case "bytes=2-2": io.WriteString(w, "c") default: t.Errorf("unexpected range %q", rng) } default: t.Errorf("unexpected steps %d: %v", step.Load(), r) http.Error(w, "unexpected steps", http.StatusInternalServerError) } }) c.MaxStreams = 1 // force serial processing of chunksums c.ChunkingThreshold = 1 // force chunking ctx, cancel := context.WithCancel(t.Context()) defer cancel() // Cancel the pull after the first chunksum is processed, but before // the second chunksum is processed (which is waiting because // MaxStreams=1). This should cause the second chunksum to error out // leaving the blob incomplete. ctx = WithTrace(ctx, &Trace{ Update: func(l *Layer, n int64, err error) { if n > 0 { cancel() } }, }) err := c.Pull(ctx, "http://o.com/library/abc") if !errors.Is(err, context.Canceled) { t.Fatalf("err = %v, want %v", err, context.Canceled) } _, err = c.Cache.Resolve("o.com/library/abc:latest") if !errors.Is(err, fs.ErrNotExist) { t.Fatalf("err = %v, want nil", err) } // Reset state and pull again to ensure the blob chunks that should // have been cached are, and the remaining chunk was downloaded, making // the blob complete. step.Store(0) var written atomic.Int64 var cached atomic.Int64 ctx = WithTrace(t.Context(), &Trace{ Update: func(l *Layer, n int64, err error) { t.Log("trace:", l.Digest.Short(), n, err) if errors.Is(err, ErrCached) { cached.Add(n) } written.Add(n) }, }) check := testutil.Checker(t) err = c.Pull(ctx, "http://o.com/library/abc") check(err) _, err = c.Cache.Resolve("o.com/library/abc:latest") check(err) if g := written.Load(); g != 5 { t.Fatalf("wrote %d bytes, want 3", g) } if g := cached.Load(); g != 2 { // "ab" should have been cached t.Fatalf("cached %d bytes, want 5", g) } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/server/internal/testutil/testutil.go
server/internal/testutil/testutil.go
package testutil import ( "bytes" "io" "log/slog" "os" "path/filepath" "testing" "time" ) // LogWriter returns an [io.Writer] that logs each Write using t.Log. func LogWriter(t *testing.T) io.Writer { return testWriter{t} } type testWriter struct{ t *testing.T } func (w testWriter) Write(b []byte) (int, error) { w.t.Logf("%s", b) return len(b), nil } // Slogger returns a [*slog.Logger] that writes each message // using t.Log. func Slogger(t *testing.T) *slog.Logger { return slog.New(slog.NewTextHandler(LogWriter(t), nil)) } // SlogBuffer returns a [*slog.Logger] that writes each message to out. func SlogBuffer() (lg *slog.Logger, out *bytes.Buffer) { var buf bytes.Buffer lg = slog.New(slog.NewTextHandler(&buf, nil)) return lg, &buf } // Check calls t.Fatal(err) if err is not nil. func Check(t *testing.T, err error) { if err != nil { t.Helper() t.Fatal(err) } } // CheckFunc exists so other packages do not need to invent their own type for // taking a Check function. type CheckFunc func(err error) // Checker returns a check function that // calls t.Fatal if err is not nil. func Checker(t *testing.T) (check func(err error)) { return func(err error) { if err != nil { t.Helper() t.Fatal(err) } } } // StopPanic runs f but silently recovers from any panic f causes. // The normal usage is: // // testutil.StopPanic(func() { // callThatShouldPanic() // t.Errorf("callThatShouldPanic did not panic") // }) func StopPanic(f func()) { defer func() { recover() }() f() } // CheckTime calls t.Fatalf if got != want. Included in the error message is // want.Sub(got) to help diagnose the difference, along with their values in // UTC. func CheckTime(t *testing.T, got, want time.Time) { t.Helper() if !got.Equal(want) { t.Fatalf("got %v, want %v (%v)", got.UTC(), want.UTC(), want.Sub(got)) } } // WriteFile writes data to a file named name. It makes the directory if it // doesn't exist and sets the file mode to perm. // // The name must be a relative path and must not contain .. or start with a /; // otherwise WriteFile will panic. func WriteFile[S []byte | string](t testing.TB, name string, data S) { t.Helper() if filepath.IsAbs(name) { t.Fatalf("WriteFile: name must be a relative path, got %q", name) } name = filepath.Clean(name) dir := filepath.Dir(name) if err := os.MkdirAll(dir, 0o755); err != nil { t.Fatal(err) } if err := os.WriteFile(name, []byte(data), 0o644); err != nil { t.Fatal(err) } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/server/internal/manifest/manifest.go
server/internal/manifest/manifest.go
// Package manifest provides documentation for the Ollama manifest format. // This package contains no code. // // # Manifests // // A manifest is a JSON object that describes a model. The JSON object has a // single field "layers" which is a list of layers that make up the model. // A layer is a single, logical unit of a model. Layers are stored in the cache // as files with the name of the digest of the layer. Layers are pushed and // pulled from the registry as blobs. // // A layer is represented as a JSON object with the following fields: // // - "digest": The digest of the layer. // - "mediaType": The media type of the layer. // - "size": The size of the layer in bytes. // // Layers are typically stored in a blob store, such as a registry, and are // referenced by their digest. This package does not define how layers are // stored or retrieved. // // # Configuration Layer // // The configuration of a model is represented as a layer with the media type: // // application/vnd.ollama.image.config; type=<type> // // The "type" parameter in the media type specifies the format of the // configuration (e.g., "safetensor" or "gguf"). // // There may be only one configuration layer in a model. // // # Template Layer // // The model template is a layer with the media type: // // application/vnd.ollama.image.template; [name=<name>] // // The "name" parameter in the media type specifies the name of the template as // for lookup at runtime. The name is optional and may be omitted. If omitted, // the template is the default template for the model. // // # Tensor Layers // // The tensors of a model are represented as layers with the media type: // // application/vnd.ollama.image.tensor; name=<name>; dtype=<dtype>; shape=<shape> // // The "name" parameter in the media type specifies the name of the tensor as // defined in the model's configuration and are bound only by the rules for // names as defined in the configuration format, as represented by the // configuration's "type". // // The "dtype" parameter in the media type specifies the data type of the tensor // as a string. // // TODO: Define more specifically how to represent data types as strings. // // The "shape" parameter in the media type specifies the shape of the tensor as // a comma-separated list of integers; one per dimension. // // # Tokenization Layers // // The tokenization of a model is represented as a layer with the media type: // // application/vnd.ollama.image.tokenizer // // The configuration of the tokenizer is represented as a layer with the media type: // // application/vnd.ollama.image.tokenizer.config // // # Miscellaneous Layers // // These extra layer mime types are reserved: // // application/vnd.ollama.image.license // // This layer contains one of the many licenses for the model in plain text. // // # Example Manifest // // The following is an example manifest containing a configuration, a model // template, and two tensors (digests shortened for brevity): // // { // "layers": [{ // "digest": "sha256:a...", // "mediaType": "application/vnd.ollama.image.config; type=safetensors", // "size": 1234 // },{ // "digest": "sha256:b...", // "mediaType": "application/vnd.ollama.image.template", // "size": 5678 // },{ // "digest": "sha256:c...", // "mediaType": "application/vnd.ollama.image.tensor; name=input; dtype=F32; shape=1,2,3", // "size": 9012 // },{ // "digest": "sha256:d...", // "mediaType": "application/vnd.ollama.image.tensor; name=output; dtype=I32; shape=4,5,6", // "size": 3456 // }] // } // // # Legacy Media Types // // The appliaction/vnd.ollama.image.model media type is deprecated, but will // remain supported for backwards compatibility, for some undefined amount of // time. New models should use the media types defined above. // // # Reserved media types // // The media type prefix "application/vnd.ollama.image." is reserved for // defining new media types for layers known to Ollama. Currently, all other // prefixes are ignored by official Ollama registry clients. package manifest
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/server/internal/registry/server_test.go
server/internal/registry/server_test.go
package registry import ( "bytes" "context" "encoding/json" "io" "io/fs" "net" "net/http" "net/http/httptest" "os" "regexp" "strings" "sync" "testing" "github.com/ollama/ollama/server/internal/cache/blob" "github.com/ollama/ollama/server/internal/client/ollama" "github.com/ollama/ollama/server/internal/testutil" "golang.org/x/tools/txtar" _ "embed" ) type panicTransport struct{} func (t *panicTransport) RoundTrip(r *http.Request) (*http.Response, error) { panic("unexpected RoundTrip call") } var panicOnRoundTrip = &http.Client{Transport: &panicTransport{}} // bytesResetter is an interface for types that can be reset and return a byte // slice, only. This is to prevent inadvertent use of bytes.Buffer.Read/Write // etc for the purpose of checking logs. type bytesResetter interface { Bytes() []byte Reset() } func newTestServer(t *testing.T, upstreamRegistry http.HandlerFunc) *Local { t.Helper() dir := t.TempDir() err := os.CopyFS(dir, os.DirFS("testdata/models")) if err != nil { t.Fatal(err) } c, err := blob.Open(dir) if err != nil { t.Fatal(err) } client := panicOnRoundTrip if upstreamRegistry != nil { s := httptest.NewTLSServer(upstreamRegistry) t.Cleanup(s.Close) tr := s.Client().Transport.(*http.Transport).Clone() tr.DialContext = func(ctx context.Context, _, _ string) (net.Conn, error) { var d net.Dialer return d.DialContext(ctx, "tcp", s.Listener.Addr().String()) } client = &http.Client{Transport: tr} } rc := &ollama.Registry{ Cache: c, HTTPClient: client, Mask: "example.com/library/_:latest", } l := &Local{ Client: rc, Logger: testutil.Slogger(t), } return l } func (s *Local) send(t *testing.T, method, path, body string) *httptest.ResponseRecorder { t.Helper() ctx := ollama.WithTrace(t.Context(), &ollama.Trace{ Update: func(l *ollama.Layer, n int64, err error) { t.Logf("update: %s %d %v", l.Digest, n, err) }, }) req := httptest.NewRequestWithContext(ctx, method, path, strings.NewReader(body)) return s.sendRequest(t, req) } func (s *Local) sendRequest(t *testing.T, req *http.Request) *httptest.ResponseRecorder { t.Helper() w := httptest.NewRecorder() s.ServeHTTP(w, req) return w } type invalidReader struct{} func (r *invalidReader) Read(p []byte) (int, error) { return 0, os.ErrInvalid } // captureLogs is a helper to capture logs from the server. It returns a // shallow copy of the server with a new logger and a bytesResetter for the // logs. func captureLogs(t *testing.T, s *Local) (*Local, bytesResetter) { t.Helper() log, logs := testutil.SlogBuffer() l := *s // shallow copy l.Logger = log return &l, logs } func TestServerDelete(t *testing.T) { check := testutil.Checker(t) s := newTestServer(t, nil) _, err := s.Client.ResolveLocal("smol") check(err) got := s.send(t, "DELETE", "/api/delete", `{"model": "smol"}`) if got.Code != 200 { t.Fatalf("Code = %d; want 200", got.Code) } _, err = s.Client.ResolveLocal("smol") if err == nil { t.Fatal("expected smol to have been deleted") } got = s.send(t, "DELETE", "/api/delete", `!`) checkErrorResponse(t, got, 400, "bad_request", "invalid character '!' looking for beginning of value") got = s.send(t, "GET", "/api/delete", `{"model": "smol"}`) checkErrorResponse(t, got, 405, "method_not_allowed", "method not allowed") got = s.send(t, "DELETE", "/api/delete", ``) checkErrorResponse(t, got, 400, "bad_request", "empty request body") got = s.send(t, "DELETE", "/api/delete", `{"model": "://"}`) checkErrorResponse(t, got, 400, "bad_request", "invalid or missing name") got = s.send(t, "DELETE", "/unknown_path", `{}`) // valid body checkErrorResponse(t, got, 404, "not_found", "not found") s, logs := captureLogs(t, s) req := httptest.NewRequestWithContext(t.Context(), "DELETE", "/api/delete", &invalidReader{}) got = s.sendRequest(t, req) checkErrorResponse(t, got, 500, "internal_error", "internal server error") ok, err := regexp.Match(`ERROR.*error="invalid argument"`, logs.Bytes()) check(err) if !ok { t.Logf("logs:\n%s", logs) t.Fatalf("expected log to contain ERROR with invalid argument") } } //go:embed testdata/registry.txt var registryTXT []byte var registryFS = sync.OnceValue(func() fs.FS { // Txtar gets hung up on \r\n line endings, so we need to convert them // to \n when parsing the txtar on Windows. data := bytes.ReplaceAll(registryTXT, []byte("\r\n"), []byte("\n")) a := txtar.Parse(data) fsys, err := txtar.FS(a) if err != nil { panic(err) } return fsys }) func TestServerPull(t *testing.T) { modelsHandler := http.FileServerFS(registryFS()) s := newTestServer(t, func(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { case "/v2/library/BOOM/manifests/latest": w.WriteHeader(999) io.WriteString(w, `{"error": "boom"}`) case "/v2/library/unknown/manifests/latest": w.WriteHeader(404) io.WriteString(w, `{"errors": [{"code": "MANIFEST_UNKNOWN", "message": "manifest unknown"}]}`) default: t.Logf("serving blob: %s", r.URL.Path) modelsHandler.ServeHTTP(w, r) } }) checkResponse := func(got *httptest.ResponseRecorder, wantlines string) { t.Helper() if got.Code != 200 { t.Errorf("Code = %d; want 200", got.Code) } gotlines := got.Body.String() if strings.TrimSpace(gotlines) == "" { gotlines = "<empty>" } t.Logf("got:\n%s", gotlines) for want := range strings.Lines(wantlines) { want = strings.TrimSpace(want) want, unwanted := strings.CutPrefix(want, "!") want = strings.TrimSpace(want) if !unwanted && !strings.Contains(gotlines, want) { t.Errorf("\t! missing %q in body", want) } if unwanted && strings.Contains(gotlines, want) { t.Errorf("\t! unexpected %q in body", want) } } } got := s.send(t, "POST", "/api/pull", `{"model": "smol"}`) checkResponse(got, ` {"status":"pulling manifest"} {"digest":"sha256:68e0ec597aee59d35f8dc44942d7b17d471ade10d3aca07a5bb7177713950312","total":5,"completed":5} {"status":"verifying sha256 digest"} {"status":"writing manifest"} {"status":"success"} `) got = s.send(t, "POST", "/api/pull", `{"model": "unknown"}`) checkResponse(got, ` {"code":"not_found","error":"model \"unknown\" not found"} `) got = s.send(t, "DELETE", "/api/pull", `{"model": "smol"}`) checkErrorResponse(t, got, 405, "method_not_allowed", "method not allowed") got = s.send(t, "POST", "/api/pull", `!`) checkErrorResponse(t, got, 400, "bad_request", "invalid character '!' looking for beginning of value") got = s.send(t, "POST", "/api/pull", ``) checkErrorResponse(t, got, 400, "bad_request", "empty request body") got = s.send(t, "POST", "/api/pull", `{"model": "://"}`) checkResponse(got, ` {"code":"bad_request","error":"invalid or missing name: \"\""} `) // Non-streaming pulls got = s.send(t, "POST", "/api/pull", `{"model": "://", "stream": false}`) checkErrorResponse(t, got, 400, "bad_request", "invalid or missing name") got = s.send(t, "POST", "/api/pull", `{"model": "smol", "stream": false}`) checkResponse(got, ` {"status":"success"} !digest !total !completed `) got = s.send(t, "POST", "/api/pull", `{"model": "unknown", "stream": false}`) checkErrorResponse(t, got, 404, "not_found", "model not found") } func TestServerUnknownPath(t *testing.T) { s := newTestServer(t, nil) got := s.send(t, "DELETE", "/api/unknown", `{}`) checkErrorResponse(t, got, 404, "not_found", "not found") var fellback bool s.Fallback = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fellback = true }) got = s.send(t, "DELETE", "/api/unknown", `{}`) if !fellback { t.Fatal("expected Fallback to be called") } if got.Code != 200 { t.Fatalf("Code = %d; want 200", got.Code) } } func checkErrorResponse(t *testing.T, got *httptest.ResponseRecorder, status int, code, msg string) { t.Helper() var printedBody bool errorf := func(format string, args ...any) { t.Helper() if !printedBody { t.Logf("BODY:\n%s", got.Body.String()) printedBody = true } t.Errorf(format, args...) } if got.Code != status { errorf("Code = %d; want %d", got.Code, status) } // unmarshal the error as *ollama.Error (proving *serverError is an *ollama.Error) var e *ollama.Error if err := json.Unmarshal(got.Body.Bytes(), &e); err != nil { errorf("unmarshal error: %v", err) t.FailNow() } if e.Code != code { errorf("Code = %q; want %q", e.Code, code) } if !strings.Contains(e.Message, msg) { errorf("Message = %q; want to contain %q", e.Message, msg) } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/server/internal/registry/server.go
server/internal/registry/server.go
// Package registry implements an http.Handler for handling local Ollama API // model management requests. See [Local] for details. package registry import ( "cmp" "context" "encoding/json" "errors" "fmt" "io" "log/slog" "net/http" "slices" "strings" "sync" "time" "github.com/ollama/ollama/server/internal/cache/blob" "github.com/ollama/ollama/server/internal/client/ollama" "github.com/ollama/ollama/server/internal/internal/backoff" ) // Local implements an http.Handler for handling local Ollama API model // management requests, such as pushing, pulling, and deleting models. // // It can be arranged for all unknown requests to be passed through to a // fallback handler, if one is provided. type Local struct { Client *ollama.Registry // required Logger *slog.Logger // required // Fallback, if set, is used to handle requests that are not handled by // this handler. Fallback http.Handler // Prune, if set, is called to prune the local disk cache after a model // is deleted. Prune func() error // optional } // serverError is like ollama.Error, but with a Status field for the HTTP // response code. We want to avoid adding that field to ollama.Error because it // would always be 0 to clients (we don't want to leak the status code in // errors), and so it would be confusing to have a field that is always 0. type serverError struct { Status int `json:"-"` // TODO(bmizerany): Decide if we want to keep this and maybe // bring back later. Code string `json:"code"` Message string `json:"error"` } func (e serverError) Error() string { return e.Message } // Common API errors var ( errMethodNotAllowed = &serverError{405, "method_not_allowed", "method not allowed"} errNotFound = &serverError{404, "not_found", "not found"} errModelNotFound = &serverError{404, "not_found", "model not found"} errInternalError = &serverError{500, "internal_error", "internal server error"} ) type statusCodeRecorder struct { _status int // use status() to get the status code http.ResponseWriter } func (r *statusCodeRecorder) WriteHeader(status int) { if r._status == 0 { r._status = status r.ResponseWriter.WriteHeader(status) } } func (r *statusCodeRecorder) Write(b []byte) (int, error) { r._status = r.status() return r.ResponseWriter.Write(b) } var ( _ http.ResponseWriter = (*statusCodeRecorder)(nil) _ http.CloseNotifier = (*statusCodeRecorder)(nil) _ http.Flusher = (*statusCodeRecorder)(nil) ) // CloseNotify implements the http.CloseNotifier interface, for Gin. Remove with Gin. // // It panics if the underlying ResponseWriter is not a CloseNotifier. func (r *statusCodeRecorder) CloseNotify() <-chan bool { return r.ResponseWriter.(http.CloseNotifier).CloseNotify() } // Flush implements the http.Flusher interface, for Gin. Remove with Gin. // // It panics if the underlying ResponseWriter is not a Flusher. func (r *statusCodeRecorder) Flush() { r.ResponseWriter.(http.Flusher).Flush() } func (r *statusCodeRecorder) status() int { return cmp.Or(r._status, 200) } func (s *Local) ServeHTTP(w http.ResponseWriter, r *http.Request) { rec := &statusCodeRecorder{ResponseWriter: w} s.serveHTTP(rec, r) } func (s *Local) serveHTTP(rec *statusCodeRecorder, r *http.Request) { var errattr slog.Attr proxied, err := func() (bool, error) { switch r.URL.Path { case "/api/delete": return false, s.handleDelete(rec, r) case "/api/pull": return false, s.handlePull(rec, r) default: if s.Fallback != nil { s.Fallback.ServeHTTP(rec, r) return true, nil } return false, errNotFound } }() if err != nil { // We always log the error, so fill in the error log attribute errattr = slog.String("error", err.Error()) var e *serverError switch { case errors.As(err, &e): case errors.Is(err, ollama.ErrNameInvalid): e = &serverError{400, "bad_request", err.Error()} default: e = errInternalError } data, err := json.Marshal(e) if err != nil { // unreachable panic(err) } rec.Header().Set("Content-Type", "application/json") rec.WriteHeader(e.Status) rec.Write(data) // fallthrough to log } if !proxied { // we're only responsible for logging if we handled the request var level slog.Level if rec.status() >= 500 { level = slog.LevelError } else if rec.status() >= 400 { level = slog.LevelWarn } s.Logger.LogAttrs(r.Context(), level, "http", errattr, // report first in line to make it easy to find // TODO(bmizerany): Write a test to ensure that we are logging // all of this correctly. That also goes for the level+error // logic above. slog.Int("status", rec.status()), slog.String("method", r.Method), slog.String("path", r.URL.Path), slog.Int64("content-length", r.ContentLength), slog.String("remote", r.RemoteAddr), slog.String("proto", r.Proto), slog.String("query", r.URL.RawQuery), ) } } type params struct { // DeprecatedName is the name of the model to push, pull, or delete, // but is deprecated. New clients should use [Model] instead. // // Use [model()] to get the model name for both old and new API requests. DeprecatedName string `json:"name"` // Model is the name of the model to push, pull, or delete. // // Use [model()] to get the model name for both old and new API requests. Model string `json:"model"` // AllowNonTLS is a flag that indicates a client using HTTP // is doing so, deliberately. // // Deprecated: This field is ignored and only present for this // deprecation message. It should be removed in a future release. // // Users can just use http or https+insecure to show intent to // communicate they want to do insecure things, without awkward and // confusing flags such as this. AllowNonTLS bool `json:"insecure"` // Stream, if true, will make the server send progress updates in a // streaming of JSON objects. If false, the server will send a single // JSON object with the final status as "success", or an error object // if an error occurred. // // Unfortunately, this API was designed to be a bit awkward. Stream is // defined to default to true if not present, so we need a way to check // if the client decisively set it to false. So, we use a pointer to a // bool. Gross. // // Use [stream()] to get the correct value for this field. Stream *bool `json:"stream"` } // model returns the model name for both old and new API requests. func (p params) model() string { return cmp.Or(p.Model, p.DeprecatedName) } func (p params) stream() bool { if p.Stream == nil { return true } return *p.Stream } func (s *Local) handleDelete(_ http.ResponseWriter, r *http.Request) error { if r.Method != "DELETE" { return errMethodNotAllowed } p, err := decodeUserJSON[*params](r.Body) if err != nil { return err } ok, err := s.Client.Unlink(p.model()) if err != nil { return err } if !ok { return errModelNotFound } if s.Prune != nil { return s.Prune() } return nil } type progressUpdateJSON struct { Error string `json:"error,omitempty,omitzero"` Status string `json:"status,omitempty,omitzero"` Digest blob.Digest `json:"digest,omitempty,omitzero"` Total int64 `json:"total,omitempty,omitzero"` Completed int64 `json:"completed,omitempty,omitzero"` } func (s *Local) handlePull(w http.ResponseWriter, r *http.Request) error { if r.Method != "POST" { return errMethodNotAllowed } p, err := decodeUserJSON[*params](r.Body) if err != nil { return err } enc := json.NewEncoder(w) if !p.stream() { if err := s.Client.Pull(r.Context(), p.model()); err != nil { if errors.Is(err, ollama.ErrModelNotFound) { return errModelNotFound } return err } enc.Encode(progressUpdateJSON{Status: "success"}) return nil } var mu sync.Mutex var progress []progressUpdateJSON flushProgress := func() { mu.Lock() progress := slices.Clone(progress) // make a copy and release lock before encoding to the wire mu.Unlock() for _, p := range progress { enc.Encode(p) } fl, _ := w.(http.Flusher) if fl != nil { fl.Flush() } } t := time.NewTicker(1<<63 - 1) // "unstarted" timer start := sync.OnceFunc(func() { flushProgress() // flush initial state t.Reset(100 * time.Millisecond) }) ctx := ollama.WithTrace(r.Context(), &ollama.Trace{ Update: func(l *ollama.Layer, n int64, err error) { if err != nil && !errors.Is(err, ollama.ErrCached) { s.Logger.Error("pulling", "model", p.model(), "error", err) return } func() { mu.Lock() defer mu.Unlock() for i, p := range progress { if p.Digest == l.Digest { progress[i].Completed = n return } } progress = append(progress, progressUpdateJSON{ Digest: l.Digest, Total: l.Size, }) }() // Block flushing progress updates until every // layer is accounted for. Clients depend on a // complete model size to calculate progress // correctly; if they use an incomplete total, // progress indicators would erratically jump // as new layers are registered. start() }, }) done := make(chan error, 1) go func() (err error) { defer func() { done <- err }() for _, err := range backoff.Loop(ctx, 3*time.Second) { if err != nil { return err } err := s.Client.Pull(ctx, p.model()) if canRetry(err) { continue } return err } return nil }() enc.Encode(progressUpdateJSON{Status: "pulling manifest"}) for { select { case <-t.C: flushProgress() case err := <-done: flushProgress() if err != nil { if errors.Is(err, ollama.ErrModelNotFound) { return &serverError{ Status: 404, Code: "not_found", Message: fmt.Sprintf("model %q not found", p.model()), } } else { return err } } // Emulate old client pull progress (for now): enc.Encode(progressUpdateJSON{Status: "verifying sha256 digest"}) enc.Encode(progressUpdateJSON{Status: "writing manifest"}) enc.Encode(progressUpdateJSON{Status: "success"}) return nil } } } func decodeUserJSON[T any](r io.Reader) (T, error) { var v T err := json.NewDecoder(r).Decode(&v) if err == nil { return v, nil } var zero T // Not sure why, but I can't seem to be able to use: // // errors.As(err, &json.UnmarshalTypeError{}) // // This is working fine in stdlib, so I'm not sure what rules changed // and why this no longer works here. So, we do it the verbose way. var a *json.UnmarshalTypeError var b *json.SyntaxError if errors.As(err, &a) || errors.As(err, &b) { err = &serverError{Status: 400, Message: err.Error(), Code: "bad_request"} } if errors.Is(err, io.EOF) { err = &serverError{Status: 400, Message: "empty request body", Code: "bad_request"} } return zero, err } func canRetry(err error) bool { if err == nil { return false } var oe *ollama.Error if errors.As(err, &oe) { return oe.Temporary() } s := err.Error() return cmp.Or( errors.Is(err, context.DeadlineExceeded), strings.Contains(s, "unreachable"), strings.Contains(s, "no route to host"), strings.Contains(s, "connection reset by peer"), ) }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/format/time.go
format/time.go
package format import ( "fmt" "math" "strings" "time" ) // humanDuration returns a human-readable approximation of a // duration (eg. "About a minute", "4 hours ago", etc.). func humanDuration(d time.Duration) string { seconds := int(d.Seconds()) switch { case seconds < 1: return "Less than a second" case seconds == 1: return "1 second" case seconds < 60: return fmt.Sprintf("%d seconds", seconds) } minutes := int(d.Minutes()) switch { case minutes == 1: return "About a minute" case minutes < 60: return fmt.Sprintf("%d minutes", minutes) } hours := int(math.Round(d.Hours())) switch { case hours == 1: return "About an hour" case hours < 48: return fmt.Sprintf("%d hours", hours) case hours < 24*7*2: return fmt.Sprintf("%d days", hours/24) case hours < 24*30*2: return fmt.Sprintf("%d weeks", hours/24/7) case hours < 24*365*2: return fmt.Sprintf("%d months", hours/24/30) } return fmt.Sprintf("%d years", int(d.Hours())/24/365) } func HumanTime(t time.Time, zeroValue string) string { return humanTime(t, zeroValue) } func HumanTimeLower(t time.Time, zeroValue string) string { return strings.ToLower(humanTime(t, zeroValue)) } func humanTime(t time.Time, zeroValue string) string { if t.IsZero() { return zeroValue } delta := time.Since(t) if int(delta.Hours())/24/365 < -20 { return "Forever" } else if delta < 0 { return humanDuration(-delta) + " from now" } return humanDuration(delta) + " ago" }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/format/time_test.go
format/time_test.go
package format import ( "testing" "time" ) func assertEqual(t *testing.T, a any, b any) { if a != b { t.Errorf("Assert failed, expected %v, got %v", b, a) } } func TestHumanTime(t *testing.T) { now := time.Now() t.Run("zero value", func(t *testing.T) { assertEqual(t, HumanTime(time.Time{}, "never"), "never") }) t.Run("time in the future", func(t *testing.T) { v := now.Add(48 * time.Hour) assertEqual(t, HumanTime(v, ""), "2 days from now") }) t.Run("time in the past", func(t *testing.T) { v := now.Add(-48 * time.Hour) assertEqual(t, HumanTime(v, ""), "2 days ago") }) t.Run("soon", func(t *testing.T) { v := now.Add(800 * time.Millisecond) assertEqual(t, HumanTime(v, ""), "Less than a second from now") }) t.Run("time way in the future", func(t *testing.T) { v := now.Add(24 * time.Hour * 365 * 200) assertEqual(t, HumanTime(v, ""), "Forever") }) t.Run("time way in the future lowercase", func(t *testing.T) { v := now.Add(24 * time.Hour * 365 * 200) assertEqual(t, HumanTimeLower(v, ""), "forever") }) }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/format/bytes_test.go
format/bytes_test.go
package format import ( "testing" ) func TestHumanBytes(t *testing.T) { type testCase struct { input int64 expected string } tests := []testCase{ // Test bytes (B) {0, "0 B"}, {1, "1 B"}, {999, "999 B"}, // Test kilobytes (KB) {1000, "1 KB"}, {1500, "1.5 KB"}, {999999, "999 KB"}, // Test megabytes (MB) {1000000, "1 MB"}, {1500000, "1.5 MB"}, {999999999, "999 MB"}, // Test gigabytes (GB) {1000000000, "1 GB"}, {1500000000, "1.5 GB"}, {999999999999, "999 GB"}, // Test terabytes (TB) {1000000000000, "1 TB"}, {1500000000000, "1.5 TB"}, {1999999999999, "2.0 TB"}, // Test fractional values {1234, "1.2 KB"}, {1234567, "1.2 MB"}, {1234567890, "1.2 GB"}, } for _, tc := range tests { t.Run(tc.expected, func(t *testing.T) { result := HumanBytes(tc.input) if result != tc.expected { t.Errorf("Expected %s, got %s", tc.expected, result) } }) } } func TestHumanBytes2(t *testing.T) { type testCase struct { input uint64 expected string } tests := []testCase{ // Test bytes (B) {0, "0 B"}, {1, "1 B"}, {1023, "1023 B"}, // Test kibibytes (KiB) {1024, "1.0 KiB"}, {1536, "1.5 KiB"}, {1048575, "1024.0 KiB"}, // Test mebibytes (MiB) {1048576, "1.0 MiB"}, {1572864, "1.5 MiB"}, {1073741823, "1024.0 MiB"}, // Test gibibytes (GiB) {1073741824, "1.0 GiB"}, {1610612736, "1.5 GiB"}, {2147483648, "2.0 GiB"}, } for _, tc := range tests { t.Run(tc.expected, func(t *testing.T) { result := HumanBytes2(tc.input) if result != tc.expected { t.Errorf("Expected %s, got %s", tc.expected, result) } }) } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/format/format.go
format/format.go
package format import ( "fmt" "math" "strconv" ) const ( Thousand = 1000 Million = Thousand * 1000 Billion = Million * 1000 ) func HumanNumber(b uint64) string { switch { case b >= Billion: number := float64(b) / Billion if number == math.Floor(number) { return fmt.Sprintf("%.0fB", number) // no decimals if whole number } return fmt.Sprintf("%.1fB", number) // one decimal if not a whole number case b >= Million: number := float64(b) / Million if number == math.Floor(number) { return fmt.Sprintf("%.0fM", number) // no decimals if whole number } return fmt.Sprintf("%.2fM", number) // two decimals if not a whole number case b >= Thousand: return fmt.Sprintf("%.0fK", float64(b)/Thousand) default: return strconv.FormatUint(b, 10) } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/format/bytes.go
format/bytes.go
package format import ( "fmt" "math" ) const ( Byte = 1 KiloByte = Byte * 1000 MegaByte = KiloByte * 1000 GigaByte = MegaByte * 1000 TeraByte = GigaByte * 1000 KibiByte = Byte * 1024 MebiByte = KibiByte * 1024 GibiByte = MebiByte * 1024 ) func HumanBytes(b int64) string { var value float64 var unit string switch { case b >= TeraByte: value = float64(b) / TeraByte unit = "TB" case b >= GigaByte: value = float64(b) / GigaByte unit = "GB" case b >= MegaByte: value = float64(b) / MegaByte unit = "MB" case b >= KiloByte: value = float64(b) / KiloByte unit = "KB" default: return fmt.Sprintf("%d B", b) } switch { case value >= 10: return fmt.Sprintf("%d %s", int(value), unit) case value != math.Trunc(value): return fmt.Sprintf("%.1f %s", value, unit) default: return fmt.Sprintf("%d %s", int(value), unit) } } func HumanBytes2(b uint64) string { switch { case b >= GibiByte: return fmt.Sprintf("%.1f GiB", float64(b)/GibiByte) case b >= MebiByte: return fmt.Sprintf("%.1f MiB", float64(b)/MebiByte) case b >= KibiByte: return fmt.Sprintf("%.1f KiB", float64(b)/KibiByte) default: return fmt.Sprintf("%d B", b) } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/format/format_test.go
format/format_test.go
package format import ( "testing" ) func TestHumanNumber(t *testing.T) { type testCase struct { input uint64 expected string } testCases := []testCase{ {0, "0"}, {999, "999"}, {1000, "1K"}, {1001, "1K"}, {1000000, "1M"}, {125000000, "125M"}, {500500000, "500.50M"}, {500550000, "500.55M"}, {1000000000, "1B"}, {2800000000, "2.8B"}, {2850000000, "2.9B"}, {1000000000000, "1000B"}, } for _, tc := range testCases { t.Run(tc.expected, func(t *testing.T) { result := HumanNumber(tc.input) if result != tc.expected { t.Errorf("Expected %s, got %s", tc.expected, result) } }) } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/thinking/parser.go
thinking/parser.go
package thinking import ( "strings" "unicode" ) type thinkingState int const ( // We're looking for the opening tag, but we haven't seen any non-whitespace // characters yet thinkingState_LookingForOpening thinkingState = iota // We've seen the opening tag, but we haven't seen any non-whitespace // characters yet (we want to eat any whitespace between the opening tag and // the thinking content) thinkingState_ThinkingStartedEatingWhitespace // We've seen non-whitespace characters after the opening tag, but we haven't // seen the closing tag yet thinkingState_Thinking // We've seen the closing tag, but we haven't seen any non-whitespace // characters after the closing tag yet (we want to eat any whitespace between // the closing tag and the content) thinkingState_ThinkingDoneEatingWhitespace // We've seen the closing tag and seen at least one non-whitespace character // after it thinkingState_ThinkingDone ) func (s thinkingState) String() string { switch s { case thinkingState_LookingForOpening: return "LookingForOpening" case thinkingState_ThinkingStartedEatingWhitespace: return "ThinkingStartedEatingWhitespace" case thinkingState_Thinking: return "Thinking" case thinkingState_ThinkingDoneEatingWhitespace: return "ThinkingDoneEatingWhitespace" case thinkingState_ThinkingDone: return "ThinkingDone" default: return "Unknown" } } type Parser struct { state thinkingState OpeningTag string ClosingTag string acc strings.Builder } // AddContent returns the thinking content and the non-thinking content that // should be immediately sent to the user. It will internally buffer if it needs // to see more raw content to disambiguate func (s *Parser) AddContent(content string) (string, string) { s.acc.WriteString(content) var thinkingSb, remainingSb strings.Builder var thinking, remaining string keepLooping := true // we loop because we might pass through multiple parsing states in a single // call to addContent, and we want to make sure callers don't have to wait for // data that's already unambiguous for keepLooping { thinking, remaining, keepLooping = eat(s) thinkingSb.WriteString(thinking) remainingSb.WriteString(remaining) } return thinkingSb.String(), remainingSb.String() } // the additional bool return is true iff we should continue eating func eat(s *Parser) (string, string, bool) { switch s.state { case thinkingState_LookingForOpening: trimmed := strings.TrimLeftFunc(s.acc.String(), unicode.IsSpace) if strings.HasPrefix(trimmed, s.OpeningTag) { after := strings.Join(strings.Split(trimmed, s.OpeningTag)[1:], s.OpeningTag) after = strings.TrimLeftFunc(after, unicode.IsSpace) // after might contain more than just thinking tokens, so we continue // parsing instead of returning it as thinking tokens here s.acc.Reset() s.acc.WriteString(after) if after == "" { s.state = thinkingState_ThinkingStartedEatingWhitespace } else { s.state = thinkingState_Thinking } return "", "", true } else if strings.HasPrefix(s.OpeningTag, trimmed) { // partial opening seen, so let's keep accumulating return "", "", false } else if trimmed == "" { // saw whitespace only, so let's keep accumulating return "", "", false } else { // didn't see an opening tag, but we have content, so thinking was skipped s.state = thinkingState_ThinkingDone // note that we use the original content, not the trimmed one because we // don't want to eat any whitespace in the real content if there were no // thinking tags untrimmed := s.acc.String() s.acc.Reset() return "", untrimmed, false } case thinkingState_ThinkingStartedEatingWhitespace: trimmed := strings.TrimLeftFunc(s.acc.String(), unicode.IsSpace) s.acc.Reset() if trimmed == "" { return "", "", false } else { s.state = thinkingState_Thinking s.acc.WriteString(trimmed) return "", "", true } case thinkingState_Thinking: acc := s.acc.String() if strings.Contains(acc, s.ClosingTag) { split := strings.Split(acc, s.ClosingTag) thinking := split[0] remaining := strings.Join(split[1:], s.ClosingTag) remaining = strings.TrimLeftFunc(remaining, unicode.IsSpace) s.acc.Reset() if remaining == "" { s.state = thinkingState_ThinkingDoneEatingWhitespace } else { s.state = thinkingState_ThinkingDone } return thinking, remaining, false } else if overlapLen := overlap(acc, s.ClosingTag); overlapLen > 0 { thinking := acc[:len(acc)-overlapLen] remaining := acc[len(acc)-overlapLen:] s.acc.Reset() // keep track of the candidate closing tag. We have to buffer it until it // becomes disambiguated s.acc.WriteString(remaining) return thinking, "", false } else { // purely just thinking tokens, so we can return them s.acc.Reset() return acc, "", false } case thinkingState_ThinkingDoneEatingWhitespace: trimmed := strings.TrimLeftFunc(s.acc.String(), unicode.IsSpace) s.acc.Reset() // if we see non-whitespace, we're done eating the leading whitespace of the content if trimmed != "" { s.state = thinkingState_ThinkingDone } return "", trimmed, false case thinkingState_ThinkingDone: acc := s.acc.String() s.acc.Reset() return "", acc, false default: panic("unknown state") } } // longest overlap between suffix of s and prefix of delim func overlap(s, delim string) int { max := min(len(delim), len(s)) for i := max; i > 0; i-- { if strings.HasSuffix(s, delim[:i]) { return i } } return 0 }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/thinking/parser_test.go
thinking/parser_test.go
package thinking import ( "testing" ) func TestExtractThinking(t *testing.T) { tests := []struct { in, wantContent, wantThink string }{ { in: "<think> internal </think> world", wantThink: "internal ", wantContent: "world", }, { in: "<think>a</think><think>b</think>c", wantThink: "a", wantContent: "<think>b</think>c", }, { in: "no think", wantThink: "", wantContent: "no think", }, } for i, tt := range tests { parser := Parser{ OpeningTag: "<think>", ClosingTag: "</think>", } gotThinking, gotContent := parser.AddContent(tt.in) if gotContent != tt.wantContent || gotThinking != tt.wantThink { t.Errorf("case %d: got (%q,%q), want (%q,%q)", i, gotThinking, gotContent, tt.wantThink, tt.wantContent) } } } func TestThinkingStreaming(t *testing.T) { type step struct { input string wantThinking string wantContent string wantStateAfter thinkingState } cases := []struct { desc string skip bool steps []step }{ { desc: "content without a thinking tag", steps: []step{ { input: " abc", wantThinking: "", wantContent: " abc", wantStateAfter: thinkingState_ThinkingDone, }, // regression test for a bug where we were transitioning directly to // ThinkingDone without clearing the buffer. This would cuase the first // step to be outputted twice { input: "def", wantThinking: "", wantContent: "def", wantStateAfter: thinkingState_ThinkingDone, }, }, }, { desc: "content before a thinking tag nerfs the thinking tag", steps: []step{ { input: " abc <think>def</think> ghi", wantThinking: "", wantContent: " abc <think>def</think> ghi", wantStateAfter: thinkingState_ThinkingDone, }, }, }, { desc: "building up a thinking tag partially", steps: []step{ { input: " <th", wantThinking: "", wantContent: "", wantStateAfter: thinkingState_LookingForOpening, }, { input: "in", wantThinking: "", wantContent: "", wantStateAfter: thinkingState_LookingForOpening, }, { input: "k>a", wantThinking: "a", wantContent: "", wantStateAfter: thinkingState_Thinking, }, }, }, { desc: "partial closing tag", steps: []step{ { input: "<think>abc</th", wantThinking: "abc", wantContent: "", wantStateAfter: thinkingState_Thinking, }, { input: "ink>def", wantThinking: "", wantContent: "def", wantStateAfter: thinkingState_ThinkingDone, }, }, }, { desc: "partial closing tag fakeout", steps: []step{ { input: "<think>abc</th", wantThinking: "abc", wantContent: "", wantStateAfter: thinkingState_Thinking, }, { input: "ing>def", wantThinking: "</thing>def", wantContent: "", wantStateAfter: thinkingState_Thinking, }, { input: "ghi</thi", wantThinking: "ghi", wantContent: "", wantStateAfter: thinkingState_Thinking, }, { input: "nk>jkl", wantThinking: "", wantContent: "jkl", wantStateAfter: thinkingState_ThinkingDone, }, }, }, { desc: "whitespace after thinking tag", steps: []step{ { input: " <think>abc</think>\n\ndef", wantThinking: "abc", wantContent: "def", wantStateAfter: thinkingState_ThinkingDone, }, }, }, { desc: "whitespace after thinking tag (incremental)", steps: []step{ { input: " <think>abc</think>", wantThinking: "abc", wantContent: "", wantStateAfter: thinkingState_ThinkingDoneEatingWhitespace, }, { input: "\n\ndef", wantThinking: "", wantContent: "def", wantStateAfter: thinkingState_ThinkingDone, }, }, }, { desc: "whitespace after thinking tag with content and more whitespace", steps: []step{ { input: " <think>abc</think>\n\ndef ", wantThinking: "abc", wantContent: "def ", wantStateAfter: thinkingState_ThinkingDone, }, { input: " ghi", wantThinking: "", wantContent: " ghi", wantStateAfter: thinkingState_ThinkingDone, }, }, }, { desc: "token by token", steps: []step{ { input: "<think>", wantThinking: "", wantContent: "", wantStateAfter: thinkingState_ThinkingStartedEatingWhitespace, }, { input: "\n", wantThinking: "", wantContent: "", wantStateAfter: thinkingState_ThinkingStartedEatingWhitespace, }, { input: "</think>", wantThinking: "", wantContent: "", wantStateAfter: thinkingState_ThinkingDoneEatingWhitespace, }, { input: "\n\n", wantThinking: "", wantContent: "", wantStateAfter: thinkingState_ThinkingDoneEatingWhitespace, }, { input: "Hi", wantThinking: "", wantContent: "Hi", wantStateAfter: thinkingState_ThinkingDone, }, { input: " there", wantThinking: "", wantContent: " there", wantStateAfter: thinkingState_ThinkingDone, }, }, }, { desc: "leading thinking whitespace", steps: []step{ { input: " <think> \t ", wantThinking: "", wantContent: "", wantStateAfter: thinkingState_ThinkingStartedEatingWhitespace, }, { input: " these are some ", wantThinking: "these are some ", wantContent: "", wantStateAfter: thinkingState_Thinking, }, { input: "thoughts </think> ", wantThinking: "thoughts ", wantContent: "", wantStateAfter: thinkingState_ThinkingDoneEatingWhitespace, }, { input: " more content", wantThinking: "", wantContent: "more content", wantStateAfter: thinkingState_ThinkingDone, }, }, }, } for _, c := range cases { parser := Parser{ OpeningTag: "<think>", ClosingTag: "</think>", } if c.skip { continue } for i, step := range c.steps { thinking, content := parser.AddContent(step.input) if content != step.wantContent || thinking != step.wantThinking { t.Errorf("case %q (step %d): got (%q,%q), want (%q,%q)", c.desc, i, content, thinking, step.wantContent, step.wantThinking) } if parser.state != step.wantStateAfter { t.Errorf("case %q (step %d): got state %s, want %s", c.desc, i, parser.state, step.wantStateAfter) } } } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/thinking/template_test.go
thinking/template_test.go
package thinking import ( "testing" "text/template" ) func TestInferThinkingTags(t *testing.T) { cases := []struct { desc string tmplString string wantOpeningTag string wantClosingTag string }{ { desc: "basic", tmplString: ` {{ if .Thinking}} /think {{ end }} {{- range $i, $_ := .Messages }} {{- $last := eq (len (slice $.Messages $i)) 1 -}} {{ if and $last .Thinking }} <think>{{ .Thinking }}</think> {{ end }} {{ end }} `, wantOpeningTag: "<think>", wantClosingTag: "</think>", }, { desc: "doubly nested range", tmplString: ` {{ if .Thinking}} /think {{ end }} {{- range $i, $_ := .Messages }} {{- range $j, $_ := .NotMessages }} {{- $last := eq (len (slice $.Messages $i)) 1 -}} {{ if and $last .Thinking }} <think>{{ .Thinking }}</think> {{ end }} {{ end }} {{ end }} `, wantOpeningTag: "", wantClosingTag: "", }, { desc: "whitespace is trimmed", tmplString: ` {{ if .Thinking}} /think {{ end }} {{- range $i, $_ := .Messages }} {{- $last := eq (len (slice $.Messages $i)) 1 -}} {{ if and $last .Thinking }} Some text before {{ .Thinking }} Some text after {{ end }} {{ end }} `, wantOpeningTag: "Some text before", wantClosingTag: "Some text after", }, { desc: "qwen3", tmplString: ` {{- if or .System .Tools .Thinking }}<|im_start|>system {{- if .System }} {{ .System }} {{- end }} {{- if .Tools }} # Tools You may call one or more functions to assist with the user query. You are provided with function signatures within <tools></tools> XML tags: <tools> {{- range .Tools }} {"type": "function", "function": {{ .Function }}} {{- end }} </tools> For each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags: <tool_call> {"name": <function-name>, "arguments": <args-json-object>} </tool_call> {{- end }} {{- if .Thinking }} /think {{- else }} /no_think {{- end }}<|im_end|> {{ end }} {{- range $i, $_ := .Messages }} {{- $last := eq (len (slice $.Messages $i)) 1 -}} {{- if eq .Role "user" }}<|im_start|>user {{ .Content }}<|im_end|> {{ else if eq .Role "assistant" }}<|im_start|>assistant {{ if and $last .Thinking }} <think>{{ .Thinking }}</think> {{ end }} {{ if .Content }}{{ .Content }} {{- else if .ToolCalls }}<tool_call> {{ range .ToolCalls }}{"name": "{{ .Function.Name }}", "arguments": {{ .Function.Arguments }}} {{ end }}</tool_call> {{- end }}{{ if not $last }}<|im_end|> {{ end }} {{- else if eq .Role "tool" }}<|im_start|>user <tool_response> {{ .Content }} </tool_response><|im_end|> {{ end }} {{- if and (ne .Role "assistant") $last }}<|im_start|>assistant {{ end }} {{- end }} `, wantOpeningTag: "<think>", wantClosingTag: "</think>", }, } for _, c := range cases { tmpl := template.Must(template.New("test").Parse(c.tmplString)) openingTag, closingTag := InferTags(tmpl) if openingTag != c.wantOpeningTag || closingTag != c.wantClosingTag { t.Errorf("case %q: got (%q,%q), want (%q,%q)", c.desc, openingTag, closingTag, c.wantOpeningTag, c.wantClosingTag) } } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/thinking/template.go
thinking/template.go
package thinking import ( "strings" "text/template" "text/template/parse" ) func templateVisit(n parse.Node, enterFn func(parse.Node) bool, exitFn func(parse.Node)) { if n == nil { return } shouldContinue := enterFn(n) if !shouldContinue { return } switch x := n.(type) { case *parse.ListNode: for _, c := range x.Nodes { templateVisit(c, enterFn, exitFn) } case *parse.BranchNode: if x.Pipe != nil { templateVisit(x.Pipe, enterFn, exitFn) } if x.List != nil { templateVisit(x.List, enterFn, exitFn) } if x.ElseList != nil { templateVisit(x.ElseList, enterFn, exitFn) } case *parse.ActionNode: templateVisit(x.Pipe, enterFn, exitFn) case *parse.WithNode: templateVisit(&x.BranchNode, enterFn, exitFn) case *parse.RangeNode: templateVisit(&x.BranchNode, enterFn, exitFn) case *parse.IfNode: templateVisit(&x.BranchNode, enterFn, exitFn) case *parse.TemplateNode: templateVisit(x.Pipe, enterFn, exitFn) case *parse.PipeNode: for _, c := range x.Cmds { templateVisit(c, enterFn, exitFn) } case *parse.CommandNode: for _, a := range x.Args { templateVisit(a, enterFn, exitFn) } // text, field, number, etc. are leaves – nothing to recurse into } if exitFn != nil { exitFn(n) } } // InferTags uses a heuristic to infer the tags that surround thinking traces: // We look for a range node that iterates over "Messages" and then look for a // reference to "Thinking" like `{{.Thinking}}`. We then go up to the nearest // ListNode and take the first and last TextNodes as the opening and closing // tags. func InferTags(t *template.Template) (string, string) { ancestors := []parse.Node{} openingTag := "" closingTag := "" enterFn := func(n parse.Node) bool { ancestors = append(ancestors, n) switch x := n.(type) { case *parse.FieldNode: if len(x.Ident) > 0 && x.Ident[0] == "Thinking" { var mostRecentRange *parse.RangeNode for i := len(ancestors) - 1; i >= 0; i-- { if r, ok := ancestors[i].(*parse.RangeNode); ok { mostRecentRange = r break } } if mostRecentRange == nil || !rangeUsesField(mostRecentRange, "Messages") { return true } // TODO(drifkin): to be more robust, check that it's in the action // part, not the `if`'s pipeline part. We do match on the nearest list // that starts and ends with text nodes, which makes this not strictly // necessary for our heuristic // go up to the nearest ancestor that is a *parse.ListNode for i := len(ancestors) - 1; i >= 0; i-- { if l, ok := ancestors[i].(*parse.ListNode); ok { firstNode := l.Nodes[0] if t, ok := firstNode.(*parse.TextNode); ok { openingTag = strings.TrimSpace(t.String()) } lastNode := l.Nodes[len(l.Nodes)-1] if t, ok := lastNode.(*parse.TextNode); ok { closingTag = strings.TrimSpace(t.String()) } break } } } } return true } exitFn := func(n parse.Node) { ancestors = ancestors[:len(ancestors)-1] } templateVisit(t.Root, enterFn, exitFn) return openingTag, closingTag } // checks to see if the given field name is present in the pipeline of the given range node func rangeUsesField(rangeNode *parse.RangeNode, field string) bool { found := false enterFn := func(n parse.Node) bool { switch x := n.(type) { case *parse.FieldNode: if x.Ident[0] == field { found = true } } return true } templateVisit(rangeNode.BranchNode.Pipe, enterFn, nil) return found }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/discover/cpu_linux_test.go
discover/cpu_linux_test.go
package discover import ( "bytes" "log/slog" "testing" ) func TestLinuxCPUDetails(t *testing.T) { type results struct { cores int efficiency int threads int } type testCase struct { input string expCPUs []results expThreadCount int } testCases := map[string]*testCase{ "#5554 Docker Ollama container inside the LXC": { input: `processor : 0 vendor_id : AuthenticAMD cpu family : 25 model : 160 model name : AMD EPYC 9754 128-Core Processor stepping : 2 microcode : 0xaa00212 cpu MHz : 2246.624 cache size : 512 KB physical id : 0 siblings : 4 core id : 0 cpu cores : 4 apicid : 0 initial apicid : 0 fpu : yes fpu_exception : yes cpuid level : 16 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm rep_good nopl cpuid extd_apicid tsc_known_freq pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm cmp_legacy svm cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw perfctr_core invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced vmmcall fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves avx512_bf16 clzero xsaveerptr wbnoinvd arat npt lbrv nrip_save tsc_scale vmcb_clean flushbyasid pausefilter pfthreshold v_vmsave_vmload vgif avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq la57 rdpid fsrm flush_l1d arch_capabilities bugs : sysret_ss_attrs spectre_v1 spectre_v2 spec_store_bypass srso bogomips : 4493.24 TLB size : 1024 4K pages clflush size : 64 cache_alignment : 64 address sizes : 52 bits physical, 57 bits virtual power management: processor : 1 vendor_id : AuthenticAMD cpu family : 25 model : 160 model name : AMD EPYC 9754 128-Core Processor stepping : 2 microcode : 0xaa00212 cpu MHz : 2246.624 cache size : 512 KB physical id : 0 siblings : 4 core id : 1 cpu cores : 4 apicid : 1 initial apicid : 1 fpu : yes fpu_exception : yes cpuid level : 16 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm rep_good nopl cpuid extd_apicid tsc_known_freq pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm cmp_legacy svm cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw perfctr_core invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced vmmcall fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves avx512_bf16 clzero xsaveerptr wbnoinvd arat npt lbrv nrip_save tsc_scale vmcb_clean flushbyasid pausefilter pfthreshold v_vmsave_vmload vgif avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq la57 rdpid fsrm flush_l1d arch_capabilities bugs : sysret_ss_attrs spectre_v1 spectre_v2 spec_store_bypass srso bogomips : 4493.24 TLB size : 1024 4K pages clflush size : 64 cache_alignment : 64 address sizes : 52 bits physical, 57 bits virtual power management: processor : 2 vendor_id : AuthenticAMD cpu family : 25 model : 160 model name : AMD EPYC 9754 128-Core Processor stepping : 2 microcode : 0xaa00212 cpu MHz : 2246.624 cache size : 512 KB physical id : 0 siblings : 4 core id : 2 cpu cores : 4 apicid : 2 initial apicid : 2 fpu : yes fpu_exception : yes cpuid level : 16 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm rep_good nopl cpuid extd_apicid tsc_known_freq pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm cmp_legacy svm cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw perfctr_core invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced vmmcall fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves avx512_bf16 clzero xsaveerptr wbnoinvd arat npt lbrv nrip_save tsc_scale vmcb_clean flushbyasid pausefilter pfthreshold v_vmsave_vmload vgif avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq la57 rdpid fsrm flush_l1d arch_capabilities bugs : sysret_ss_attrs spectre_v1 spectre_v2 spec_store_bypass srso bogomips : 4493.24 TLB size : 1024 4K pages clflush size : 64 cache_alignment : 64 address sizes : 52 bits physical, 57 bits virtual power management: processor : 3 vendor_id : AuthenticAMD cpu family : 25 model : 160 model name : AMD EPYC 9754 128-Core Processor stepping : 2 microcode : 0xaa00212 cpu MHz : 2246.624 cache size : 512 KB physical id : 0 siblings : 4 core id : 3 cpu cores : 4 apicid : 3 initial apicid : 3 fpu : yes fpu_exception : yes cpuid level : 16 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm rep_good nopl cpuid extd_apicid tsc_known_freq pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm cmp_legacy svm cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw perfctr_core invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced vmmcall fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves avx512_bf16 clzero xsaveerptr wbnoinvd arat npt lbrv nrip_save tsc_scale vmcb_clean flushbyasid pausefilter pfthreshold v_vmsave_vmload vgif avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq la57 rdpid fsrm flush_l1d arch_capabilities bugs : sysret_ss_attrs spectre_v1 spectre_v2 spec_store_bypass srso bogomips : 4493.24 TLB size : 1024 4K pages clflush size : 64 cache_alignment : 64 address sizes : 52 bits physical, 57 bits virtual power management: processor : 4 vendor_id : AuthenticAMD cpu family : 25 model : 160 model name : AMD EPYC 9754 128-Core Processor stepping : 2 microcode : 0xaa00212 cpu MHz : 2246.624 cache size : 512 KB physical id : 1 siblings : 4 core id : 0 cpu cores : 4 apicid : 4 initial apicid : 4 fpu : yes fpu_exception : yes cpuid level : 16 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm rep_good nopl cpuid extd_apicid tsc_known_freq pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm cmp_legacy svm cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw perfctr_core invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced vmmcall fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves avx512_bf16 clzero xsaveerptr wbnoinvd arat npt lbrv nrip_save tsc_scale vmcb_clean flushbyasid pausefilter pfthreshold v_vmsave_vmload vgif avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq la57 rdpid fsrm flush_l1d arch_capabilities bugs : sysret_ss_attrs spectre_v1 spectre_v2 spec_store_bypass srso bogomips : 4493.24 TLB size : 1024 4K pages clflush size : 64 cache_alignment : 64 address sizes : 52 bits physical, 57 bits virtual power management: processor : 5 vendor_id : AuthenticAMD cpu family : 25 model : 160 model name : AMD EPYC 9754 128-Core Processor stepping : 2 microcode : 0xaa00212 cpu MHz : 2246.624 cache size : 512 KB physical id : 1 siblings : 4 core id : 1 cpu cores : 4 apicid : 5 initial apicid : 5 fpu : yes fpu_exception : yes cpuid level : 16 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm rep_good nopl cpuid extd_apicid tsc_known_freq pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm cmp_legacy svm cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw perfctr_core invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced vmmcall fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves avx512_bf16 clzero xsaveerptr wbnoinvd arat npt lbrv nrip_save tsc_scale vmcb_clean flushbyasid pausefilter pfthreshold v_vmsave_vmload vgif avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq la57 rdpid fsrm flush_l1d arch_capabilities bugs : sysret_ss_attrs spectre_v1 spectre_v2 spec_store_bypass srso bogomips : 4493.24 TLB size : 1024 4K pages clflush size : 64 cache_alignment : 64 address sizes : 52 bits physical, 57 bits virtual power management: processor : 6 vendor_id : AuthenticAMD cpu family : 25 model : 160 model name : AMD EPYC 9754 128-Core Processor stepping : 2 microcode : 0xaa00212 cpu MHz : 2246.624 cache size : 512 KB physical id : 1 siblings : 4 core id : 2 cpu cores : 4 apicid : 6 initial apicid : 6 fpu : yes fpu_exception : yes cpuid level : 16 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm rep_good nopl cpuid extd_apicid tsc_known_freq pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm cmp_legacy svm cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw perfctr_core invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced vmmcall fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves avx512_bf16 clzero xsaveerptr wbnoinvd arat npt lbrv nrip_save tsc_scale vmcb_clean flushbyasid pausefilter pfthreshold v_vmsave_vmload vgif avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq la57 rdpid fsrm flush_l1d arch_capabilities bugs : sysret_ss_attrs spectre_v1 spectre_v2 spec_store_bypass srso bogomips : 4493.24 TLB size : 1024 4K pages clflush size : 64 cache_alignment : 64 address sizes : 52 bits physical, 57 bits virtual power management: processor : 7 vendor_id : AuthenticAMD cpu family : 25 model : 160 model name : AMD EPYC 9754 128-Core Processor stepping : 2 microcode : 0xaa00212 cpu MHz : 2246.624 cache size : 512 KB physical id : 1 siblings : 4 core id : 3 cpu cores : 4 apicid : 7 initial apicid : 7 fpu : yes fpu_exception : yes cpuid level : 16 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm rep_good nopl cpuid extd_apicid tsc_known_freq pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm cmp_legacy svm cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw perfctr_core invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced vmmcall fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves avx512_bf16 clzero xsaveerptr wbnoinvd arat npt lbrv nrip_save tsc_scale vmcb_clean flushbyasid pausefilter pfthreshold v_vmsave_vmload vgif avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq la57 rdpid fsrm flush_l1d arch_capabilities bugs : sysret_ss_attrs spectre_v1 spectre_v2 spec_store_bypass srso bogomips : 4493.24 TLB size : 1024 4K pages clflush size : 64 cache_alignment : 64 address sizes : 52 bits physical, 57 bits virtual power management: `, expCPUs: []results{ { cores: 4, efficiency: 0, threads: 4, }, { cores: 4, efficiency: 0, threads: 4, }, }, expThreadCount: 8, }, // Single Socket, 8 cores "#5554 LXC direct output": { input: `processor : 0 vendor_id : AuthenticAMD cpu family : 25 model : 160 model name : AMD EPYC 9754 128-Core Processor stepping : 2 microcode : 0xaa00212 cpu MHz : 3094.910 cache size : 1024 KB physical id : 0 siblings : 256 core id : 0 cpu cores : 128 apicid : 0 initial apicid : 0 fpu : yes fpu_exception : yes cpuid level : 16 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good amd_lbr_v2 nopl nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 hw_pstate ssbd mba perfmon_v2 ibrs ibpb stibp ibrs_enhanced vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local user_shstk avx512_bf16 clzero irperf xsaveerptr rdpru wbnoinvd amd_ppin cppc arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif x2avic v_spec_ctrl vnmi avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq la57 rdpid overflow_recov succor smca fsrm flush_l1d debug_swap bugs : sysret_ss_attrs spectre_v1 spectre_v2 spec_store_bypass srso bogomips : 4492.85 TLB size : 3584 4K pages clflush size : 64 cache_alignment : 64 address sizes : 52 bits physical, 57 bits virtual power management: ts ttp tm hwpstate cpb eff_freq_ro [13] [14] processor : 1 vendor_id : AuthenticAMD cpu family : 25 model : 160 model name : AMD EPYC 9754 128-Core Processor stepping : 2 microcode : 0xaa00212 cpu MHz : 3094.470 cache size : 1024 KB physical id : 0 siblings : 256 core id : 1 cpu cores : 128 apicid : 2 initial apicid : 2 fpu : yes fpu_exception : yes cpuid level : 16 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good amd_lbr_v2 nopl nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 hw_pstate ssbd mba perfmon_v2 ibrs ibpb stibp ibrs_enhanced vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local user_shstk avx512_bf16 clzero irperf xsaveerptr rdpru wbnoinvd amd_ppin cppc arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif x2avic v_spec_ctrl vnmi avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq la57 rdpid overflow_recov succor smca fsrm flush_l1d debug_swap bugs : sysret_ss_attrs spectre_v1 spectre_v2 spec_store_bypass srso bogomips : 4492.85 TLB size : 3584 4K pages clflush size : 64 cache_alignment : 64 address sizes : 52 bits physical, 57 bits virtual power management: ts ttp tm hwpstate cpb eff_freq_ro [13] [14] processor : 2 vendor_id : AuthenticAMD cpu family : 25 model : 160 model name : AMD EPYC 9754 128-Core Processor stepping : 2 microcode : 0xaa00212 cpu MHz : 3094.918 cache size : 1024 KB physical id : 0 siblings : 256 core id : 2 cpu cores : 128 apicid : 4 initial apicid : 4 fpu : yes fpu_exception : yes cpuid level : 16 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good amd_lbr_v2 nopl nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 hw_pstate ssbd mba perfmon_v2 ibrs ibpb stibp ibrs_enhanced vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local user_shstk avx512_bf16 clzero irperf xsaveerptr rdpru wbnoinvd amd_ppin cppc arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif x2avic v_spec_ctrl vnmi avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq la57 rdpid overflow_recov succor smca fsrm flush_l1d debug_swap bugs : sysret_ss_attrs spectre_v1 spectre_v2 spec_store_bypass srso bogomips : 4492.85 TLB size : 3584 4K pages clflush size : 64 cache_alignment : 64 address sizes : 52 bits physical, 57 bits virtual power management: ts ttp tm hwpstate cpb eff_freq_ro [13] [14] processor : 3 vendor_id : AuthenticAMD cpu family : 25 model : 160 model name : AMD EPYC 9754 128-Core Processor stepping : 2 microcode : 0xaa00212 cpu MHz : 2250.000 cache size : 1024 KB physical id : 0 siblings : 256 core id : 3 cpu cores : 128 apicid : 6 initial apicid : 6 fpu : yes fpu_exception : yes cpuid level : 16 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good amd_lbr_v2 nopl nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 hw_pstate ssbd mba perfmon_v2 ibrs ibpb stibp ibrs_enhanced vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local user_shstk avx512_bf16 clzero irperf xsaveerptr rdpru wbnoinvd amd_ppin cppc arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif x2avic v_spec_ctrl vnmi avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq la57 rdpid overflow_recov succor smca fsrm flush_l1d debug_swap bugs : sysret_ss_attrs spectre_v1 spectre_v2 spec_store_bypass srso bogomips : 4492.85 TLB size : 3584 4K pages clflush size : 64 cache_alignment : 64 address sizes : 52 bits physical, 57 bits virtual power management: ts ttp tm hwpstate cpb eff_freq_ro [13] [14] processor : 4 vendor_id : AuthenticAMD cpu family : 25 model : 160 model name : AMD EPYC 9754 128-Core Processor stepping : 2 microcode : 0xaa00212 cpu MHz : 3090.662 cache size : 1024 KB physical id : 0 siblings : 256 core id : 4 cpu cores : 128 apicid : 8 initial apicid : 8 fpu : yes fpu_exception : yes cpuid level : 16 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good amd_lbr_v2 nopl nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 hw_pstate ssbd mba perfmon_v2 ibrs ibpb stibp ibrs_enhanced vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local user_shstk avx512_bf16 clzero irperf xsaveerptr rdpru wbnoinvd amd_ppin cppc arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif x2avic v_spec_ctrl vnmi avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq la57 rdpid overflow_recov succor smca fsrm flush_l1d debug_swap bugs : sysret_ss_attrs spectre_v1 spectre_v2 spec_store_bypass srso bogomips : 4492.85 TLB size : 3584 4K pages clflush size : 64 cache_alignment : 64 address sizes : 52 bits physical, 57 bits virtual power management: ts ttp tm hwpstate cpb eff_freq_ro [13] [14] processor : 5 vendor_id : AuthenticAMD cpu family : 25 model : 160 model name : AMD EPYC 9754 128-Core Processor stepping : 2 microcode : 0xaa00212 cpu MHz : 3093.734 cache size : 1024 KB physical id : 0 siblings : 256 core id : 5 cpu cores : 128 apicid : 10 initial apicid : 10 fpu : yes fpu_exception : yes cpuid level : 16 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good amd_lbr_v2 nopl nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 hw_pstate ssbd mba perfmon_v2 ibrs ibpb stibp ibrs_enhanced vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local user_shstk avx512_bf16 clzero irperf xsaveerptr rdpru wbnoinvd amd_ppin cppc arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif x2avic v_spec_ctrl vnmi avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq la57 rdpid overflow_recov succor smca fsrm flush_l1d debug_swap bugs : sysret_ss_attrs spectre_v1 spectre_v2 spec_store_bypass srso bogomips : 4492.85 TLB size : 3584 4K pages clflush size : 64 cache_alignment : 64 address sizes : 52 bits physical, 57 bits virtual power management: ts ttp tm hwpstate cpb eff_freq_ro [13] [14] processor : 6 vendor_id : AuthenticAMD cpu family : 25 model : 160 model name : AMD EPYC 9754 128-Core Processor stepping : 2 microcode : 0xaa00212 cpu MHz : 2250.000 cache size : 1024 KB physical id : 0 siblings : 256 core id : 6 cpu cores : 128 apicid : 12 initial apicid : 12 fpu : yes fpu_exception : yes cpuid level : 16 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good amd_lbr_v2 nopl nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 hw_pstate ssbd mba perfmon_v2 ibrs ibpb stibp ibrs_enhanced vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local user_shstk avx512_bf16 clzero irperf xsaveerptr rdpru wbnoinvd amd_ppin cppc arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif x2avic v_spec_ctrl vnmi avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq la57 rdpid overflow_recov succor smca fsrm flush_l1d debug_swap bugs : sysret_ss_attrs spectre_v1 spectre_v2 spec_store_bypass srso bogomips : 4492.85 TLB size : 3584 4K pages clflush size : 64 cache_alignment : 64 address sizes : 52 bits physical, 57 bits virtual power management: ts ttp tm hwpstate cpb eff_freq_ro [13] [14] processor : 7 vendor_id : AuthenticAMD cpu family : 25 model : 160 model name : AMD EPYC 9754 128-Core Processor stepping : 2 microcode : 0xaa00212 cpu MHz : 2250.000 cache size : 1024 KB physical id : 0 siblings : 256 core id : 7 cpu cores : 128 apicid : 14 initial apicid : 14 fpu : yes fpu_exception : yes cpuid level : 16 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good amd_lbr_v2 nopl nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 hw_pstate ssbd mba perfmon_v2 ibrs ibpb stibp ibrs_enhanced vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local user_shstk avx512_bf16 clzero irperf xsaveerptr rdpru wbnoinvd amd_ppin cppc arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif x2avic v_spec_ctrl vnmi avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq la57 rdpid overflow_recov succor smca fsrm flush_l1d debug_swap bugs : sysret_ss_attrs spectre_v1 spectre_v2 spec_store_bypass srso bogomips : 4492.85 TLB size : 3584 4K pages clflush size : 64 cache_alignment : 64 address sizes : 52 bits physical, 57 bits virtual power management: ts ttp tm hwpstate cpb eff_freq_ro [13] [14] `, expCPUs: []results{ { cores: 8, efficiency: 0, threads: 8, }, }, expThreadCount: 8, }, // Note: this was a partial cut-and-paste missing at least some initial logical processor definitions // Single Socket, 29 cores "#5554 LXC docker container output": { input: `processor : 483 vendor_id : AuthenticAMD cpu family : 25 model : 160 model name : AMD EPYC 9754 128-Core Processor stepping : 2 microcode : 0xaa00212 cpu MHz : 2250.000 cache size : 1024 KB physical id : 1 siblings : 256 core id : 19 cpu cores : 128 apicid : 295 initial apicid : 295 fpu : yes fpu_exception : yes cpuid level : 16 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good amd_lbr_v2 nopl nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 hw_pstate ssbd mba perfmon_v2 ibrs ibpb stibp ibrs_enhanced vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local user_shstk avx512_bf16 clzero irperf xsaveerptr rdpru wbnoinvd amd_ppin cppc arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif x2avic v_spec_ctrl vnmi avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq la57 rdpid overflow_recov succor smca fsrm flush_l1d debug_swap bugs : sysret_ss_attrs spectre_v1 spectre_v2 spec_store_bypass srso bogomips : 4492.85 TLB size : 3584 4K pages clflush size : 64 cache_alignment : 64 address sizes : 52 bits physical, 57 bits virtual power management: ts ttp tm hwpstate cpb eff_freq_ro [13] [14] processor : 484 vendor_id : AuthenticAMD cpu family : 25 model : 160 model name : AMD EPYC 9754 128-Core Processor stepping : 2 microcode : 0xaa00212 cpu MHz : 2250.000 cache size : 1024 KB physical id : 1 siblings : 256 core id : 20 cpu cores : 128 apicid : 297 initial apicid : 297 fpu : yes fpu_exception : yes cpuid level : 16 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good amd_lbr_v2 nopl nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 hw_pstate ssbd mba perfmon_v2 ibrs ibpb stibp ibrs_enhanced vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local user_shstk avx512_bf16 clzero irperf xsaveerptr rdpru wbnoinvd amd_ppin cppc arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif x2avic v_spec_ctrl vnmi avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq la57 rdpid overflow_recov succor smca fsrm flush_l1d debug_swap bugs : sysret_ss_attrs spectre_v1 spectre_v2 spec_store_bypass srso bogomips : 4492.85 TLB size : 3584 4K pages clflush size : 64 cache_alignment : 64 address sizes : 52 bits physical, 57 bits virtual power management: ts ttp tm hwpstate cpb eff_freq_ro [13] [14] processor : 485 vendor_id : AuthenticAMD cpu family : 25 model : 160 model name : AMD EPYC 9754 128-Core Processor stepping : 2 microcode : 0xaa00212 cpu MHz : 2250.000 cache size : 1024 KB physical id : 1 siblings : 256 core id : 21 cpu cores : 128 apicid : 299 initial apicid : 299 fpu : yes fpu_exception : yes cpuid level : 16 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good amd_lbr_v2 nopl nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 hw_pstate ssbd mba perfmon_v2 ibrs ibpb stibp ibrs_enhanced vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local user_shstk avx512_bf16 clzero irperf xsaveerptr rdpru wbnoinvd amd_ppin cppc arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif x2avic v_spec_ctrl vnmi avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq la57 rdpid overflow_recov succor smca fsrm flush_l1d debug_swap bugs : sysret_ss_attrs spectre_v1 spectre_v2 spec_store_bypass srso bogomips : 4492.85 TLB size : 3584 4K pages clflush size : 64 cache_alignment : 64 address sizes : 52 bits physical, 57 bits virtual power management: ts ttp tm hwpstate cpb eff_freq_ro [13] [14]
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
true
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/discover/cpu_linux.go
discover/cpu_linux.go
package discover import ( "bufio" "errors" "fmt" "io" "log/slog" "os" "path/filepath" "reflect" "regexp" "sort" "strconv" "strings" "github.com/ollama/ollama/format" ) func GetCPUMem() (memInfo, error) { mem, err := getCPUMem() if err != nil { return memInfo{}, err } return getCPUMemByCgroups(mem), nil } func getCPUMem() (memInfo, error) { var mem memInfo var total, available, free, buffers, cached, freeSwap uint64 f, err := os.Open("/proc/meminfo") if err != nil { return mem, err } defer f.Close() s := bufio.NewScanner(f) for s.Scan() { line := s.Text() switch { case strings.HasPrefix(line, "MemTotal:"): _, err = fmt.Sscanf(line, "MemTotal:%d", &total) case strings.HasPrefix(line, "MemAvailable:"): _, err = fmt.Sscanf(line, "MemAvailable:%d", &available) case strings.HasPrefix(line, "MemFree:"): _, err = fmt.Sscanf(line, "MemFree:%d", &free) case strings.HasPrefix(line, "Buffers:"): _, err = fmt.Sscanf(line, "Buffers:%d", &buffers) case strings.HasPrefix(line, "Cached:"): _, err = fmt.Sscanf(line, "Cached:%d", &cached) case strings.HasPrefix(line, "SwapFree:"): _, err = fmt.Sscanf(line, "SwapFree:%d", &freeSwap) default: continue } if err != nil { return mem, err } } mem.TotalMemory = total * format.KibiByte mem.FreeSwap = freeSwap * format.KibiByte if available > 0 { mem.FreeMemory = available * format.KibiByte } else { mem.FreeMemory = (free + buffers + cached) * format.KibiByte } return mem, nil } func getCPUMemByCgroups(mem memInfo) memInfo { total, err := getUint64ValueFromFile("/sys/fs/cgroup/memory.max") if err == nil { mem.TotalMemory = total } used, err := getUint64ValueFromFile("/sys/fs/cgroup/memory.current") if err == nil { mem.FreeMemory = mem.TotalMemory - used } return mem } func getUint64ValueFromFile(path string) (uint64, error) { f, err := os.Open(path) if err != nil { return 0, err } defer f.Close() s := bufio.NewScanner(f) for s.Scan() { line := s.Text() return strconv.ParseUint(line, 10, 64) } return 0, errors.New("empty file content") } const CpuInfoFilename = "/proc/cpuinfo" type linuxCpuInfo struct { ID string `cpuinfo:"processor"` VendorID string `cpuinfo:"vendor_id"` ModelName string `cpuinfo:"model name"` PhysicalID string `cpuinfo:"physical id"` Siblings string `cpuinfo:"siblings"` CoreID string `cpuinfo:"core id"` } func GetCPUDetails() []CPU { file, err := os.Open(CpuInfoFilename) if err != nil { slog.Warn("failed to get CPU details", "error", err) return nil } defer file.Close() cpus := linuxCPUDetails(file) return overwriteThreadCountByLinuxCgroups(cpus) } func overwriteThreadCountByLinuxCgroups(cpus []CPU) []CPU { file, err := os.Open("/sys/fs/cgroup/cpu.max") if err != nil { return cpus } defer file.Close() scanner := bufio.NewScanner(file) for scanner.Scan() { line := scanner.Text() if sl := strings.Split(line, " "); len(sl) == 2 { allowdUs, err := strconv.ParseInt(sl[0], 10, 64) if err != nil { slog.Warn("failed to parse CPU allowed micro secs", "error", err) return cpus } unitUs, err := strconv.ParseInt(sl[1], 10, 64) if err != nil { slog.Warn("failed to parse CPU unit micro secs", "error", err) return cpus } threads := int(max(allowdUs/unitUs, 1)) cpu := cpus[0] cpu.CoreCount = threads cpu.ThreadCount = threads return []CPU{cpu} } } return cpus } func linuxCPUDetails(file io.Reader) []CPU { reColumns := regexp.MustCompile("\t+: ") scanner := bufio.NewScanner(file) cpuInfos := []linuxCpuInfo{} cpu := &linuxCpuInfo{} for scanner.Scan() { line := scanner.Text() if sl := reColumns.Split(line, 2); len(sl) > 1 { t := reflect.TypeOf(cpu).Elem() s := reflect.ValueOf(cpu).Elem() for i := range t.NumField() { field := t.Field(i) tag := field.Tag.Get("cpuinfo") if tag == sl[0] { s.FieldByName(field.Name).SetString(sl[1]) break } } } else if strings.TrimSpace(line) == "" && cpu.ID != "" { cpuInfos = append(cpuInfos, *cpu) cpu = &linuxCpuInfo{} } } if cpu.ID != "" { cpuInfos = append(cpuInfos, *cpu) } // Process the sockets/cores/threads socketByID := map[string]*CPU{} coreBySocket := map[string]map[string]struct{}{} threadsByCoreBySocket := map[string]map[string]int{} for _, c := range cpuInfos { if _, found := socketByID[c.PhysicalID]; !found { socketByID[c.PhysicalID] = &CPU{ ID: c.PhysicalID, VendorID: c.VendorID, ModelName: c.ModelName, } coreBySocket[c.PhysicalID] = map[string]struct{}{} threadsByCoreBySocket[c.PhysicalID] = map[string]int{} } if c.CoreID != "" { coreBySocket[c.PhysicalID][c.PhysicalID+":"+c.CoreID] = struct{}{} threadsByCoreBySocket[c.PhysicalID][c.PhysicalID+":"+c.CoreID]++ } else { coreBySocket[c.PhysicalID][c.PhysicalID+":"+c.ID] = struct{}{} threadsByCoreBySocket[c.PhysicalID][c.PhysicalID+":"+c.ID]++ } } // Tally up the values from the tracking maps for id, s := range socketByID { s.CoreCount = len(coreBySocket[id]) s.ThreadCount = 0 // This only works if HT is enabled, consider a more reliable model, maybe cache size comparisons? efficiencyCoreCount := 0 for _, threads := range threadsByCoreBySocket[id] { s.ThreadCount += threads if threads == 1 { efficiencyCoreCount++ } } if efficiencyCoreCount == s.CoreCount { // 1:1 mapping means they're not actually efficiency cores, but regular cores s.EfficiencyCoreCount = 0 } else { s.EfficiencyCoreCount = efficiencyCoreCount } } keys := make([]string, 0, len(socketByID)) result := make([]CPU, 0, len(socketByID)) for k := range socketByID { keys = append(keys, k) } sort.Strings(keys) for _, k := range keys { result = append(result, *socketByID[k]) } return result } func IsNUMA() bool { ids := map[string]any{} packageIds, _ := filepath.Glob("/sys/devices/system/cpu/cpu*/topology/physical_package_id") for _, packageId := range packageIds { id, err := os.ReadFile(packageId) if err == nil { ids[strings.TrimSpace(string(id))] = struct{}{} } } return len(ids) > 1 }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/discover/runner_test.go
discover/runner_test.go
package discover import ( "log/slog" "os" "testing" ) func init() { logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug})) slog.SetDefault(logger) } func TestFilterOverlapByLibrary(t *testing.T) { type testcase struct { name string inp map[string]map[string]map[string]int exp []bool } for _, tc := range []testcase{ { name: "empty", inp: map[string]map[string]map[string]int{}, exp: []bool{}, // needs deletion }, { name: "single no overlap", inp: map[string]map[string]map[string]int{ "CUDA": { "cuda_v12": { "GPU-d7b00605-c0c8-152d-529d-e03726d5dc52": 0, }, }, }, exp: []bool{false}, }, { name: "100% overlap pick 2nd", inp: map[string]map[string]map[string]int{ "CUDA": { "cuda_v12": { "GPU-d7b00605-c0c8-152d-529d-e03726d5dc52": 0, "GPU-cd6c3216-03d2-a8eb-8235-2ffbf571712e": 1, }, "cuda_v13": { "GPU-d7b00605-c0c8-152d-529d-e03726d5dc52": 2, "GPU-cd6c3216-03d2-a8eb-8235-2ffbf571712e": 3, }, }, }, exp: []bool{true, true, false, false}, }, { name: "100% overlap pick 1st", inp: map[string]map[string]map[string]int{ "CUDA": { "cuda_v13": { "GPU-d7b00605-c0c8-152d-529d-e03726d5dc52": 0, "GPU-cd6c3216-03d2-a8eb-8235-2ffbf571712e": 1, }, "cuda_v12": { "GPU-d7b00605-c0c8-152d-529d-e03726d5dc52": 2, "GPU-cd6c3216-03d2-a8eb-8235-2ffbf571712e": 3, }, }, }, exp: []bool{false, false, true, true}, }, { name: "partial overlap pick older", inp: map[string]map[string]map[string]int{ "CUDA": { "cuda_v13": { "GPU-d7b00605-c0c8-152d-529d-e03726d5dc52": 0, }, "cuda_v12": { "GPU-d7b00605-c0c8-152d-529d-e03726d5dc52": 1, "GPU-cd6c3216-03d2-a8eb-8235-2ffbf571712e": 2, }, }, }, exp: []bool{true, false, false}, }, { name: "no overlap", inp: map[string]map[string]map[string]int{ "CUDA": { "cuda_v13": { "GPU-d7b00605-c0c8-152d-529d-e03726d5dc52": 0, }, "cuda_v12": { "GPU-cd6c3216-03d2-a8eb-8235-2ffbf571712e": 1, }, }, }, exp: []bool{false, false}, }, } { t.Run(tc.name, func(t *testing.T) { needsDelete := make([]bool, len(tc.exp)) filterOverlapByLibrary(tc.inp, needsDelete) for i, exp := range tc.exp { if needsDelete[i] != exp { t.Fatalf("expected: %v\ngot: %v", tc.exp, needsDelete) } } }) } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/discover/types.go
discover/types.go
package discover import ( "log/slog" "path/filepath" "sort" "strings" "github.com/ollama/ollama/format" "github.com/ollama/ollama/ml" ) type memInfo struct { TotalMemory uint64 `json:"total_memory,omitempty"` FreeMemory uint64 `json:"free_memory,omitempty"` FreeSwap uint64 `json:"free_swap,omitempty"` // TODO split this out for system only } // CPU type represents a CPU Package occupying a socket type CPU struct { ID string `cpuinfo:"processor"` VendorID string `cpuinfo:"vendor_id"` ModelName string `cpuinfo:"model name"` CoreCount int EfficiencyCoreCount int // Performance = CoreCount - Efficiency ThreadCount int } func LogDetails(devices []ml.DeviceInfo) { sort.Sort(sort.Reverse(ml.ByFreeMemory(devices))) // Report devices in order of scheduling preference for _, dev := range devices { var libs []string for _, dir := range dev.LibraryPath { if strings.Contains(dir, filepath.Join("lib", "ollama")) { libs = append(libs, filepath.Base(dir)) } } typeStr := "discrete" if dev.Integrated { typeStr = "iGPU" } slog.Info("inference compute", "id", dev.ID, "filter_id", dev.FilterID, "library", dev.Library, "compute", dev.Compute(), "name", dev.Name, "description", dev.Description, "libdirs", strings.Join(libs, ","), "driver", dev.Driver(), "pci_id", dev.PCIID, "type", typeStr, "total", format.HumanBytes2(dev.TotalMemory), "available", format.HumanBytes2(dev.FreeMemory), ) } // CPU inference if len(devices) == 0 { dev, _ := GetCPUMem() slog.Info("inference compute", "id", "cpu", "library", "cpu", "compute", "", "name", "cpu", "description", "cpu", "libdirs", "ollama", "driver", "", "pci_id", "", "type", "", "total", format.HumanBytes2(dev.TotalMemory), "available", format.HumanBytes2(dev.FreeMemory), ) } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/discover/gpu.go
discover/gpu.go
package discover import ( "log/slog" "os" "regexp" "runtime" "strconv" "strings" "time" "github.com/ollama/ollama/logutil" "github.com/ollama/ollama/ml" ) // Jetson devices have JETSON_JETPACK="x.y.z" factory set to the Jetpack version installed. // Included to drive logic for reducing Ollama-allocated overhead on L4T/Jetson devices. var CudaTegra string = os.Getenv("JETSON_JETPACK") // GetSystemInfo returns the last cached state of the GPUs on the system func GetSystemInfo() ml.SystemInfo { logutil.Trace("performing CPU discovery") startDiscovery := time.Now() defer func() { logutil.Trace("CPU discovery completed", "duration", time.Since(startDiscovery)) }() memInfo, err := GetCPUMem() if err != nil { slog.Warn("error looking up system memory", "error", err) } var threadCount int cpus := GetCPUDetails() for _, c := range cpus { threadCount += c.CoreCount - c.EfficiencyCoreCount } if threadCount == 0 { // Fall back to Go's num CPU threadCount = runtime.NumCPU() } return ml.SystemInfo{ ThreadCount: threadCount, TotalMemory: memInfo.TotalMemory, FreeMemory: memInfo.FreeMemory, FreeSwap: memInfo.FreeSwap, } } func cudaJetpack() string { if runtime.GOARCH == "arm64" && runtime.GOOS == "linux" { if CudaTegra != "" { ver := strings.Split(CudaTegra, ".") if len(ver) > 0 { return "jetpack" + ver[0] } } else if data, err := os.ReadFile("/etc/nv_tegra_release"); err == nil { r := regexp.MustCompile(` R(\d+) `) m := r.FindSubmatch(data) if len(m) != 2 { slog.Info("Unexpected format for /etc/nv_tegra_release. Set JETSON_JETPACK to select version") } else { if l4t, err := strconv.Atoi(string(m[1])); err == nil { // Note: mapping from L4t -> JP is inconsistent (can't just subtract 30) // https://developer.nvidia.com/embedded/jetpack-archive switch l4t { case 35: return "jetpack5" case 36: return "jetpack6" default: // Newer Jetson systems use the SBSU runtime slog.Debug("unrecognized L4T version", "nv_tegra_release", string(data)) } } } } } return "" }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/discover/runner.go
discover/runner.go
package discover // Runner based GPU discovery import ( "context" "io" "log/slog" "os" "os/exec" "path/filepath" "runtime" "sort" "strconv" "strings" "sync" "time" "github.com/ollama/ollama/envconfig" "github.com/ollama/ollama/format" "github.com/ollama/ollama/llm" "github.com/ollama/ollama/logutil" "github.com/ollama/ollama/ml" ) var ( deviceMu sync.Mutex devices []ml.DeviceInfo libDirs map[string]struct{} exe string bootstrapped bool ) func GPUDevices(ctx context.Context, runners []ml.FilteredRunnerDiscovery) []ml.DeviceInfo { deviceMu.Lock() defer deviceMu.Unlock() startDiscovery := time.Now() msg := "overall device VRAM discovery took" defer func() { slog.Debug(msg, "duration", time.Since(startDiscovery)) }() if !bootstrapped { msg = "GPU bootstrap discovery took" libDirs = make(map[string]struct{}) var err error exe, err = os.Executable() if err != nil { slog.Error("unable to lookup executable path", "error", err) return nil } if eval, err := filepath.EvalSymlinks(exe); err == nil { exe = eval } files, err := filepath.Glob(filepath.Join(ml.LibOllamaPath, "*", "*ggml-*")) if err != nil { slog.Debug("unable to lookup runner library directories", "error", err) } for _, file := range files { libDirs[filepath.Dir(file)] = struct{}{} } if len(libDirs) == 0 { libDirs[""] = struct{}{} } slog.Info("discovering available GPUs...") detectIncompatibleLibraries() // Warn if any user-overrides are set which could lead to incorrect GPU discovery overrideWarnings() requested := envconfig.LLMLibrary() jetpack := cudaJetpack() // For our initial discovery pass, we gather all the known GPUs through // all the libraries that were detected. This pass may include GPUs that // are enumerated, but not actually supported. // We run this in serial to avoid potentially initializing a GPU multiple // times concurrently leading to memory contention for dir := range libDirs { // Typically bootstrapping takes < 1s, but on some systems, with devices // in low power/idle mode, initialization can take multiple seconds. We // set a longer timeout just for bootstrap discovery to reduce the chance // of giving up too quickly bootstrapTimeout := 30 * time.Second if runtime.GOOS == "windows" { // On Windows with Defender enabled, AV scanning of the DLLs // takes place sequentially and this can significantly increase // the time it takes too do the initial discovery pass. // Subsequent loads will be faster as the scan results are // cached bootstrapTimeout = 90 * time.Second } var dirs []string if dir != "" { if requested != "" && filepath.Base(dir) != requested { slog.Debug("skipping available library at user's request", "requested", requested, "libDir", dir) continue } else if jetpack != "" && filepath.Base(dir) != "cuda_"+jetpack { continue } else if jetpack == "" && strings.Contains(filepath.Base(dir), "cuda_jetpack") { slog.Debug("jetpack not detected (set JETSON_JETPACK or OLLAMA_LLM_LIBRARY to override), skipping", "libDir", dir) continue } else if !envconfig.EnableVulkan() && strings.Contains(filepath.Base(dir), "vulkan") { slog.Info("experimental Vulkan support disabled. To enable, set OLLAMA_VULKAN=1") continue } dirs = []string{ml.LibOllamaPath, dir} } else { dirs = []string{ml.LibOllamaPath} } ctx1stPass, cancel := context.WithTimeout(ctx, bootstrapTimeout) defer cancel() // For this pass, we retain duplicates in case any are incompatible with some libraries devices = append(devices, bootstrapDevices(ctx1stPass, dirs, nil)...) } // In the second pass, we more deeply initialize the GPUs to weed out devices that // aren't supported by a given library. We run this phase in parallel to speed up discovery. // Only devices that need verification are included in this pass slog.Debug("evaluating which, if any, devices to filter out", "initial_count", len(devices)) ctx2ndPass, cancel := context.WithTimeout(ctx, 30*time.Second) defer cancel() var wg sync.WaitGroup needsDelete := make([]bool, len(devices)) supportedMu := sync.Mutex{} supported := make(map[string]map[string]map[string]int) // [Library][libDir][ID] = pre-deletion devices index for i := range devices { libDir := devices[i].LibraryPath[len(devices[i].LibraryPath)-1] if !devices[i].NeedsInitValidation() { // No need to validate, add to the supported map supportedMu.Lock() if _, ok := supported[devices[i].Library]; !ok { supported[devices[i].Library] = make(map[string]map[string]int) } if _, ok := supported[devices[i].Library][libDir]; !ok { supported[devices[i].Library][libDir] = make(map[string]int) } supported[devices[i].Library][libDir][devices[i].ID] = i supportedMu.Unlock() continue } slog.Debug("verifying if device is supported", "library", libDir, "description", devices[i].Description, "compute", devices[i].Compute(), "id", devices[i].ID, "pci_id", devices[i].PCIID) wg.Add(1) go func(i int) { defer wg.Done() extraEnvs := ml.GetVisibleDevicesEnv(devices[i:i+1], true) devices[i].AddInitValidation(extraEnvs) if len(bootstrapDevices(ctx2ndPass, devices[i].LibraryPath, extraEnvs)) == 0 { slog.Debug("filtering device which didn't fully initialize", "id", devices[i].ID, "libdir", devices[i].LibraryPath[len(devices[i].LibraryPath)-1], "pci_id", devices[i].PCIID, "library", devices[i].Library, ) needsDelete[i] = true } else { supportedMu.Lock() if _, ok := supported[devices[i].Library]; !ok { supported[devices[i].Library] = make(map[string]map[string]int) } if _, ok := supported[devices[i].Library][libDir]; !ok { supported[devices[i].Library][libDir] = make(map[string]int) } supported[devices[i].Library][libDir][devices[i].ID] = i supportedMu.Unlock() } }(i) } wg.Wait() logutil.Trace("supported GPU library combinations before filtering", "supported", supported) // Mark for deletion any overlaps - favoring the library version that can cover all GPUs if possible filterOverlapByLibrary(supported, needsDelete) // Any Libraries that utilize numeric IDs need adjusting based on any possible filtering taking place postFilteredID := map[string]int{} for i := 0; i < len(needsDelete); i++ { if needsDelete[i] { logutil.Trace("removing unsupported or overlapping GPU combination", "libDir", devices[i].LibraryPath[len(devices[i].LibraryPath)-1], "description", devices[i].Description, "compute", devices[i].Compute(), "pci_id", devices[i].PCIID) devices = append(devices[:i], devices[i+1:]...) needsDelete = append(needsDelete[:i], needsDelete[i+1:]...) i-- } else { if _, ok := postFilteredID[devices[i].Library]; !ok { postFilteredID[devices[i].Library] = 0 } if _, err := strconv.Atoi(devices[i].ID); err == nil { // Replace the numeric ID with the post-filtered IDs slog.Debug("adjusting filtering IDs", "FilterID", devices[i].ID, "new_ID", strconv.Itoa(postFilteredID[devices[i].Library])) devices[i].FilterID = devices[i].ID devices[i].ID = strconv.Itoa(postFilteredID[devices[i].Library]) } postFilteredID[devices[i].Library]++ } } // Now filter out any overlap with different libraries (favor CUDA/HIP over others) for i := 0; i < len(devices); i++ { for j := i + 1; j < len(devices); j++ { // For this pass, we only drop exact duplicates switch devices[i].Compare(devices[j]) { case ml.SameBackendDevice: // Same library and device, skip it devices = append(devices[:j], devices[j+1:]...) j-- continue case ml.DuplicateDevice: // Different library, choose based on priority var droppedDevice ml.DeviceInfo if devices[i].PreferredLibrary(devices[j]) { droppedDevice = devices[j] } else { droppedDevice = devices[i] devices[i] = devices[j] } devices = append(devices[:j], devices[j+1:]...) j-- typeStr := "discrete" if droppedDevice.Integrated { typeStr = "iGPU" } slog.Debug("dropping duplicate device", "id", droppedDevice.ID, "library", droppedDevice.Library, "compute", droppedDevice.Compute(), "name", droppedDevice.Name, "description", droppedDevice.Description, "libdirs", strings.Join(droppedDevice.LibraryPath, ","), "driver", droppedDevice.Driver(), "pci_id", droppedDevice.PCIID, "type", typeStr, "total", format.HumanBytes2(droppedDevice.TotalMemory), "available", format.HumanBytes2(droppedDevice.FreeMemory), ) continue } } } // Reset the libDirs to what we actually wind up using for future refreshes libDirs = make(map[string]struct{}) for _, dev := range devices { dir := dev.LibraryPath[len(dev.LibraryPath)-1] if dir != ml.LibOllamaPath { libDirs[dir] = struct{}{} } } if len(libDirs) == 0 { libDirs[""] = struct{}{} } bootstrapped = true } else { if runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" { // metal never updates free VRAM return append([]ml.DeviceInfo{}, devices...) } slog.Debug("refreshing free memory") updated := make([]bool, len(devices)) allDone := func() bool { allDone := true for _, done := range updated { if !done { allDone = false break } } return allDone } // First try to use existing runners to refresh VRAM since they're already // active on GPU(s) for _, runner := range runners { if runner == nil { continue } deviceIDs := runner.GetActiveDeviceIDs() if len(deviceIDs) == 0 { // Skip this runner since it doesn't have active GPU devices continue } // Check to see if this runner is active on any devices that need a refresh skip := true devCheck: for _, dev := range deviceIDs { for i := range devices { if dev == devices[i].DeviceID { if !updated[i] { skip = false break devCheck } } } } if skip { continue } // Typical refresh on existing runner is ~500ms but allow longer if the system // is under stress before giving up and using stale data. ctx, cancel := context.WithTimeout(ctx, 3*time.Second) defer cancel() start := time.Now() updatedDevices := runner.GetDeviceInfos(ctx) slog.Debug("existing runner discovery took", "duration", time.Since(start)) for _, u := range updatedDevices { for i := range devices { if u.DeviceID == devices[i].DeviceID { updated[i] = true devices[i].FreeMemory = u.FreeMemory break } } } // Short circuit if we've updated all the devices if allDone() { break } } if !allDone() { slog.Debug("unable to refresh all GPUs with existing runners, performing bootstrap discovery") // Bootstrapping may take longer in some cases (AMD windows), but we // would rather use stale free data to get the model running sooner ctx, cancel := context.WithTimeout(ctx, 3*time.Second) defer cancel() // Apply any dev filters to avoid re-discovering unsupported devices, and get IDs correct // We avoid CUDA filters here to keep ROCm from failing to discover GPUs in a mixed environment devFilter := ml.GetVisibleDevicesEnv(devices, false) for dir := range libDirs { updatedDevices := bootstrapDevices(ctx, []string{ml.LibOllamaPath, dir}, devFilter) for _, u := range updatedDevices { for i := range devices { if u.DeviceID == devices[i].DeviceID && u.PCIID == devices[i].PCIID { updated[i] = true devices[i].FreeMemory = u.FreeMemory break } } // TODO - consider evaluating if new devices have appeared (e.g. hotplug) } if allDone() { break } } if !allDone() { slog.Warn("unable to refresh free memory, using old values") } } } return append([]ml.DeviceInfo{}, devices...) } func filterOverlapByLibrary(supported map[string]map[string]map[string]int, needsDelete []bool) { // For multi-GPU systems, use the newest version that supports all the GPUs for _, byLibDirs := range supported { libDirs := make([]string, 0, len(byLibDirs)) for libDir := range byLibDirs { libDirs = append(libDirs, libDir) } sort.Sort(sort.Reverse(sort.StringSlice(libDirs))) anyMissing := false var newest string for _, newest = range libDirs { for _, libDir := range libDirs { if libDir == newest { continue } if len(byLibDirs[newest]) != len(byLibDirs[libDir]) { anyMissing = true break } for dev := range byLibDirs[newest] { if _, found := byLibDirs[libDir][dev]; !found { anyMissing = true break } } } if !anyMissing { break } } // Now we can mark overlaps for deletion for _, libDir := range libDirs { if libDir == newest { continue } for dev, i := range byLibDirs[libDir] { if _, found := byLibDirs[newest][dev]; found { slog.Debug("filtering device with overlapping libraries", "id", dev, "library", libDir, "delete_index", i, "kept_library", newest, ) needsDelete[i] = true } } } } } type bootstrapRunner struct { port int cmd *exec.Cmd } func (r *bootstrapRunner) GetPort() int { return r.port } func (r *bootstrapRunner) HasExited() bool { if r.cmd != nil && r.cmd.ProcessState != nil { return true } return false } func bootstrapDevices(ctx context.Context, ollamaLibDirs []string, extraEnvs map[string]string) []ml.DeviceInfo { var out io.Writer if envconfig.LogLevel() == logutil.LevelTrace { out = os.Stderr } start := time.Now() defer func() { slog.Debug("bootstrap discovery took", "duration", time.Since(start), "OLLAMA_LIBRARY_PATH", ollamaLibDirs, "extra_envs", extraEnvs) }() logutil.Trace("starting runner for device discovery", "libDirs", ollamaLibDirs, "extraEnvs", extraEnvs) cmd, port, err := llm.StartRunner( true, // ollama engine "", // no model ollamaLibDirs, out, extraEnvs, ) if err != nil { slog.Debug("failed to start runner to discovery GPUs", "error", err) return nil } go func() { cmd.Wait() // exit status ignored }() defer cmd.Process.Kill() devices, err := ml.GetDevicesFromRunner(ctx, &bootstrapRunner{port: port, cmd: cmd}) if err != nil { if cmd.ProcessState != nil && cmd.ProcessState.ExitCode() >= 0 { // Expected during bootstrapping while we filter out unsupported AMD GPUs logutil.Trace("runner exited", "OLLAMA_LIBRARY_PATH", ollamaLibDirs, "extra_envs", extraEnvs, "code", cmd.ProcessState.ExitCode()) } else { slog.Info("failure during GPU discovery", "OLLAMA_LIBRARY_PATH", ollamaLibDirs, "extra_envs", extraEnvs, "error", err) } } logutil.Trace("runner enumerated devices", "OLLAMA_LIBRARY_PATH", ollamaLibDirs, "devices", devices) return devices } func overrideWarnings() { anyFound := false m := envconfig.AsMap() for _, k := range []string{ "CUDA_VISIBLE_DEVICES", "HIP_VISIBLE_DEVICES", "ROCR_VISIBLE_DEVICES", "GGML_VK_VISIBLE_DEVICES", "GPU_DEVICE_ORDINAL", "HSA_OVERRIDE_GFX_VERSION", } { if e, found := m[k]; found && e.Value != "" { anyFound = true slog.Warn("user overrode visible devices", k, e.Value) } } if anyFound { slog.Warn("if GPUs are not correctly discovered, unset and try again") } } func detectIncompatibleLibraries() { if runtime.GOOS != "windows" { return } basePath, err := exec.LookPath("ggml-base.dll") if err != nil || basePath == "" { return } if !strings.HasPrefix(basePath, ml.LibOllamaPath) { slog.Warn("potentially incompatible library detected in PATH", "location", basePath) } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/discover/cpu_windows_test.go
discover/cpu_windows_test.go
package discover import "testing" func TestProcessSystemLogicalProcessorInforationList(t *testing.T) { type pkgs struct { cores int efficiency int threads int } type testCase struct { input []byte expected []pkgs } testCases := map[string]*testCase{ "AMD64 Family 25 Model 97 Stepping 2 ": { input: []byte{ 0x3, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0xff, 0xff, 0xff, 0xff, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x1, 0x8, 0x40, 0x0, 0x0, 0x80, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x1, 0x8, 0x40, 0x0, 0x0, 0x80, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x2, 0x8, 0x40, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x3, 0x10, 0x40, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0xff, 0xff, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0xc, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0xc, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x1, 0x8, 0x40, 0x0, 0x0, 0x80, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0xc, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x1, 0x8, 0x40, 0x0, 0x0, 0x80, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0xc, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x2, 0x8, 0x40, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0xc, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x1, 0x8, 0x40, 0x0, 0x0, 0x80, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x1, 0x8, 0x40, 0x0, 0x0, 0x80, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x2, 0x8, 0x40, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0xc0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0xc0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x1, 0x8, 0x40, 0x0, 0x0, 0x80, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0xc0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x1, 0x8, 0x40, 0x0, 0x0, 0x80, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0xc0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x2, 0x8, 0x40, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0xc0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x1, 0x8, 0x40, 0x0, 0x0, 0x80, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x1, 0x8, 0x40, 0x0, 0x0, 0x80, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x2, 0x8, 0x40, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x1, 0x8, 0x40, 0x0, 0x0, 0x80, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x1, 0x8, 0x40, 0x0, 0x0, 0x80, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x2, 0x8, 0x40, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x1, 0x8, 0x40, 0x0, 0x0, 0x80, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x1, 0x8, 0x40, 0x0, 0x0, 0x80, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x2, 0x8, 0x40, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0xc0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0xc0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x1, 0x8, 0x40, 0x0, 0x0, 0x80, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0xc0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x1, 0x8, 0x40, 0x0, 0x0, 0x80, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0xc0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x2, 0x8, 0x40, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0xc0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x1, 0x8, 0x40, 0x0, 0x0, 0x80, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x1, 0x8, 0x40, 0x0, 0x0, 0x80, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x2, 0x8, 0x40, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x3, 0x10, 0x40, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0xff, 0xff, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x1, 0x8, 0x40, 0x0, 0x0, 0x80, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x1, 0x8, 0x40, 0x0, 0x0, 0x80, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x2, 0x8, 0x40, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x1, 0x8, 0x40, 0x0, 0x0, 0x80, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x1, 0x8, 0x40, 0x0, 0x0, 0x80, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x2, 0x8, 0x40, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0xc0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0xc0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x1, 0x8, 0x40, 0x0, 0x0, 0x80, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0xc0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x1, 0x8, 0x40, 0x0, 0x0, 0x80, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0xc0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x2, 0x8, 0x40, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0xc0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x1, 0x8, 0x40, 0x0, 0x0, 0x80, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x1, 0x8, 0x40, 0x0, 0x0, 0x80, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x2, 0x8, 0x40, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x1, 0x8, 0x40, 0x0, 0x0, 0x80, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x1, 0x8, 0x40, 0x0, 0x0, 0x80, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x2, 0x8, 0x40, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x1, 0x8, 0x40, 0x0, 0x0, 0x80, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x1, 0x8, 0x40, 0x0, 0x0, 0x80, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x2, 0x8, 0x40, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0xc0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0xc0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x1, 0x8, 0x40, 0x0, 0x0, 0x80, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0xc0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x1, 0x8, 0x40, 0x0, 0x0, 0x80, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0xc0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x2, 0x8, 0x40, 0x0, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0xc0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0xff, 0xff, 0xff, 0xff, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4, 0x0, 0x0, 0x0, 0x50, 0x0, 0x0, 0x0, 0x1, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x20, 0x20, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff, 0xff, 0xff, 0xff, 0x0, 0x0, 0x0, 0x0, }, expected: []pkgs{ { cores: 16, efficiency: 0, threads: 32, }, }, }, "Intel64 Family 6 Model 183 Stepping 1": { input: []byte{
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
true
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/discover/gpu_darwin.go
discover/gpu_darwin.go
package discover /* #cgo CFLAGS: -x objective-c #cgo LDFLAGS: -framework Foundation -framework CoreGraphics -framework Metal #include "gpu_info_darwin.h" */ import "C" import ( "log/slog" "syscall" "github.com/ollama/ollama/format" ) const ( metalMinimumMemory = 512 * format.MebiByte ) func GetCPUMem() (memInfo, error) { return memInfo{ TotalMemory: uint64(C.getPhysicalMemory()), FreeMemory: uint64(C.getFreeMemory()), // FreeSwap omitted as Darwin uses dynamic paging }, nil } func GetCPUDetails() []CPU { query := "hw.perflevel0.physicalcpu" perfCores, err := syscall.SysctlUint32(query) if err != nil { slog.Warn("failed to discover physical CPU details", "query", query, "error", err) } query = "hw.perflevel1.physicalcpu" efficiencyCores, _ := syscall.SysctlUint32(query) // On x86 xeon this wont return data // Determine thread count query = "hw.logicalcpu" logicalCores, _ := syscall.SysctlUint32(query) return []CPU{ { CoreCount: int(perfCores + efficiencyCores), EfficiencyCoreCount: int(efficiencyCores), ThreadCount: int(logicalCores), }, } } func IsNUMA() bool { // numa support in ggml is linux only return false }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/discover/cpu_windows.go
discover/cpu_windows.go
package discover import ( "fmt" "log/slog" "syscall" "unsafe" "github.com/ollama/ollama/logutil" ) type MEMORYSTATUSEX struct { length uint32 MemoryLoad uint32 TotalPhys uint64 AvailPhys uint64 TotalPageFile uint64 AvailPageFile uint64 TotalVirtual uint64 AvailVirtual uint64 AvailExtendedVirtual uint64 } var ( k32 = syscall.NewLazyDLL("kernel32.dll") globalMemoryStatusExProc = k32.NewProc("GlobalMemoryStatusEx") sizeofMemoryStatusEx = uint32(unsafe.Sizeof(MEMORYSTATUSEX{})) GetLogicalProcessorInformationEx = k32.NewProc("GetLogicalProcessorInformationEx") ) func GetCPUMem() (memInfo, error) { memStatus := MEMORYSTATUSEX{length: sizeofMemoryStatusEx} r1, _, err := globalMemoryStatusExProc.Call(uintptr(unsafe.Pointer(&memStatus))) if r1 == 0 { return memInfo{}, fmt.Errorf("GlobalMemoryStatusEx failed: %w", err) } return memInfo{TotalMemory: memStatus.TotalPhys, FreeMemory: memStatus.AvailPhys, FreeSwap: memStatus.AvailPageFile}, nil } type LOGICAL_PROCESSOR_RELATIONSHIP uint32 const ( RelationProcessorCore LOGICAL_PROCESSOR_RELATIONSHIP = iota RelationNumaNode RelationCache RelationProcessorPackage RelationGroup RelationProcessorDie RelationNumaNodeEx RelationProcessorModule ) const RelationAll LOGICAL_PROCESSOR_RELATIONSHIP = 0xffff type GROUP_AFFINITY struct { Mask uintptr // KAFFINITY Group uint16 Reserved [3]uint16 } type PROCESSOR_RELATIONSHIP struct { Flags byte EfficiencyClass byte Reserved [20]byte GroupCount uint16 GroupMask [1]GROUP_AFFINITY // len GroupCount } // Omitted unused structs: NUMA_NODE_RELATIONSHIP CACHE_RELATIONSHIP GROUP_RELATIONSHIP type SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX struct { Relationship LOGICAL_PROCESSOR_RELATIONSHIP Size uint32 U [1]byte // Union len Size // PROCESSOR_RELATIONSHIP // NUMA_NODE_RELATIONSHIP // CACHE_RELATIONSHIP // GROUP_RELATIONSHIP } func (group *GROUP_AFFINITY) IsMember(target *GROUP_AFFINITY) bool { if group == nil || target == nil { return false } return group.Mask&target.Mask != 0 } type winPackage struct { groups []*GROUP_AFFINITY coreCount int // performance cores = coreCount - efficiencyCoreCount efficiencyCoreCount int threadCount int } func (pkg *winPackage) IsMember(target *GROUP_AFFINITY) bool { for _, group := range pkg.groups { if group.IsMember(target) { return true } } return false } func getLogicalProcessorInformationEx() ([]byte, error) { buf := make([]byte, 1) bufSize := len(buf) ret, _, err := GetLogicalProcessorInformationEx.Call( uintptr(RelationAll), uintptr(unsafe.Pointer(&buf[0])), uintptr(unsafe.Pointer(&bufSize)), ) if ret != 0 { logutil.Trace("failed to retrieve CPU payload size", "ret", ret, "size", bufSize, "error", err) return nil, fmt.Errorf("failed to determine size info ret:%d %w", ret, err) } buf = make([]byte, bufSize) ret, _, err = GetLogicalProcessorInformationEx.Call( uintptr(RelationAll), uintptr(unsafe.Pointer(&buf[0])), uintptr(unsafe.Pointer(&bufSize)), ) if ret == 0 { logutil.Trace("failed to retrieve CPU information", "ret", ret, "size", len(buf), "new_size", bufSize, "error", err) return nil, fmt.Errorf("failed to gather processor information ret:%d buflen:%d %w", ret, bufSize, err) } return buf, nil } func processSystemLogicalProcessorInforationList(buf []byte) []*winPackage { var slpi *SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX // Find all the packages first packages := []*winPackage{} for bufOffset := 0; bufOffset < len(buf); bufOffset += int(slpi.Size) { slpi = (*SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX)(unsafe.Pointer(&buf[bufOffset])) if slpi.Relationship != RelationProcessorPackage { continue } pr := (*PROCESSOR_RELATIONSHIP)(unsafe.Pointer(&slpi.U[0])) pkg := &winPackage{} ga0 := unsafe.Pointer(&pr.GroupMask[0]) for j := range pr.GroupCount { gm := (*GROUP_AFFINITY)(unsafe.Pointer(uintptr(ga0) + uintptr(j)*unsafe.Sizeof(GROUP_AFFINITY{}))) pkg.groups = append(pkg.groups, gm) } packages = append(packages, pkg) } slog.Info("packages", "count", len(packages)) // To identify efficiency cores we have to compare the relative values // Larger values are "less efficient" (aka, more performant) var maxEfficiencyClass byte for bufOffset := 0; bufOffset < len(buf); bufOffset += int(slpi.Size) { slpi = (*SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX)(unsafe.Pointer(&buf[bufOffset])) if slpi.Relationship != RelationProcessorCore { continue } pr := (*PROCESSOR_RELATIONSHIP)(unsafe.Pointer(&slpi.U[0])) if pr.EfficiencyClass > maxEfficiencyClass { maxEfficiencyClass = pr.EfficiencyClass } } if maxEfficiencyClass > 0 { slog.Info("efficiency cores detected", "maxEfficiencyClass", maxEfficiencyClass) } // then match up the Cores to the Packages, count up cores, threads and efficiency cores for bufOffset := 0; bufOffset < len(buf); bufOffset += int(slpi.Size) { slpi = (*SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX)(unsafe.Pointer(&buf[bufOffset])) if slpi.Relationship != RelationProcessorCore { continue } pr := (*PROCESSOR_RELATIONSHIP)(unsafe.Pointer(&slpi.U[0])) ga0 := unsafe.Pointer(&pr.GroupMask[0]) for j := range pr.GroupCount { gm := (*GROUP_AFFINITY)(unsafe.Pointer(uintptr(ga0) + uintptr(j)*unsafe.Sizeof(GROUP_AFFINITY{}))) for _, pkg := range packages { if pkg.IsMember(gm) { pkg.coreCount++ if pr.Flags == 0 { pkg.threadCount++ } else { pkg.threadCount += 2 } if pr.EfficiencyClass < maxEfficiencyClass { pkg.efficiencyCoreCount++ } } } } } // Summarize the results for i, pkg := range packages { slog.Info("", "package", i, "cores", pkg.coreCount, "efficiency", pkg.efficiencyCoreCount, "threads", pkg.threadCount) } return packages } func GetCPUDetails() []CPU { buf, err := getLogicalProcessorInformationEx() if err != nil { slog.Warn("failed to get CPU details", "error", err) return nil } packages := processSystemLogicalProcessorInforationList(buf) cpus := make([]CPU, len(packages)) for i, pkg := range packages { cpus[i].CoreCount = pkg.coreCount cpus[i].EfficiencyCoreCount = pkg.efficiencyCoreCount cpus[i].ThreadCount = pkg.threadCount } return cpus } func IsNUMA() bool { // numa support in ggml is linux only return false }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/parser/parser.go
parser/parser.go
package parser import ( "bufio" "bytes" "crypto/sha256" "errors" "fmt" "io" "net/http" "os" "os/user" "path/filepath" "runtime" "slices" "strconv" "strings" "sync" "golang.org/x/mod/semver" "golang.org/x/sync/errgroup" "golang.org/x/text/encoding/unicode" "golang.org/x/text/transform" "github.com/ollama/ollama/api" ) var ErrModelNotFound = errors.New("no Modelfile or safetensors files found") type Modelfile struct { Commands []Command } func (f Modelfile) String() string { var sb strings.Builder for _, cmd := range f.Commands { fmt.Fprintln(&sb, cmd.String()) } return sb.String() } var deprecatedParameters = []string{ "penalize_newline", "low_vram", "f16_kv", "logits_all", "vocab_only", "use_mlock", "mirostat", "mirostat_tau", "mirostat_eta", } // CreateRequest creates a new *api.CreateRequest from an existing Modelfile func (f Modelfile) CreateRequest(relativeDir string) (*api.CreateRequest, error) { req := &api.CreateRequest{} var messages []api.Message var licenses []string params := make(map[string]any) for _, c := range f.Commands { switch c.Name { case "model": path, err := expandPath(c.Args, relativeDir) if err != nil { return nil, err } digestMap, err := fileDigestMap(path) if errors.Is(err, os.ErrNotExist) { req.From = c.Args continue } else if err != nil { return nil, err } if req.Files == nil { req.Files = digestMap } else { for k, v := range digestMap { req.Files[k] = v } } case "adapter": path, err := expandPath(c.Args, relativeDir) if err != nil { return nil, err } digestMap, err := fileDigestMap(path) if err != nil { return nil, err } req.Adapters = digestMap case "template": req.Template = c.Args case "system": req.System = c.Args case "license": licenses = append(licenses, c.Args) case "renderer": req.Renderer = c.Args case "parser": req.Parser = c.Args case "requires": // golang.org/x/mod/semver requires "v" prefix requires := c.Args if !strings.HasPrefix(requires, "v") { requires = "v" + requires } if !semver.IsValid(requires) { return nil, fmt.Errorf("requires must be a valid semver (e.g. 0.14.0)") } req.Requires = strings.TrimPrefix(requires, "v") case "message": role, msg, _ := strings.Cut(c.Args, ": ") messages = append(messages, api.Message{Role: role, Content: msg}) default: if slices.Contains(deprecatedParameters, c.Name) { fmt.Printf("warning: parameter %s is deprecated\n", c.Name) break } ps, err := api.FormatParams(map[string][]string{c.Name: {c.Args}}) if err != nil { return nil, err } for k, v := range ps { if ks, ok := params[k].([]string); ok { params[k] = append(ks, v.([]string)...) } else if vs, ok := v.([]string); ok { params[k] = vs } else { params[k] = v } } } } if len(params) > 0 { req.Parameters = params } if len(messages) > 0 { req.Messages = messages } if len(licenses) > 0 { req.License = licenses } return req, nil } func fileDigestMap(path string) (map[string]string, error) { fl := make(map[string]string) fi, err := os.Stat(path) if err != nil { return nil, err } var files []string if fi.IsDir() { fs, err := filesForModel(path) if err != nil { return nil, err } for _, f := range fs { f, err := filepath.EvalSymlinks(f) if err != nil { return nil, err } rel, err := filepath.Rel(path, f) if err != nil { return nil, err } if !filepath.IsLocal(rel) { return nil, fmt.Errorf("insecure path: %s", rel) } files = append(files, f) } } else { files = []string{path} } var mu sync.Mutex var g errgroup.Group g.SetLimit(max(runtime.GOMAXPROCS(0)-1, 1)) for _, f := range files { g.Go(func() error { digest, err := digestForFile(f) if err != nil { return err } mu.Lock() defer mu.Unlock() fl[f] = digest return nil }) } if err := g.Wait(); err != nil { return nil, err } return fl, nil } func digestForFile(filename string) (string, error) { filepath, err := filepath.EvalSymlinks(filename) if err != nil { return "", err } bin, err := os.Open(filepath) if err != nil { return "", err } defer bin.Close() hash := sha256.New() if _, err := io.Copy(hash, bin); err != nil { return "", err } return fmt.Sprintf("sha256:%x", hash.Sum(nil)), nil } func filesForModel(path string) ([]string, error) { detectContentType := func(path string) (string, error) { f, err := os.Open(path) if err != nil { return "", err } defer f.Close() var b bytes.Buffer b.Grow(512) if _, err := io.CopyN(&b, f, 512); err != nil && !errors.Is(err, io.EOF) { return "", err } contentType, _, _ := strings.Cut(http.DetectContentType(b.Bytes()), ";") return contentType, nil } glob := func(pattern, contentType string) ([]string, error) { matches, err := filepath.Glob(pattern) if err != nil { return nil, err } for _, match := range matches { if ct, err := detectContentType(match); err != nil { return nil, err } else if len(contentType) > 0 && ct != contentType { return nil, fmt.Errorf("invalid content type: expected %s for %s", ct, match) } } return matches, nil } var files []string // some safetensors files do not properly match "application/octet-stream", so skip checking their contentType if st, _ := glob(filepath.Join(path, "model*.safetensors"), ""); len(st) > 0 { // safetensors files might be unresolved git lfs references; skip if they are // covers model-x-of-y.safetensors, model.fp32-x-of-y.safetensors, model.safetensors files = append(files, st...) } else if st, _ := glob(filepath.Join(path, "consolidated*.safetensors"), ""); len(st) > 0 { // covers consolidated.safetensors files = append(files, st...) } else if pt, _ := glob(filepath.Join(path, "pytorch_model*.bin"), "application/zip"); len(pt) > 0 { // pytorch files might also be unresolved git lfs references; skip if they are // covers pytorch_model-x-of-y.bin, pytorch_model.fp32-x-of-y.bin, pytorch_model.bin files = append(files, pt...) } else if pt, _ := glob(filepath.Join(path, "consolidated*.pth"), "application/zip"); len(pt) > 0 { // pytorch files might also be unresolved git lfs references; skip if they are // covers consolidated.x.pth, consolidated.pth files = append(files, pt...) } else if gg, _ := glob(filepath.Join(path, "*.gguf"), "application/octet-stream"); len(gg) > 0 { // covers gguf files ending in .gguf files = append(files, gg...) } else if gg, _ := glob(filepath.Join(path, "*.bin"), "application/octet-stream"); len(gg) > 0 { // covers gguf files ending in .bin files = append(files, gg...) } else { return nil, ErrModelNotFound } // add configuration files, json files are detected as text/plain js, err := glob(filepath.Join(path, "*.json"), "text/plain") if err != nil { return nil, err } files = append(files, js...) // bert models require a nested config.json // TODO(mxyng): merge this with the glob above js, err = glob(filepath.Join(path, "**/*.json"), "text/plain") if err != nil { return nil, err } files = append(files, js...) // add tokenizer.model if it exists (tokenizer.json is automatically picked up by the previous glob) // tokenizer.model might be a unresolved git lfs reference; error if it is if tks, _ := glob(filepath.Join(path, "tokenizer.model"), "application/octet-stream"); len(tks) > 0 { files = append(files, tks...) } else if tks, _ := glob(filepath.Join(path, "**/tokenizer.model"), "text/plain"); len(tks) > 0 { // some times tokenizer.model is in a subdirectory (e.g. meta-llama/Meta-Llama-3-8B) files = append(files, tks...) } return files, nil } type Command struct { Name string Args string } func (c Command) String() string { var sb strings.Builder switch c.Name { case "model": fmt.Fprintf(&sb, "FROM %s", c.Args) case "license", "template", "system", "adapter", "renderer", "parser", "requires": fmt.Fprintf(&sb, "%s %s", strings.ToUpper(c.Name), quote(c.Args)) case "message": role, message, _ := strings.Cut(c.Args, ": ") fmt.Fprintf(&sb, "MESSAGE %s %s", role, quote(message)) default: fmt.Fprintf(&sb, "PARAMETER %s %s", c.Name, quote(c.Args)) } return sb.String() } type state int const ( stateNil state = iota stateName stateValue stateParameter stateMessage stateComment ) var ( errMissingFrom = errors.New("no FROM line") errInvalidMessageRole = errors.New("message role must be one of \"system\", \"user\", or \"assistant\"") errInvalidCommand = errors.New("command must be one of \"from\", \"license\", \"template\", \"system\", \"adapter\", \"renderer\", \"parser\", \"parameter\", \"message\", or \"requires\"") ) type ParserError struct { LineNumber int Msg string } func (e *ParserError) Error() string { if e.LineNumber > 0 { return fmt.Sprintf("(line %d): %s", e.LineNumber, e.Msg) } return e.Msg } func ParseFile(r io.Reader) (*Modelfile, error) { var cmd Command var curr state var currLine int = 1 var b bytes.Buffer var role string var f Modelfile tr := unicode.BOMOverride(unicode.UTF8.NewDecoder()) br := bufio.NewReader(transform.NewReader(r, tr)) for { r, _, err := br.ReadRune() if errors.Is(err, io.EOF) { break } else if err != nil { return nil, err } if isNewline(r) { currLine++ } next, r, err := parseRuneForState(r, curr) if errors.Is(err, io.ErrUnexpectedEOF) { return nil, fmt.Errorf("%w: %s", err, b.String()) } else if err != nil { return nil, &ParserError{ LineNumber: currLine, Msg: err.Error(), } } // process the state transition, some transitions need to be intercepted and redirected if next != curr { switch curr { case stateName: if !isValidCommand(b.String()) { return nil, &ParserError{ LineNumber: currLine, Msg: errInvalidCommand.Error(), } } // next state sometimes depends on the current buffer value switch s := strings.ToLower(b.String()); s { case "from": cmd.Name = "model" case "parameter": // transition to stateParameter which sets command name next = stateParameter case "message": // transition to stateMessage which validates the message role next = stateMessage fallthrough default: cmd.Name = s } case stateParameter: cmd.Name = b.String() case stateMessage: if !isValidMessageRole(b.String()) { return nil, &ParserError{ LineNumber: currLine, Msg: errInvalidMessageRole.Error(), } } role = b.String() case stateComment, stateNil: // pass case stateValue: s, ok := unquote(strings.TrimSpace(b.String())) if !ok || isSpace(r) { if _, err := b.WriteRune(r); err != nil { return nil, err } continue } if role != "" { s = role + ": " + s role = "" } cmd.Args = s f.Commands = append(f.Commands, cmd) } b.Reset() curr = next } if strconv.IsPrint(r) { if _, err := b.WriteRune(r); err != nil { return nil, err } } } // flush the buffer switch curr { case stateComment, stateNil: // pass; nothing to flush case stateValue: s, ok := unquote(strings.TrimSpace(b.String())) if !ok { return nil, io.ErrUnexpectedEOF } if role != "" { s = role + ": " + s } cmd.Args = s f.Commands = append(f.Commands, cmd) default: return nil, io.ErrUnexpectedEOF } for _, cmd := range f.Commands { if cmd.Name == "model" { return &f, nil } } return nil, errMissingFrom } func parseRuneForState(r rune, cs state) (state, rune, error) { switch cs { case stateNil: switch { case r == '#': return stateComment, 0, nil case isSpace(r), isNewline(r): return stateNil, 0, nil default: return stateName, r, nil } case stateName: switch { case isAlpha(r): return stateName, r, nil case isSpace(r): return stateValue, 0, nil default: return stateNil, 0, errInvalidCommand } case stateValue: switch { case isNewline(r): return stateNil, r, nil case isSpace(r): return stateNil, r, nil default: return stateValue, r, nil } case stateParameter: switch { case isAlpha(r), isNumber(r), r == '_': return stateParameter, r, nil case isSpace(r): return stateValue, 0, nil default: return stateNil, 0, io.ErrUnexpectedEOF } case stateMessage: switch { case isAlpha(r): return stateMessage, r, nil case isSpace(r): return stateValue, 0, nil default: return stateNil, 0, io.ErrUnexpectedEOF } case stateComment: switch { case isNewline(r): return stateNil, 0, nil default: return stateComment, 0, nil } default: return stateNil, 0, errors.New("") } } func quote(s string) string { if strings.Contains(s, "\n") || strings.HasPrefix(s, " ") || strings.HasSuffix(s, " ") { if strings.Contains(s, "\"") { return `"""` + s + `"""` } return `"` + s + `"` } return s } func unquote(s string) (string, bool) { // TODO: single quotes if len(s) >= 3 && s[:3] == `"""` { if len(s) >= 6 && s[len(s)-3:] == `"""` { return s[3 : len(s)-3], true } return "", false } if len(s) >= 1 && s[0] == '"' { if len(s) >= 2 && s[len(s)-1] == '"' { return s[1 : len(s)-1], true } return "", false } return s, true } func isAlpha(r rune) bool { return r >= 'a' && r <= 'z' || r >= 'A' && r <= 'Z' } func isNumber(r rune) bool { return r >= '0' && r <= '9' } func isSpace(r rune) bool { return r == ' ' || r == '\t' } func isNewline(r rune) bool { return r == '\r' || r == '\n' } func isValidMessageRole(role string) bool { return role == "system" || role == "user" || role == "assistant" } func isValidCommand(cmd string) bool { switch strings.ToLower(cmd) { case "from", "license", "template", "system", "adapter", "renderer", "parser", "parameter", "message", "requires": return true default: return false } } func expandPathImpl(path, relativeDir string, currentUserFunc func() (*user.User, error), lookupUserFunc func(string) (*user.User, error)) (string, error) { if filepath.IsAbs(path) || strings.HasPrefix(path, "\\") || strings.HasPrefix(path, "/") { return filepath.Abs(path) } else if strings.HasPrefix(path, "~") { var homeDir string if path == "~" || strings.HasPrefix(path, "~/") { // Current user's home directory currentUser, err := currentUserFunc() if err != nil { return "", fmt.Errorf("failed to get current user: %w", err) } homeDir = currentUser.HomeDir path = strings.TrimPrefix(path, "~") } else { // Specific user's home directory parts := strings.SplitN(path[1:], "/", 2) userInfo, err := lookupUserFunc(parts[0]) if err != nil { return "", fmt.Errorf("failed to find user '%s': %w", parts[0], err) } homeDir = userInfo.HomeDir if len(parts) > 1 { path = "/" + parts[1] } else { path = "" } } path = filepath.Join(homeDir, path) } else { path = filepath.Join(relativeDir, path) } return filepath.Abs(path) } func expandPath(path, relativeDir string) (string, error) { return expandPathImpl(path, relativeDir, user.Current, user.Lookup) }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/parser/expandpath_test.go
parser/expandpath_test.go
package parser import ( "os" "os/user" "path/filepath" "runtime" "testing" ) func TestExpandPath(t *testing.T) { mockCurrentUser := func() (*user.User, error) { return &user.User{ Username: "testuser", HomeDir: func() string { if os.PathSeparator == '\\' { return filepath.FromSlash("D:/home/testuser") } return "/home/testuser" }(), }, nil } mockLookupUser := func(username string) (*user.User, error) { fakeUsers := map[string]string{ "testuser": func() string { if os.PathSeparator == '\\' { return filepath.FromSlash("D:/home/testuser") } return "/home/testuser" }(), "anotheruser": func() string { if os.PathSeparator == '\\' { return filepath.FromSlash("D:/home/anotheruser") } return "/home/anotheruser" }(), } if homeDir, ok := fakeUsers[username]; ok { return &user.User{ Username: username, HomeDir: homeDir, }, nil } return nil, os.ErrNotExist } pwd, err := os.Getwd() if err != nil { t.Fatal(err) } t.Run("unix tests", func(t *testing.T) { if runtime.GOOS == "windows" { return } tests := []struct { path string relativeDir string expected string shouldErr bool }{ {"~", "", "/home/testuser", false}, {"~/myfolder/myfile.txt", "", "/home/testuser/myfolder/myfile.txt", false}, {"~anotheruser/docs/file.txt", "", "/home/anotheruser/docs/file.txt", false}, {"~nonexistentuser/file.txt", "", "", true}, {"relative/path/to/file", "", filepath.Join(pwd, "relative/path/to/file"), false}, {"/absolute/path/to/file", "", "/absolute/path/to/file", false}, {"/absolute/path/to/file", "someotherdir/", "/absolute/path/to/file", false}, {".", pwd, pwd, false}, {".", "", pwd, false}, {"somefile", "somedir", filepath.Join(pwd, "somedir", "somefile"), false}, } for _, test := range tests { result, err := expandPathImpl(test.path, test.relativeDir, mockCurrentUser, mockLookupUser) if (err != nil) != test.shouldErr { t.Errorf("expandPathImpl(%q) returned error: %v, expected error: %v", test.path, err != nil, test.shouldErr) } if result != test.expected && !test.shouldErr { t.Errorf("expandPathImpl(%q) = %q, want %q", test.path, result, test.expected) } } }) t.Run("windows tests", func(t *testing.T) { if runtime.GOOS != "windows" { return } tests := []struct { path string relativeDir string expected string shouldErr bool }{ {"~", "", "D:\\home\\testuser", false}, {"~/myfolder/myfile.txt", "", "D:\\home\\testuser\\myfolder\\myfile.txt", false}, {"~anotheruser/docs/file.txt", "", "D:\\home\\anotheruser\\docs\\file.txt", false}, {"~nonexistentuser/file.txt", "", "", true}, {"relative\\path\\to\\file", "", filepath.Join(pwd, "relative\\path\\to\\file"), false}, {"D:\\absolute\\path\\to\\file", "", "D:\\absolute\\path\\to\\file", false}, {"D:\\absolute\\path\\to\\file", "someotherdir/", "D:\\absolute\\path\\to\\file", false}, {".", pwd, pwd, false}, {".", "", pwd, false}, {"somefile", "somedir", filepath.Join(pwd, "somedir", "somefile"), false}, } for _, test := range tests { result, err := expandPathImpl(test.path, test.relativeDir, mockCurrentUser, mockLookupUser) if (err != nil) != test.shouldErr { t.Errorf("expandPathImpl(%q) returned error: %v, expected error: %v", test.path, err != nil, test.shouldErr) } if result != test.expected && !test.shouldErr { t.Errorf("expandPathImpl(%q) = %q, want %q", test.path, result, test.expected) } } }) }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/parser/parser_test.go
parser/parser_test.go
package parser import ( "bytes" "crypto/sha256" "encoding/binary" "errors" "fmt" "io" "maps" "os" "path/filepath" "strings" "testing" "unicode/utf16" "github.com/google/go-cmp/cmp" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "golang.org/x/text/encoding" "golang.org/x/text/encoding/unicode" "github.com/ollama/ollama/api" "github.com/ollama/ollama/fs/ggml" ) func TestParseFileFile(t *testing.T) { input := ` FROM model1 ADAPTER adapter1 LICENSE MIT PARAMETER param1 value1 PARAMETER param2 value2 TEMPLATE """{{ if .System }}<|start_header_id|>system<|end_header_id|> {{ .System }}<|eot_id|>{{ end }}{{ if .Prompt }}<|start_header_id|>user<|end_header_id|> {{ .Prompt }}<|eot_id|>{{ end }}<|start_header_id|>assistant<|end_header_id|> {{ .Response }}<|eot_id|>""" ` reader := strings.NewReader(input) modelfile, err := ParseFile(reader) require.NoError(t, err) expectedCommands := []Command{ {Name: "model", Args: "model1"}, {Name: "adapter", Args: "adapter1"}, {Name: "license", Args: "MIT"}, {Name: "param1", Args: "value1"}, {Name: "param2", Args: "value2"}, {Name: "template", Args: "{{ if .System }}<|start_header_id|>system<|end_header_id|>\n\n{{ .System }}<|eot_id|>{{ end }}{{ if .Prompt }}<|start_header_id|>user<|end_header_id|>\n\n{{ .Prompt }}<|eot_id|>{{ end }}<|start_header_id|>assistant<|end_header_id|>\n\n{{ .Response }}<|eot_id|>"}, } assert.Equal(t, expectedCommands, modelfile.Commands) } func TestParseFileTrimSpace(t *testing.T) { input := ` FROM " model 1" ADAPTER adapter3 LICENSE "MIT " PARAMETER param1 value1 PARAMETER param2 value2 TEMPLATE """ {{ if .System }}<|start_header_id|>system<|end_header_id|> {{ .System }}<|eot_id|>{{ end }}{{ if .Prompt }}<|start_header_id|>user<|end_header_id|> {{ .Prompt }}<|eot_id|>{{ end }}<|start_header_id|>assistant<|end_header_id|> {{ .Response }}<|eot_id|> """ ` reader := strings.NewReader(input) modelfile, err := ParseFile(reader) require.NoError(t, err) expectedCommands := []Command{ {Name: "model", Args: " model 1"}, {Name: "adapter", Args: "adapter3"}, {Name: "license", Args: "MIT "}, {Name: "param1", Args: "value1"}, {Name: "param2", Args: "value2"}, {Name: "template", Args: " {{ if .System }}<|start_header_id|>system<|end_header_id|>\n\n{{ .System }}<|eot_id|>{{ end }}{{ if .Prompt }}<|start_header_id|>user<|end_header_id|>\n\n{{ .Prompt }}<|eot_id|>{{ end }}<|start_header_id|>assistant<|end_header_id|>\n\n{{ .Response }}<|eot_id|> "}, } assert.Equal(t, expectedCommands, modelfile.Commands) } func TestParseFileFrom(t *testing.T) { cases := []struct { input string expected []Command err error }{ { "FROM \"FOO BAR \"", []Command{{Name: "model", Args: "FOO BAR "}}, nil, }, { "FROM \"FOO BAR\"\nPARAMETER param1 value1", []Command{{Name: "model", Args: "FOO BAR"}, {Name: "param1", Args: "value1"}}, nil, }, { "FROM FOOO BAR ", []Command{{Name: "model", Args: "FOOO BAR"}}, nil, }, { "FROM /what/is/the path ", []Command{{Name: "model", Args: "/what/is/the path"}}, nil, }, { "FROM foo", []Command{{Name: "model", Args: "foo"}}, nil, }, { "FROM /path/to/model", []Command{{Name: "model", Args: "/path/to/model"}}, nil, }, { "FROM /path/to/model/fp16.bin", []Command{{Name: "model", Args: "/path/to/model/fp16.bin"}}, nil, }, { "FROM llama3:latest", []Command{{Name: "model", Args: "llama3:latest"}}, nil, }, { "FROM llama3:7b-instruct-q4_K_M", []Command{{Name: "model", Args: "llama3:7b-instruct-q4_K_M"}}, nil, }, { "", nil, errMissingFrom, }, { "PARAMETER param1 value1", nil, errMissingFrom, }, { "PARAMETER param1 value1\nFROM foo", []Command{{Name: "param1", Args: "value1"}, {Name: "model", Args: "foo"}}, nil, }, { "PARAMETER what the \nFROM lemons make lemonade ", []Command{{Name: "what", Args: "the"}, {Name: "model", Args: "lemons make lemonade"}}, nil, }, } for _, c := range cases { t.Run("", func(t *testing.T) { modelfile, err := ParseFile(strings.NewReader(c.input)) require.ErrorIs(t, err, c.err) if modelfile != nil { assert.Equal(t, c.expected, modelfile.Commands) } }) } } func TestParseFileParametersMissingValue(t *testing.T) { input := ` FROM foo PARAMETER param1 ` reader := strings.NewReader(input) _, err := ParseFile(reader) require.ErrorIs(t, err, io.ErrUnexpectedEOF) } func TestParseFileBadCommand(t *testing.T) { input := ` FROM foo BADCOMMAND param1 value1 ` parserError := &ParserError{ LineNumber: 3, Msg: errInvalidCommand.Error(), } _, err := ParseFile(strings.NewReader(input)) if !errors.As(err, &parserError) { t.Errorf("unexpected error: expected: %s, actual: %s", parserError.Error(), err.Error()) } } func TestParseFileRenderer(t *testing.T) { input := ` FROM foo RENDERER renderer1 ` reader := strings.NewReader(input) modelfile, err := ParseFile(reader) require.NoError(t, err) assert.Equal(t, []Command{{Name: "model", Args: "foo"}, {Name: "renderer", Args: "renderer1"}}, modelfile.Commands) } func TestParseFileParser(t *testing.T) { input := ` FROM foo PARSER parser1 ` reader := strings.NewReader(input) modelfile, err := ParseFile(reader) require.NoError(t, err) assert.Equal(t, []Command{{Name: "model", Args: "foo"}, {Name: "parser", Args: "parser1"}}, modelfile.Commands) } func TestParseFileMessages(t *testing.T) { cases := []struct { input string expected []Command err error }{ { ` FROM foo MESSAGE system You are a file parser. Always parse things. `, []Command{ {Name: "model", Args: "foo"}, {Name: "message", Args: "system: You are a file parser. Always parse things."}, }, nil, }, { ` FROM foo MESSAGE system You are a file parser. Always parse things.`, []Command{ {Name: "model", Args: "foo"}, {Name: "message", Args: "system: You are a file parser. Always parse things."}, }, nil, }, { ` FROM foo MESSAGE system You are a file parser. Always parse things. MESSAGE user Hey there! MESSAGE assistant Hello, I want to parse all the things! `, []Command{ {Name: "model", Args: "foo"}, {Name: "message", Args: "system: You are a file parser. Always parse things."}, {Name: "message", Args: "user: Hey there!"}, {Name: "message", Args: "assistant: Hello, I want to parse all the things!"}, }, nil, }, { ` FROM foo MESSAGE system """ You are a multiline file parser. Always parse things. """ `, []Command{ {Name: "model", Args: "foo"}, {Name: "message", Args: "system: \nYou are a multiline file parser. Always parse things.\n"}, }, nil, }, { ` FROM foo MESSAGE badguy I'm a bad guy! `, nil, &ParserError{ LineNumber: 3, Msg: errInvalidMessageRole.Error(), }, }, { ` FROM foo MESSAGE system `, nil, io.ErrUnexpectedEOF, }, { ` FROM foo MESSAGE system`, nil, io.ErrUnexpectedEOF, }, } for _, tt := range cases { t.Run("", func(t *testing.T) { modelfile, err := ParseFile(strings.NewReader(tt.input)) if modelfile != nil { assert.Equal(t, tt.expected, modelfile.Commands) } if tt.err == nil { if err != nil { t.Fatalf("expected no error, but got %v", err) } return } switch tt.err.(type) { case *ParserError: var pErr *ParserError if errors.As(err, &pErr) { // got the correct type of error return } } if errors.Is(err, tt.err) { return } t.Fatalf("unexpected error: expected: %v, actual: %v", tt.err, err) }) } } func TestParseFileQuoted(t *testing.T) { cases := []struct { multiline string expected []Command err error }{ { ` FROM foo SYSTEM """ This is a multiline system. """ `, []Command{ {Name: "model", Args: "foo"}, {Name: "system", Args: "\nThis is a\nmultiline system.\n"}, }, nil, }, { ` FROM foo SYSTEM """ This is a multiline system.""" `, []Command{ {Name: "model", Args: "foo"}, {Name: "system", Args: "\nThis is a\nmultiline system."}, }, nil, }, { ` FROM foo SYSTEM """This is a multiline system.""" `, []Command{ {Name: "model", Args: "foo"}, {Name: "system", Args: "This is a\nmultiline system."}, }, nil, }, { ` FROM foo SYSTEM """This is a multiline system.""" `, []Command{ {Name: "model", Args: "foo"}, {Name: "system", Args: "This is a multiline system."}, }, nil, }, { ` FROM foo SYSTEM """This is a multiline system."" `, nil, io.ErrUnexpectedEOF, }, { ` FROM foo SYSTEM " `, nil, io.ErrUnexpectedEOF, }, { ` FROM foo SYSTEM """ This is a multiline system with "quotes". """ `, []Command{ {Name: "model", Args: "foo"}, {Name: "system", Args: "\nThis is a multiline system with \"quotes\".\n"}, }, nil, }, { ` FROM foo SYSTEM """""" `, []Command{ {Name: "model", Args: "foo"}, {Name: "system", Args: ""}, }, nil, }, { ` FROM foo SYSTEM "" `, []Command{ {Name: "model", Args: "foo"}, {Name: "system", Args: ""}, }, nil, }, { ` FROM foo SYSTEM "'" `, []Command{ {Name: "model", Args: "foo"}, {Name: "system", Args: "'"}, }, nil, }, { ` FROM foo SYSTEM """''"'""'""'"'''''""'""'""" `, []Command{ {Name: "model", Args: "foo"}, {Name: "system", Args: `''"'""'""'"'''''""'""'`}, }, nil, }, { ` FROM foo TEMPLATE """ {{ .Prompt }} """`, []Command{ {Name: "model", Args: "foo"}, {Name: "template", Args: "\n{{ .Prompt }}\n"}, }, nil, }, } for _, c := range cases { t.Run("", func(t *testing.T) { modelfile, err := ParseFile(strings.NewReader(c.multiline)) require.ErrorIs(t, err, c.err) if modelfile != nil { assert.Equal(t, c.expected, modelfile.Commands) } }) } } func TestParseFileParameters(t *testing.T) { cases := map[string]struct { name, value string }{ "numa true": {"numa", "true"}, "num_ctx 1": {"num_ctx", "1"}, "num_batch 1": {"num_batch", "1"}, "num_gqa 1": {"num_gqa", "1"}, "num_gpu 1": {"num_gpu", "1"}, "main_gpu 1": {"main_gpu", "1"}, "use_mmap true": {"use_mmap", "true"}, "num_thread 1": {"num_thread", "1"}, "num_keep 1": {"num_keep", "1"}, "seed 1": {"seed", "1"}, "num_predict 1": {"num_predict", "1"}, "top_k 1": {"top_k", "1"}, "top_p 1.0": {"top_p", "1.0"}, "min_p 0.05": {"min_p", "0.05"}, "typical_p 1.0": {"typical_p", "1.0"}, "repeat_last_n 1": {"repeat_last_n", "1"}, "temperature 1.0": {"temperature", "1.0"}, "repeat_penalty 1.0": {"repeat_penalty", "1.0"}, "presence_penalty 1.0": {"presence_penalty", "1.0"}, "frequency_penalty 1.0": {"frequency_penalty", "1.0"}, "penalize_newline true": {"penalize_newline", "true"}, "stop ### User:": {"stop", "### User:"}, "stop ### User: ": {"stop", "### User:"}, "stop \"### User:\"": {"stop", "### User:"}, "stop \"### User: \"": {"stop", "### User: "}, "stop \"\"\"### User:\"\"\"": {"stop", "### User:"}, "stop \"\"\"### User:\n\"\"\"": {"stop", "### User:\n"}, "stop <|endoftext|>": {"stop", "<|endoftext|>"}, "stop <|eot_id|>": {"stop", "<|eot_id|>"}, "stop </s>": {"stop", "</s>"}, } for k, v := range cases { t.Run(k, func(t *testing.T) { var b bytes.Buffer fmt.Fprintln(&b, "FROM foo") fmt.Fprintln(&b, "PARAMETER", k) modelfile, err := ParseFile(&b) require.NoError(t, err) assert.Equal(t, []Command{ {Name: "model", Args: "foo"}, {Name: v.name, Args: v.value}, }, modelfile.Commands) }) } } func TestParseFileComments(t *testing.T) { cases := []struct { input string expected []Command }{ { ` # comment FROM foo `, []Command{ {Name: "model", Args: "foo"}, }, }, } for _, c := range cases { t.Run("", func(t *testing.T) { modelfile, err := ParseFile(strings.NewReader(c.input)) require.NoError(t, err) assert.Equal(t, c.expected, modelfile.Commands) }) } } func TestParseFileFormatParseFile(t *testing.T) { cases := []string{ ` FROM foo ADAPTER adapter1 LICENSE MIT PARAMETER param1 value1 PARAMETER param2 value2 TEMPLATE template1 MESSAGE system You are a file parser. Always parse things. MESSAGE user Hey there! MESSAGE assistant Hello, I want to parse all the things! `, ` FROM foo ADAPTER adapter1 LICENSE MIT PARAMETER param1 value1 PARAMETER param2 value2 TEMPLATE template1 MESSAGE system """ You are a store greeter. Always respond with "Hello!". """ MESSAGE user Hey there! MESSAGE assistant Hello, I want to parse all the things! `, ` FROM foo ADAPTER adapter1 LICENSE """ Very long and boring legal text. Blah blah blah. "Oh look, a quote!" """ PARAMETER param1 value1 PARAMETER param2 value2 TEMPLATE template1 MESSAGE system """ You are a store greeter. Always respond with "Hello!". """ MESSAGE user Hey there! MESSAGE assistant Hello, I want to parse all the things! `, ` FROM foo SYSTEM "" `, } for _, c := range cases { t.Run("", func(t *testing.T) { modelfile, err := ParseFile(strings.NewReader(c)) require.NoError(t, err) modelfile2, err := ParseFile(strings.NewReader(modelfile.String())) require.NoError(t, err) assert.Equal(t, modelfile, modelfile2) }) } } func TestParseFileUTF16ParseFile(t *testing.T) { data := `FROM bob PARAMETER param1 1 PARAMETER param2 4096 SYSTEM You are a utf16 file. ` expected := []Command{ {Name: "model", Args: "bob"}, {Name: "param1", Args: "1"}, {Name: "param2", Args: "4096"}, {Name: "system", Args: "You are a utf16 file."}, } t.Run("le", func(t *testing.T) { var b bytes.Buffer require.NoError(t, binary.Write(&b, binary.LittleEndian, []byte{0xff, 0xfe})) require.NoError(t, binary.Write(&b, binary.LittleEndian, utf16.Encode([]rune(data)))) actual, err := ParseFile(&b) require.NoError(t, err) assert.Equal(t, expected, actual.Commands) }) t.Run("be", func(t *testing.T) { var b bytes.Buffer require.NoError(t, binary.Write(&b, binary.BigEndian, []byte{0xfe, 0xff})) require.NoError(t, binary.Write(&b, binary.BigEndian, utf16.Encode([]rune(data)))) actual, err := ParseFile(&b) require.NoError(t, err) assert.Equal(t, expected, actual.Commands) }) } func TestParseMultiByte(t *testing.T) { input := `FROM test SYSTEM 你好👋` expect := []Command{ {Name: "model", Args: "test"}, {Name: "system", Args: "你好👋"}, } encodings := []encoding.Encoding{ unicode.UTF8, unicode.UTF16(unicode.LittleEndian, unicode.UseBOM), unicode.UTF16(unicode.BigEndian, unicode.UseBOM), } for _, encoding := range encodings { t.Run(fmt.Sprintf("%s", encoding), func(t *testing.T) { s, err := encoding.NewEncoder().String(input) require.NoError(t, err) actual, err := ParseFile(strings.NewReader(s)) require.NoError(t, err) assert.Equal(t, expect, actual.Commands) }) } } func TestCreateRequest(t *testing.T) { cases := []struct { input string expected *api.CreateRequest }{ { `FROM test`, &api.CreateRequest{From: "test"}, }, { `FROM test TEMPLATE some template `, &api.CreateRequest{ From: "test", Template: "some template", }, }, { `FROM test LICENSE single license PARAMETER temperature 0.5 MESSAGE user Hello `, &api.CreateRequest{ From: "test", License: []string{"single license"}, Parameters: map[string]any{"temperature": float32(0.5)}, Messages: []api.Message{ {Role: "user", Content: "Hello"}, }, }, }, { `FROM test PARAMETER temperature 0.5 PARAMETER top_k 1 SYSTEM You are a bot. LICENSE license1 LICENSE license2 MESSAGE user Hello there! MESSAGE assistant Hi! How are you? `, &api.CreateRequest{ From: "test", License: []string{"license1", "license2"}, System: "You are a bot.", Parameters: map[string]any{"temperature": float32(0.5), "top_k": int64(1)}, Messages: []api.Message{ {Role: "user", Content: "Hello there!"}, {Role: "assistant", Content: "Hi! How are you?"}, }, }, }, } for _, c := range cases { s, err := unicode.UTF8.NewEncoder().String(c.input) if err != nil { t.Fatal(err) } p, err := ParseFile(strings.NewReader(s)) if err != nil { t.Error(err) } actual, err := p.CreateRequest("") if err != nil { t.Error(err) } if diff := cmp.Diff(actual, c.expected); diff != "" { t.Errorf("mismatch (-got +want):\n%s", diff) } } } func getSHA256Digest(t *testing.T, r io.Reader) (string, int64) { t.Helper() h := sha256.New() n, err := io.Copy(h, r) if err != nil { t.Fatal(err) } return fmt.Sprintf("sha256:%x", h.Sum(nil)), n } func createBinFile(t *testing.T, kv map[string]any, ti []*ggml.Tensor) (string, string) { t.Helper() f, err := os.CreateTemp(t.TempDir(), "testbin.*.gguf") if err != nil { t.Fatal(err) } defer f.Close() base := map[string]any{"general.architecture": "test"} maps.Copy(base, kv) if err := ggml.WriteGGUF(f, base, ti); err != nil { t.Fatal(err) } // Calculate sha256 of file if _, err := f.Seek(0, 0); err != nil { t.Fatal(err) } digest, _ := getSHA256Digest(t, f) return f.Name(), digest } func TestCreateRequestFiles(t *testing.T) { n1, d1 := createBinFile(t, nil, nil) n2, d2 := createBinFile(t, map[string]any{"foo": "bar"}, nil) cases := []struct { input string expected *api.CreateRequest }{ { fmt.Sprintf("FROM %s", n1), &api.CreateRequest{Files: map[string]string{n1: d1}}, }, { fmt.Sprintf("FROM %s\nFROM %s", n1, n2), &api.CreateRequest{Files: map[string]string{n1: d1, n2: d2}}, }, } for _, c := range cases { s, err := unicode.UTF8.NewEncoder().String(c.input) if err != nil { t.Fatal(err) } p, err := ParseFile(strings.NewReader(s)) if err != nil { t.Error(err) } actual, err := p.CreateRequest("") if err != nil { t.Error(err) } if diff := cmp.Diff(actual, c.expected); diff != "" { t.Errorf("mismatch (-got +want):\n%s", diff) } } } func TestFilesForModel(t *testing.T) { tests := []struct { name string setup func(string) error wantFiles []string wantErr bool expectErrType error }{ { name: "safetensors model files", setup: func(dir string) error { files := []string{ "model-00001-of-00002.safetensors", "model-00002-of-00002.safetensors", "config.json", "tokenizer.json", } for _, file := range files { if err := os.WriteFile(filepath.Join(dir, file), []byte("test content"), 0o644); err != nil { return err } } return nil }, wantFiles: []string{ "model-00001-of-00002.safetensors", "model-00002-of-00002.safetensors", "config.json", "tokenizer.json", }, }, { name: "safetensors with both tokenizer.json and tokenizer.model", setup: func(dir string) error { // Create binary content for tokenizer.model (application/octet-stream) binaryContent := make([]byte, 512) for i := range binaryContent { binaryContent[i] = byte(i % 256) } files := []string{ "model-00001-of-00001.safetensors", "config.json", "tokenizer.json", } for _, file := range files { if err := os.WriteFile(filepath.Join(dir, file), []byte("test content"), 0o644); err != nil { return err } } // Write tokenizer.model as binary if err := os.WriteFile(filepath.Join(dir, "tokenizer.model"), binaryContent, 0o644); err != nil { return err } return nil }, wantFiles: []string{ "model-00001-of-00001.safetensors", "config.json", "tokenizer.json", "tokenizer.model", }, }, { name: "safetensors with consolidated files - prefers model files", setup: func(dir string) error { files := []string{ "model-00001-of-00001.safetensors", "consolidated.safetensors", "config.json", } for _, file := range files { if err := os.WriteFile(filepath.Join(dir, file), []byte("test content"), 0o644); err != nil { return err } } return nil }, wantFiles: []string{ "model-00001-of-00001.safetensors", // consolidated files should be excluded "config.json", }, }, { name: "safetensors without model-.safetensors files - uses consolidated", setup: func(dir string) error { files := []string{ "consolidated.safetensors", "config.json", } for _, file := range files { if err := os.WriteFile(filepath.Join(dir, file), []byte("test content"), 0o644); err != nil { return err } } return nil }, wantFiles: []string{ "consolidated.safetensors", "config.json", }, }, { name: "pytorch model files", setup: func(dir string) error { // Create a file that will be detected as application/zip zipHeader := []byte{0x50, 0x4B, 0x03, 0x04} // PK zip header files := []string{ "pytorch_model-00001-of-00002.bin", "pytorch_model-00002-of-00002.bin", "config.json", } for _, file := range files { content := zipHeader if file == "config.json" { content = []byte(`{"config": true}`) } if err := os.WriteFile(filepath.Join(dir, file), content, 0o644); err != nil { return err } } return nil }, wantFiles: []string{ "pytorch_model-00001-of-00002.bin", "pytorch_model-00002-of-00002.bin", "config.json", }, }, { name: "consolidated pth files", setup: func(dir string) error { zipHeader := []byte{0x50, 0x4B, 0x03, 0x04} files := []string{ "consolidated.00.pth", "consolidated.01.pth", "config.json", } for _, file := range files { content := zipHeader if file == "config.json" { content = []byte(`{"config": true}`) } if err := os.WriteFile(filepath.Join(dir, file), content, 0o644); err != nil { return err } } return nil }, wantFiles: []string{ "consolidated.00.pth", "consolidated.01.pth", "config.json", }, }, { name: "gguf files", setup: func(dir string) error { // Create binary content that will be detected as application/octet-stream binaryContent := make([]byte, 512) for i := range binaryContent { binaryContent[i] = byte(i % 256) } files := []string{ "model.gguf", "config.json", } for _, file := range files { content := binaryContent if file == "config.json" { content = []byte(`{"config": true}`) } if err := os.WriteFile(filepath.Join(dir, file), content, 0o644); err != nil { return err } } return nil }, wantFiles: []string{ "model.gguf", "config.json", }, }, { name: "bin files as gguf", setup: func(dir string) error { binaryContent := make([]byte, 512) for i := range binaryContent { binaryContent[i] = byte(i % 256) } files := []string{ "model.bin", "config.json", } for _, file := range files { content := binaryContent if file == "config.json" { content = []byte(`{"config": true}`) } if err := os.WriteFile(filepath.Join(dir, file), content, 0o644); err != nil { return err } } return nil }, wantFiles: []string{ "model.bin", "config.json", }, }, { name: "no model files found", setup: func(dir string) error { // Only create non-model files files := []string{"README.md", "config.json"} for _, file := range files { if err := os.WriteFile(filepath.Join(dir, file), []byte("content"), 0o644); err != nil { return err } } return nil }, wantErr: true, expectErrType: ErrModelNotFound, }, { name: "invalid content type for pytorch model", setup: func(dir string) error { // Create pytorch model file with wrong content type (text instead of zip) files := []string{ "pytorch_model.bin", "config.json", } for _, file := range files { content := []byte("plain text content") if err := os.WriteFile(filepath.Join(dir, file), content, 0o644); err != nil { return err } } return nil }, wantErr: true, }, } tmpDir := t.TempDir() for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { testDir := filepath.Join(tmpDir, tt.name) if err := os.MkdirAll(testDir, 0o755); err != nil { t.Fatalf("Failed to create test directory: %v", err) } if err := tt.setup(testDir); err != nil { t.Fatalf("Setup failed: %v", err) } files, err := filesForModel(testDir) if tt.wantErr { if err == nil { t.Error("Expected error, but got none") } if tt.expectErrType != nil && err != tt.expectErrType { t.Errorf("Expected error type %v, got %v", tt.expectErrType, err) } return } if err != nil { t.Errorf("Unexpected error: %v", err) return } var relativeFiles []string for _, file := range files { rel, err := filepath.Rel(testDir, file) if err != nil { t.Fatalf("Failed to get relative path: %v", err) } relativeFiles = append(relativeFiles, rel) } if len(relativeFiles) != len(tt.wantFiles) { t.Errorf("Expected %d files, got %d: %v", len(tt.wantFiles), len(relativeFiles), relativeFiles) } fileSet := make(map[string]bool) for _, file := range relativeFiles { fileSet[file] = true } for _, wantFile := range tt.wantFiles { if !fileSet[wantFile] { t.Errorf("Missing expected file: %s", wantFile) } } }) } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/fs/config.go
fs/config.go
package fs type Config interface { Architecture() string String(string, ...string) string Uint(string, ...uint32) uint32 Float(string, ...float32) float32 Bool(string, ...bool) bool Strings(string, ...[]string) []string Ints(string, ...[]int32) []int32 Floats(string, ...[]float32) []float32 Bools(string, ...[]bool) []bool }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/fs/util/bufioutil/buffer_seeker.go
fs/util/bufioutil/buffer_seeker.go
package bufioutil import ( "bufio" "io" ) type BufferedSeeker struct { rs io.ReadSeeker br *bufio.Reader } func NewBufferedSeeker(rs io.ReadSeeker, size int) *BufferedSeeker { return &BufferedSeeker{ rs: rs, br: bufio.NewReaderSize(rs, size), } } func (b *BufferedSeeker) Read(p []byte) (int, error) { return b.br.Read(p) } func (b *BufferedSeeker) Seek(offset int64, whence int) (int64, error) { if whence == io.SeekCurrent { offset -= int64(b.br.Buffered()) } n, err := b.rs.Seek(offset, whence) if err != nil { return 0, err } b.br.Reset(b.rs) return n, nil }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/fs/util/bufioutil/buffer_seeker_test.go
fs/util/bufioutil/buffer_seeker_test.go
package bufioutil import ( "bytes" "io" "strings" "testing" ) func TestBufferedSeeker(t *testing.T) { const alphabet = "abcdefghijklmnopqrstuvwxyz" bs := NewBufferedSeeker(strings.NewReader(alphabet), 0) // minReadBufferSize = 16 checkRead := func(buf []byte, expected string) { t.Helper() _, err := bs.Read(buf) if err != nil { t.Fatal(err) } if !bytes.Equal(buf, []byte(expected)) { t.Fatalf("expected %s, got %s", expected, buf) } } // Read the first 5 bytes buf := make([]byte, 5) checkRead(buf, "abcde") // Seek back to the beginning _, err := bs.Seek(0, io.SeekStart) if err != nil { t.Fatal(err) } // read 'a' checkRead(buf[:1], "a") if bs.br.Buffered() == 0 { t.Fatalf("totally unexpected sanity check failed") } // Seek past 'b' _, err = bs.Seek(1, io.SeekCurrent) if err != nil { t.Fatal(err) } checkRead(buf, "cdefg") // Seek back to the beginning _, err = bs.Seek(0, io.SeekStart) if err != nil { t.Fatal(err) } checkRead(buf, "abcde") // Seek to the end _, err = bs.Seek(-5, io.SeekEnd) if err != nil { t.Fatal(err) } checkRead(buf, "vwxyz") }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/fs/ggml/gguf.go
fs/ggml/gguf.go
package ggml import ( "bytes" "cmp" "encoding/binary" "encoding/json" "fmt" "io" "log/slog" "maps" "os" "runtime" "slices" "strings" "golang.org/x/sync/errgroup" ) type containerGGUF struct { ByteOrder binary.ByteOrder Version uint32 V1 struct { NumTensor uint32 NumKV uint32 } V2 struct { NumTensor uint64 NumKV uint64 } V3 struct { NumTensor uint64 NumKV uint64 } maxArraySize int } func (c *containerGGUF) Name() string { return "gguf" } func (c *containerGGUF) Decode(rs io.ReadSeeker) (model, error) { if err := binary.Read(rs, c.ByteOrder, &c.Version); err != nil { return nil, err } var err error switch c.Version { case 1: err = binary.Read(rs, c.ByteOrder, &c.V1) case 2: err = binary.Read(rs, c.ByteOrder, &c.V2) default: err = binary.Read(rs, c.ByteOrder, &c.V3) } if err != nil { return nil, err } model := newGGUF(c) if err := model.Decode(rs); err != nil { return nil, err } return model, nil } const ( ggufTypeUint8 uint32 = iota ggufTypeInt8 ggufTypeUint16 ggufTypeInt16 ggufTypeUint32 ggufTypeInt32 ggufTypeFloat32 ggufTypeBool ggufTypeString ggufTypeArray ggufTypeUint64 ggufTypeInt64 ggufTypeFloat64 ) type gguf struct { *containerGGUF kv KV tensors []*Tensor parameters uint64 tensorOffset uint64 scratch [16 << 10]byte } func newGGUF(container *containerGGUF) *gguf { return &gguf{ containerGGUF: container, kv: make(KV), } } func (llm *gguf) KV() KV { return llm.kv } func (llm *gguf) Tensors() Tensors { return Tensors{ items: llm.tensors, Offset: llm.tensorOffset, } } func (llm *gguf) numTensor() uint64 { switch llm.Version { case 1: return uint64(llm.V1.NumTensor) case 2: return llm.V2.NumTensor default: return llm.V3.NumTensor } } func (llm *gguf) numKV() uint64 { switch llm.Version { case 1: return uint64(llm.V1.NumKV) case 2: return llm.V2.NumKV default: return llm.V3.NumKV } } func (llm *gguf) Decode(rs io.ReadSeeker) error { // decode key-values for i := 0; uint64(i) < llm.numKV(); i++ { k, err := readGGUFString(llm, rs) if err != nil { return err } t, err := readGGUF[uint32](llm, rs) if err != nil { return err } var v any switch t { case ggufTypeUint8: v, err = readGGUF[uint8](llm, rs) case ggufTypeInt8: v, err = readGGUF[int8](llm, rs) case ggufTypeUint16: v, err = readGGUF[uint16](llm, rs) case ggufTypeInt16: v, err = readGGUF[int16](llm, rs) case ggufTypeUint32: v, err = readGGUF[uint32](llm, rs) case ggufTypeInt32: v, err = readGGUF[int32](llm, rs) case ggufTypeUint64: v, err = readGGUF[uint64](llm, rs) case ggufTypeInt64: v, err = readGGUF[int64](llm, rs) case ggufTypeFloat32: v, err = readGGUF[float32](llm, rs) case ggufTypeFloat64: v, err = readGGUF[float64](llm, rs) case ggufTypeBool: v, err = readGGUF[bool](llm, rs) case ggufTypeString: v, err = readGGUFString(llm, rs) case ggufTypeArray: v, err = readGGUFArray(llm, rs) default: return fmt.Errorf("invalid type: %d", t) } if err != nil { return err } llm.kv[k] = v } // decode tensors for range llm.numTensor() { name, err := readGGUFString(llm, rs) if err != nil { return fmt.Errorf("failed to read tensor name: %w", err) } // dims is the number of dimensions in the tensor dims, err := readGGUF[uint32](llm, rs) if err != nil { return fmt.Errorf("failed to read tensor dimensions: %w", err) } shape := make([]uint64, dims) for i := 0; uint32(i) < dims; i++ { shape[i], err = readGGUF[uint64](llm, rs) if err != nil { return fmt.Errorf("failed to read tensor shape: %w", err) } } kind, err := readGGUF[uint32](llm, rs) if err != nil { return fmt.Errorf("failed to read tensor kind: %w", err) } offset, err := readGGUF[uint64](llm, rs) if err != nil { return fmt.Errorf("failed to read tensor offset: %w", err) } tensor := Tensor{ Name: name, Kind: kind, Offset: offset, Shape: shape[:], } llm.tensors = append(llm.tensors, &tensor) llm.parameters += tensor.Elements() } // patch KV with parameter count llm.kv["general.parameter_count"] = llm.parameters alignment := llm.kv.Uint("general.alignment", 32) offset, err := rs.Seek(0, io.SeekCurrent) if err != nil { return err } padding := ggufPadding(offset, int64(alignment)) llm.tensorOffset = uint64(offset + padding) for _, tensor := range llm.tensors { offset, err := rs.Seek(0, io.SeekCurrent) if err != nil { return fmt.Errorf("failed to get current offset: %w", err) } padding := ggufPadding(offset, int64(alignment)) if _, err := rs.Seek(padding, io.SeekCurrent); err != nil { return fmt.Errorf("failed to seek to init padding: %w", err) } if _, err := rs.Seek(int64(tensor.Size()), io.SeekCurrent); err != nil { return fmt.Errorf("failed to seek to tensor: %w", err) } } return nil } func readGGUF[T any](llm *gguf, r io.Reader) (T, error) { var t T err := binary.Read(r, llm.ByteOrder, &t) return t, err } func writeGGUF[V any](w io.Writer, t uint32, v V) error { if err := binary.Write(w, binary.LittleEndian, t); err != nil { return err } return binary.Write(w, binary.LittleEndian, v) } func readGGUFV1String(llm *gguf, r io.Reader) (string, error) { var length uint64 if err := binary.Read(r, llm.ByteOrder, &length); err != nil { return "", err } var b bytes.Buffer if _, err := io.CopyN(&b, r, int64(length)); err != nil { return "", err } // gguf v1 strings are null-terminated b.Truncate(b.Len() - 1) return b.String(), nil } func readGGUFV1StringsData(llm *gguf, r io.Reader, a *array[string]) (any, error) { for i := range a.size { if a.values != nil { e, err := readGGUFV1String(llm, r) if err != nil { return nil, err } a.values[i] = e } else { _ = discardGGUFString(llm, r) } } return a, nil } func discardGGUFString(llm *gguf, r io.Reader) error { buf := llm.scratch[:8] _, err := io.ReadFull(r, buf) if err != nil { return err } size := int(llm.ByteOrder.Uint64(buf)) for size > 0 { n, err := r.Read(llm.scratch[:min(size, cap(llm.scratch))]) if err != nil { return err } size -= n } return nil } func readGGUFString(llm *gguf, r io.Reader) (string, error) { if llm.Version == 1 { return readGGUFV1String(llm, r) } buf := llm.scratch[:8] _, err := io.ReadFull(r, buf) if err != nil { return "", err } length := int(llm.ByteOrder.Uint64(buf)) if length > len(llm.scratch) { buf = make([]byte, length) } else { buf = llm.scratch[:length] } clear(buf) _, err = io.ReadFull(r, buf) if err != nil { return "", err } return string(buf), nil } func writeGGUFString(w io.Writer, s string) error { if err := binary.Write(w, binary.LittleEndian, ggufTypeString); err != nil { return err } if err := binary.Write(w, binary.LittleEndian, uint64(len(s))); err != nil { return err } _, err := io.Copy(w, strings.NewReader(s)) return err } func readGGUFStringsData(llm *gguf, r io.Reader, a *array[string]) (any, error) { for i := range a.size { if a.values != nil { e, err := readGGUFString(llm, r) if err != nil { return nil, err } a.values[i] = e } else { discardGGUFString(llm, r) } } return a, nil } type array[T any] struct { // size is the actual size of the array size int // values is the array of values. this is nil if the array is larger than configured maxSize values []T } func (a *array[T]) MarshalJSON() ([]byte, error) { return json.Marshal(a.values) } func newArray[T any](size, maxSize int) *array[T] { a := array[T]{size: size} if maxSize < 0 || size <= maxSize { a.values = make([]T, size) } return &a } func readGGUFArray(llm *gguf, r io.Reader) (any, error) { t, err := readGGUF[uint32](llm, r) if err != nil { return nil, err } n, err := readGGUF[uint64](llm, r) if err != nil { return nil, err } switch t { case ggufTypeUint8: a := newArray[uint8](int(n), llm.maxArraySize) return readGGUFArrayData(llm, r, a) case ggufTypeInt8: a := newArray[int8](int(n), llm.maxArraySize) return readGGUFArrayData(llm, r, a) case ggufTypeUint16: a := newArray[uint16](int(n), llm.maxArraySize) return readGGUFArrayData(llm, r, a) case ggufTypeInt16: a := newArray[int16](int(n), llm.maxArraySize) return readGGUFArrayData(llm, r, a) case ggufTypeUint32: a := newArray[uint32](int(n), llm.maxArraySize) return readGGUFArrayData(llm, r, a) case ggufTypeInt32: a := newArray[int32](int(n), llm.maxArraySize) return readGGUFArrayData(llm, r, a) case ggufTypeUint64: a := newArray[uint64](int(n), llm.maxArraySize) return readGGUFArrayData(llm, r, a) case ggufTypeInt64: a := newArray[int64](int(n), llm.maxArraySize) return readGGUFArrayData(llm, r, a) case ggufTypeFloat32: a := newArray[float32](int(n), llm.maxArraySize) return readGGUFArrayData(llm, r, a) case ggufTypeFloat64: a := newArray[float64](int(n), llm.maxArraySize) return readGGUFArrayData(llm, r, a) case ggufTypeBool: a := newArray[bool](int(n), llm.maxArraySize) return readGGUFArrayData(llm, r, a) case ggufTypeString: a := newArray[string](int(n), llm.maxArraySize) if llm.Version == 1 { return readGGUFV1StringsData(llm, r, a) } return readGGUFStringsData(llm, r, a) default: return nil, fmt.Errorf("invalid array type: %d", t) } } func readGGUFArrayData[T any](llm *gguf, r io.Reader, a *array[T]) (any, error) { for i := range a.size { e, err := readGGUF[T](llm, r) if err != nil { return nil, err } if a.values != nil { a.values[i] = e } } return a, nil } // writeGGUFArray writes a slice s of type E to the write with a gguf type of t func writeGGUFArray[S ~[]E, E any](w io.Writer, t uint32, s S) error { if err := binary.Write(w, binary.LittleEndian, ggufTypeArray); err != nil { return err } if err := binary.Write(w, binary.LittleEndian, t); err != nil { return err } if err := binary.Write(w, binary.LittleEndian, uint64(len(s))); err != nil { return err } if t == ggufTypeString { for _, e := range any(s).([]string) { if err := binary.Write(w, binary.LittleEndian, uint64(len(e))); err != nil { return err } if err := binary.Write(w, binary.LittleEndian, []byte(e)); err != nil { return err } } return nil } return binary.Write(w, binary.LittleEndian, s) } func WriteGGUF(f *os.File, kv KV, ts []*Tensor) error { arch := kv.String("general.architecture") if arch == "" { return fmt.Errorf("architecture not set") } if err := binary.Write(f, binary.LittleEndian, []byte("GGUF")); err != nil { return err } if err := binary.Write(f, binary.LittleEndian, uint32(3)); err != nil { return err } if err := binary.Write(f, binary.LittleEndian, uint64(len(ts))); err != nil { return err } if err := binary.Write(f, binary.LittleEndian, uint64(len(kv))); err != nil { return err } for _, key := range slices.Sorted(maps.Keys(kv)) { if err := ggufWriteKV(f, arch, key, kv[key]); err != nil { return err } } slices.SortStableFunc( ts, func(a, b *Tensor) int { return cmp.Or( cmp.Compare(a.block(), b.block()), cmp.Compare(a.Name, b.Name), ) }, ) alignment := kv.Uint("general.alignment", 32) var s uint64 for i := range ts { ts[i].Offset = s if err := ggufWriteTensorInfo(f, ts[i]); err != nil { return err } s += ts[i].Size() s += uint64(ggufPadding(int64(s), int64(alignment))) } offset, err := f.Seek(0, io.SeekCurrent) if err != nil { return err } offset += ggufPadding(offset, int64(alignment)) var g errgroup.Group g.SetLimit(runtime.GOMAXPROCS(0)) // TODO consider reducing if tensors size * gomaxprocs is larger than free memory for _, t := range ts { w := io.NewOffsetWriter(f, offset+int64(t.Offset)) g.Go(func() error { _, err := t.WriteTo(w) return err }) } return g.Wait() } func ggufWriteKV(ws io.WriteSeeker, arch, k string, v any) error { if !strings.HasPrefix(k, arch+".") && !strings.HasPrefix(k, "general.") && !strings.HasPrefix(k, "adapter.") && !strings.HasPrefix(k, "tokenizer.") { k = arch + "." + k } slog.Debug(k, "type", fmt.Sprintf("%T", v)) if err := binary.Write(ws, binary.LittleEndian, uint64(len(k))); err != nil { return err } if err := binary.Write(ws, binary.LittleEndian, []byte(k)); err != nil { return err } var err error switch v := v.(type) { case int32: err = writeGGUF(ws, ggufTypeInt32, v) case int64: err = writeGGUF(ws, ggufTypeInt64, v) case uint32, FileType: err = writeGGUF(ws, ggufTypeUint32, v) case uint64: err = writeGGUF(ws, ggufTypeUint64, v) case float32: err = writeGGUF(ws, ggufTypeFloat32, v) case bool: err = writeGGUF(ws, ggufTypeBool, v) case string: err = writeGGUFString(ws, v) case []int32: err = writeGGUFArray(ws, ggufTypeInt32, v) case *array[int32]: err = writeGGUFArray(ws, ggufTypeInt32, v.values) case []int64: err = writeGGUFArray(ws, ggufTypeInt64, v) case *array[int64]: err = writeGGUFArray(ws, ggufTypeInt64, v.values) case []uint32: err = writeGGUFArray(ws, ggufTypeUint32, v) case *array[uint32]: err = writeGGUFArray(ws, ggufTypeUint32, v.values) case []float32: err = writeGGUFArray(ws, ggufTypeFloat32, v) case *array[float32]: err = writeGGUFArray(ws, ggufTypeFloat32, v.values) case []string: err = writeGGUFArray(ws, ggufTypeString, v) case *array[string]: err = writeGGUFArray(ws, ggufTypeString, v.values) case []bool: err = writeGGUFArray(ws, ggufTypeBool, v) case *array[bool]: err = writeGGUFArray(ws, ggufTypeBool, v.values) default: return fmt.Errorf("improper type for '%s'", k) } return err } func ggufWriteTensorInfo(ws io.WriteSeeker, t *Tensor) error { slog.Debug(t.Name, "kind", t.Kind, "shape", t.Shape, "offset", t.Offset) if err := binary.Write(ws, binary.LittleEndian, uint64(len(t.Name))); err != nil { return err } if err := binary.Write(ws, binary.LittleEndian, []byte(t.Name)); err != nil { return err } if err := binary.Write(ws, binary.LittleEndian, uint32(len(t.Shape))); err != nil { return err } for _, n := range t.Shape { if err := binary.Write(ws, binary.LittleEndian, n); err != nil { return err } } if err := binary.Write(ws, binary.LittleEndian, t.Kind); err != nil { return err } return binary.Write(ws, binary.LittleEndian, t.Offset) } func ggufPadding(offset, align int64) int64 { return (align - offset%align) % align }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/fs/ggml/ggml_test.go
fs/ggml/ggml_test.go
package ggml import ( "maps" "math" "slices" "strconv" "strings" "testing" "github.com/google/go-cmp/cmp" ) func TestTensorLayers(t *testing.T) { tensors := make(map[string]*Tensor) for _, name := range []string{ "token_embd.weight", "blk.0.attn_k.weight", "blk.0.attn_output.weight", "blk.0.attn_q.weight", "blk.0.attn_v.weight", "blk.0.attn_norm.weight", "blk.0.ffn_down.weight", "blk.0.ffn_gate.weight", "blk.0.ffn_up.weight", "blk.0.ffn_norm.weight", "output_norm.weight", "mm.0.bias", "mm.0.weight", "v.blk.0.attn_k.weight", "v.blk.0.attn_output.weight", "v.blk.0.attn_q.weight", "v.blk.0.attn_v.weight", "v.blk.0.attn_norm.weight", "v.blk.0.ffn_down.weight", "v.blk.0.ffn_gate.weight", "v.blk.0.ffn_up.weight", "v.blk.0.ffn_norm.weight", "v.patch_embd.weight", "v.position_embd.gate", "v.position_embd.weight", } { tensors[name] = &Tensor{Name: name} } cases := []struct { name string items []*Tensor want map[string]Layer }{ { name: "text", items: slices.Collect(func(yield func(*Tensor) bool) { for k, v := range tensors { if !strings.HasPrefix(k, "mm.") && !strings.HasPrefix(k, "v.") { if !yield(v) { return } } } }), want: map[string]Layer{ "blk.0": { "attn_k.weight": tensors["blk.0.attn_k.weight"], "attn_q.weight": tensors["blk.0.attn_q.weight"], "attn_v.weight": tensors["blk.0.attn_v.weight"], "attn_output.weight": tensors["blk.0.attn_output.weight"], "attn_norm.weight": tensors["blk.0.attn_norm.weight"], "ffn_down.weight": tensors["blk.0.ffn_down.weight"], "ffn_gate.weight": tensors["blk.0.ffn_gate.weight"], "ffn_up.weight": tensors["blk.0.ffn_up.weight"], "ffn_norm.weight": tensors["blk.0.ffn_norm.weight"], }, "token_embd": {"weight": tensors["token_embd.weight"]}, "output_norm": {"weight": tensors["output_norm.weight"]}, }, }, { name: "vision", items: slices.Collect(func(yield func(*Tensor) bool) { for k, v := range tensors { if strings.HasPrefix(k, "mm.") || strings.HasPrefix(k, "v.") { if !yield(v) { return } } } }), want: map[string]Layer{ "mm.0": { "bias": tensors["mm.0.bias"], "weight": tensors["mm.0.weight"], }, "v.blk.0": { "attn_k.weight": tensors["v.blk.0.attn_k.weight"], "attn_q.weight": tensors["v.blk.0.attn_q.weight"], "attn_v.weight": tensors["v.blk.0.attn_v.weight"], "attn_output.weight": tensors["v.blk.0.attn_output.weight"], "attn_norm.weight": tensors["v.blk.0.attn_norm.weight"], "ffn_down.weight": tensors["v.blk.0.ffn_down.weight"], "ffn_gate.weight": tensors["v.blk.0.ffn_gate.weight"], "ffn_up.weight": tensors["v.blk.0.ffn_up.weight"], "ffn_norm.weight": tensors["v.blk.0.ffn_norm.weight"], }, "v": { "patch_embd.weight": tensors["v.patch_embd.weight"], "position_embd.gate": tensors["v.position_embd.gate"], "position_embd.weight": tensors["v.position_embd.weight"], }, }, }, { name: "vision and text", items: slices.Collect(maps.Values(tensors)), want: map[string]Layer{ "blk.0": { "attn_k.weight": tensors["blk.0.attn_k.weight"], "attn_q.weight": tensors["blk.0.attn_q.weight"], "attn_v.weight": tensors["blk.0.attn_v.weight"], "attn_output.weight": tensors["blk.0.attn_output.weight"], "attn_norm.weight": tensors["blk.0.attn_norm.weight"], "ffn_down.weight": tensors["blk.0.ffn_down.weight"], "ffn_gate.weight": tensors["blk.0.ffn_gate.weight"], "ffn_up.weight": tensors["blk.0.ffn_up.weight"], "ffn_norm.weight": tensors["blk.0.ffn_norm.weight"], }, "token_embd": {"weight": tensors["token_embd.weight"]}, "output_norm": {"weight": tensors["output_norm.weight"]}, "mm.0": { "bias": tensors["mm.0.bias"], "weight": tensors["mm.0.weight"], }, "v.blk.0": { "attn_k.weight": tensors["v.blk.0.attn_k.weight"], "attn_q.weight": tensors["v.blk.0.attn_q.weight"], "attn_v.weight": tensors["v.blk.0.attn_v.weight"], "attn_output.weight": tensors["v.blk.0.attn_output.weight"], "attn_norm.weight": tensors["v.blk.0.attn_norm.weight"], "ffn_down.weight": tensors["v.blk.0.ffn_down.weight"], "ffn_gate.weight": tensors["v.blk.0.ffn_gate.weight"], "ffn_up.weight": tensors["v.blk.0.ffn_up.weight"], "ffn_norm.weight": tensors["v.blk.0.ffn_norm.weight"], }, "v": { "patch_embd.weight": tensors["v.patch_embd.weight"], "position_embd.gate": tensors["v.position_embd.gate"], "position_embd.weight": tensors["v.position_embd.weight"], }, }, }, } for _, tt := range cases { t.Run(tt.name, func(t *testing.T) { got := Tensors{items: tt.items}.GroupLayers() if diff := cmp.Diff(got, tt.want); diff != "" { t.Errorf("unexpected layers (-got +want):\n%s", diff) } }) } } // ref: https://github.com/ggml-org/llama.cpp/blob/a82c9e7c23ef6db48cebfa194dc9cebbc4ac3552/ggml/src/ggml.c#L572 func TestTensorTypes(t *testing.T) { cases := []struct { kind uint32 blockSize uint64 typeSize uint64 }{ {0, 1, 4}, {1, 1, 2}, {2, 32, 18}, {3, 32, 20}, {6, 32, 22}, {7, 32, 24}, {8, 32, 34}, {9, 32, 36}, {10, 256, 84}, {11, 256, 110}, {12, 256, 144}, {13, 256, 176}, {14, 256, 210}, {15, 256, 292}, {16, 256, 66}, {17, 256, 74}, {18, 256, 98}, {19, 256, 50}, {20, 32, 18}, {21, 256, 110}, {22, 256, 82}, {23, 256, 136}, {24, 1, 1}, {25, 1, 2}, {26, 1, 4}, {27, 1, 8}, {28, 1, 8}, {29, 256, 56}, {30, 1, 2}, } for _, tt := range cases { t.Run(strconv.Itoa(int(tt.kind)), func(t *testing.T) { tensor := Tensor{Kind: tt.kind} if tensor.blockSize() != tt.blockSize { t.Errorf("unexpected block size: got=%d want=%d", tensor.blockSize(), tt.blockSize) } if tensor.typeSize() != tt.typeSize { t.Errorf("unexpected type size: got=%d want=%d", tensor.typeSize(), tt.typeSize) } }) } } func TestKeyValue(t *testing.T) { kv := KV{ "general.architecture": "test", "test.strings": &array[string]{size: 3, values: []string{"a", "b", "c"}}, "test.float32s": &array[float32]{size: 3, values: []float32{1.0, 2.0, 3.0}}, "test.int32s": &array[int32]{size: 3, values: []int32{1, 2, 3}}, "test.uint32s": &array[uint32]{size: 3, values: []uint32{1, 2, 3}}, } if diff := cmp.Diff(kv.Strings("strings"), []string{"a", "b", "c"}); diff != "" { t.Errorf("unexpected strings (-got +want):\n%s", diff) } if diff := cmp.Diff(kv.Strings("nonexistent.strings"), []string(nil)); diff != "" { t.Errorf("unexpected strings (-got +want):\n%s", diff) } if diff := cmp.Diff(kv.Strings("default.strings", []string{"ollama"}), []string{"ollama"}); diff != "" { t.Errorf("unexpected strings (-got +want):\n%s", diff) } if diff := cmp.Diff(kv.Floats("float32s"), []float32{1.0, 2.0, 3.0}); diff != "" { t.Errorf("unexpected float32s (-got +want):\n%s", diff) } if diff := cmp.Diff(kv.Floats("nonexistent.float32s"), []float32(nil)); diff != "" { t.Errorf("unexpected float32s (-got +want):\n%s", diff) } if diff := cmp.Diff(kv.Floats("default.float32s", []float32{math.MaxFloat32}), []float32{math.MaxFloat32}); diff != "" { t.Errorf("unexpected float32s (-got +want):\n%s", diff) } if diff := cmp.Diff(kv.Ints("int32s"), []int32{1, 2, 3}); diff != "" { t.Errorf("unexpected int8s (-got +want):\n%s", diff) } if diff := cmp.Diff(kv.Ints("nonexistent.int32s"), []int32(nil)); diff != "" { t.Errorf("unexpected int8s (-got +want):\n%s", diff) } if diff := cmp.Diff(kv.Ints("default.int32s", []int32{math.MaxInt32}), []int32{math.MaxInt32}); diff != "" { t.Errorf("unexpected int8s (-got +want):\n%s", diff) } if diff := cmp.Diff(kv.Uints("uint32s"), []uint32{1, 2, 3}); diff != "" { t.Errorf("unexpected uint8s (-got +want):\n%s", diff) } if diff := cmp.Diff(kv.Uints("nonexistent.uint32s"), []uint32(nil)); diff != "" { t.Errorf("unexpected uint8s (-got +want):\n%s", diff) } if diff := cmp.Diff(kv.Uints("default.uint32s", []uint32{math.MaxUint32}), []uint32{math.MaxUint32}); diff != "" { t.Errorf("unexpected uint8s (-got +want):\n%s", diff) } } func TestHeadCount(t *testing.T) { valuesArray := []int32{1, 5, 3, 4} cases := []struct { kv KV want uint64 }{ { kv: KV{ "general.architecture": "abc", "abc.attention.head_count": &array[int32]{values: valuesArray, size: len(valuesArray)}, }, want: uint64(5), }, { kv: KV{ "general.architecture": "abc", "abc.attention.head_count": uint32(3), }, want: uint64(3), }, } for _, tt := range cases { got := tt.kv.HeadCountMax() if got != tt.want { t.Errorf("unexpected max value: got=%d want=%d", got, tt.want) } } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/fs/ggml/gguf_test.go
fs/ggml/gguf_test.go
package ggml import ( "bytes" "math/rand/v2" "os" "strings" "testing" "github.com/google/go-cmp/cmp" ) func TestWriteGGUF(t *testing.T) { b := bytes.NewBuffer(make([]byte, 2*3)) for range 8 { t.Run("shuffle", func(t *testing.T) { t.Parallel() ts := []*Tensor{ {Name: "token_embd.weight", Shape: []uint64{2, 3}, WriterTo: b}, {Name: "blk.0.ffn_norm.weight", Shape: []uint64{2, 3}, WriterTo: b}, {Name: "blk.0.attn_norm.weight", Shape: []uint64{2, 3}, WriterTo: b}, {Name: "blk.1.ffn_up.weight", Shape: []uint64{2, 3}, WriterTo: b}, {Name: "blk.2.ffn_norm.weight", Shape: []uint64{2, 3}, WriterTo: b}, {Name: "blk.1.ffn_down.weight", Shape: []uint64{2, 3}, WriterTo: b}, {Name: "blk.0.attn_k.weight", Shape: []uint64{2, 3}, WriterTo: b}, {Name: "output_norm.weight", Shape: []uint64{3, 2}, WriterTo: b}, {Name: "output.weight", Shape: []uint64{3, 2}, WriterTo: b}, } rand.Shuffle(len(ts), func(i, j int) { ts[i], ts[j] = ts[j], ts[i] }) w, err := os.CreateTemp(t.TempDir(), strings.ReplaceAll(t.Name(), "/", "_")+"*.bin") if err != nil { t.Fatal(err) } defer w.Close() if err := WriteGGUF(w, KV{ "general.architecture": "test", "general.alignment": uint32(16), "test.key": "value", "test.int32_key": int32(-42), "test.int64_key": int64(-9223372036854775808), "test.int32_array": []int32{-1, 0, 1, 2147483647, -2147483648}, "test.int64_array": []int64{-1, 0, 1, 9223372036854775807, -9223372036854775808}, "attention.key": "value2", "tokenizer.key": "value3", "adapter.key": "value4", }, ts); err != nil { t.Fatal(err) } r, err := os.Open(w.Name()) if err != nil { t.Fatal(err) } defer r.Close() ff, err := Decode(r, -1) if err != nil { t.Fatal(err) } if diff := cmp.Diff(KV{ "general.architecture": "test", "general.alignment": uint32(16), "general.parameter_count": uint64(54), "test.key": "value", "test.int32_key": int32(-42), "test.int64_key": int64(-9223372036854775808), "test.int32_array": &array[int32]{size: 5, values: []int32{-1, 0, 1, 2147483647, -2147483648}}, "test.int64_array": &array[int64]{size: 5, values: []int64{-1, 0, 1, 9223372036854775807, -9223372036854775808}}, "test.attention.key": "value2", "tokenizer.key": "value3", "adapter.key": "value4", }, ff.KV(), cmp.AllowUnexported(array[int32]{}, array[int64]{})); diff != "" { t.Errorf("Mismatch (-want +got):\n%s", diff) } if diff := cmp.Diff(Tensors{ Offset: 992, items: []*Tensor{ {Name: "blk.0.attn_k.weight", Offset: 0, Shape: []uint64{2, 3}}, {Name: "blk.0.attn_norm.weight", Offset: 32, Shape: []uint64{2, 3}}, {Name: "blk.0.ffn_norm.weight", Offset: 64, Shape: []uint64{2, 3}}, {Name: "blk.1.ffn_down.weight", Offset: 96, Shape: []uint64{2, 3}}, {Name: "blk.1.ffn_up.weight", Offset: 128, Shape: []uint64{2, 3}}, {Name: "blk.2.ffn_norm.weight", Offset: 160, Shape: []uint64{2, 3}}, {Name: "output.weight", Offset: 192, Shape: []uint64{3, 2}}, {Name: "output_norm.weight", Offset: 224, Shape: []uint64{3, 2}}, {Name: "token_embd.weight", Offset: 256, Shape: []uint64{2, 3}}, }, }, ff.Tensors(), cmp.AllowUnexported(Tensors{})); diff != "" { t.Errorf("Mismatch (-want +got):\n%s", diff) } }) } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/fs/ggml/type.go
fs/ggml/type.go
package ggml import ( "fmt" "log/slog" "strings" ) // FileType is the Go equivalent to llama_ftype used for gguf file typing type FileType uint32 const ( FileTypeF32 FileType = iota FileTypeF16 fileTypeQ4_0 fileTypeQ4_1 fileTypeMXFP4 // originally fileTypeQ4_1_F16 // unused by GGML fileTypeQ4_2 // unused by GGML fileTypeQ4_3 // unused by GGML FileTypeQ8_0 fileTypeQ5_0 fileTypeQ5_1 fileTypeQ2_K fileTypeQ3_K_S fileTypeQ3_K_M fileTypeQ3_K_L FileTypeQ4_K_S FileTypeQ4_K_M fileTypeQ5_K_S fileTypeQ5_K_M fileTypeQ6_K fileTypeIQ2_XXS fileTypeIQ2_XS fileTypeQ2_K_S fileTypeIQ3_XS fileTypeIQ3_XXS fileTypeIQ1_S fileTypeIQ4_NL fileTypeIQ3_S fileTypeIQ3_M fileTypeIQ2_S fileTypeIQ2_M fileTypeIQ4_XS fileTypeIQ1_M FileTypeBF16 fileTypeQ4_0_4_4 // unused by GGML fileTypeQ4_0_4_8 // unused by GGML fileTypeQ4_0_8_8 // unused by GGML fileTypeTQ1_0 fileTypeTQ2_0 FileTypeUnknown = 1024 ) // ParseFileType parses the provided GGUF file type // Only Ollama supported types are considered valid func ParseFileType(s string) (FileType, error) { switch s { case "F32": return FileTypeF32, nil case "F16": return FileTypeF16, nil case "Q8_0": return FileTypeQ8_0, nil case "Q4_K_S": return FileTypeQ4_K_S, nil case "Q4_K_M", "Q4_K": return FileTypeQ4_K_M, nil case "BF16": return FileTypeBF16, nil default: supportedFileTypes := []FileType{ FileTypeF32, FileTypeF16, FileTypeQ4_K_S, FileTypeQ4_K_M, FileTypeQ8_0, // fsggml.FileTypeBF16, // TODO } strs := make([]string, len(supportedFileTypes)) for i := range supportedFileTypes { strs[i] = supportedFileTypes[i].String() } return FileTypeUnknown, fmt.Errorf("unsupported quantization type %s - supported types are %s", s, strings.Join(strs, ", ")) } } func (t FileType) String() string { // Note: this routine will return a broader set of file types for existing models switch t { case FileTypeF32: return "F32" case FileTypeF16: return "F16" case fileTypeQ4_0: return "Q4_0" case fileTypeQ4_1: return "Q4_1" case fileTypeMXFP4: return "MXFP4" case FileTypeQ8_0: return "Q8_0" case fileTypeQ5_0: return "Q5_0" case fileTypeQ5_1: return "Q5_1" case fileTypeQ2_K: return "Q2_K" case fileTypeQ3_K_S: return "Q3_K_S" case fileTypeQ3_K_M: return "Q3_K_M" case fileTypeQ3_K_L: return "Q3_K_L" case FileTypeQ4_K_S: return "Q4_K_S" case FileTypeQ4_K_M: return "Q4_K_M" case fileTypeQ5_K_S: return "Q5_K_S" case fileTypeQ5_K_M: return "Q5_K_M" case fileTypeQ6_K: return "Q6_K" case fileTypeQ2_K_S: return "Q2_K_S" case FileTypeBF16: return "BF16" default: return "unknown" } } func (t FileType) Value() uint32 { return uint32(t) } func (ftype FileType) ToTensorType() TensorType { switch ftype { case FileTypeF32: return TensorTypeF32 case FileTypeF16: return TensorTypeF16 case fileTypeQ4_0: return TensorTypeQ4_0 case fileTypeQ4_1: return TensorTypeQ4_1 case FileTypeQ8_0: return TensorTypeQ8_0 case fileTypeQ5_0: return TensorTypeQ5_0 case fileTypeQ5_1: return TensorTypeQ5_1 case fileTypeQ2_K: return TensorTypeQ2_K case fileTypeQ3_K_S: return TensorTypeQ3_K case fileTypeQ3_K_M: return TensorTypeQ3_K case fileTypeQ3_K_L: return TensorTypeQ3_K case FileTypeQ4_K_S: return TensorTypeQ4_K case FileTypeQ4_K_M: return TensorTypeQ4_K case fileTypeQ5_K_S: return TensorTypeQ5_K case fileTypeQ5_K_M: return TensorTypeQ5_K case fileTypeQ6_K: return TensorTypeQ6_K case fileTypeQ2_K_S: return TensorTypeQ2_K case FileTypeBF16: return TensorTypeBF16 case fileTypeMXFP4: return TensorTypeMXFP4 default: slog.Warn("unsupported file type", "type", ftype) return 0 // F32 } } // TensorType is equivalent to ggml_type for individual tensor types // Note: these are not the same as FileType type TensorType uint32 const ( TensorTypeF32 TensorType = iota TensorTypeF16 TensorTypeQ4_0 TensorTypeQ4_1 tensorTypeQ4_2 tensorTypeQ4_3 // unused by GGML TensorTypeQ5_0 TensorTypeQ5_1 TensorTypeQ8_0 TensorTypeQ8_1 TensorTypeQ2_K TensorTypeQ3_K TensorTypeQ4_K TensorTypeQ5_K TensorTypeQ6_K TensorTypeQ8_K tensorTypeIQ2_XXS // not supported by ollama tensorTypeIQ2_XS // not supported by ollama tensorTypeIQ3_XXS // not supported by ollama tensorTypeIQ1_S // not supported by ollama tensorTypeIQ4_NL // not supported by ollama tensorTypeIQ3_S // not supported by ollama tensorTypeIQ2_S // not supported by ollama tensorTypeIQ4_XS // not supported by ollama TensorTypeI8 TensorTypeI16 TensorTypeI32 TensorTypeI64 TensorTypeF64 tensorTypeIQ1_M // not supported by ollama TensorTypeBF16 tensorTypeQ4_0_4_4 // unused by GGML tensorTypeQ4_0_4_8 // unused by GGML tensorTypeQ4_0_8_8 // unused by GGML tensorTypeTQ1_0 // not supported by ollama tensorTypeTQ2_0 // not supported by ollama tensorTypeIQ4_NL_4_4 // unused by GGML tensorTypeIQ4_NL_4_8 // unused by GGML tensorTypeIQ4_NL_8_8 // unused by GGML TensorTypeMXFP4 ) // ParseTensorType parses the provided GGUF tensor type // Only Ollama supported types are considered valid func ParseTensorType(s string) (TensorType, error) { switch s { case "F32": return TensorTypeF32, nil case "F16": return TensorTypeF16, nil case "Q4_0": return TensorTypeQ4_0, nil case "Q4_1": return TensorTypeQ4_1, nil case "Q5_0": return TensorTypeQ5_0, nil case "Q5_1": return TensorTypeQ5_1, nil case "Q8_0": return TensorTypeQ8_0, nil case "Q8_1": return TensorTypeQ8_1, nil case "Q2_K": return TensorTypeQ2_K, nil case "Q3_K": return TensorTypeQ3_K, nil case "Q4_K": return TensorTypeQ4_K, nil case "Q5_K": return TensorTypeQ5_K, nil case "Q6_K": return TensorTypeQ6_K, nil case "Q8_K": return TensorTypeQ8_K, nil case "F64": return TensorTypeF64, nil case "BF16": return TensorTypeBF16, nil case "MXFP4": return TensorTypeMXFP4, nil default: return 0, fmt.Errorf("unsupported quantization type %s", s) } } func (t TensorType) IsQuantized() bool { switch t { case TensorTypeF32, TensorTypeF16, TensorTypeBF16: return false default: return true } } func (t TensorType) RowSize(ne uint64) uint64 { return t.TypeSize() * ne / t.BlockSize() } func (t TensorType) String() string { switch t { case TensorTypeF32: return "F32" case TensorTypeF16: return "F16" case TensorTypeQ4_0: return "Q4_0" case TensorTypeQ4_1: return "Q4_1" case TensorTypeQ5_0: return "Q5_0" case TensorTypeQ5_1: return "Q5_1" case TensorTypeQ8_0: return "Q8_0" case TensorTypeQ8_1: return "Q8_1" case TensorTypeQ2_K: return "Q2_K" case TensorTypeQ3_K: return "Q3_K" case TensorTypeQ4_K: return "Q4_K" case TensorTypeQ5_K: return "Q5_K" case TensorTypeQ6_K: return "Q6_K" case TensorTypeQ8_K: return "Q8_K" case TensorTypeF64: return "F64" case TensorTypeBF16: return "BF16" case 4, TensorTypeMXFP4: return "MXFP4" default: return "unknown" } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/fs/ggml/ggml.go
fs/ggml/ggml.go
package ggml import ( "cmp" "encoding/binary" "errors" "fmt" "io" "log/slog" "math" "slices" "strings" "github.com/ollama/ollama/format" "github.com/ollama/ollama/fs/util/bufioutil" "github.com/ollama/ollama/ml" ) type GGML struct { container model Length int64 } type model interface { KV() KV Tensors() Tensors } type KV map[string]any func (kv KV) Architecture() string { return kv.String("general.architecture", "unknown") } func (kv KV) Kind() string { return kv.String("general.type", "unknown") } func (kv KV) ParameterCount() uint64 { val, _ := keyValue(kv, "general.parameter_count", uint64(0)) return val } func (kv KV) FileType() FileType { if t := kv.Uint("general.file_type"); t > 0 { return FileType(t) } return FileTypeUnknown } func (kv KV) BlockCount() uint64 { return uint64(kv.Uint("block_count")) } func (kv KV) EmbeddingLength() uint64 { return uint64(kv.Uint("embedding_length")) } func (kv KV) HeadCount() []uint64 { headCountDefault := uint32(1) headCount := kv.UintOrArrayValueAsArray("attention.head_count", headCountDefault) if len(headCount) == 1 { headCountDefault = headCount[0] } nLayers := int(kv.BlockCount()) if len(headCount) > nLayers { slog.Warn("got more elements of attention.head_count than layers", "len(headCount)", len(headCount), "layers", nLayers) } out := make([]uint64, nLayers) for i := range nLayers { if i >= len(headCount) { out[i] = uint64(headCountDefault) } else { out[i] = uint64(headCount[i]) } } return out } func (kv KV) HeadCountMax() uint64 { return uint64(kv.UintOrMaxArrayValue("attention.head_count", 1)) } func (kv KV) HeadCountMin() uint64 { return uint64(kv.UintOrMinArrayValue("attention.head_count", 1)) } func (kv KV) HeadCountKV() []uint64 { headCountKVDefault := uint32(1) headCountKV := kv.UintOrArrayValueAsArray("attention.head_count_kv", headCountKVDefault) if len(headCountKV) == 1 { headCountKVDefault = headCountKV[0] } nLayers := int(kv.BlockCount()) if len(headCountKV) > nLayers { slog.Warn("got more elements of attention.head_count than layers", "len(headCountKV)", len(headCountKV), "layers", nLayers) } out := make([]uint64, nLayers) for i := range nLayers { if i >= len(headCountKV) { out[i] = uint64(headCountKVDefault) } else { out[i] = uint64(headCountKV[i]) } } return out } func (kv KV) HeadCountKVMax() uint64 { return uint64(kv.UintOrMaxArrayValue("attention.head_count_kv", 1)) } func (kv KV) HeadCountKVMin() uint64 { return uint64(kv.UintOrMinArrayValue("attention.head_count_kv", 1)) } func (kv KV) EmbeddingHeadCountMax() uint64 { if heads := kv.HeadCountMin(); heads > 0 { return kv.EmbeddingLength() / heads } return 0 } func (kv KV) EmbeddingHeadCountK() uint64 { return uint64(kv.Uint("attention.key_length", uint32(kv.EmbeddingHeadCountMax()))) } func (kv KV) EmbeddingHeadCountV() uint64 { return uint64(kv.Uint("attention.value_length", uint32(kv.EmbeddingHeadCountMax()))) } func (kv KV) ContextLength() uint64 { return uint64(kv.Uint("context_length")) } func (kv KV) ChatTemplate() string { return kv.String("tokenizer.chat_template") } // ssm architecture parameters func (kv KV) SSMConvKernel() uint64 { return uint64(kv.Uint("ssm.conv_kernel")) } func (kv KV) SSMInnerSize() uint64 { return uint64(kv.Uint("ssm.inner_size")) } func (kv KV) SSMStateSize() uint64 { return uint64(kv.Uint("ssm.state_size")) } func (kv KV) SSMGroupCount() uint64 { return uint64(kv.Uint("ssm.group_count")) } // general types func (kv KV) String(key string, defaultValue ...string) string { val, _ := keyValue(kv, key, append(defaultValue, "")...) return val } func (kv KV) Uint(key string, defaultValue ...uint32) uint32 { val, _ := keyValue(kv, key, append(defaultValue, 0)...) return val } func (kv KV) Float(key string, defaultValue ...float32) float32 { val, _ := keyValue(kv, key, append(defaultValue, 0)...) return val } func (kv KV) Bool(key string, defaultValue ...bool) bool { val, _ := keyValue(kv, key, append(defaultValue, false)...) return val } func (kv KV) UintOrMaxArrayValue(key string, defaultValue uint32) uint32 { _, max := kv.UintOrArrayValue(key, defaultValue) return max } func (kv KV) UintOrMinArrayValue(key string, defaultValue uint32) uint32 { min, _ := kv.UintOrArrayValue(key, defaultValue) return min } func (kv KV) UintOrArrayValue(key string, defaultValue uint32) (uint32, uint32) { arrVal := kv.UintOrArrayValueAsArray(key, defaultValue) return slices.Min(arrVal), slices.Max(arrVal) } func (kv KV) UintOrArrayValueAsArray(key string, defaultValue uint32) []uint32 { if u32, ok := keyValue(kv, key, uint32(0)); ok { return []uint32{u32} } else if u32s, ok := keyValue(kv, key, &array[uint32]{}); ok { return u32s.values } else if i32s, ok := keyValue(kv, key, &array[int32]{}); ok { dst := make([]uint32, len(i32s.values)) for i, v := range i32s.values { if v < 0 { slog.Warn("array values are unexpectedly negative", "key", key, "i", i, "v", v) } dst[i] = uint32(v) } return dst } return []uint32{defaultValue} } func (kv KV) Strings(key string, defaultValue ...[]string) []string { val, _ := keyValue(kv, key, &array[string]{values: append(defaultValue, []string(nil))[0]}) return val.values } func (kv KV) Ints(key string, defaultValue ...[]int32) []int32 { val, _ := keyValue(kv, key, &array[int32]{values: append(defaultValue, []int32(nil))[0]}) return val.values } func (kv KV) Uints(key string, defaultValue ...[]uint32) []uint32 { val, _ := keyValue(kv, key, &array[uint32]{values: append(defaultValue, []uint32(nil))[0]}) return val.values } func (kv KV) Floats(key string, defaultValue ...[]float32) []float32 { val, _ := keyValue(kv, key, &array[float32]{values: append(defaultValue, []float32(nil))[0]}) return val.values } func (kv KV) Bools(key string, defaultValue ...[]bool) []bool { val, _ := keyValue(kv, key, &array[bool]{values: append(defaultValue, []bool(nil))[0]}) return val.values } func (kv KV) OllamaEngineRequired() bool { return slices.Contains([]string{ "bert", "deepseek2", "deepseekocr", "gemma3", "gemma3n", "gptoss", "gpt-oss", "llama4", "mistral3", "mllama", "nomic-bert", "olmo3", "qwen25vl", "qwen3", "qwen3moe", "qwen3vl", "qwen3vlmoe", }, kv.Architecture()) } type valueTypes interface { uint8 | int8 | uint16 | int16 | uint32 | int32 | uint64 | int64 | string | float32 | float64 | bool } type arrayValueTypes interface { *array[uint8] | *array[int8] | *array[uint16] | *array[int16] | *array[uint32] | *array[int32] | *array[uint64] | *array[int64] | *array[string] | *array[float32] | *array[float64] | *array[bool] } func keyValue[T valueTypes | arrayValueTypes](kv KV, key string, defaultValue ...T) (T, bool) { if !strings.HasPrefix(key, "tokenizer.") && !strings.HasPrefix(key, "general.") { key = kv.Architecture() + "." + key } if val, ok := kv[key].(T); ok { return val, true } slog.Debug("key with type not found", "key", key, "default", defaultValue[0]) return defaultValue[0], false } type Tensors struct { items []*Tensor Offset uint64 } func (s Tensors) Items(prefix ...string) []*Tensor { if len(prefix) == 0 { return s.items } var items []*Tensor for _, t := range s.items { if strings.HasPrefix(t.Name, prefix[0]) { items = append(items, t) } } return items } func (ts Tensors) GroupLayers() map[string]Layer { layers := make(map[string]Layer) for _, t := range ts.items { parts := strings.Split(t.Name, ".") if index := slices.IndexFunc(parts, func(s string) bool { return s == "blk" || s == "mm" }); index != -1 { if len(parts) > index+2 { // blk and mm should have a number after them, join it parts = append( []string{strings.Join(parts[:index+2], ".")}, parts[index+2:]...) } } if _, ok := layers[parts[0]]; !ok { layers[parts[0]] = make(Layer) } layers[parts[0]][strings.Join(parts[1:], ".")] = t } return layers } type Layer map[string]*Tensor func (l Layer) Size() (size uint64) { for _, t := range l { size += t.Size() } return size } type Tensor struct { Name string `json:"name"` Kind uint32 `json:"kind"` Offset uint64 `json:"-"` // Shape is the number of elements in each dimension Shape []uint64 `json:"shape"` io.WriterTo `json:"-"` } func (t Tensor) block() (n int) { if _, err := fmt.Sscanf(t.Name, "blk.%d.", &n); err != nil { return math.MaxInt } return } func (t Tensor) blockSize() uint64 { return TensorType(t.Kind).BlockSize() } func (t TensorType) BlockSize() uint64 { switch t { case TensorTypeF32, TensorTypeF16, TensorTypeI8, TensorTypeI16, TensorTypeI32, TensorTypeI64, TensorTypeF64, TensorTypeBF16: return 1 case TensorTypeQ4_0, TensorTypeQ4_1, TensorTypeQ5_0, TensorTypeQ5_1, TensorTypeQ8_0, TensorTypeQ8_1, tensorTypeIQ4_NL, 4, TensorTypeMXFP4: return 32 default: return 256 } } func (t Tensor) typeSize() uint64 { return TensorType(t.Kind).TypeSize() } func (t TensorType) TypeSize() uint64 { blockSize := t.BlockSize() switch t { case TensorTypeF32: return 4 case TensorTypeF16: return 2 case TensorTypeQ4_0: return 2 + blockSize/2 case TensorTypeQ4_1: return 2 + 2 + blockSize/2 case TensorTypeQ5_0: return 2 + 4 + blockSize/2 case TensorTypeQ5_1: return 2 + 2 + 4 + blockSize/2 case TensorTypeQ8_0: return 2 + blockSize case TensorTypeQ8_1: return 2 + 2 + blockSize case TensorTypeQ2_K: return blockSize/16 + blockSize/4 + 2 + 2 case TensorTypeQ3_K: return blockSize/8 + blockSize/4 + 12 + 2 case TensorTypeQ4_K: return 2 + 2 + 12 + blockSize/2 case TensorTypeQ5_K: return 2 + 2 + 12 + blockSize/8 + blockSize/2 case TensorTypeQ6_K: return blockSize/2 + blockSize/4 + blockSize/16 + 2 case TensorTypeQ8_K: return 4 + blockSize + 2*blockSize/16 case tensorTypeIQ2_XXS: return 2 + 2*blockSize/8 case tensorTypeIQ2_XS: return 2 + 2*blockSize/8 + blockSize/32 case tensorTypeIQ3_XXS: return 2 + blockSize/4 + blockSize/8 case tensorTypeIQ1_S: return 2 + blockSize/8 + blockSize/16 case tensorTypeIQ4_NL: return 2 + blockSize/2 case tensorTypeIQ3_S: return 2 + blockSize/4 + blockSize/8 + blockSize/32 + 4 case tensorTypeIQ2_S: return 2 + blockSize/4 + blockSize/16 case tensorTypeIQ4_XS: return 2 + 2 + blockSize/2 + blockSize/64 case TensorTypeI8: return 1 case TensorTypeI16: return 2 case TensorTypeI32: return 4 case TensorTypeI64: return 8 case TensorTypeF64: return 8 case tensorTypeIQ1_M: return blockSize/8 + blockSize/16 + blockSize/32 case TensorTypeBF16: return 2 case 4, TensorTypeMXFP4: return 1 + blockSize/2 default: return 0 } } func (t Tensor) Elements() uint64 { var count uint64 = 1 for _, n := range t.Shape { count *= n } return count } func (t Tensor) Size() uint64 { return t.Elements() * t.typeSize() / t.blockSize() } func (t Tensor) Type() string { return TensorType(t.Kind).String() } type container interface { Name() string Decode(io.ReadSeeker) (model, error) } const ( // Magic constant for `ggml` files (unversioned). FILE_MAGIC_GGML = 0x67676d6c // Magic constant for `ggml` files (versioned, ggmf). FILE_MAGIC_GGMF = 0x67676d66 // Magic constant for `ggml` files (versioned, ggjt). FILE_MAGIC_GGJT = 0x67676a74 // Magic constant for `ggla` files (LoRA adapter). FILE_MAGIC_GGLA = 0x67676C61 // Magic constant for `gguf` files (versioned, gguf) FILE_MAGIC_GGUF_LE = 0x46554747 FILE_MAGIC_GGUF_BE = 0x47475546 ) var ErrUnsupportedFormat = errors.New("unsupported model format") func DetectContentType(b []byte) string { switch binary.LittleEndian.Uint32(b[:4]) { case FILE_MAGIC_GGML: return "ggml" case FILE_MAGIC_GGMF: return "ggmf" case FILE_MAGIC_GGJT: return "ggjt" case FILE_MAGIC_GGLA: return "ggla" case FILE_MAGIC_GGUF_LE, FILE_MAGIC_GGUF_BE: return "gguf" default: return "" } } // Decode decodes a GGML model from the given reader. // // It collects array values for arrays with a size less than or equal to // maxArraySize. If the maxArraySize is negative, all arrays are collected. func Decode(rs io.ReadSeeker, maxArraySize int) (*GGML, error) { rs = bufioutil.NewBufferedSeeker(rs, 32<<10) var magic uint32 if err := binary.Read(rs, binary.LittleEndian, &magic); err != nil { return nil, err } var c container switch magic { case FILE_MAGIC_GGUF_LE: c = &containerGGUF{ByteOrder: binary.LittleEndian, maxArraySize: maxArraySize} case FILE_MAGIC_GGUF_BE: c = &containerGGUF{ByteOrder: binary.BigEndian, maxArraySize: maxArraySize} default: return nil, errors.New("invalid file magic") } model, err := c.Decode(rs) if err != nil { return nil, err } offset, err := rs.Seek(0, io.SeekCurrent) if err != nil { return nil, err } // final model type return &GGML{ container: c, model: model, Length: offset, }, nil } func (f GGML) GraphSize(context, batch uint64, numParallel int, kvCacheType string, useFlashAttention ml.FlashAttentionType) (kv []uint64, partialOffload, fullOffload uint64) { context *= uint64(numParallel) embedding := f.KV().EmbeddingLength() heads := f.KV().HeadCountMax() headsArr := f.KV().HeadCount() headsKV := f.KV().HeadCountKVMax() headsKVArr := f.KV().HeadCountKV() vocab := uint64(f.KV()["tokenizer.ggml.tokens"].(*array[string]).size) embeddingHeads := f.KV().EmbeddingHeadCountMax() embeddingHeadsK := f.KV().EmbeddingHeadCountK() embeddingHeadsV := f.KV().EmbeddingHeadCountV() layers := f.Tensors().GroupLayers() bytesPerElement := kvCacheBytesPerElement(kvCacheType) // Default for models unless special-cased below. These defaults mirror the // cache usage in llama.cpp under the assumption that models without special // cases below will use the llamarunner and caching will be handled by the // llama.cpp layer. // // This also assumes that a layer without heads or headsKV set is recurrent // which is usually the case. Some models (eg nemotronh) use "blocks" in // place of layers where some are MLP blocks that don't have any cache. // Models like this will need a special case below to be accurately // estimated. var kvTotal uint64 kv = make([]uint64, f.KV().BlockCount()) kvSizeAttn := uint64(0) kvSizeRecurrent := uint64(0) for i := range kv { headsL := headsArr[i] headsKVL := headsKVArr[i] if headsL > 0 && headsKVL > 0 { // full attention layer // NOTE: Assumes uniform values for all attn layers kv[i] = uint64(float64(context*(embeddingHeadsK+embeddingHeadsV)*headsKVL) * bytesPerElement) kvSizeAttn += kv[i] } else { // recurrent layer ssmDConv := f.KV().SSMConvKernel() ssmDState := f.KV().SSMStateSize() ssmDInner := f.KV().SSMInnerSize() ssmNGroups := f.KV().SSMGroupCount() nEmbdR := uint64(0) if ssmDConv > 0 { nEmbdR = (ssmDConv - 1) * (ssmDInner + 2*ssmNGroups*ssmDState) } nEmbdS := ssmDState * ssmDInner // recurrent always uses F32 in llama.cpp backend // https://github.com/ggml-org/llama.cpp/blob/master/src/llama-model.cpp#L18644 bytesPerElementRecurrent := kvCacheBytesPerElement("f32") kv[i] = (nEmbdR + nEmbdS) * uint64(bytesPerElementRecurrent) kvSizeRecurrent += kv[i] } kvTotal += kv[i] } slog.Debug("default cache size estimate", "attention MiB", float32(kvSizeAttn)/(1024.*1024.), "attention bytes", kvSizeAttn, "recurrent MiB", float32(kvSizeRecurrent)/(1024.*1024.), "recurrent bytes", kvSizeRecurrent) switch f.KV().Architecture() { case "llama", "llama4": fullOffload = max( 4*batch*(1+4*embedding+context*(1+heads)), 4*batch*(embedding+vocab), ) partialOffload = 4 * batch * embedding partialOffload += max( 4*batch*(1+embedding+max(context, embedding))+embedding*embedding*9/16+4*context*(batch*heads+embeddingHeads*headsKV), 4*batch*(embedding+vocab)+embedding*vocab*105/128, ) if ffnGateExpsWeight, ok := layers["blk.0"]["ffn_gate_exps.weight"]; ok { // mixtral 8x22b ff := uint64(f.KV().Uint("feed_forward_length")) partialOffload = max( 3*ffnGateExpsWeight.Size()+4*batch*(2*ff+headsKV+embedding+context+embeddingHeads*headsKV), 4*(context*batch*heads+context*embeddingHeads*headsKV+batch*1024+embeddingHeads*headsKV*batch), ) } else if ffnGateWeight, ok := layers["blk.0"]["ffn_gate.0.weight"]; ok { // mixtral 8x7b ffnGateWeight1 := ffnGateWeight.Shape[1] fullOffload = 4 * batch * (2 + 3*embedding + context*(1+heads) + 2*headsKV + ffnGateWeight1) partialOffload = max( 4*batch*(3+embeddingHeads*headsKV+embedding+context*(1+heads)+ffnGateWeight1)+(embedding*embedding+3*embedding*headsKV*ffnGateWeight1)*9/16, 4*batch*(1+2*embedding+context*(1+heads))+embedding*(6*context*headsKV/heads+embedding*9/16), ) } case "mllama": var visionTokens, tiles uint64 = 1601, 4 crossAttentionLayers := f.KV().Ints("attention.cross_attention_layers") for i := range kv { if slices.Contains(crossAttentionLayers, int32(i)) { kv[i] = headsKV * (embeddingHeadsK + embeddingHeadsV) * 4 * // sizeof(float32) visionTokens * tiles } } fullOffload = max( 4*batch*(2+3*embedding+embeddingHeadsK*heads+context*(1+heads)), // vocab graph 4*batch*(embedding+vocab), ) var ropeFreqsCount uint64 if ropeFreqs, ok := f.Tensors().GroupLayers()["rope_freqs"]; ok { if ropeFreqsWeights, ok := ropeFreqs["weights"]; ok { ropeFreqsCount = ropeFreqsWeights.Elements() } } partialOffload = max( 4*(batch* (2*embedding+1+context*(1+heads)+embeddingHeadsK*heads)+ ropeFreqsCount+ embeddingHeadsK*context*headsKV), // vocab graph 4*batch*(embedding+vocab)+embedding*vocab*105/128, ) case "gemma", "gemma2", "gemma3", "gemma3n": fullOffload = max( 4*batch*(embedding+vocab), 4*batch*(2+context+context*heads+2*embedding+2*embeddingHeadsK*heads), ) partialOffload = max( 4*embedding*batch+embedding*vocab*105/128+4*vocab*batch, 4*batch*(2*embedding+1+2*embeddingHeadsK*heads+context+context*heads)+ 4*embeddingHeadsK*context*8+ embedding*embeddingHeadsK*heads*9/16, ) if f.KV().Architecture() == "gemma3n" { fullOffload *= 4 partialOffload *= 4 } // Gemma2 also has sliding window attention but we only have an optimized implementation in the Ollama // engine. Gemma3 always uses the Ollama engine. if f.KV().Architecture() == "gemma3" { const gemma3GlobalCacheCount = 6 slidingWindow := (uint64(numParallel) * uint64(f.KV().Uint("attention.sliding_window"))) + batch for i := range kv { // Every 6th layer is a global layer, which is the full context size that has already been set. The other // layers are the smaller local (sliding) layers. if (i+1)%gemma3GlobalCacheCount != 0 { kv[i] = uint64(float64(slidingWindow*(embeddingHeadsK+embeddingHeadsV)*headsKV) * bytesPerElement) } } } case "command-r": fullOffload = max( 4*batch*(embedding+vocab), 4*batch*(2+4*embedding+context*(1+heads)), ) partialOffload = max( 4*batch*(embedding+vocab)+embedding*vocab*105/128, 4*batch*(1+2*embedding+context*(1+heads))+4*embedding*context+embedding*embedding*9/16, ) case "qwen2": fullOffload = max( 4*batch*(embedding+vocab), 4*batch*(1+2*embedding+context+context*heads), ) partialOffload = max( 4*batch*(embedding+vocab)+embedding*vocab*105/128, 4*(batch*(1+2*embedding+context*(1+heads))+embedding*(1+context)), ) case "phi2": fullOffload = max( 4*batch*(embedding+vocab), 4*batch*(1+4*embedding+context+context*heads), ) partialOffload = max( 4*batch*(2*embedding+vocab)+embedding*vocab*105/128, 4*batch*(2+3*embedding+context+context*heads), ) case "stablelm": fullOffload = 4 * batch * (context*(1+heads) + 3*embedding + 2) partialOffload = max( 4*batch*(vocab+2*embedding), fullOffload, ) case "deepseek2": fullOffload = max( 4*batch*(3*embedding+vocab), 4*batch*(3*embedding+2+context*(1+headsKV)+2*embeddingHeadsK*headsKV), ) partialOffload = max( 4*batch*(3*embedding+vocab)+embedding*vocab*105/128, 4*batch*(2*embedding+1+2*embeddingHeadsK*headsKV+context+context*headsKV)+4*embeddingHeadsK*context*headsKV+embedding*embeddingHeadsK*headsKV*9/16, ) case "chatglm": fullOffload = 4 * batch * (embedding + vocab) partialOffload = 4*batch*(embedding+vocab) + embedding*vocab*105/128 if qkvBias, ok := layers["blk.0"]["attn_qkv.bias"]; ok { fullOffload = max( fullOffload, 4*batch*(2+ 2*embedding+ context+ context*heads+ embeddingHeadsK*heads+ qkvBias.Shape[0]), ) partialOffload = max( partialOffload, 4*batch*(1+ 2*embedding+ embeddingHeadsK*heads+ context+ context*heads)+ 4*embeddingHeadsK*context+ 4*context*embeddingHeadsK+ 4*qkvBias.Shape[0], ) } case "gptoss", "gpt-oss": kv = make([]uint64, f.KV().BlockCount()) for i := range kv { kv[i] = uint64(float64((embeddingHeadsK+embeddingHeadsV)*headsKV) * bytesPerElement) if i%2 == 0 { kv[i] *= (uint64(numParallel)*4096 + batch) } else { kv[i] *= context } } partialOffload = 2 * f.KV().HeadCountMax() / cmp.Or(f.KV().HeadCountKVMin(), 1) * kvTotal / 6 if useFlashAttention == ml.FlashAttentionEnabled { // rough estimate of graph size with flash attention on partialOffload = (4*uint64(numParallel) + context>>10 + 110) * format.MebiByte } } return } // SupportsKVCacheType checks if the requested cache type is supported func (f GGML) SupportsKVCacheType(cacheType string) bool { if cacheType == "" || cacheType == "f16" { return true } return slices.Contains([]string{"q8_0", "q4_0"}, cacheType) } // KVCacheTypeIsQuantized checks if the requested cache type is a quantized type func (f GGML) KVCacheTypeIsQuantized(cacheType string) bool { if cacheType == "" || cacheType == "f16" || cacheType == "f32" || cacheType == "bf16" { return false } return true } // SupportsFlashAttention checks if the model supports flash attention func (f GGML) SupportsFlashAttention() bool { _, isEmbedding := f.KV()[fmt.Sprintf("%s.pooling_type", f.KV().Architecture())] if isEmbedding { return false } if arch := f.KV().Architecture(); slices.Contains([]string{"gemma2"}, arch) { return false } // Check head counts match and are non-zero headCountK := f.KV().EmbeddingHeadCountK() headCountV := f.KV().EmbeddingHeadCountV() return headCountK != 0 && headCountV != 0 && headCountK == headCountV } // FlashAttention checks if the model should enable flash attention func (f GGML) FlashAttention() bool { return slices.Contains([]string{ "bert", "gemma3", "gptoss", "gpt-oss", "mistral3", "olmo3", "qwen3", "qwen3moe", "qwen3vl", "qwen3vlmoe", }, f.KV().String("general.architecture")) } // kvCacheBytesPerElement returns the number of bytes per element for a given KV cache type func kvCacheBytesPerElement(cacheType string) float64 { switch cacheType { case "q8_0": return 1 // 1/2 of fp16 case "q4_0": return 0.5 // 1/4 of fp16 case "f32": return 4 // f32 (default for recurrent) default: return 2 // f16 (default) } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/fs/gguf/lazy.go
fs/gguf/lazy.go
package gguf import ( "encoding/binary" "iter" "log/slog" ) type lazy[T any] struct { count uint64 next func() (T, bool) stop func() values []T // successFunc is called when all values have been successfully read. successFunc func() error } func newLazy[T any](f *File, fn func() (T, error)) (*lazy[T], error) { it := lazy[T]{} if err := binary.Read(f.reader, binary.LittleEndian, &it.count); err != nil { return nil, err } it.values = make([]T, 0) it.next, it.stop = iter.Pull(func(yield func(T) bool) { for i := range it.count { t, err := fn() if err != nil { slog.Error("error reading tensor", "index", i, "error", err) return } it.values = append(it.values, t) if !yield(t) { break } } if it.successFunc != nil { it.successFunc() } }) return &it, nil } func (g *lazy[T]) Values() iter.Seq[T] { return func(yield func(T) bool) { for _, v := range g.All() { if !yield(v) { break } } } } func (g *lazy[T]) All() iter.Seq2[int, T] { return func(yield func(int, T) bool) { for i := range int(g.count) { if i < len(g.values) { if !yield(i, g.values[i]) { break } } else { t, ok := g.next() if !ok { break } if !yield(i, t) { break } } } } } func (g *lazy[T]) rest() (collected bool) { for { _, ok := g.next() collected = collected || ok if !ok { break } } return collected }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/fs/gguf/gguf.go
fs/gguf/gguf.go
package gguf import ( "bytes" "cmp" "encoding/binary" "errors" "fmt" "io" "iter" "os" "slices" "strings" ) const ( typeUint8 uint32 = iota typeInt8 typeUint16 typeInt16 typeUint32 typeInt32 typeFloat32 typeBool typeString typeArray typeUint64 typeInt64 typeFloat64 ) var ErrUnsupported = errors.New("unsupported") type File struct { Magic [4]byte Version uint32 keyValues *lazy[KeyValue] tensors *lazy[TensorInfo] offset int64 file *os.File reader *bufferedReader bts []byte } func Open(path string) (f *File, err error) { f = &File{bts: make([]byte, 4096)} f.file, err = os.Open(path) if err != nil { return nil, err } f.reader = newBufferedReader(f.file, 32<<10) if err := binary.Read(f.reader, binary.LittleEndian, &f.Magic); err != nil { return nil, err } if bytes.Equal(f.Magic[:], []byte("gguf")) { return nil, fmt.Errorf("%w file type %v", ErrUnsupported, f.Magic) } if err := binary.Read(f.reader, binary.LittleEndian, &f.Version); err != nil { return nil, err } if f.Version < 2 { return nil, fmt.Errorf("%w version %v", ErrUnsupported, f.Version) } f.tensors, err = newLazy(f, f.readTensor) if err != nil { return nil, err } f.tensors.successFunc = func() error { offset := f.reader.offset alignment := cmp.Or(f.KeyValue("general.alignment").Int(), 32) f.offset = offset + (alignment-offset%alignment)%alignment return nil } f.keyValues, err = newLazy(f, f.readKeyValue) if err != nil { return nil, err } return f, nil } func (f *File) readTensor() (TensorInfo, error) { name, err := readString(f) if err != nil { return TensorInfo{}, err } dims, err := read[uint32](f) if err != nil { return TensorInfo{}, err } shape := make([]uint64, dims) for i := range dims { shape[i], err = read[uint64](f) if err != nil { return TensorInfo{}, err } } type_, err := read[uint32](f) if err != nil { return TensorInfo{}, err } offset, err := read[uint64](f) if err != nil { return TensorInfo{}, err } return TensorInfo{ Name: name, Offset: offset, Shape: shape, Type: TensorType(type_), }, nil } func (f *File) readKeyValue() (KeyValue, error) { key, err := readString(f) if err != nil { return KeyValue{}, err } t, err := read[uint32](f) if err != nil { return KeyValue{}, err } value, err := func() (any, error) { switch t { case typeUint8: return read[uint8](f) case typeInt8: return read[int8](f) case typeUint16: return read[uint16](f) case typeInt16: return read[int16](f) case typeUint32: return read[uint32](f) case typeInt32: return read[int32](f) case typeUint64: return read[uint64](f) case typeInt64: return read[int64](f) case typeFloat32: return read[float32](f) case typeFloat64: return read[float64](f) case typeBool: return read[bool](f) case typeString: return readString(f) case typeArray: return readArray(f) default: return nil, fmt.Errorf("%w type %d", ErrUnsupported, t) } }() if err != nil { return KeyValue{}, err } return KeyValue{ Key: key, Value: Value{value}, }, nil } func read[T any](f *File) (t T, err error) { err = binary.Read(f.reader, binary.LittleEndian, &t) return t, err } func readString(f *File) (string, error) { n, err := read[uint64](f) if err != nil { return "", err } if int(n) > len(f.bts) { f.bts = make([]byte, n) } bts := f.bts[:n] if _, err := io.ReadFull(f.reader, bts); err != nil { return "", err } defer clear(bts) return string(bts), nil } func readArray(f *File) (any, error) { t, err := read[uint32](f) if err != nil { return nil, err } n, err := read[uint64](f) if err != nil { return nil, err } switch t { case typeUint8: return readArrayData[uint8](f, n) case typeInt8: return readArrayData[int8](f, n) case typeUint16: return readArrayData[uint16](f, n) case typeInt16: return readArrayData[int16](f, n) case typeUint32: return readArrayData[uint32](f, n) case typeInt32: return readArrayData[int32](f, n) case typeUint64: return readArrayData[uint64](f, n) case typeInt64: return readArrayData[int64](f, n) case typeFloat32: return readArrayData[float32](f, n) case typeFloat64: return readArrayData[float64](f, n) case typeBool: return readArrayData[bool](f, n) case typeString: return readArrayString(f, n) default: return nil, fmt.Errorf("%w type %d", ErrUnsupported, t) } } func readArrayData[T any](f *File, n uint64) (s []T, err error) { s = make([]T, n) for i := range n { e, err := read[T](f) if err != nil { return nil, err } s[i] = e } return s, nil } func readArrayString(f *File, n uint64) (s []string, err error) { s = make([]string, n) for i := range n { e, err := readString(f) if err != nil { return nil, err } s[i] = e } return s, nil } func (f *File) Close() error { f.keyValues.stop() f.tensors.stop() return f.file.Close() } func (f *File) KeyValue(key string) KeyValue { if !strings.HasPrefix(key, "general.") && !strings.HasPrefix(key, "tokenizer.") { key = f.KeyValue("general.architecture").String() + "." + key } if index := slices.IndexFunc(f.keyValues.values, func(kv KeyValue) bool { return kv.Key == key }); index >= 0 { return f.keyValues.values[index] } for keyValue, ok := f.keyValues.next(); ok; keyValue, ok = f.keyValues.next() { if keyValue.Key == key { return keyValue } } return KeyValue{} } func (f *File) NumKeyValues() int { return int(f.keyValues.count) } func (f *File) KeyValues() iter.Seq2[int, KeyValue] { return f.keyValues.All() } func (f *File) TensorInfo(name string) TensorInfo { if index := slices.IndexFunc(f.tensors.values, func(t TensorInfo) bool { return t.Name == name }); index >= 0 { return f.tensors.values[index] } // fast-forward through key values if we haven't already _ = f.keyValues.rest() for tensor, ok := f.tensors.next(); ok; tensor, ok = f.tensors.next() { if tensor.Name == name { return tensor } } return TensorInfo{} } func (f *File) NumTensors() int { return int(f.tensors.count) } func (f *File) TensorInfos() iter.Seq2[int, TensorInfo] { // fast forward through key values if we haven't already f.keyValues.rest() return f.tensors.All() } func (f *File) TensorReader(name string) (TensorInfo, io.Reader, error) { t := f.TensorInfo(name) if t.NumBytes() == 0 { return TensorInfo{}, nil, fmt.Errorf("tensor %s not found", name) } // fast forward through tensor info if we haven't already _ = f.tensors.rest() return t, io.NewSectionReader(f.file, f.offset+int64(t.Offset), t.NumBytes()), nil }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/fs/gguf/tensor.go
fs/gguf/tensor.go
package gguf import ( "log/slog" "strings" ) type TensorInfo struct { Name string Offset uint64 Shape []uint64 Type TensorType } func (ti TensorInfo) Valid() bool { return ti.Name != "" && ti.NumBytes() > 0 } func (ti TensorInfo) NumValues() int64 { var numItems int64 = 1 for _, dim := range ti.Shape { numItems *= int64(dim) } return numItems } // NumBytes returns the number of bytes in the tensor. func (ti TensorInfo) NumBytes() int64 { return int64(float64(ti.NumValues()) * ti.Type.NumBytes()) } func (ti TensorInfo) LogValue() slog.Value { return slog.GroupValue( slog.String("name", ti.Name), slog.Int64("offset", int64(ti.Offset)), slog.Any("shape", ti.Shape), slog.Int64("num_values", ti.NumValues()), slog.Int64("num_bytes", ti.NumBytes()), slog.Any("type", ti.Type), ) } type TensorType uint32 const ( TensorTypeF32 TensorType = iota TensorTypeF16 TensorTypeQ4_0 TensorTypeQ4_1 // unexported // unused in gguf tensorTypeQ4_2 tensorTypeQ4_3 TensorTypeQ5_0 TensorTypeQ5_1 TensorTypeQ8_0 TensorTypeQ8_1 TensorTypeQ2_K TensorTypeQ3_K TensorTypeQ4_K TensorTypeQ5_K TensorTypeQ6_K TensorTypeQ8_K // unexported // unquantizable by ollama tensorTypeIQ2_XXS tensorTypeIQ2_XS tensorTypeIQ3_XXS tensorTypeIQ1_S tensorTypeIQ4_NL tensorTypeIQ3_S tensorTypeIQ2_S tensorTypeIQ4_XS TensorTypeI8 TensorTypeI16 TensorTypeI32 TensorTypeI64 TensorTypeF64 // unexported // unquantizable by ollama tensorTypeIQ1_M TensorTypeBF16 // unexported // unused in gguf tensorTypeQ4_0_4_4 tensorTypeQ4_0_4_8 tensorTypeQ4_0_8_8 // unexported // unquantizable by ollama tensorTypeTQ1_0 tensorTypeTQ2_0 // unexported // unused in gguf tensorTypeIQ4_NL_4_4 tensorTypeIQ4_NL_4_8 tensorTypeIQ4_NL_8_8 ) func (tt TensorType) NumBytes() float64 { return float64(tt.typeSize()) / float64(tt.blockSize()) } func (tt TensorType) typeSize() int64 { switch tt { case TensorTypeF32: return 4 case TensorTypeF16: return 2 case TensorTypeQ4_0: return 2 + tt.blockSize()/2 case TensorTypeQ4_1: return 2 + 2 + tt.blockSize()/2 case TensorTypeQ5_0: return 2 + 4 + tt.blockSize()/2 case TensorTypeQ5_1: return 2 + 2 + 4 + tt.blockSize()/2 case TensorTypeQ8_0: return 2 + tt.blockSize() case TensorTypeQ8_1: return 2 + 2 + tt.blockSize() case TensorTypeQ2_K: return tt.blockSize()/16 + tt.blockSize()/4 + 2 + 2 case TensorTypeQ3_K: return tt.blockSize()/8 + tt.blockSize()/4 + 12 + 2 case TensorTypeQ4_K: return 2 + 2 + 12 + tt.blockSize()/2 case TensorTypeQ5_K: return 2 + 2 + 12 + tt.blockSize()/8 + tt.blockSize()/2 case TensorTypeQ6_K: return tt.blockSize()/2 + tt.blockSize()/4 + tt.blockSize()/16 + 2 case TensorTypeQ8_K: return 4 + tt.blockSize() + 2*tt.blockSize()/16 case tensorTypeIQ2_XXS: return 2 + 2*tt.blockSize()/8 case tensorTypeIQ2_XS: return 2 + 2*tt.blockSize()/8 + tt.blockSize()/32 case tensorTypeIQ3_XXS: return 2 + tt.blockSize()/4 + tt.blockSize()/8 case tensorTypeIQ1_S: return 2 + tt.blockSize()/8 + tt.blockSize()/16 case tensorTypeIQ4_NL: return 2 + tt.blockSize()/2 case tensorTypeIQ3_S: return 2 + tt.blockSize()/4 + tt.blockSize()/8 + tt.blockSize()/32 + 4 case tensorTypeIQ2_S: return 2 + tt.blockSize()/4 + tt.blockSize()/16 case tensorTypeIQ4_XS: return 2 + 2 + tt.blockSize()/2 + tt.blockSize()/64 case TensorTypeI8: return 1 case TensorTypeI16: return 2 case TensorTypeI32: return 4 case TensorTypeI64: return 8 case TensorTypeF64: return 8 case tensorTypeIQ1_M: return tt.blockSize()/8 + tt.blockSize()/16 + tt.blockSize()/32 case TensorTypeBF16: return 2 default: return 0 } } func (tt TensorType) blockSize() int64 { switch tt { case TensorTypeF32, TensorTypeF16, TensorTypeI8, TensorTypeI16, TensorTypeI32, TensorTypeI64, TensorTypeF64, TensorTypeBF16: return 1 case TensorTypeQ4_0, TensorTypeQ4_1, TensorTypeQ5_0, TensorTypeQ5_1, TensorTypeQ8_0, TensorTypeQ8_1, tensorTypeIQ4_NL: return 32 default: return 256 } } func (tt TensorType) String() string { switch tt { case TensorTypeF32: return "f32" case TensorTypeF16: return "f16" case TensorTypeQ4_0: return "q4_0" case TensorTypeQ4_1: return "q4_1" case tensorTypeQ4_2: return "q4_2" case tensorTypeQ4_3: return "q4_3" case TensorTypeQ5_0: return "q5_0" case TensorTypeQ5_1: return "q5_1" case TensorTypeQ8_0: return "q8_0" case TensorTypeQ8_1: return "q8_1" case TensorTypeQ2_K: return "q2_k" case TensorTypeQ3_K: return "q3_k" case TensorTypeQ4_K: return "q4_k" case TensorTypeQ5_K: return "q5_k" case TensorTypeQ6_K: return "q6_k" case TensorTypeQ8_K: return "q8_k" case tensorTypeIQ2_XXS: return "iq2_xxs" case tensorTypeIQ2_XS: return "iq2_xs" case tensorTypeIQ3_XXS: return "iq3_xxs" case tensorTypeIQ1_S: return "iq1_s" case tensorTypeIQ4_NL: return "iq4_nl" case tensorTypeIQ3_S: return "iq3_s" case tensorTypeIQ2_S: return "iq2_s" case tensorTypeIQ4_XS: return "iq4_xs" case TensorTypeI8: return "i8" case TensorTypeI16: return "i16" case TensorTypeI32: return "i32" case TensorTypeI64: return "i64" case TensorTypeF64: return "f64" case tensorTypeIQ1_M: return "iq1_m" case TensorTypeBF16: return "bf16" case tensorTypeQ4_0_4_4: return "q4_0_4_4" case tensorTypeQ4_0_4_8: return "q4_0_4_8" case tensorTypeQ4_0_8_8: return "q4_0_8_8" case tensorTypeTQ1_0: return "tq1_0" case tensorTypeTQ2_0: return "tq2_0" case tensorTypeIQ4_NL_4_4: return "iq4_nl_4_4" case tensorTypeIQ4_NL_4_8: return "iq4_nl_4_8" case tensorTypeIQ4_NL_8_8: return "iq4_nl_8_8" default: return "unknown" } } func (tt TensorType) LogValue() slog.Value { return slog.GroupValue( slog.Uint64("value", uint64(tt)), slog.String("name", strings.ToUpper(tt.String())), slog.Int64("size", tt.typeSize()), slog.Int64("block_size", tt.blockSize()), slog.Float64("num_bytes", tt.NumBytes()), ) }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/fs/gguf/keyvalue.go
fs/gguf/keyvalue.go
package gguf import ( "reflect" "slices" ) type KeyValue struct { Key string Value } func (kv KeyValue) Valid() bool { return kv.Key != "" && kv.Value.value != nil } type Value struct { value any } func value[T any](v Value, kinds ...reflect.Kind) (t T) { vv := reflect.ValueOf(v.value) if slices.Contains(kinds, vv.Kind()) { t = vv.Convert(reflect.TypeOf(t)).Interface().(T) } return } func values[T any](v Value, kinds ...reflect.Kind) (ts []T) { switch vv := reflect.ValueOf(v.value); vv.Kind() { case reflect.Slice: if slices.Contains(kinds, vv.Type().Elem().Kind()) { ts = make([]T, vv.Len()) for i := range vv.Len() { ts[i] = vv.Index(i).Convert(reflect.TypeOf(ts[i])).Interface().(T) } } } return } // Int returns Value as a signed integer. If it is not a signed integer, it returns 0. func (v Value) Int() int64 { return value[int64](v, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64) } // Ints returns Value as a signed integer slice. If it is not a signed integer slice, it returns nil. func (v Value) Ints() (i64s []int64) { return values[int64](v, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64) } // Uint converts an unsigned integer value to uint64. If the value is not a unsigned integer, it returns 0. func (v Value) Uint() uint64 { return value[uint64](v, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64) } // Uints returns Value as a unsigned integer slice. If it is not a unsigned integer slice, it returns nil. func (v Value) Uints() (u64s []uint64) { return values[uint64](v, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64) } // Float returns Value as a float. If it is not a float, it returns 0. func (v Value) Float() float64 { return value[float64](v, reflect.Float32, reflect.Float64) } // Floats returns Value as a float slice. If it is not a float slice, it returns nil. func (v Value) Floats() (f64s []float64) { return values[float64](v, reflect.Float32, reflect.Float64) } // Bool returns Value as a boolean. If it is not a boolean, it returns false. func (v Value) Bool() bool { return value[bool](v, reflect.Bool) } // Bools returns Value as a boolean slice. If it is not a boolean slice, it returns nil. func (v Value) Bools() (bools []bool) { return values[bool](v, reflect.Bool) } // String returns Value as a string. If it is not a string, it returns an empty string. func (v Value) String() string { return value[string](v, reflect.String) } // Strings returns Value as a string slice. If it is not a string slice, it returns nil. func (v Value) Strings() (strings []string) { return values[string](v, reflect.String) }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/fs/gguf/reader.go
fs/gguf/reader.go
package gguf import ( "bufio" "io" ) type bufferedReader struct { offset int64 *bufio.Reader } func newBufferedReader(rs io.ReadSeeker, size int) *bufferedReader { return &bufferedReader{ Reader: bufio.NewReaderSize(rs, size), } } func (rs *bufferedReader) Read(p []byte) (n int, err error) { n, err = rs.Reader.Read(p) rs.offset += int64(n) return n, err }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/fs/gguf/keyvalue_test.go
fs/gguf/keyvalue_test.go
package gguf import ( "testing" "github.com/google/go-cmp/cmp" ) func split(name string, values map[string][]any) (matched []any, unmatched []any) { for key, value := range values { if key == name { matched = value } else { unmatched = append(unmatched, value...) } } return } func TestValue(t *testing.T) { values := map[string][]any{ "int64": {int(42), int8(42), int16(42), int32(42), int64(42)}, "uint64": {uint(42), uint8(42), uint16(42), uint32(42), uint64(42)}, "float64": {float32(42), float64(42)}, "string": {"42", "hello"}, "bool": {true, false}, } t.Run("int64", func(t *testing.T) { matched, unmatched := split("int64", values) for _, v := range matched { kv := KeyValue{"key", Value{v}} if i64 := kv.Int(); i64 != 42 { t.Errorf("expected 42, got %d", i64) } } for _, v := range unmatched { kv := KeyValue{"key", Value{v}} if i64 := kv.Int(); i64 != 0 { t.Errorf("expected 42, got %d", i64) } } }) t.Run("uint64", func(t *testing.T) { matched, unmatched := split("uint64", values) for _, v := range matched { kv := KeyValue{"key", Value{v}} if u64 := kv.Uint(); u64 != 42 { t.Errorf("expected 42, got %d", u64) } } for _, v := range unmatched { kv := KeyValue{"key", Value{v}} if u64 := kv.Uint(); u64 != 0 { t.Errorf("expected 42, got %d", u64) } } }) t.Run("float64", func(t *testing.T) { matched, unmatched := split("float64", values) for _, v := range matched { kv := KeyValue{"key", Value{v}} if f64 := kv.Float(); f64 != 42 { t.Errorf("expected 42, got %f", f64) } } for _, v := range unmatched { kv := KeyValue{"key", Value{v}} if f64 := kv.Float(); f64 != 0 { t.Errorf("expected 42, got %f", f64) } } }) t.Run("string", func(t *testing.T) { matched, unmatched := split("string", values) for _, v := range matched { kv := KeyValue{"key", Value{v}} if s := kv.String(); s != v { t.Errorf("expected 42, got %s", s) } } for _, v := range unmatched { kv := KeyValue{"key", Value{v}} if s := kv.String(); s != "" { t.Errorf("expected 42, got %s", s) } } }) t.Run("bool", func(t *testing.T) { matched, unmatched := split("bool", values) for _, v := range matched { kv := KeyValue{"key", Value{v}} if b := kv.Bool(); b != v { t.Errorf("expected true, got %v", b) } } for _, v := range unmatched { kv := KeyValue{"key", Value{v}} if b := kv.Bool(); b != false { t.Errorf("expected false, got %v", b) } } }) } func TestValues(t *testing.T) { values := map[string][]any{ "int64s": {[]int{42}, []int8{42}, []int16{42}, []int32{42}, []int64{42}}, "uint64s": {[]uint{42}, []uint8{42}, []uint16{42}, []uint32{42}, []uint64{42}}, "float64s": {[]float32{42}, []float64{42}}, "strings": {[]string{"42"}, []string{"hello"}}, "bools": {[]bool{true}, []bool{false}}, } t.Run("int64s", func(t *testing.T) { matched, unmatched := split("int64s", values) for _, v := range matched { kv := KeyValue{"key", Value{v}} if diff := cmp.Diff(kv.Ints(), []int64{42}); diff != "" { t.Errorf("diff: %s", diff) } } for _, v := range unmatched { kv := KeyValue{"key", Value{v}} if i64s := kv.Ints(); i64s != nil { t.Errorf("expected nil, got %v", i64s) } } }) t.Run("uint64s", func(t *testing.T) { matched, unmatched := split("uint64s", values) for _, v := range matched { kv := KeyValue{"key", Value{v}} if diff := cmp.Diff(kv.Uints(), []uint64{42}); diff != "" { t.Errorf("diff: %s", diff) } } for _, v := range unmatched { kv := KeyValue{"key", Value{v}} if u64s := kv.Uints(); u64s != nil { t.Errorf("expected nil, got %v", u64s) } } }) t.Run("float64s", func(t *testing.T) { matched, unmatched := split("float64s", values) for _, v := range matched { kv := KeyValue{"key", Value{v}} if diff := cmp.Diff(kv.Floats(), []float64{42}); diff != "" { t.Errorf("diff: %s", diff) } } for _, v := range unmatched { kv := KeyValue{"key", Value{v}} if f64s := kv.Floats(); f64s != nil { t.Errorf("expected nil, got %v", f64s) } } }) t.Run("strings", func(t *testing.T) { matched, unmatched := split("strings", values) for _, v := range matched { kv := KeyValue{"key", Value{v}} if diff := cmp.Diff(kv.Strings(), v); diff != "" { t.Errorf("diff: %s", diff) } } for _, v := range unmatched { kv := KeyValue{"key", Value{v}} if s := kv.Strings(); s != nil { t.Errorf("expected nil, got %v", s) } } }) t.Run("bools", func(t *testing.T) { matched, unmatched := split("bools", values) for _, v := range matched { kv := KeyValue{"key", Value{v}} if diff := cmp.Diff(kv.Bools(), v); diff != "" { t.Errorf("diff: %s", diff) } } for _, v := range unmatched { kv := KeyValue{"key", Value{v}} if b := kv.Bools(); b != nil { t.Errorf("expected nil, got %v", b) } } }) }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/fs/gguf/gguf_test.go
fs/gguf/gguf_test.go
package gguf_test import ( "bytes" "os" "strconv" "strings" "testing" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/ollama/ollama/fs/ggml" "github.com/ollama/ollama/fs/gguf" ) func createBinFile(tb testing.TB) string { tb.Helper() f, err := os.CreateTemp(tb.TempDir(), "") if err != nil { tb.Fatal(err) } defer f.Close() kv := ggml.KV{ "general.architecture": "llama", "llama.block_count": uint32(8), "llama.embedding_length": uint32(3), "llama.attention.head_count": uint32(2), "llama.attention.head_count_kv": uint32(2), "llama.attention.key_length": uint32(3), "llama.rope.dimension_count": uint32(4), "llama.rope.freq_base": float32(10000.0), "llama.rope.freq_scale": float32(1.0), "llama.attention.layer_norm_rms_epsilon": float32(1e-6), "tokenizer.ggml.eos_token_id": uint32(0), "tokenizer.ggml.eos_token_ids": []int32{1, 2, 3}, "tokenizer.ggml.tokens": []string{"hello", "world"}, "tokenizer.ggml.scores": []float32{0, 1}, } tensors := []*ggml.Tensor{ { Name: "token_embd.weight", Kind: 0, Shape: []uint64{2, 3}, WriterTo: bytes.NewBuffer(make([]byte, 4*2*3)), }, { Name: "output.weight", Kind: 0, Shape: []uint64{3, 2}, WriterTo: bytes.NewBuffer(make([]byte, 4*3*2)), }, } for i := range 8 { tensors = append(tensors, &ggml.Tensor{ Name: "blk." + strconv.Itoa(i) + ".attn_q.weight", Kind: 0, Shape: []uint64{3, 3}, WriterTo: bytes.NewBuffer(make([]byte, 4*3*3)), }, &ggml.Tensor{ Name: "blk." + strconv.Itoa(i) + ".attn_k.weight", Kind: 0, Shape: []uint64{3, 3}, WriterTo: bytes.NewBuffer(make([]byte, 4*3*3)), }, &ggml.Tensor{ Name: "blk." + strconv.Itoa(i) + ".attn_v.weight", Kind: 0, Shape: []uint64{3, 3}, WriterTo: bytes.NewBuffer(make([]byte, 4*3*3)), }, &ggml.Tensor{ Name: "blk." + strconv.Itoa(i) + ".attn_output.weight", Kind: 0, Shape: []uint64{3, 3}, WriterTo: bytes.NewBuffer(make([]byte, 4*3*3)), }) } if err := ggml.WriteGGUF(f, kv, tensors); err != nil { tb.Fatal(err) } return f.Name() } func TestRead(t *testing.T) { f, err := gguf.Open(createBinFile(t)) if err != nil { t.Fatal(err) } defer f.Close() if got := f.KeyValue("does.not.exist").Valid(); got { t.Errorf(`KeyValue("does.not.exist").Exists() = %v, want false`, got) } if got := f.KeyValue("general.architecture").String(); got != "llama" { t.Errorf(`KeyValue("general.architecture").String() = %q, want %q`, got, "llama") } if got := f.TensorInfo("token_embd.weight"); got.Name != "token_embd.weight" { t.Errorf(`TensorInfo("token_embd.weight").Name = %q, want %q`, got.Name, "token_embd.weight") } else if diff := cmp.Diff(got.Shape, []uint64{2, 3}); diff != "" { t.Errorf(`TensorInfo("token_embd.weight").Shape mismatch (-got +want):\n%s`, diff) } else if got.Type != gguf.TensorTypeF32 { t.Errorf(`TensorInfo("token_embd.weight").Type = %d, want %d`, got.Type, gguf.TensorTypeF32) } if got := f.KeyValue("block_count").Uint(); got != 8 { t.Errorf(`KeyValue("block_count").Uint() = %d, want %d`, got, 8) } if diff := cmp.Diff(f.KeyValue("tokenizer.ggml.tokens").Strings(), []string{"hello", "world"}); diff != "" { t.Errorf("KeyValue(\"tokenizer.ggml.tokens\").Strings() mismatch (-got +want):\n%s", diff) } if diff := cmp.Diff(f.KeyValue("tokenizer.ggml.scores").Floats(), []float64{0, 1}); diff != "" { t.Errorf("KeyValue(\"tokenizer.ggml.scores\").Ints() mismatch (-got +want):\n%s", diff) } var kvs []string for _, kv := range f.KeyValues() { if !kv.Valid() { t.Error("found invalid key-value pair:", kv) } kvs = append(kvs, kv.Key) } if len(kvs) != f.NumKeyValues() { t.Errorf("iterated key count = %d, want %d", len(kvs), f.NumKeyValues()) } if diff := cmp.Diff(kvs, []string{ "general.architecture", "llama.block_count", "llama.embedding_length", "llama.attention.head_count", "llama.attention.head_count_kv", "llama.attention.key_length", "llama.rope.dimension_count", "llama.rope.freq_base", "llama.rope.freq_scale", "llama.attention.layer_norm_rms_epsilon", "tokenizer.ggml.eos_token_id", "tokenizer.ggml.eos_token_ids", "tokenizer.ggml.tokens", "tokenizer.ggml.scores", }, cmpopts.SortSlices(strings.Compare)); diff != "" { t.Errorf("KeyValues() mismatch (-got +want):\n%s", diff) } var tis []string for _, ti := range f.TensorInfos() { if !ti.Valid() { t.Error("found invalid tensor info:", ti) } tis = append(tis, ti.Name) } if len(tis) != f.NumTensors() { t.Errorf("iterated tensor count = %d, want %d", len(tis), f.NumTensors()) } if diff := cmp.Diff(tis, []string{ "token_embd.weight", "output.weight", "blk.0.attn_q.weight", "blk.0.attn_k.weight", "blk.0.attn_v.weight", "blk.0.attn_output.weight", "blk.1.attn_q.weight", "blk.1.attn_k.weight", "blk.1.attn_v.weight", "blk.1.attn_output.weight", "blk.2.attn_q.weight", "blk.2.attn_k.weight", "blk.2.attn_v.weight", "blk.2.attn_output.weight", "blk.3.attn_q.weight", "blk.3.attn_k.weight", "blk.3.attn_v.weight", "blk.3.attn_output.weight", "blk.4.attn_q.weight", "blk.4.attn_k.weight", "blk.4.attn_v.weight", "blk.4.attn_output.weight", "blk.5.attn_q.weight", "blk.5.attn_k.weight", "blk.5.attn_v.weight", "blk.5.attn_output.weight", "blk.6.attn_q.weight", "blk.6.attn_k.weight", "blk.6.attn_v.weight", "blk.6.attn_output.weight", "blk.7.attn_q.weight", "blk.7.attn_k.weight", "blk.7.attn_v.weight", "blk.7.attn_output.weight", }, cmpopts.SortSlices(strings.Compare)); diff != "" { t.Errorf("TensorInfos() mismatch (-got +want):\n%s", diff) } ti, r, err := f.TensorReader("output.weight") if err != nil { t.Fatalf(`TensorReader("output.weight") error: %v`, err) } if ti.Name != "output.weight" { t.Errorf(`TensorReader("output.weight").Name = %q, want %q`, ti.Name, "output.weight") } else if diff := cmp.Diff(ti.Shape, []uint64{3, 2}); diff != "" { t.Errorf(`TensorReader("output.weight").Shape mismatch (-got +want):\n%s`, diff) } else if ti.Type != gguf.TensorTypeF32 { t.Errorf(`TensorReader("output.weight").Type = %d, want %d`, ti.Type, gguf.TensorTypeF32) } var b bytes.Buffer if _, err := b.ReadFrom(r); err != nil { t.Fatalf(`ReadFrom TensorReader("output.weight") error: %v`, err) } if b.Len() != int(ti.NumBytes()) { t.Errorf(`ReadFrom TensorReader("output.weight") length = %d, want %d`, b.Len(), ti.NumBytes()) } } func BenchmarkRead(b *testing.B) { b.ReportAllocs() p := createBinFile(b) for b.Loop() { f, err := gguf.Open(p) if err != nil { b.Fatal(err) } if got := f.KeyValue("general.architecture").String(); got != "llama" { b.Errorf("got = %q, want %q", got, "llama") } // Iterate through some tensors for range f.TensorInfos() { } f.Close() } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/logutil/logutil.go
logutil/logutil.go
package logutil import ( "context" "io" "log/slog" "path/filepath" "runtime" "time" ) const LevelTrace slog.Level = -8 func NewLogger(w io.Writer, level slog.Level) *slog.Logger { return slog.New(slog.NewTextHandler(w, &slog.HandlerOptions{ Level: level, AddSource: true, ReplaceAttr: func(_ []string, attr slog.Attr) slog.Attr { switch attr.Key { case slog.LevelKey: switch attr.Value.Any().(slog.Level) { case LevelTrace: attr.Value = slog.StringValue("TRACE") } case slog.SourceKey: source := attr.Value.Any().(*slog.Source) source.File = filepath.Base(source.File) } return attr }, })) } type key string func Trace(msg string, args ...any) { TraceContext(context.WithValue(context.TODO(), key("skip"), 1), msg, args...) } func TraceContext(ctx context.Context, msg string, args ...any) { if logger := slog.Default(); logger.Enabled(ctx, LevelTrace) { skip, _ := ctx.Value(key("skip")).(int) pc, _, _, _ := runtime.Caller(1 + skip) record := slog.NewRecord(time.Now(), LevelTrace, msg, pc) record.Add(args...) logger.Handler().Handle(ctx, record) } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/envconfig/config.go
envconfig/config.go
package envconfig import ( "fmt" "log/slog" "math" "net" "net/url" "os" "path/filepath" "runtime" "strconv" "strings" "time" ) // Host returns the scheme and host. Host can be configured via the OLLAMA_HOST environment variable. // Default is scheme "http" and host "127.0.0.1:11434" func Host() *url.URL { defaultPort := "11434" s := strings.TrimSpace(Var("OLLAMA_HOST")) scheme, hostport, ok := strings.Cut(s, "://") switch { case !ok: scheme, hostport = "http", s if s == "ollama.com" { scheme, hostport = "https", "ollama.com:443" } case scheme == "http": defaultPort = "80" case scheme == "https": defaultPort = "443" } hostport, path, _ := strings.Cut(hostport, "/") host, port, err := net.SplitHostPort(hostport) if err != nil { host, port = "127.0.0.1", defaultPort if ip := net.ParseIP(strings.Trim(hostport, "[]")); ip != nil { host = ip.String() } else if hostport != "" { host = hostport } } if n, err := strconv.ParseInt(port, 10, 32); err != nil || n > 65535 || n < 0 { slog.Warn("invalid port, using default", "port", port, "default", defaultPort) port = defaultPort } return &url.URL{ Scheme: scheme, Host: net.JoinHostPort(host, port), Path: path, } } // AllowedOrigins returns a list of allowed origins. AllowedOrigins can be configured via the OLLAMA_ORIGINS environment variable. func AllowedOrigins() (origins []string) { if s := Var("OLLAMA_ORIGINS"); s != "" { origins = strings.Split(s, ",") } for _, origin := range []string{"localhost", "127.0.0.1", "0.0.0.0"} { origins = append(origins, fmt.Sprintf("http://%s", origin), fmt.Sprintf("https://%s", origin), fmt.Sprintf("http://%s", net.JoinHostPort(origin, "*")), fmt.Sprintf("https://%s", net.JoinHostPort(origin, "*")), ) } origins = append(origins, "app://*", "file://*", "tauri://*", "vscode-webview://*", "vscode-file://*", ) return origins } // Models returns the path to the models directory. Models directory can be configured via the OLLAMA_MODELS environment variable. // Default is $HOME/.ollama/models func Models() string { if s := Var("OLLAMA_MODELS"); s != "" { return s } home, err := os.UserHomeDir() if err != nil { panic(err) } return filepath.Join(home, ".ollama", "models") } // KeepAlive returns the duration that models stay loaded in memory. KeepAlive can be configured via the OLLAMA_KEEP_ALIVE environment variable. // Negative values are treated as infinite. Zero is treated as no keep alive. // Default is 5 minutes. func KeepAlive() (keepAlive time.Duration) { keepAlive = 5 * time.Minute if s := Var("OLLAMA_KEEP_ALIVE"); s != "" { if d, err := time.ParseDuration(s); err == nil { keepAlive = d } else if n, err := strconv.ParseInt(s, 10, 64); err == nil { keepAlive = time.Duration(n) * time.Second } } if keepAlive < 0 { return time.Duration(math.MaxInt64) } return keepAlive } // LoadTimeout returns the duration for stall detection during model loads. LoadTimeout can be configured via the OLLAMA_LOAD_TIMEOUT environment variable. // Zero or Negative values are treated as infinite. // Default is 5 minutes. func LoadTimeout() (loadTimeout time.Duration) { loadTimeout = 5 * time.Minute if s := Var("OLLAMA_LOAD_TIMEOUT"); s != "" { if d, err := time.ParseDuration(s); err == nil { loadTimeout = d } else if n, err := strconv.ParseInt(s, 10, 64); err == nil { loadTimeout = time.Duration(n) * time.Second } } if loadTimeout <= 0 { return time.Duration(math.MaxInt64) } return loadTimeout } func Remotes() []string { var r []string raw := strings.TrimSpace(Var("OLLAMA_REMOTES")) if raw == "" { r = []string{"ollama.com"} } else { r = strings.Split(raw, ",") } return r } func BoolWithDefault(k string) func(defaultValue bool) bool { return func(defaultValue bool) bool { if s := Var(k); s != "" { b, err := strconv.ParseBool(s) if err != nil { return true } return b } return defaultValue } } func Bool(k string) func() bool { withDefault := BoolWithDefault(k) return func() bool { return withDefault(false) } } // LogLevel returns the log level for the application. // Values are 0 or false INFO (Default), 1 or true DEBUG, 2 TRACE func LogLevel() slog.Level { level := slog.LevelInfo if s := Var("OLLAMA_DEBUG"); s != "" { if b, _ := strconv.ParseBool(s); b { level = slog.LevelDebug } else if i, _ := strconv.ParseInt(s, 10, 64); i != 0 { level = slog.Level(i * -4) } } return level } var ( // FlashAttention enables the experimental flash attention feature. FlashAttention = BoolWithDefault("OLLAMA_FLASH_ATTENTION") // KvCacheType is the quantization type for the K/V cache. KvCacheType = String("OLLAMA_KV_CACHE_TYPE") // NoHistory disables readline history. NoHistory = Bool("OLLAMA_NOHISTORY") // NoPrune disables pruning of model blobs on startup. NoPrune = Bool("OLLAMA_NOPRUNE") // SchedSpread allows scheduling models across all GPUs. SchedSpread = Bool("OLLAMA_SCHED_SPREAD") // MultiUserCache optimizes prompt caching for multi-user scenarios MultiUserCache = Bool("OLLAMA_MULTIUSER_CACHE") // Enable the new Ollama engine NewEngine = Bool("OLLAMA_NEW_ENGINE") // ContextLength sets the default context length ContextLength = Uint("OLLAMA_CONTEXT_LENGTH", 4096) // Auth enables authentication between the Ollama client and server UseAuth = Bool("OLLAMA_AUTH") // Enable Vulkan backend EnableVulkan = Bool("OLLAMA_VULKAN") ) func String(s string) func() string { return func() string { return Var(s) } } var ( LLMLibrary = String("OLLAMA_LLM_LIBRARY") CudaVisibleDevices = String("CUDA_VISIBLE_DEVICES") HipVisibleDevices = String("HIP_VISIBLE_DEVICES") RocrVisibleDevices = String("ROCR_VISIBLE_DEVICES") VkVisibleDevices = String("GGML_VK_VISIBLE_DEVICES") GpuDeviceOrdinal = String("GPU_DEVICE_ORDINAL") HsaOverrideGfxVersion = String("HSA_OVERRIDE_GFX_VERSION") ) func Uint(key string, defaultValue uint) func() uint { return func() uint { if s := Var(key); s != "" { if n, err := strconv.ParseUint(s, 10, 64); err != nil { slog.Warn("invalid environment variable, using default", "key", key, "value", s, "default", defaultValue) } else { return uint(n) } } return defaultValue } } var ( // NumParallel sets the number of parallel model requests. NumParallel can be configured via the OLLAMA_NUM_PARALLEL environment variable. NumParallel = Uint("OLLAMA_NUM_PARALLEL", 1) // MaxRunners sets the maximum number of loaded models. MaxRunners can be configured via the OLLAMA_MAX_LOADED_MODELS environment variable. MaxRunners = Uint("OLLAMA_MAX_LOADED_MODELS", 0) // MaxQueue sets the maximum number of queued requests. MaxQueue can be configured via the OLLAMA_MAX_QUEUE environment variable. MaxQueue = Uint("OLLAMA_MAX_QUEUE", 512) ) func Uint64(key string, defaultValue uint64) func() uint64 { return func() uint64 { if s := Var(key); s != "" { if n, err := strconv.ParseUint(s, 10, 64); err != nil { slog.Warn("invalid environment variable, using default", "key", key, "value", s, "default", defaultValue) } else { return n } } return defaultValue } } // Set aside VRAM per GPU var GpuOverhead = Uint64("OLLAMA_GPU_OVERHEAD", 0) type EnvVar struct { Name string Value any Description string } func AsMap() map[string]EnvVar { ret := map[string]EnvVar{ "OLLAMA_DEBUG": {"OLLAMA_DEBUG", LogLevel(), "Show additional debug information (e.g. OLLAMA_DEBUG=1)"}, "OLLAMA_FLASH_ATTENTION": {"OLLAMA_FLASH_ATTENTION", FlashAttention(false), "Enabled flash attention"}, "OLLAMA_KV_CACHE_TYPE": {"OLLAMA_KV_CACHE_TYPE", KvCacheType(), "Quantization type for the K/V cache (default: f16)"}, "OLLAMA_GPU_OVERHEAD": {"OLLAMA_GPU_OVERHEAD", GpuOverhead(), "Reserve a portion of VRAM per GPU (bytes)"}, "OLLAMA_HOST": {"OLLAMA_HOST", Host(), "IP Address for the ollama server (default 127.0.0.1:11434)"}, "OLLAMA_KEEP_ALIVE": {"OLLAMA_KEEP_ALIVE", KeepAlive(), "The duration that models stay loaded in memory (default \"5m\")"}, "OLLAMA_LLM_LIBRARY": {"OLLAMA_LLM_LIBRARY", LLMLibrary(), "Set LLM library to bypass autodetection"}, "OLLAMA_LOAD_TIMEOUT": {"OLLAMA_LOAD_TIMEOUT", LoadTimeout(), "How long to allow model loads to stall before giving up (default \"5m\")"}, "OLLAMA_MAX_LOADED_MODELS": {"OLLAMA_MAX_LOADED_MODELS", MaxRunners(), "Maximum number of loaded models per GPU"}, "OLLAMA_MAX_QUEUE": {"OLLAMA_MAX_QUEUE", MaxQueue(), "Maximum number of queued requests"}, "OLLAMA_MODELS": {"OLLAMA_MODELS", Models(), "The path to the models directory"}, "OLLAMA_NOHISTORY": {"OLLAMA_NOHISTORY", NoHistory(), "Do not preserve readline history"}, "OLLAMA_NOPRUNE": {"OLLAMA_NOPRUNE", NoPrune(), "Do not prune model blobs on startup"}, "OLLAMA_NUM_PARALLEL": {"OLLAMA_NUM_PARALLEL", NumParallel(), "Maximum number of parallel requests"}, "OLLAMA_ORIGINS": {"OLLAMA_ORIGINS", AllowedOrigins(), "A comma separated list of allowed origins"}, "OLLAMA_SCHED_SPREAD": {"OLLAMA_SCHED_SPREAD", SchedSpread(), "Always schedule model across all GPUs"}, "OLLAMA_MULTIUSER_CACHE": {"OLLAMA_MULTIUSER_CACHE", MultiUserCache(), "Optimize prompt caching for multi-user scenarios"}, "OLLAMA_CONTEXT_LENGTH": {"OLLAMA_CONTEXT_LENGTH", ContextLength(), "Context length to use unless otherwise specified (default: 4096)"}, "OLLAMA_NEW_ENGINE": {"OLLAMA_NEW_ENGINE", NewEngine(), "Enable the new Ollama engine"}, "OLLAMA_REMOTES": {"OLLAMA_REMOTES", Remotes(), "Allowed hosts for remote models (default \"ollama.com\")"}, // Informational "HTTP_PROXY": {"HTTP_PROXY", String("HTTP_PROXY")(), "HTTP proxy"}, "HTTPS_PROXY": {"HTTPS_PROXY", String("HTTPS_PROXY")(), "HTTPS proxy"}, "NO_PROXY": {"NO_PROXY", String("NO_PROXY")(), "No proxy"}, } if runtime.GOOS != "windows" { // Windows environment variables are case-insensitive so there's no need to duplicate them ret["http_proxy"] = EnvVar{"http_proxy", String("http_proxy")(), "HTTP proxy"} ret["https_proxy"] = EnvVar{"https_proxy", String("https_proxy")(), "HTTPS proxy"} ret["no_proxy"] = EnvVar{"no_proxy", String("no_proxy")(), "No proxy"} } if runtime.GOOS != "darwin" { ret["CUDA_VISIBLE_DEVICES"] = EnvVar{"CUDA_VISIBLE_DEVICES", CudaVisibleDevices(), "Set which NVIDIA devices are visible"} ret["HIP_VISIBLE_DEVICES"] = EnvVar{"HIP_VISIBLE_DEVICES", HipVisibleDevices(), "Set which AMD devices are visible by numeric ID"} ret["ROCR_VISIBLE_DEVICES"] = EnvVar{"ROCR_VISIBLE_DEVICES", RocrVisibleDevices(), "Set which AMD devices are visible by UUID or numeric ID"} ret["GGML_VK_VISIBLE_DEVICES"] = EnvVar{"GGML_VK_VISIBLE_DEVICES", VkVisibleDevices(), "Set which Vulkan devices are visible by numeric ID"} ret["GPU_DEVICE_ORDINAL"] = EnvVar{"GPU_DEVICE_ORDINAL", GpuDeviceOrdinal(), "Set which AMD devices are visible by numeric ID"} ret["HSA_OVERRIDE_GFX_VERSION"] = EnvVar{"HSA_OVERRIDE_GFX_VERSION", HsaOverrideGfxVersion(), "Override the gfx used for all detected AMD GPUs"} ret["OLLAMA_VULKAN"] = EnvVar{"OLLAMA_VULKAN", EnableVulkan(), "Enable experimental Vulkan support"} } return ret } func Values() map[string]string { vals := make(map[string]string) for k, v := range AsMap() { vals[k] = fmt.Sprintf("%v", v.Value) } return vals } // Var returns an environment variable stripped of leading and trailing quotes or spaces func Var(key string) string { return strings.Trim(strings.TrimSpace(os.Getenv(key)), "\"'") }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/envconfig/config_test.go
envconfig/config_test.go
package envconfig import ( "log/slog" "math" "testing" "time" "github.com/google/go-cmp/cmp" "github.com/ollama/ollama/logutil" ) func TestHost(t *testing.T) { cases := map[string]struct { value string expect string }{ "empty": {"", "http://127.0.0.1:11434"}, "only address": {"1.2.3.4", "http://1.2.3.4:11434"}, "only port": {":1234", "http://:1234"}, "address and port": {"1.2.3.4:1234", "http://1.2.3.4:1234"}, "hostname": {"example.com", "http://example.com:11434"}, "hostname and port": {"example.com:1234", "http://example.com:1234"}, "zero port": {":0", "http://:0"}, "too large port": {":66000", "http://:11434"}, "too small port": {":-1", "http://:11434"}, "ipv6 localhost": {"[::1]", "http://[::1]:11434"}, "ipv6 world open": {"[::]", "http://[::]:11434"}, "ipv6 no brackets": {"::1", "http://[::1]:11434"}, "ipv6 + port": {"[::1]:1337", "http://[::1]:1337"}, "extra space": {" 1.2.3.4 ", "http://1.2.3.4:11434"}, "extra quotes": {"\"1.2.3.4\"", "http://1.2.3.4:11434"}, "extra space+quotes": {" \" 1.2.3.4 \" ", "http://1.2.3.4:11434"}, "extra single quotes": {"'1.2.3.4'", "http://1.2.3.4:11434"}, "http": {"http://1.2.3.4", "http://1.2.3.4:80"}, "http port": {"http://1.2.3.4:4321", "http://1.2.3.4:4321"}, "https": {"https://1.2.3.4", "https://1.2.3.4:443"}, "https port": {"https://1.2.3.4:4321", "https://1.2.3.4:4321"}, "proxy path": {"https://example.com/ollama", "https://example.com:443/ollama"}, "ollama.com": {"ollama.com", "https://ollama.com:443"}, } for name, tt := range cases { t.Run(name, func(t *testing.T) { t.Setenv("OLLAMA_HOST", tt.value) if host := Host(); host.String() != tt.expect { t.Errorf("%s: expected %s, got %s", name, tt.expect, host.String()) } }) } } func TestOrigins(t *testing.T) { cases := []struct { value string expect []string }{ {"", []string{ "http://localhost", "https://localhost", "http://localhost:*", "https://localhost:*", "http://127.0.0.1", "https://127.0.0.1", "http://127.0.0.1:*", "https://127.0.0.1:*", "http://0.0.0.0", "https://0.0.0.0", "http://0.0.0.0:*", "https://0.0.0.0:*", "app://*", "file://*", "tauri://*", "vscode-webview://*", "vscode-file://*", }}, {"http://10.0.0.1", []string{ "http://10.0.0.1", "http://localhost", "https://localhost", "http://localhost:*", "https://localhost:*", "http://127.0.0.1", "https://127.0.0.1", "http://127.0.0.1:*", "https://127.0.0.1:*", "http://0.0.0.0", "https://0.0.0.0", "http://0.0.0.0:*", "https://0.0.0.0:*", "app://*", "file://*", "tauri://*", "vscode-webview://*", "vscode-file://*", }}, {"http://172.16.0.1,https://192.168.0.1", []string{ "http://172.16.0.1", "https://192.168.0.1", "http://localhost", "https://localhost", "http://localhost:*", "https://localhost:*", "http://127.0.0.1", "https://127.0.0.1", "http://127.0.0.1:*", "https://127.0.0.1:*", "http://0.0.0.0", "https://0.0.0.0", "http://0.0.0.0:*", "https://0.0.0.0:*", "app://*", "file://*", "tauri://*", "vscode-webview://*", "vscode-file://*", }}, {"http://totally.safe,http://definitely.legit", []string{ "http://totally.safe", "http://definitely.legit", "http://localhost", "https://localhost", "http://localhost:*", "https://localhost:*", "http://127.0.0.1", "https://127.0.0.1", "http://127.0.0.1:*", "https://127.0.0.1:*", "http://0.0.0.0", "https://0.0.0.0", "http://0.0.0.0:*", "https://0.0.0.0:*", "app://*", "file://*", "tauri://*", "vscode-webview://*", "vscode-file://*", }}, } for _, tt := range cases { t.Run(tt.value, func(t *testing.T) { t.Setenv("OLLAMA_ORIGINS", tt.value) if diff := cmp.Diff(AllowedOrigins(), tt.expect); diff != "" { t.Errorf("%s: mismatch (-want +got):\n%s", tt.value, diff) } }) } } func TestBool(t *testing.T) { cases := map[string]bool{ "": false, "true": true, "false": false, "1": true, "0": false, // invalid values "random": true, "something": true, } for k, v := range cases { t.Run(k, func(t *testing.T) { t.Setenv("OLLAMA_BOOL", k) if b := Bool("OLLAMA_BOOL")(); b != v { t.Errorf("%s: expected %t, got %t", k, v, b) } }) } } func TestUint(t *testing.T) { cases := map[string]uint{ "0": 0, "1": 1, "1337": 1337, // default values "": 11434, "-1": 11434, "0o10": 11434, "0x10": 11434, "string": 11434, } for k, v := range cases { t.Run(k, func(t *testing.T) { t.Setenv("OLLAMA_UINT", k) if i := Uint("OLLAMA_UINT", 11434)(); i != v { t.Errorf("%s: expected %d, got %d", k, v, i) } }) } } func TestKeepAlive(t *testing.T) { cases := map[string]time.Duration{ "": 5 * time.Minute, "1s": time.Second, "1m": time.Minute, "1h": time.Hour, "5m0s": 5 * time.Minute, "1h2m3s": 1*time.Hour + 2*time.Minute + 3*time.Second, "0": time.Duration(0), "60": 60 * time.Second, "120": 2 * time.Minute, "3600": time.Hour, "-0": time.Duration(0), "-1": time.Duration(math.MaxInt64), "-1m": time.Duration(math.MaxInt64), // invalid values " ": 5 * time.Minute, "???": 5 * time.Minute, "1d": 5 * time.Minute, "1y": 5 * time.Minute, "1w": 5 * time.Minute, } for tt, expect := range cases { t.Run(tt, func(t *testing.T) { t.Setenv("OLLAMA_KEEP_ALIVE", tt) if actual := KeepAlive(); actual != expect { t.Errorf("%s: expected %s, got %s", tt, expect, actual) } }) } } func TestLoadTimeout(t *testing.T) { defaultTimeout := 5 * time.Minute cases := map[string]time.Duration{ "": defaultTimeout, "1s": time.Second, "1m": time.Minute, "1h": time.Hour, "5m0s": defaultTimeout, "1h2m3s": 1*time.Hour + 2*time.Minute + 3*time.Second, "0": time.Duration(math.MaxInt64), "60": 60 * time.Second, "120": 2 * time.Minute, "3600": time.Hour, "-0": time.Duration(math.MaxInt64), "-1": time.Duration(math.MaxInt64), "-1m": time.Duration(math.MaxInt64), // invalid values " ": defaultTimeout, "???": defaultTimeout, "1d": defaultTimeout, "1y": defaultTimeout, "1w": defaultTimeout, } for tt, expect := range cases { t.Run(tt, func(t *testing.T) { t.Setenv("OLLAMA_LOAD_TIMEOUT", tt) if actual := LoadTimeout(); actual != expect { t.Errorf("%s: expected %s, got %s", tt, expect, actual) } }) } } func TestVar(t *testing.T) { cases := map[string]string{ "value": "value", " value ": "value", " 'value' ": "value", ` "value" `: "value", " ' value ' ": " value ", ` " value " `: " value ", } for k, v := range cases { t.Run(k, func(t *testing.T) { t.Setenv("OLLAMA_VAR", k) if s := Var("OLLAMA_VAR"); s != v { t.Errorf("%s: expected %q, got %q", k, v, s) } }) } } func TestContextLength(t *testing.T) { cases := map[string]uint{ "": 4096, "2048": 2048, } for k, v := range cases { t.Run(k, func(t *testing.T) { t.Setenv("OLLAMA_CONTEXT_LENGTH", k) if i := ContextLength(); i != v { t.Errorf("%s: expected %d, got %d", k, v, i) } }) } } func TestLogLevel(t *testing.T) { cases := map[string]slog.Level{ // Default to INFO "": slog.LevelInfo, "false": slog.LevelInfo, "f": slog.LevelInfo, "0": slog.LevelInfo, // True values enable Debug "true": slog.LevelDebug, "t": slog.LevelDebug, // Positive values increase verbosity "1": slog.LevelDebug, "2": logutil.LevelTrace, // Negative values decrease verbosity "-1": slog.LevelWarn, "-2": slog.LevelError, } for k, v := range cases { t.Run(k, func(t *testing.T) { t.Setenv("OLLAMA_DEBUG", k) if i := LogLevel(); i != v { t.Errorf("%s: expected %d, got %d", k, v, i) } }) } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/convert/convert_mllama.go
convert/convert_mllama.go
package convert import ( "strings" "github.com/ollama/ollama/fs/ggml" "github.com/pdevine/tensor" "github.com/pdevine/tensor/native" ) type mllamaModel struct { ModelParameters TextModel struct { llamaModel CrossAttentionLayers []int32 `json:"cross_attention_layers"` } `json:"text_config"` VisionModel struct { NumHiddenLayers uint32 `json:"num_hidden_layers"` NumGlobalLayers uint32 `json:"num_global_layers"` IntermediateLayersIndices []int32 `json:"intermediate_layers_indices"` HiddenSize uint32 `json:"hidden_size"` IntermediateSize uint32 `json:"intermediate_size"` AttentionHeads uint32 `json:"attention_heads"` ImageSize uint32 `json:"image_size"` PatchSize uint32 `json:"patch_size"` NumChannels uint32 `json:"num_channels"` MaxNumTiles uint32 `json:"max_num_tiles"` NormEpsilon float32 `json:"norm_eps"` RopeTheta float32 `json:"rope.freq_base"` } `json:"vision_config"` } func (m *mllamaModel) KV(t *Tokenizer) ggml.KV { kv := m.ModelParameters.KV(t) kv["general.architecture"] = "mllama" for k, v := range m.TextModel.KV(t) { if strings.HasPrefix(k, "llama.") { kv[strings.ReplaceAll(k, "llama.", "mllama.")] = v } } kv["mllama.attention.cross_attention_layers"] = m.TextModel.CrossAttentionLayers kv["mllama.vision.block_count"] = m.VisionModel.NumHiddenLayers kv["mllama.vision.global.block_count"] = m.VisionModel.NumGlobalLayers kv["mllama.vision.intermediate_layers_indices"] = m.VisionModel.IntermediateLayersIndices kv["mllama.vision.embedding_length"] = m.VisionModel.HiddenSize kv["mllama.vision.feed_forward_length"] = m.VisionModel.IntermediateSize kv["mllama.vision.attention.head_count"] = m.VisionModel.AttentionHeads kv["mllama.vision.attention.layer_norm_epsilon"] = m.VisionModel.NormEpsilon kv["mllama.vision.image_size"] = m.VisionModel.ImageSize kv["mllama.vision.patch_size"] = m.VisionModel.PatchSize kv["mllama.vision.max_num_tiles"] = m.VisionModel.MaxNumTiles kv["mllama.vision.num_channels"] = m.VisionModel.NumChannels return kv } func (m *mllamaModel) Replacements() []string { return append( m.TextModel.Replacements(), "language_model.", "", "gate_attn", "attn_gate", "gate_ffn", "ffn_gate", "cross_attn.", "cross_attn_", "vision_model", "v", "class_embedding", "class_embd", "patch_embedding", "patch_embd", "gated_positional_embedding.tile_embedding", "tile_position_embd", "gated_positional_embedding.embedding", "position_embd.weight", "gated_positional_embedding", "position_embd", "embedding.weight", "weight", "pre_tile_positional_embedding", "pre_tile_position_embd", "post_tile_positional_embedding", "post_tile_position_embd", "layernorm_pre", "pre_ln", "layernorm_post", "post_ln", "global_transformer.layers", "global.blk", "transformer.layers", "blk", "mlp.fc1", "ffn_up", "mlp.fc2", "ffn_down", "multi_modal_projector", "mm.0", ) } func (m *mllamaModel) Tensors(ts []Tensor) []*ggml.Tensor { var out []*ggml.Tensor var text []Tensor for _, t := range ts { if !strings.HasPrefix(t.Name(), "v.") && !strings.HasPrefix(t.Name(), "mm.") { text = append(text, t) } else if t.Name() == "v.position_embd.gate" { for _, name := range []string{"v.position_embd.gate", "v.tile_position_embd.gate"} { tt := t.Clone() tt.SetRepacker(m.repack(name)) out = append(out, &ggml.Tensor{ Name: name, Kind: t.Kind(), Shape: t.Shape(), WriterTo: tt, }) } } else { if t.Name() == "v.pre_tile_position_embd.gate" || t.Name() == "v.post_tile_position_embd.gate" { t.SetRepacker(m.repack(t.Name())) } else if strings.HasSuffix(t.Name(), "attn_q.weight") || strings.HasSuffix(t.Name(), "attn_k.weight") { t.SetRepacker(m.repack(t.Name())) } else if strings.HasSuffix(t.Name(), "attn_gate") || strings.HasSuffix(t.Name(), "ffn_gate") { t.SetRepacker(m.repack(t.Name())) } out = append(out, &ggml.Tensor{ Name: t.Name(), Kind: t.Kind(), Shape: t.Shape(), WriterTo: t, }) } } return append(out, m.TextModel.Tensors(text)...) } func (m *mllamaModel) repack(name string) Repacker { return func(_ string, data []float32, shape []uint64) (_ []float32, err error) { dims := make([]int, len(shape)) for i, dim := range shape { dims[i] = int(dim) } var t tensor.Tensor = tensor.New(tensor.WithShape(dims...), tensor.WithBacking(data)) if strings.HasSuffix(name, "attn_q.weight") || strings.HasSuffix(name, "attn_k.weight") { heads := m.VisionModel.AttentionHeads if err := t.Reshape(append([]int{int(heads), 2, dims[0] / int(heads) / 2}, dims[1:]...)...); err != nil { return nil, err } if err := t.T(0, 2, 1, 3); err != nil { return nil, err } if err := t.Reshape(dims...); err != nil { return nil, err } if err := t.Transpose(); err != nil { return nil, err } } else { t, err = tensor.Tanh(t) if err != nil { return nil, err } if name == "v.position_embd.gate" { t, err = tensor.Sub(float32(1), t) if err != nil { return nil, err } } } t = tensor.Materialize(t) // flatten tensor so it can be return as a vector if err := t.Reshape(t.Shape().TotalSize()); err != nil { return nil, err } return native.VectorF32(t.(*tensor.Dense)) } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/convert/reader_torch.go
convert/reader_torch.go
package convert import ( "io" "io/fs" "strings" "github.com/nlpodyssey/gopickle/pytorch" "github.com/nlpodyssey/gopickle/types" ) func parseTorch(fsys fs.FS, replacer *strings.Replacer, ps ...string) ([]Tensor, error) { var ts []Tensor for _, p := range ps { pt, err := pytorch.Load(p) if err != nil { return nil, err } for _, k := range pt.(*types.Dict).Keys() { t := pt.(*types.Dict).MustGet(k) var shape []uint64 for dim := range t.(*pytorch.Tensor).Size { shape = append(shape, uint64(dim)) } ts = append(ts, torch{ storage: t.(*pytorch.Tensor).Source, tensorBase: &tensorBase{ name: replacer.Replace(k.(string)), shape: shape, }, }) } } return ts, nil } type torch struct { storage pytorch.StorageInterface *tensorBase } func (t torch) Clone() Tensor { return torch{ storage: t.storage, tensorBase: &tensorBase{ name: t.name, shape: t.shape, repacker: t.repacker, }, } } func (pt torch) WriteTo(w io.Writer) (int64, error) { return 0, nil }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/convert/convert_mixtral.go
convert/convert_mixtral.go
package convert import ( "fmt" "github.com/ollama/ollama/fs/ggml" ) type mixtralModel struct { llamaModel NumLocalExperts uint32 `json:"num_local_experts"` NumExpertsPerToken uint32 `json:"num_experts_per_tok"` } func (p *mixtralModel) KV(t *Tokenizer) ggml.KV { kv := p.llamaModel.KV(t) if p.NumLocalExperts > 0 { kv["llama.expert_count"] = p.NumLocalExperts } if p.NumExpertsPerToken > 0 { kv["llama.expert_used_count"] = p.NumExpertsPerToken } return kv } func (p *mixtralModel) Tensors(ts []Tensor) []*ggml.Tensor { merges := make([]merge, 0, p.NumHiddenLayers*6) for i := range p.NumHiddenLayers { merges = append(merges, merge{ fmt.Sprintf("blk.%d.*.w1.weight", i), fmt.Sprintf("blk.%d.ffn_gate_exps.weight", i), }, merge{ fmt.Sprintf("blk.%d.*.w1.bias", i), fmt.Sprintf("blk.%d.ffn_gate_exps.bias", i), }, merge{ fmt.Sprintf("blk.%d.*.w2.weight", i), fmt.Sprintf("blk.%d.ffn_up_exps.weight", i), }, merge{ fmt.Sprintf("blk.%d.*.w2.bias", i), fmt.Sprintf("blk.%d.ffn_up_exps.bias", i), }, merge{ fmt.Sprintf("blk.%d.*.w3.weight", i), fmt.Sprintf("blk.%d.ffn_down_exps.weight", i), }, merge{ fmt.Sprintf("blk.%d.*.w3.bias", i), fmt.Sprintf("blk.%d.ffn_down_exps.bias", i), }) } out, ts := mergeTensors(ts, merges...) return append(out, p.llamaModel.Tensors(ts)...) } func (p *mixtralModel) Replacements() []string { return append( p.llamaModel.Replacements(), "model.layers", "blk", "block_sparse_moe.gate", "ffn_gate_inp", "block_sparse_moe.experts.", ".", ) }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/convert/convert_deepseek2.go
convert/convert_deepseek2.go
package convert import ( "cmp" "fmt" "log/slog" "regexp" "strconv" "github.com/ollama/ollama/fs/ggml" ) type deepseek2Model struct { ModelParameters // architectures, vocab_size MaxPositionEmbeddings uint32 `json:"max_position_embeddings"` HiddenSize uint32 `json:"hidden_size"` HiddenLayers uint32 `json:"num_hidden_layers"` IntermediateSize uint32 `json:"intermediate_size"` NumAttentionHeads uint32 `json:"num_attention_heads"` NumKeyValueHeads uint32 `json:"num_key_value_heads"` RMSNormEPS float32 `json:"rms_norm_eps"` RopeTheta float32 `json:"rope_theta"` QKNopeHeadDim uint32 `json:"qk_nope_head_dim"` QKRopeHeadDim uint32 `json:"qk_rope_head_dim"` KVLoraRank uint32 `json:"kv_lora_rank"` QLoraRank uint32 `json:"q_lora_rank"` VHeadDim uint32 `json:"v_head_dim"` ExpertCount uint32 `json:"n_routed_experts"` ExpertSharedCount uint32 `json:"n_shared_experts"` ExpertIntermediateSize uint32 `json:"moe_intermediate_size"` ExpertUsedCount uint32 `json:"num_experts_per_tok"` ExpertWeightsNorm bool `json:"norm_topk_prob"` ExpertWeightsScale float32 `json:"routed_scaling_factor"` ScoringFunc string `json:"scoring_func"` LeadingDenseBlockCount uint32 `json:"first_k_dense_replace"` RopeScaling struct { Factor float32 `json:"factor"` OriginalMaxPositionEmbeddings uint32 `json:"original_max_position_embeddings"` Type string `json:"type"` MScaleAllDim float32 `json:"mscale_all_dim"` } `json:"rope_scaling"` Architecture string } func (p *deepseek2Model) KV(t *Tokenizer) ggml.KV { kv := p.ModelParameters.KV(t) kv["general.architecture"] = "deepseek2" kv["general.type"] = "model" kv["deepseek2.block_count"] = p.HiddenLayers numHeads := p.NumAttentionHeads numKVHeads := p.NumKeyValueHeads kv["deepseek2.attention.head_count"] = numHeads kv["deepseek2.attention.head_count_kv"] = numKVHeads kv["deepseek2.attention.key_length"] = p.QKNopeHeadDim + p.QKRopeHeadDim kv["deepseek2.attention.kv_lora_rank"] = p.KVLoraRank kv["deepseek2.attention.layer_norm_rms_epsilon"] = p.RMSNormEPS kv["deepseek2.attention.q_lora_rank"] = p.QLoraRank kv["deepseek2.attention.value_length"] = p.VHeadDim kv["deepseek2.context_length"] = p.MaxPositionEmbeddings kv["deepseek2.embedding_length"] = p.HiddenSize kv["deepseek2.expert_count"] = p.ExpertCount kv["deepseek2.expert_feed_forward_length"] = p.ExpertIntermediateSize kv["deepseek2.expert_shared_count"] = p.ExpertSharedCount var scoringFunc uint32 switch p.ScoringFunc { case "softmax": // not currently supported in the model, but needed for Deepseek-OCR scoringFunc = 1 case "sigmoid": scoringFunc = 2 } kv["deepseek2.expert_gating_func"] = scoringFunc kv["deepseek2.expert_used_count"] = p.ExpertUsedCount kv["deepseek2.expert_weights_norm"] = p.ExpertWeightsNorm kv["deepseek2.expert_weights_scale"] = p.ExpertWeightsScale kv["deepseek2.feed_forward_length"] = p.IntermediateSize kv["deepseek2.leading_dense_block_count"] = p.LeadingDenseBlockCount kv["deepseek2.rope.dimension_count"] = p.QKRopeHeadDim kv["deepseek2.rope.freq_base"] = cmp.Or(p.RopeTheta, 10000.0) kv["deepseek2.rope.scaling.factor"] = p.RopeScaling.Factor kv["deepseek2.rope.scaling.original_context_length"] = p.RopeScaling.OriginalMaxPositionEmbeddings kv["deepseek2.rope.scaling.type"] = p.RopeScaling.Type kv["deepseek2.rope.scaling.yarn_log_multiplier"] = 0.1 * p.RopeScaling.MScaleAllDim kv["tokenizer.ggml.pre"] = "deepseek-v3" return kv } func (p *deepseek2Model) Replacements() []string { return []string{ "lm_head", "output", "model.embed_tokens", "token_embd", "model.norm", "output_norm", "language_model.", "", "model.layers", "blk", "input_layernorm", "attn_norm", "self_attn.kv_a_proj_with_mqa", "attn_kv_a_mqa", "self_attn.kv_a_layernorm", "attn_kv_a_norm", "self_attn.kv_b_proj", "attn_kv_b", "self_attn.q_a_proj", "attn_q_a", "self_attn.q_a_layernorm", "attn_q_a_norm", "self_attn.q_b_proj", "attn_q_b", "self_attn.o_proj", "attn_output", "post_attention_layernorm", "ffn_norm", "mlp.shared_experts.down_proj", "ffn_down_shexp", "mlp.shared_experts.gate_proj", "ffn_gate_shexp", "mlp.shared_experts.up_proj", "ffn_up_shexp", "mlp.gate_proj", "ffn_gate", "mlp.down_proj", "ffn_down", "mlp.up_proj", "ffn_up", "mlp.gate.e_score_correction_bias", "exp_probs_b.bias", "mlp.gate", "ffn_gate_inp", } } func (p *deepseek2Model) Tensors(s []Tensor) (out []*ggml.Tensor) { merges := make([]merge, p.HiddenLayers*3) for i := range p.HiddenLayers { merges[i*3+0] = merge{ fmt.Sprintf("blk.%d.mlp.experts.*.gate_proj.weight", i), fmt.Sprintf("blk.%d.ffn_gate_exps.weight", i), } merges[i*3+1] = merge{ fmt.Sprintf("blk.%d.mlp.experts.*.up_proj.weight", i), fmt.Sprintf("blk.%d.ffn_up_exps.weight", i), } merges[i*3+2] = merge{ fmt.Sprintf("blk.%d.mlp.experts.*.down_proj.weight", i), fmt.Sprintf("blk.%d.ffn_down_exps.weight", i), } } skipLayer := func(n string, minValue uint32) bool { re := regexp.MustCompile(`^blk\.(\d+)`) matches := re.FindStringSubmatch(n) if matches == nil { return false } blkNum, err := strconv.Atoi(matches[1]) if err != nil { return false } return uint32(blkNum) >= minValue } out, s = mergeTensors(s, merges...) for _, t := range s { // skip any additional layers (such as the Multi-Token Prediction layer) if skipLayer(t.Name(), p.HiddenLayers) { slog.Debug("skipping layer", "name", t.Name()) continue } out = append(out, &ggml.Tensor{ Name: t.Name(), Kind: t.Kind(), Shape: t.Shape(), WriterTo: t, }) } return out }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/convert/reader_safetensors.go
convert/reader_safetensors.go
package convert import ( "bufio" "bytes" "encoding/binary" "encoding/json" "errors" "fmt" "io" "io/fs" "maps" "slices" "strings" "github.com/d4l3k/go-bfloat16" "github.com/x448/float16" ) type safetensorMetadata struct { Type string `json:"dtype"` Shape []uint64 `json:"shape"` Offsets []int64 `json:"data_offsets"` } func parseSafetensors(fsys fs.FS, replacer *strings.Replacer, ps ...string) ([]Tensor, error) { var ts []Tensor for _, p := range ps { f, err := fsys.Open(p) if err != nil { return nil, err } defer f.Close() var n int64 if err := binary.Read(f, binary.LittleEndian, &n); err != nil { return nil, err } b := bytes.NewBuffer(make([]byte, 0, n)) if _, err = io.CopyN(b, f, n); err != nil { return nil, err } var headers map[string]safetensorMetadata if err := json.NewDecoder(b).Decode(&headers); err != nil { return nil, err } keys := slices.Sorted(maps.Keys(headers)) names := make(map[string]struct{}, len(keys)) for _, key := range keys { if value := headers[key]; value.Type != "" { // bitsandbytes quantized models are unsupported if len(value.Shape) == 0 { return nil, errors.New("unsupported safetensors model") } ggufName := replacer.Replace(key) if _, ok := names[ggufName]; ok { return nil, fmt.Errorf("duplicate tensor name '%s' was found for this model", ggufName) } names[ggufName] = struct{}{} ts = append(ts, safetensor{ fs: fsys, path: p, dtype: value.Type, offset: safetensorsPad(n, value.Offsets[0]), size: safetensorsPad(n, value.Offsets[1]) - safetensorsPad(n, value.Offsets[0]), tensorBase: &tensorBase{ name: ggufName, shape: value.Shape, }, }) } } } return ts, nil } // safetensorsPad returns the padded size of the safetensors file given a length n and offset s func safetensorsPad(n, offset int64) int64 { return 8 + n + offset } type safetensor struct { fs fs.FS path string dtype string offset int64 size int64 *tensorBase } func (st safetensor) Kind() uint32 { kind := st.tensorBase.Kind() if st.dtype == "BF16" && !strings.HasPrefix(st.name, "v.") && !strings.HasPrefix(st.name, "s.") && kind != tensorKindFP32 { kind = tensorKindBF16 } return kind } func (st safetensor) Clone() Tensor { return &safetensor{ fs: st.fs, path: st.path, dtype: st.dtype, offset: st.offset, size: st.size, tensorBase: &tensorBase{ name: st.name, repacker: st.repacker, shape: slices.Clone(st.shape), }, } } func (st safetensor) WriteTo(w io.Writer) (int64, error) { f, err := st.fs.Open(st.path) if err != nil { return 0, err } defer f.Close() r, err := func() (io.Reader, error) { if readerAt, ok := f.(io.ReaderAt); ok { return io.NewSectionReader(readerAt, st.offset, st.size), nil } else if seeker, ok := f.(io.Seeker); ok { _, err := seeker.Seek(st.offset, io.SeekStart) return f, err } else { _, err := io.CopyN(io.Discard, f, st.offset) return f, err } }() if err != nil { return 0, err } br := bufio.NewReaderSize(r, min(32<<10, int(st.size))) // special case when input and output are same type and the // tensor doesn't need repacking if (st.repacker == nil) && ((st.dtype == "F32" && st.Kind() == tensorKindFP32) || (st.dtype == "F16" && st.Kind() == tensorKindFP16) || (st.dtype == "U8")) { return io.CopyN(w, br, st.size) } var f32s []float32 switch st.dtype { case "F32": f32s = make([]float32, st.size/4) if err = binary.Read(br, binary.LittleEndian, f32s); err != nil { return 0, err } case "F16": u16s := make([]uint16, st.size/2) if err = binary.Read(br, binary.LittleEndian, u16s); err != nil { return 0, err } f32s = make([]float32, len(u16s)) for i := range u16s { f32s[i] = float16.Frombits(u16s[i]).Float32() } case "BF16": u8s := make([]uint8, st.size) if err = binary.Read(br, binary.LittleEndian, u8s); err != nil { return 0, err } f32s = bfloat16.DecodeFloat32(u8s) default: return 0, fmt.Errorf("unknown data type: %s", st.dtype) } if st.repacker != nil { f32s, err = st.repacker(st.Name(), f32s, st.Shape()) if err != nil { return 0, err } } switch st.Kind() { case tensorKindFP32: return int64(len(f32s) * 4), binary.Write(w, binary.LittleEndian, f32s) case tensorKindFP16: f16s := make([]uint16, len(f32s)) for i := range f32s { f16s[i] = float16.Fromfloat32(f32s[i]).Bits() } return int64(len(f16s) * 2), binary.Write(w, binary.LittleEndian, f16s) case tensorKindBF16: u8s := bfloat16.EncodeFloat32(f32s) return int64(len(u8s)), binary.Write(w, binary.LittleEndian, u8s) default: return 0, fmt.Errorf("unknown storage type: %d", st.Kind()) } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/convert/convert_gemma2.go
convert/convert_gemma2.go
package convert import "github.com/ollama/ollama/fs/ggml" type gemma2Model struct { gemmaModel SlidingWindow uint32 `json:"sliding_window"` AttentionLogitSoftcap float32 `json:"attn_logit_softcapping"` FinalLogitSoftcap float32 `json:"final_logit_softcapping"` } func (p *gemma2Model) KV(t *Tokenizer) ggml.KV { kv := p.ModelParameters.KV(t) kv["general.architecture"] = "gemma2" kv["gemma2.context_length"] = p.MaxPositionEmbeddings kv["gemma2.embedding_length"] = p.HiddenSize kv["gemma2.block_count"] = p.HiddenLayers kv["gemma2.feed_forward_length"] = p.IntermediateSize kv["gemma2.attention.head_count"] = p.NumAttentionHeads kv["gemma2.attention.head_count_kv"] = p.NumKeyValueHeads kv["gemma2.attention.layer_norm_rms_epsilon"] = p.RMSNormEPS kv["gemma2.attention.key_length"] = p.HeadDim kv["gemma2.attention.value_length"] = p.HeadDim kv["gemma2.attention.sliding_window"] = p.SlidingWindow kv["gemma2.attn_logit_softcapping"] = p.AttentionLogitSoftcap kv["gemma2.final_logit_softcapping"] = p.FinalLogitSoftcap kv["tokenizer.ggml.eot_token_id"] = uint32(107) kv["tokenizer.ggml.middle_token_id"] = uint32(68) kv["tokenizer.ggml.prefix_token_id"] = uint32(67) kv["tokenizer.ggml.suffix_token_id"] = uint32(69) return kv } func (p *gemma2Model) Replacements() []string { return []string{ "model.embed_tokens", "token_embd", "model.norm", "output_norm", "model.layers", "blk", "input_layernorm", "attn_norm", "self_attn.q_proj", "attn_q", "self_attn.k_proj", "attn_k", "self_attn.v_proj", "attn_v", "self_attn.o_proj", "attn_output", "mlp.gate_proj", "ffn_gate", "mlp.down_proj", "ffn_down", "mlp.up_proj", "ffn_up", "post_attention_layernorm", "post_attention_norm", "pre_feedforward_layernorm", "ffn_norm", "post_feedforward_layernorm", "post_ffw_norm", } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/convert/tensor_test.go
convert/tensor_test.go
package convert import ( "bytes" "encoding/binary" "fmt" "io" "iter" "math/rand/v2" "slices" "strings" "testing" "github.com/google/go-cmp/cmp" "github.com/ollama/ollama/fs/ggml" "github.com/pdevine/tensor" ) type fakeTensor struct { name string shape []uint64 data []float32 repacker Repacker } func (f fakeTensor) Name() string { return f.name } func (f fakeTensor) Shape() []uint64 { return f.shape } func (f fakeTensor) Kind() uint32 { return 0 } func (f *fakeTensor) SetRepacker(fn Repacker) { f.repacker = fn } func (f fakeTensor) Clone() Tensor { return &fakeTensor{ name: f.name, shape: slices.Clone(f.shape), data: slices.Clone(f.data), repacker: f.repacker, } } func (f fakeTensor) WriteTo(w io.Writer) (n int64, err error) { data := f.data if f.repacker != nil { data, err = f.repacker(f.name, data, f.shape) if err != nil { return 0, err } } if err := binary.Write(w, binary.LittleEndian, data); err != nil { return 0, err } return int64(len(data) * 4), nil } func mul(shape []uint64) int { n := 1 for _, dim := range shape { n *= int(dim) } return n } func TestSplitDim(t *testing.T) { t.Run("2d", func(t *testing.T) { r := fakeTensor{ name: "a.b", shape: []uint64{3, 4}, data: []float32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, } t.Run("no split", func(t *testing.T) { for tt := range splitDim(&r, 0, split{Replacer: strings.NewReplacer("a", "x")}) { if tt.Name != "x.b" { t.Fatalf("expected name 'x', got '%s'", tt.Name) } if diff := cmp.Diff(tt.Shape, []uint64{3, 4}); diff != "" { t.Errorf("unexpected shape (-want +got):\n%s", diff) } var b bytes.Buffer if _, err := tt.WriteTo(&b); err != nil { t.Fatal(err) } f32s := make([]float32, mul(tt.Shape)) if err := binary.Read(&b, binary.LittleEndian, &f32s); err != nil { t.Fatal(err) } if diff := cmp.Diff(f32s, []float32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}); diff != "" { t.Errorf("unexpected data (-want +got):\n%s", diff) } } }) t.Run("even split", func(t *testing.T) { next, stop := iter.Pull(splitDim(&r, 1, split{Replacer: strings.NewReplacer("a", "x")}, split{Replacer: strings.NewReplacer("b", "y")}, )) defer stop() { tt, ok := next() if !ok { t.Fatal("expected at least one split") } if tt.Name != "x.b" { t.Fatal("expected name 'x.b', got", tt.Name) } if diff := cmp.Diff(tt.Shape, []uint64{3, 2}); diff != "" { t.Errorf("unexpected shape (-want +got):\n%s", diff) } var b bytes.Buffer if _, err := tt.WriteTo(&b); err != nil { t.Fatal(err) } f32s := make([]float32, mul(tt.Shape)) if err := binary.Read(&b, binary.LittleEndian, &f32s); err != nil { t.Fatal(err) } if diff := cmp.Diff(f32s, []float32{0, 1, 4, 5, 8, 9}); diff != "" { t.Errorf("unexpected data (-want +got):\n%s", diff) } } { tt, ok := next() if !ok { t.Fatal("expected at least one split") } if tt.Name != "a.y" { t.Fatal("expected name 'a.y', got", tt.Name) } if diff := cmp.Diff(tt.Shape, []uint64{3, 2}); diff != "" { t.Errorf("unexpected shape (-want +got):\n%s", diff) } var b bytes.Buffer if _, err := tt.WriteTo(&b); err != nil { t.Fatal(err) } f32s := make([]float32, mul(tt.Shape)) if err := binary.Read(&b, binary.LittleEndian, &f32s); err != nil { t.Fatal(err) } if diff := cmp.Diff(f32s, []float32{2, 3, 6, 7, 10, 11}); diff != "" { t.Errorf("unexpected data (-want +got):\n%s", diff) } } }) t.Run("uneven split", func(t *testing.T) { next, stop := iter.Pull(splitDim(&r, 0, split{Replacer: strings.NewReplacer("a", "x"), dim: 2}, split{Replacer: strings.NewReplacer("b", "y"), dim: 1}, )) defer stop() { tt, ok := next() if !ok { t.Fatal("expected at least one split") } if tt.Name != "x.b" { t.Fatal("expected name 'x.b', got", tt.Name) } if diff := cmp.Diff(tt.Shape, []uint64{2, 4}); diff != "" { t.Errorf("unexpected shape (-want +got):\n%s", diff) } var b bytes.Buffer if _, err := tt.WriteTo(&b); err != nil { t.Fatal(err) } f32s := make([]float32, mul(tt.Shape)) if err := binary.Read(&b, binary.LittleEndian, &f32s); err != nil { t.Fatal(err) } if diff := cmp.Diff(f32s, []float32{0, 1, 2, 3, 4, 5, 6, 7}); diff != "" { t.Errorf("unexpected data (-want +got):\n%s", diff) } } { tt, ok := next() if !ok { t.Fatal("expected at least one split") } if tt.Name != "a.y" { t.Fatal("expected name 'a.y', got", tt.Name) } if diff := cmp.Diff(tt.Shape, []uint64{1, 4}); diff != "" { t.Errorf("unexpected shape (-want +got):\n%s", diff) } var b bytes.Buffer if _, err := tt.WriteTo(&b); err != nil { t.Fatal(err) } f32s := make([]float32, mul(tt.Shape)) if err := binary.Read(&b, binary.LittleEndian, &f32s); err != nil { t.Fatal(err) } if diff := cmp.Diff(f32s, []float32{8, 9, 10, 11}); diff != "" { t.Errorf("unexpected data (-want +got):\n%s", diff) } } }) t.Run("three way split", func(t *testing.T) { next, stop := iter.Pull(splitDim(&r, 0, split{Replacer: strings.NewReplacer("a", "x"), dim: 1}, split{Replacer: strings.NewReplacer("b", "y"), dim: 1}, split{Replacer: strings.NewReplacer("b", "z"), dim: 1}, )) defer stop() { tt, ok := next() if !ok { t.Fatal("expected at least one split") } if tt.Name != "x.b" { t.Fatal("expected name 'x.b', got", tt.Name) } if diff := cmp.Diff(tt.Shape, []uint64{1, 4}); diff != "" { t.Errorf("unexpected shape (-want +got):\n%s", diff) } var b bytes.Buffer if _, err := tt.WriteTo(&b); err != nil { t.Fatal(err) } f32s := make([]float32, mul(tt.Shape)) if err := binary.Read(&b, binary.LittleEndian, &f32s); err != nil { t.Fatal(err) } if diff := cmp.Diff(f32s, []float32{0, 1, 2, 3}); diff != "" { t.Errorf("unexpected data (-want +got):\n%s", diff) } } { tt, ok := next() if !ok { t.Fatal("expected at least one split") } if tt.Name != "a.y" { t.Fatal("expected name 'x.b', got", tt.Name) } if diff := cmp.Diff(tt.Shape, []uint64{1, 4}); diff != "" { t.Errorf("unexpected shape (-want +got):\n%s", diff) } var b bytes.Buffer if _, err := tt.WriteTo(&b); err != nil { t.Fatal(err) } f32s := make([]float32, mul(tt.Shape)) if err := binary.Read(&b, binary.LittleEndian, &f32s); err != nil { t.Fatal(err) } if diff := cmp.Diff(f32s, []float32{4, 5, 6, 7}); diff != "" { t.Errorf("unexpected data (-want +got):\n%s", diff) } } { tt, ok := next() if !ok { t.Fatal("expected at least one split") } if tt.Name != "a.z" { t.Fatal("expected name 'x.b', got", tt.Name) } if diff := cmp.Diff(tt.Shape, []uint64{1, 4}); diff != "" { t.Errorf("unexpected shape (-want +got):\n%s", diff) } var b bytes.Buffer if _, err := tt.WriteTo(&b); err != nil { t.Fatal(err) } f32s := make([]float32, mul(tt.Shape)) if err := binary.Read(&b, binary.LittleEndian, &f32s); err != nil { t.Fatal(err) } if diff := cmp.Diff(f32s, []float32{8, 9, 10, 11}); diff != "" { t.Errorf("unexpected data (-want +got):\n%s", diff) } } }) t.Run("uneven three way split", func(t *testing.T) { next, stop := iter.Pull(splitDim(&r, 1, split{Replacer: strings.NewReplacer("a", "x"), dim: 2}, split{Replacer: strings.NewReplacer("b", "y"), dim: 1}, split{Replacer: strings.NewReplacer("b", "z"), dim: 1}, )) defer stop() { tt, ok := next() if !ok { t.Fatal("expected at least one split") } if tt.Name != "x.b" { t.Fatal("expected name 'x.b', got", tt.Name) } if diff := cmp.Diff(tt.Shape, []uint64{3, 2}); diff != "" { t.Errorf("unexpected shape (-want +got):\n%s", diff) } var b bytes.Buffer if _, err := tt.WriteTo(&b); err != nil { t.Fatal(err) } f32s := make([]float32, mul(tt.Shape)) if err := binary.Read(&b, binary.LittleEndian, &f32s); err != nil { t.Fatal(err) } if diff := cmp.Diff(f32s, []float32{0, 1, 4, 5, 8, 9}); diff != "" { t.Errorf("unexpected data (-want +got):\n%s", diff) } } { tt, ok := next() if !ok { t.Fatal("expected at least one split") } if tt.Name != "a.y" { t.Fatal("expected name 'x.b', got", tt.Name) } if diff := cmp.Diff(tt.Shape, []uint64{3, 1}); diff != "" { t.Errorf("unexpected shape (-want +got):\n%s", diff) } var b bytes.Buffer if _, err := tt.WriteTo(&b); err != nil { t.Fatal(err) } f32s := make([]float32, mul(tt.Shape)) if err := binary.Read(&b, binary.LittleEndian, &f32s); err != nil { t.Fatal(err) } if diff := cmp.Diff(f32s, []float32{2, 6, 10}); diff != "" { t.Errorf("unexpected data (-want +got):\n%s", diff) } } { tt, ok := next() if !ok { t.Fatal("expected at least one split") } if tt.Name != "a.z" { t.Fatal("expected name 'x.b', got", tt.Name) } if diff := cmp.Diff(tt.Shape, []uint64{3, 1}); diff != "" { t.Errorf("unexpected shape (-want +got):\n%s", diff) } var b bytes.Buffer if _, err := tt.WriteTo(&b); err != nil { t.Fatal(err) } f32s := make([]float32, mul(tt.Shape)) if err := binary.Read(&b, binary.LittleEndian, &f32s); err != nil { t.Fatal(err) } if diff := cmp.Diff(f32s, []float32{3, 7, 11}); diff != "" { t.Errorf("unexpected data (-want +got):\n%s", diff) } } }) t.Run("split with transpose", func(t *testing.T) { next, stop := iter.Pull(splitDim(&r, 1, split{Replacer: strings.NewReplacer("a", "x")}, split{Replacer: strings.NewReplacer("b", "y"), afterFunc: func(tt tensor.Tensor) (tensor.Tensor, error) { return tensor.Transpose(tt, 1, 0) }}, )) defer stop() { tt, ok := next() if !ok { t.Fatal("expected at least one split") } if tt.Name != "x.b" { t.Fatal("expected name 'x.b', got", tt.Name) } if diff := cmp.Diff(tt.Shape, []uint64{3, 2}); diff != "" { t.Errorf("unexpected shape (-want +got):\n%s", diff) } var b bytes.Buffer if _, err := tt.WriteTo(&b); err != nil { t.Fatal(err) } f32s := make([]float32, mul(tt.Shape)) if err := binary.Read(&b, binary.LittleEndian, &f32s); err != nil { t.Fatal(err) } if diff := cmp.Diff(f32s, []float32{0, 1, 4, 5, 8, 9}); diff != "" { t.Errorf("unexpected data (-want +got):\n%s", diff) } } { tt, ok := next() if !ok { t.Fatal("expected at least one split") } if tt.Name != "a.y" { t.Fatal("expected name 'a.y', got", tt.Name) } if diff := cmp.Diff(tt.Shape, []uint64{3, 2}); diff != "" { t.Errorf("unexpected shape (-want +got):\n%s", diff) } var b bytes.Buffer if _, err := tt.WriteTo(&b); err != nil { t.Fatal(err) } f32s := make([]float32, mul(tt.Shape)) if err := binary.Read(&b, binary.LittleEndian, &f32s); err != nil { t.Fatal(err) } if diff := cmp.Diff(f32s, []float32{2, 6, 10, 3, 7, 11}); diff != "" { t.Errorf("unexpected data (-want +got):\n%s", diff) } } }) }) t.Run("3d", func(t *testing.T) { r := fakeTensor{ name: "a.b", shape: []uint64{3, 4, 2}, data: []float32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}, } t.Run("no split", func(t *testing.T) { for tt := range splitDim(&r, 0, split{Replacer: strings.NewReplacer("a", "x")}) { if tt.Name != "x.b" { t.Fatalf("expected name 'x', got '%s'", tt.Name) } if diff := cmp.Diff(tt.Shape, []uint64{3, 4, 2}); diff != "" { t.Errorf("unexpected shape (-want +got):\n%s", diff) } var b bytes.Buffer if _, err := tt.WriteTo(&b); err != nil { t.Fatal(err) } f32s := make([]float32, mul(tt.Shape)) if err := binary.Read(&b, binary.LittleEndian, &f32s); err != nil { t.Fatal(err) } if diff := cmp.Diff(f32s, []float32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}); diff != "" { t.Errorf("unexpected data (-want +got):\n%s", diff) } } }) t.Run("even split", func(t *testing.T) { next, stop := iter.Pull(splitDim(&r, 1, split{Replacer: strings.NewReplacer("a", "x")}, split{Replacer: strings.NewReplacer("b", "y")}, )) defer stop() { tt, ok := next() if !ok { t.Fatal("expected at least one split") } if tt.Name != "x.b" { t.Fatal("expected name 'x.b', got", tt.Name) } if diff := cmp.Diff(tt.Shape, []uint64{3, 2, 2}); diff != "" { t.Errorf("unexpected shape (-want +got):\n%s", diff) } var b bytes.Buffer if _, err := tt.WriteTo(&b); err != nil { t.Fatal(err) } f32s := make([]float32, mul(tt.Shape)) if err := binary.Read(&b, binary.LittleEndian, &f32s); err != nil { t.Fatal(err) } if diff := cmp.Diff(f32s, []float32{0, 1, 2, 3, 8, 9, 10, 11, 16, 17, 18, 19}); diff != "" { t.Errorf("unexpected data (-want +got):\n%s", diff) } } { tt, ok := next() if !ok { t.Fatal("expected at least one split") } if tt.Name != "a.y" { t.Fatal("expected name 'a.y', got", tt.Name) } if diff := cmp.Diff(tt.Shape, []uint64{3, 2, 2}); diff != "" { t.Errorf("unexpected shape (-want +got):\n%s", diff) } var b bytes.Buffer if _, err := tt.WriteTo(&b); err != nil { t.Fatal(err) } f32s := make([]float32, mul(tt.Shape)) if err := binary.Read(&b, binary.LittleEndian, &f32s); err != nil { t.Fatal(err) } if diff := cmp.Diff(f32s, []float32{4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23}); diff != "" { t.Errorf("unexpected data (-want +got):\n%s", diff) } } }) t.Run("uneven split", func(t *testing.T) { next, stop := iter.Pull(splitDim(&r, 0, split{Replacer: strings.NewReplacer("a", "x"), dim: 2}, split{Replacer: strings.NewReplacer("b", "y"), dim: 1}, )) defer stop() { tt, ok := next() if !ok { t.Fatal("expected at least one split") } if tt.Name != "x.b" { t.Fatal("expected name 'x.b', got", tt.Name) } if diff := cmp.Diff(tt.Shape, []uint64{2, 4, 2}); diff != "" { t.Errorf("unexpected shape (-want +got):\n%s", diff) } var b bytes.Buffer if _, err := tt.WriteTo(&b); err != nil { t.Fatal(err) } f32s := make([]float32, mul(tt.Shape)) if err := binary.Read(&b, binary.LittleEndian, &f32s); err != nil { t.Fatal(err) } if diff := cmp.Diff(f32s, []float32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}); diff != "" { t.Errorf("unexpected data (-want +got):\n%s", diff) } } { tt, ok := next() if !ok { t.Fatal("expected at least one split") } if tt.Name != "a.y" { t.Fatal("expected name 'a.y', got", tt.Name) } if diff := cmp.Diff(tt.Shape, []uint64{1, 4, 2}); diff != "" { t.Errorf("unexpected shape (-want +got):\n%s", diff) } var b bytes.Buffer if _, err := tt.WriteTo(&b); err != nil { t.Fatal(err) } f32s := make([]float32, mul(tt.Shape)) if err := binary.Read(&b, binary.LittleEndian, &f32s); err != nil { t.Fatal(err) } if diff := cmp.Diff(f32s, []float32{16, 17, 18, 19, 20, 21, 22, 23}); diff != "" { t.Errorf("unexpected data (-want +got):\n%s", diff) } } }) t.Run("three way split", func(t *testing.T) { next, stop := iter.Pull(splitDim(&r, 0, split{Replacer: strings.NewReplacer("a", "x"), dim: 1}, split{Replacer: strings.NewReplacer("b", "y"), dim: 1}, split{Replacer: strings.NewReplacer("b", "z"), dim: 1}, )) defer stop() { tt, ok := next() if !ok { t.Fatal("expected at least one split") } if tt.Name != "x.b" { t.Fatal("expected name 'x.b', got", tt.Name) } if diff := cmp.Diff(tt.Shape, []uint64{1, 4, 2}); diff != "" { t.Errorf("unexpected shape (-want +got):\n%s", diff) } var b bytes.Buffer if _, err := tt.WriteTo(&b); err != nil { t.Fatal(err) } f32s := make([]float32, mul(tt.Shape)) if err := binary.Read(&b, binary.LittleEndian, &f32s); err != nil { t.Fatal(err) } if diff := cmp.Diff(f32s, []float32{0, 1, 2, 3, 4, 5, 6, 7}); diff != "" { t.Errorf("unexpected data (-want +got):\n%s", diff) } } { tt, ok := next() if !ok { t.Fatal("expected at least one split") } if tt.Name != "a.y" { t.Fatal("expected name 'x.b', got", tt.Name) } if diff := cmp.Diff(tt.Shape, []uint64{1, 4, 2}); diff != "" { t.Errorf("unexpected shape (-want +got):\n%s", diff) } var b bytes.Buffer if _, err := tt.WriteTo(&b); err != nil { t.Fatal(err) } f32s := make([]float32, mul(tt.Shape)) if err := binary.Read(&b, binary.LittleEndian, &f32s); err != nil { t.Fatal(err) } if diff := cmp.Diff(f32s, []float32{8, 9, 10, 11, 12, 13, 14, 15}); diff != "" { t.Errorf("unexpected data (-want +got):\n%s", diff) } } { tt, ok := next() if !ok { t.Fatal("expected at least one split") } if tt.Name != "a.z" { t.Fatal("expected name 'x.b', got", tt.Name) } if diff := cmp.Diff(tt.Shape, []uint64{1, 4, 2}); diff != "" { t.Errorf("unexpected shape (-want +got):\n%s", diff) } var b bytes.Buffer if _, err := tt.WriteTo(&b); err != nil { t.Fatal(err) } f32s := make([]float32, mul(tt.Shape)) if err := binary.Read(&b, binary.LittleEndian, &f32s); err != nil { t.Fatal(err) } if diff := cmp.Diff(f32s, []float32{16, 17, 18, 19, 20, 21, 22, 23}); diff != "" { t.Errorf("unexpected data (-want +got):\n%s", diff) } } }) t.Run("uneven three way split", func(t *testing.T) { next, stop := iter.Pull(splitDim(&r, 1, split{Replacer: strings.NewReplacer("a", "x"), dim: 2}, split{Replacer: strings.NewReplacer("b", "y"), dim: 1}, split{Replacer: strings.NewReplacer("b", "z"), dim: 1}, )) defer stop() { tt, ok := next() if !ok { t.Fatal("expected at least one split") } if tt.Name != "x.b" { t.Fatal("expected name 'x.b', got", tt.Name) } if diff := cmp.Diff(tt.Shape, []uint64{3, 2, 2}); diff != "" { t.Errorf("unexpected shape (-want +got):\n%s", diff) } var b bytes.Buffer if _, err := tt.WriteTo(&b); err != nil { t.Fatal(err) } f32s := make([]float32, mul(tt.Shape)) if err := binary.Read(&b, binary.LittleEndian, &f32s); err != nil { t.Fatal(err) } if diff := cmp.Diff(f32s, []float32{0, 1, 2, 3, 8, 9, 10, 11, 16, 17, 18, 19}); diff != "" { t.Errorf("unexpected data (-want +got):\n%s", diff) } } { tt, ok := next() if !ok { t.Fatal("expected at least one split") } if tt.Name != "a.y" { t.Fatal("expected name 'x.b', got", tt.Name) } if diff := cmp.Diff(tt.Shape, []uint64{3, 1, 2}); diff != "" { t.Errorf("unexpected shape (-want +got):\n%s", diff) } var b bytes.Buffer if _, err := tt.WriteTo(&b); err != nil { t.Fatal(err) } f32s := make([]float32, mul(tt.Shape)) if err := binary.Read(&b, binary.LittleEndian, &f32s); err != nil { t.Fatal(err) } if diff := cmp.Diff(f32s, []float32{4, 5, 12, 13, 20, 21}); diff != "" { t.Errorf("unexpected data (-want +got):\n%s", diff) } } { tt, ok := next() if !ok { t.Fatal("expected at least one split") } if tt.Name != "a.z" { t.Fatal("expected name 'x.b', got", tt.Name) } if diff := cmp.Diff(tt.Shape, []uint64{3, 1, 2}); diff != "" { t.Errorf("unexpected shape (-want +got):\n%s", diff) } var b bytes.Buffer if _, err := tt.WriteTo(&b); err != nil { t.Fatal(err) } f32s := make([]float32, mul(tt.Shape)) if err := binary.Read(&b, binary.LittleEndian, &f32s); err != nil { t.Fatal(err) } if diff := cmp.Diff(f32s, []float32{6, 7, 14, 15, 22, 23}); diff != "" { t.Errorf("unexpected data (-want +got):\n%s", diff) } } }) }) } func TestMerge(t *testing.T) { unmatched := []Tensor{ &fakeTensor{ name: "a.0.b", shape: []uint64{5, 2}, data: []float32{10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, }, &fakeTensor{ name: "a.1.b", shape: []uint64{5, 2}, data: []float32{20, 21, 22, 23, 24, 25, 26, 27, 28, 29}, }, &fakeTensor{ name: "c.0.d", shape: []uint64{5, 2}, data: []float32{30, 31, 32, 33, 34, 35, 36, 37, 38, 39}, }, &fakeTensor{ name: "c.1.d", shape: []uint64{5, 2}, data: []float32{40, 41, 42, 43, 44, 45, 46, 47, 48, 49}, }, &fakeTensor{ name: "e.0.f", shape: []uint64{5, 2}, data: []float32{50, 51, 52, 53, 54, 55, 56, 57, 58, 59}, }, } checkMatched := func(t *testing.T, n int, matched []*ggml.Tensor) { for i := range n { got := matched[i] if diff := cmp.Diff([]uint64{2, 5, 2}, got.Shape); diff != "" { t.Errorf("unexpected (-want +got):\n%s", diff) } var b bytes.Buffer if _, err := got.WriteTo(&b); err != nil { t.Fatal(err) } f32s := make([]float32, 20) if err := binary.Read(&b, binary.LittleEndian, &f32s); err != nil { t.Fatal(err) } offset := 10 + (i * 20) want := make([]float32, 20) for j := range 20 { want[j] = float32(offset + j) } if diff := cmp.Diff(want, f32s); diff != "" { t.Errorf("unexpected data (-want +got):\n%s", diff) } } } t.Run("single merge", func(t *testing.T) { matched, unmatched := mergeTensors(unmatched, merge{"a.*.b", "a.b"}) if len(unmatched) != 3 { t.Error("expected 3 remaining tensors, got", len(unmatched)) } if len(matched) != 1 { t.Error("expected 1 merged tensor, got", len(matched)) } checkMatched(t, 1, matched) }) t.Run("multiple merges", func(t *testing.T) { matched, unmatched := mergeTensors(unmatched, merge{"a.*.b", "a.b"}, merge{"c.*.d", "c.d"}) if len(unmatched) != 1 { t.Error("expected 1 remaining tensors, got", len(unmatched)) } if len(matched) != 2 { t.Error("expected 2 merged tensor, got", len(matched)) } checkMatched(t, 2, matched) }) t.Run("no match", func(t *testing.T) { matched, unmatched := mergeTensors(unmatched, merge{"x.*.y", "x.y"}) if len(unmatched) != 5 { t.Error("expected 5 remaining tensors, got", len(unmatched)) } if len(matched) != 0 { t.Error("expected no merged tensors, got", len(matched)) } }) } func TestMergeOrder(t *testing.T) { for range 8 { t.Run("", func(t *testing.T) { tensors := make([]Tensor, 16) for i := range tensors { tensors[i] = &fakeTensor{ name: fmt.Sprintf("layer.%d.weight", i), shape: []uint64{1}, data: []float32{float32(i)}, } } rand.Shuffle(len(tensors), func(i, j int) { tensors[i], tensors[j] = tensors[j], tensors[i] }) matched, unmatched := mergeTensors(tensors, merge{"layer.*.weight", "layer.weight"}) if len(unmatched) != 0 { t.Error("expected no remaining tensors, got", len(unmatched)) } if len(matched) != 1 { t.Error("expected 1 merged tensor, got", len(matched)) } var b bytes.Buffer if _, err := matched[0].WriteTo(&b); err != nil { t.Fatal(err) } var f32s [16]float32 if err := binary.Read(&b, binary.LittleEndian, &f32s); err != nil { t.Fatal(err) } if !slices.IsSorted(f32s[:]) { t.Errorf("merged tensor data is not in order: %+v", f32s) } }) } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/convert/convert_gemma3n.go
convert/convert_gemma3n.go
package convert import ( "slices" "strings" "github.com/ollama/ollama/fs/ggml" "github.com/pdevine/tensor" "github.com/pdevine/tensor/native" "gonum.org/v1/gonum/stat/distuv" ) type gemma3nModel struct { ModelParameters TextModel struct { ActivationSparsityPattern []float32 `json:"activation_sparsity_pattern"` AltupActiveIdx uint32 `json:"altup_active_idx"` AltupCoefClip float32 `json:"altup_coef_clip"` AltupCorrectScale bool `json:"altup_correct_scale"` AltupLRMultiplier float32 `json:"altup_lr_multiplier"` AltupNumInputs uint32 `json:"altup_num_inputs"` HeadDim uint32 `json:"head_dim"` HiddenSize uint32 `json:"hidden_size"` HiddenSizePerLayerInput uint32 `json:"hidden_size_per_layer_input"` IntermediateSize uint32 `json:"intermediate_size"` MaxPositionEmbeddings uint32 `json:"max_position_embeddings"` NumAttentionHeads uint32 `json:"num_attention_heads"` NumHiddenLayers uint32 `json:"num_hidden_layers"` NumKeyValueHeads uint32 `json:"num_key_value_heads"` NumKVSharedLayers uint32 `json:"num_kv_shared_layers"` RMSNormEPS float32 `json:"rms_norm_eps"` RopeLocalBaseFreq float32 `json:"rope_local_base_freq"` RopeTheta float32 `json:"rope_theta"` SlidingWindow uint32 `json:"sliding_window"` LayerTypes []string `json:"layer_types"` } `json:"text_config"` VisionModel struct{} `json:"vision_config"` } func (m *gemma3nModel) KV(t *Tokenizer) ggml.KV { kv := m.ModelParameters.KV(t) kv["general.architecture"] = "gemma3n" kv["gemma3n.activation_sparsity_scale"] = slices.Collect(func(yield func(float32) bool) { norm := distuv.Normal{Mu: 0, Sigma: 1} for _, v := range m.TextModel.ActivationSparsityPattern { if !yield(float32(norm.Quantile(float64(v)))) { break } } }) kv["gemma3n.altup.active_idx"] = m.TextModel.AltupActiveIdx kv["gemma3n.altup.correct_scale"] = m.TextModel.AltupCorrectScale kv["gemma3n.altup.lr_multiplier"] = m.TextModel.AltupLRMultiplier kv["gemma3n.altup.num_inputs"] = m.TextModel.AltupNumInputs kv["gemma3n.attention.head_count_kv"] = m.TextModel.NumKeyValueHeads kv["gemma3n.attention.head_count"] = m.TextModel.NumAttentionHeads kv["gemma3n.attention.layer_norm_rms_epsilon"] = m.TextModel.RMSNormEPS kv["gemma3n.attention.sliding_window"] = m.TextModel.SlidingWindow kv["gemma3n.attention.sliding_window_pattern"] = slices.Collect(func(yield func(bool) bool) { for _, t := range m.TextModel.LayerTypes { if !yield(t == "sliding_attention") { break } } }) kv["gemma3n.attention.shared_kv_layers"] = m.TextModel.NumKVSharedLayers kv["gemma3n.block_count"] = m.TextModel.NumHiddenLayers kv["gemma3n.context_length"] = m.TextModel.MaxPositionEmbeddings kv["gemma3n.embedding_length_per_layer_input"] = m.TextModel.HiddenSizePerLayerInput kv["gemma3n.embedding_length"] = m.TextModel.HiddenSize kv["gemma3n.feed_forward_length"] = m.TextModel.IntermediateSize kv["gemma3n.head_dim"] = m.TextModel.HeadDim kv["gemma3n.rope.freq_base_local"] = m.TextModel.RopeLocalBaseFreq kv["gemma3n.rope.freq_base"] = m.TextModel.RopeTheta return kv } func (m *gemma3nModel) Tensors(ts []Tensor) []*ggml.Tensor { out, ts := mergeTensors(ts, merge{"altup_proj.*.weight", "altup_proj.weight"}, merge{"altup_unembd_proj.*.weight", "altup_unembd_proj.weight"}, ) for _, t := range ts { switch { case strings.Contains(t.Name(), "audio_tower"), strings.Contains(t.Name(), "embed_audio"), strings.Contains(t.Name(), "vision_tower"), strings.Contains(t.Name(), "embed_vision"): // TODO: handle audio and vision towers continue case strings.Contains(t.Name(), "altup_predict_coef"), strings.Contains(t.Name(), "altup_correct_coef"): if m.TextModel.AltupCoefClip > 0 { t.SetRepacker(func(name string, data []float32, shape []uint64) (_ []float32, err error) { dims := make([]int, len(shape)) for i := range shape { dims[i] = int(shape[i]) } var t tensor.Tensor = tensor.New(tensor.WithShape(dims...), tensor.WithBacking(data)) t, err = tensor.Clamp(t, -m.TextModel.AltupCoefClip, m.TextModel.AltupCoefClip) if err != nil { return nil, err } if err := t.Reshape(t.Shape().TotalSize()); err != nil { return nil, err } return native.VectorF32(t.(*tensor.Dense)) }) } } out = append(out, &ggml.Tensor{ Name: t.Name(), Kind: t.Kind(), Shape: t.Shape(), WriterTo: t, }) } return out } func (m *gemma3nModel) Replacements() []string { return []string{ "model.language_model.embed_tokens_per_layer", "per_layer_token_embd", "model.language_model.embed_tokens", "token_embd", "model.language_model.per_layer_model_projection", "per_layer_model_proj", "model.language_model.per_layer_projection_norm", "per_layer_proj_norm", "model.language_model.altup_projections", "altup_proj", "model.language_model.altup_unembed_projections", "altup_unembd_proj", "model.language_model.norm", "output_norm", "model.language_model.layers", "blk", "input_layernorm", "attn_norm", "self_attn.q_proj", "attn_q", "self_attn.q_norm", "attn_q_norm", "self_attn.k_proj", "attn_k", "self_attn.k_norm", "attn_k_norm", "self_attn.v_proj", "attn_v", "self_attn.o_proj", "attn_output", "post_attention_layernorm", "post_attention_norm", "pre_feedforward_layernorm", "ffn_norm", "mlp.gate_proj", "ffn_gate", "mlp.up_proj", "ffn_up", "mlp.down_proj", "ffn_down", "post_feedforward_layernorm", "post_ffw_norm", "per_layer_input_gate", "inp_gate", "per_layer_projection", "proj", "post_per_layer_input_norm", "post_norm", "altup.", "altup_", "modality_router", "router", "prediction_coefs", "predict_coef", "correction_coefs", "correct_coef", "correct_output_scale", "correct_scale.weight", "laurel.", "laurel_", "linear_left", "l", "linear_right", "r", "post_laurel_norm", "post_norm", } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/convert/convert_nomicbert.go
convert/convert_nomicbert.go
package convert import ( "cmp" "encoding/json" "io/fs" "path/filepath" "slices" "strings" "github.com/ollama/ollama/fs/ggml" ) type nomicbertModel struct { ModelParameters NLayers uint32 `json:"n_layers"` NumHiddenLayers uint32 `json:"num_hidden_layers"` MaxPositionEmbeddings uint32 `json:"max_position_embeddings"` HiddenSize uint32 `json:"hidden_size"` IntermediateSize uint32 `json:"intermediate_size"` NumAttentionHeads uint32 `json:"num_attention_heads"` NumKeyValueHeads uint32 `json:"num_key_value_heads"` LayerNormEPS float32 `json:"layer_norm_eps"` LayerNormEpsilon float32 `json:"layer_norm_epsilon"` RopeFreqBase float32 `json:"rope_theta"` normalizeEmbeddings bool PoolingType uint32 // MoE parameters (only present in v2 models) NumExperts uint32 `json:"num_local_experts"` NumExpertsUsed uint32 `json:"num_experts_per_tok"` MoEEveryNLayers uint32 `json:"moe_every_n_layers"` } var ( _ ModelConverter = (*nomicbertModel)(nil) _ moreParser = (*nomicbertModel)(nil) ) func (p *nomicbertModel) parseMore(fsys fs.FS) error { bts, err := fs.ReadFile(fsys, "modules.json") if err != nil { return err } var modules []struct { Type string `json:"type"` Path string `json:"path"` } if err := json.Unmarshal(bts, &modules); err != nil { return err } var pooling string for _, m := range modules { switch m.Type { case "sentence_transformers.models.Pooling": pooling = m.Path case "sentence_transformers.models.Normalize": p.normalizeEmbeddings = true } } if pooling != "" { bts, err := fs.ReadFile(fsys, filepath.Join(pooling, "config.json")) if err != nil { return err } var pc struct { PoolingModeCLSToken bool `json:"pooling_mode_cls_token"` PoolingModeMeanTokens bool `json:"pooling_mode_mean_tokens"` } if err := json.Unmarshal(bts, &pc); err != nil { return err } if pc.PoolingModeMeanTokens { p.PoolingType = 1 } else if pc.PoolingModeCLSToken { p.PoolingType = 2 } } return nil } func (p *nomicbertModel) KV(t *Tokenizer) ggml.KV { kv := p.ModelParameters.KV(t) // Determine architecture based on MoE parameters (following qwen3 pattern) arch := "nomic-bert" if p.MoEEveryNLayers > 0 { arch += "-moe" } kv["general.architecture"] = arch kv["attention.causal"] = false kv["pooling_type"] = p.PoolingType kv["normalize_embeddings"] = p.normalizeEmbeddings kv["block_count"] = cmp.Or(p.NLayers, p.NumHiddenLayers) if contextLength := p.MaxPositionEmbeddings; contextLength > 0 { kv["context_length"] = contextLength } if embeddingLength := p.HiddenSize; embeddingLength > 0 { kv["embedding_length"] = p.HiddenSize } if feedForwardLength := p.IntermediateSize; feedForwardLength > 0 { kv["feed_forward_length"] = p.IntermediateSize } if headCount := p.NumAttentionHeads; headCount > 0 { kv["attention.head_count"] = p.NumAttentionHeads } if kvHeadCount := p.NumKeyValueHeads; kvHeadCount > 0 { kv["attention.head_count_kv"] = p.NumKeyValueHeads } if layerNormEpsilon := cmp.Or(p.LayerNormEPS, p.LayerNormEpsilon); layerNormEpsilon > 0 { kv["attention.layer_norm_epsilon"] = layerNormEpsilon } if p.RopeFreqBase > 0 { kv["rope.freq_base"] = p.RopeFreqBase } // MoE specific parameters (only if MoE is enabled) if p.NumExperts > 0 { kv["expert_count"] = p.NumExperts } if p.NumExpertsUsed > 0 { kv["expert_used_count"] = p.NumExpertsUsed } if p.MoEEveryNLayers > 0 { kv["moe_every_n_layers"] = p.MoEEveryNLayers } kv["tokenizer.ggml.model"] = "bert" kv["tokenizer.ggml.token_type_count"] = uint32(2) // convert to phantom space tokens for i, e := range t.Tokens { switch { case strings.HasPrefix(e, "[") && strings.HasSuffix(e, "]"): // noop - keep special tokens as-is case strings.HasPrefix(e, "##"): t.Tokens[i] = e[2:] default: t.Tokens[i] = "\u2581" + e } } kv["tokenizer.ggml.tokens"] = t.Tokens return kv } func (p *nomicbertModel) Tensors(ts []Tensor) []*ggml.Tensor { out := make([]*ggml.Tensor, 0, len(ts)) for _, t := range ts { if slices.Contains([]string{ "embeddings.position_ids", "pooler.dense.weight", "pooler.dense.bias", }, t.Name()) { continue } out = append(out, &ggml.Tensor{ Name: t.Name(), Kind: t.Kind(), Shape: t.Shape(), WriterTo: t, }) } return out } func (nomicbertModel) Replacements() []string { return []string{ "encoder.layer", "blk", "encoder.layers", "blk", "embeddings.word_embeddings", "token_embd", "embeddings.token_type_embeddings", "token_types", "embeddings.LayerNorm", "token_embd_norm", "attention.self.qkv", "attn_qkv", "attention.output.dense", "attn_output", "attention.output.LayerNorm", "attn_output_norm", "mlp.up", "ffn_up", "mlp.down", "ffn_down", "mlp.router", "ffn_gate_inp", "mlp.experts.up", "ffn_up_exps", "mlp.experts.down", "ffn_down_exps", "intermediate.dense", "ffn_up", "output.dense", "ffn_down", "output.LayerNorm", "layer_output_norm", } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/convert/convert_mistral.go
convert/convert_mistral.go
package convert import ( "cmp" "fmt" "strings" "github.com/pdevine/tensor" "github.com/pdevine/tensor/native" "github.com/ollama/ollama/fs/ggml" ) type mistral3Model struct { ModelParameters ImageTokenIndex uint32 `json:"image_token_index"` SpatialMergeSize uint32 `json:"spatial_merge_size"` VisionFeatureLayer int32 `json:"vision_feature_layer"` TextModel struct { NumHiddenLayers uint32 `json:"num_hidden_layers"` MaxPositionEmbeddings uint32 `json:"max_position_embeddings"` HiddenSize uint32 `json:"hidden_size"` IntermediateSize uint32 `json:"intermediate_size"` NumAttentionHeads uint32 `json:"num_attention_heads"` NumKeyValueHeads uint32 `json:"num_key_value_heads"` RopeTheta float32 `json:"rope_theta"` RMSNormEPS float32 `json:"rms_norm_eps"` HeadDim uint32 `json:"head_dim"` SlidingWindow *uint32 `json:"sliding_window"` HiddenAct string `json:"hidden_act"` VocabSize uint32 `json:"vocab_size"` RopeParameters struct { BetaFast float32 `json:"beta_fast"` BetaSlow float32 `json:"beta_slow"` Factor float32 `json:"factor"` Llama4ScalingBeta *float32 `json:"llama_4_scaling_beta"` OrigMaxPositionEmbeddings uint32 `json:"original_max_position_embeddings"` RopeType string `json:"rope_type"` RopeTheta float32 `json:"rope_theta"` Mscale *float32 `json:"mscale"` MscaleAllDim *float32 `json:"mscale_all_dim"` } `json:"rope_parameters"` } `json:"text_config"` VisionModel struct { NumAttentionHeads uint32 `json:"num_attention_heads"` NumHiddenLayers uint32 `json:"num_hidden_layers"` HiddenSize uint32 `json:"hidden_size"` IntermediateSize uint32 `json:"intermediate_size"` ImageSize uint32 `json:"image_size"` NumChannels uint32 `json:"num_channels"` PatchSize uint32 `json:"patch_size"` HeadDim uint32 `json:"head_dim"` HiddenAct string `json:"hidden_act"` RopeTheta float32 `json:"rope_theta"` RopeParameters struct { RopeTheta float32 `json:"rope_theta"` } `json:"rope_parameters"` } `json:"vision_config"` MultiModalProjectorBias bool `json:"multimodal_projector_bias"` ProjectorHiddenAct string `json:"projector_hidden_act"` } func (p *mistral3Model) KV(t *Tokenizer) ggml.KV { kv := p.ModelParameters.KV(t) kv["general.architecture"] = "mistral3" kv["mistral3.vocab_size"] = p.TextModel.VocabSize // Text configuration kv["mistral3.block_count"] = p.TextModel.NumHiddenLayers kv["mistral3.context_length"] = p.TextModel.MaxPositionEmbeddings kv["mistral3.embedding_length"] = p.TextModel.HiddenSize kv["mistral3.feed_forward_length"] = p.TextModel.IntermediateSize kv["mistral3.attention.head_count"] = p.TextModel.NumAttentionHeads kv["mistral3.attention.head_count_kv"] = p.TextModel.NumKeyValueHeads kv["mistral3.attention.layer_norm_rms_epsilon"] = p.TextModel.RMSNormEPS kv["mistral3.attention.key_length"] = p.TextModel.HeadDim kv["mistral3.attention.value_length"] = p.TextModel.HeadDim kv["mistral3.rope.dimension_count"] = cmp.Or(p.TextModel.HeadDim, p.TextModel.HiddenSize/p.TextModel.NumAttentionHeads) kv["mistral3.rope.freq_base"] = cmp.Or(p.TextModel.RopeTheta, p.TextModel.RopeParameters.RopeTheta) kv["mistral3.rope.scaling.factor"] = p.TextModel.RopeParameters.Factor kv["mistral3.rope.scaling.type"] = p.TextModel.RopeParameters.RopeType kv["mistral3.rope.scaling.beta_fast"] = p.TextModel.RopeParameters.BetaFast kv["mistral3.rope.scaling.beta_slow"] = p.TextModel.RopeParameters.BetaSlow if p.TextModel.RopeParameters.Mscale != nil { kv["mistral3.rope.scaling.mscale"] = *p.TextModel.RopeParameters.Mscale } if p.TextModel.RopeParameters.MscaleAllDim != nil { kv["mistral3.rope.scaling.mscale_all_dim"] = *p.TextModel.RopeParameters.MscaleAllDim } if p.TextModel.RopeParameters.OrigMaxPositionEmbeddings > 0 { kv["mistral3.rope.scaling.original_context_length"] = p.TextModel.RopeParameters.OrigMaxPositionEmbeddings } if p.TextModel.RopeParameters.Llama4ScalingBeta != nil { kv["mistral3.rope.scaling_beta"] = *p.TextModel.RopeParameters.Llama4ScalingBeta } // Vision configuration kv["mistral3.vision.block_count"] = p.VisionModel.NumHiddenLayers kv["mistral3.vision.embedding_length"] = p.VisionModel.HiddenSize kv["mistral3.vision.feed_forward_length"] = p.VisionModel.IntermediateSize kv["mistral3.vision.attention.head_count"] = p.VisionModel.NumAttentionHeads kv["mistral3.vision.attention.key_length"] = p.VisionModel.HeadDim kv["mistral3.vision.image_size"] = p.VisionModel.ImageSize kv["mistral3.vision.patch_size"] = p.VisionModel.PatchSize kv["mistral3.vision.num_channels"] = p.VisionModel.NumChannels // kv["mistral3.vision.attention.layer_norm_epsilon"] = 1e-05 // Default value kv["mistral3.vision.rope.freq_base"] = cmp.Or(p.VisionModel.RopeTheta, p.VisionModel.RopeParameters.RopeTheta) // Multimodal configuration kv["mistral3.image_token_index"] = p.ImageTokenIndex kv["mistral3.spatial_merge_size"] = p.SpatialMergeSize kv["mistral3.mm.projector_bias"] = p.MultiModalProjectorBias if p.ProjectorHiddenAct != "" { kv["mistral3.mm.projector_hidden_act"] = p.ProjectorHiddenAct } return kv } func (p *mistral3Model) Tensors(ts []Tensor) []*ggml.Tensor { var out []*ggml.Tensor for _, t := range ts { if !strings.HasPrefix(t.Name(), "v.") { if strings.HasSuffix(t.Name(), ".attn_q.weight") || strings.HasSuffix(t.Name(), ".attn_k.weight") { t.SetRepacker(p.repack) } } out = append(out, &ggml.Tensor{ Name: t.Name(), Kind: t.Kind(), Shape: t.Shape(), WriterTo: t, }) } return out } func (p *mistral3Model) Replacements() []string { return []string{ "language_model.model.norm", "output_norm", "language_model.model.", "", "language_model.", "", "layers", "blk", "transformer.layers", "blk", "vision_tower", "v", "ln_pre", "encoder_norm", "input_layernorm", "attn_norm", "post_attention_layernorm", "ffn_norm", "embed_tokens", "token_embd", "self_attn.q_proj", "attn_q", "self_attn.k_proj", "attn_k", "self_attn.v_proj", "attn_v", "self_attn.o_proj", "attn_output", "mlp.down_proj", "ffn_down", "mlp.gate_proj", "ffn_gate", "mlp.up_proj", "ffn_up", "attention.q_proj", "attn_q", "attention.k_proj", "attn_k", "attention.v_proj", "attn_v", "attention.o_proj", "attn_output", "attention_norm", "attn_norm", "feed_forward.gate_proj", "ffn_gate", "feed_forward.down_proj", "ffn_down", "feed_forward.up_proj", "ffn_up", "multi_modal_projector", "mm", "ffn_norm", "ffn_norm", "lm_head", "output", } } func (p *mistral3Model) repack(name string, data []float32, shape []uint64) ([]float32, error) { var dims []int for _, dim := range shape { dims = append(dims, int(dim)) } var heads uint32 if strings.HasSuffix(name, ".attn_q.weight") { heads = p.TextModel.NumAttentionHeads } else if strings.HasSuffix(name, ".attn_k.weight") { heads = cmp.Or(p.TextModel.NumKeyValueHeads, p.TextModel.NumAttentionHeads) } else { return nil, fmt.Errorf("unknown tensor for repack: %s", name) } n := tensor.New(tensor.WithShape(dims...), tensor.WithBacking(data)) if err := n.Reshape(append([]int{int(heads), 2, dims[0] / int(heads) / 2}, dims[1:]...)...); err != nil { return nil, err } if err := n.T(0, 2, 1, 3); err != nil { return nil, err } if err := n.Reshape(dims...); err != nil { return nil, err } if err := n.Transpose(); err != nil { return nil, err } ts, err := native.SelectF32(n, 1) if err != nil { return nil, err } var f32s []float32 for _, t := range ts { f32s = append(f32s, t...) } return f32s, nil }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/convert/convert_test.go
convert/convert_test.go
package convert import ( "bytes" "crypto/sha256" "encoding/binary" "encoding/hex" "encoding/json" "flag" "fmt" "io" "io/fs" "log/slog" "maps" "os" "path/filepath" "slices" "strings" "testing" "github.com/google/go-cmp/cmp" "github.com/ollama/ollama/fs/ggml" ) type tensorData struct { Offsets []int `json:"data_offsets"` Type string `json:"dtype"` Shape []int `json:"shape"` } func convertFull(t *testing.T, fsys fs.FS) (*os.File, ggml.KV, ggml.Tensors) { t.Helper() f, err := os.CreateTemp(t.TempDir(), "f16") if err != nil { t.Fatal(err) } defer f.Close() if err := ConvertModel(fsys, f); err != nil { t.Fatal(err) } r, err := os.Open(f.Name()) if err != nil { t.Fatal(err) } t.Cleanup(func() { r.Close() }) m, err := ggml.Decode(r, -1) if err != nil { t.Fatal(err) } if _, err := r.Seek(0, io.SeekStart); err != nil { t.Fatal(err) } return r, m.KV(), m.Tensors() } func generateResultsJSON(t *testing.T, f *os.File, kv ggml.KV, tensors ggml.Tensors) map[string]string { actual := make(map[string]string) for k, v := range kv { if s, ok := v.(json.Marshaler); !ok { actual[k] = fmt.Sprintf("%v", v) } else { bts, err := json.Marshal(s) if err != nil { t.Fatal(err) } actual[k] = fmt.Sprintf("%x", sha256.Sum256(bts)) } } for _, tensor := range tensors.Items() { sha256sum := sha256.New() sr := io.NewSectionReader(f, int64(tensors.Offset+tensor.Offset), int64(tensor.Size())) if _, err := io.Copy(sha256sum, sr); err != nil { t.Fatal(err) } actual[tensor.Name] = hex.EncodeToString(sha256sum.Sum(nil)) } return actual } func TestMain(m *testing.M) { var level slog.Level flag.TextVar(&level, "level", slog.LevelInfo, "log level") flag.Parse() slog.SetLogLoggerLevel(level) os.Exit(m.Run()) } func TestConvertModel(t *testing.T) { cases := []string{ "Meta-Llama-3-8B-Instruct", "Meta-Llama-3.1-8B-Instruct", "Mistral-7B-Instruct-v0.2", "Mixtral-8x7B-Instruct-v0.1", "gemma-2b-it", "gemma-2-2b-it", // microsoft/Phi-3-mini-128-instruct@d548c233192db00165d842bf8edff054bb3212f8 "Phi-3-mini-128k-instruct", "all-MiniLM-L6-v2", "gemma-2-9b-it", "Qwen2.5-0.5B-Instruct", "c4ai-command-r-v01", } for i := range cases { tt := cases[i] t.Run(tt, func(t *testing.T) { t.Parallel() p := filepath.Join("testdata", tt) if testing.Short() { t.Skip("skipping in short mode") } else if _, err := os.Stat(p); err != nil { t.Skipf("%s not found", p) } f, kv, tensors := convertFull(t, os.DirFS(p)) actual := generateResultsJSON(t, f, kv, tensors) expectFile, err := os.Open(filepath.Join("testdata", fmt.Sprintf("%s.json", tt))) if err != nil { t.Fatal(err) } defer expectFile.Close() var expect map[string]string if err := json.NewDecoder(expectFile).Decode(&expect); err != nil { t.Fatal(err) } for _, k := range slices.Sorted(maps.Keys(expect)) { if v, ok := actual[k]; !ok { t.Errorf("missing %s", k) } else if v != expect[k] { t.Errorf("unexpected %s: want %s, got %s", k, expect[k], v) } } }) } } func TestConvertInvalidTensorNames(t *testing.T) { f, err := os.CreateTemp(t.TempDir(), "testmodel") if err != nil { t.Fatal(err) } defer f.Close() tempDir := t.TempDir() td := map[string]*tensorData{} offset := 4096 td["model.layers.0.self_attn.q_proj.weight"] = &tensorData{ Offsets: []int{0, offset}, Type: "F32", Shape: []int{4096, 4096}, } td["blk.0.attn_q.weight"] = &tensorData{ Offsets: []int{offset, offset * 2}, Type: "F32", Shape: []int{4096, 4096}, } generateSafetensorTestData(t, tempDir, td) err = ConvertModel(os.DirFS(tempDir), f) if err == nil || !strings.HasPrefix(err.Error(), "duplicate tensor name") { t.Errorf("expected error but didn't get one") } } func TestConvertInvalidDatatype(t *testing.T) { f, err := os.CreateTemp(t.TempDir(), "testmodel") if err != nil { t.Fatal(err) } defer f.Close() tempDir := t.TempDir() td := map[string]*tensorData{} offset := 4096 * 14336 td["model.layers.0.mlp.down_proj.weight"] = &tensorData{ Offsets: []int{0, offset}, Type: "I8", Shape: []int{4096, 14336}, } td["model.layers.0.mlp.down_proj.weight_format"] = &tensorData{ Offsets: []int{offset, offset}, Type: "U8", Shape: []int{}, } generateSafetensorTestData(t, tempDir, td) err = ConvertModel(os.DirFS(tempDir), f) if err == nil || err.Error() != "unsupported safetensors model" { t.Errorf("expected error but didn't get one") } } func generateSafetensorTestData(t *testing.T, tempDir string, tensorData map[string]*tensorData) { data, err := json.Marshal(tensorData) if err != nil { t.Fatal(err) } var buf bytes.Buffer l := int64(len(data)) err = binary.Write(&buf, binary.LittleEndian, l) if err != nil { t.Fatal(err) } _, err = buf.Write(data) if err != nil { t.Fatal(err) } fdata, err := os.Create(filepath.Join(tempDir, "model-00001-of-00001.safetensors")) if err != nil { t.Fatal(err) } defer fdata.Close() _, err = fdata.Write(buf.Bytes()) if err != nil { t.Fatal(err) } configData := ` { "architectures": [ "LlamaForCausalLM" ] } ` f, err := os.Create(filepath.Join(tempDir, "config.json")) if err != nil { t.Fatal(err) } defer f.Close() _, err = f.WriteString(configData) if err != nil { t.Fatal(err) } tokenizerData := ` { } ` f, err = os.Create(filepath.Join(tempDir, "tokenizer.json")) if err != nil { t.Fatal(err) } defer f.Close() _, err = f.WriteString(tokenizerData) if err != nil { t.Fatal(err) } } func TestConvertAdapter(t *testing.T) { type AdapterCase struct { Name string BaseKV map[string]any Expected map[string]string } cases := []AdapterCase{ { Name: "discollama", BaseKV: map[string]any{ "general.architecture": "llama", "llama.attention.head_count": uint32(32), "llama.attention.head_count_kv": uint32(8), }, Expected: map[string]string{ "general.architecture": "llama", "general.file_type": "1", "general.parameter_count": "106496", "general.type": "adapter", "general.version": "v0.2", "adapter.lora.alpha": "16", "adapter.type": "lora", "llama.attention.head_count": "32", "llama.attention.head_count_kv": "8", "blk.31.attn_q.weight.lora_a": "0eb3318b02cd313429bcc7621b539fdbb10240fea190c56c9e5f93fcd37a4e50", "blk.31.attn_q.weight.lora_b": "0eb3318b02cd313429bcc7621b539fdbb10240fea190c56c9e5f93fcd37a4e50", "blk.31.attn_v.weight.lora_a": "0eb3318b02cd313429bcc7621b539fdbb10240fea190c56c9e5f93fcd37a4e50", "blk.31.attn_v.weight.lora_b": "071dcafe89df065d6e1c935ecb8fdf6479b3c202eb912e7da938597673ff5857", }, }, } for _, c := range cases { t.Run(c.Name, func(t *testing.T) { t.Parallel() f, err := os.CreateTemp(t.TempDir(), "f16") if err != nil { t.Fatal(err) } defer f.Close() tempDir := t.TempDir() generateLoraTestData(t, tempDir) if err = ConvertAdapter(os.DirFS(tempDir), f, c.BaseKV); err != nil { t.Fatal(err) } r, err := os.Open(f.Name()) if err != nil { t.Fatal(err) } defer r.Close() m, err := ggml.Decode(r, -1) if err != nil { t.Fatal(err) } if _, err := r.Seek(0, io.SeekStart); err != nil { t.Fatal(err) } actual := generateResultsJSON(t, r, m.KV(), m.Tensors()) if diff := cmp.Diff(c.Expected, actual); diff != "" { t.Errorf("mismatch (-want +got):\n%s", diff) } }) } } func generateLoraTestData(t *testing.T, tempDir string) { offset := 4096 * 8 * 4 td := map[string]*tensorData{"__metadata__": nil} td["model.layers.31.self_attn.q_proj.lora_a"] = &tensorData{ Offsets: []int{0, offset}, Type: "F32", Shape: []int{4096, 8}, } td["model.layers.31.self_attn.q_proj.lora_b"] = &tensorData{ Offsets: []int{offset, offset * 2}, Type: "F32", Shape: []int{8, 4096}, } td["model.layers.31.self_attn.v_proj.lora_a"] = &tensorData{ Offsets: []int{offset * 2, offset * 3}, Type: "F32", Shape: []int{4096, 8}, } td["model.layers.31.self_attn.v_proj.lora_b"] = &tensorData{ Offsets: []int{offset * 3, offset*3 + 8*1024*4}, Type: "F32", Shape: []int{8, 1024}, } data, err := json.Marshal(td) if err != nil { t.Fatal(err) } var buf bytes.Buffer l := int64(len(data)) err = binary.Write(&buf, binary.LittleEndian, l) if err != nil { t.Fatal(err) } _, err = buf.Write(data) if err != nil { t.Fatal(err) } // write some data for the tensors ones := make([]float32, 4096*8) for i := range ones { ones[i] = float32(1) } for range 3 { err = binary.Write(&buf, binary.LittleEndian, ones) if err != nil { t.Fatal(err) } } ones = make([]float32, 1024*8) for i := range ones { ones[i] = float32(1) } err = binary.Write(&buf, binary.LittleEndian, ones) if err != nil { t.Fatal(err) } fdata, err := os.Create(filepath.Join(tempDir, "adapters.safetensors")) if err != nil { t.Fatal(err) } defer fdata.Close() _, err = fdata.Write(buf.Bytes()) if err != nil { t.Fatal(err) } configData := ` { "adapter_path": "adapters-test", "batch_size": 8, "config": "config-tiny.json", "data": "../discollama-completion", "grad_checkpoint": null, "iters": 1000, "learning_rate": 1e-05, "lora_layers": 1, "lora_parameters": { "rank": 8, "alpha": 16, "dropout": 0.0, "scale": 2.0 }, "lr_schedule": null, "max_seq_length": 2048, "model": "/Users/pdevine/git/Meta-Llama-3-8B-Instruct", "resume_adapter_file": null, "save_every": 100, "seed": 0, "steps_per_eval": 200, "steps_per_report": 10, "test": false, "test_batches": 500, "train": true, "use_dora": false, "val_batches": 25 } ` f, err := os.Create(filepath.Join(tempDir, "adapter_config.json")) if err != nil { t.Fatal(err) } defer f.Close() _, err = f.WriteString(configData) if err != nil { t.Fatal(err) } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/convert/convert_phi3.go
convert/convert_phi3.go
package convert import ( "cmp" "encoding/binary" "io" "math" "strings" "sync" "github.com/ollama/ollama/fs/ggml" ) type phi3Model struct { ModelParameters NumHiddenLayers uint32 `json:"num_hidden_layers"` NLayers uint32 `json:"n_layers"` HiddenSize uint32 `json:"hidden_size"` NEmbd uint32 `json:"n_embd"` IntermediateSize uint32 `json:"intermediate_size"` NumAttentionHeads uint32 `json:"num_attention_heads"` NHead uint32 `json:"n_head"` NumKeyValueHeads uint32 `json:"num_key_value_heads"` NHeadKV uint32 `json:"n_head_kv"` RopeTheta float32 `json:"rope_theta"` RopeScaling struct { Type string `json:"type"` LongFactor ropeFactor `json:"long_factor"` ShortFactor ropeFactor `json:"short_factor"` } `json:"rope_scaling"` RMSNormEPS float32 `json:"rms_norm_eps"` NPositions uint32 `json:"n_positions"` MaxPositionEmbeddings uint32 `json:"max_position_embeddings"` OriginalMaxPositionEmbeddings uint32 `json:"original_max_position_embeddings"` SlidingWindow uint32 `json:"sliding_window"` } var _ ModelConverter = (*phi3Model)(nil) func (p *phi3Model) KV(t *Tokenizer) ggml.KV { kv := p.ModelParameters.KV(t) kv["general.architecture"] = "phi3" kv["phi3.context_length"] = p.MaxPositionEmbeddings kv["phi3.embedding_length"] = cmp.Or(p.HiddenSize, p.NEmbd) kv["phi3.feed_forward_length"] = p.IntermediateSize kv["phi3.block_count"] = cmp.Or(p.NumHiddenLayers, p.NLayers) kv["phi3.attention.head_count"] = cmp.Or(p.NumAttentionHeads, p.NHead) kv["phi3.attention.head_count_kv"] = cmp.Or(p.NumKeyValueHeads, p.NHeadKV) kv["phi3.attention.layer_norm_rms_epsilon"] = p.RMSNormEPS kv["phi3.rope.dimension_count"] = p.HiddenSize / cmp.Or(p.NumAttentionHeads, p.NHead) kv["phi3.rope.freq_base"] = p.RopeTheta kv["phi3.rope.scaling.original_context_length"] = p.OriginalMaxPositionEmbeddings kv["phi3.attention.sliding_window"] = p.SlidingWindow scale := float64(p.MaxPositionEmbeddings) / float64(p.OriginalMaxPositionEmbeddings) switch p.RopeScaling.Type { case "": // no scaling case "su", "longrope": kv["phi3.rope.scaling.attn_factor"] = float32(max(math.Sqrt(1+math.Log(scale)/math.Log(float64(p.OriginalMaxPositionEmbeddings))), 1.0)) case "yarn": kv["phi3.rope.scaling.attn_factor"] = float32(max(0.1*math.Log(scale)+1.0, 1.0)) default: panic("unknown rope scaling type") } return kv } func (p *phi3Model) Tensors(ts []Tensor) []*ggml.Tensor { var addRopeFactors sync.Once out := make([]*ggml.Tensor, 0, len(ts)+2) for _, t := range ts { if strings.HasPrefix(t.Name(), "blk.0.") { addRopeFactors.Do(func() { out = append(out, &ggml.Tensor{ Name: "rope_factors_long.weight", Kind: 0, Shape: []uint64{uint64(len(p.RopeScaling.LongFactor))}, WriterTo: p.RopeScaling.LongFactor, }, &ggml.Tensor{ Name: "rope_factors_short.weight", Kind: 0, Shape: []uint64{uint64(len(p.RopeScaling.ShortFactor))}, WriterTo: p.RopeScaling.ShortFactor, }) }) } out = append(out, &ggml.Tensor{ Name: t.Name(), Kind: t.Kind(), Shape: t.Shape(), WriterTo: t, }) } return out } func (p *phi3Model) Replacements() []string { return []string{ "lm_head", "output", "model.embed_tokens", "token_embd", "model.norm", "output_norm", "model.layers", "blk", "input_layernorm", "attn_norm", "self_attn.qkv_proj", "attn_qkv", "self_attn.o_proj", "attn_output", "mlp.down_proj", "ffn_down", "mlp.gate_up_proj", "ffn_up", "post_attention_layernorm", "ffn_norm", } } type ropeFactor []float32 func (r ropeFactor) WriteTo(w io.Writer) (int64, error) { return 0, binary.Write(w, binary.LittleEndian, r) }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/convert/tensor.go
convert/tensor.go
package convert import ( "cmp" "errors" "io" "iter" "path" "slices" "strconv" "strings" "github.com/pdevine/tensor" "github.com/pdevine/tensor/native" "github.com/ollama/ollama/fs/ggml" ) type split struct { *strings.Replacer dim int slices []tensor.Slice // afterFunc is an optional function to apply to the tensor after slicing afterFunc func(tensor.Tensor) (tensor.Tensor, error) } // splitDim splits a tensor along a specified dimension into multiple tensors. The dimension // is split evenly based on the number of replacers provided unless a specific count is given. func splitDim(t Tensor, dim int, splits ...split) iter.Seq[*ggml.Tensor] { return func(yield func(*ggml.Tensor) bool) { var offset int for _, split := range splits { t := t.Clone() shape := slices.Clone(t.Shape()) shape[dim] = cmp.Or(uint64(split.dim), shape[dim]/uint64(len(splits))) slice := split.slices if len(slice) == 0 { slice = slices.Repeat([]tensor.Slice{nil}, len(shape)) slice[dim] = tensor.S(offset, offset+int(shape[dim])) offset += int(shape[dim]) } t.SetRepacker(func(_ string, data []float32, shape []uint64) ([]float32, error) { dims := make([]int, len(shape)) for i := range shape { dims[i] = int(shape[i]) } var tt tensor.Tensor = tensor.New(tensor.WithShape(dims...), tensor.WithBacking(data)) tt, err := tt.Slice(slice...) if err != nil { return nil, err } tt = tensor.Materialize(tt) if split.afterFunc != nil { tt, err = split.afterFunc(tt) if err != nil { return nil, err } } // flatten tensor so it can be written as a vector if err := tt.Reshape(tt.Shape().TotalSize()); err != nil { return nil, err } return native.VectorF32(tt.(*tensor.Dense)) }) if !yield(&ggml.Tensor{ Name: split.Replace(t.Name()), Kind: t.Kind(), Shape: shape, WriterTo: t, }) { break } } } } type merge struct { pattern, name string } // mergeTensors merges tensors that match a given pattern into a single tensor. func mergeTensors(unmatched []Tensor, merges ...merge) (out []*ggml.Tensor, _ []Tensor) { var matched []Tensor for i := range merges { matched, unmatched = slicesSplitFunc(unmatched, func(t Tensor) bool { matched, _ := path.Match(merges[i].pattern, t.Name()) return matched }) slices.SortStableFunc(matched, func(a, b Tensor) int { x := strings.Split(a.Name(), ".") y := strings.Split(b.Name(), ".") if len(x) != len(y) { return cmp.Compare(len(x), len(y)) } vals := make([]int, len(x)) for i := range x { vals[i] = strings.Compare(x[i], y[i]) m, err := strconv.ParseInt(x[i], 0, 0) n, err2 := strconv.ParseInt(y[i], 0, 0) if errors.Join(err, err2) == nil { vals[i] = cmp.Compare(m, n) } } return cmp.Or(vals...) }) if len(matched) > 0 { out = append(out, &ggml.Tensor{ Name: merges[i].name, Kind: matched[0].Kind(), Shape: append([]uint64{uint64(len(matched))}, matched[0].Shape()...), WriterTo: mergeGroup(matched), }) } } return out, unmatched } // slicesSplitFunc splits a slice into two slices based on a predicate function. func slicesSplitFunc[S ~[]E, E comparable](s S, fn func(e E) bool) (matched, unmatched S) { for _, e := range s { if fn(e) { matched = append(matched, e) } else { unmatched = append(unmatched, e) } } return matched, unmatched } type mergeGroup []Tensor func (g mergeGroup) WriteTo(w io.Writer) (int64, error) { for _, t := range g { if _, err := t.WriteTo(w); err != nil { return 0, err } } return 0, nil }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/convert/tokenizer.go
convert/tokenizer.go
package convert import ( "crypto/sha256" "encoding/hex" "encoding/json" "errors" "fmt" "io/fs" "log/slog" "maps" "os" "slices" "strings" ) const ( _ int32 = iota tokenTypeNormal tokenTypeUnknown tokenTypeControl tokenTypeUserDefined tokenTypeUnused tokenTypeByte ) type Tokenizer struct { *Vocabulary SpecialVocabulary []*SpecialVocabulary Merges []string Pre string Template string } func parseTokenizer(fsys fs.FS, specialTokenTypes []string) (*Tokenizer, error) { v, err := parseVocabulary(fsys) if err != nil { return nil, err } t := &Tokenizer{ Vocabulary: v, Pre: "default", } addedTokens := make(map[string]token) if f, err := fsys.Open("tokenizer.json"); errors.Is(err, os.ErrNotExist) { } else if err != nil { return nil, err } else { defer f.Close() var tt tokenizer if err := json.NewDecoder(f).Decode(&tt); err != nil { return nil, err } for _, t := range tt.AddedTokens { addedTokens[t.Content] = t } if len(tt.Model.Merges) == 0 { // noop; merges is empty } else if err := json.Unmarshal(tt.Model.Merges, &t.Merges); err == nil { // noop; merges is []string } else if merges, err := func() ([][]string, error) { var merges [][]string if err := json.Unmarshal(tt.Model.Merges, &merges); err != nil { return nil, err } return merges, nil }(); err == nil { t.Merges = make([]string, len(merges)) for i := range merges { t.Merges[i] = strings.Join(merges[i], " ") } } else { return nil, fmt.Errorf("could not parse tokenizer merges. expected []string or [][]string: %w", err) } sha256sum := sha256.New() for _, pt := range tt.PreTokenizer.PreTokenizers { switch pt.Type { case "Split": if pt.Pattern.Regex != "" { // create a checksum of all Split pretokenizers which should be sufficient // to identify the pretokenizer sha256sum.Write([]byte(pt.Pattern.Regex)) } } } switch digest := hex.EncodeToString(sha256sum.Sum(nil)); digest { case "d98f9631be1e9607a9848c26c1f9eac1aa9fc21ac6ba82a2fc0741af9780a48f": t.Pre = "llama-bpe" case "03df5c5863ad70781dcfdef491ead25140f895fe8010964be0daefe27be32b02": t.Pre = "deepseek-llm" case "21cde974d587f0d54dc8d56b183cc1e6239600172035c68fbd6d4b9f8da0576e": t.Pre = "deepseek-coder" case "1ff7f41064896984db5d1bb6ff64fa4bc29007d08c1b439e505b7392777a319e": t.Pre = "qwen2" case "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855": // noop, empty pretokenizer default: slog.Warn("unknown pretokenizer, using default", "digest", digest) } } if f, err := fsys.Open("tokenizer_config.json"); errors.Is(err, os.ErrNotExist) { // noop } else if err != nil { return nil, err } else { defer f.Close() var p map[string]json.RawMessage if err := json.NewDecoder(f).Decode(&p); err != nil { return nil, err } if template, ok := p["chat_template"]; ok { var s []struct { Name string `json:"name"` Template string `json:"template"` } if err := json.Unmarshal(template, &t.Template); err == nil { // noop } else if err := json.Unmarshal(template, &s); err == nil { for _, e := range s { if e.Name == "default" { t.Template = e.Template break } } } else { return nil, fmt.Errorf("invalid chat_template: %w", err) } } for _, st := range specialTokenTypes { sv := SpecialVocabulary{Type: st} if bts, ok := p[fmt.Sprintf("add_%s_token", st)]; ok { if err := json.Unmarshal(bts, &sv.AddToken); err != nil { return nil, err } } if bts, ok := p[fmt.Sprintf("%s_token", st)]; ok { var content string if err := json.Unmarshal(bts, &content); err != nil { var mm map[string]any if err := json.Unmarshal(bts, &mm); err != nil { continue } content, ok = mm["content"].(string) if !ok { continue } } sv.Content = content } if id, ok := addedTokens[sv.Content]; ok { sv.ID = id.ID t.SpecialVocabulary = append(t.SpecialVocabulary, &sv) } } } if f, err := fsys.Open("generation_config.json"); errors.Is(err, os.ErrNotExist) { } else if err != nil { return nil, err } else { defer f.Close() var p map[string]json.RawMessage if err := json.NewDecoder(f).Decode(&p); err != nil { return nil, err } for _, st := range specialTokenTypes { if bts, ok := p[fmt.Sprintf("%s_token_id", st)]; ok { var ids []int32 if err := json.Unmarshal(bts, &ids); err != nil { // value is not a list so the existing ID is used continue } if i := slices.IndexFunc(t.SpecialVocabulary, func(sv *SpecialVocabulary) bool { return sv.Type == st }); i >= 0 { t.SpecialVocabulary[i].IDs = ids } } } } return t, nil } type tokenizer struct { AddedTokens []token `json:"added_tokens"` Model struct { Type string `json:"type"` Vocab map[string]int `json:"vocab"` Merges json.RawMessage `json:"merges"` } `json:"model"` PreTokenizer struct { PreTokenizers []struct { Type string `json:"type"` Pattern struct { Regex string `json:"Regex"` } `json:"pattern"` } `json:"pretokenizers"` } `json:"pre_tokenizer"` } type token struct { ID int `json:"id"` Content string `json:"content"` Special bool `json:"special"` UserDefined bool } type Vocabulary struct { Model string Tokens []string Scores []float32 Types []int32 } func parseVocabularyFromTokenizer(fsys fs.FS) (*Vocabulary, error) { f, err := fsys.Open("tokenizer.json") if err != nil { return nil, err } defer f.Close() var t tokenizer if err := json.NewDecoder(f).Decode(&t); err != nil { return nil, err } tokens := make(map[int]token, len(t.Model.Vocab)) for k, v := range t.Model.Vocab { tokens[v] = token{ ID: v, Content: k, } } for _, token := range t.AddedTokens { token.UserDefined = true tokens[token.ID] = token } v := Vocabulary{Model: "gpt2"} for _, k := range slices.Sorted(maps.Keys(tokens)) { token := tokens[k] v.Tokens = append(v.Tokens, token.Content) v.Scores = append(v.Scores, float32(token.ID)) switch { case token.Special: v.Types = append(v.Types, tokenTypeControl) case token.UserDefined: v.Types = append(v.Types, tokenTypeUserDefined) default: v.Types = append(v.Types, tokenTypeNormal) } } return &v, nil } func parseVocabulary(fsys fs.FS) (*Vocabulary, error) { patterns := []struct { Pattern string Func func(fs.FS) (*Vocabulary, error) }{ {"tokenizer.model", parseSentencePiece}, {"tokenizer.json", parseVocabularyFromTokenizer}, } for _, pattern := range patterns { if _, err := fs.Stat(fsys, pattern.Pattern); errors.Is(err, os.ErrNotExist) { continue } else if err != nil { return nil, err } return pattern.Func(fsys) } return nil, errors.New("unknown tokenizer format") } type SpecialVocabulary struct { Type string ID int Content string AddToken bool // IDs is populated by generation_config.json IDs []int32 } func (sv SpecialVocabulary) Key() string { switch t := sv.Type; t { case "bos", "eos", "cls", "mask": return t case "unk": return "unknown" case "sep": //nolint:misspell // this is an upstream typo return "seperator" case "pad": return "padding" } panic("unknown special vocabulary type") }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/convert/tokenizer_spm.go
convert/tokenizer_spm.go
package convert import ( "cmp" "encoding/json" "errors" "fmt" "io/fs" "log/slog" "os" "reflect" "slices" "google.golang.org/protobuf/proto" "github.com/ollama/ollama/convert/sentencepiece" ) func parseSentencePiece(fsys fs.FS) (*Vocabulary, error) { slog.Debug("using spm vocabulary") ast, err := parseAdditionalSpecialTokens(fsys) if err != nil { return nil, err } bts, err := fs.ReadFile(fsys, "tokenizer.model") if err != nil { return nil, err } var spm sentencepiece.ModelProto if err := proto.Unmarshal(bts, &spm); err != nil { return nil, err } v := Vocabulary{Model: "llama"} for _, piece := range spm.GetPieces() { v.Tokens = append(v.Tokens, piece.GetPiece()) v.Scores = append(v.Scores, piece.GetScore()) switch t := piece.GetType(); t { case sentencepiece.ModelProto_SentencePiece_UNKNOWN, sentencepiece.ModelProto_SentencePiece_CONTROL, sentencepiece.ModelProto_SentencePiece_UNUSED, sentencepiece.ModelProto_SentencePiece_BYTE: v.Types = append(v.Types, int32(t)) default: tt := int32(sentencepiece.ModelProto_SentencePiece_NORMAL) // temporary fix to handle gemma3 broken configs // TODO(parthsareen): allow reading of tokenizer.json to allow managing special tokens when using spm if slices.Contains([]string{"<end_of_turn>", "<start_of_turn>", "<start_function_declaration>", "<end_function_declaration>", "<start_function_call>", "<end_function_call>", "<start_function_response>", "<end_function_response>", "<escape>"}, piece.GetPiece()) { tt = int32(sentencepiece.ModelProto_SentencePiece_CONTROL) } for _, t := range ast { if t.Content == piece.GetPiece() { tt = int32(sentencepiece.ModelProto_SentencePiece_CONTROL) break } } v.Types = append(v.Types, tt) } } f, err := fsys.Open("added_tokens.json") if errors.Is(err, os.ErrNotExist) { return &v, nil } else if err != nil { return nil, err } defer f.Close() var atm map[string]int if err := json.NewDecoder(f).Decode(&atm); err != nil { return nil, err } type t struct { id int content string } var ts []t for content, id := range atm { ts = append(ts, t{id, content}) } slices.SortFunc(ts, func(i, j t) int { return cmp.Compare(i.id, j.id) }) for _, t := range ts { if t.id < len(v.Tokens) { if v.Tokens[t.id] == t.content { slog.Warn("tokenizer", "duplicate token", t.content, "id", t.id) continue } return nil, fmt.Errorf("token mismatch: %s != %s at pos [%d]", t.content, v.Tokens[t.id], t.id) } if t.id != len(v.Tokens) { return nil, fmt.Errorf("invalid token id: [%d] as pos [%d]", t.id, len(v.Tokens)) } v.Tokens = append(v.Tokens, t.content) v.Scores = append(v.Scores, -1000.0) v.Types = append(v.Types, tokenTypeUserDefined) } return &v, nil } type specialToken struct { Content string `json:"content"` Lstrip bool `json:"lstrip"` Normalized bool `json:"normalized"` Rstrip bool `json:"rstrip"` SingleWord bool `json:"single_word"` } func parseAdditionalSpecialTokens(fsys fs.FS) ([]specialToken, error) { f, err := fsys.Open("special_tokens_map.json") if errors.Is(err, os.ErrNotExist) { return nil, nil } else if err != nil { return nil, err } defer f.Close() var m struct { AdditionalSpecialTokens any `json:"additional_special_tokens"` } if err := json.NewDecoder(f).Decode(&m); err != nil { return nil, err } var ast []specialToken switch st := m.AdditionalSpecialTokens.(type) { case []string: for _, s := range st { ast = append(ast, specialToken{Content: s}) } case []any: for _, s := range st { // marshal and unmarshal the object to get the special token tMap := s.(map[string]any) data, err := json.Marshal(tMap) if err != nil { return nil, err } var token specialToken err = json.Unmarshal(data, &token) if err != nil { return nil, err } ast = append(ast, token) } default: slog.Warn("special token", "unknown token", reflect.TypeOf(st)) } slog.Debug("spm tokenizer", "additional tokens", ast) return ast, nil }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/convert/convert_deepseekocr.go
convert/convert_deepseekocr.go
package convert import ( "fmt" "github.com/ollama/ollama/fs/ggml" ) type deepseekocr struct { ModelParameters LanguageConfig struct { MaxPositionEmbeddings uint32 `json:"max_position_embeddings"` HiddenSize uint32 `json:"hidden_size"` HiddenLayers uint32 `json:"num_hidden_layers"` IntermediateSize uint32 `json:"intermediate_size"` NumAttentionHeads uint32 `json:"num_attention_heads"` NumKeyValueHeads uint32 `json:"num_key_value_heads"` NumRoutedExperts uint32 `json:"n_routed_experts"` NumSharedExperts uint32 `json:"n_shared_experts"` NumExpertsPerToken uint32 `json:"num_experts_per_tok"` FirstKDenseReplace uint32 `json:"first_k_dense_replace"` } `json:"language_config"` VisionConfig struct { ImageSize uint32 `json:"image_size"` Width struct { Vision struct { Heads uint32 `json:"heads"` ImageSize uint32 `json:"image_size"` Layers uint32 `json:"layers"` PatchSize uint32 `json:"patch_size"` Width uint32 `json:"width"` } `json:"clip-l-14-224"` Sam struct { GlobalAttentionIndexes []int32 `json:"global_attn_indexes"` Heads uint32 `json:"heads"` Layers uint32 `json:"layers"` Width uint32 `json:"width"` } `json:"sam_vit_b"` } } `json:"vision_config"` } func (m *deepseekocr) KV(t *Tokenizer) ggml.KV { kv := m.ModelParameters.KV(t) kv["general.architecture"] = "deepseekocr" kv["block_count"] = m.LanguageConfig.HiddenLayers kv["context_length"] = m.LanguageConfig.MaxPositionEmbeddings kv["embedding_length"] = m.LanguageConfig.HiddenSize kv["feed_forward_length"] = m.LanguageConfig.IntermediateSize kv["attention.head_count"] = m.LanguageConfig.NumAttentionHeads kv["attention.head_count_kv"] = m.LanguageConfig.NumKeyValueHeads kv["expert_count"] = m.LanguageConfig.NumRoutedExperts kv["expert_used_count"] = m.LanguageConfig.NumExpertsPerToken kv["leading_dense_block_count"] = m.LanguageConfig.FirstKDenseReplace kv["vision.block_count"] = m.VisionConfig.Width.Vision.Layers kv["vision.embedding_length"] = m.VisionConfig.Width.Vision.Width kv["vision.head_count"] = m.VisionConfig.Width.Vision.Heads kv["vision.image_size"] = m.VisionConfig.Width.Vision.ImageSize kv["vision.patch_size"] = m.VisionConfig.Width.Vision.PatchSize kv["sam.block_count"] = m.VisionConfig.Width.Sam.Layers kv["sam.embedding_length"] = m.VisionConfig.Width.Sam.Width kv["sam.head_count"] = m.VisionConfig.Width.Sam.Heads kv["sam.global_attention_indexes"] = m.VisionConfig.Width.Sam.GlobalAttentionIndexes return kv } func (m *deepseekocr) Tensors(s []Tensor) (out []*ggml.Tensor) { merges := make([]merge, m.LanguageConfig.HiddenLayers*3) for i := range m.LanguageConfig.HiddenLayers { merges[i*3+0] = merge{ fmt.Sprintf("blk.%d.mlp.experts.*.gate_proj.weight", i), fmt.Sprintf("blk.%d.ffn_gate_exps.weight", i), } merges[i*3+1] = merge{ fmt.Sprintf("blk.%d.mlp.experts.*.up_proj.weight", i), fmt.Sprintf("blk.%d.ffn_up_exps.weight", i), } merges[i*3+2] = merge{ fmt.Sprintf("blk.%d.mlp.experts.*.down_proj.weight", i), fmt.Sprintf("blk.%d.ffn_down_exps.weight", i), } } out, s = mergeTensors(s, merges...) for _, t := range s { out = append(out, &ggml.Tensor{ Name: t.Name(), Kind: t.Kind(), Shape: t.Shape(), WriterTo: t, }) } return out } func (m *deepseekocr) Replacements() []string { return []string{ "model.embed_tokens", "token_embd", "model.layers", "blk", "input_layernorm", "attn_norm", "self_attn.q_proj", "attn_q", "self_attn.k_proj", "attn_k", "self_attn.v_proj", "attn_v", "self_attn.o_proj", "attn_output", "post_attention_layernorm", "ffn_norm", "mlp.gate_proj", "ffn_gate", "mlp.up_proj", "ffn_up", "mlp.down_proj", "ffn_down", "mlp.gate", "ffn_gate_inp", "mlp.shared_experts.gate_proj", "ffn_gate_shexp", "mlp.shared_experts.up_proj", "ffn_up_shexp", "mlp.shared_experts.down_proj", "ffn_down_shexp", "model.norm", "output_norm", "lm_head", "output", "model.vision_model", "v", "embeddings.patch_embedding", "patch_embd", "embeddings.class_embedding", "class_embd", "embeddings.position_embedding", "position_embd", "transformer.layers", "blk", "model.projector", "mm", "model.image_newline", "mm.image_newline", //nolint:misspell // this misspelling is upstream. fixing it breaks the model "model.view_seperator", "mm.view_seperator", "model.sam_model.patch_embed.proj", "s.patch_embd", "model.sam_model.pos_embed", "s.position_embd", "model.sam_model.blocks", "s.blk", "model.sam_model.neck", "s.neck", "model.sam_model.net_", "s.net_", } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/convert/convert.go
convert/convert.go
package convert import ( "cmp" "encoding/json" "errors" "fmt" "io/fs" "log/slog" "os" "slices" "strings" "github.com/ollama/ollama/fs/ggml" ) type ModelParameters struct { Architectures []string `json:"architectures"` VocabSize uint32 `json:"vocab_size"` TextModel struct { VocabSize uint32 `json:"vocab_size"` } `json:"text_config"` } type AdapterParameters struct { Alpha uint32 `json:"lora_alpha"` LoraLayers uint32 `json:"lora_layers"` LoraParameters struct { Rank uint32 `json:"rank"` Alpha float32 `json:"alpha"` Scale float32 `json:"scale"` } `json:"lora_parameters"` } func (ModelParameters) KV(t *Tokenizer) ggml.KV { kv := ggml.KV{ "general.file_type": uint32(1), "general.quantization_version": uint32(2), "tokenizer.ggml.pre": t.Pre, "tokenizer.ggml.model": t.Vocabulary.Model, "tokenizer.ggml.tokens": t.Vocabulary.Tokens, "tokenizer.ggml.scores": t.Vocabulary.Scores, "tokenizer.ggml.token_type": t.Vocabulary.Types, } if len(t.Merges) > 0 { kv["tokenizer.ggml.merges"] = t.Merges } if t.Template != "" { kv["tokenizer.chat_template"] = t.Template } for _, sv := range t.SpecialVocabulary { kv[fmt.Sprintf("tokenizer.ggml.add_%s_token", sv.Key())] = sv.AddToken kv[fmt.Sprintf("tokenizer.ggml.%s_token_id", sv.Key())] = uint32(sv.ID) if len(sv.IDs) > 0 { kv[fmt.Sprintf("tokenizer.ggml.%s_token_ids", sv.Key())] = sv.IDs } } return kv } func (p AdapterParameters) KV() ggml.KV { var alpha float32 if p.LoraParameters.Alpha == 0 { alpha = float32(p.Alpha) } else { alpha = p.LoraParameters.Alpha } kv := ggml.KV{ "adapter.lora.alpha": alpha, "adapter.type": "lora", "general.file_type": uint32(1), "general.type": "adapter", "general.version": "v0.2", } return kv } func (ModelParameters) specialTokenTypes() []string { return []string{ "bos", "eos", "unk", "sep", "pad", "cls", "mask", } } type ModelConverter interface { // KV maps parameters to LLM key-values KV(*Tokenizer) ggml.KV // Tensors maps input tensors to LLM tensors. Model specific modifications can be done here. Tensors([]Tensor) []*ggml.Tensor // Replacements returns a list of string pairs to replace in tensor names. // See [strings.Replacer](https://pkg.go.dev/strings#Replacer) for details Replacements() []string // specialTokenTypes returns any special token types the model uses specialTokenTypes() []string } type moreParser interface { parseMore(fs.FS) error } type AdapterConverter interface { // KV maps parameters to LLM key-values KV(ggml.KV) ggml.KV // Tensors maps input tensors to LLM tensors. Adapter specific modifications can be done here. Tensors([]Tensor) []*ggml.Tensor // Replacements returns a list of string pairs to replace in tensor names. // See [strings.Replacer](https://pkg.go.dev/strings#Replacer) for details Replacements() []string } func ConvertAdapter(fsys fs.FS, f *os.File, baseKV ggml.KV) error { bts, err := fs.ReadFile(fsys, "adapter_config.json") if err != nil { return err } var p AdapterParameters if err := json.Unmarshal(bts, &p); err != nil { return err } arch, ok := baseKV["general.architecture"] if !ok { return errors.New("architecture not set for the base model") } var conv AdapterConverter switch arch { case "llama": conv = &llamaAdapter{} case "gemma2": conv = &gemma2Adapter{} default: return errors.New("unsupported architecture") } ts, err := parseTensors(fsys, strings.NewReplacer(conv.Replacements()...)) if err != nil { return err } if err := json.Unmarshal(bts, conv); err != nil { return err } return writeFile(f, conv.KV(baseKV), conv.Tensors(ts)) } // Convert writes an Ollama compatible model to the provided io.WriteSeeker based on configurations // and files it finds in the input path. // Supported input model formats include safetensors. // Supported input tokenizers files include tokenizer.json (preferred) and tokenizer.model. func ConvertModel(fsys fs.FS, f *os.File) error { bts, err := fs.ReadFile(fsys, "config.json") if err != nil { return err } var p ModelParameters if err := json.Unmarshal(bts, &p); err != nil { return err } if len(p.Architectures) < 1 { return errors.New("unknown architecture") } var conv ModelConverter switch p.Architectures[0] { case "LlamaForCausalLM": conv = &llamaModel{} case "MllamaForConditionalGeneration": conv = &mllamaModel{} case "Llama4ForConditionalGeneration": conv = &llama4Model{} case "Mistral3ForConditionalGeneration": conv = &mistral3Model{} case "Ministral3ForCausalLM": conv = &mistral3CausalModel{} case "MixtralForCausalLM": conv = &mixtralModel{} case "GemmaForCausalLM": conv = &gemmaModel{} case "Gemma2ForCausalLM": conv = &gemma2Model{} case "Gemma3ForCausalLM", "Gemma3ForConditionalGeneration": conv = &gemma3Model{Architecture: p.Architectures[0]} case "Gemma3nForConditionalGeneration": conv = &gemma3nModel{} case "Phi3ForCausalLM": conv = &phi3Model{} case "Qwen2ForCausalLM": conv = &qwen2Model{} case "Qwen2_5_VLForConditionalGeneration": conv = &qwen25VLModel{} case "Qwen3VLForConditionalGeneration", "Qwen3VLMoeForConditionalGeneration": conv = &qwen3VLModel{} case "Olmo3ForCausalLM": conv = &olmoModel{} case "BertModel": conv = &bertModel{} case "NomicBertModel", "NomicBertMoEModel": conv = &nomicbertModel{} case "CohereForCausalLM": conv = &commandrModel{} case "GptOssForCausalLM": conv = &gptossModel{} case "DeepseekOCRForCausalLM": conv = &deepseekocr{} case "DeepseekV3ForCausalLM": conv = &deepseek2Model{} default: return fmt.Errorf("unsupported architecture %q", p.Architectures[0]) } if err := json.Unmarshal(bts, conv); err != nil { return err } if t, ok := conv.(moreParser); ok { if err := t.parseMore(fsys); err != nil { return err } } t, err := parseTokenizer(fsys, conv.specialTokenTypes()) if err != nil { return err } vocabSize := int(cmp.Or(p.VocabSize, p.TextModel.VocabSize)) switch { case vocabSize == 0: slog.Debug("vocabulary size was not explicitly set by the model", "default size", len(t.Vocabulary.Tokens)) case vocabSize > len(t.Vocabulary.Tokens): slog.Debug("vocabulary is smaller than expected, padding with dummy tokens", "expect", vocabSize, "actual", len(t.Vocabulary.Tokens)) for i := range vocabSize - len(t.Vocabulary.Tokens) { t.Vocabulary.Tokens = append(t.Vocabulary.Tokens, fmt.Sprintf("[PAD%d]", i)) t.Vocabulary.Scores = append(t.Vocabulary.Scores, -1) t.Vocabulary.Types = append(t.Vocabulary.Types, tokenTypeUserDefined) } case vocabSize < len(t.Vocabulary.Tokens): slog.Debug("vocabulary is larger than expected", "want", vocabSize, "got", len(t.Vocabulary.Tokens)) p.VocabSize = uint32(len(t.Vocabulary.Tokens)) p.TextModel.VocabSize = uint32(len(t.Vocabulary.Tokens)) default: slog.Debug("vocabulary", "size", len(t.Vocabulary.Tokens)) } ts, err := parseTensors(fsys, strings.NewReplacer(conv.Replacements()...)) if err != nil { return err } return writeFile(f, conv.KV(t), conv.Tensors(ts)) } func writeFile(f *os.File, kv ggml.KV, ts []*ggml.Tensor) error { for i := range ts { ts[i].Shape = slices.Clone(ts[i].Shape) slices.Reverse(ts[i].Shape) } return ggml.WriteGGUF(f, kv, ts) }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/convert/convert_gemma2_adapter.go
convert/convert_gemma2_adapter.go
package convert import ( "strings" "github.com/pdevine/tensor" "github.com/pdevine/tensor/native" "github.com/ollama/ollama/fs/ggml" ) type gemma2Adapter struct { AdapterParameters } var _ AdapterConverter = (*gemma2Adapter)(nil) func (p *gemma2Adapter) KV(baseKV ggml.KV) ggml.KV { kv := p.AdapterParameters.KV() kv["general.architecture"] = "gemma2" return kv } func (p *gemma2Adapter) Tensors(ts []Tensor) []*ggml.Tensor { var out []*ggml.Tensor for _, t := range ts { shape := t.Shape() if (strings.HasSuffix(t.Name(), "weight.lora_a") && shape[0] > shape[1]) || (strings.HasSuffix(t.Name(), "weight.lora_b") && shape[0] < shape[1]) { shape[0], shape[1] = shape[1], shape[0] t.SetRepacker(p.repack) } out = append(out, &ggml.Tensor{ Name: t.Name(), Kind: t.Kind(), Shape: t.Shape(), WriterTo: t, }) } return out } func (p *gemma2Adapter) Replacements() []string { return []string{ "base_model.model.", "", "model.layers", "blk", "self_attn.q_proj", "attn_q", "self_attn.k_proj", "attn_k", "self_attn.v_proj", "attn_v", "self_attn.o_proj", "attn_output", "mlp.gate_proj", "ffn_gate", "mlp.down_proj", "ffn_down", "mlp.up_proj", "ffn_up", "lora_A.weight", "weight.lora_a", "lora_B.weight", "weight.lora_b", "lora_a", "weight.lora_a", "lora_b", "weight.lora_b", } } func (p *gemma2Adapter) repack(name string, data []float32, shape []uint64) ([]float32, error) { dims := []int{int(shape[1]), int(shape[0])} n := tensor.New(tensor.WithShape(dims...), tensor.WithBacking(data)) if err := n.T(1, 0); err != nil { return nil, err } if err := n.Reshape(dims...); err != nil { return nil, err } if err := n.Transpose(); err != nil { return nil, err } ts, err := native.SelectF32(n, 1) if err != nil { return nil, err } var f32s []float32 for _, t := range ts { f32s = append(f32s, t...) } return f32s, nil }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/convert/reader.go
convert/reader.go
package convert import ( "errors" "io" "io/fs" "strings" ) type Tensor interface { Name() string Shape() []uint64 Kind() uint32 SetRepacker(Repacker) WriteTo(io.Writer) (int64, error) Clone() Tensor } type tensorBase struct { name string shape []uint64 repacker Repacker } func (t tensorBase) Name() string { return t.name } func (t tensorBase) Shape() []uint64 { return t.shape } const ( tensorKindFP32 uint32 = iota tensorKindFP16 tensorKindBF16 = 30 tensorKindMXFP4 = 39 ) func (t tensorBase) Kind() uint32 { if strings.HasSuffix(t.name, ".ffn_gate_inp.weight") || strings.HasSuffix(t.name, ".bias") || t.name == "token_types.weight" || t.name == "v.positional_embedding_vlm" || t.name == "v.tile_position_embd.weight" || t.name == "v.pre_tile_position_embd.weight" || t.name == "v.post_tile_position_embd.weight" || t.name == "s.position_embd" || strings.HasSuffix(t.name, "rel_pos_h") || strings.HasSuffix(t.name, "rel_pos_w") { // these tensors are always F32 return tensorKindFP32 } switch len(t.shape) { case 0: panic("invalid tensor shape") case 1: return tensorKindFP32 default: return tensorKindFP16 } } func (t *tensorBase) SetRepacker(fn Repacker) { t.repacker = fn } type Repacker func(string, []float32, []uint64) ([]float32, error) func parseTensors(fsys fs.FS, replacer *strings.Replacer) ([]Tensor, error) { patterns := []struct { Pattern string Func func(fs.FS, *strings.Replacer, ...string) ([]Tensor, error) }{ {"*.safetensors", parseSafetensors}, {"pytorch_model-*-of-*.bin", parseTorch}, {"pytorch_model.bin", parseTorch}, {"consolidated.*.pth", parseTorch}, } for _, pattern := range patterns { matches, err := fs.Glob(fsys, pattern.Pattern) if err != nil { return nil, err } if len(matches) > 0 { return pattern.Func(fsys, replacer, matches...) } } return nil, errors.New("unknown tensor format") }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/convert/reader_test.go
convert/reader_test.go
package convert import ( "bytes" "encoding/binary" "os" "path/filepath" "testing" "github.com/d4l3k/go-bfloat16" "github.com/google/go-cmp/cmp" "github.com/x448/float16" ) func TestSafetensors(t *testing.T) { t.Parallel() root, err := os.OpenRoot(t.TempDir()) if err != nil { t.Fatal(err) } defer root.Close() cases := []struct { name, dtype string offset, size int64 shape []uint64 setup func(*testing.T, *os.File) want []byte }{ { name: "fp32-fp32", dtype: "F32", size: 32 * 4, // 32 floats, each 4 bytes shape: []uint64{32}, setup: func(t *testing.T, f *os.File) { f32s := make([]float32, 32) for i := range f32s { f32s[i] = float32(i) } if err := binary.Write(f, binary.LittleEndian, f32s); err != nil { t.Fatal(err) } }, want: []byte{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40, 0x40, 0x00, 0x00, 0x80, 0x40, 0x00, 0x00, 0xa0, 0x40, 0x00, 0x00, 0xc0, 0x40, 0x00, 0x00, 0xe0, 0x40, 0x00, 0x00, 0x00, 0x41, 0x00, 0x00, 0x10, 0x41, 0x00, 0x00, 0x20, 0x41, 0x00, 0x00, 0x30, 0x41, 0x00, 0x00, 0x40, 0x41, 0x00, 0x00, 0x50, 0x41, 0x00, 0x00, 0x60, 0x41, 0x00, 0x00, 0x70, 0x41, 0x00, 0x00, 0x80, 0x41, 0x00, 0x00, 0x88, 0x41, 0x00, 0x00, 0x90, 0x41, 0x00, 0x00, 0x98, 0x41, 0x00, 0x00, 0xa0, 0x41, 0x00, 0x00, 0xa8, 0x41, 0x00, 0x00, 0xb0, 0x41, 0x00, 0x00, 0xb8, 0x41, 0x00, 0x00, 0xc0, 0x41, 0x00, 0x00, 0xc8, 0x41, 0x00, 0x00, 0xd0, 0x41, 0x00, 0x00, 0xd8, 0x41, 0x00, 0x00, 0xe0, 0x41, 0x00, 0x00, 0xe8, 0x41, 0x00, 0x00, 0xf0, 0x41, 0x00, 0x00, 0xf8, 0x41, }, }, { name: "fp32-fp16", dtype: "F32", size: 32 * 4, // 32 floats, each 4 bytes shape: []uint64{16, 2}, setup: func(t *testing.T, f *os.File) { f32s := make([]float32, 32) for i := range f32s { f32s[i] = float32(i) } if err := binary.Write(f, binary.LittleEndian, f32s); err != nil { t.Fatal(err) } }, want: []byte{ 0x00, 0x00, 0x00, 0x3c, 0x00, 0x40, 0x00, 0x42, 0x00, 0x44, 0x00, 0x45, 0x00, 0x46, 0x00, 0x47, 0x00, 0x48, 0x80, 0x48, 0x00, 0x49, 0x80, 0x49, 0x00, 0x4a, 0x80, 0x4a, 0x00, 0x4b, 0x80, 0x4b, 0x00, 0x4c, 0x40, 0x4c, 0x80, 0x4c, 0xc0, 0x4c, 0x00, 0x4d, 0x40, 0x4d, 0x80, 0x4d, 0xc0, 0x4d, 0x00, 0x4e, 0x40, 0x4e, 0x80, 0x4e, 0xc0, 0x4e, 0x00, 0x4f, 0x40, 0x4f, 0x80, 0x4f, 0xc0, 0x4f, }, }, { name: "fp16-fp16", dtype: "F16", size: 32 * 2, // 32 floats, each 2 bytes shape: []uint64{16, 2}, setup: func(t *testing.T, f *os.File) { u16s := make([]uint16, 32) for i := range u16s { u16s[i] = float16.Fromfloat32(float32(i)).Bits() } if err := binary.Write(f, binary.LittleEndian, u16s); err != nil { t.Fatal(err) } }, want: []byte{ 0x00, 0x00, 0x00, 0x3c, 0x00, 0x40, 0x00, 0x42, 0x00, 0x44, 0x00, 0x45, 0x00, 0x46, 0x00, 0x47, 0x00, 0x48, 0x80, 0x48, 0x00, 0x49, 0x80, 0x49, 0x00, 0x4a, 0x80, 0x4a, 0x00, 0x4b, 0x80, 0x4b, 0x00, 0x4c, 0x40, 0x4c, 0x80, 0x4c, 0xc0, 0x4c, 0x00, 0x4d, 0x40, 0x4d, 0x80, 0x4d, 0xc0, 0x4d, 0x00, 0x4e, 0x40, 0x4e, 0x80, 0x4e, 0xc0, 0x4e, 0x00, 0x4f, 0x40, 0x4f, 0x80, 0x4f, 0xc0, 0x4f, }, }, { name: "fp16-fp32", dtype: "F16", size: 32 * 2, // 32 floats, each 2 bytes shape: []uint64{32}, setup: func(t *testing.T, f *os.File) { u16s := make([]uint16, 32) for i := range u16s { u16s[i] = float16.Fromfloat32(float32(i)).Bits() } if err := binary.Write(f, binary.LittleEndian, u16s); err != nil { t.Fatal(err) } }, want: []byte{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40, 0x40, 0x00, 0x00, 0x80, 0x40, 0x00, 0x00, 0xa0, 0x40, 0x00, 0x00, 0xc0, 0x40, 0x00, 0x00, 0xe0, 0x40, 0x00, 0x00, 0x00, 0x41, 0x00, 0x00, 0x10, 0x41, 0x00, 0x00, 0x20, 0x41, 0x00, 0x00, 0x30, 0x41, 0x00, 0x00, 0x40, 0x41, 0x00, 0x00, 0x50, 0x41, 0x00, 0x00, 0x60, 0x41, 0x00, 0x00, 0x70, 0x41, 0x00, 0x00, 0x80, 0x41, 0x00, 0x00, 0x88, 0x41, 0x00, 0x00, 0x90, 0x41, 0x00, 0x00, 0x98, 0x41, 0x00, 0x00, 0xa0, 0x41, 0x00, 0x00, 0xa8, 0x41, 0x00, 0x00, 0xb0, 0x41, 0x00, 0x00, 0xb8, 0x41, 0x00, 0x00, 0xc0, 0x41, 0x00, 0x00, 0xc8, 0x41, 0x00, 0x00, 0xd0, 0x41, 0x00, 0x00, 0xd8, 0x41, 0x00, 0x00, 0xe0, 0x41, 0x00, 0x00, 0xe8, 0x41, 0x00, 0x00, 0xf0, 0x41, 0x00, 0x00, 0xf8, 0x41, }, }, { name: "bf16-bf16", dtype: "BF16", size: 32 * 2, // 32 brain floats, each 2 bytes shape: []uint64{16, 2}, setup: func(t *testing.T, f *os.File) { f32s := make([]float32, 32) for i := range f32s { f32s[i] = float32(i) } if err := binary.Write(f, binary.LittleEndian, bfloat16.EncodeFloat32(f32s)); err != nil { t.Fatal(err) } }, want: []byte{ 0x00, 0x00, 0x80, 0x3f, 0x00, 0x40, 0x40, 0x40, 0x80, 0x40, 0xa0, 0x40, 0xc0, 0x40, 0xe0, 0x40, 0x00, 0x41, 0x10, 0x41, 0x20, 0x41, 0x30, 0x41, 0x40, 0x41, 0x50, 0x41, 0x60, 0x41, 0x70, 0x41, 0x80, 0x41, 0x88, 0x41, 0x90, 0x41, 0x98, 0x41, 0xa0, 0x41, 0xa8, 0x41, 0xb0, 0x41, 0xb8, 0x41, 0xc0, 0x41, 0xc8, 0x41, 0xd0, 0x41, 0xd8, 0x41, 0xe0, 0x41, 0xe8, 0x41, 0xf0, 0x41, 0xf8, 0x41, }, }, { name: "bf16-fp32", dtype: "BF16", size: 32 * 2, // 32 brain floats, each 2 bytes shape: []uint64{32}, setup: func(t *testing.T, f *os.File) { f32s := make([]float32, 32) for i := range f32s { f32s[i] = float32(i) } if err := binary.Write(f, binary.LittleEndian, bfloat16.EncodeFloat32(f32s)); err != nil { t.Fatal(err) } }, want: []byte{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40, 0x40, 0x00, 0x00, 0x80, 0x40, 0x00, 0x00, 0xa0, 0x40, 0x00, 0x00, 0xc0, 0x40, 0x00, 0x00, 0xe0, 0x40, 0x00, 0x00, 0x00, 0x41, 0x00, 0x00, 0x10, 0x41, 0x00, 0x00, 0x20, 0x41, 0x00, 0x00, 0x30, 0x41, 0x00, 0x00, 0x40, 0x41, 0x00, 0x00, 0x50, 0x41, 0x00, 0x00, 0x60, 0x41, 0x00, 0x00, 0x70, 0x41, 0x00, 0x00, 0x80, 0x41, 0x00, 0x00, 0x88, 0x41, 0x00, 0x00, 0x90, 0x41, 0x00, 0x00, 0x98, 0x41, 0x00, 0x00, 0xa0, 0x41, 0x00, 0x00, 0xa8, 0x41, 0x00, 0x00, 0xb0, 0x41, 0x00, 0x00, 0xb8, 0x41, 0x00, 0x00, 0xc0, 0x41, 0x00, 0x00, 0xc8, 0x41, 0x00, 0x00, 0xd0, 0x41, 0x00, 0x00, 0xd8, 0x41, 0x00, 0x00, 0xe0, 0x41, 0x00, 0x00, 0xe8, 0x41, 0x00, 0x00, 0xf0, 0x41, 0x00, 0x00, 0xf8, 0x41, }, }, { name: "u8-u8", dtype: "U8", size: 32, // 32 brain floats, each 1 bytes shape: []uint64{32}, setup: func(t *testing.T, f *os.File) { u8s := make([]uint8, 32) for i := range u8s { u8s[i] = uint8(i) } if err := binary.Write(f, binary.LittleEndian, u8s); err != nil { t.Fatal(err) } }, want: []byte{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, }, }, } for _, tt := range cases { t.Run(tt.name, func(t *testing.T) { path := filepath.Base(t.Name()) st := safetensor{ fs: root.FS(), path: path, dtype: tt.dtype, offset: tt.offset, size: tt.size, tensorBase: &tensorBase{ name: tt.name, shape: tt.shape, }, } f, err := root.Create(path) if err != nil { t.Fatal(err) } defer f.Close() tt.setup(t, f) var b bytes.Buffer if _, err := st.WriteTo(&b); err != nil { t.Fatal(err) } if diff := cmp.Diff(tt.want, b.Bytes()); diff != "" { t.Errorf("safetensor.WriteTo() mismatch (-want +got):\n%s", diff) } }) } } func TestSafetensorKind(t *testing.T) { tests := []struct { name string st safetensor expected uint32 }{ { name: "BF16 dtype with non-v. prefix and non-FP32 base kind should return BF16", st: safetensor{ tensorBase: &tensorBase{ name: "weight.matrix", shape: []uint64{10, 10}, // will default to FP16 }, dtype: "BF16", }, expected: tensorKindBF16, }, { name: "BF16 dtype with v. prefix should return base kind", st: safetensor{ tensorBase: &tensorBase{ name: "v.weight.matrix", shape: []uint64{10, 10}, // will default to FP16 }, dtype: "BF16", }, expected: tensorKindFP16, }, { name: "BF16 dtype with FP32 base kind should return FP32", st: safetensor{ tensorBase: &tensorBase{ name: "weight.matrix", shape: []uint64{10}, // will default to FP32 }, dtype: "BF16", }, expected: tensorKindFP32, }, { name: "Non-BF16 dtype should return base kind", st: safetensor{ tensorBase: &tensorBase{ name: "weight.matrix", shape: []uint64{10, 10}, // will default to FP16 }, dtype: "FP16", }, expected: tensorKindFP16, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { result := tt.st.Kind() if result != tt.expected { t.Errorf("Kind() = %d, expected %d", result, tt.expected) } }) } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/convert/convert_gptoss.go
convert/convert_gptoss.go
package convert import ( "bytes" "cmp" "encoding/binary" "io" "slices" "strings" "github.com/ollama/ollama/fs/ggml" "github.com/pdevine/tensor" "github.com/pdevine/tensor/native" ) type gptossModel struct { ModelParameters HiddenLayers uint32 `json:"num_hidden_layers"` MaxPositionEmbeddings uint32 `json:"max_position_embeddings"` HiddenSize uint32 `json:"hidden_size"` IntermediateSize uint32 `json:"intermediate_size"` AttentionHeads uint32 `json:"num_attention_heads"` KeyValueHeads uint32 `json:"num_key_value_heads"` HeadDim uint32 `json:"head_dim"` Experts uint32 `json:"num_experts"` LocalExperts uint32 `json:"num_local_experts"` ExpertsPerToken uint32 `json:"experts_per_token"` RMSNormEpsilon float32 `json:"rms_norm_eps"` InitialContextLength uint32 `json:"initial_context_length"` RopeTheta float32 `json:"rope_theta"` RopeScalingFactor float32 `json:"rope_scaling_factor"` RopeScaling struct { Factor float32 `json:"factor"` } `json:"rope_scaling"` SlidingWindow uint32 `json:"sliding_window"` } var _ ModelConverter = (*gptossModel)(nil) func (m *gptossModel) KV(t *Tokenizer) ggml.KV { kv := m.ModelParameters.KV(t) kv["general.architecture"] = "gptoss" kv["general.file_type"] = uint32(4) kv["gptoss.context_length"] = cmp.Or(m.MaxPositionEmbeddings, uint32(m.RopeScalingFactor*float32(m.InitialContextLength))) kv["gptoss.block_count"] = m.HiddenLayers kv["gptoss.embedding_length"] = m.HiddenSize kv["gptoss.feed_forward_length"] = m.IntermediateSize kv["gptoss.expert_count"] = cmp.Or(m.Experts, m.LocalExperts) kv["gptoss.expert_used_count"] = m.ExpertsPerToken kv["gptoss.attention.head_count"] = m.AttentionHeads kv["gptoss.attention.head_count_kv"] = m.KeyValueHeads kv["gptoss.attention.key_length"] = m.HeadDim kv["gptoss.attention.value_length"] = m.HeadDim kv["gptoss.attention.layer_norm_rms_epsilon"] = cmp.Or(m.RMSNormEpsilon, 1e-5) kv["gptoss.attention.sliding_window"] = m.SlidingWindow kv["gptoss.rope.freq_base"] = m.RopeTheta kv["gptoss.rope.scaling.factor"] = cmp.Or(m.RopeScalingFactor, m.RopeScaling.Factor) kv["gptoss.rope.scaling.original_context_length"] = m.InitialContextLength kv["tokenizer.ggml.bos_token_id"] = uint32(199998) // <|startoftext|> kv["tokenizer.ggml.add_bos_token"] = false kv["tokenizer.ggml.eos_token_id"] = uint32(199999) // <|endoftext|> kv["tokenizer.ggml.eos_token_ids"] = []int32{ 199999, /* <|endoftext|> */ 200002, /* <|return|> */ 200012, /* <|call|> */ } kv["tokenizer.ggml.add_eos_token"] = false return kv } func (m *gptossModel) Tensors(ts []Tensor) []*ggml.Tensor { var out []*ggml.Tensor mxfp4s := make(map[string]*mxfp4) for _, t := range ts { if strings.HasSuffix(t.Name(), ".blocks") || strings.HasSuffix(t.Name(), ".scales") { dot := strings.LastIndex(t.Name(), ".") name, suffix := t.Name()[:dot], t.Name()[dot+1:] if _, ok := mxfp4s[name]; !ok { mxfp4s[name] = &mxfp4{} } switch suffix { case "blocks": mxfp4s[name].blocks = t case "scales": mxfp4s[name].scales = t } } else if strings.HasSuffix(t.Name(), "gate_up_exps.bias") { // gate_up_exps is interleaved, need to split into gate_exps and up_exps // e.g. gate_exps, up_exps = gate_up_exps[:, 0::2, ...], gate_up_exps[:, 1::2, ...] out = append(out, slices.Collect(splitDim(t, 1, split{ Replacer: strings.NewReplacer("gate_up_exps", "gate_exps"), slices: []tensor.Slice{nil, tensor.S(0, int(t.Shape()[1]), 2)}, }, split{ Replacer: strings.NewReplacer("gate_up_exps", "up_exps"), slices: []tensor.Slice{nil, tensor.S(1, int(t.Shape()[1]), 2)}, }, ))...) } else { out = append(out, &ggml.Tensor{ Name: t.Name(), Kind: t.Kind(), Shape: t.Shape(), WriterTo: t, }) } } for name, mxfp4 := range mxfp4s { dims := mxfp4.blocks.Shape() if !strings.HasSuffix(name, ".weight") { name = name + ".weight" } if strings.Contains(name, "ffn_down_exps") { out = append(out, &ggml.Tensor{ Name: name, Kind: uint32(ggml.TensorTypeMXFP4), Shape: []uint64{dims[0], dims[1], dims[2] * dims[3] * 2}, WriterTo: mxfp4, }) } else if strings.Contains(name, "ffn_gate_up_exps") { // gate_up_exps is interleaved, need to split into gate_exps and up_exps // e.g. gate_exps, up_exps = gate_up_exps[:, 0::2, ...], gate_up_exps[:, 1::2, ...] out = append(out, &ggml.Tensor{ Name: strings.Replace(name, "gate_up", "gate", 1), Kind: uint32(ggml.TensorTypeMXFP4), Shape: []uint64{dims[0], dims[1] / 2, dims[2] * dims[3] * 2}, WriterTo: mxfp4.slice(1, 0, int(dims[1]), 2), }, &ggml.Tensor{ Name: strings.Replace(name, "gate_up", "up", 1), Kind: uint32(ggml.TensorTypeMXFP4), Shape: []uint64{dims[0], dims[1] / 2, dims[2] * dims[3] * 2}, WriterTo: mxfp4.slice(1, 1, int(dims[1]), 2), }) } } return out } func (m *gptossModel) Replacements() []string { var replacements []string if m.MaxPositionEmbeddings > 0 { // hf flavored model replacements = []string{ "lm_head", "output", "model.embed_tokens", "token_embd", "model.layers", "blk", "input_layernorm", "attn_norm", "self_attn.q_proj", "attn_q", "self_attn.k_proj", "attn_k", "self_attn.v_proj", "attn_v", "self_attn.o_proj", "attn_out", "self_attn.sinks", "attn_sinks", "post_attention_layernorm", "ffn_norm", "mlp.router", "ffn_gate_inp", "mlp.experts.gate_up_proj_", "ffn_gate_up_exps.", "mlp.experts.down_proj_", "ffn_down_exps.", "model.norm", "output_norm", } } else { replacements = []string{ // noop replacements so other replacements will not be applied ".blocks", ".blocks", ".scales", ".scales", // real replacements "block", "blk", "attn.norm", "attn_norm", "attn.qkv", "attn_qkv", "attn.sinks", "attn_sinks", "attn.out", "attn_out", "mlp.norm", "ffn_norm", "mlp.gate", "ffn_gate_inp", "mlp.mlp1_", "ffn_gate_up_exps.", "mlp.mlp2_", "ffn_down_exps.", "embedding", "token_embd", "norm", "output_norm", "unembedding", "output", "scale", "weight", } } return replacements } type mxfp4 struct { slices []tensor.Slice blocks, scales Tensor } func (m *mxfp4) slice(dim, start, end, step int) *mxfp4 { slice := slices.Repeat([]tensor.Slice{nil}, len(m.blocks.Shape())) slice[dim] = tensor.S(start, end, step) return &mxfp4{ slices: slice, blocks: m.blocks, scales: m.scales, } } func (m *mxfp4) WriteTo(w io.Writer) (int64, error) { var b bytes.Buffer if _, err := m.blocks.WriteTo(&b); err != nil { return 0, err } blocksDims := make([]int, len(m.blocks.Shape())) for i, d := range m.blocks.Shape() { blocksDims[i] = int(d) } bts := b.Bytes() var tmp [16]byte for i := 0; i < b.Len(); i += 16 { for j := range 8 { // transform a1b2c3 ... x7y8z9 -> 71xa82yb93zc a, b := bts[i+j], bts[i+j+8] tmp[2*j+0] = (a & 0x0F) | (b << 4) tmp[2*j+1] = (a >> 4) | (b & 0xF0) } copy(bts[i:i+16], tmp[:]) } var blocks tensor.Tensor = tensor.New(tensor.WithShape(blocksDims...), tensor.WithBacking(bts)) var s bytes.Buffer if _, err := m.scales.WriteTo(&s); err != nil { return 0, err } scalesDims := slices.Repeat([]int{1}, len(m.blocks.Shape())) for i, d := range m.scales.Shape() { scalesDims[i] = int(d) } var scales tensor.Tensor = tensor.New(tensor.WithShape(scalesDims...), tensor.WithBacking(s.Bytes())) out, err := tensor.Concat(3, scales, blocks) if err != nil { return 0, err } if len(m.slices) > 0 { out, err = out.Slice(m.slices...) if err != nil { return 0, err } } out = tensor.Materialize(out) if err := out.Reshape(out.Shape().TotalSize()); err != nil { return 0, err } u8s, err := native.VectorU8(out.(*tensor.Dense)) if err != nil { return 0, err } if err := binary.Write(w, binary.LittleEndian, u8s); err != nil { return 0, err } return int64(len(u8s)), nil }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/convert/convert_gemma.go
convert/convert_gemma.go
package convert import ( "strings" "github.com/pdevine/tensor" "github.com/pdevine/tensor/native" "github.com/ollama/ollama/fs/ggml" ) type gemmaModel struct { ModelParameters MaxPositionEmbeddings uint32 `json:"max_position_embeddings"` HiddenSize uint32 `json:"hidden_size"` HiddenLayers uint32 `json:"num_hidden_layers"` IntermediateSize uint32 `json:"intermediate_size"` NumAttentionHeads uint32 `json:"num_attention_heads"` NumKeyValueHeads uint32 `json:"num_key_value_heads"` RMSNormEPS float32 `json:"rms_norm_eps"` HeadDim uint32 `json:"head_dim"` } var _ ModelConverter = (*gemmaModel)(nil) func (p *gemmaModel) KV(t *Tokenizer) ggml.KV { kv := p.ModelParameters.KV(t) kv["general.architecture"] = "gemma" kv["gemma.context_length"] = p.MaxPositionEmbeddings kv["gemma.embedding_length"] = p.HiddenSize kv["gemma.block_count"] = p.HiddenLayers kv["gemma.feed_forward_length"] = p.IntermediateSize kv["gemma.attention.head_count"] = p.NumAttentionHeads kv["gemma.attention.head_count_kv"] = p.NumKeyValueHeads kv["gemma.attention.layer_norm_rms_epsilon"] = p.RMSNormEPS kv["gemma.attention.key_length"] = p.HeadDim kv["gemma.attention.value_length"] = p.HeadDim kv["tokenizer.ggml.eot_token_id"] = uint32(107) kv["tokenizer.ggml.middle_token_id"] = uint32(68) kv["tokenizer.ggml.prefix_token_id"] = uint32(67) kv["tokenizer.ggml.suffix_token_id"] = uint32(69) return kv } func (p *gemmaModel) Tensors(ts []Tensor) []*ggml.Tensor { var out []*ggml.Tensor for _, t := range ts { if !strings.HasPrefix(t.Name(), "v.") && strings.HasSuffix(t.Name(), "_norm.weight") { t.SetRepacker(p.addOne) } out = append(out, &ggml.Tensor{ Name: t.Name(), Kind: t.Kind(), Shape: t.Shape(), WriterTo: t, }) } return out } func (p *gemmaModel) Replacements() []string { return []string{ "model.embed_tokens", "token_embd", "model.norm", "output_norm", "model.layers", "blk", "input_layernorm", "attn_norm", "self_attn.q_proj", "attn_q", "self_attn.k_proj", "attn_k", "self_attn.v_proj", "attn_v", "self_attn.o_proj", "attn_output", "mlp.gate_proj", "ffn_gate", "mlp.down_proj", "ffn_down", "mlp.up_proj", "ffn_up", "post_attention_layernorm", "ffn_norm", } } func (*gemmaModel) addOne(_ string, data []float32, shape []uint64) ([]float32, error) { n := tensor.New(tensor.WithShape(int(shape[0])), tensor.WithBacking(data)) ones := tensor.Ones(tensor.Float32, int(shape[0])) n, err := n.Add(ones) if err != nil { return nil, err } ts, err := native.SelectF32(n, 0) if err != nil { return nil, err } var f32s []float32 for _, t := range ts { f32s = append(f32s, t...) } return f32s, nil }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/convert/convert_gemma3.go
convert/convert_gemma3.go
package convert import ( "cmp" "slices" "github.com/ollama/ollama/fs/ggml" ) type gemma3Model struct { gemmaModel Architecture string TextModel struct { HeadDim uint32 `json:"head_dim"` HiddenSize uint32 `json:"hidden_size"` HiddenLayers uint32 `json:"num_hidden_layers"` IntermediateSize uint32 `json:"intermediate_size"` SlidingWindow uint32 `json:"sliding_window"` } `json:"text_config"` VisionModel struct { NumAttentionHeads uint32 `json:"num_attention_heads"` // attention.head_count 16 LayerNormEpsilon float32 `json:"layer_norm_eps"` // attention.layer_norm_epsilon 1e-05 NumHiddenLayers uint32 `json:"num_hidden_layers"` // block_count 32 HiddenSize uint32 `json:"hidden_size"` // embedding_length 1280 IntermediateSize uint32 `json:"intermediate_size"` // feed_forward_length 5120 ImageSize uint32 `json:"image_size"` // image_size 560 NumChannels uint32 `json:"num_channels"` // num_channels 3 PatchSize uint32 `json:"patch_size"` // patch_size 14 } `json:"vision_config"` MaxPositionEmbeddings uint32 `json:"max_position_embeddings"` NumAttentionHeads uint32 `json:"num_attention_heads"` NumKeyValueHeads uint32 `json:"num_key_value_heads"` RMSNormEPS float32 `json:"rms_norm_eps"` HeadDim uint32 `json:"head_dim"` FinalLogitSoftcap float32 `json:"final_logit_softcapping"` RopeLocalTheta float32 `json:"rope_local_base_freq"` RopeTheta float32 `json:"rope_theta"` SlidingWindow uint32 `json:"sliding_window"` SlidingWindowPattern *uint32 `json:"sliding_window_pattern"` LayerTypes []string `json:"layer_types"` MultiModalTokensPerImage uint32 `json:"mm_tokens_per_image"` RopeScaling *struct { Type string `json:"rope_type"` Factor float32 `json:"factor"` OriginalMaxPositionEmbeddings uint32 `json:"original_max_position_embeddings"` ExtrapolationFactor float32 `json:"extrapolation_factor"` BetaFast float32 `json:"beta_fast"` BetaSlow float32 `json:"beta_slow"` } `json:"rope_scaling"` } const ( gemma4BLayerCount = 34 gemma12BLayerCount = 48 gemma27BLayerCount = 62 ) func (p *gemma3Model) KV(t *Tokenizer) ggml.KV { kv := p.ModelParameters.KV(t) kv["general.architecture"] = "gemma3" numBlocks := cmp.Or(p.HiddenLayers, p.TextModel.HiddenLayers) kv["gemma3.block_count"] = numBlocks var ( numHeads uint32 numKVHeads uint32 ) switch numBlocks { case gemma4BLayerCount: numHeads = 8 numKVHeads = 4 case gemma12BLayerCount: numHeads = 16 numKVHeads = 8 case gemma27BLayerCount: numHeads = 32 numKVHeads = 16 default: numHeads = p.NumAttentionHeads numKVHeads = p.NumKeyValueHeads } kv["gemma3.attention.head_count"] = numHeads kv["gemma3.attention.head_count_kv"] = numKVHeads switch p.Architecture { case "Gemma3ForCausalLM": kv["gemma3.context_length"] = p.MaxPositionEmbeddings kv["gemma3.attention.layer_norm_rms_epsilon"] = p.RMSNormEPS kv["gemma3.attention.key_length"] = p.HeadDim kv["gemma3.attention.value_length"] = p.HeadDim kv["gemma3.attention.sliding_window"] = p.SlidingWindow // The sliding window pattern is either provided as the sliding_window_pattern // key (an int) or as the layer_types key (a list of strings). if p.SlidingWindowPattern != nil || len(p.LayerTypes) > 0 { kv["gemma3.attention.sliding_window_pattern"] = slices.Collect(func(yield func(bool) bool) { for i := range numBlocks { var isLocal bool if len(p.LayerTypes) > 0 && int(i) < len(p.LayerTypes) { isLocal = p.LayerTypes[i] == "sliding_attention" } else if p.SlidingWindowPattern != nil && *p.SlidingWindowPattern > 0 { isLocal = (i+1)%*p.SlidingWindowPattern != 0 } if !yield(isLocal) { break } } }) } if p.FinalLogitSoftcap > 0 { kv["gemma3.final_logit_softcapping"] = p.FinalLogitSoftcap } kv["gemma3.rope.local.freq_base"] = cmp.Or(p.RopeLocalTheta, 10000.0) kv["gemma3.rope.freq_base"] = cmp.Or(p.RopeTheta, 1000000.0) if p.RopeScaling != nil && p.RopeScaling.Type == "yarn" && p.RopeScaling.Factor > 0 { kv["gemma3.rope.scaling.type"] = "yarn" kv["gemma3.rope.scaling.factor"] = p.RopeScaling.Factor kv["gemma3.rope.scaling.original_context_length"] = p.RopeScaling.OriginalMaxPositionEmbeddings kv["gemma3.rope.scaling.extrapolation_factor"] = cmp.Or(p.RopeScaling.ExtrapolationFactor, float32(1.0)) kv["gemma3.rope.scaling.beta_fast"] = cmp.Or(p.RopeScaling.BetaFast, float32(64.0)) kv["gemma3.rope.scaling.beta_slow"] = cmp.Or(p.RopeScaling.BetaSlow, float32(1.0)) } kv["gemma3.embedding_length"] = p.HiddenSize kv["gemma3.feed_forward_length"] = p.IntermediateSize default: kv["gemma3.context_length"] = cmp.Or(p.MaxPositionEmbeddings, 131072) kv["gemma3.embedding_length"] = p.TextModel.HiddenSize kv["gemma3.feed_forward_length"] = p.TextModel.IntermediateSize kv["gemma3.attention.sliding_window"] = p.TextModel.SlidingWindow kv["gemma3.vision.block_count"] = p.VisionModel.NumHiddenLayers kv["gemma3.vision.embedding_length"] = p.VisionModel.HiddenSize kv["gemma3.vision.feed_forward_length"] = p.VisionModel.IntermediateSize kv["gemma3.vision.image_size"] = p.VisionModel.ImageSize kv["gemma3.vision.patch_size"] = p.VisionModel.PatchSize kv["gemma3.vision.num_channels"] = cmp.Or(p.VisionModel.NumChannels, 3) kv["gemma3.vision.attention.head_count"] = p.VisionModel.NumAttentionHeads kv["gemma3.vision.attention.layer_norm_epsilon"] = cmp.Or(p.VisionModel.LayerNormEpsilon, 1e-6) kv["gemma3.attention.key_length"] = cmp.Or(p.TextModel.HeadDim, 256) kv["gemma3.attention.value_length"] = cmp.Or(p.TextModel.HeadDim, 256) } if p.MultiModalTokensPerImage > 0 { kv["gemma3.mm.tokens_per_image"] = p.MultiModalTokensPerImage } return kv } func (p *gemma3Model) Replacements() []string { return []string{ "lm_head", "output", "model.embed_tokens", "token_embd", "model.norm", "output_norm", "vision_tower.vision_model.embeddings", "v", "vision_tower.vision_model", "v", "vision_model.vision_model.embeddings", "v", "vision_model.vision_model", "v", "language_model.", "", "model.layers", "blk", "encoder.layers", "blk", "input_layernorm", "attn_norm", "self_attn.q_proj", "attn_q", "self_attn.q_norm", "attn_q_norm", "self_attn.k_proj", "attn_k", "self_attn.k_norm", "attn_k_norm", "self_attn.v_proj", "attn_v", "self_attn.o_proj", "attn_output", "self_attn.out_proj", "attn_output", "mlp.gate_proj", "ffn_gate", "mlp.down_proj", "ffn_down", "mlp.up_proj", "ffn_up", "post_attention_layernorm", "post_attention_norm", "pre_feedforward_layernorm", "ffn_norm", "post_feedforward_layernorm", "post_ffw_norm", "input_projection_weight", "input_projection.weight", "multi_modal_projector", "mm", } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/convert/convert_llama4.go
convert/convert_llama4.go
package convert import ( "slices" "strings" "github.com/pdevine/tensor" "github.com/pdevine/tensor/native" "github.com/ollama/ollama/fs/ggml" ) type llama4Model struct { ModelParameters TextModel struct { llamaModel NumExpertsPerToken uint32 `json:"num_experts_per_tok"` NumLocalExperts uint32 `json:"num_local_experts"` InterleaveMOELayerStep uint32 `json:"interleave_moe_layer_step"` UseQKNorm bool `json:"use_qk_norm"` IntermediateSizeMLP uint32 `json:"intermediate_size_mlp"` AttentionChunkSize uint32 `json:"attention_chunk_size"` } `json:"text_config"` VisionModel struct { NumHiddenLayers uint32 `json:"num_hidden_layers"` HiddenSize uint32 `json:"hidden_size"` IntermediateSize uint32 `json:"intermediate_size"` NumAttentionHeads uint32 `json:"num_attention_heads"` ImageSize uint32 `json:"image_size"` PatchSize uint32 `json:"patch_size"` RopeTheta float32 `json:"rope_theta"` NormEpsilon float32 `json:"norm_eps"` PixelShuffleRatio float32 `json:"pixel_shuffle_ratio"` } `json:"vision_config"` } // KV implements ModelConverter. func (p *llama4Model) KV(t *Tokenizer) ggml.KV { kv := p.ModelParameters.KV(t) kv["general.architecture"] = "llama4" for k, v := range p.TextModel.KV(t) { if strings.HasPrefix(k, "llama.") { kv[strings.ReplaceAll(k, "llama.", "llama4.")] = v } } kv["llama4.feed_forward_length"] = p.TextModel.IntermediateSizeMLP kv["llama4.expert_feed_forward_length"] = p.TextModel.IntermediateSize kv["llama4.expert_count"] = p.TextModel.NumLocalExperts kv["llama4.expert_used_count"] = p.TextModel.NumExpertsPerToken kv["llama4.interleave_moe_layer_step"] = p.TextModel.InterleaveMOELayerStep kv["llama4.use_qk_norm"] = p.TextModel.UseQKNorm kv["llama4.attention.chunk_size"] = p.TextModel.AttentionChunkSize kv["llama4.vision.block_count"] = p.VisionModel.NumHiddenLayers kv["llama4.vision.embedding_length"] = p.VisionModel.HiddenSize kv["llama4.vision.feed_forward_length"] = p.VisionModel.IntermediateSize kv["llama4.vision.attention.head_count"] = p.VisionModel.NumAttentionHeads kv["llama4.vision.image_size"] = p.VisionModel.ImageSize kv["llama4.vision.patch_size"] = p.VisionModel.PatchSize kv["llama4.vision.rope.freq_base"] = p.VisionModel.RopeTheta kv["llama4.vision.layer_norm_epsilon"] = p.VisionModel.NormEpsilon kv["llama4.vision.pixel_shuffle_ratio"] = p.VisionModel.PixelShuffleRatio return kv } // Replacements implements ModelConverter. func (p *llama4Model) Replacements() []string { return append( p.TextModel.Replacements(), "language_model.", "", "vision_model", "v", "multi_modal_projector", "mm", "feed_forward.down_proj", "ffn_down", "feed_forward.up_proj", "ffn_up", "feed_forward.gate_proj", "ffn_gate", "feed_forward.", "ffn_", "shared_expert.down_proj", "down_shexp", "shared_expert.gate_proj", "gate_shexp", "shared_expert.up_proj", "up_shexp", "experts.down_proj", "down_exps.weight", "experts.gate_up_proj", "gate_up_exps.weight", "router", "gate_inp", "patch_embedding.linear", "patch_embedding", ) } // Tensors implements ModelConverter. func (p *llama4Model) Tensors(ts []Tensor) []*ggml.Tensor { var out []*ggml.Tensor var textTensors []Tensor for _, t := range ts { if strings.HasPrefix(t.Name(), "v.") || strings.HasPrefix(t.Name(), "mm.") { out = append(out, &ggml.Tensor{ Name: t.Name(), Kind: t.Kind(), Shape: t.Shape(), WriterTo: t, }) } else if strings.Contains(t.Name(), "ffn_gate_up_exps") { // gate and up projectors are fused // dims[1], dims[2] must be swapped // [experts, hidden_size, intermediate_size * 2] --> [experts, intermediate_size, hidden_size] halfDim := int(t.Shape()[2]) / 2 newShape := slices.Clone(t.Shape()) newShape[1], newShape[2] = newShape[2]/2, newShape[1] for i, name := range []string{"ffn_gate_exps", "ffn_up_exps"} { // clone tensor since we need separate repackers tt := t.Clone() tt.SetRepacker(p.repack(nil, nil, tensor.S(i*halfDim, (i+1)*halfDim))) out = append(out, &ggml.Tensor{ Name: strings.ReplaceAll(tt.Name(), "ffn_gate_up_exps", name), Kind: tt.Kind(), Shape: newShape, WriterTo: tt, }) } } else if strings.Contains(t.Name(), "ffn_down_exps") { // dims[1], dims[2] must be swapped // [experts, intermediate_size, hidden_size] --> [experts, hidden_size, intermediate_size] t.SetRepacker(p.repack()) newShape := slices.Clone(t.Shape()) newShape[1], newShape[2] = newShape[2], newShape[1] out = append(out, &ggml.Tensor{ Name: t.Name(), Kind: t.Kind(), Shape: newShape, WriterTo: t, }) } else { textTensors = append(textTensors, t) } } p.TextModel.skipRepack = true out = append(out, p.TextModel.Tensors(textTensors)...) return out } func (p *llama4Model) repack(slice ...tensor.Slice) Repacker { return func(name string, data []float32, shape []uint64) ([]float32, error) { dims := make([]int, len(shape)) for i, dim := range shape { dims[i] = int(dim) } var t tensor.Tensor = tensor.New(tensor.WithShape(dims...), tensor.WithBacking(data)) t, err := t.Slice(slice...) if err != nil { return nil, err } if err := t.T(0, 2, 1); err != nil { return nil, err } t = tensor.Materialize(t) // flatten tensor so it can be return as a vector if err := t.Reshape(t.Shape().TotalSize()); err != nil { return nil, err } return native.VectorF32(t.(*tensor.Dense)) } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/convert/tokenizer_test.go
convert/tokenizer_test.go
package convert import ( "io" "io/fs" "os" "path/filepath" "strings" "testing" "github.com/google/go-cmp/cmp" ) func createTokenizerFS(t *testing.T, dir string, files map[string]io.Reader) fs.FS { t.Helper() for k, v := range files { if err := func() error { f, err := os.Create(filepath.Join(dir, k)) if err != nil { return err } defer f.Close() if _, err := io.Copy(f, v); err != nil { return err } return nil }(); err != nil { t.Fatalf("unexpected error: %v", err) } } return os.DirFS(dir) } func TestParseTokenizer(t *testing.T) { cases := []struct { name string fsys fs.FS specialTokenTypes []string want *Tokenizer }{ { name: "string chat template", fsys: createTokenizerFS(t, t.TempDir(), map[string]io.Reader{ "tokenizer.json": strings.NewReader(`{}`), "tokenizer_config.json": strings.NewReader(`{ "chat_template": "<default template>" }`), }), want: &Tokenizer{ Vocabulary: &Vocabulary{Model: "gpt2"}, Pre: "default", Template: "<default template>", }, }, { name: "list chat template", fsys: createTokenizerFS(t, t.TempDir(), map[string]io.Reader{ "tokenizer.json": strings.NewReader(`{}`), "tokenizer_config.json": strings.NewReader(`{ "chat_template": [ { "name": "default", "template": "<default template>" }, { "name": "tools", "template": "<tools template>" } ] }`), }), want: &Tokenizer{ Vocabulary: &Vocabulary{Model: "gpt2"}, Pre: "default", Template: "<default template>", }, }, { name: "added tokens", fsys: createTokenizerFS(t, t.TempDir(), map[string]io.Reader{ "tokenizer.json": strings.NewReader(`{ "added_tokens": [ { "id": 999, "content": "<unused999>", "special": false } ] }`), }), want: &Tokenizer{ Vocabulary: &Vocabulary{ Model: "gpt2", Tokens: []string{"<unused999>"}, Scores: []float32{999}, Types: []int32{4}, }, Pre: "default", }, }, { name: "added tokens overlap vocab", fsys: createTokenizerFS(t, t.TempDir(), map[string]io.Reader{ "tokenizer.json": strings.NewReader(`{ "added_tokens": [ { "id": 0, "content": "<pad>", "special": true } ], "model": { "vocab": { "<pad>": 0 } } }`), }), want: &Tokenizer{ Vocabulary: &Vocabulary{ Model: "gpt2", Tokens: []string{"<pad>"}, Scores: []float32{0}, Types: []int32{3}, }, Pre: "default", }, }, { name: "special token types", fsys: createTokenizerFS(t, t.TempDir(), map[string]io.Reader{ "tokenizer.json": strings.NewReader(`{ "added_tokens": [ { "id": 0, "content": "<pad>", "special": true }, { "id": 1, "content": "<eos>", "special": true }, { "id": 2, "content": "<bos>", "special": true }, { "id": 3, "content": "<unk>", "special": true } ], "model": { "vocab": { "<pad>": 0, "<eos>": 1, "<bos>": 2, "<unk>": 3 } } }`), "tokenizer_config.json": strings.NewReader(`{ "add_bos_token": true, "add_eos_token": false, "bos_token": "<bos>", "eos_token": "<eos>", "pad_token": "<pad>", "unk_token": "<unk>" }`), }), specialTokenTypes: []string{"pad", "eos", "bos", "unk"}, want: &Tokenizer{ Vocabulary: &Vocabulary{ Model: "gpt2", Tokens: []string{"<pad>", "<eos>", "<bos>", "<unk>"}, Scores: []float32{0, 1, 2, 3}, Types: []int32{3, 3, 3, 3}, }, SpecialVocabulary: []*SpecialVocabulary{ {Type: "pad", Content: "<pad>", ID: 0, AddToken: false}, {Type: "eos", Content: "<eos>", ID: 1, AddToken: false}, {Type: "bos", Content: "<bos>", ID: 2, AddToken: true}, {Type: "unk", Content: "<unk>", ID: 3, AddToken: false}, }, Pre: "default", }, }, { name: "list string merges", fsys: createTokenizerFS(t, t.TempDir(), map[string]io.Reader{ "tokenizer.json": strings.NewReader(`{ "model": { "merges": [ "a b", "c d", "e f" ] } }`), }), want: &Tokenizer{ Vocabulary: &Vocabulary{ Model: "gpt2", }, Merges: []string{ "a b", "c d", "e f", }, Pre: "default", }, }, { name: "list list string merges", fsys: createTokenizerFS(t, t.TempDir(), map[string]io.Reader{ "tokenizer.json": strings.NewReader(`{ "model": { "merges": [ [ "a", "b" ], [ "c", "d" ], [ "e", "f" ] ] } }`), }), want: &Tokenizer{ Vocabulary: &Vocabulary{ Model: "gpt2", }, Merges: []string{ "a b", "c d", "e f", }, Pre: "default", }, }, { name: "generation config eos token ids", fsys: createTokenizerFS(t, t.TempDir(), map[string]io.Reader{ "tokenizer.json": strings.NewReader(`{ "added_tokens": [ { "id": 0, "content": "<bos>", "special": true }, { "id": 1, "content": "<eos>", "special": true }, { "id": 2, "content": "<eot>", "special": true }, { "id": 3, "content": "<eom>", "special": true } ], "model": { "vocab": { "<bos>": 0, "<eos>": 1, "<eot>": 2, "<eom>": 3 } } }`), "tokenizer_config.json": strings.NewReader(`{ "add_bos_token": true, "add_eos_token": false, "bos_token": "<bos>", "eos_token": "<eos>" }`), "generation_config.json": strings.NewReader(`{ "bos_token_id": 0, "eos_token_id": [1, 2, 3] }`), }), specialTokenTypes: []string{"pad", "eos", "bos", "unk"}, want: &Tokenizer{ Vocabulary: &Vocabulary{ Model: "gpt2", Tokens: []string{"<bos>", "<eos>", "<eot>", "<eom>"}, Scores: []float32{0, 1, 2, 3}, Types: []int32{3, 3, 3, 3}, }, SpecialVocabulary: []*SpecialVocabulary{ {Type: "eos", Content: "<eos>", ID: 1, IDs: []int32{1, 2, 3}, AddToken: false}, {Type: "bos", Content: "<bos>", ID: 0, AddToken: true}, }, Pre: "default", }, }, } for _, tt := range cases { t.Run(tt.name, func(t *testing.T) { tokenizer, err := parseTokenizer(tt.fsys, tt.specialTokenTypes) if err != nil { t.Fatalf("unexpected error: %v", err) } if diff := cmp.Diff(tt.want, tokenizer); diff != "" { t.Errorf("unexpected tokenizer (-want +got):\n%s", diff) } }) } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false
ollama/ollama
https://github.com/ollama/ollama/blob/626af2d80973270c4d59b8df7153ac47ad67ed7b/convert/convert_olmo.go
convert/convert_olmo.go
package convert import ( "cmp" "github.com/ollama/ollama/fs/ggml" ) type ropeScaling struct { Factor float32 `json:"factor"` OriginalMaxPositionEmbeds uint32 `json:"original_max_position_embeddings"` AttentionFactor float32 `json:"attention_factor"` BetaFast float32 `json:"beta_fast"` BetaSlow float32 `json:"beta_slow"` RopeType string `json:"rope_type"` ExtrapolationFactor float32 `json:"extrapolation_factor"` } type olmoModel struct { ModelParameters HiddenSize uint32 `json:"hidden_size"` NumHiddenLayers uint32 `json:"num_hidden_layers"` IntermediateSize uint32 `json:"intermediate_size"` NumAttentionHeads uint32 `json:"num_attention_heads"` NumKeyValueHeads uint32 `json:"num_key_value_heads"` MaxPositionEmbeddings uint32 `json:"max_position_embeddings"` RMSNormEPS float32 `json:"rms_norm_eps"` RopeTheta float32 `json:"rope_theta"` RopeScaling *ropeScaling `json:"rope_scaling"` SlidingWindow uint32 `json:"sliding_window"` LayerTypes []string `json:"layer_types"` } var _ ModelConverter = (*olmoModel)(nil) func (p *olmoModel) KV(t *Tokenizer) ggml.KV { kv := p.ModelParameters.KV(t) kv["general.architecture"] = "olmo3" kv["olmo3.block_count"] = p.NumHiddenLayers kv["olmo3.context_length"] = p.MaxPositionEmbeddings kv["olmo3.embedding_length"] = p.HiddenSize kv["olmo3.feed_forward_length"] = p.IntermediateSize kv["olmo3.attention.head_count"] = p.NumAttentionHeads kv["olmo3.attention.head_count_kv"] = cmp.Or(p.NumKeyValueHeads, p.NumAttentionHeads) if p.RopeTheta > 0 { kv["olmo3.rope.freq_base"] = p.RopeTheta } if p.RopeScaling != nil { if p.RopeScaling.Factor > 0 { kv["olmo3.rope.scaling.factor"] = p.RopeScaling.Factor } if p.RopeScaling.OriginalMaxPositionEmbeds > 0 { kv["olmo3.rope.scaling.original_context_length"] = p.RopeScaling.OriginalMaxPositionEmbeds } if p.RopeScaling.AttentionFactor > 0 { kv["olmo3.rope.scaling.attn_factor"] = p.RopeScaling.AttentionFactor } if p.RopeScaling.RopeType != "" { kv["olmo3.rope.scaling.type"] = p.RopeScaling.RopeType } } if p.RMSNormEPS > 0 { kv["olmo3.attention.layer_norm_rms_epsilon"] = p.RMSNormEPS } if p.SlidingWindow > 0 { kv["olmo3.attention.sliding_window"] = p.SlidingWindow } if len(p.LayerTypes) > 0 { slidingPattern := make([]bool, len(p.LayerTypes)) for i, layerType := range p.LayerTypes { slidingPattern[i] = (layerType == "sliding_attention") } kv["olmo3.attention.sliding_window_pattern"] = slidingPattern } return kv } func (p *olmoModel) Tensors(ts []Tensor) []*ggml.Tensor { out := make([]*ggml.Tensor, 0, len(ts)) for _, t := range ts { out = append(out, &ggml.Tensor{ Name: t.Name(), Kind: t.Kind(), Shape: t.Shape(), WriterTo: t, }) } return out } func (p *olmoModel) Replacements() []string { return []string{ "lm_head", "output", "model.embed_tokens", "token_embd", "model.layers", "blk", "model.norm", "output_norm", "self_attn.q_proj", "attn_q", "self_attn.k_proj", "attn_k", "self_attn.v_proj", "attn_v", "self_attn.o_proj", "attn_output", "self_attn.q_norm", "attn_q_norm", "self_attn.k_norm", "attn_k_norm", "post_attention_layernorm", "post_attention_norm", "post_feedforward_layernorm", "post_ffw_norm", "mlp.gate_proj", "ffn_gate", "mlp.down_proj", "ffn_down", "mlp.up_proj", "ffn_up", } }
go
MIT
626af2d80973270c4d59b8df7153ac47ad67ed7b
2026-01-07T08:35:43.337630Z
false