PTWM
ptwm

ptwm.delta

Delta (reference-frame) compression primitives.

ptwm.delta

Delta (reference-frame) compression primitives.

pymodule

ptwm.delta

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

Delta (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.
pyfunction

ptwm.delta.decode

python/ptwm/delta/__init__.py:55
def decode(residual: bytes | memoryview, reference: bytes | memoryview, mode: DeltaMode = DeltaMode.XOR, expected_reference_hash: bytes | None = None) -> bytes

Reconstruct 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.
pyclass

ptwm.delta.DeltaMode

python/ptwm/delta/__init__.py:30
class DeltaMode(StrEnum):

Supported delta schemes.

pyconstant

ptwm.delta.DeltaMode.FLOAT

python/ptwm/delta/__init__.py:34
FLOAT = 'float'
pyconstant

ptwm.delta.DeltaMode.XOR

python/ptwm/delta/__init__.py:33
XOR = 'xor'
pyfunction

ptwm.delta.encode

python/ptwm/delta/__init__.py:37
def encode(data: bytes | memoryview, reference: bytes | memoryview, mode: DeltaMode = DeltaMode.XOR) -> bytes

Compute 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`.
pyfunction

ptwm.delta.reference_hash

python/ptwm/delta/__init__.py:83
def reference_hash(reference: bytes | memoryview) -> bytes

BLAKE3 digest (32 bytes) of ``reference``. Deterministic.