2023-01-10 19:11:03 +00:00
######################################
How to structure your code with Fabric
######################################
2023-01-25 10:45:09 +00:00
Fabric is flexible enough to adapt to any project structure, regardless of whether you are experimenting with a simple script or an extensive framework, because it makes no assumptions about how your code is organized.
2023-01-10 19:11:03 +00:00
Despite the ultimate freedom, this page is meant to give beginners a template for how to organize a typical training script with Fabric:
2023-03-07 15:43:47 +00:00
We also have several :doc: `examples <../examples/index>` that you can take inspiration from.
2023-01-10 19:11:03 +00:00
2023-01-12 13:37:24 +00:00
----
2023-01-10 19:11:03 +00:00
***** ***** ***** **
The Main Function
***** ***** ***** **
At the highest level, every Python script should contain the following boilerplate code to guard the entry point for the main function:
.. code-block :: python
def main():
# Here goes all the rest of the code
...
if __name__ == "__main__":
# This is the entry point of your program
main()
2023-01-25 10:45:09 +00:00
This ensures that any form of multiprocessing will work properly (for example, `` DataLoader(num_workers=...) `` etc.)
2023-01-10 19:11:03 +00:00
2023-01-12 13:37:24 +00:00
----
2023-01-10 19:11:03 +00:00
***** ***** *** *
Model Training
***** ***** *** *
Here is a skeleton for training a model in a function `` train() `` :
.. code-block :: python
import lightning as L
def train(fabric, model, optimizer, dataloader):
# Training loop
model.train()
for epoch in range(num_epochs):
for i, batch in enumerate(dataloader):
...
def main():
# (Optional) Parse command line options
args = parse_args()
# Configure Fabric
fabric = L.Fabric(...)
# Instantiate objects
model = ...
optimizer = ...
train_dataloader = ...
# Set up objects
model, optimizer = fabric.setup(model, optimizer)
train_dataloader = fabric.setup_dataloaders(train_dataloader)
# Run training loop
train(fabric, model, optimizer, train_dataloader)
if __name__ == "__main__":
main()
2023-01-12 13:37:24 +00:00
----
2023-01-10 19:11:03 +00:00
***** ***** ***** ***** ***** *** *
Training, Validation, Testing
***** ***** ***** ***** ***** *** *
2023-01-25 10:45:09 +00:00
Often it is desired to evaluate the ability of the model to generalize on unseen data.
2023-01-10 19:11:03 +00:00
Here is how the code would be structured if we did that periodically during training (called validation) and after training (called testing).
.. code-block :: python
import lightning as L
def train(fabric, model, optimizer, train_dataloader, val_dataloader):
# Training loop with validation every few epochs
model.train()
for epoch in range(num_epochs):
for i, batch in enumerate(train_dataloader):
...
if epoch % validate_every_n_epoch == 0:
validate(fabric, model, val_dataloader)
def validate(fabric, model, dataloader):
# Validation loop
model.eval()
for i, batch in enumerate(dataloader):
...
def test(fabric, model, dataloader):
# Test/Prediction loop
model.eval()
for i, batch in enumerate(dataloader):
...
def main():
...
# Run training loop with validation
train(fabric, model, optimizer, train_dataloader, val_dataloader)
2023-02-05 00:09:58 +00:00
# Test on unseen data
test(fabric, model, test_dataloader)
2023-01-10 19:11:03 +00:00
if __name__ == "__main__":
main()
2023-01-12 13:37:24 +00:00
----
2023-01-10 19:11:03 +00:00
***** ***** **
Full Trainer
***** ***** **
2023-03-07 15:43:47 +00:00
Building a fully-fledged, personalized Trainer can be a lot of work.
To get started quickly, copy `this <https://github.com/Lightning-AI/lightning/tree/master/examples/fabric/build_your_own_trainer> `_ Trainer template and adapt it to your needs.
- Only ~500 lines of code, all in one file
- Relies on Fabric to configure accelerator, devices, strategy
- Simple epoch based training with validation loop
- Only essential features included: Checkpointing, loggers, progress bar, callbacks, gradient accumulation
.. raw :: html
<div class="display-card-container">
<div class="row">
.. displayitem ::
:header: Trainer Template
:description: Take our Fabric Trainer template and customize it for your needs
:button_link: https://github.com/Lightning-AI/lightning/tree/master/examples/fabric/build_your_own_trainer
:col_css: col-md-4
:height: 150
:tag: intermediate
.. raw :: html
</div>
</div>