Skip to main content

ptdeco is a library implementing decomposition algorithms for model optimization.

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ptdeco-0.5.0.tar.gz (29.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ptdeco-0.5.0-py3-none-any.whl (29.6 kB view details)

Uploaded Python 3

File details

Details for the file ptdeco-0.5.0.tar.gz.

File metadata

  • Download URL: ptdeco-0.5.0.tar.gz
  • Upload date:
  • Size: 29.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.10.6

File hashes

Hashes for ptdeco-0.5.0.tar.gz
Algorithm Hash digest
SHA256 664d77740c147c0d4a6c7e22e1d465adc6ea025ade1f88f970a3459625bdc979
MD5 9ba3fee6057f7508dc6a1de7e442ec95
BLAKE2b-256 921ab2319651e6b74c5beae98d5d39d3e0fcd530117cd69f524fb68eb89f8fa5

See more details on using hashes here.

File details

Details for the file ptdeco-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: ptdeco-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 29.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.10.6

File hashes

Hashes for ptdeco-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 842fe38ae3cc0ef2b33a7a556257beffdb5e783a771d15697145cde35d588886
MD5 a4a0323c6fc99cadcfaaa026d7f293cf
BLAKE2b-256 9c90240cd14c11e02c4ab57d8d82a286ca5ddd45f1d34003af0ce9c16408abe9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page