No project description provided
Project description
nshutils
nshutils is a collection of utility functions and classes that I've found useful in my day-to-day work as an ML researcher. This library includes utilities for typechecking, logging, and saving/loading activations from neural networks.
Installation
To install nshutils, simply run:
pip install nshutils
Features
Typechecking
nshutils provides a simple way to typecheck your code using the jaxtyping library. Simply call typecheck_this_module() at the top of your module (i.e., in the root __init__.py file) to enable typechecking for the entire module:
from nshutils.typecheck import typecheck_this_module
typecheck_this_module()
You can also use the tassert function to assert that a value is of a certain type:
import nshutils.typecheck as tc
def my_function(x: tc.Float[torch.Tensor, "bsz seq len"]) -> tc.Float[torch.Tensor, "bsz seq len"]:
tc.tassert(tc.Float[torch.Tensor, "bsz seq len"], x)
...
Logging
nshutils provides a simple way to configure logging for your project. Simply call one of the logging setup functions:
from nshutils.logging import init_python_logging
init_python_logging()
This will configure logging to use pretty formatting for PyTorch tensors and numpy arrays (inspired by and/or utilizing lovely-numpy and lovely-tensors), and will also enable rich logging if the rich library is installed.
Activation Saving/Loading
nshutils provides a simple way to save and load activations from neural networks. To save activations, use the ActSave object:
from nshutils import ActSave
def my_model_forward(x):
...
# Save activations to "{save_dir}/encoder.activations/{idx}.npy"
ActSave({"encoder.activations": x})
# Equivalent to the above
with ActSave.context("encoder"):
ActSave(activations=x)
...
ActSave.enable(save_dir="path/to/activations")
x = torch.randn(...)
my_model_forward(x)
# Activations are saved to disk under the "path/to/activations" directory
This will save the x tensor to disk under the encoder prefix.
To load activations, use the ActLoad class:
from nshutils import ActLoad
act_load = ActLoad.from_latest_version("path/to/activations")
encoder_acts = act_load["encoder"]
for act in encoder_acts:
print(act.shape)
This will load all of the activations saved under the encoder prefix.
Other Utilities
nshutils also provides a few other utility functions/classes:
snoop: A simple way to debug your code using thepysnooperlibrary, based on thetorchsnooperlibrary.apply_to_collection: Recursively apply a function to all elements of a collection that match a certain type, taken from thepytorch-lightninglibrary.
Contributing
Contributions to nshutils are welcome! Please open an issue or submit a pull request on the GitHub repository.
License
nshutils is released under the MIT License. See the LICENSE file for more details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
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
File details
Details for the file nshutils-0.37.0b2.tar.gz.
File metadata
- Download URL: nshutils-0.37.0b2.tar.gz
- Upload date:
- Size: 58.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.12.3 Linux/6.6.87.2-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aee536a3f9841e639bf84f39e03216eeba8fd2c9fccd13a320a052c04b20a447
|
|
| MD5 |
53e98ede27132f358c1ee6676b721515
|
|
| BLAKE2b-256 |
d65fb646b57bc45edb12aea8de3baca01778d3f9c31c16239ef2a8bd13e2278e
|
File details
Details for the file nshutils-0.37.0b2-py3-none-any.whl.
File metadata
- Download URL: nshutils-0.37.0b2-py3-none-any.whl
- Upload date:
- Size: 71.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.12.3 Linux/6.6.87.2-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
da156668cc9f4ea13a3be31a2848266be942689f7618faf17f05eed696461b1d
|
|
| MD5 |
4b45ff794cdd43275412a3a1883afa71
|
|
| BLAKE2b-256 |
e9c7333e1a0d1823c9640e53559427a37589579726a44b65b312a5faa418a825
|