|
21 | 21 |
|
22 | 22 | import jax |
23 | 23 | import jax.numpy as jnp |
| 24 | +from jax.sharding import PartitionSpec as P |
24 | 25 | import numpy as np |
25 | 26 | from flax.linen import partitioning as nn_partitioning |
26 | 27 |
|
27 | 28 | from maxdiffusion import max_logging |
| 29 | +from maxdiffusion.max_utils import device_put_replicated |
28 | 30 | from ..pipeline_flax_utils import FlaxDiffusionPipeline |
29 | 31 | from ...models.flux.transformers.transformer_flux_flax import Flux2KleinTransformer2DModel |
30 | 32 | from ...models.vae_flax import FlaxAutoencoderKL |
@@ -240,6 +242,17 @@ def __call__( |
240 | 242 | trace["prompt_encoding"] = time.perf_counter() - t0 |
241 | 243 | max_logging.log(f" -> [TIMING] Prompt Encoding (Qwen3): {trace['prompt_encoding']:.4f} seconds ⏱️") |
242 | 244 |
|
| 245 | + # Shard pipeline batch inputs across data_sharding for SPMD multi-host execution |
| 246 | + data_sharding = jax.sharding.NamedSharding(self.mesh, P(*self._config.data_sharding)) |
| 247 | + |
| 248 | + def put_data_on_devices(x, sharding): |
| 249 | + if hasattr(sharding, "is_fully_addressable") and sharding.is_fully_addressable: |
| 250 | + return jax.device_put(x, sharding) |
| 251 | + return device_put_replicated(x, sharding) |
| 252 | + |
| 253 | + latents_jax = put_data_on_devices(latents_jax, data_sharding) |
| 254 | + prompt_embeds_jax = put_data_on_devices(prompt_embeds_jax, data_sharding) |
| 255 | + |
243 | 256 | # --------------------------------------------------------------------- |
244 | 257 | # PHASE B: Denoising Loop (Flux Transformer) |
245 | 258 | # --------------------------------------------------------------------- |
|
0 commit comments