Get up and running with Kimi, GLM, MiniMax, DeepSeek, gpt-oss, Qwen, Gemma and other models.
ollamarunner: Separate text and multimodal graphs
Jesse Gross3c14461d5d22
diff --git a/model/input/input.go b/model/input/input.goindex d66f52a0d..bd9b53ec6 100644--- a/model/input/input.go+++ b/model/input/input.go@@ -2,16 +2,30 @@ package inputimport "github.com/ollama/ollama/ml"+// Multimodal is a multimodal embedding or a component of one.+// For example, it could be a row of an image that can be processed+// independently.+type Multimodal struct {+ // Tensor is the embedding data. Implementations may chose what to+ // store here or it may be nil if not needed. However, any ml.Tensor+ // objects must be stored here and not in Data.+ Tensor ml.Tensor++ // Data is implementation-specific opaque data, such as metadata on how+ // to layout Tensor. It may be nil if not needed. It may also store larger+ // objects such as complete images if they are to be processed later.+ Data any+}+// Input represents one token in the input streamtype Input struct {// Token is a single element of text.Token int32- // Multimodal is opaque data representing a non-text- // element such as an image (or part of one if the image- // can be processed in pieces). It may be either together- // with Token or on its own.- Multimodal any+ // Multimodal is represents a non-text element such as an+ // image (or part of one if the image can be processed in pieces).+ // It may be used either together with Token or on its own.+ Multimodal []Multimodal// MultimodalHash is a unique representation of the data// stored in Multimodal, used for caching and comparing@@ -32,7 +46,7 @@ type Input struct {// Positions slice.type MultimodalIndex struct {Index int- Multimodal any+ Multimodal []Multimodal}// Batch contains the inputs for a model forward passdiff --git a/model/model.go b/model/model.goindex 7883b8517..98381c904 100644--- a/model/model.go+++ b/model/model.go@@ -40,12 +40,13 @@ type MultimodalProcessor interface {// EncodeMultimodal processes a single input (such as an image) and// generates an output (typically an embedding) that can be used by the model.//- // The return value is most typically an ml.Tensor, however, different- // type are possible, such as an object containing a tensor plus- // additional metadata, a slice of tensors or even just the original input.+ // The return value is one or more tensors, each with optional model-specific+ // opaque metadata. Typically, the tensors might be views into an embedding+ // with each view representing a chunk of data that can be processed independently+ // in different batches.//// The result may be cached by the runner.- EncodeMultimodal(ml.Context, []byte) (any, error)+ EncodeMultimodal(ml.Context, []byte) ([]input.Multimodal, error)// PostTokenize is called after tokenization to allow the model to edit the// input stream to correctly arrange multimodal elements.diff --git a/model/models/gemma3/model.go b/model/models/gemma3/model.goindex bf396b6a0..d53eb6ccc 100644--- a/model/models/gemma3/model.go+++ b/model/models/gemma3/model.go@@ -82,7 +82,7 @@ func New(c fs.Config) (model.Model, error) {return &m, nil}-func (m *Model) EncodeMultimodal(ctx ml.Context, multimodalData []byte) (any, error) {+func (m *Model) EncodeMultimodal(ctx ml.Context, multimodalData []byte) ([]input.Multimodal, error) {if len(m.VisionModel.Layers) == 0 {return nil, model.ErrNoVisionModel}@@ -108,22 +108,22 @@ func (m *Model) EncodeMultimodal(ctx ml.Context, multimodalData []byte) (any, ervisionOutputs := m.VisionModel.Forward(ctx, pixelValues)visionOutputs = m.MultiModalProjector.Forward(ctx, visionOutputs, m.imageSize, m.patchSize, m.VisionModel.eps)- return visionOutputs, nil+ return []input.Multimodal{{Tensor: visionOutputs}}, nil}func (m *Model) PostTokenize(inputs []input.Input) ([]input.Input, error) {var result []input.Inputfor _, inp := range inputs {- if inp.Multimodal == nil {+ if len(inp.Multimodal) == 0 {result = append(result, inp)} else {- inputMultimodal := inp.Multimodal.(ml.Tensor)+ inputMultimodal := inp.Multimodal[0].Tensorresult = append(result,- input.Input{Token: 108, SameBatch: inputMultimodal.Dim(1) + 3}, // "\n\n"- input.Input{Token: 255999}, // "<start_of_image>""- input.Input{Multimodal: inputMultimodal, MultimodalHash: inp.MultimodalHash}, // image data is on the first placeholder+ input.Input{Token: 108, SameBatch: inputMultimodal.Dim(1) + 3}, // "\n\n"+ input.Input{Token: 255999}, // "<start_of_image>""+ input.Input{Multimodal: []input.Multimodal{{Tensor: inputMultimodal}}, MultimodalHash: inp.MultimodalHash}, // image data is on the first placeholder)// add image token placeholdersdiff --git a/model/models/gemma3/model_text.go b/model/models/gemma3/model_text.goindex 741818a29..a40614af2 100644--- a/model/models/gemma3/model_text.go+++ b/model/models/gemma3/model_text.go@@ -165,7 +165,7 @@ func (m *TextModel) Forward(ctx ml.Context, inputs, positions, outputs ml.Tensor// set image embeddingsvar except []intfor _, image := range batch.Multimodal {- visionOutputs := image.Multimodal.(ml.Tensor)+ visionOutputs := image.Multimodal[0].Tensorctx.Forward(visionOutputs.Copy(ctx, hiddenState.View(ctx, image.Index*hiddenState.Stride(1), visionOutputs.Dim(0)*visionOutputs.Dim(1))))for i := range visionOutputs.Dim(1) {diff --git a/model/models/llama4/model.go b/model/models/llama4/model.goindex 798f0d162..c94aa72f6 100644--- a/model/models/llama4/model.go+++ b/model/models/llama4/model.go@@ -4,7 +4,6 @@ import ("bytes""image""slices"- "sync""github.com/ollama/ollama/fs""github.com/ollama/ollama/kvcache"@@ -63,7 +62,7 @@ func New(c fs.Config) (model.Model, error) {return &m, nil}-func (m *Model) EncodeMultimodal(ctx ml.Context, multimodalData []byte) (any, error) {+func (m *Model) EncodeMultimodal(ctx ml.Context, multimodalData []byte) ([]input.Multimodal, error) {if len(m.VisionModel.Layers) < 1 {return nil, model.ErrNoVisionModel}@@ -103,70 +102,79 @@ func (m *Model) EncodeMultimodal(ctx ml.Context, multimodalData []byte) (any, ervisionOutputs := m.VisionModel.Forward(ctx, pixelValues)visionOutputs = visionOutputs.Reshape(ctx, visionOutputs.Dim(0), visionOutputs.Dim(1)*visionOutputs.Dim(2)*visionOutputs.Dim(3))projectedOutputs := m.Projector.Forward(ctx, visionOutputs)- return &chunks{Model: m, Tensor: projectedOutputs, aspectRatio: image.Point{ratioW, ratioH}}, nil-}-type chunks struct {- *Model- ml.Tensor- aspectRatio image.Point+ 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+ }+ }+ }- dataOnce sync.Once- data []float32-}+ view := projectedOutputs.View(ctx, projectedOutputs.Stride(1)*offset,+ projectedOutputs.Dim(0), projectedOutputs.Stride(1),+ patchesPerChunk)+ multimodal = append(multimodal, input.Multimodal{Tensor: view, Data: &separator{}})-type chunk struct {- *chunks- s, n int+ return multimodal, nil}-func (r *chunk) floats() []float32 {- r.dataOnce.Do(func() {- temp := r.Backend().NewContext()- defer temp.Close()- temp.Forward(r.Tensor).Compute(r.Tensor)- r.data = r.Floats()- })-- return r.data[r.s*r.Dim(0) : (r.s+r.n)*r.Dim(0)]+type separator struct {+ x bool+ y bool}func (m *Model) PostTokenize(inputs []input.Input) ([]input.Input, error) {var result []input.Inputfor _, inp := range inputs {- if inp.Multimodal == nil {+ if len(inp.Multimodal) == 0 {result = append(result, inp)continue}- t := inp.Multimodal.(*chunks)var imageInputs []input.InputimageInputs = append(imageInputs, input.Input{Token: 200080}) // <|image_start|>- var offset int- patchesPerChunk := t.Dim(1)- if t.aspectRatio.Y*t.aspectRatio.X > 1 {- patchesPerChunk = t.Dim(1) / (t.aspectRatio.X*t.aspectRatio.Y + 1)-- for range t.aspectRatio.Y {- for x := range t.aspectRatio.X {- imageInputs = append(imageInputs, input.Input{Token: 200092, Multimodal: &chunk{t, offset, patchesPerChunk}, MultimodalHash: inp.MultimodalHash, SameBatch: patchesPerChunk}) // <|patch|>- imageInputs = append(imageInputs, slices.Repeat([]input.Input{{Token: 200092}}, patchesPerChunk-1)...)- if x < t.aspectRatio.X-1 {- imageInputs = append(imageInputs, input.Input{Token: 200084}) // <|tile_x_separator|>- }- offset += patchesPerChunk- }+ for i, mm := range inp.Multimodal {+ patchesPerChunk := mm.Tensor.Dim(1)++ if i < len(inp.Multimodal)-1 {+ separator := mm.Data.(*separator)++ 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: 200085}) // <|tile_y_separator|>+ if separator.x {+ imageInputs = append(imageInputs, input.Input{Token: 200084}) // <|tile_x_separator|>+ }+ if separator.y {+ imageInputs = append(imageInputs, input.Input{Token: 200085}) // <|tile_y_separator|>+ }+ } else {+ 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|>}}- imageInputs = append(imageInputs, input.Input{Token: 200090}) // <|image|>- imageInputs = append(imageInputs, input.Input{Token: 200092, Multimodal: &chunk{t, offset, patchesPerChunk}, 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|>-result = append(result, imageInputs...)}diff --git a/model/models/llama4/model_text.go b/model/models/llama4/model_text.goindex 3f9f578f1..d98587bd0 100644--- a/model/models/llama4/model_text.go+++ b/model/models/llama4/model_text.go@@ -210,12 +210,7 @@ func (m *TextModel) Forward(ctx ml.Context, inputs, positions, outputs ml.TensorhiddenStates := m.TokenEmbedding.Forward(ctx, inputs).Duplicate(ctx)for _, mi := range batch.Multimodal {- f32s := mi.Multimodal.(*chunk).floats()- img, err := ctx.Input().FromFloatSlice(f32s, len(f32s)/m.hiddenSize, m.hiddenSize)- if err != nil {- panic(err)- }-+ img := mi.Multimodal[0].Tensorctx.Forward(img.Copy(ctx, hiddenStates.View(ctx, mi.Index*hiddenStates.Stride(1), img.Dim(0)*img.Dim(1))))}diff --git a/model/models/mistral3/model.go b/model/models/mistral3/model.goindex c96852441..b93882a9e 100644--- a/model/models/mistral3/model.go+++ b/model/models/mistral3/model.go@@ -4,7 +4,6 @@ import ("bytes""image""slices"- "sync""github.com/ollama/ollama/fs""github.com/ollama/ollama/kvcache"@@ -105,7 +104,7 @@ func newMultiModalProjector(c fs.Config) *MultiModalProjector {}}-func (m *Model) EncodeMultimodal(ctx ml.Context, multimodalData []byte) (any, error) {+func (m *Model) EncodeMultimodal(ctx ml.Context, multimodalData []byte) ([]input.Multimodal, error) {if len(m.VisionModel.Layers) == 0 {return nil, model.ErrNoVisionModel}@@ -129,37 +128,14 @@ func (m *Model) EncodeMultimodal(ctx ml.Context, multimodalData []byte) (any, erfeatures, size := m.MultiModalProjector.Forward(ctx, visionOutputs, size)// split into patches to be sent to the text transformer- parent := imageFeatures{tensor: features}- rows := make([]*imageRow, size.Y)+ rows := make([]input.Multimodal, size.Y)for i := range rows {- rows[i] = &imageRow{parent: &parent, s: i, shape: []int{features.Dim(0), size.X}}+ rows[i].Tensor = features.View(ctx, features.Stride(1)*size.X*i, features.Dim(0), features.Stride(1), size.X)}return rows, nil}-type imageFeatures struct {- tensor ml.Tensor-- dataOnce sync.Once- data []float32-}--type imageRow struct {- parent *imageFeatures- s int- shape []int-}--func (r *imageRow) data() []float32 {- n := 1- for _, s := range r.shape {- n *= s- }-- return r.parent.data[r.s*n : (r.s+1)*n]-}-// PostTokenize arranges Mistral 3's inputs for the forward pass// In Mistral 3 and Pixtral, the input patches are arranged as follows:// [IMG]...[IMG][IMG_BREAK][IMG]...[IMG][IMG_BREAK][IMG]...[IMG][IMG_END]@@ -168,15 +144,14 @@ func (r *imageRow) data() []float32 {func (m *Model) PostTokenize(inputs []input.Input) ([]input.Input, error) {var result []input.Inputfor _, inp := range inputs {- if inp.Multimodal == nil {+ if len(inp.Multimodal) == 0 {result = append(result, inp)} else {- inputMultimodal := inp.Multimodal.([]*imageRow)- for i, row := range inputMultimodal {+ for i, row := range inp.Multimodal {// [IMG]- result = append(result, input.Input{Token: 10, Multimodal: row, MultimodalHash: inp.MultimodalHash, SameBatch: row.shape[1]})- result = append(result, slices.Repeat([]input.Input{{Token: 10}}, row.shape[1]-1)...)- if i == len(inputMultimodal)-1 {+ result = append(result, input.Input{Token: 10, Multimodal: []input.Multimodal{{Tensor: row.Tensor}}, MultimodalHash: inp.MultimodalHash, SameBatch: row.Tensor.Dim(1)})+ result = append(result, slices.Repeat([]input.Input{{Token: 10}}, row.Tensor.Dim(1)-1)...)+ if i == len(inp.Multimodal)-1 {// [IMG_END]result = append(result, input.Input{Token: 13})} else {diff --git a/model/models/mistral3/model_text.go b/model/models/mistral3/model_text.goindex 565b001a7..17939800f 100644--- a/model/models/mistral3/model_text.go+++ b/model/models/mistral3/model_text.go@@ -9,7 +9,6 @@ import ("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")@@ -20,8 +19,6 @@ type TextOptions struct {}type TextModel struct {- model.Base-TokenEmbedding *nn.Embedding `gguf:"token_embd"`Layers []Layer `gguf:"blk"`OutputNorm *nn.RMSNorm `gguf:"output_norm"`@@ -109,20 +106,7 @@ func (m *TextModel) Forward(ctx ml.Context, inputs, positions, outputs ml.Tensor// image embeddingsfor _, image := range batch.Multimodal {- row := image.Multimodal.(*imageRow)- row.parent.dataOnce.Do(func() {- // use a new, throwaway context so the image tensor is not added to the graph- temp := m.Backend().NewContext()- temp.Forward(row.parent.tensor).Compute(row.parent.tensor)- row.parent.data = row.parent.tensor.Floats()- temp.Close()- })-- imageFeature, err := ctx.Input().FromFloatSlice(row.data(), row.shape...)- if err != nil {- panic(err)- }-+ imageFeature := image.Multimodal[0].Tensorctx.Forward(imageFeature.Copy(ctx, hiddenState.View(ctx, image.Index*hiddenState.Stride(1), imageFeature.Dim(0)*imageFeature.Dim(1))))}diff --git a/model/models/mllama/model.go b/model/models/mllama/model.goindex 4d5bdd4a1..15571d9c2 100644--- a/model/models/mllama/model.go+++ b/model/models/mllama/model.go@@ -59,7 +59,7 @@ func New(c fs.Config) (model.Model, error) {return &m, nil}-func (m *Model) EncodeMultimodal(ctx ml.Context, multimodalData []byte) (any, error) {+func (m *Model) EncodeMultimodal(ctx ml.Context, multimodalData []byte) ([]input.Multimodal, error) {if len(m.VisionModel.Transformer.Layers) == 0 || len(m.GlobalTransformer.Layers) == 0 {return nil, model.ErrNoVisionModel}@@ -92,7 +92,9 @@ func (m *Model) EncodeMultimodal(ctx ml.Context, multimodalData []byte) (any, erpositionIDs := ctx.Arange(0, 1601, 1, ml.DTypeI32)crossAttentionStates := m.VisionModel.Forward(ctx, pixelValues, positionIDs, aspectRatio)- return m.Projector.Forward(ctx, crossAttentionStates), nil+ projectedOutputs := m.Projector.Forward(ctx, crossAttentionStates)++ return []input.Multimodal{{Tensor: projectedOutputs}}, nil}func (m *Model) PostTokenize(inputs []input.Input) ([]input.Input, error) {@@ -108,7 +110,7 @@ func (m *Model) PostTokenize(inputs []input.Input) ([]input.Input, error) {func (m *Model) Forward(ctx ml.Context, batch input.Batch) (ml.Tensor, error) {var crossAttentionStates ml.Tensorif len(batch.Multimodal) > 0 {- crossAttentionStates = batch.Multimodal[len(batch.Multimodal)-1].Multimodal.(ml.Tensor)+ crossAttentionStates = batch.Multimodal[len(batch.Multimodal)-1].Multimodal[0].Tensor}positions, err := ctx.Input().FromIntSlice(batch.Positions, len(batch.Positions))diff --git a/model/models/qwen25vl/model.go b/model/models/qwen25vl/model.goindex 9d243c30f..486554501 100644--- a/model/models/qwen25vl/model.go+++ b/model/models/qwen25vl/model.go@@ -5,7 +5,6 @@ import ("fmt""image""slices"- "sync""github.com/ollama/ollama/fs""github.com/ollama/ollama/kvcache"@@ -77,7 +76,7 @@ func (m *Model) PixelValues(ctx ml.Context, multimodalData []byte) (ml.Tensor, *return pixelValues, grid, nil}-func (m *Model) EncodeMultimodal(ctx ml.Context, multimodalData []byte) (any, error) {+func (m *Model) EncodeMultimodal(ctx ml.Context, multimodalData []byte) ([]input.Multimodal, error) {if len(m.VisionModel.Layers) == 0 {return nil, model.ErrNoVisionModel}@@ -88,31 +87,7 @@ func (m *Model) EncodeMultimodal(ctx ml.Context, multimodalData []byte) (any, er}visionOutputs := m.VisionModel.Forward(ctx, pixels, grid)- return &chunks{Model: m, Tensor: visionOutputs}, nil-}--type chunks struct {- *Model- ml.Tensor-- dataOnce sync.Once- data []float32-}--type chunk struct {- *chunks- s, n int-}--func (r *chunk) floats() []float32 {- r.dataOnce.Do(func() {- temp := r.Backend().NewContext()- defer temp.Close()- temp.Forward(r.Tensor).Compute(r.Tensor)- r.data = r.Floats()- })-- return r.data[r.s*r.Dim(0) : (r.s+r.n)*r.Dim(0)]+ return []input.Multimodal{{Tensor: visionOutputs}}, nil}// PostTokenize arranges Qwen-2.5-VL's inputs for the forward pass@@ -142,20 +117,16 @@ func (m *Model) PostTokenize(inputs []input.Input) ([]input.Input, error) {result = append(result, input.Input{Token: pre[i]})}- // This is an image token with multimodal data- chunksData := inp.Multimodal.(*chunks)- patchesPerChunk := chunksData.Dim(1)+ patchesPerChunk := inp.Multimodal[0].Tensor.Dim(1)// First add the vision start token- result = append(result, input.Input{Token: visionStartToken, SameBatch: patchesPerChunk + 2})+ result = append(result, input.Input{Token: visionStartToken, SameBatch: patchesPerChunk + 1})// Add the image token with the multimodal tensor data at the first position- // Create a chunk with proper s and n valuesresult = append(result, input.Input{Token: imageToken,- Multimodal: &chunk{chunks: chunksData, s: 0, n: patchesPerChunk},+ Multimodal: inp.Multimodal,MultimodalHash: inp.MultimodalHash,- SameBatch: patchesPerChunk,})// Add the placeholder tokens for the remaining positions (tokensPerGrid-1)diff --git a/model/models/qwen25vl/model_text.go b/model/models/qwen25vl/model_text.goindex 6b062f8c5..800fd961d 100644--- a/model/models/qwen25vl/model_text.go+++ b/model/models/qwen25vl/model_text.go@@ -129,12 +129,7 @@ func (m *TextModel) Forward(ctx ml.Context, inputs, positions, outputs ml.TensorhiddenStates := m.TokenEmbedding.Forward(ctx, inputs).Duplicate(ctx)for _, mi := range batch.Multimodal {- f32s := mi.Multimodal.(*chunk).floats()- img, err := ctx.Input().FromFloatSlice(f32s, len(f32s)/m.hiddenSize, m.hiddenSize)- if err != nil {- panic(err)- }-+ img := mi.Multimodal[0].Tensorctx.Forward(img.Copy(ctx, hiddenStates.View(ctx, mi.Index*hiddenStates.Stride(1), img.Dim(0)*img.Dim(1))))}diff --git a/runner/ollamarunner/cache_test.go b/runner/ollamarunner/cache_test.goindex 062b654cf..6897b5e46 100644--- a/runner/ollamarunner/cache_test.go+++ b/runner/ollamarunner/cache_test.go@@ -3,7 +3,6 @@ package ollamarunnerimport ("errors""fmt"- "image""testing""time"@@ -12,10 +11,6 @@ import ()func TestCountCommon(t *testing.T) {- imgA := image.NewRGBA(image.Rect(0, 0, 100, 100))- imgB := image.NewRGBA(image.Rect(0, 0, 50, 50))- imgC := image.NewRGBA(image.Rect(50, 50, 100, 100))-tests := []struct {name stringt1 []input.Input@@ -36,20 +31,20 @@ func TestCountCommon(t *testing.T) {},{name: "Image Prefix",- t1: []input.Input{{Multimodal: imgA, MultimodalHash: 1}},- t2: []input.Input{{Multimodal: imgA, MultimodalHash: 1}, {Multimodal: imgB, MultimodalHash: 2}, {Multimodal: imgC, MultimodalHash: 3}},+ t1: []input.Input{{MultimodalHash: 1}},+ t2: []input.Input{{MultimodalHash: 1}, {MultimodalHash: 2}, {MultimodalHash: 3}},expected: 1,},{name: "Mixed",- t1: []input.Input{{Token: 1}, {Multimodal: imgA, MultimodalHash: 1}},- t2: []input.Input{{Token: 1}, {Multimodal: imgA, MultimodalHash: 1}, {Token: 5}},+ t1: []input.Input{{Token: 1}, {MultimodalHash: 1}},+ t2: []input.Input{{Token: 1}, {MultimodalHash: 1}, {Token: 5}},expected: 2,},{name: "Mixed, Same Length",- t1: []input.Input{{Token: 1}, {Multimodal: imgA, MultimodalHash: 1}},- t2: []input.Input{{Token: 1}, {Multimodal: imgB, MultimodalHash: 2}},+ t1: []input.Input{{Token: 1}, {MultimodalHash: 1}},+ t2: []input.Input{{Token: 1}, {MultimodalHash: 2}},expected: 1,},{diff --git a/runner/ollamarunner/multimodal.go b/runner/ollamarunner/multimodal.gonew file mode 100644index 000000000..16d359219--- /dev/null+++ b/runner/ollamarunner/multimodal.go@@ -0,0 +1,103 @@+package ollamarunner++import (+ "errors"++ "github.com/ollama/ollama/ml"+ "github.com/ollama/ollama/model/input"+)++// Tensors can't be used across multiple compute graphs. This is a problem+// if a single embedding is split across batches using views since all of+// the views will have the same source tensor. We also don't want to+// recompute the entire embedding for each batch.+//+// To avoid this, we compute all of the tensors for the embedding on the+// first use and then store the result in system memory. When we need+// additional tensors, we recreate them from the stored data.++// multimodalEntry represents the embeddings of a single object (such+// as an image).+type multimodalEntry struct {+ // mm is the original set of tensors created by EncodeMultimodal+ mm []input.Multimodal++ // data is the computed result of mm. Nil if not yet computed+ data [][]float32+}++// multimodalStore maps from an individual tensor (of which there+// may be many in a single multimodal object) to its parent embedding+type multimodalStore map[ml.Tensor]*multimodalEntry++func newMultimodalStore() multimodalStore {+ return make(multimodalStore)+}++// addMultimodal stores an embedding for later use in a compute graph+func (m multimodalStore) addMultimodal(embedding []input.Multimodal) {+ entry := &multimodalEntry{mm: embedding}++ for _, e := range embedding {+ if e.Tensor != nil {+ m[e.Tensor] = entry+ }+ }+}++// getMultimodal takes a source set of tensors (which may contain a whole or+// parts of one or more images) and returns the equivalent that can be used in+// the current context+func (m multimodalStore) getMultimodal(backend ml.Backend, ctx ml.Context, in []input.Multimodal) ([]input.Multimodal, error) {+ out := make([]input.Multimodal, len(in))+ for i := range out {+ if in[i].Tensor != nil {+ var err error+ out[i].Tensor, err = m.getTensor(backend, ctx, in[i].Tensor)+ if err != nil {+ return nil, err+ }+ }++ out[i].Data = in[i].Data+ }++ return out, nil+}++func (m multimodalStore) getTensor(backend ml.Backend, ctx ml.Context, in ml.Tensor) (ml.Tensor, error) {+ entry := m[in]++ if entry.data == nil {+ computeCtx := backend.NewContext()+ defer computeCtx.Close()++ var tensors []ml.Tensor+ for _, t := range entry.mm {+ if t.Tensor != nil {+ tensors = append(tensors, t.Tensor)+ }+ }++ if len(tensors) == 0 {+ return nil, nil+ }++ computeCtx.Forward(tensors...).Compute(tensors...)++ entry.data = make([][]float32, len(entry.mm))+ for i, t := range entry.mm {+ if t.Tensor != nil {+ entry.data[i] = t.Tensor.Floats()+ }+ }+ }++ for i, t := range entry.mm {+ if in == t.Tensor {+ return ctx.Input().FromFloatSlice(entry.data[i], t.Tensor.Shape()...)+ }+ }++ return nil, errors.New("multimodal tensor not found")+}diff --git a/runner/ollamarunner/runner.go b/runner/ollamarunner/runner.goindex 9a5222236..4e203b7be 100644--- a/runner/ollamarunner/runner.go+++ b/runner/ollamarunner/runner.go@@ -40,6 +40,9 @@ type Sequence struct {// multimodal embeddingsctxs []ml.Context+ // mmStore holds multimodal embeddings to mange memory and enable splitting across batches+ mmStore multimodalStore+// batch indexiBatch int@@ -101,7 +104,7 @@ func (s *Server) NewSequence(prompt string, images []llm.ImageData, params NewSestartTime := time.Now()- inputs, ctxs, err := s.inputs(prompt, images)+ inputs, ctxs, mmStore, err := s.inputs(prompt, images)if err != nil {return nil, fmt.Errorf("failed to process inputs: %w", err)} else if len(inputs) == 0 {@@ -156,6 +159,7 @@ func (s *Server) NewSequence(prompt string, images []llm.ImageData, params NewSereturn &Sequence{ctxs: ctxs,+ mmStore: mmStore,inputs: inputs,numPromptInputs: len(inputs),startProcessingTime: startTime,@@ -174,9 +178,10 @@ func (s *Server) NewSequence(prompt string, images []llm.ImageData, params NewSe// inputs processes the prompt and images into a list of inputs// by splitting the prompt on [img-<n>] tags, tokenizing text and// decoding images-func (s *Server) inputs(prompt string, images []llm.ImageData) ([]input.Input, []ml.Context, error) {+func (s *Server) inputs(prompt string, images []llm.ImageData) ([]input.Input, []ml.Context, multimodalStore, error) {var inputs []input.Inputvar ctxs []ml.Context+ var mmStore multimodalStorevar parts []stringvar matches [][]string@@ -187,6 +192,7 @@ func (s *Server) inputs(prompt string, images []llm.ImageData) ([]input.Input, [re := regexp.MustCompile(`\[img-(\d+)\]`)parts = re.Split(prompt, -1)matches = re.FindAllStringSubmatch(prompt, -1)+ mmStore = newMultimodalStore()} else {parts = []string{prompt}}@@ -196,7 +202,7 @@ func (s *Server) inputs(prompt string, images []llm.ImageData) ([]input.Input, [// text - tokenizetokens, err := s.model.(model.TextProcessor).Encode(part, i == 0)if err != nil {- return nil, nil, err+ return nil, nil, nil, err}for _, t := range tokens {@@ -216,7 +222,7 @@ func (s *Server) inputs(prompt string, images []llm.ImageData) ([]input.Input, [}if imageIndex < 0 {- return nil, nil, fmt.Errorf("invalid image index: %d", n)+ return nil, nil, nil, fmt.Errorf("invalid image index: %d", n)}ctx := s.model.Backend().NewContext()@@ -224,13 +230,15 @@ func (s *Server) inputs(prompt string, images []llm.ImageData) ([]input.Input, [ctxs = append(ctxs, ctx)imageEmbeddings, err := multimodalProcessor.EncodeMultimodal(ctx, images[imageIndex].Data)if err != nil {- return nil, nil, err+ return nil, nil, nil, err}s.multimodalHash.Reset()_, _ = s.multimodalHash.Write(images[imageIndex].Data)imageHash := s.multimodalHash.Sum64()+ mmStore.addMultimodal(imageEmbeddings)+inputs = append(inputs, input.Input{Multimodal: imageEmbeddings, MultimodalHash: imageHash})postTokenize = true}@@ -240,11 +248,11 @@ func (s *Server) inputs(prompt string, images []llm.ImageData) ([]input.Input, [var err errorinputs, err = multimodalProcessor.PostTokenize(inputs)if err != nil {- return nil, nil, err+ return nil, nil, nil, err}}- return inputs, ctxs, nil+ return inputs, ctxs, mmStore, nil}type Server struct {@@ -363,6 +371,9 @@ func (s *Server) processBatch() error {}defer s.mu.Unlock()+ ctx := s.model.Backend().NewContext()+ defer ctx.Close()+var batchInputs []int32var batch input.Batch@@ -433,7 +444,11 @@ func (s *Server) processBatch() error {batchInputs = append(batchInputs, inp.Token)if inp.Multimodal != nil {- batch.Multimodal = append(batch.Multimodal, input.MultimodalIndex{Index: len(batchInputs) - 1, Multimodal: inp.Multimodal})+ mm, err := seq.mmStore.getMultimodal(s.model.Backend(), ctx, inp.Multimodal)+ if err != nil {+ return err+ }+ batch.Multimodal = append(batch.Multimodal, input.MultimodalIndex{Index: len(batchInputs) - 1, Multimodal: mm})}batch.Positions = append(batch.Positions, int32(len(seq.cache.Inputs)+len(seq.pendingInputs)))@@ -459,9 +474,6 @@ func (s *Server) processBatch() error {return nil}- ctx := s.model.Backend().NewContext()- defer ctx.Close()-modelOutput, err := model.Forward(ctx, s.model, batchInputs, batch)if err != nil {return fmt.Errorf("failed to decode batch: %w", err)