PTWM
Guides

Transformers

Patch `transformers.modeling_utils.load_state_dict` to transparently decompress `.ptwm` payloads.

For HuggingFace models stored as safetensors, the safetensors patcher is enough — it intercepts loading at the safe_open boundary.

For non-safetensors HuggingFace checkpoints, use patch_transformers() instead. It patches transformers.modeling_utils.load_state_dict so PyTorch-pickle and sharded HuggingFace loaders both transparently decompress sibling .ptwm files.

Default behaviour

from ptwm import patch_transformers
patch_transformers()

from transformers import AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained("path/to/model")

PTWM decompresses a sibling .ptwm shard in memory and leaves the on-disk file untouched.

Replace local files

To cache the decompressed result on disk for future loads:

patch_transformers(replace_local_file=True)

This deletes the .ptwm shard after a successful decompression and writes the canonical .safetensors / .bin in its place. Useful for one-time decompression on a workstation; unhelpful in CI, where the smaller artefact should stay on disk.

Compatibility

Tested against transformers >= 4.45. The patch intercepts only load_state_dict; nothing else in the HuggingFace stack changes.

Combining patchers

patch_safetensors() and patch_transformers() are independent; call them together as needed. The safetensors patcher handles the safe_open path; the transformers patcher catches everything else.

from ptwm import patch_safetensors, patch_transformers
patch_safetensors()
patch_transformers()