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
40b8fdbd
Commit
40b8fdbd
authored
Apr 03, 2025
by
Michael Yang
Committed by
Michael Yang
Apr 18, 2025
Browse files
arange
parent
1d99451a
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
42 additions
and
20 deletions
+42
-20
kvcache/causal_test.go
kvcache/causal_test.go
+11
-0
ml/backend.go
ml/backend.go
+3
-0
ml/backend/ggml/ggml.go
ml/backend/ggml/ggml.go
+26
-0
model/models/gemma3/model_vision.go
model/models/gemma3/model_vision.go
+1
-10
model/models/mllama/model.go
model/models/mllama/model.go
+1
-10
No files found.
kvcache/causal_test.go
View file @
40b8fdbd
...
...
@@ -424,6 +424,17 @@ func (c *testContext) FromIntSlice(s []int32, shape ...int) (ml.Tensor, error) {
return
out
,
nil
}
func
(
c
*
testContext
)
Arange
(
start
,
stop
,
step
float32
,
dtype
ml
.
DType
)
ml
.
Tensor
{
s
:=
make
([]
float32
,
0
,
int
((
stop
-
start
)
/
step
))
for
i
:=
start
;
i
<
stop
;
i
+=
step
{
s
=
append
(
s
,
i
)
}
out
,
_
:=
c
.
FromFloatSlice
(
s
,
len
(
s
))
out
.
(
*
testTensor
)
.
dtype
=
dtype
return
out
}
func
(
c
*
testContext
)
Input
()
ml
.
Context
{
return
c
}
func
(
c
*
testContext
)
Layer
(
int
)
ml
.
Context
{
return
c
}
...
...
ml/backend.go
View file @
40b8fdbd
...
...
@@ -95,6 +95,9 @@ type Context interface {
FromFloatSlice
(
s
[]
float32
,
shape
...
int
)
(
Tensor
,
error
)
FromIntSlice
(
s
[]
int32
,
shape
...
int
)
(
Tensor
,
error
)
// Arange creates a 1D tensor with values within an interval (start, stop] increased by step.
Arange
(
start
,
stop
,
step
float32
,
dtype
DType
)
Tensor
Forward
(
...
Tensor
)
Context
Compute
(
...
Tensor
)
...
...
ml/backend/ggml/ggml.go
View file @
40b8fdbd
...
...
@@ -696,6 +696,32 @@ func (c *Context) FromIntSlice(s []int32, shape ...int) (ml.Tensor, error) {
return
t
,
nil
}
func
(
c
Context
)
Arange
(
start
,
stop
,
step
float32
,
dtype
ml
.
DType
)
ml
.
Tensor
{
switch
dtype
{
case
ml
.
DTypeF32
:
// ggml_arange creates a float32 tensor
return
&
Tensor
{
b
:
c
.
b
,
t
:
C
.
ggml_arange
(
c
.
ctx
,
C
.
float
(
start
),
C
.
float
(
stop
),
C
.
float
(
step
)),
}
case
ml
.
DTypeI32
:
// ggml_cast does not support float32 to int32 conversion
arange
:=
make
([]
int32
,
0
,
int
((
stop
-
start
)
/
step
))
for
i
:=
start
;
i
<
stop
;
i
+=
step
{
arange
=
append
(
arange
,
int32
(
i
))
}
t
,
err
:=
c
.
Input
()
.
FromIntSlice
(
arange
,
len
(
arange
))
if
err
!=
nil
{
panic
(
err
)
}
return
t
default
:
panic
(
"unsupported dtype for arange"
)
}
}
func
(
c
*
Context
)
Close
()
{
if
c
!=
nil
{
for
_
,
b
:=
range
*
c
.
allocatedBuffers
{
...
...
model/models/gemma3/model_vision.go
View file @
40b8fdbd
...
...
@@ -92,16 +92,7 @@ func (m *VisionModel) Forward(ctx ml.Context, pixelValues ml.Tensor) ml.Tensor {
hiddenState
=
hiddenState
.
Reshape
(
ctx
,
numPatches
,
m
.
hiddenSize
)
hiddenState
=
hiddenState
.
Permute
(
ctx
,
1
,
0
,
2
,
3
)
.
Contiguous
(
ctx
)
positions
:=
make
([]
int32
,
numPatches
)
for
i
:=
range
positions
{
positions
[
i
]
=
int32
(
i
)
}
positionIDs
,
err
:=
ctx
.
Input
()
.
FromIntSlice
(
positions
,
len
(
positions
))
if
err
!=
nil
{
panic
(
err
)
}
positionIDs
:=
ctx
.
Arange
(
0
,
float32
(
numPatches
),
1
,
ml
.
DTypeI32
)
hiddenState
=
hiddenState
.
Add
(
ctx
,
m
.
PositionEmbedding
.
Forward
(
ctx
,
positionIDs
))
for
_
,
layer
:=
range
m
.
Layers
{
...
...
model/models/mllama/model.go
View file @
40b8fdbd
...
...
@@ -93,16 +93,7 @@ func (m *Model) EncodeMultimodal(ctx ml.Context, multimodalData []byte) (any, er
return
nil
,
err
}
positions
:=
make
([]
int32
,
1601
)
for
i
:=
range
positions
{
positions
[
i
]
=
int32
(
i
)
}
positionIDs
,
err
:=
ctx
.
Input
()
.
FromIntSlice
(
positions
,
len
(
positions
))
if
err
!=
nil
{
return
nil
,
err
}
positionIDs
:=
ctx
.
Arange
(
0
,
1601
,
1
,
ml
.
DTypeI32
)
crossAttentionStates
:=
m
.
VisionModel
.
Forward
(
ctx
,
pixelValues
,
positionIDs
,
aspectRatio
)
return
m
.
Projector
.
Forward
(
ctx
,
crossAttentionStates
),
nil
}
...
...
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