From c895a7d13f74c66aee4c68aed75aaeddb7fbcf18 Mon Sep 17 00:00:00 2001 From: Michael Yang Date: Tue, 21 May 2024 22:07:57 -0700 Subject: [PATCH] some gocritic --- .golangci.yaml | 2 ++ api/types.go | 2 +- convert/llama.go | 7 ++++--- convert/safetensors.go | 2 +- convert/tokenizer.go | 7 ++----- convert/torch.go | 2 +- envconfig/config.go | 2 +- llm/server.go | 13 +++++++------ server/sched.go | 2 +- types/model/name_test.go | 2 +- 10 files changed, 21 insertions(+), 20 deletions(-) diff --git a/.golangci.yaml b/.golangci.yaml index 7dec49de..df966a16 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -14,4 +14,6 @@ linters: # - goimports - misspell - nilerr + - nolintlint + - nosprintfhostport - unused diff --git a/api/types.go b/api/types.go index 4195a7c5..230f58e8 100644 --- a/api/types.go +++ b/api/types.go @@ -306,7 +306,7 @@ type GenerateResponse struct { // Model is the model name that generated the response. Model string `json:"model"` - //CreatedAt is the timestamp of the response. + // CreatedAt is the timestamp of the response. CreatedAt time.Time `json:"created_at"` // Response is the textual response itself. diff --git a/convert/llama.go b/convert/llama.go index 7853c4cf..b4211b02 100644 --- a/convert/llama.go +++ b/convert/llama.go @@ -119,11 +119,12 @@ func llamaRepack(name string, params *Params, data []float32, shape []uint64) ([ } var heads int - if strings.HasSuffix(name, "attn_q.weight") { + switch { + case strings.HasSuffix(name, "attn_q.weight"): heads = params.AttentionHeads - } else if strings.HasSuffix(name, "attn_k.weight") { + case strings.HasSuffix(name, "attn_k.weight"): heads = cmp.Or(params.KeyValHeads, params.AttentionHeads) - } else { + default: return nil, fmt.Errorf("unknown tensor name: %s", name) } diff --git a/convert/safetensors.go b/convert/safetensors.go index 69270b87..f45687f1 100644 --- a/convert/safetensors.go +++ b/convert/safetensors.go @@ -120,7 +120,7 @@ func (m *SafetensorFormat) readTensors(fn string, offset uint64, params *Params) Name: name, Kind: kind, Offset: offset, - Shape: shape[:], + Shape: shape, } t.WriterTo = safetensorWriterTo{ diff --git a/convert/tokenizer.go b/convert/tokenizer.go index efeb5491..fd6df5f5 100644 --- a/convert/tokenizer.go +++ b/convert/tokenizer.go @@ -85,11 +85,8 @@ func parseTokens(dirpath string) (pre string, tokens []Token, merges []string, e sha256sum := sha256.New() for _, pt := range t.PreTokenizer.PreTokenizers { - switch pt.Type { - case "Split": - if pt.Pattern.Regex != "" { - sha256sum.Write([]byte(pt.Pattern.Regex)) - } + if pt.Type == "Split" && pt.Pattern.Regex != "" { + sha256sum.Write([]byte(pt.Pattern.Regex)) } } diff --git a/convert/torch.go b/convert/torch.go index b7ae0f76..eef41a48 100644 --- a/convert/torch.go +++ b/convert/torch.go @@ -88,7 +88,7 @@ func (tf *TorchFormat) GetTensors(dirpath string, params *Params) ([]llm.Tensor, Name: ggufName, Kind: kind, Offset: offset, // calculate the offset - Shape: shape[:], + Shape: shape, } tensor.WriterTo = torchWriterTo{ diff --git a/envconfig/config.go b/envconfig/config.go index 875c9039..77e3e789 100644 --- a/envconfig/config.go +++ b/envconfig/config.go @@ -127,7 +127,7 @@ func LoadConfig() { var paths []string for _, root := range []string{filepath.Dir(appExe), cwd} { paths = append(paths, - filepath.Join(root), + root, filepath.Join(root, "windows-"+runtime.GOARCH), filepath.Join(root, "dist", "windows-"+runtime.GOARCH), ) diff --git a/llm/server.go b/llm/server.go index 3af8a329..f4027865 100644 --- a/llm/server.go +++ b/llm/server.go @@ -104,21 +104,22 @@ func NewLlamaServer(gpus gpu.GpuInfoList, model string, ggml *GGML, adapters, pr var layers int layers, estimatedVRAM, estimatedTotal = EstimateGPULayers(gpus, ggml, projectors, opts) - if gpus[0].Library == "metal" && estimatedVRAM > systemMemory { + switch { + case gpus[0].Library == "metal" && estimatedVRAM > systemMemory: // disable partial offloading when model is greater than total system memory as this // can lead to locking up the system opts.NumGPU = 0 - } else if gpus[0].Library != "metal" && layers == 0 { + case gpus[0].Library != "metal" && layers == 0: // Don't bother loading into the GPU if no layers can fit cpuRunner = serverForCpu() gpuCount = 0 - } else if opts.NumGPU < 0 && layers > 0 && gpus[0].Library != "cpu" { + case opts.NumGPU < 0 && layers > 0 && gpus[0].Library != "cpu": opts.NumGPU = layers } } // Loop through potential servers - finalErr := fmt.Errorf("no suitable llama servers found") + finalErr := errors.New("no suitable llama servers found") if len(adapters) > 1 { return nil, errors.New("ollama supports only one lora adapter, but multiple were provided") @@ -284,7 +285,7 @@ func NewLlamaServer(gpus gpu.GpuInfoList, model string, ggml *GGML, adapters, pr server := filepath.Join(dir, "ollama_llama_server") if runtime.GOOS == "windows" { - server = server + ".exe" + server += ".exe" } // Detect tmp cleaners wiping out the file @@ -459,7 +460,7 @@ func (s *llmServer) getServerStatus(ctx context.Context) (ServerStatus, error) { resp, err := http.DefaultClient.Do(req) if err != nil { if errors.Is(err, context.DeadlineExceeded) { - return ServerStatusNotResponding, fmt.Errorf("server not responding") + return ServerStatusNotResponding, errors.New("server not responding") } return ServerStatusError, fmt.Errorf("health resp: %w", err) } diff --git a/server/sched.go b/server/sched.go index 46fe2f60..3694b4d0 100644 --- a/server/sched.go +++ b/server/sched.go @@ -66,7 +66,7 @@ func (s *Scheduler) GetRunner(c context.Context, model *Model, opts api.Options, opts.NumCtx = 4 } - opts.NumCtx = opts.NumCtx * envconfig.NumParallel + opts.NumCtx *= envconfig.NumParallel req := &LlmRequest{ ctx: c, diff --git a/types/model/name_test.go b/types/model/name_test.go index 26d70ef3..c88fffdb 100644 --- a/types/model/name_test.go +++ b/types/model/name_test.go @@ -325,7 +325,7 @@ func TestParseNameFromFilepath(t *testing.T) { filepath.Join("host:port", "namespace", "model", "tag"): {Host: "host:port", Namespace: "namespace", Model: "model", Tag: "tag"}, filepath.Join("namespace", "model", "tag"): {}, filepath.Join("model", "tag"): {}, - filepath.Join("model"): {}, + "model": {}, filepath.Join("..", "..", "model", "tag"): {}, filepath.Join("", "namespace", ".", "tag"): {}, filepath.Join(".", ".", ".", "."): {},