Commit 27eda58
committed
Fix VAE timing regression for large batch sizes
**Root Cause:**
In commit 7b28885, an optimization was added to prevent OOM errors for large batch sizes (`batch_size > 2`) by batch-sharding the latents and disabling sequential slicing. However, this logic used an `elif replicate_vae:` block, which caused the explicit replication of VAE weights to be entirely skipped for large batch sizes.
Without explicit weight replication, the XLA SPMD partitioner attempts to match the sharding of the input `latents` (which are batch-sharded) with the VAE decode computation. Because `vae.decode` involves fully-replicated noise injection and massive 3D convolutions, XLA heuristically decides to insert enormous amounts of cross-device communication (AllGather/AllReduce) to shard the weights or activations, ballooning the execution time from ~2.8s to ~68.5s for non-upsampled latents.
(Note: For upsampled latents, the memory layout generated by the JIT-compiled upsampler bypasses this XLA heuristic trap, allowing it to execute quickly in ~1.5s, which masked the issue).
**Fix:**
This PR decouples the batch-sharding of `latents` from the replication of VAE weights. It explicitly applies a full replication constraint `NamedSharding(mesh, P())` to the VAE weights in all cases where `replicate_vae` is True, even if `latents` are batch-sharded. This forces XLA into the optimal data-parallel compilation path, restoring the fast ~1.5s - ~2.8s execution time for all scenarios without risking the concatenation-related OOMs.1 parent 4a3ec4f commit 27eda58
1 file changed
Lines changed: 9 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1818 | 1818 | | |
1819 | 1819 | | |
1820 | 1820 | | |
1821 | | - | |
1822 | 1821 | | |
1823 | 1822 | | |
1824 | | - | |
| 1823 | + | |
1825 | 1824 | | |
1826 | 1825 | | |
1827 | 1826 | | |
| |||
1847 | 1846 | | |
1848 | 1847 | | |
1849 | 1848 | | |
| 1849 | + | |
| 1850 | + | |
| 1851 | + | |
| 1852 | + | |
| 1853 | + | |
| 1854 | + | |
| 1855 | + | |
1850 | 1856 | | |
1851 | 1857 | | |
1852 | 1858 | | |
1853 | 1859 | | |
1854 | 1860 | | |
1855 | 1861 | | |
1856 | 1862 | | |
1857 | | - | |
| 1863 | + | |
1858 | 1864 | | |
1859 | 1865 | | |
1860 | 1866 | | |
| |||
0 commit comments