Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Thunder Thunder

Make PyTorch models Lightning fast.


Lightning.ai • Performance • Get started • Install • Examples • Inside Thunder • Get involved! • Documentation

license CI testing General checks Documentation Status pre-commit.ci status

Welcome to ⚡ Lightning Thunder

Thunder makes PyTorch models Lightning fast.

Thunder is a source-to-source compiler for PyTorch. It makes PyTorch programs faster by combining and using different hardware executors at once (for instance, nvFuser, torch.compile, cuDNN, and TransformerEngine FP8).

It supports both single and multi-GPU configurations. Thunder aims to be usable, understandable, and extensible.

 

[!Note] Lightning Thunder is in alpha. Feel free to get involved, but expect a few bumps along the way.

 

Single-GPU performance

Thunder can achieve significant speedups over standard non-compiled PyTorch code ("PyTorch eager"), through the compounding effects of optimizations and the use of best-in-class executors. The figure below shows the pretraining throughput for Llama 2 7B as implemented in LitGPT.

Thunder

As shown in the plot above, Thunder achieves 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.

 

Multi-GPU performance

Thunder also supports distributed strategies such as DDP and FSDP for training models on multiple GPUs. The following plot displays the normalized throughput measured for Llama 2 7B without FP8 mixed precision; support for FSDP is in progress.

Thunder

 

Get started

The easiest way to get started with Thunder, requiring no extra installations or setups, is by using our Zero to Thunder Tutorial Studio.

 

Install Thunder

To use Thunder on your local machine:

  • install nvFuser nightly and PyTorch nightly together as follows:
# install nvFuser which installs the matching nightly PyTorch
pip install --pre 'nvfuser-cu121[torch]' --extra-index-url https://pypi.nvidia.com
  • install cudnn as follows:
# install cudnn
pip install nvidia-cudnn-frontend
  • Finally, install Thunder as follows:
# install thunder
pip install lightning-thunder
Advanced install options

 

Install from main

Alternatively, you can install the latest version of Thunder directly from this GitHub repository as follows:

# 1) Install nvFuser and PyTorch nightly dependencies:
pip install --pre 'nvfuser-cu121[torch]' --extra-index-url https://pypi.nvidia.com
# 2) Install Thunder itself
pip install git+https://github.com/Lightning-AI/lightning-thunder.git

 

Install to tinker and contribute

If you are interested in tinkering with and contributing to Thunder, we recommend cloning the Thunder repository and installing it in pip's editable mode:

git clone https://github.com/Lightning-AI/lightning-thunder.git
cd lightning-thunder
pip install -e .

 

Develop and run tests

After cloning the lightning-thunder repository and installing it as an editable package as explained above, ou can set up your environment for developing Thunder by installing the development requirements:

pip install -r requirements/devel.txt

Now you run tests:

pytest thunder/tests

Thunder is very thoroughly tested, so expect this to take a while.

 

Hello World

Below is a simple example of how Thunder allows you to 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.

 

Train models

Thunder is in its early stages and should not be used for production runs yet.

However, it can already deliver outstanding performance for pretraining and finetuning LLMs supported by LitGPT, such as Mistral, Llama 2, Gemma, Falcon, and others.

Check out the LitGPT integration to learn about running LitGPT and Thunder together.

 

Inside Thunder: A brief look at the core features

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 intermediate representation (IR) to represent operations as a trace of a reduced operation set
  • An extensible set of transformations on the trace of a computational graph, such as grad, fusions, distributed (like ddp, fsdp), functional (like vmap, 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, such as GPUs, directly. It acquires and transforms user programs so that it's possible to optimally select or generate device code using fast executors like:

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.

 

Documentation

Online documentation is available. To build documentation locally you can use

make docs

and point your browser to the generated docs at docs/build/index.html.

 

Get involved!

We appreciate your feedback and contributions. If you have feature requests, questions, or want to contribute code or config files, please don't hesitate to use the GitHub Issue tracker.

We welcome all individual contributors, regardless of their level of experience or hardware. Your contributions are valuable, and we are excited to see what you can accomplish in this collaborative and supportive environment.

 

License

Lightning Thunder is released under the Apache 2.0 license. See the LICENSE file for details.

Metadata

Release files for lightning-thunder 0.2.0.dev20240818

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

Source distribution (sdist)

Source distribution for lightning-thunder 0.2.0.dev20240818
File Size Uploaded
lightning_thunder-0.2.0.dev20240818.tar.gz 484.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for lightning-thunder 0.2.0.dev20240818
File Interpreter ABI Platform
lightning_thunder-0.2.0.dev20240818-py3-none-any.whl Python 3 none any Details

Total release size: 1.2 MB

Release files / lightning_thunder-0.2.0.dev20240818.tar.gz

Download URL lightning_thunder-0.2.0.dev20240818.tar.gz
Size 484.8 kB
Tags Source
SHA-256 checksum
How to use checksums
44e313384e550265338356bd70d1772e6565154d7175f5e55f1b7dc6f0b72d0b
BLAKE2b-256 checksum
How to use checksums
69bb77f5492740d28a830ff8fe55b8fa1717d32b22654d5204e3f770af91c1e1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.12.5

Release files / lightning_thunder-0.2.0.dev20240818-py3-none-any.whl

Download URL lightning_thunder-0.2.0.dev20240818-py3-none-any.whl
Size 754.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4e77c70f8b4ad513b7adf7de64ad54951afbd62d9b4e6097d20f975a07e48ef0
BLAKE2b-256 checksum
How to use checksums
943c709560c4cc3f244fcfc3d8e1e7cf5365463ff69bc63050dc9f7fd2fd802d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.12.5

Release history Release notifications | RSS feed

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

This release

0.1.0

2 release files

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