Skip to content

[TPU] TorchTPU backend integration - eager / torch.compile / tp - #14039

Open
JingyaHuang wants to merge 40 commits into
huggingface:mainfrom
JingyaHuang:add-torchtpu-support
Open

[TPU] TorchTPU backend integration - eager / torch.compile / tp#14039
JingyaHuang wants to merge 40 commits into
huggingface:mainfrom
JingyaHuang:add-torchtpu-support

Conversation

@JingyaHuang

@JingyaHuang JingyaHuang commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Need the fix #14739 first.

This 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.

@github-actions github-actions Bot added documentation Improvements or additions to documentation models utils pipelines size/L PR with diff > 200 LOC labels Jun 22, 2026
@JingyaHuang JingyaHuang changed the title [TPU] Initial TorchTPU backend integration (eager + torch.compile) [TPU] TorchTPU backend integration - eager / torch.compile / tp Sep 7, 2026
@JingyaHuang
JingyaHuang marked this pull request as ready for review September 7, 2026 16:23
…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>
@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

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.

Comment thread docs/source/en/optimization/tpu.md
Comment thread docs/source/en/optimization/tpu.md Outdated
pipe.enable_tpu_compile()

# Warmup — triggers static graph compilation.
pipe.tpu_warmup(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tpu_warmup -- where is this function coming from?

Comment on lines +15 to +22
"""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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this also seems like a generally useful thing for me to do on GPUs as well. Why keep this separate?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/diffusers/models/unets/unet_2d_condition.py Outdated
Comment thread tests/models/transformers/run_flux2_tp_tpu_pipeline.py Outdated
reference, and the test checks its exit code.
"""

WORLD_SIZE = 2

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can keep the Neuron related changes in a separate PR please?


@is_tensor_parallel
@require_torch_tpu
class TestFlux2TransformerTensorParallelTPU:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we not create a tester mixin class for this like

class TensorParallelTesterMixin:
and use that instead?

It follows how we test other parallelism techniques.

@stevhliu stevhliu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, thanks for adding docs!

do we need to also mention TP here or no?

Comment thread docs/source/en/optimization/tpu.md Outdated
Comment thread docs/source/en/optimization/tpu.md Outdated
Comment thread docs/source/en/optimization/tpu.md Outdated
Comment thread docs/source/en/optimization/tpu.md Outdated
Comment thread docs/source/en/optimization/tpu.md Outdated
Comment thread docs/source/en/optimization/tpu.md Outdated
Comment thread docs/source/en/optimization/tpu.md Outdated
Comment thread docs/source/en/optimization/tpu.md Outdated
> [!TIP]
> For the best production throughput, prefer `torch.compile` via `pipe.enable_tpu_compile()`.

## API reference

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the API section should gets it own separate page under API > Main classes under Parallel inference as thats the closest related topic

Comment thread docs/source/en/optimization/tpu.md Outdated
Comment thread docs/source/en/optimization/tpu.md Outdated
JingyaHuang and others added 17 commits September 9, 2026 17:35
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>
@github-actions github-actions Bot removed the models label Sep 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation hooks modular-pipelines pipelines size/L PR with diff > 200 LOC tests utils

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants