Lightning Thunder project.
Project description
Welcome to ⚡ Lightning Thunder
Lightning Thunder is a source-to-source compiler for PyTorch.
It makes PyTorch programs faster both on single accelerators or in distributed settings.
Thunder aims to be usable, understandable, and extensible.
Performance
Thunder can achieve significant speedups over standard PyTorch eager code, through the compounding effects of optimizations and the use of best in class executors. Here is an example of the pretraining throughput for Llama 2 7B as implemented in LitGPT.
We achieve a 40% speedup in training throughput compared to eager code on H100 using a combination of executors including nvFuser, torch.compile, cuDNN, and TransformerEngine FP8.
Thunder supports distributed strategies like DDP and FSDP (ZeRO2 and ZeRO3). Here is the normalized throughput measured for Llama 2 7B (this time without FP8 mixed precision, support for FSDP is underway).
NOTE: Lightning Thunder is alpha. Feel free to get involved, expect a few bumps along the way.
Start with Thunder
Try Thunder without installing by using our Zero to Thunder Tutorial Studio.
Install Thunder
Install nvFuser nightly, which will also install the matching PyTorch nightly:
pip install --pre 'nvfuser-cu121[torch]' --extra-index-url https://pypi.nvidia.com
Install Thunder:
pip install git+https://github.com/Lightning-AI/lightning-thunder.git
or install from the local repo:
pip install .
Hello World
Here is a simple example of how Thunder lets you compile and run PyTorch code:
import torch
import thunder
def foo(a, b):
return a + b
jfoo = thunder.jit(foo)
a = torch.full((2, 2), 1)
b = torch.full((2, 2), 3)
result = jfoo(a, b)
print(result)
# prints
# tensor(
# [[4, 4]
# [4, 4]])
The compiled function jfoo
takes and returns PyTorch tensors, just like the original function, so modules and functions compiled by Thunder can be used as part of larger PyTorch programs.
Running training
Thunder is in its early stages, it should not be used for production runs yet.
However, it can already deliver outstanding performance on models supported by LitGPT, such as Mistral, Llama2, Gemma, Falcon, and derivatives.
Run training loop for Llama, single-GPU:
python examples/lit-gpt/train.py
Run training loop for Llama, multi-GPU, using FSDP:
python examples/lit-gpt/train_fsdp.py
See README.md for details on running LitGPT with Thunder.
What's in the box
Given a python callable or PyTorch module, Thunder can generate an optimized program that:
- Computes its forward and backward passes
- Coalesces operations into efficient fusion regions
- Dispatches computations to optimized kernels
- Distributes computations optimally across machines
To do so, Thunder ships with:
- A JIT for acquiring Python programs targeting PyTorch and custom operations
- A multi-level IR to represent operations as a trace of a reduced op-set
- An extensible set of transformations on the trace, such as
grad
, fusions, distributed (likeddp
,fsdp
), functional (likevmap
,vjp
,jvp
) - A way to dispatch operations to an extensible collection of executors
Thunder is written entirely in Python. Even its trace is represented as valid Python at all stages of transformation. This allows unprecedented levels of introspection and extensibility.
Thunder doesn't generate code for accelerators directly. It acquires and transforms user programs so that it's possible to optimally select or generate device code using fast executors like:
- torch.compile
- nvFuser
- cuDNN
- Apex
- TransformerEngine
- PyTorch eager
- custom kernels, including those written with OpenAI Triton
Modules and functions compiled with Thunder fully interoperate with vanilla PyTorch and support PyTorch's autograd. Also, Thunder works alongside torch.compile to leverage its state-of-the-art optimizations.
Build the documentation
Docs are currently not hosted publicly. However you can build them locally really quickly:
make docs
and point your browser to the generated docs at docs/build/index.html
.
Develop and run tests
You can set up your environment for developing Thunder by installing the development requirements:
pip install -r requirements/devel.txt
Install Thunder as an editable package (optional):
pip install -e .
Now you run tests:
pytest thunder/tests
Thunder is very thoroughly tested, so expect this to take a while.
License
Lightning Thunder is released under the Apache 2.0 license. See LICENSE file for 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
File details
Details for the file lightning-thunder-0.1.0.tar.gz
.
File metadata
- Download URL: lightning-thunder-0.1.0.tar.gz
- Upload date:
- Size: 447.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 80ccab2d9c9b1ad73f3fc3c05dd87bd996e25b07f215c82603106ffabe5b8216 |
|
MD5 | bbf08857b2d912d10a9dc26dd209fc46 |
|
BLAKE2b-256 | 902126d3cdf52c8322c9fb6b2324d9060b8349b8369881106c8e09cafd7b32a8 |
File details
Details for the file lightning_thunder-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: lightning_thunder-0.1.0-py3-none-any.whl
- Upload date:
- Size: 484.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dd95bbfc2fbd81a1e8cafc34d28ed94b33ae77d6656c617887af3dc1091cd550 |
|
MD5 | cab06adf92a0c61cd36701c5fda0e2ae |
|
BLAKE2b-256 | 336ecfaffd25f946b0db76fb813d91125871b84b1498c1e00e72169012cf0cc1 |