PTWM
ptwm

ptwm.utils

Utility functions for PTWM.

ptwm.utils

Utility functions for PTWM.

pymodule

ptwm.utils

python/ptwm/utils/__init__.py:1
module ptwm.utils

Utility functions for PTWM.

pyconstant

ptwm.utils.COMPRESSED_DTYPE

python/ptwm/utils/_safetensors.py:12
COMPRESSED_DTYPE = torch.uint8
pyconstant

ptwm.utils.COMPRESSION_METHOD

python/ptwm/utils/_safetensors.py:11
COMPRESSION_METHOD = 'HUFFMAN'
pyfunction

ptwm.utils.decode_ptwm_tensor

python/ptwm/utils/_decode.py:77
def decode_ptwm_tensor(blob: bytes, name: str) -> torch.Tensor

Decode a single named tensor from a ``.ptwm`` container blob.

Falls back to a flat ``uint8`` tensor (with a warning) when the container has no shape metadata for ``name``.
pyfunction

ptwm.utils.divide_int

python/ptwm/utils/_torch.py:33
def divide_int(tensor: torch.Tensor, divisor: float) -> torch.Tensor

Inverse of :func:`multiply_if_max_below`: cast to float32 and divide.

pyclass

ptwm.utils.DType

python/ptwm/utils/_torch.py:128
class DType(Enum):

Enum for torch weights dtypes.

pymethod

ptwm.utils.DType.__init__

python/ptwm/utils/_torch.py:177
def __init__(self, torch_dtype: torch.dtype | None, numpy_dtype: type | None, python_dtype: type | None, dtype_str: str, code: int) -> None
pyconstant

ptwm.utils.DType.BFLOAT16

python/ptwm/utils/_torch.py:137
BFLOAT16 = (torch.bfloat16, None, None, 'bfloat16', 6)
pyconstant

ptwm.utils.DType.BOOL

python/ptwm/utils/_torch.py:158
BOOL = (torch.bool, np.bool_, bool, 'bool', 24)
pyconstant

ptwm.utils.DType.CDOUBLE

python/ptwm/utils/_torch.py:143
CDOUBLE = (torch.cdouble, np.complex128, complex, 'cdouble', 12)
pyconstant

ptwm.utils.DType.CFLOAT

python/ptwm/utils/_torch.py:141
CFLOAT = (torch.cfloat, np.complex64, complex, 'cfloat', 10)
pyconstant

ptwm.utils.DType.CHALF

python/ptwm/utils/_torch.py:139
CHALF = (torch.complex32, None, None, 'chalf', 8)
pyattribute

ptwm.utils.DType.code

python/ptwm/utils/_torch.py:189
code = code
pyconstant

ptwm.utils.DType.COMPLEX128

python/ptwm/utils/_torch.py:142
COMPLEX128 = (torch.complex128, np.complex128, complex, 'complex128', 11)
pyconstant

ptwm.utils.DType.COMPLEX32

python/ptwm/utils/_torch.py:138
COMPLEX32 = (torch.complex32, None, None, 'complex32', 7)
pyconstant

ptwm.utils.DType.COMPLEX64

python/ptwm/utils/_torch.py:140
COMPLEX64 = (torch.complex64, np.complex64, complex, 'complex64', 9)
pyattribute

ptwm.utils.DType.dtype_str

python/ptwm/utils/_torch.py:188
dtype_str = dtype_str
pyconstant

ptwm.utils.DType.FLOAT

python/ptwm/utils/_torch.py:133
FLOAT = (torch.float, np.float32, float, 'float', 2)
pyconstant

ptwm.utils.DType.FLOAT16

python/ptwm/utils/_torch.py:135
FLOAT16 = (torch.float16, np.float16, None, 'float16', 4)
pyconstant

ptwm.utils.DType.FLOAT32

python/ptwm/utils/_torch.py:132
FLOAT32 = (torch.float32, np.float32, float, 'float32', 1)
pyconstant

ptwm.utils.DType.FLOAT4_E2M1FN_X2

python/ptwm/utils/_torch.py:169
FLOAT4_E2M1FN_X2 = (getattr(torch, 'float4_e2m1fn_x2', None), None, None, 'float4_e2m1fn_x2', 31)
pyconstant

ptwm.utils.DType.FLOAT64

python/ptwm/utils/_torch.py:134
FLOAT64 = (torch.float64, np.float64, float, 'float64', 3)
pyconstant

ptwm.utils.DType.FLOAT8_E4M3FN

python/ptwm/utils/_torch.py:163
FLOAT8_E4M3FN = (torch.float8_e4m3fn, None, None, 'float8_e4m3fn', 29)
pyconstant

ptwm.utils.DType.FLOAT8_E5M2

python/ptwm/utils/_torch.py:164
FLOAT8_E5M2 = (torch.float8_e5m2, None, None, 'float8_e5m2', 30)
pymethod

ptwm.utils.DType.from_code

python/ptwm/utils/_torch.py:234
def from_code(cls, code: int) -> str

Get the enum member name from a code.

pymethod

ptwm.utils.DType.from_dtype

python/ptwm/utils/_torch.py:191
def from_dtype(cls, dtype: Any) -> DType

Return the enum member matching ``dtype``.

Accepts ``torch.dtype``, ``numpy.dtype``, Python type, or canonical name string. For numpy 2.x / ml_dtypes extension dtypes (BF16, FP8, packed FP4 — entries with ``numpy_dtype is None`` because no first- class numpy alias exists), a fallback comparison against ``dtype.name`` recovers the right member.
pyconstant

ptwm.utils.DType.HALF

python/ptwm/utils/_torch.py:136
HALF = (torch.half, np.float16, None, 'half', 5)
pyconstant

ptwm.utils.DType.INT

python/ptwm/utils/_torch.py:155
INT = (torch.int32, np.int32, int, 'int', 21)
pyconstant

ptwm.utils.DType.INT16

python/ptwm/utils/_torch.py:152
INT16 = (torch.int16, np.int16, None, 'int16', 18)
pyconstant

ptwm.utils.DType.INT32

python/ptwm/utils/_torch.py:154
INT32 = (torch.int32, np.int32, int, 'int32', 20)
pyconstant

ptwm.utils.DType.INT64

python/ptwm/utils/_torch.py:156
INT64 = (torch.int64, np.int64, int, 'int64', 22)
pyconstant

ptwm.utils.DType.INT8

python/ptwm/utils/_torch.py:151
INT8 = (torch.int8, np.int8, None, 'int8', 17)
pyconstant

ptwm.utils.DType.LONG

python/ptwm/utils/_torch.py:157
LONG = (torch.int64, np.int64, int, 'long', 23)
pyconstant

ptwm.utils.DType.NONE

python/ptwm/utils/_torch.py:131
NONE = (None, None, None, 'none', 0)
pyattribute

ptwm.utils.DType.numpy_dtype

python/ptwm/utils/_torch.py:186
numpy_dtype = numpy_dtype
pyattribute

ptwm.utils.DType.python_dtype

python/ptwm/utils/_torch.py:187
python_dtype = python_dtype
pyconstant

ptwm.utils.DType.QINT32

python/ptwm/utils/_torch.py:161
QINT32 = (torch.qint32, None, None, 'qint32', 27)
pyconstant

ptwm.utils.DType.QINT8

python/ptwm/utils/_torch.py:160
QINT8 = (torch.qint8, None, None, 'qint8', 26)
pyconstant

ptwm.utils.DType.QUINT4X2

python/ptwm/utils/_torch.py:162
QUINT4X2 = (torch.quint4x2, None, None, 'quint4x2', 28)
pyconstant

ptwm.utils.DType.QUINT8

python/ptwm/utils/_torch.py:159
QUINT8 = (torch.quint8, None, None, 'quint8', 25)
pyconstant

ptwm.utils.DType.SHORT

python/ptwm/utils/_torch.py:153
SHORT = (torch.int16, np.int16, None, 'short', 19)
pyattribute

ptwm.utils.DType.torch_dtype

python/ptwm/utils/_torch.py:185
torch_dtype = torch_dtype
pyconstant

ptwm.utils.DType.UINT16

python/ptwm/utils/_torch.py:146
UINT16 = (None, np.uint16, None, 'uint16', 14)
pyconstant

ptwm.utils.DType.UINT32

python/ptwm/utils/_torch.py:148
UINT32 = (None, np.uint32, None, 'uint32', 15)
pyconstant

ptwm.utils.DType.UINT64

python/ptwm/utils/_torch.py:150
UINT64 = (None, np.uint64, None, 'uint64', 16)
pyconstant

ptwm.utils.DType.UINT8

python/ptwm/utils/_torch.py:144
UINT8 = (torch.uint8, np.uint8, None, 'uint8', 13)
pyfunction

ptwm.utils.get_compressed_tensors_metadata

python/ptwm/utils/_safetensors.py:47
def get_compressed_tensors_metadata(metadata: dict[str, str]) -> dict[str, CompressedTensorInfo]

Retrieve file-level metadata on all compressed tensors.

pyfunction

ptwm.utils.get_dtype_bits

python/ptwm/utils/_torch.py:39
def get_dtype_bits(dtype: torch.dtype) -> tuple[int, torch.dtype]

Return ``(bit_width, matching_int_dtype)`` for a floating-point dtype.

pyfunction

ptwm.utils.is_floating_point

python/ptwm/utils/_torch.py:108
def is_floating_point(data_format_value: int, data: Any, bytearray_dtype: str | None) -> bool | None

Check if the data is floating point.

pyfunction

ptwm.utils.multi_process_patcher

python/ptwm/utils/_patch.py:10
def multi_process_patcher(patch_func: Callable[..., Any]) -> None

Run patch_func on this process and on every subsequent child process.

pyfunction

ptwm.utils.multiply_if_max_below

python/ptwm/utils/_torch.py:15
def multiply_if_max_below(tensor: torch.Tensor, max_val: float, multiplier: float, dtype: torch.dtype) -> tuple[torch.Tensor, bool]

Scale ``tensor`` into integer-friendly range for lossy compression.

If ``tensor.abs().amax() < max_val``, returns ``(tensor * multiplier).to(dtype)`` paired with ``True``; otherwise returns the input unchanged with ``False``.
pyfunction

ptwm.utils.pack_shape

python/ptwm/utils/_torch.py:51
def pack_shape(shape: torch.Size | list[int] | tuple[int, ...]) -> bytes

Pack tensor dimensions into bytes with per-dim size indicators.

Layout: one byte for the dim count, then ``(size_indicator, value)`` pairs where the indicator (1/2/4/8) is the byte width of the following value.
pyfunction

ptwm.utils.raw_to_tensor

python/ptwm/utils/_decode.py:22
def raw_to_tensor(raw: bytes, shape: list[int] | tuple[int, ...] | None, dtype_name: str) -> torch.Tensor

Reinterpret ``raw`` bytes as a tensor of ``dtype_name`` and ``shape``.

Recovers the original torch dtype from the canonical ``dtype_name``. Dtypes that ``torch.frombuffer`` cannot ingest directly (BF16, FP8, packed FP4) are materialised through an unsigned-integer view of matching byte width. Falls back to a flat ``uint8`` view (with a warning) when the dtype is unknown.
pyfunction

ptwm.utils.unpack_shape

python/ptwm/utils/_torch.py:77
def unpack_shape(packed_data: bytes) -> tuple[tuple[int, ...], int]

Inverse of :func:`pack_shape`. Returns ``(dims, bytes_consumed)``.