Get up and running with Kimi, GLM, MiniMax, DeepSeek, gpt-oss, Qwen, Gemma and other models.
logutil: add Trace and TraceContext helpers (#12110)
Michael Yangfb92b61754ed
diff --git a/harmony/harmonyparser.go b/harmony/harmonyparser.goindex e72496614..a51819dda 100644--- a/harmony/harmonyparser.go+++ b/harmony/harmonyparser.go@@ -1,7 +1,6 @@package harmonyimport (- "context""fmt""log/slog""strings"@@ -292,7 +291,7 @@ func (h *HarmonyMessageHandler) AddContent(content string, toolParser *HarmonyTofor _, event := range events {switch event := event.(type) {case HarmonyEventHeaderComplete:- slog.Log(context.TODO(), logutil.LevelTrace, "harmony event header complete", "header", event.Header)+ logutil.Trace("harmony event header complete", "header", event.Header)switch event.Header.Channel {case "analysis":if event.Header.Recipient != "" {@@ -315,7 +314,7 @@ func (h *HarmonyMessageHandler) AddContent(content string, toolParser *HarmonyToh.state = harmonyMessageState_Normal}case HarmonyEventContentEmitted:- slog.Log(context.TODO(), logutil.LevelTrace, "harmony event content", "content", event.Content, "state", h.state)+ logutil.Trace("harmony event content", "content", event.Content, "state", h.state)if h.state == harmonyMessageState_Normal {contentSb.WriteString(event.Content)} else if h.state == harmonyMessageState_Thinking {diff --git a/llm/server.go b/llm/server.goindex b8d3cdd3d..664a69fb3 100644--- a/llm/server.go+++ b/llm/server.go@@ -865,7 +865,7 @@ func (s *ollamaServer) createLayout(systemInfo discover.SystemInfo, systemGPUs d}layers[i] += memory.CPU.Weights[i].Sizelayers[i] += memory.CPU.Cache[i].Size- slog.Log(context.TODO(), logutil.LevelTrace, "layer to assign", "layer", i, "size", format.HumanBytes2(layers[i]))+ logutil.Trace("layer to assign", "layer", i, "size", format.HumanBytes2(layers[i]))}gpuLayers := ml.GPULayersList{}diff --git a/logutil/logutil.go b/logutil/logutil.goindex 406caf540..fff277b84 100644--- a/logutil/logutil.go+++ b/logutil/logutil.go@@ -1,6 +1,7 @@package logutilimport (+ "context""io""log/slog""path/filepath"@@ -27,3 +28,11 @@ func NewLogger(w io.Writer, level slog.Level) *slog.Logger {},}))}++func Trace(msg string, args ...any) {+ slog.Log(context.TODO(), LevelTrace, msg, args...)+}++func TraceContext(ctx context.Context, msg string, args ...any) {+ slog.Log(ctx, LevelTrace, msg, args...)+}diff --git a/ml/backend/ggml/ggml.go b/ml/backend/ggml/ggml.goindex 5fef97cdd..931386d56 100644--- a/ml/backend/ggml/ggml.go+++ b/ml/backend/ggml/ggml.go@@ -271,7 +271,7 @@ func New(modelPath string, params ml.BackendParams) (ml.Backend, error) {tt := C.ggml_new_tensor(ctxs[bt], kind, C.int(len(t.source.Shape)), (*C.int64_t)(unsafe.Pointer(&t.source.Shape[0])))C.ggml_set_name(tt, cname)- slog.Log(context.TODO(), logutil.LevelTrace, "created tensor", "name", name, "shape", t.source.Shape, "dtype", t.source.Kind, "buffer_type", C.GoString(C.ggml_backend_buft_name(bt)))+ logutil.Trace("created tensor", "name", name, "shape", t.source.Shape, "dtype", t.source.Kind, "buffer_type", C.GoString(C.ggml_backend_buft_name(bt)))size := pad(C.ggml_backend_buft_get_alloc_size(bt, tt), C.ggml_backend_buft_get_alignment(bt))if layer == -1 {@@ -378,7 +378,7 @@ func New(modelPath string, params ml.BackendParams) (ml.Backend, error) {}for bs := range maps.Values(bbs) {- slog.Log(context.TODO(), logutil.LevelTrace, "model weights", "buffer", C.GoString(C.ggml_backend_buffer_name(bs)),+ logutil.Trace("model weights", "buffer", C.GoString(C.ggml_backend_buffer_name(bs)),"size", format.HumanBytes2(uint64(C.ggml_backend_buffer_get_size(bs))))}@@ -811,7 +811,7 @@ func (c *Context) Reserve() {}}- slog.Log(context.TODO(), logutil.LevelTrace, "compute graph", "backend", C.GoString(C.ggml_backend_name(c.b.schedBackends[i])),+ logutil.Trace("compute graph", "backend", C.GoString(C.ggml_backend_name(c.b.schedBackends[i])),"buffer_type", C.GoString(C.ggml_backend_buft_name(c.b.schedBufts[i])), "size", format.HumanBytes2(uint64(bufferStatus.size)))}diff --git a/model/bytepairencoding.go b/model/bytepairencoding.goindex e4083dfce..b2cb11325 100644--- a/model/bytepairencoding.go+++ b/model/bytepairencoding.go@@ -2,7 +2,6 @@ package modelimport ("cmp"- "context""fmt""iter""log/slog"@@ -202,7 +201,7 @@ func (bpe BytePairEncoding) Encode(s string, addSpecial bool) ([]int32, error) {}}- slog.Log(context.TODO(), logutil.LevelTrace, "encoded", "string", s, "ids", ids)+ logutil.Trace("encoded", "string", s, "ids", ids)if addSpecial && len(ids) > 0 {ids = bpe.vocab.addSpecials(ids)@@ -243,6 +242,6 @@ func (bpe BytePairEncoding) Decode(ids []int32) (string, error) {}}- slog.Log(context.TODO(), logutil.LevelTrace, "decoded", "string", sb.String(), "from", lazyIdsString{ids: ids})+ logutil.Trace("decoded", "string", sb.String(), "from", lazyIdsString{ids: ids})return sb.String(), nil}diff --git a/model/model.go b/model/model.goindex 190dedc76..30754143a 100644--- a/model/model.go+++ b/model/model.go@@ -1,12 +1,10 @@package modelimport (- "context""errors""fmt"_ "image/jpeg"_ "image/png"- "log/slog""os""reflect""strconv"@@ -198,7 +196,7 @@ func populateFields(base Base, v reflect.Value, tags ...Tag) reflect.Value {names := fn(tagsCopy)for _, name := range names {if tensor := base.Backend().Get(strings.Join(name, ".")); tensor != nil {- slog.Log(context.TODO(), logutil.LevelTrace, "found tensor", "", tensor)+ logutil.Trace("found tensor", "", tensor)vv.Set(reflect.ValueOf(tensor))break}diff --git a/model/sentencepiece.go b/model/sentencepiece.goindex 7d725f04f..4300f45f5 100644--- a/model/sentencepiece.go+++ b/model/sentencepiece.go@@ -2,7 +2,6 @@ package modelimport ("container/heap"- "context""fmt""log/slog""strconv"@@ -25,7 +24,7 @@ func (spm SentencePieceModel) Vocabulary() *Vocabulary {}func NewSentencePieceModel(vocab *Vocabulary) SentencePieceModel {- slog.Log(context.TODO(), logutil.LevelTrace, "Tokens", "num tokens", len(vocab.Values), "vals", vocab.Values[:5], "scores", vocab.Scores[:5], "types", vocab.Types[:5])+ logutil.Trace("Tokens", "num tokens", len(vocab.Values), "vals", vocab.Values[:5], "scores", vocab.Scores[:5], "types", vocab.Types[:5])counter := map[int]int{}var maxTokenLen int@@ -39,7 +38,7 @@ func NewSentencePieceModel(vocab *Vocabulary) SentencePieceModel {}}- slog.Log(context.TODO(), logutil.LevelTrace, "Token counts", "normal", counter[TOKEN_TYPE_NORMAL], "unknown", counter[TOKEN_TYPE_UNKNOWN], "control", counter[TOKEN_TYPE_CONTROL],+ logutil.Trace("Token counts", "normal", counter[TOKEN_TYPE_NORMAL], "unknown", counter[TOKEN_TYPE_UNKNOWN], "control", counter[TOKEN_TYPE_CONTROL],"user defined", counter[TOKEN_TYPE_USER_DEFINED], "unused", counter[TOKEN_TYPE_UNUSED], "byte", counter[TOKEN_TYPE_BYTE],"max token len", maxTokenLen)@@ -182,7 +181,7 @@ func (spm SentencePieceModel) Encode(s string, addSpecial bool) ([]int32, error)}}- slog.Log(context.TODO(), logutil.LevelTrace, "encoded", "string", s, "ids", ids)+ logutil.Trace("encoded", "string", s, "ids", ids)if addSpecial && len(ids) > 0 {ids = spm.vocab.addSpecials(ids)@@ -246,6 +245,6 @@ func (spm SentencePieceModel) Decode(ids []int32) (string, error) {}}- slog.Log(context.TODO(), logutil.LevelTrace, "decoded", "ids", ids, "string", sb.String())+ logutil.Trace("decoded", "ids", ids, "string", sb.String())return sb.String(), nil}