Skip to content

make examples in docs device agnostic - #14414

Merged
stevhliu merged 3 commits into
huggingface:mainfrom
kaixuanliu:doc-device
Aug 25, 2026
Merged

make examples in docs device agnostic#14414
stevhliu merged 3 commits into
huggingface:mainfrom
kaixuanliu:doc-device

Conversation

@kaixuanliu

@kaixuanliu kaixuanliu commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Following #12161, which aims to fix #14575

@github-actions github-actions Bot added documentation Improvements or additions to documentation size/L PR with diff > 200 LOC labels Aug 7, 2026
@kaixuanliu
kaixuanliu marked this pull request as ready for review August 10, 2026 03:13
@kaixuanliu

Copy link
Copy Markdown
Contributor Author

@stevhliu Hi, could you help review this as well? Thx!

@yao-matrix

Copy link
Copy Markdown
Contributor

@IlyasMoutawwakil , pls help review, thx.

@stevhliu

Copy link
Copy Markdown
Member

hi, thanks for your PR! i think here for Diffusers, we'd like to keep it as "cuda" for now but we're also thinking more about how we can make it clear that Diffusers also supports other devices. let me know if you have any ideas!

Comment on lines +32 to +37
from diffusers.utils.torch_utils import get_device
from diffusers.utils import load_image, make_image_grid



device = get_device()

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.

yeh this complicates snippets that are meant to be minimal, with transformers "auto" is already a common and well known value for devie_map, but here this more like pure pytorch so users should just replace "cuda" with "xpu" not use a get_device util.

@yao-matrix yao-matrix Aug 14, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Possible to pass device_map="auto" to from_pretrained(), then we can avoid to() which is not best practice and will lead to spike CPU memory usage as staging buffer. @IlyasMoutawwakil @kaixuanliu @stevhliu

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.

i'm not sure how optimized is device_map api in diffusers (zero-copy or not), cc @sayakpaul

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.

device_map="cuda" follows the same strategy as transformers. Also, what do you mean by zero-copy? Something like the following?

with torch.device("meta"):
    model = init_model()
model.load_state_dict(..., assign=True)

If so, yes.

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.

"auto", within the context of pipelines changes meaning, though. A DiffusionPipeline is NOT a single model, but a collection of different models connected in a directed graph. So, we shouldn't use "auto", IMO.

@kaixuanliu kaixuanliu Aug 18, 2026

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.

For pipelines, we use "balanced" or get_device: L124, here in diffusers we already use get_device to do device-agnostic processing, so I think in the doc it is ok to use get_device utils, WDYT? @IlyasMoutawwakil @sayakpaul @yao-matrix , or we can use device = torch.accelerator.current_accelerator().type if torch.accelerator.is_available() else "cpu" instead. device_map=balanced is another option, but it is not suitable for all the cases.

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.

IMO we're still complicating the things that are supposed to be very simpler. I am not sure if the approaches adapted in Transformers directly transfer because of the complexities around the pipeline system. Perhaps it's better to document that in a central manner.

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.

Then can we add comment like device = "cuda" # or "mps", "xpu", "cpu" ? This will not bring actual change to all the existing examples.

@yao-matrix

Copy link
Copy Markdown
Contributor

@kaixuanliu , could you change to use device_map="auto" rather than to()?

@github-actions

Copy link
Copy Markdown
Contributor

Hi @kaixuanliu, thanks for the PR! It does not appear to link an issue it fixes. If this PR addresses an existing issue, please add a closing keyword (e.g. Fixes #1234) to the PR description so the issue is linked. See the contribution guide for more details. If this PR intentionally does not fix a tracked issue, a maintainer can add the no-issue-needed label to silence this reminder.

Please note that PRs without a linked issue are likely to be automatically closed 10 days after this notice.

Once the PR links an issue (or gets the no-issue-needed label), you can ignore this message — it stays here as a comment, but it no longer applies.

@kaixuanliu

kaixuanliu commented Aug 24, 2026

Copy link
Copy Markdown
Contributor Author

@sayakpaul @stevhliu @IlyasMoutawwakil Hi, I have updated the code by adding comment。 This is the least intrusive method I can think of... Is it OK now?

@stevhliu

Copy link
Copy Markdown
Member

thanks, i think this is better and we can also add a sentence or two in the Installation doc that says something like "Diffusers runs on any accelerator, swap in ..."

Signed-off-by: kaixuanliu <kaixuan.liu@intel.com>
@kaixuanliu

Copy link
Copy Markdown
Contributor Author

@stevhliu Thx for your advice. done for it.

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

thanks! 🤗

@stevhliu
stevhliu merged commit b9bc22b into huggingface:main Aug 25, 2026
6 checks passed
@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.

@kaixuanliu
kaixuanliu deleted the doc-device branch August 26, 2026 01:14
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 fixes-issue size/L PR with diff > 200 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

diffusers docs have too many cuda hardcoding, this is unfriendly to device-agnostic

6 participants