safetensors

package
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2025 License: Apache-2.0, Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	QuantizationUnknown = "unknown"
	QuantizationMixed   = "mixed"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Header struct {
	Metadata map[string]interface{}
	Tensors  map[string]TensorInfo
}

Header represents the JSON header in a safetensors file

func ParseSafetensorsHeader

func ParseSafetensorsHeader(path string) (*Header, error)

ParseSafetensorsHeader reads only the header from a safetensors file without loading the entire file. This is memory-efficient for large model files (which can be many GB).

Safetensors format:

[8 bytes: header length (uint64, little-endian)]
[N bytes: JSON header]
[remaining: tensor data]

func (*Header) CalculateParameters

func (h *Header) CalculateParameters() int64

CalculateParameters sums up all tensor parameters

func (*Header) ExtractMetadata

func (h *Header) ExtractMetadata() map[string]string

ExtractMetadata converts header to string map (similar to GGUF)

func (*Header) GetQuantization

func (h *Header) GetQuantization() string

GetQuantization determines the quantization type from tensor dtypes

type Model

type Model struct {
	// contains filtered or unexported fields
}

func NewModel

func NewModel(paths []string) (*Model, error)

NewModel creates a new safetensors model from one or more safetensors files If a sharded model pattern is detected (e.g., model-00001-of-00002.safetensors), it will auto-discover all related shards

func (*Model) Config

func (m *Model) Config() (types.Config, error)

func (*Model) ConfigFile

func (m *Model) ConfigFile() (*v1.ConfigFile, error)

func (*Model) ConfigName

func (m *Model) ConfigName() (v1.Hash, error)

func (*Model) Descriptor

func (m *Model) Descriptor() (types.Descriptor, error)

func (*Model) Digest

func (m *Model) Digest() (v1.Hash, error)

func (*Model) ID

func (m *Model) ID() (string, error)

func (*Model) LayerByDiffID

func (m *Model) LayerByDiffID(hash v1.Hash) (v1.Layer, error)

func (*Model) LayerByDigest

func (m *Model) LayerByDigest(hash v1.Hash) (v1.Layer, error)

func (*Model) Layers

func (m *Model) Layers() ([]v1.Layer, error)

func (*Model) Manifest

func (m *Model) Manifest() (*v1.Manifest, error)

func (*Model) MediaType

func (m *Model) MediaType() (ggcr.MediaType, error)

func (*Model) RawConfigFile

func (m *Model) RawConfigFile() ([]byte, error)

func (*Model) RawManifest

func (m *Model) RawManifest() ([]byte, error)

func (*Model) Size

func (m *Model) Size() (int64, error)

type TensorInfo

type TensorInfo struct {
	Dtype       string
	Shape       []int64
	DataOffsets [2]int64
}

TensorInfo contains information about a tensor

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL