From 7bdcd1da9417ecc1a1069f2d73f0b88a3a43857d Mon Sep 17 00:00:00 2001 From: Michael Yang Date: Tue, 11 Jun 2024 15:55:44 -0700 Subject: [PATCH] Revert "Merge pull request #4938 from ollama/mxyng/fix-byte-order" This reverts commit f5f245cc154580fa7b4052c001d2a7e3d771cfb8, reversing changes made to 94d37fdcae30ddeb6c9f65c8707004f5ec9eaf33. this change broke gguf v2 which is incorrectly detected as big endian --- llm/ggml.go | 13 ++++++++----- llm/gguf.go | 15 +-------------- 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/llm/ggml.go b/llm/ggml.go index 16da4c9d..645447d5 100644 --- a/llm/ggml.go +++ b/llm/ggml.go @@ -231,7 +231,8 @@ const ( // Magic constant for `ggla` files (LoRA adapter). FILE_MAGIC_GGLA = 0x67676C61 // Magic constant for `gguf` files (versioned, gguf) - FILE_MAGIC_GGUF = 0x46554747 + FILE_MAGIC_GGUF_LE = 0x46554747 + FILE_MAGIC_GGUF_BE = 0x47475546 ) var ErrUnsupportedFormat = errors.New("unsupported model format") @@ -246,7 +247,7 @@ func DetectGGMLType(b []byte) string { return "ggjt" case FILE_MAGIC_GGLA: return "ggla" - case FILE_MAGIC_GGUF: + case FILE_MAGIC_GGUF_LE, FILE_MAGIC_GGUF_BE: return "gguf" default: return "" @@ -254,19 +255,21 @@ func DetectGGMLType(b []byte) string { } func DecodeGGML(rs io.ReadSeeker) (*GGML, int64, error) { - var magic [4]byte + var magic uint32 if err := binary.Read(rs, binary.LittleEndian, &magic); err != nil { return nil, 0, err } var c container - switch binary.LittleEndian.Uint32(magic[:]) { + switch magic { case FILE_MAGIC_GGML, FILE_MAGIC_GGMF, FILE_MAGIC_GGJT: return nil, 0, ErrUnsupportedFormat case FILE_MAGIC_GGLA: c = &containerGGLA{} - case FILE_MAGIC_GGUF: + case FILE_MAGIC_GGUF_LE: c = &containerGGUF{ByteOrder: binary.LittleEndian} + case FILE_MAGIC_GGUF_BE: + c = &containerGGUF{ByteOrder: binary.BigEndian} default: return nil, 0, errors.New("invalid file magic") } diff --git a/llm/gguf.go b/llm/gguf.go index 8c64e166..234efe57 100644 --- a/llm/gguf.go +++ b/llm/gguf.go @@ -36,23 +36,10 @@ func (c *containerGGUF) Name() string { } func (c *containerGGUF) Decode(rs io.ReadSeeker) (model, error) { - var version [4]byte - if err := binary.Read(rs, c.ByteOrder, &version); err != nil { + if err := binary.Read(rs, c.ByteOrder, &c.Version); err != nil { return nil, err } - // if the lower 16 bits are 0, the byte order is probably wrong - if c.ByteOrder.Uint32(version[:])&1<<4 == 0 { - switch c.ByteOrder { - case binary.LittleEndian: - c.ByteOrder = binary.BigEndian - case binary.BigEndian: - c.ByteOrder = binary.LittleEndian - } - } - - c.Version = c.ByteOrder.Uint32(version[:]) - var err error switch c.Version { case 1: