fal.distributed module enables you to scale your AI workloads across multiple GPUs, dramatically improving performance for both inference and training tasks. Whether you need to generate multiple images simultaneously or train large models faster, distributed computing on fal makes it straightforward.
Why Use Multiple GPUs?
For Inference
- Higher Throughput: Generate multiple outputs simultaneously (e.g., 4 images at once on 4 GPUs)
- Faster Single Output: Split large models across GPUs for faster generation
- Cost Efficiency: Maximize GPU utilization for batch processing
For Training
- Faster Training: Distribute training across multiple GPUs with synchronized gradient updates
- Larger Batches: Train with bigger batch sizes for better model convergence
- Parallel Preprocessing: Speed up data preprocessing by distributing it across GPUs
Core Concepts
Architecture Overview
Here’s how the distributed computing components work together:DistributedRunner
TheDistributedRunner is the main orchestration class that manages multi-GPU workloads. It handles process management, inter-process communication via ZMQ, and coordination between worker processes.
Key Methods
DistributedRunner(worker_cls, world_size)
Creates a runner that will spawn world_size instances of your worker_cls.
await runner.start(**kwargs)
Launches all worker processes and calls their setup() method. Any **kwargs are passed to each worker’s setup. Call this once in your app’s setup().
await runner.invoke(payload)
Executes your worker’s __call__() method with the given payload dict and returns the final result. Use this for standard (non-streaming) requests.
runner.stream(payload, as_text_events=True)
Returns an async iterator that streams intermediate results from workers. Use this with StreamingResponse for real-time progress updates.
Example Usage
For complete API documentation with all parameters and examples, see the DistributedRunner API Reference.
DistributedWorker
Your custom worker class extendsDistributedWorker and runs on each GPU. Each worker is independent and has its own copy of the model.
Methods to Override
def setup(self, **kwargs)
Called once when the worker initializes. Load your model, download weights, and prepare resources here. Receives any **kwargs passed to runner.start().
def __call__(self, streaming=False, **kwargs)
Called for each request. Implement your inference or training logic here. Receives the payload dict from runner.invoke() or runner.stream() as **kwargs.
Properties You’ll Use
self.device - The PyTorch CUDA device for this worker (cuda:0, cuda:1, etc.)
self.rank - The worker’s rank (0 to world_size-1). Rank 0 is typically responsible for returning results.
self.world_size - Total number of workers (GPUs).
Utility Methods
self.add_streaming_result(data, as_text_event=True)
Sends intermediate results to the client during streaming. Only call from rank 0 to avoid duplicates.
self.rank_print(message)
Prints a message with the worker’s rank prefix for easier debugging.
For complete API documentation with detailed examples, see the DistributedWorker API Reference.
PyTorch Distributed Primitives
For training and coordinated inference, you can use PyTorch’s distributed primitives:Parallelism Strategies
Inference Strategies
Different parallelism strategies optimize for different inference scenarios. Choose based on your use case and model architecture.Data Parallelism
Pipeline Parallelism (PipeFusion)
Tensor Parallelism
Sequence Parallelism (Ulysses)
CFG Parallelism
Hybrid Strategies
Training Strategies
Multi-GPU training strategies focus on distributing the computational and memory requirements of training large models.Distributed Data Parallel (DDP)
Pipeline Parallelism
Tensor Parallelism
FSDP/ZeRO
Many production workloads benefit from hybrid strategies. For example, xFuser can combine PipeFusion + Ulysses + CFG parallelism to scale across 8+ GPUs efficiently.
Configuration
Specifying GPU Count
Multi-GPU Machine Types
fal supports various multi-GPU configurations:GPU-H100withnum_gpus=2: 2x H100 GPUsGPU-H100withnum_gpus=4: 4x H100 GPUsGPU-H100withnum_gpus=8: 8x H100 GPUsGPU-A100withnum_gpus=2/4/8: 2-8x A100 GPUs
Examples
All examples are available in the fal-demos repository:- Parallel SDXL: Data parallelism for image generation (code)
- xFuser: Model parallelism with DiT models (code)
- Flux LoRA Training: Complete DDP training pipeline (code)
Next Steps
API Reference
Multi-GPU Inference Tutorial
Multi-GPU Training Example
Event Streaming
Additional Resources
- fal-demos distributed examples - Complete working code
- Python SDK Reference - Full Python API
- App Lifecycle - How apps are structured and how runners start up and shut down