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

import (
	"bytes"
	"image"
Michael Yang's avatar
Michael Yang committed
6
	"slices"
Michael Yang's avatar
llama4  
Michael Yang committed
7
8
9
10
11
12
13
14
15
16
17
18

	"github.com/ollama/ollama/fs"
	"github.com/ollama/ollama/kvcache"
	"github.com/ollama/ollama/ml"
	"github.com/ollama/ollama/ml/nn"
	"github.com/ollama/ollama/model"
	"github.com/ollama/ollama/model/input"
)

type Model struct {
	model.Base
	model.BytePairEncoding
Michael Yang's avatar
Michael Yang committed
19
	ImageProcessor
Michael Yang's avatar
llama4  
Michael Yang committed
20

21
	*VisionModel `gguf:"v"`
Michael Yang's avatar
llama4  
Michael Yang committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
	*Projector   `gguf:"mm"`
	*TextModel
}

type Projector struct {
	Linear1 *nn.Linear `gguf:"linear_1"`
}

func (p *Projector) Forward(ctx ml.Context, visionOutputs ml.Tensor) ml.Tensor {
	return p.Linear1.Forward(ctx, visionOutputs)
}

func New(c fs.Config) (model.Model, error) {
	m := Model{
		BytePairEncoding: model.NewBytePairEncoding(
			&model.Vocabulary{
				Values: c.Strings("tokenizer.ggml.tokens"),
Michael Yang's avatar
Michael Yang committed
39
				Types:  c.Ints("tokenizer.ggml.token_type"),
Michael Yang's avatar
llama4  
Michael Yang committed
40
41
				Merges: c.Strings("tokenizer.ggml.merges"),
				AddBOS: c.Bool("tokenizer.ggml.add_bos_token", true),
42
				BOS:    []int32{int32(c.Uint("tokenizer.ggml.bos_token_id"))},
Michael Yang's avatar
llama4  
Michael Yang committed
43
				AddEOS: c.Bool("tokenizer.ggml.add_eos_token", false),
44
45
46
47
				EOS: append(
					[]int32{int32(c.Uint("tokenizer.ggml.eos_token_id"))},
					c.Ints("tokenizer.ggml.eos_token_ids")...,
				),
Michael Yang's avatar
llama4  
Michael Yang committed
48
			},
49
			`[^\r\n\p{L}\p{N}]?[\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{M}]*[\p{Ll}\p{Lm}\p{Lo}\p{M}]+(?i:'s|'t|'re|'ve|'m|'ll|'d)?|[^\r\n\p{L}\p{N}]?[\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{M}]+[\p{Ll}\p{Lm}\p{Lo}\p{M}]*(?i:'s|'t|'re|'ve|'m|'ll|'d)?|\p{N}{1,3}| ?[^\s\p{L}\p{N}]+[\r\n/]*|\s*[\r\n]+|\s+(?!\S)|\s+`,
Michael Yang's avatar
llama4  
Michael Yang committed
50
		),
Michael Yang's avatar
Michael Yang committed
51
52
53
		ImageProcessor: newImageProcessor(c),
		VisionModel:    newVisionModel(c),
		TextModel:      newTextModel(c),
Michael Yang's avatar
llama4  
Michael Yang committed
54
55
56
	}

	m.Cache = kvcache.NewWrapperCache(
Michael Yang's avatar
Michael Yang committed
57
		kvcache.NewChunkedAttentionCache(int32(c.Uint("attention.chunk_size", 8192)), m.Shift),
Michael Yang's avatar
llama4  
Michael Yang committed
58
59
60
61
62
63
		kvcache.NewCausalCache(m.Shift),
	)

	return &m, nil
}

64
func (m *Model) EncodeMultimodal(ctx ml.Context, multimodalData []byte) ([]input.Multimodal, error) {
Michael Yang's avatar
llama4  
Michael Yang committed
65
66
67
68
69
70
71
72
73
	if len(m.VisionModel.Layers) < 1 {
		return nil, model.ErrNoVisionModel
	}

	img, _, err := image.Decode(bytes.NewReader(multimodalData))
	if err != nil {
		return nil, err
	}

Michael Yang's avatar
Michael Yang committed
74
	pixelsLocal, pixelsGlobal, size, err := m.ProcessImage(img)
Michael Yang's avatar
llama4  
Michael Yang committed
75
76
77
78
	if err != nil {
		return nil, err
	}

79
	tilesLocal := ctx.Input().FromFloatSlice(pixelsLocal, size.X, size.Y, m.numChannels)
Michael Yang's avatar
llama4  
Michael Yang committed
80

Michael Yang's avatar
Michael Yang committed
81
	ratioW, ratioH := size.X/m.imageSize, size.Y/m.imageSize
Michael Yang's avatar
Michael Yang committed
82
83
84
85
86
87
88
89

	tilesLocal = tilesLocal.Reshape(ctx, size.X/ratioW, ratioW, size.Y, m.numChannels).Permute(ctx, 0, 2, 1, 3).Contiguous(ctx)
	tilesLocal = tilesLocal.Reshape(ctx, size.X/ratioW*size.Y/ratioH, ratioH, ratioW, m.numChannels).Permute(ctx, 0, 3, 2, 1).Contiguous(ctx)
	tilesLocal = tilesLocal.Reshape(ctx, size.X/ratioW, size.Y/ratioH, m.numChannels, ratioH*ratioW)

	pixelValues := tilesLocal

	if len(pixelsGlobal) > 0 {
90
		tilesGlobal := ctx.Input().FromFloatSlice(pixelsGlobal, m.imageSize, m.imageSize, m.numChannels)
Michael Yang's avatar
Michael Yang committed
91
92
93
		pixelValues = pixelValues.Concat(ctx, tilesGlobal, 3)
	}

Michael Yang's avatar
llama4  
Michael Yang committed
94
95
	visionOutputs := m.VisionModel.Forward(ctx, pixelValues)
	visionOutputs = visionOutputs.Reshape(ctx, visionOutputs.Dim(0), visionOutputs.Dim(1)*visionOutputs.Dim(2)*visionOutputs.Dim(3))
Michael Yang's avatar
Michael Yang committed
96
97
	projectedOutputs := m.Projector.Forward(ctx, visionOutputs)

98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
	var multimodal []input.Multimodal
	aspectRatio := image.Point{ratioW, ratioH}

	var offset int
	patchesPerChunk := projectedOutputs.Dim(1)
	if aspectRatio.Y*aspectRatio.X > 1 {
		patchesPerChunk = projectedOutputs.Dim(1) / (aspectRatio.X*aspectRatio.Y + 1)

		for range aspectRatio.Y {
			for x := range aspectRatio.X {
				view := projectedOutputs.View(ctx, projectedOutputs.Stride(1)*offset,
					projectedOutputs.Dim(0), projectedOutputs.Stride(1),
					patchesPerChunk)
				var separator separator
				if x < aspectRatio.X-1 {
					separator.x = true // <|tile_x_separator|>
				} else {
					separator.y = true // <|tile_y_separator|>
				}
				multimodal = append(multimodal, input.Multimodal{Tensor: view, Data: &separator})
				offset += patchesPerChunk
			}
		}
	}
Michael Yang's avatar
Michael Yang committed
122

123
124
125
126
	view := projectedOutputs.View(ctx, projectedOutputs.Stride(1)*offset,
		projectedOutputs.Dim(0), projectedOutputs.Stride(1),
		patchesPerChunk)
	multimodal = append(multimodal, input.Multimodal{Tensor: view, Data: &separator{}})
Michael Yang's avatar
Michael Yang committed
127

128
	return multimodal, nil
Michael Yang's avatar
Michael Yang committed
129
130
}

131
132
133
type separator struct {
	x bool
	y bool
Michael Yang's avatar
llama4  
Michael Yang committed
134
135
}

136
137
func (m *Model) PostTokenize(inputs []*input.Input) ([]*input.Input, error) {
	var result []*input.Input
Michael Yang's avatar
Michael Yang committed
138
	for _, inp := range inputs {
139
		if len(inp.Multimodal) == 0 {
Michael Yang's avatar
Michael Yang committed
140
141
142
143
			result = append(result, inp)
			continue
		}

144
145
		var imageInputs []*input.Input
		imageInputs = append(imageInputs, &input.Input{Token: 200080}) // <|image_start|>
Michael Yang's avatar
Michael Yang committed
146

147
148
149
150
151
152
		for i, mm := range inp.Multimodal {
			patchesPerChunk := mm.Tensor.Dim(1)

			if i < len(inp.Multimodal)-1 {
				separator := mm.Data.(*separator)

153
154
				imageInputs = append(imageInputs, &input.Input{Token: 200092, Multimodal: []input.Multimodal{{Tensor: mm.Tensor}}, MultimodalHash: inp.MultimodalHash, SameBatch: patchesPerChunk}) // <|patch|>
				imageInputs = append(imageInputs, slices.Repeat([]*input.Input{{Token: 200092}}, patchesPerChunk-1)...)
Michael Yang's avatar
Michael Yang committed
155

156
				if separator.x {
157
					imageInputs = append(imageInputs, &input.Input{Token: 200084}) // <|tile_x_separator|>
158
159
				}
				if separator.y {
160
					imageInputs = append(imageInputs, &input.Input{Token: 200085}) // <|tile_y_separator|>
161
162
				}
			} else {
163
164
165
166
				imageInputs = append(imageInputs, &input.Input{Token: 200090})                                                                                                                      // <|image|>
				imageInputs = append(imageInputs, &input.Input{Token: 200092, Multimodal: []input.Multimodal{{Tensor: mm.Tensor}}, MultimodalHash: inp.MultimodalHash, SameBatch: patchesPerChunk}) // <|patch|>
				imageInputs = append(imageInputs, slices.Repeat([]*input.Input{{Token: 200092}}, patchesPerChunk-1)...)
				imageInputs = append(imageInputs, &input.Input{Token: 200080}) // <|image_end|>
Michael Yang's avatar
Michael Yang committed
167
168
169
170
171
172
173
			}
		}

		result = append(result, imageInputs...)
	}

	return result, nil
Michael Yang's avatar
Michael Yang committed
174
175
}

Michael Yang's avatar
llama4  
Michael Yang committed
176
func (m *Model) Forward(ctx ml.Context, batch input.Batch) (ml.Tensor, error) {
177
	positions := ctx.Input().FromIntSlice(batch.Positions, len(batch.Positions))
178
	return m.TextModel.Forward(ctx, batch.Inputs, positions, batch.Outputs, batch, m.Cache), nil
Michael Yang's avatar
llama4  
Michael Yang committed
179
180
181
182
183
}

func init() {
	model.Register("llama4", New)
}