8000 gguf: store TensorInfo and KeyValue by value in lookup maps by janpfeifer · Pull Request #83 · gomlx/go-huggingface · GitHub
[go: up one dir, main page]

Skip to content
8000
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

- 2026/11/16:
- `models/gguf`:
- Stored `TensorInfo` and `KeyValue` by value in `File.tensorByName` (`map[string]TensorInfo`) and `File.kvByKey` (`map[string]KeyValue`) instead of pointers into `File.TensorInfos` / `File.KeyValues`, fixing `GetTensorInfo` returning mismatched entries after `slices.SortFunc(file.TensorInfos, ...)` reordered the slice (GHSA-372p-cjj6-v33v).
- Added `TestTensorInfoLookupWithUnsortedOffsets` regression test in `models/gguf/gguf_test.go`.

- 2026/11/15:
- `models/transformer`:
- Added native half-precision (`Float16` / `BFloat16`) support for unquantized half-precision GGUF models in `ggufDTypes()`, `loadStoreGGUF`, `loadSplitGGUFQKV`, and `CreateGoMLXModel`: preserves half-precision weights and split QKV tensors natively while configuring `tm.WithNormDType(normDType)` when GGUF normalization parameters are `Float32` (as in llama.cpp `F16`/`BF16` exports); quantized GGUFs continue to use `Float32` with `QuantGGML` weights.
Expand Down
20 changes: 10 additions & 10 deletions models/gguf/gguf.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ type File struct {
// TensorInfos holds parsed information about every tensor in the file.
TensorInfos []TensorInfo

kvByKey map[string]*KeyValue
tensorByName map[string]*TensorInfo
kvByKey map[string]KeyValue
tensorByName map[string]TensorInfo
path string
dataOffset int64
}
Expand Down Expand Up @@ -111,13 +111,13 @@ func Open(path string) (*File, error) {
}

// Build indexes (needed before alignment lookup).
file.kvByKey = make(map[string]*KeyValue, len(file.KeyValues))
for i := range file.KeyValues {
file.kvByKey[file.KeyValues[i].Key] = &file.KeyValues[i]
file.kvByKey = make(map[string]KeyValue, len(file.KeyValues))
for _, kv := range file.KeyValues {
file.kvByKey[kv.Key] = kv
}
file.tensorByName = make(map[string]*TensorInfo, len(file.TensorInfos))
for i := range file.TensorInfos {
file.tensorByName[file.TensorInfos[i].Name] = &file.TensorInfos[i]
file.tensorByName = make(map[string]TensorInfo, len(file.TensorInfos))
for _, ti := range file.TensorInfos {
file.tensorByName[ti.Name] = ti
}

// Sort tensors by offset for optimal sequential I/O.
Expand Down Expand Up @@ -155,7 +155,7 @@ func (f *File) GetKeyValue(key string) (KeyValue, bool) {
if !ok {
return KeyValue{}, false
}
return *kv, true
return kv, true
}

// GetTensorInfo looks up a tensor by name.
Expand All @@ -164,7 +164,7 @@ func (f *File) GetTensorInfo(name string) (TensorInfo, bool) {
if !ok {
return TensorInfo{}, false
}
return *ti, true
return ti, true
}

// Architecture returns the model architecture string (e.g., "llama", "gemma"),
Expand Down
33 changes: 33 additions & 0 deletions models/gguf/gguf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,39 @@ func TestTensorInfoParsing(t *testing.T) {
assert.Equal(t, uint64(5), info2.NumElements())
}

func TestTensorInfoLookupWithUnsortedOffsets(t *testing.T) {
// Write tensor infos out of offset order so Open's sort reorders f.TensorInfos.
// Verify GetTensorInfo still returns the matching entry for each name.
tensorData := make([]byte, 68)
path := buildMinimalGGUF(t, 1, 2,
func(b *ggufBuilder) {
b.writeKVString("general.architecture", "test")
},
func(b *ggufBuilder) {
b.writeTensorInfo("later", []uint64{5}, TensorTypeF32, 48)
b.writeTensorInfo("earlier", []uint64{3, 4}, TensorTypeF32, 0)
},
tensorData)

f, err := Open(path)
require.NoError(t, err)
require.Len(t, f.TensorInfos, 2)
assert.Equal(t, "earlier", f.TensorInfos[0].Name)
assert.Equal(t, "later", f.TensorInfos[1].Name)

earlier, ok := f.GetTensorInfo("earlier")
require.True(t, ok)
assert.Equal(t, "earlier", earlier.Name)
assert.Equal(t, uint64(0), earlier.Offset)
assert.Equal(t, []uint64{3, 4}, earlier.Shape)

later, ok := f.GetTensorInfo("later")
require.True(t, ok)
assert.Equal(t, "later", later.Name)
assert.Equal(t, uint64(48), later.Offset)
assert.Equal(t, []uint64{5}, later.Shape)
}

func TestListTensorNames(t *testing.T) {
path := buildMinimalGGUF(t, 1, 2,
func(b *ggufBuilder) {
Expand Down
Loading
0