mirror of
https://github.com/dogkeeper886/ollama37.git
synced 2025-12-10 07:46:59 +00:00
* bf16 * tests * gpt-oss * enable gptoss for engine * rough estimate * convert to mxfp4 * handle safetensors U8 * clamp glu/linear * update tokenizer * MXFP4 support This implements the Open Compute Microscaling (MX) FP4 format as a tensor type with backend implementations focusing on mulmat and mulmatid on CPU, CUDA, and Metal. * Unit tests for MXFP4 support This exercises various operations and shapes on both CPU and GPU (if detected on the system) * cuda graph * unit test adjustments * cuda: optimize memory access Read 4 bytes at a time (8 elements) when performing mul_mat_vec_mxfp4 * mac: fix crash on old macos versions cblas_sgemm is only supported on v13.3 and up, however bf16 is only supported on v14+ so we were falling back to ggml-blas and crashing on bf16 tensors. Checking for the function being null seems to be the simplest way to condittionally avoid registering the backend. * server: Minimum context length for gptoss This model requires a minimum context length of 8192 to function effectively. Users can set higher values through all normal mechanisms but lower values will be silently reset. * ggml: Multiply by numParallel for gptoss sliding window When computing the graph size estimate, the context size is already multiplied by numParallel so estimates reflect that. However, since sliding window models use a smaller, fixed context size, they need to manually take numParallel into account. * gpt-oss integration includes harmony parser and thinking levels, etc. * fix sync * fix tests * fix lint --------- Co-authored-by: Daniel Hiltgen <daniel@ollama.com> Co-authored-by: Jesse Gross <jesse@ollama.com> Co-authored-by: Devon Rifkin <drifkin@drifkin.net>
193 lines
4.2 KiB
Go
193 lines
4.2 KiB
Go
package convert
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"io/fs"
|
|
"maps"
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/d4l3k/go-bfloat16"
|
|
"github.com/x448/float16"
|
|
)
|
|
|
|
type safetensorMetadata struct {
|
|
Type string `json:"dtype"`
|
|
Shape []uint64 `json:"shape"`
|
|
Offsets []int64 `json:"data_offsets"`
|
|
}
|
|
|
|
func parseSafetensors(fsys fs.FS, replacer *strings.Replacer, ps ...string) ([]Tensor, error) {
|
|
var ts []Tensor
|
|
for _, p := range ps {
|
|
f, err := fsys.Open(p)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer f.Close()
|
|
|
|
var n int64
|
|
if err := binary.Read(f, binary.LittleEndian, &n); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
b := bytes.NewBuffer(make([]byte, 0, n))
|
|
if _, err = io.CopyN(b, f, n); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var headers map[string]safetensorMetadata
|
|
if err := json.NewDecoder(b).Decode(&headers); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
keys := slices.Sorted(maps.Keys(headers))
|
|
|
|
names := make(map[string]struct{}, len(keys))
|
|
|
|
for _, key := range keys {
|
|
if value := headers[key]; value.Type != "" {
|
|
// bitsandbytes quantized models are unsupported
|
|
if len(value.Shape) == 0 {
|
|
return nil, errors.New("unsupported safetensors model")
|
|
}
|
|
ggufName := replacer.Replace(key)
|
|
if _, ok := names[ggufName]; ok {
|
|
return nil, fmt.Errorf("duplicate tensor name '%s' was found for this model", ggufName)
|
|
}
|
|
names[ggufName] = struct{}{}
|
|
ts = append(ts, safetensor{
|
|
fs: fsys,
|
|
path: p,
|
|
dtype: value.Type,
|
|
offset: safetensorsPad(n, value.Offsets[0]),
|
|
size: safetensorsPad(n, value.Offsets[1]) - safetensorsPad(n, value.Offsets[0]),
|
|
tensorBase: &tensorBase{
|
|
name: ggufName,
|
|
shape: value.Shape,
|
|
},
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
return ts, nil
|
|
}
|
|
|
|
// safetensorsPad returns the padded size of the safetensors file given a length n and offset s
|
|
func safetensorsPad(n, offset int64) int64 {
|
|
return 8 + n + offset
|
|
}
|
|
|
|
type safetensor struct {
|
|
fs fs.FS
|
|
path string
|
|
dtype string
|
|
offset int64
|
|
size int64
|
|
*tensorBase
|
|
}
|
|
|
|
func (st safetensor) Kind() uint32 {
|
|
kind := st.tensorBase.Kind()
|
|
if st.dtype == "BF16" && kind != tensorKindFP32 {
|
|
kind = tensorKindBF16
|
|
}
|
|
|
|
return kind
|
|
}
|
|
|
|
func (st safetensor) Clone() Tensor {
|
|
return &safetensor{
|
|
fs: st.fs,
|
|
path: st.path,
|
|
dtype: st.dtype,
|
|
offset: st.offset,
|
|
size: st.size,
|
|
tensorBase: &tensorBase{
|
|
name: st.name,
|
|
repacker: st.repacker,
|
|
shape: slices.Clone(st.shape),
|
|
},
|
|
}
|
|
}
|
|
|
|
func (st safetensor) WriteTo(w io.Writer) (int64, error) {
|
|
f, err := st.fs.Open(st.path)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
defer f.Close()
|
|
|
|
if seeker, ok := f.(io.Seeker); ok {
|
|
if _, err := seeker.Seek(st.offset, io.SeekStart); err != nil {
|
|
return 0, err
|
|
}
|
|
} else {
|
|
if _, err := io.CopyN(io.Discard, f, st.offset); err != nil {
|
|
return 0, err
|
|
}
|
|
}
|
|
|
|
var f32s []float32
|
|
switch st.dtype {
|
|
case "F32":
|
|
f32s = make([]float32, st.size/4)
|
|
if err = binary.Read(f, binary.LittleEndian, f32s); err != nil {
|
|
return 0, err
|
|
}
|
|
case "F16":
|
|
u16s := make([]uint16, st.size/2)
|
|
if err = binary.Read(f, binary.LittleEndian, u16s); err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
f32s = make([]float32, len(u16s))
|
|
for i := range u16s {
|
|
f32s[i] = float16.Frombits(u16s[i]).Float32()
|
|
}
|
|
|
|
case "BF16":
|
|
u8s := make([]uint8, st.size)
|
|
if err = binary.Read(f, binary.LittleEndian, u8s); err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
f32s = bfloat16.DecodeFloat32(u8s)
|
|
case "U8":
|
|
// U8 tensors do not support repacking or type conversion.
|
|
return io.CopyN(w, f, st.size)
|
|
default:
|
|
return 0, fmt.Errorf("unknown data type: %s", st.dtype)
|
|
}
|
|
|
|
if st.repacker != nil {
|
|
f32s, err = st.repacker(st.Name(), f32s, st.Shape())
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
}
|
|
|
|
switch st.Kind() {
|
|
case tensorKindFP32:
|
|
return 0, binary.Write(w, binary.LittleEndian, f32s)
|
|
case tensorKindFP16:
|
|
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())
|
|
}
|
|
}
|