drop bfloat16 dependency

goos: darwin
goarch: arm64
pkg: github.com/ollama/ollama/convert/bfloat16
cpu: Apple M3 Max
BenchmarkBfloat16/d4l3k/go-bfloat16-16               516           2269453 ns/op
BenchmarkBfloat16/simple-16                          1759            626316 ns/op
PASS
ok      github.com/ollama/ollama/convert/bfloat16        2.502s
This commit is contained in:
Michael Yang
2025-08-05 17:32:22 -07:00
parent ef7d26ba2c
commit 276c4df770
6 changed files with 123 additions and 21 deletions

View File

@@ -0,0 +1,21 @@
package bfloat16
import "math"
// FromFloat32s converts a slice of float32 values to a slice of bfloat16 values, represented as uint16s.
func FromFloat32s(f32s []float32) (u16s []uint16) {
u16s = make([]uint16, len(f32s))
for i := range f32s {
u16s[i] = uint16(math.Float32bits(f32s[i]) >> 16)
}
return u16s
}
// Float32s converts a slice of bfloat16 values, represented as uint16s, back to a slice of float32 values.
func Float32s(u16s []uint16) (f32s []float32) {
f32s = make([]float32, len(u16s))
for i := range u16s {
f32s[i] = math.Float32frombits(uint32(u16s[i]) << 16)
}
return f32s
}