ptwm
ptwm.delta
Delta (reference-frame) compression primitives.
ptwm.delta
Delta (reference-frame) compression primitives.
ptwm.delta
python/ptwm/delta/__init__.py:1module ptwm.deltaDelta (reference-frame) compression primitives.
A delta-compressed tensor stores the residual between the tensor bytes and a
reference (e.g. the matching tensor from a base model checkpoint). For
fine-tuned models the residual concentrates on very few bits and compresses
far better than the raw tensor.
This module exposes the delta math only. To obtain a compressed blob,
compose with :mod:`weights.codecs`::
residual = ptwm.delta.encode(finetune_bytes, reference=base_bytes)
blob = weights.codecs.get_codec("huffman").encode(residual)
# inverse:
residual = weights.codecs.get_codec("huffman").decode(blob, original_len)
recovered = ptwm.delta.decode(residual, reference=base_bytes)
Use :func:`reference_hash` to record which reference a residual was produced
against; :func:`decode` accepts an optional ``expected_reference_hash`` for
cross-checking at call time.
ptwm.delta.decode
python/ptwm/delta/__init__.py:55def decode(residual: bytes | memoryview, reference: bytes | memoryview, mode: DeltaMode = DeltaMode.XOR, expected_reference_hash: bytes | None = None) -> bytesReconstruct the original bytes from ``residual`` and ``reference``.
If ``expected_reference_hash`` is given, the BLAKE3 digest of
``reference`` is checked against it before reconstruction, guarding
against silently decoding with the wrong reference.
ptwm.delta.DeltaMode
python/ptwm/delta/__init__.py:30class DeltaMode(StrEnum):Supported delta schemes.
ptwm.delta.DeltaMode.FLOAT
python/ptwm/delta/__init__.py:34FLOAT = 'float'ptwm.delta.DeltaMode.XOR
python/ptwm/delta/__init__.py:33XOR = 'xor'ptwm.delta.encode
python/ptwm/delta/__init__.py:37def encode(data: bytes | memoryview, reference: bytes | memoryview, mode: DeltaMode = DeltaMode.XOR) -> bytesCompute the delta residual between ``data`` and ``reference``.
``data`` and ``reference`` must have identical byte length. Returns a
buffer of the same length; the inverse is :func:`decode`.
ptwm.delta.reference_hash
python/ptwm/delta/__init__.py:83def reference_hash(reference: bytes | memoryview) -> bytesBLAKE3 digest (32 bytes) of ``reference``. Deterministic.