lit-wsl
My personal library of reusable Pytorch Lightning components
Features
Installation
With pip:
python -m pip install lit-wsl
With uv:
uv add lit-wsl
How to use it
IntermediateLayerGetter
Capture intermediate layer outputs during forward pass:
import torch
from torchvision.models import resnet18
from lit_wsl.models.intermediate_layer_getter import IntermediateLayerGetter
model = resnet18(pretrained=True)
# Specify which layers to capture: {layer_name: output_name}
return_layers = {"layer2": "feat1", "layer4": "feat2"}
layer_getter = IntermediateLayerGetter(model, return_layers, keep_output=True)
x = torch.randn(1, 3, 224, 224)
intermediate_outputs, final_output = layer_getter(x)
# intermediate_outputs is OrderedDict with keys "feat1" and "feat2"
print(intermediate_outputs["feat1"].shape) # torch.Size([1, 128, 28, 28])
WeightRenamer
Rename keys in checkpoint files:
from lit_wsl.models.weight_renamer import WeightRenamer
# Load checkpoint
renamer = WeightRenamer("old_model.pth")
# Remove common prefix
renamer.remove_prefix("model.")
# Rename specific keys
renamer.rename_keys({
"backbone.conv1": "encoder.conv1",
"head.fc": "classifier.fc"
})
# Save modified checkpoint
renamer.save("renamed_model.pth")
WeightMapper
Automatically map weights between different model architectures:
import torch
from lit_wsl.mapper.weight_mapper import WeightMapper
from lit_wsl.models.weight_renamer import WeightRenamer
# Define your models (with different layer names)
old_model = OldModelArchitecture()
new_model = NewModelArchitecture()
# Analyze and suggest mapping
mapper = WeightMapper(old_model, new_model)
mapping, unmatched = mapper.suggest_mapping(threshold=0.6)
# Apply mapping to checkpoint
renamer = WeightRenamer("old_weights.pth")
renamer.rename_keys(mapping)
renamer.save("adapted_weights.pth")
# Load adapted weights
new_model.load_state_dict(torch.load("adapted_weights.pth"))
Docs
uv run mkdocs build -f ./mkdocs.yml -d ./_build/
Update template
copier update --trust -A --vcs-ref=HEAD
Credits
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
lit_wsl-0.3.4.tar.gz
(54.4 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
lit_wsl-0.3.4-py3-none-any.whl
(61.8 kB
view details)
File details
Details for the file lit_wsl-0.3.4.tar.gz.
File metadata
- Download URL: lit_wsl-0.3.4.tar.gz
- Upload date:
- Size: 54.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
uv/0.11.31 {"installer":{"name":"uv","version":"0.11.31","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
303c00748b54d22ebbb1fee0d917d74bf7d506fe7b8382ad2ae326d95dee150e
|
|
| MD5 |
99ca0bdbe55fb4ecbb909bd8f6d5cf5d
|
|
| BLAKE2b-256 |
c238853327eaba1dfe6df493926d44562861b68b0b636147c7c00944b6e0f30a
|
File details
Details for the file lit_wsl-0.3.4-py3-none-any.whl.
File metadata
- Download URL: lit_wsl-0.3.4-py3-none-any.whl
- Upload date:
- Size: 61.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
uv/0.11.31 {"installer":{"name":"uv","version":"0.11.31","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c85f4708619f1cf87becfac4d94f117f00de9b4f98be2994096ba954dcbe12d6
|
|
| MD5 |
e5197573feaf386fbe76cc36a8c4bfa6
|
|
| BLAKE2b-256 |
f5791214dfd7d0cfc0df21e8067c97038f6ab436833ce26818a5ff43ec210398
|