This commit is contained in:
Michael Yang 2025-11-18 14:07:58 -08:00
parent f01c83ed6d
commit 4d24d8a77d
22 changed files with 58 additions and 91 deletions

View File

@ -36,6 +36,12 @@ linters:
errcheck: errcheck:
exclude-functions: exclude-functions:
- fmt.Fprintf - fmt.Fprintf
gocritic:
disabled-checks:
# Detects suspicious duplicated sub-expressions.
# Prone to false positives when used on cgo code
# https://github.com/go-critic/go-critic/issues/897#issuecomment-568892104
- dupSubExpr
perfsprint: perfsprint:
strconcat: false strconcat: false
concat-loop: false concat-loop: false

View File

@ -22,6 +22,7 @@ import (
var ErrCancelled = errors.New("Cancelled") var ErrCancelled = errors.New("Cancelled")
// Cancelled refers to ErrCancelled. // Cancelled refers to ErrCancelled.
//
// Deprecated: Use ErrCancelled instead. // Deprecated: Use ErrCancelled instead.
var Cancelled = ErrCancelled var Cancelled = ErrCancelled

View File

@ -345,11 +345,9 @@ func GetInferenceComputer(ctx context.Context) ([]InferenceCompute, error) {
slog.Info("Matched", "inference compute", ic) slog.Info("Matched", "inference compute", ic)
inference = append(inference, ic) inference = append(inference, ic)
} else { } else if len(inference) > 0 {
// Break out on first non matching line after we start matching // Break out on first non matching line after we start matching
if len(inference) > 0 { return inference, nil
return inference, nil
}
} }
} }
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)

View File

@ -384,15 +384,9 @@ func wrapLines(text string, width int) []string {
wrapped = append(wrapped, "") wrapped = append(wrapped, "")
} else if len(line) <= width { } else if len(line) <= width {
wrapped = append(wrapped, line) wrapped = append(wrapped, line)
} else if words := strings.Fields(line); len(words) == 0 {
wrapped = append(wrapped, line)
} else { } else {
// Word wrapping while preserving whitespace structure
words := strings.Fields(line)
if len(words) == 0 {
// Line with only whitespace
wrapped = append(wrapped, line)
continue
}
currentLine := "" currentLine := ""
for _, word := range words { for _, word := range words {
// Check if adding this word would exceed width // Check if adding this word would exceed width
@ -537,15 +531,13 @@ func (b *BrowserOpen) Execute(ctx context.Context, args map[string]any) (any, st
if err != nil { if err != nil {
return nil, "", fmt.Errorf("page not found for cursor %d: %w", cursor, err) return nil, "", fmt.Errorf("page not found for cursor %d: %w", cursor, err)
} }
} else { } else if len(b.state.Data.PageStack) != 0 {
// get last page // get last page
if len(b.state.Data.PageStack) != 0 { pageURL := b.state.Data.PageStack[len(b.state.Data.PageStack)-1]
pageURL := b.state.Data.PageStack[len(b.state.Data.PageStack)-1] var err error
var err error page, err = b.getPageFromStack(pageURL)
page, err = b.getPageFromStack(pageURL) if err != nil {
if err != nil { return nil, "", fmt.Errorf("page not found for cursor %d: %w", cursor, err)
return nil, "", fmt.Errorf("page not found for cursor %d: %w", cursor, err)
}
} }
} }

View File

@ -924,10 +924,8 @@ func TestPushHandler(t *testing.T) {
t.Errorf("expected output %q, got %q", tt.expectedOutput, got) t.Errorf("expected output %q, got %q", tt.expectedOutput, got)
} }
} }
} else { } else if err == nil || !strings.Contains(err.Error(), tt.expectedError) {
if err == nil || !strings.Contains(err.Error(), tt.expectedError) { t.Errorf("expected error containing %q, got %v", tt.expectedError, err)
t.Errorf("expected error containing %q, got %v", tt.expectedError, err)
}
} }
}) })
} }
@ -1014,10 +1012,8 @@ func TestListHandler(t *testing.T) {
if got := string(output); got != tt.expectedOutput { if got := string(output); got != tt.expectedOutput {
t.Errorf("expected output:\n%s\ngot:\n%s", tt.expectedOutput, got) t.Errorf("expected output:\n%s\ngot:\n%s", tt.expectedOutput, got)
} }
} else { } else if err == nil || !strings.Contains(err.Error(), tt.expectedError) {
if err == nil || !strings.Contains(err.Error(), tt.expectedError) { t.Errorf("expected error containing %q, got %v", tt.expectedError, err)
t.Errorf("expected error containing %q, got %v", tt.expectedError, err)
}
} }
}) })
} }

View File

@ -111,7 +111,7 @@ func (m *gptossModel) Tensors(ts []Tensor) []*ggml.Tensor {
for name, mxfp4 := range mxfp4s { for name, mxfp4 := range mxfp4s {
dims := mxfp4.blocks.Shape() dims := mxfp4.blocks.Shape()
if !strings.HasSuffix(name, ".weight") { if !strings.HasSuffix(name, ".weight") {
name = name + ".weight" name += ".weight"
} }
if strings.Contains(name, "ffn_down_exps") { if strings.Contains(name, "ffn_down_exps") {
out = append(out, &ggml.Tensor{ out = append(out, &ggml.Tensor{

View File

@ -226,7 +226,7 @@ func (llm *gguf) Decode(rs io.ReadSeeker) error {
Name: name, Name: name,
Kind: kind, Kind: kind,
Offset: offset, Offset: offset,
Shape: shape[:], Shape: shape,
} }
llm.tensors = append(llm.tensors, &tensor) llm.tensors = append(llm.tensors, &tensor)

View File

@ -200,9 +200,7 @@ func (s *HarmonyParser) parseHeader(raw string) HarmonyHeader {
before := raw[:channelIndex] before := raw[:channelIndex]
after := raw[channelIndex+len("<|channel|>"):] after := raw[channelIndex+len("<|channel|>"):]
// the channel name is `after` all the way up to the first (if any) whitespace character // the channel name is `after` all the way up to the first (if any) whitespace character
idx := strings.IndexFunc(after, func(r rune) bool { idx := strings.IndexFunc(after, unicode.IsSpace)
return unicode.IsSpace(r)
})
if idx == -1 { if idx == -1 {
idx = len(after) idx = len(after)
} }

View File

@ -982,13 +982,11 @@ nextLayer:
slog.Warn("model request too large for system", "requested", format.HumanBytes2(cpuSize), "available", format.HumanBytes2(available), "total", format.HumanBytes2(systemInfo.TotalMemory), "free", format.HumanBytes2(systemInfo.FreeMemory), "swap", format.HumanBytes2(systemInfo.FreeSwap)) slog.Warn("model request too large for system", "requested", format.HumanBytes2(cpuSize), "available", format.HumanBytes2(available), "total", format.HumanBytes2(systemInfo.TotalMemory), "free", format.HumanBytes2(systemInfo.FreeMemory), "swap", format.HumanBytes2(systemInfo.FreeSwap))
return fmt.Errorf("model requires more system memory (%s) than is available (%s)", format.HumanBytes2(cpuSize), format.HumanBytes2(available)) return fmt.Errorf("model requires more system memory (%s) than is available (%s)", format.HumanBytes2(cpuSize), format.HumanBytes2(available))
} }
} else { } else if vramSize > systemInfo.TotalMemory {
if vramSize > systemInfo.TotalMemory { // disable partial offloading when model is greater than total system memory as this
// disable partial offloading when model is greater than total system memory as this // can lead to locking up the system
// can lead to locking up the system s.options.NumGPU = 0
s.options.NumGPU = 0 gpuLayers = ml.GPULayersList{}
gpuLayers = ml.GPULayersList{}
}
} }
if gpuLayers.Sum() == 0 { if gpuLayers.Sum() == 0 {

View File

@ -210,10 +210,8 @@ func TestEmbeddingsMiddleware_InvalidEncodingFormat(t *testing.T) {
if !strings.Contains(errResp.Error.Message, "encoding_format") { if !strings.Contains(errResp.Error.Message, "encoding_format") {
t.Errorf("expected error message to mention encoding_format, got %q", errResp.Error.Message) t.Errorf("expected error message to mention encoding_format, got %q", errResp.Error.Message)
} }
} else { } else if resp.Code != http.StatusOK {
if resp.Code != http.StatusOK { t.Errorf("expected status 200, got %d: %s", resp.Code, resp.Body.String())
t.Errorf("expected status 200, got %d: %s", resp.Code, resp.Body.String())
}
} }
}) })
} }

View File

@ -543,12 +543,12 @@ func (d DeviceInfo) updateVisibleDevicesEnv(env map[string]string) {
} }
v, existing := env[envVar] v, existing := env[envVar]
if existing { if existing {
v = v + "," v += ","
} }
if d.FilterID != "" { if d.FilterID != "" {
v = v + d.FilterID v += d.FilterID
} else { } else {
v = v + d.ID v += d.ID
} }
env[envVar] = v env[envVar] = v
} }

View File

@ -143,9 +143,9 @@ func (bpe BytePairEncoding) Encode(s string, addSpecial bool) ([]int32, error) {
case r == 0x00ad: case r == 0x00ad:
r = 0x0143 r = 0x0143
case r <= 0x0020: case r <= 0x0020:
r = r + 0x0100 r += 0x0100
case r >= 0x007f && r <= 0x00a0: case r >= 0x007f && r <= 0x00a0:
r = r + 0x00a2 r += 0x00a2
} }
sb.WriteRune(r) sb.WriteRune(r)
@ -264,9 +264,9 @@ func (bpe BytePairEncoding) Decode(ids []int32) (string, error) {
case r == 0x0143: case r == 0x0143:
r = 0x00ad r = 0x00ad
case r > 0x0100 && r <= 0x0120: case r > 0x0100 && r <= 0x0120:
r = r - 0x0100 r -= 0x0100
case r > 0x0120 && r <= 0x0142: case r > 0x0120 && r <= 0x0142:
r = r - 0x00a2 r -= 0x00a2
} }
// NOTE: not using WriteRune here because it writes the UTF-8 // NOTE: not using WriteRune here because it writes the UTF-8

View File

@ -146,7 +146,7 @@ func NewTextProcessor(s string) (TextProcessor, error) {
func modelForArch(c fs.Config) (Model, error) { func modelForArch(c fs.Config) (Model, error) {
arch := c.Architecture() arch := c.Architecture()
if pooling.Type(c.Uint("pooling_type")) != pooling.TypeNone { if pooling.Type(c.Uint("pooling_type")) != pooling.TypeNone {
arch = arch + "_embed" arch += "_embed"
} }
f, ok := models[arch] f, ok := models[arch]

View File

@ -326,17 +326,11 @@ MESSAGE system`,
return 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) { if errors.Is(err, tt.err) {
return return
} else if pErr := (*ParserError)(nil); errors.As(err, &pErr) {
// got the correct type of error
return
} }
t.Fatalf("unexpected error: expected: %v, actual: %v", tt.err, err) t.Fatalf("unexpected error: expected: %v, actual: %v", tt.err, err)

View File

@ -61,7 +61,7 @@ func (c *ImageContext) MultimodalTokenize(llamaContext *llama.Context, data []by
return nil, nil return nil, nil
} }
if len(data) <= 0 { if len(data) == 0 {
return nil, errors.New("received zero length image") return nil, errors.New("received zero length image")
} }

View File

@ -995,7 +995,6 @@ func Execute(args []string) error {
log.Println("Server listening on", addr) log.Println("Server listening on", addr)
if err := httpServer.Serve(listener); err != nil { if err := httpServer.Serve(listener); err != nil {
log.Fatal("server error:", err)
return err return err
} }

View File

@ -1429,7 +1429,6 @@ func Execute(args []string) error {
log.Println("Server listening on", addr) log.Println("Server listening on", addr)
if err := httpServer.Serve(listener); err != nil { if err := httpServer.Serve(listener); err != nil {
log.Fatal("server error:", err)
return err return err
} }

View File

@ -30,7 +30,7 @@ func temperature(ts []token, temp float32) {
// Ensure temperature clipping near 0 to avoid numerical instability // Ensure temperature clipping near 0 to avoid numerical instability
temp = max(temp, 1e-7) temp = max(temp, 1e-7)
for i := range ts { for i := range ts {
ts[i].value = ts[i].value / temp ts[i].value /= temp
} }
} }

View File

@ -362,11 +362,9 @@ func (s *Server) GenerateHandler(c *gin.Context) {
if req.Think == nil { if req.Think == nil {
req.Think = &api.ThinkValue{Value: true} req.Think = &api.ThinkValue{Value: true}
} }
} else { } else if req.Think != nil && req.Think.Bool() {
if req.Think != nil && req.Think.Bool() { c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("%q does not support thinking", req.Model)})
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("%q does not support thinking", req.Model)}) return
return
}
} }
r, m, opts, err := s.scheduleRunner(c.Request.Context(), name.String(), caps, req.Options, req.KeepAlive) r, m, opts, err := s.scheduleRunner(c.Request.Context(), name.String(), caps, req.Options, req.KeepAlive)
@ -1996,11 +1994,9 @@ func (s *Server) ChatHandler(c *gin.Context) {
if req.Think == nil { if req.Think == nil {
req.Think = &api.ThinkValue{Value: true} req.Think = &api.ThinkValue{Value: true}
} }
} else { } else if req.Think != nil && req.Think.Bool() {
if req.Think != nil && req.Think.Bool() { c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("%q does not support thinking", req.Model)})
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("%q does not support thinking", req.Model)}) return
return
}
} }
r, m, opts, err := s.scheduleRunner(c.Request.Context(), name.String(), caps, req.Options, req.KeepAlive) r, m, opts, err := s.scheduleRunner(c.Request.Context(), name.String(), caps, req.Options, req.KeepAlive)

View File

@ -196,11 +196,9 @@ func TestGenerateDebugRenderOnly(t *testing.T) {
if tt.expectNumImages > 0 && response.DebugInfo.ImageCount != tt.expectNumImages { if tt.expectNumImages > 0 && response.DebugInfo.ImageCount != tt.expectNumImages {
t.Errorf("expected image count %d, got %d", tt.expectNumImages, response.DebugInfo.ImageCount) t.Errorf("expected image count %d, got %d", tt.expectNumImages, response.DebugInfo.ImageCount)
} }
} else { } else if w.Code != http.StatusOK {
// When debug is disabled, it should attempt normal processing // When debug is disabled, it should attempt normal processing
if w.Code != http.StatusOK { t.Errorf("expected status %d, got %d", http.StatusOK, w.Code)
t.Errorf("expected status %d, got %d", http.StatusOK, w.Code)
}
} }
}) })
} }
@ -401,11 +399,9 @@ func TestChatDebugRenderOnly(t *testing.T) {
if tt.expectNumImages > 0 && response.DebugInfo.ImageCount != tt.expectNumImages { if tt.expectNumImages > 0 && response.DebugInfo.ImageCount != tt.expectNumImages {
t.Errorf("expected image count %d, got %d", tt.expectNumImages, response.DebugInfo.ImageCount) t.Errorf("expected image count %d, got %d", tt.expectNumImages, response.DebugInfo.ImageCount)
} }
} else { } else if w.Code != http.StatusOK {
// When debug is disabled, it should attempt normal processing // When debug is disabled, it should attempt normal processing
if w.Code != http.StatusOK { t.Errorf("expected status %d, got %d", http.StatusOK, w.Code)
t.Errorf("expected status %d, got %d", http.StatusOK, w.Code)
}
} }
}) })
} }

View File

@ -429,10 +429,8 @@ func (s *Scheduler) load(req *LlmRequest, f *ggml.GGML, systemInfo ml.SystemInfo
} }
s.activeLoading = llama s.activeLoading = llama
} else { } else if s.activeLoading.ModelPath() != req.model.ModelPath {
if s.activeLoading.ModelPath() != req.model.ModelPath { panic(fmt.Errorf("attempting to load different model after eviction (original %v new %v)", s.activeLoading.ModelPath(), req.model.ModelPath))
panic(fmt.Errorf("attempting to load different model after eviction (original %v new %v)", s.activeLoading.ModelPath(), req.model.ModelPath))
}
} }
s.loadedMu.Unlock() s.loadedMu.Unlock()

View File

@ -68,8 +68,7 @@ func InferTags(t *template.Template) (string, string) {
enterFn := func(n parse.Node) bool { enterFn := func(n parse.Node) bool {
ancestors = append(ancestors, n) ancestors = append(ancestors, n)
switch x := n.(type) { if x, ok := n.(*parse.FieldNode); ok {
case *parse.FieldNode:
if len(x.Ident) > 0 && x.Ident[0] == "Thinking" { if len(x.Ident) > 0 && x.Ident[0] == "Thinking" {
var mostRecentRange *parse.RangeNode var mostRecentRange *parse.RangeNode
for i := len(ancestors) - 1; i >= 0; i-- { for i := len(ancestors) - 1; i >= 0; i-- {
@ -121,8 +120,7 @@ func InferTags(t *template.Template) (string, string) {
func rangeUsesField(rangeNode *parse.RangeNode, field string) bool { func rangeUsesField(rangeNode *parse.RangeNode, field string) bool {
found := false found := false
enterFn := func(n parse.Node) bool { enterFn := func(n parse.Node) bool {
switch x := n.(type) { if x, ok := n.(*parse.FieldNode); ok {
case *parse.FieldNode:
if x.Ident[0] == field { if x.Ident[0] == field {
found = true found = true
} }