[TPU] TorchTPU backend integration - eager / torch.compile / tp - #14039
[TPU] TorchTPU backend integration - eager / torch.compile / tp#14039JingyaHuang wants to merge 40 commits into
Conversation
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ster tpu.md; drop redundant execution_device check - Propagate the text_encoder.device-based fix (introduced for TPU CPU-offload support) from FluxPipeline/Flux2KleinPipeline/WanPipeline into their `# Copied from` copies (flux/*, flux2_klein_inpaint, visualcloze, anyflow, chronoedit, lucy_edit, skyreels_v2/*). SDXL-family copies of StableDiffusionXLPipeline.encode_prompt are intentionally left untouched; they'll be handled in a follow-up PR that fixes device placement for every pipeline component (not just text encoders). - Register docs/source/en/optimization/tpu.md in _toctree.yml (was breaking the docs build: "not present in the table of contents"). - Remove the redundant "prefer non-CPU, non-meta component" loop from DiffusionPipeline._execution_device: PR huggingface#14383 already fixed this in DiffusionPipeline.device, which _execution_device falls back to. Verified on TPU hardware that _execution_device still resolves correctly for a split-placement pipeline after the removal. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
| pipe.enable_tpu_compile() | ||
|
|
||
| # Warmup — triggers static graph compilation. | ||
| pipe.tpu_warmup( |
There was a problem hiding this comment.
tpu_warmup -- where is this function coming from?
| """TPU backend for tensor parallelism, dispatched from ``apply_tensor_parallel(backend="tpu")``. | ||
|
|
||
| The structure mirrors the Neuron backend (``tensor_parallel_neuron.py``). The motivation differs: on Neuron the | ||
| pre-shard path works around an NRT consecutive-reduce-scatter bug; here it prevents OOM. Without pre-sharding, | ||
| ``parallelize_module`` calls ``distribute_tensor`` internally, which loads the full weight matrix on every TPU chip | ||
| before scattering. For large diffusion models this exhausts HBM. Pre-sharding each weight on CPU via | ||
| ``DTensor.from_local`` first means each chip only receives its local shard, then ``parallelize_module`` is called as a | ||
| no-op for weights (they are already DTensors) but still registers the input/output hooks for the forward pass. |
There was a problem hiding this comment.
But this also seems like a generally useful thing for me to do on GPUs as well. Why keep this separate?
There was a problem hiding this comment.
Yeah, this PR was opened in parallel with the TP initial support PR, it would be better to get #14544 landed first, so that the shards are tackled in a more generic way on TPU.
| reference, and the test checks its exit code. | ||
| """ | ||
|
|
||
| WORLD_SIZE = 2 |
There was a problem hiding this comment.
Can keep the Neuron related changes in a separate PR please?
|
|
||
| @is_tensor_parallel | ||
| @require_torch_tpu | ||
| class TestFlux2TransformerTensorParallelTPU: |
There was a problem hiding this comment.
Can we not create a tester mixin class for this like
It follows how we test other parallelism techniques.
stevhliu
left a comment
There was a problem hiding this comment.
nice, thanks for adding docs!
do we need to also mention TP here or no?
| > [!TIP] | ||
| > For the best production throughput, prefer `torch.compile` via `pipe.enable_tpu_compile()`. | ||
|
|
||
| ## API reference |
There was a problem hiding this comment.
the API section should gets it own separate page under API > Main classes under Parallel inference as thats the closest related topic
Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com>
Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com>
Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com>
Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com>
Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com>
Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com>
Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com>
Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com>
Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com>
Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com>
Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com>
…rs into add-torchtpu-support
What does this PR do?
Need the fix #14739 first.
torch.compileThis is a preparation based on TorchTPU beta before the official release.
Who can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.