Skip to main content

license check test Code style: black Type checker: mypy

ptdeco

ptdeco is a library for model optimization by decomposition built on top of PyTorch.

Table of contents

Introduction

Currently, ptdeco implements the following methods:

  • lockd - method based on local knowledge distillation, tested on vision models (lockd = LOCal Knowledge Distillation)

  • falor - method based on low-rank decomposition of features inspired by Compressing Transformers: Features Are Low-Rank, but Weights Are Not! by Yu Hao, Wu Jianxin (2023), tested on vision models (falor = Features Are LOw Rank)

  • dwain - iterative method based on low-rank decomposition of features, tested on Large Language Models (dwain = Decomposing Weights Algorithm - an Iterative techNique)

lockd method requires short (~ 10 ImageNet epochs) knowledge distillation pretraining before decomposition is made. It can decompose linear layers and convolutions.

falor method does not require pretraining. Model decomposition lasts < 1 GPU hour (depending on model size and parameters). It can decompose linear layers and 1x1 convolutions.

dwain method does not require pretraining. It can decompose linear layers and 1x1 convolutions.

Installation

pip install ptdeco

Saving and loading a decomposed model

Saving a decomposed model

As a result of decomposition you get decompose_config dictionary. You need to serialize this e.g. to JSON. This will let you recreate the structure of a decomposed model. Except this, you need to save state_dict to recover the weights of a decomposed model. The code below illustrates the procedure:

import json
import pathlib

# Your decomposition code

output_path = pathlib.Path("YOUR/CHEKCPOINT/DIRECTORY")
out_decompose_config_path = output_path / "decompose_config.json"
with open(out_decompose_config_path, "wt") as f:
    json.dump(decompose_config, f)
out_decompose_state_dict_path = output_path / "decompose_state_dict.pt"
torch.save(model.state_dict(), out_decompose_state_dict_path)

Loading a decomposed model

To load the model, you need to recreate the original model first. Next, you load and apply the decompose_config. Finally, you load the state_dict (note the state dict "fits" the decomposed model, so you need to do it as a last step). The code below illustrates the procedure:

import json
import pathlib

import ptdeco

model = ... # Build original model
device = ...     # Specify the device original model uses

output_path = pathlib.Path("YOUR/CHEKCPOINT/DIRECTORY")

with open(output_path / "decompose_config.json", "rt") as f:
        decompose_config = json.load(f)

ptdeco.utils.apply_decompose_config_in_place(model, decompose_config)

sd = torch.load(output_path / "decompose_state_dict.pt")

model.load_state_dict(sd, map_location=device)

# Now `model` is decomposed and contains appropriate weights

Metadata

Release files for ptdeco 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ptdeco 0.5.0
File Size Uploaded Provenance Actions
ptdeco-0.5.0.tar.gz 29.8 kB Without provenance Details for ptdeco-0.5.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ptdeco 0.5.0
File Interpreter ABI Platform Actions
ptdeco-0.5.0-py3-none-any.whl Python 3 none any Details for ptdeco-0.5.0-py3-none-any.whl

Total release size: 59.4 kB

Release files / ptdeco-0.5.0.tar.gz

Download URL ptdeco-0.5.0.tar.gz
Size 29.8 kB
Tags Source
SHA-256 checksum
How to use checksums
664d77740c147c0d4a6c7e22e1d465adc6ea025ade1f88f970a3459625bdc979
BLAKE2b-256 checksum
How to use checksums
921ab2319651e6b74c5beae98d5d39d3e0fcd530117cd69f524fb68eb89f8fa5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.10.6

Release files / ptdeco-0.5.0-py3-none-any.whl

Download URL ptdeco-0.5.0-py3-none-any.whl
Size 29.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
842fe38ae3cc0ef2b33a7a556257beffdb5e783a771d15697145cde35d588886
BLAKE2b-256 checksum
How to use checksums
9c90240cd14c11e02c4ab57d8d82a286ca5ddd45f1d34003af0ce9c16408abe9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.10.6

Security for ptdeco 0.5.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page