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
4a8fc3f9
Commit
4a8fc3f9
authored
Jul 09, 2025
by
Michael Yang
Browse files
bf16
parent
4183bb05
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
11 additions
and
7 deletions
+11
-7
convert/reader.go
convert/reader.go
+6
-5
convert/reader_safetensors.go
convert/reader_safetensors.go
+5
-2
No files found.
convert/reader.go
View file @
4a8fc3f9
...
...
@@ -31,8 +31,9 @@ func (t tensorBase) Shape() []uint64 {
}
const
(
tensorKindF32
uint32
=
iota
tensorKindF16
tensorKindFP32
uint32
=
iota
tensorKindFP16
tensorKindBF16
=
30
)
func
(
t
tensorBase
)
Kind
()
uint32
{
...
...
@@ -43,16 +44,16 @@ func (t tensorBase) Kind() uint32 {
t
.
name
==
"v.pre_tile_position_embd.weight"
||
t
.
name
==
"v.post_tile_position_embd.weight"
{
// these tensors are always F32
return
0
return
tensorKindFP32
}
switch
len
(
t
.
shape
)
{
case
0
:
panic
(
"invalid tensor shape"
)
case
1
:
return
tensorKindF32
return
tensorKindF
P
32
default
:
return
tensorKindF16
return
tensorKind
B
F16
}
}
...
...
convert/reader_safetensors.go
View file @
4a8fc3f9
...
...
@@ -162,15 +162,18 @@ func (st safetensor) WriteTo(w io.Writer) (int64, error) {
}
switch
st
.
Kind
()
{
case
tensorKindF32
:
case
tensorKindF
P
32
:
return
0
,
binary
.
Write
(
w
,
binary
.
LittleEndian
,
f32s
)
case
tensorKindF16
:
case
tensorKindF
P
16
:
f16s
:=
make
([]
uint16
,
len
(
f32s
))
for
i
:=
range
f32s
{
f16s
[
i
]
=
float16
.
Fromfloat32
(
f32s
[
i
])
.
Bits
()
}
return
0
,
binary
.
Write
(
w
,
binary
.
LittleEndian
,
f16s
)
case
tensorKindBF16
:
u8s
:=
bfloat16
.
EncodeFloat32
(
f32s
)
return
0
,
binary
.
Write
(
w
,
binary
.
LittleEndian
,
u8s
)
default
:
return
0
,
fmt
.
Errorf
(
"unknown storage type: %d"
,
st
.
Kind
())
}
...
...
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