Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
ollama
Commits
ff180c34
Unverified
Commit
ff180c34
authored
May 19, 2025
by
Michael Yang
Committed by
GitHub
May 19, 2025
Browse files
fix llama and mistral3 models (#10774)
* fix llama model * fix mistral3.1 model do not set default vision layers
parent
3fe74fba
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
13 additions
and
32 deletions
+13
-32
model/models/llama/model.go
model/models/llama/model.go
+7
-11
model/models/mistral3/model.go
model/models/mistral3/model.go
+1
-6
model/models/mistral3/model_text.go
model/models/mistral3/model_text.go
+4
-14
model/models/mistral3/model_vision.go
model/models/mistral3/model_vision.go
+1
-1
No files found.
model/models/llama/model.go
View file @
ff180c34
package
llama
package
llama
import
(
import
(
"
fmt
"
"
cmp
"
"math"
"math"
"strings"
"github.com/ollama/ollama/fs"
"github.com/ollama/ollama/fs"
"github.com/ollama/ollama/kvcache"
"github.com/ollama/ollama/kvcache"
...
@@ -14,9 +13,9 @@ import (
...
@@ -14,9 +13,9 @@ import (
)
)
type
Options
struct
{
type
Options
struct
{
hiddenSize
,
numHeads
,
numKVHeads
int
hiddenSize
,
numHeads
,
numKVHeads
,
headDim
int
eps
,
ropeBase
,
ropeScale
float32
eps
,
ropeBase
,
ropeScale
float32
ropeDim
uint32
ropeDim
uint32
}
}
type
Model
struct
{
type
Model
struct
{
...
@@ -32,10 +31,6 @@ type Model struct {
...
@@ -32,10 +31,6 @@ type Model struct {
}
}
func
New
(
c
fs
.
Config
)
(
model
.
Model
,
error
)
{
func
New
(
c
fs
.
Config
)
(
model
.
Model
,
error
)
{
if
!
strings
.
EqualFold
(
c
.
String
(
"tokenizer.ggml.model"
),
"gpt2"
)
{
return
nil
,
fmt
.
Errorf
(
"tokenizer %s not yet supported"
,
c
.
String
(
"tokenizer.ggml.model"
))
}
m
:=
Model
{
m
:=
Model
{
BytePairEncoding
:
model
.
NewBytePairEncoding
(
BytePairEncoding
:
model
.
NewBytePairEncoding
(
c
.
String
(
"tokenizer.ggml.pretokenizer"
,
`(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\r\n\p{L}\p{N}]?\p{L}+|\p{N}{1,3}| ?[^\s\p{L}\p{N}]+[\r\n]*|\s*[\r\n]+|\s+(?!\S)|\s+`
),
c
.
String
(
"tokenizer.ggml.pretokenizer"
,
`(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\r\n\p{L}\p{N}]?\p{L}+|\p{N}{1,3}| ?[^\s\p{L}\p{N}]+[\r\n]*|\s*[\r\n]+|\s+(?!\S)|\s+`
),
...
@@ -57,6 +52,7 @@ func New(c fs.Config) (model.Model, error) {
...
@@ -57,6 +52,7 @@ func New(c fs.Config) (model.Model, error) {
hiddenSize
:
int
(
c
.
Uint
(
"embedding_length"
)),
hiddenSize
:
int
(
c
.
Uint
(
"embedding_length"
)),
numHeads
:
int
(
c
.
Uint
(
"attention.head_count"
)),
numHeads
:
int
(
c
.
Uint
(
"attention.head_count"
)),
numKVHeads
:
int
(
c
.
Uint
(
"attention.head_count_kv"
)),
numKVHeads
:
int
(
c
.
Uint
(
"attention.head_count_kv"
)),
headDim
:
int
(
c
.
Uint
(
"attention.key_length"
)),
eps
:
c
.
Float
(
"attention.layer_norm_rms_epsilon"
),
eps
:
c
.
Float
(
"attention.layer_norm_rms_epsilon"
),
ropeBase
:
c
.
Float
(
"rope.freq_base"
),
ropeBase
:
c
.
Float
(
"rope.freq_base"
),
ropeScale
:
c
.
Float
(
"rope.freq_scale"
,
1
),
ropeScale
:
c
.
Float
(
"rope.freq_scale"
,
1
),
...
@@ -79,7 +75,7 @@ type SelfAttention struct {
...
@@ -79,7 +75,7 @@ type SelfAttention struct {
func
(
sa
*
SelfAttention
)
Forward
(
ctx
ml
.
Context
,
hiddenState
,
positionIDs
ml
.
Tensor
,
cache
kvcache
.
Cache
,
opts
*
Options
)
ml
.
Tensor
{
func
(
sa
*
SelfAttention
)
Forward
(
ctx
ml
.
Context
,
hiddenState
,
positionIDs
ml
.
Tensor
,
cache
kvcache
.
Cache
,
opts
*
Options
)
ml
.
Tensor
{
batchSize
:=
hiddenState
.
Dim
(
1
)
batchSize
:=
hiddenState
.
Dim
(
1
)
headDim
:=
opts
.
hiddenSize
/
opts
.
numHeads
headDim
:=
cmp
.
Or
(
opts
.
headDim
,
opts
.
hiddenSize
/
opts
.
numHeads
)
ropeType
:=
uint32
(
0
)
ropeType
:=
uint32
(
0
)
q
:=
sa
.
Query
.
Forward
(
ctx
,
hiddenState
)
q
:=
sa
.
Query
.
Forward
(
ctx
,
hiddenState
)
...
@@ -95,7 +91,7 @@ func (sa *SelfAttention) Forward(ctx ml.Context, hiddenState, positionIDs ml.Ten
...
@@ -95,7 +91,7 @@ func (sa *SelfAttention) Forward(ctx ml.Context, hiddenState, positionIDs ml.Ten
scaleFactor
:=
1.0
/
math
.
Sqrt
(
float64
(
headDim
))
scaleFactor
:=
1.0
/
math
.
Sqrt
(
float64
(
headDim
))
kqv
:=
nn
.
Attention
(
ctx
,
q
,
k
,
v
,
scaleFactor
,
cache
)
kqv
:=
nn
.
Attention
(
ctx
,
q
,
k
,
v
,
scaleFactor
,
cache
)
kqv
=
kqv
.
Reshape
(
ctx
,
opts
.
hiddenSize
,
batchSize
)
kqv
=
kqv
.
Reshape
(
ctx
,
headDim
*
opts
.
numHeads
,
batchSize
)
return
sa
.
Output
.
Forward
(
ctx
,
kqv
)
return
sa
.
Output
.
Forward
(
ctx
,
kqv
)
}
}
...
...
model/models/mistral3/model.go
View file @
ff180c34
...
@@ -31,11 +31,6 @@ var _ model.MultimodalProcessor = (*Model)(nil)
...
@@ -31,11 +31,6 @@ var _ model.MultimodalProcessor = (*Model)(nil)
var
_
model
.
TextProcessor
=
(
*
Model
)(
nil
)
var
_
model
.
TextProcessor
=
(
*
Model
)(
nil
)
func
New
(
c
fs
.
Config
)
(
model
.
Model
,
error
)
{
func
New
(
c
fs
.
Config
)
(
model
.
Model
,
error
)
{
textModel
,
err
:=
NewTextModel
(
c
)
if
err
!=
nil
{
return
nil
,
err
}
m
:=
&
Model
{
m
:=
&
Model
{
BytePairEncoding
:
model
.
NewBytePairEncoding
(
BytePairEncoding
:
model
.
NewBytePairEncoding
(
c
.
String
(
"tokenizer.ggml.pretokenizer"
,
`[^\r\n\p{L}\p{N}]?[\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{M}]*[\p{Ll}\p{Lm}\p{Lo}\p{M}]+|[^\r\n\p{L}\p{N}]?[\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{M}]+[\p{Ll}\p{Lm}\p{Lo}\p{M}]*|\p{N}| ?[^\s\p{L}\p{N}]+[\r\n/]*|\s*[\r\n]+|\s+(?!\S)|\s+`
),
c
.
String
(
"tokenizer.ggml.pretokenizer"
,
`[^\r\n\p{L}\p{N}]?[\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{M}]*[\p{Ll}\p{Lm}\p{Lo}\p{M}]+|[^\r\n\p{L}\p{N}]?[\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{M}]+[\p{Ll}\p{Lm}\p{Lo}\p{M}]*|\p{N}| ?[^\s\p{L}\p{N}]+[\r\n/]*|\s*[\r\n]+|\s+(?!\S)|\s+`
),
...
@@ -52,7 +47,7 @@ func New(c fs.Config) (model.Model, error) {
...
@@ -52,7 +47,7 @@ func New(c fs.Config) (model.Model, error) {
),
),
},
},
),
),
TextModel
:
t
extModel
,
TextModel
:
newT
extModel
(
c
)
,
VisionModel
:
newVisionModel
(
c
),
VisionModel
:
newVisionModel
(
c
),
ImageProcessor
:
newImageProcessor
(
c
),
ImageProcessor
:
newImageProcessor
(
c
),
MultiModalProjector
:
newMultiModalProjector
(
c
),
MultiModalProjector
:
newMultiModalProjector
(
c
),
...
...
model/models/mistral3/model_text.go
View file @
ff180c34
package
mistral3
package
mistral3
import
(
import
(
"
fmt
"
"
cmp
"
"math"
"math"
"strings"
"github.com/ollama/ollama/fs"
"github.com/ollama/ollama/fs"
"github.com/ollama/ollama/kvcache"
"github.com/ollama/ollama/kvcache"
...
@@ -37,10 +36,7 @@ type SelfAttention struct {
...
@@ -37,10 +36,7 @@ type SelfAttention struct {
func
(
sa
*
SelfAttention
)
Forward
(
ctx
ml
.
Context
,
hiddenState
,
positionIDs
ml
.
Tensor
,
cache
kvcache
.
Cache
,
opts
*
TextOptions
)
ml
.
Tensor
{
func
(
sa
*
SelfAttention
)
Forward
(
ctx
ml
.
Context
,
hiddenState
,
positionIDs
ml
.
Tensor
,
cache
kvcache
.
Cache
,
opts
*
TextOptions
)
ml
.
Tensor
{
batchSize
:=
hiddenState
.
Dim
(
1
)
batchSize
:=
hiddenState
.
Dim
(
1
)
ropeType
:=
uint32
(
0
)
ropeType
:=
uint32
(
0
)
headDim
:=
opts
.
headDim
headDim
:=
cmp
.
Or
(
opts
.
headDim
,
opts
.
hiddenSize
/
opts
.
numHeads
)
if
headDim
==
0
{
headDim
=
opts
.
hiddenSize
/
opts
.
numHeads
}
q
:=
sa
.
Query
.
Forward
(
ctx
,
hiddenState
)
q
:=
sa
.
Query
.
Forward
(
ctx
,
hiddenState
)
q
=
q
.
Reshape
(
ctx
,
headDim
,
opts
.
numHeads
,
batchSize
)
q
=
q
.
Reshape
(
ctx
,
headDim
,
opts
.
numHeads
,
batchSize
)
...
@@ -125,12 +121,8 @@ func (m *TextModel) Forward(ctx ml.Context, inputs, positions, outputs ml.Tensor
...
@@ -125,12 +121,8 @@ func (m *TextModel) Forward(ctx ml.Context, inputs, positions, outputs ml.Tensor
return
m
.
Output
.
Forward
(
ctx
,
hiddenState
)
return
m
.
Output
.
Forward
(
ctx
,
hiddenState
)
}
}
func
NewTextModel
(
c
fs
.
Config
)
(
*
TextModel
,
error
)
{
func
newTextModel
(
c
fs
.
Config
)
*
TextModel
{
if
!
strings
.
EqualFold
(
c
.
String
(
"tokenizer.ggml.model"
),
"gpt2"
)
{
return
&
TextModel
{
return
nil
,
fmt
.
Errorf
(
"tokenizer %s not yet supported"
,
c
.
String
(
"tokenizer.ggml.model"
))
}
textModel
:=
&
TextModel
{
Layers
:
make
([]
Layer
,
c
.
Uint
(
"block_count"
)),
Layers
:
make
([]
Layer
,
c
.
Uint
(
"block_count"
)),
TextOptions
:
&
TextOptions
{
TextOptions
:
&
TextOptions
{
hiddenSize
:
int
(
c
.
Uint
(
"embedding_length"
)),
hiddenSize
:
int
(
c
.
Uint
(
"embedding_length"
)),
...
@@ -143,6 +135,4 @@ func NewTextModel(c fs.Config) (*TextModel, error) {
...
@@ -143,6 +135,4 @@ func NewTextModel(c fs.Config) (*TextModel, error) {
ropeDim
:
c
.
Uint
(
"rope.dimension_count"
),
ropeDim
:
c
.
Uint
(
"rope.dimension_count"
),
},
},
}
}
return
textModel
,
nil
}
}
model/models/mistral3/model_vision.go
View file @
ff180c34
...
@@ -170,7 +170,7 @@ func (m *VisionModel) Forward(ctx ml.Context, pixelValues ml.Tensor) ml.Tensor {
...
@@ -170,7 +170,7 @@ func (m *VisionModel) Forward(ctx ml.Context, pixelValues ml.Tensor) ml.Tensor {
func
newVisionModel
(
c
fs
.
Config
)
*
VisionModel
{
func
newVisionModel
(
c
fs
.
Config
)
*
VisionModel
{
return
&
VisionModel
{
return
&
VisionModel
{
Layers
:
make
([]
VisionEncoderLayer
,
c
.
Uint
(
"vision.block_count"
,
24
)),
Layers
:
make
([]
VisionEncoderLayer
,
c
.
Uint
(
"vision.block_count"
)),
VisionModelOptions
:
&
VisionModelOptions
{
VisionModelOptions
:
&
VisionModelOptions
{
hiddenSize
:
int
(
c
.
Uint
(
"vision.embedding_length"
,
1024
)),
hiddenSize
:
int
(
c
.
Uint
(
"vision.embedding_length"
,
1024
)),
numHeads
:
int
(
c
.
Uint
(
"vision.attention.head_count"
,
16
)),
numHeads
:
int
(
c
.
Uint
(
"vision.attention.head_count"
,
16
)),
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment