weighttransfer

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package weighttransfer transfers independently pinned safetensors weights.

This package is outside the compatibility promise of github.com/tmc/lib/nvidia. Its API may change or be removed in patch releases.

Package weighttransfer transfers independently pinned safetensors weights.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Checkpoint

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

Checkpoint holds verified checkpoint shards open. Construct it with Open.

func Open

func Open(dir string, index []byte, indexSHA [sha256.Size]byte, shardSHA map[string][sha256.Size]byte) (*Checkpoint, error)

Open checks the supplied index bytes and opens every shard under its expected SHA-256 digest. Obtain both pins from a trusted manifest. Shards must be immutable until Close.

func (*Checkpoint) Close

func (c *Checkpoint) Close() error

Close releases the retained shard descriptors. It must not race Transfer.

func (*Checkpoint) Transfer

func (c *Checkpoint) Transfer(ctx context.Context, dst io.Writer, w Weight) (n int64, err error)

Transfer writes a bound weight to dst in little-endian order. It checks every source binding before writing and uses bounded scratch space. On an I/O error or cancellation, n counts the bytes already written. The caller must discard a partially populated destination on error.

func (*Checkpoint) Upload

func (c *Checkpoint) Upload(ctx context.Context, dst cuda.Slice[byte], stream *cuda.Stream, w Weight) (n int64, cleanup *UploadCleanup, err error)

Upload transfers a weight into an exact-length CUDA byte slice using one pinned staging allocation. Each copy completes before staging is reused. The stream must be live and outside graph capture. The caller owns dst and stream and must serialize their use and destruction with the upload. On error discard the destination; n counts only chunks confirmed by the transfer wait. Cancellation stops copying new chunks, then cleanup deliberately waits without cancellation to drain submitted work. Retain any returned cleanup handle and retry Close.

Example
var c Checkpoint
var dst cuda.Slice[byte]
_, _, err := c.Upload(context.Background(), dst, nil, Weight{})
fmt.Println(err)
Output:
weighttransfer: missing CUDA stream or destination

func (*Checkpoint) ValidateWeight

func (c *Checkpoint) ValidateWeight(w Weight) (int64, error)

ValidateWeight checks a weight's exact metadata against the open verified shards and returns its decoded byte length. It does not read tensor payloads, decode values or allocate a destination.

Example
var verified *Checkpoint
// An unopened checkpoint cannot validate a weight description.
_, err := verified.ValidateWeight(Weight{})
fmt.Println(err)
Output:
weighttransfer: missing checkpoint

type Tensor

type Tensor struct {
	Name, Shard string
	DType       safetensors.Dtype
	Shape       []int
	Offset      int64
	Size        int64
}

Tensor describes an exact tensor extent from the reviewed checkpoint inventory. Offset is relative to the shard's data section.

type UploadCleanup

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

UploadCleanup retains the pinned staging buffer and any pending copy when Upload could not confirm cleanup. Retry Close before releasing the stream or destination. Its methods must be called serially.

func UploadConverted

func UploadConverted(ctx context.Context, dst []cuda.Slice[byte], stream *cuda.Stream, convert func([]io.Writer) error) (*UploadCleanup, error)

UploadConverted copies converted weights into exact-length disjoint CUDA byte slices using one bounded pinned staging allocation. convert receives equally ordered writers. It may interleave writes serially, and must not use the writers after returning. It must return errors normally so this call can drain pending copies and report any cleanup owner. It must not panic or terminate the goroutine. Each successful destination must have exactly its declared length written.

The caller owns dst and stream. The stream must be live, outside graph capture and serialized against external work for the duration of this call and any cleanup retry. On any error discard the destinations. Pending copies are drained without cancellation. If cleanup fails, retain the returned owner and retry Close before releasing the stream or destinations.

Example
_, err := UploadConverted(context.Background(), []cuda.Slice[byte]{}, nil, nil)
fmt.Println(err)
Output:
weighttransfer: missing context, CUDA stream, destination, or converter

func (*UploadCleanup) Close

func (u *UploadCleanup) Close() error

Close drains the pending copy before freeing staging. An error leaves the unconfirmed resources owned by this handle. A successful close is final.

Example
var owner *UploadCleanup
fmt.Println(owner.Close())
Output:
<nil>

type Weight

type Weight struct {
	Encoding         string
	DestinationDType safetensors.Dtype
	DestinationShape []int
	Tensors          []Tensor
}

Weight describes one destination buffer and the reviewed source tensors in transfer order. For one tensor the shape must match DestinationShape. Several tensors must provide all slices of the first axis in order. Encoding is one of "bf16", "f32", "widen_bf16", or "widen_f16".

Jump to

Keyboard shortcuts

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