package gguf import ( "bytes" "context" "encoding/binary" "errors" "net/http" "net/http/httptest" "strings" "testing" "time" ) // header builds a GGUF v3 header: an array before the architecture, the way // real files put tokenizer data, so skipping values is exercised. func header(arch string) []byte { var b bytes.Buffer w := func(v any) { _ = binary.Write(&b, binary.LittleEndian, v) } str := func(s string) { w(uint64(len(s))); b.WriteString(s) } b.WriteString("GGUF") w(uint32(3)) w(uint64(0)) // tensors w(uint64(4)) // kvs str("general.alignment") w(uint32(tUint32)) w(uint32(32)) str("some.list") w(uint32(tArray)) w(uint32(tString)) w(uint64(2)) str("a") str("bc") str("general.architecture") w(uint32(tString)) str(arch) str("general.name") w(uint32(tString)) str("A Model") return b.Bytes() } func TestTheArchitectureIsReadFromTheHeader(t *testing.T) { h, err := Read(bytes.NewReader(header("ggmlc"))) if err != nil { t.Fatal(err) } if h.Architecture != "ggmlc" || h.Name != "A Model" || h.Version != 3 { t.Errorf("header = %+v", h) } } func TestSomethingElseIsNotAGGUF(t *testing.T) { if _, err := Read(strings.NewReader("not found")); !errors.Is(err, ErrNotGGUF) { t.Errorf("an HTML page read as %v", err) } } func TestOnlyTheStartOfARemoteFileIsFetched(t *testing.T) { body := append(header("qwen2"), make([]byte, 1<<20)...) var rng string srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { rng = r.Header.Get("Range") http.ServeContent(w, r, "m.gguf", timeZero, bytes.NewReader(body)) })) defer srv.Close() h, err := FromURL(context.Background(), nil, srv.URL+"/m.gguf") if err != nil || h.Architecture != "qwen2" { t.Fatalf("%+v, %v", h, err) } if !strings.HasPrefix(rng, "bytes=0-") { t.Errorf("asked for %q rather than the start of the file", rng) } } func TestLlamaCppKnowsWhatItCanLoad(t *testing.T) { if v := LlamaCpp("b10991", "qwen2"); !v.Servable || !v.Checked { t.Errorf("qwen2: %+v", v) } // The file that started this: downloaded, catalogued, placed, and then // refused by every node with "unknown model architecture: 'ggmlc'". if v := LlamaCpp("b10991", "ggmlc"); v.Servable || !strings.Contains(v.Reason, "cannot load") { t.Errorf("ggmlc: %+v", v) } if v := LlamaCpp("b10991", "clip"); v.Servable { t.Errorf("a vision projector alone: %+v", v) } // A build llmgw has no list for is not refused, only not vouched for. if v := LlamaCpp("b99999", "ggmlc"); !v.Servable || v.Checked { t.Errorf("an unknown build: %+v", v) } } var timeZero = time.Time{}