2025-02-14 00:31:21 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"slices"
|
2025-03-04 09:03:46 -08:00
|
|
|
"strings"
|
2025-02-14 00:31:21 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
2025-03-04 09:03:46 -08:00
|
|
|
fs "github.com/ollama/ollama/fs/ggml"
|
2025-02-14 00:31:21 +00:00
|
|
|
"github.com/ollama/ollama/ml"
|
|
|
|
"github.com/ollama/ollama/ml/backend/ggml"
|
|
|
|
"github.com/ollama/ollama/ml/nn"
|
2025-03-08 15:45:31 -08:00
|
|
|
"github.com/ollama/ollama/model/input"
|
2025-02-14 00:31:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseTags(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
value string
|
|
|
|
want Tag
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
value: "output",
|
|
|
|
want: Tag{
|
|
|
|
Name: "output",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "output,alt:token_embd",
|
|
|
|
want: Tag{
|
|
|
|
Name: "output",
|
|
|
|
Alternate: []string{
|
|
|
|
"token_embd",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range cases {
|
|
|
|
t.Run(tt.value, func(t *testing.T) {
|
|
|
|
got := ParseTags(tt.value)
|
|
|
|
if diff := cmp.Diff(tt.want, got); diff != "" {
|
|
|
|
t.Errorf("ParseTags() returned unexpected values (-want +got):\n%s", diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeBackend struct {
|
|
|
|
*ggml.Backend
|
|
|
|
names []string
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeTensor struct {
|
|
|
|
*ggml.Tensor
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *fakeBackend) Get(name string) ml.Tensor {
|
|
|
|
if slices.Contains(m.names, name) {
|
|
|
|
return &fakeTensor{Name: name}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPopulateFields(t *testing.T) {
|
|
|
|
type fakeLayer struct {
|
|
|
|
Query *nn.Linear `gguf:"attn_q"`
|
|
|
|
Key *nn.Linear `gguf:"attn_k"`
|
|
|
|
Value *nn.Linear `gguf:"attn_v"`
|
|
|
|
Output *nn.Linear `gguf:"attn_o"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeModel struct {
|
|
|
|
Input *nn.Embedding `gguf:"input"`
|
|
|
|
OutputNorm *nn.RMSNorm `gguf:"output_norm"`
|
|
|
|
Output *nn.Linear `gguf:"output"`
|
|
|
|
Layers [2]fakeLayer `gguf:"blk"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var m fakeModel
|
|
|
|
v := reflect.ValueOf(&m)
|
2024-12-17 19:59:41 -08:00
|
|
|
v.Elem().Set(populateFields(Base{b: &fakeBackend{
|
2025-02-14 00:31:21 +00:00
|
|
|
names: []string{
|
|
|
|
"input.weight",
|
|
|
|
"blk.0.attn_q.weight",
|
|
|
|
"blk.0.attn_k.weight",
|
|
|
|
"blk.0.attn_v.weight",
|
|
|
|
"blk.1.attn_q.weight",
|
|
|
|
"blk.1.attn_k.weight",
|
|
|
|
"blk.1.attn_v.weight",
|
|
|
|
"output_norm.weight",
|
|
|
|
"output.weight",
|
|
|
|
},
|
2024-12-17 19:59:41 -08:00
|
|
|
}}, v.Elem()))
|
2025-02-14 00:31:21 +00:00
|
|
|
|
|
|
|
if diff := cmp.Diff(fakeModel{
|
|
|
|
Input: &nn.Embedding{Weight: &fakeTensor{Name: "input.weight"}},
|
|
|
|
OutputNorm: &nn.RMSNorm{Weight: &fakeTensor{Name: "output_norm.weight"}},
|
|
|
|
Output: &nn.Linear{Weight: &fakeTensor{Name: "output.weight"}},
|
|
|
|
Layers: [2]fakeLayer{
|
|
|
|
{
|
|
|
|
Query: &nn.Linear{Weight: &fakeTensor{Name: "blk.0.attn_q.weight"}},
|
|
|
|
Key: &nn.Linear{Weight: &fakeTensor{Name: "blk.0.attn_k.weight"}},
|
|
|
|
Value: &nn.Linear{Weight: &fakeTensor{Name: "blk.0.attn_v.weight"}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Query: &nn.Linear{Weight: &fakeTensor{Name: "blk.1.attn_q.weight"}},
|
|
|
|
Key: &nn.Linear{Weight: &fakeTensor{Name: "blk.1.attn_k.weight"}},
|
|
|
|
Value: &nn.Linear{Weight: &fakeTensor{Name: "blk.1.attn_v.weight"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, m); diff != "" {
|
|
|
|
t.Errorf("populateFields() set incorrect values (-want +got):\n%s", diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPopulateFieldsAlternateName(t *testing.T) {
|
|
|
|
type fakeModel struct {
|
|
|
|
Input *nn.Embedding `gguf:"input"`
|
|
|
|
Output *nn.Linear `gguf:"output,alt:input"`
|
|
|
|
}
|
|
|
|
|
|
|
|
m := fakeModel{}
|
|
|
|
v := reflect.ValueOf(&m)
|
2024-12-17 19:59:41 -08:00
|
|
|
v.Elem().Set(populateFields(Base{b: &fakeBackend{
|
2025-02-14 00:31:21 +00:00
|
|
|
names: []string{
|
|
|
|
"input.weight",
|
|
|
|
},
|
2024-12-17 19:59:41 -08:00
|
|
|
}}, v.Elem()))
|
2025-02-14 00:31:21 +00:00
|
|
|
|
|
|
|
if diff := cmp.Diff(fakeModel{
|
|
|
|
Input: &nn.Embedding{Weight: &fakeTensor{Name: "input.weight"}},
|
|
|
|
Output: &nn.Linear{Weight: &fakeTensor{Name: "input.weight"}},
|
|
|
|
}, m); diff != "" {
|
|
|
|
t.Errorf("populateFields() set incorrect values (-want +got):\n%s", diff)
|
|
|
|
}
|
|
|
|
}
|
2025-03-04 09:03:46 -08:00
|
|
|
|
|
|
|
func TestGetTextProcessor(t *testing.T) {
|
|
|
|
tp, err := getTextProcessor(fs.KV{})
|
|
|
|
if err == nil {
|
|
|
|
t.Error("expected error")
|
|
|
|
} else if !strings.Contains(err.Error(), "unsupported model architecture") {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
} else if tp != nil {
|
|
|
|
t.Error("expected nil tp")
|
|
|
|
}
|
|
|
|
|
|
|
|
models["dummy"] = func(ml.Config) (Model, error) {
|
|
|
|
return notTextProcessorModel{}, nil
|
|
|
|
}
|
|
|
|
tp, err = getTextProcessor(fs.KV{"general.architecture": "dummy"})
|
|
|
|
if err == nil {
|
|
|
|
t.Error("expected error")
|
|
|
|
} else if !strings.Contains(err.Error(), "not a TextProcessor") {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
} else if tp != nil {
|
|
|
|
t.Error("expected nil tp")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type notTextProcessorModel struct{}
|
|
|
|
|
2025-03-08 15:45:31 -08:00
|
|
|
func (notTextProcessorModel) Forward(ml.Context, input.Options) (ml.Tensor, error) {
|
2025-03-04 09:03:46 -08:00
|
|
|
panic("unimplemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (notTextProcessorModel) Backend() ml.Backend {
|
|
|
|
panic("unimplemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (notTextProcessorModel) Config() config {
|
|
|
|
panic("unimplemented")
|
|
|
|
}
|