model_test.go 4.41 KB
Newer Older
Michael Yang's avatar
Michael Yang committed
1
2
3
4
5
package model

import (
	"reflect"
	"slices"
6
	"strings"
Michael Yang's avatar
Michael Yang committed
7
8
9
	"testing"

	"github.com/google/go-cmp/cmp"
10
11
	"github.com/ollama/ollama/fs"
	fsggml "github.com/ollama/ollama/fs/ggml"
Michael Yang's avatar
Michael Yang committed
12
13
14
	"github.com/ollama/ollama/ml"
	"github.com/ollama/ollama/ml/backend/ggml"
	"github.com/ollama/ollama/ml/nn"
15
	"github.com/ollama/ollama/model/input"
Michael Yang's avatar
Michael Yang committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
)

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)
Jesse Gross's avatar
Jesse Gross committed
85
	v.Elem().Set(populateFields(Base{b: &fakeBackend{
Michael Yang's avatar
Michael Yang committed
86
87
88
89
90
91
92
93
94
95
96
		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",
		},
Jesse Gross's avatar
Jesse Gross committed
97
	}}, v.Elem()))
Michael Yang's avatar
Michael Yang committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120

	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) {
121
122
123
124
	type nested struct {
		Weight *nn.Linear `gguf:"a,alt:b"`
	}

Michael Yang's avatar
Michael Yang committed
125
126
127
	type fakeModel struct {
		Input  *nn.Embedding `gguf:"input"`
		Output *nn.Linear    `gguf:"output,alt:input"`
128
		Nested *nested       `gguf:"nested"`
Michael Yang's avatar
Michael Yang committed
129
130
	}

131
	var m fakeModel
Michael Yang's avatar
Michael Yang committed
132
	v := reflect.ValueOf(&m)
Jesse Gross's avatar
Jesse Gross committed
133
	v.Elem().Set(populateFields(Base{b: &fakeBackend{
Michael Yang's avatar
Michael Yang committed
134
135
		names: []string{
			"input.weight",
136
			"nested.b.weight",
Michael Yang's avatar
Michael Yang committed
137
		},
Jesse Gross's avatar
Jesse Gross committed
138
	}}, v.Elem()))
Michael Yang's avatar
Michael Yang committed
139
140
141
142

	if diff := cmp.Diff(fakeModel{
		Input:  &nn.Embedding{Weight: &fakeTensor{Name: "input.weight"}},
		Output: &nn.Linear{Weight: &fakeTensor{Name: "input.weight"}},
143
144
145
		Nested: &nested{
			Weight: &nn.Linear{Weight: &fakeTensor{Name: "nested.b.weight"}},
		},
Michael Yang's avatar
Michael Yang committed
146
147
148
149
	}, m); diff != "" {
		t.Errorf("populateFields() set incorrect values (-want +got):\n%s", diff)
	}
}
150
151

func TestGetTextProcessor(t *testing.T) {
152
	tp, err := getTextProcessor(fsggml.KV{})
153
154
155
156
157
158
159
160
	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")
	}

161
	models["dummy"] = func(fs.Config) (Model, error) {
162
163
		return notTextProcessorModel{}, nil
	}
164
	tp, err = getTextProcessor(fsggml.KV{"general.architecture": "dummy"})
165
166
167
168
169
170
171
172
173
174
175
	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{}

Jesse Gross's avatar
Jesse Gross committed
176
func (notTextProcessorModel) Forward(ml.Context, input.Batch) (ml.Tensor, error) {
177
178
179
180
181
182
183
184
185
186
	panic("unimplemented")
}

func (notTextProcessorModel) Backend() ml.Backend {
	panic("unimplemented")
}

func (notTextProcessorModel) Config() config {
	panic("unimplemented")
}