2023-03-15 19:19:41 +00:00
.. include :: links.rst
2023-03-17 08:42:58 +00:00
####################
Welcome to ⚡ Fabric
####################
2021-10-30 10:25:52 +00:00
2023-02-09 18:06:29 +00:00
Fabric is the fast and lightweight way to scale PyTorch models without boilerplate code.
2021-11-02 15:13:01 +00:00
2023-02-09 18:06:29 +00:00
- Easily switch from running on CPU to GPU (Apple Silicon, CUDA, ...), TPU, multi-GPU or even multi-node training
- State-of-the-art distributed training strategies (DDP, FSDP, DeepSpeed) and mixed precision out of the box
- Handles all the boilerplate device logic for you
- Brings useful tools to help you build a trainer (callbacks, logging, checkpoints, ...)
- Designed with multi-billion parameter models in mind
2021-10-30 10:25:52 +00:00
2023-02-09 18:06:29 +00:00
|
2021-10-30 10:25:52 +00:00
2023-01-04 18:11:29 +00:00
.. code-block :: diff
2021-10-30 10:25:52 +00:00
2023-01-04 18:11:29 +00:00
import torch
import torch.nn as nn
from torch.utils.data import DataLoader, Dataset
2021-10-30 10:25:52 +00:00
2023-01-04 18:11:29 +00:00
+ from lightning.fabric import Fabric
2021-10-30 10:25:52 +00:00
2023-01-12 12:08:32 +00:00
class PyTorchModel(nn.Module):
2023-01-04 18:11:29 +00:00
...
2021-10-30 10:25:52 +00:00
2023-01-12 12:08:32 +00:00
class PyTorchDataset(Dataset):
2023-01-04 18:11:29 +00:00
...
2021-10-30 10:25:52 +00:00
2023-01-04 18:11:29 +00:00
+ fabric = Fabric(accelerator="cuda", devices=8, strategy="ddp")
+ fabric.launch()
2021-10-30 10:25:52 +00:00
2023-02-20 10:09:55 +00:00
- device = "cuda" if torch.cuda.is_available() else "cpu"
2023-01-12 12:08:32 +00:00
model = PyTorchModel(...)
2023-01-04 18:11:29 +00:00
optimizer = torch.optim.SGD(model.parameters())
+ model, optimizer = fabric.setup(model, optimizer)
2023-01-12 12:08:32 +00:00
dataloader = DataLoader(PyTorchDataset(...), ...)
2023-01-04 18:11:29 +00:00
+ dataloader = fabric.setup_dataloaders(dataloader)
model.train()
2021-10-30 10:25:52 +00:00
2023-01-04 18:11:29 +00:00
for epoch in range(num_epochs):
for batch in dataloader:
2023-01-12 12:08:32 +00:00
input, target = batch
- input, target = input.to(device), target.to(device)
2023-01-04 18:11:29 +00:00
optimizer.zero_grad()
2023-01-12 12:08:32 +00:00
output = model(input)
loss = loss_fn(output, target)
2023-01-04 18:11:29 +00:00
- loss.backward()
+ fabric.backward(loss)
optimizer.step()
2023-01-23 13:28:20 +00:00
lr_scheduler.step()
2021-10-30 10:25:52 +00:00
2023-01-12 13:37:24 +00:00
----
2021-10-30 10:25:52 +00:00
2023-02-09 18:06:29 +00:00
***** ***** *
Why Fabric?
***** ***** *
2023-03-10 17:16:07 +00:00
|
|
.. figure :: https://pl-public-data.s3.amazonaws.com/assets_lightning/fabric/PyTorch-to-Fabric-Spectrum-2.svg
:alt: Fabric spans across a large spectrum - from raw PyTorch all the way to high-level PyTorch Lightning
:width: 100%
|
|
2023-03-15 19:19:41 +00:00
Fabric differentiates itself from a fully-fledged trainer like Lightning's `Trainer`_ in these key aspects:
2023-02-09 18:06:29 +00:00
**Fast to implement**
There is no need to restructure your code: Just change a few lines in the PyTorch script and you'll be able to leverage Fabric features.
**Maximum Flexibility**
Write your own training and/or inference logic down to the individual optimizer calls.
2023-03-15 19:19:41 +00:00
You aren't forced to conform to a standardized epoch-based training loop like the one in Lightning `Trainer`_ .
2023-02-09 18:06:29 +00:00
You can do flexible iteration based training, meta-learning, cross-validation and other types of optimization algorithms without digging into framework internals.
This also makes it super easy to adopt Fabric in existing PyTorch projects to speed-up and scale your models without the compromise on large refactors.
Just remember: With great power comes a great responsibility.
**Maximum Control**
2023-03-15 19:19:41 +00:00
The Lightning `Trainer`_ has many built-in features to make research simpler with less boilerplate, but debugging it requires some familiarity with the framework internals.
2023-02-09 18:06:29 +00:00
In Fabric, everything is opt-in. Think of it as a toolbox: You take out the tools (Fabric functions) you need and leave the other ones behind.
This makes it easier to develop and debug your PyTorch code as you gradually add more features to it.
Fabric provides important tools to remove undesired boilerplate code (distributed, hardware, checkpoints, logging, ...), but leaves the design and orchestration fully up to you.
2023-03-07 15:43:47 +00:00
----
***** ***** **
Installation
***** ***** **
Fabric ships directly with Lightning. Install it with
.. code-block :: bash
pip install lightning
2023-03-09 12:28:06 +00:00
For alternative ways to install, read the :doc: `installation guide <fundamentals/installation>` .
2023-03-07 15:43:47 +00:00
2023-01-06 15:54:19 +00:00
2023-01-10 19:11:03 +00:00
.. raw :: html
2023-01-06 15:54:19 +00:00
2023-03-06 15:13:51 +00:00
<div style="display:none">
2023-01-06 15:54:19 +00:00
2023-03-06 15:13:51 +00:00
.. toctree ::
:maxdepth: 1
:name: start
2023-03-17 08:42:58 +00:00
:caption: Home
2023-01-06 15:54:19 +00:00
2023-03-17 08:42:58 +00:00
self
Install <fundamentals/installation>
2023-01-09 18:33:18 +00:00
2023-03-06 15:13:51 +00:00
.. toctree ::
2023-03-17 08:42:58 +00:00
:maxdepth: 1
:caption: Get started in steps
2023-01-09 18:33:18 +00:00
2023-03-17 08:42:58 +00:00
Basic <levels/basic>
Intermediate <levels/intermediate>
Advanced <levels/advanced>
2023-01-12 14:31:34 +00:00
2023-01-09 18:33:18 +00:00
2023-03-06 15:13:51 +00:00
.. toctree ::
:maxdepth: 1
:name: api
2023-03-17 08:42:58 +00:00
:caption: Core API Reference
2023-01-09 18:33:18 +00:00
2023-03-06 15:13:51 +00:00
Fabric Arguments <api/fabric_args>
Fabric Methods <api/fabric_methods>
Utilities <api/utilities>
Full API Reference <api_reference>
2023-01-09 18:33:18 +00:00
2023-03-17 08:42:58 +00:00
.. toctree ::
:maxdepth: 1
:name: more
:caption: More
Examples <examples/index>
2023-01-10 19:11:03 +00:00
.. raw :: html
</div>