Weights¶
tinax.weights owns tensor manifests and bounded Safetensors interchange. Loading inspects manifests and byte budgets before tensor materialization. Serialization accepts explicit host tensors and never silently gathers global JAX arrays.
See the Safetensors Interchange guide for task-oriented usage.
TensorManifest
dataclass
¶
TensorManifest(rules: tuple[TensorRule, ...])
Apply immutable rules with exact source coverage.
Attributes:
| Name | Type | Description |
|---|---|---|
rules |
tuple[TensorRule, ...]
|
Tuple of |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If two rules share a source name or collide on a destination name. |
apply
¶
Transform tensors after requiring exact source-name coverage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tensors
|
Mapping[str, NDArray[generic]]
|
Mapping of source name to host NumPy array. Its key set must exactly match the manifest's source names. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, NDArray[generic]]
|
A dict mapping each destination name to its transformed NumPy array. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If the source names do not exactly match, or a transform produces an unexpected shape. |
TensorRule
dataclass
¶
TensorRule(
source_name: str,
destination_name: str,
transform: Callable[
[NDArray[generic]], NDArray[generic]
],
expected_shape: tuple[int, ...],
expected_dtype: dtype[generic],
)
Describe one source-to-destination tensor transformation.
Attributes:
| Name | Type | Description |
|---|---|---|
source_name |
str
|
Non-empty source tensor name. |
destination_name |
str
|
Non-empty destination tensor name. |
transform |
Callable[[NDArray[generic]], NDArray[generic]]
|
Callable mapping the source NumPy array to the destination array. |
expected_shape |
tuple[int, ...]
|
Tuple of non-negative dimensions the transform must produce. |
expected_dtype |
dtype[generic]
|
NumPy dtype the transform must produce. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If a name is not a string, |
ValueError
|
If a name is empty or an |
inspect_safetensors
¶
inspect_safetensors(
path: str | PathLike[str],
*,
max_metadata_bytes: int = 0,
) -> SafetensorsInfo
Inspect tensor keys, shapes, dtypes, sizes, and bounded metadata without loading payloads.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike[str]
|
Path to the Safetensors file. |
required |
max_metadata_bytes
|
int
|
Maximum header metadata bytes to accept. |
0
|
Returns:
| Type | Description |
|---|---|
SafetensorsInfo
|
A |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
load_safetensors
¶
load_safetensors(
path: str | PathLike[str],
*,
max_bytes: int,
names: Iterable[str] | None = None,
max_metadata_bytes: int = 0,
) -> LoadedSafetensors
Inspect and selectively load NumPy tensors within an explicit nominal byte budget.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike[str]
|
Path to the Safetensors file. |
required |
max_bytes
|
int
|
Maximum total nominal payload bytes to load. |
required |
names
|
Iterable[str] | None
|
Tensor names to load, or |
None
|
max_metadata_bytes
|
int
|
Maximum header metadata bytes to accept. |
0
|
Returns:
| Type | Description |
|---|---|
LoadedSafetensors
|
A |
Raises:
| Type | Description |
|---|---|
TypeError
|
If a byte limit is not an integer, or a loaded tensor has an unmaterializable or unexpected dtype. |
ValueError
|
If a byte limit is negative, requested names are absent, the selection
exceeds |
save_safetensors
¶
save_safetensors(
path: str | PathLike[str],
tensors: Mapping[str, NDArray[generic]],
*,
overwrite: bool,
metadata: Mapping[str, str] | None = None,
max_metadata_bytes: int = 0,
) -> None
Atomically save explicit C-contiguous host tensors in the destination directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike[str]
|
Destination file path. Written atomically through a temporary file. |
required |
tensors
|
Mapping[str, NDArray[generic]]
|
Mapping of tensor name to host NumPy array to serialize. |
required |
overwrite
|
bool
|
Whether to replace an existing destination. |
required |
metadata
|
Mapping[str, str] | None
|
Optional string-to-string header metadata. |
None
|
max_metadata_bytes
|
int
|
Maximum metadata bytes to accept. |
0
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
SafetensorsInfo
dataclass
¶
SafetensorsInfo(
tensors: Mapping[str, TensorInfo],
metadata: Mapping[str, str],
)
Describe a validated Safetensors header and its bounded metadata.
Attributes:
| Name | Type | Description |
|---|---|---|
tensors |
Mapping[str, TensorInfo]
|
Read-only mapping of tensor name to |
metadata |
Mapping[str, str]
|
Read-only mapping of validated header metadata. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If a tensor name is invalid. |
TensorInfo
dataclass
¶
Describe one tensor without materializing its payload.
Attributes:
| Name | Type | Description |
|---|---|---|
shape |
tuple[int, ...]
|
Tuple of non-negative tensor dimensions. |
dtype |
str
|
Safetensors dtype string (for example, |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If a |
LoadedSafetensors
dataclass
¶
LoadedSafetensors(
tensors: Mapping[str, NDArray[generic]],
info: SafetensorsInfo,
)
Hold selected host tensors and the inspected file information.
Attributes:
| Name | Type | Description |
|---|---|---|
tensors |
Mapping[str, NDArray[generic]]
|
Read-only mapping of tensor name to host NumPy array. |
info |
SafetensorsInfo
|
The |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If a tensor name is invalid, absent from |