App Best Practices¶
Tip
This page covers practical recommendations for TorchX applications: portable storage with fsspec, train loop frameworks, checkpointing, metrics, and testing. See Component Best Practices for component authoring guidance.
Prerequisites: Quickstart and Basic Concepts.
Data Passing and Storage¶
Use fsspec (Filesystem Spec) for storage access. fsspec provides a unified interface to many storage backends (local, S3, GCS, etc.), so apps run on different infrastructures by changing paths alone.
import fsspec
with fsspec.open("s3://bucket/data.pt", "rb") as f:
data = torch.load(f, weights_only=True)
Train Loops¶
Common choices:
Pure PyTorch
See Train for more information.
Metrics¶
Use TensorBoard for logging metrics. TensorBoard supports remote storage (S3, GCS) for viewing metrics during training.
See Metrics for TorchX metric components.
Checkpointing¶
Periodic checkpoints enable failure recovery and training resumption. PyTorch Lightning provides standardized checkpointing.
Fine Tuning¶
Provide a command line argument to your app that resumes from a checkpoint file. This enables transfer learning, fine tuning, and failure recovery with a single application.
Interpretability¶
Use Captum for model interpretability. See Interpret for built-in components.
Model Packaging¶
Python + Saved Weights¶
The most common format. Load a model definition from Python, then load weights
from a .ckpt or .pt file.
TorchScript Models¶
Serializable, optimized models executable without Python. See the TorchScript docs.
torch.export¶
PyTorch’s modern export path for production deployment. Produces
ExportedProgram artifacts
that work with torch.compile and inference runtimes.
Serving / Inference¶
Use TorchServe for standard use cases. See Serve for built-in components.
Testing¶
Since TorchX apps are standard Python, test them like any other Python code:
import unittest
from your.custom.app import main
class CustomAppTest(unittest.TestCase):
def test_main(self) -> None:
main(["--src", "src", "--dst", "dst"])
self.assertTrue(...)
See also
- Component Best Practices
Best practices for authoring reusable TorchX components.
- Quickstart
Getting started with TorchX from scratch.