mirror of
https://github.com/ollama/ollama.git
synced 2026-04-23 17:29:54 +02:00
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:
21
convert/bfloat16/bfloat16.go
Normal file
21
convert/bfloat16/bfloat16.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user