ptwm.utils
ptwm.utils
Utility functions for PTWM.
ptwm.utils
python/ptwm/utils/__init__.py:1module ptwm.utilsUtility functions for PTWM.
ptwm.utils.COMPRESSED_DTYPE
python/ptwm/utils/_safetensors.py:12COMPRESSED_DTYPE = torch.uint8ptwm.utils.COMPRESSION_METHOD
python/ptwm/utils/_safetensors.py:11COMPRESSION_METHOD = 'HUFFMAN'ptwm.utils.decode_ptwm_tensor
python/ptwm/utils/_decode.py:77def decode_ptwm_tensor(blob: bytes, name: str) -> torch.TensorDecode a single named tensor from a ``.ptwm`` container blob.
ptwm.utils.divide_int
python/ptwm/utils/_torch.py:33def divide_int(tensor: torch.Tensor, divisor: float) -> torch.TensorInverse of :func:`multiply_if_max_below`: cast to float32 and divide.
ptwm.utils.DType
python/ptwm/utils/_torch.py:128class DType(Enum):Enum for torch weights dtypes.
ptwm.utils.DType.__init__
python/ptwm/utils/_torch.py:177def __init__(self, torch_dtype: torch.dtype | None, numpy_dtype: type | None, python_dtype: type | None, dtype_str: str, code: int) -> Noneptwm.utils.DType.BFLOAT16
python/ptwm/utils/_torch.py:137BFLOAT16 = (torch.bfloat16, None, None, 'bfloat16', 6)ptwm.utils.DType.BOOL
python/ptwm/utils/_torch.py:158BOOL = (torch.bool, np.bool_, bool, 'bool', 24)ptwm.utils.DType.CDOUBLE
python/ptwm/utils/_torch.py:143CDOUBLE = (torch.cdouble, np.complex128, complex, 'cdouble', 12)ptwm.utils.DType.CFLOAT
python/ptwm/utils/_torch.py:141CFLOAT = (torch.cfloat, np.complex64, complex, 'cfloat', 10)ptwm.utils.DType.CHALF
python/ptwm/utils/_torch.py:139CHALF = (torch.complex32, None, None, 'chalf', 8)ptwm.utils.DType.code
python/ptwm/utils/_torch.py:189code = codeptwm.utils.DType.COMPLEX128
python/ptwm/utils/_torch.py:142COMPLEX128 = (torch.complex128, np.complex128, complex, 'complex128', 11)ptwm.utils.DType.COMPLEX32
python/ptwm/utils/_torch.py:138COMPLEX32 = (torch.complex32, None, None, 'complex32', 7)ptwm.utils.DType.COMPLEX64
python/ptwm/utils/_torch.py:140COMPLEX64 = (torch.complex64, np.complex64, complex, 'complex64', 9)ptwm.utils.DType.dtype_str
python/ptwm/utils/_torch.py:188dtype_str = dtype_strptwm.utils.DType.FLOAT
python/ptwm/utils/_torch.py:133FLOAT = (torch.float, np.float32, float, 'float', 2)ptwm.utils.DType.FLOAT16
python/ptwm/utils/_torch.py:135FLOAT16 = (torch.float16, np.float16, None, 'float16', 4)ptwm.utils.DType.FLOAT32
python/ptwm/utils/_torch.py:132FLOAT32 = (torch.float32, np.float32, float, 'float32', 1)ptwm.utils.DType.FLOAT4_E2M1FN_X2
python/ptwm/utils/_torch.py:169FLOAT4_E2M1FN_X2 = (getattr(torch, 'float4_e2m1fn_x2', None), None, None, 'float4_e2m1fn_x2', 31)ptwm.utils.DType.FLOAT64
python/ptwm/utils/_torch.py:134FLOAT64 = (torch.float64, np.float64, float, 'float64', 3)ptwm.utils.DType.FLOAT8_E4M3FN
python/ptwm/utils/_torch.py:163FLOAT8_E4M3FN = (torch.float8_e4m3fn, None, None, 'float8_e4m3fn', 29)ptwm.utils.DType.FLOAT8_E5M2
python/ptwm/utils/_torch.py:164FLOAT8_E5M2 = (torch.float8_e5m2, None, None, 'float8_e5m2', 30)ptwm.utils.DType.from_code
python/ptwm/utils/_torch.py:234def from_code(cls, code: int) -> strGet the enum member name from a code.
ptwm.utils.DType.from_dtype
python/ptwm/utils/_torch.py:191def from_dtype(cls, dtype: Any) -> DTypeReturn the enum member matching ``dtype``.
ptwm.utils.DType.HALF
python/ptwm/utils/_torch.py:136HALF = (torch.half, np.float16, None, 'half', 5)ptwm.utils.DType.INT
python/ptwm/utils/_torch.py:155INT = (torch.int32, np.int32, int, 'int', 21)ptwm.utils.DType.INT16
python/ptwm/utils/_torch.py:152INT16 = (torch.int16, np.int16, None, 'int16', 18)ptwm.utils.DType.INT32
python/ptwm/utils/_torch.py:154INT32 = (torch.int32, np.int32, int, 'int32', 20)ptwm.utils.DType.INT64
python/ptwm/utils/_torch.py:156INT64 = (torch.int64, np.int64, int, 'int64', 22)ptwm.utils.DType.INT8
python/ptwm/utils/_torch.py:151INT8 = (torch.int8, np.int8, None, 'int8', 17)ptwm.utils.DType.LONG
python/ptwm/utils/_torch.py:157LONG = (torch.int64, np.int64, int, 'long', 23)ptwm.utils.DType.NONE
python/ptwm/utils/_torch.py:131NONE = (None, None, None, 'none', 0)ptwm.utils.DType.numpy_dtype
python/ptwm/utils/_torch.py:186numpy_dtype = numpy_dtypeptwm.utils.DType.python_dtype
python/ptwm/utils/_torch.py:187python_dtype = python_dtypeptwm.utils.DType.QINT32
python/ptwm/utils/_torch.py:161QINT32 = (torch.qint32, None, None, 'qint32', 27)ptwm.utils.DType.QINT8
python/ptwm/utils/_torch.py:160QINT8 = (torch.qint8, None, None, 'qint8', 26)ptwm.utils.DType.QUINT4X2
python/ptwm/utils/_torch.py:162QUINT4X2 = (torch.quint4x2, None, None, 'quint4x2', 28)ptwm.utils.DType.QUINT8
python/ptwm/utils/_torch.py:159QUINT8 = (torch.quint8, None, None, 'quint8', 25)ptwm.utils.DType.SHORT
python/ptwm/utils/_torch.py:153SHORT = (torch.int16, np.int16, None, 'short', 19)ptwm.utils.DType.torch_dtype
python/ptwm/utils/_torch.py:185torch_dtype = torch_dtypeptwm.utils.DType.UINT16
python/ptwm/utils/_torch.py:146UINT16 = (None, np.uint16, None, 'uint16', 14)ptwm.utils.DType.UINT32
python/ptwm/utils/_torch.py:148UINT32 = (None, np.uint32, None, 'uint32', 15)ptwm.utils.DType.UINT64
python/ptwm/utils/_torch.py:150UINT64 = (None, np.uint64, None, 'uint64', 16)ptwm.utils.DType.UINT8
python/ptwm/utils/_torch.py:144UINT8 = (torch.uint8, np.uint8, None, 'uint8', 13)ptwm.utils.get_compressed_tensors_metadata
python/ptwm/utils/_safetensors.py:47def get_compressed_tensors_metadata(metadata: dict[str, str]) -> dict[str, CompressedTensorInfo]Retrieve file-level metadata on all compressed tensors.
ptwm.utils.get_dtype_bits
python/ptwm/utils/_torch.py:39def get_dtype_bits(dtype: torch.dtype) -> tuple[int, torch.dtype]Return ``(bit_width, matching_int_dtype)`` for a floating-point dtype.
ptwm.utils.is_floating_point
python/ptwm/utils/_torch.py:108def is_floating_point(data_format_value: int, data: Any, bytearray_dtype: str | None) -> bool | NoneCheck if the data is floating point.
ptwm.utils.multi_process_patcher
python/ptwm/utils/_patch.py:10def multi_process_patcher(patch_func: Callable[..., Any]) -> NoneRun patch_func on this process and on every subsequent child process.
ptwm.utils.multiply_if_max_below
python/ptwm/utils/_torch.py:15def 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.
ptwm.utils.pack_shape
python/ptwm/utils/_torch.py:51def pack_shape(shape: torch.Size | list[int] | tuple[int, ...]) -> bytesPack tensor dimensions into bytes with per-dim size indicators.
ptwm.utils.raw_to_tensor
python/ptwm/utils/_decode.py:22def raw_to_tensor(raw: bytes, shape: list[int] | tuple[int, ...] | None, dtype_name: str) -> torch.TensorReinterpret ``raw`` bytes as a tensor of ``dtype_name`` and ``shape``.
ptwm.utils.unpack_shape
python/ptwm/utils/_torch.py:77def unpack_shape(packed_data: bytes) -> tuple[tuple[int, ...], int]Inverse of :func:`pack_shape`. Returns ``(dims, bytes_consumed)``.