Skip to content

bugfix: add support for global_ordinal, local_ordinal, world_size in xla #20872

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

AlexandrByzov
Copy link

@AlexandrByzov AlexandrByzov commented Jun 3, 2025

What does this PR do?

PyTorch XLA has deprecated several methods in PyTorch 2.7:

  • Remove torch_xla.core.xla_model.xrt_world_size, replace with torch_xla.runtime.world_size
  • Remove torch_xla.core.xla_model.get_ordinal, replace with torch_xla.runtime.global_ordinal

See here

Fixes #20852

Before submitting
  • Was this discussed/agreed via a GitHub issue? (not for typos and docs): No
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you make sure to update the documentation with your changes? (if necessary)
  • Did you write any new necessary tests? (not for typos and docs): Tests were already written
  • Did you verify new and existing tests pass locally with your changes?
  • Did you list all the breaking changes introduced by this pull request?
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or minor internal changes/refactors)

PR review

Anyone in the community is welcome to review the PR.
Before you start reviewing, make sure you have read the review guidelines. In short, see the following bullet-list:

Reviewer checklist
  • Is this pull request ready for review? (if not, please submit in draft mode)
  • Check that all items from Before submitting are resolved
  • Make sure the title is self-explanatory and the description concisely explains the PR
  • Add labels and milestones (and optionally projects) to the PR so it can be classified

📚 Documentation preview 📚: https://pytorch-lightning--20872.org.readthedocs.build/en/20872/

@github-actions github-actions bot added the fabric lightning.fabric.Fabric label Jun 3, 2025
Copy link

codecov bot commented Jun 3, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 86%. Comparing base (6675932) to head (140ac0b).
Report is 29 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff            @@
##           master   #20872    +/-   ##
========================================
- Coverage      87%      86%    -0%     
========================================
  Files         268      268            
  Lines       23411    23451    +40     
========================================
- Hits        20360    20278    -82     
- Misses       3051     3173   +122     

@Borda Borda changed the title bugfix: add support for global_ordinal, local_ordinal, world_size in xla bugfix: add support for global_ordinal, local_ordinal, world_size in xla Jun 6, 2025
Copy link
Member

@Borda Borda left a comment

Choose a reason for hiding this comment

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

thank you for this finding!
could you pls add test to cover this new behaviour?
can this XLA be also run without TPU?

@AlexandrByzov
Copy link
Author

thank you for this finding! could you pls add test to cover this new behaviour? can this XLA be also run without TPU?

Thank you for taking the time to review. I implemented a test with mocks, because as far as I understand torch_xla is not used in tests without TPU. Generally, it is very much possible to use XLA with CPU or even GPU, but this will require a large change in tests and probably a separate issue/s

Comment on lines +69 to +73
if _XLA_GREATER_EQUAL_2_1:
from torch_xla import runtime as xr

return xr.world_size()

Copy link

@bhimrazy bhimrazy Jun 20, 2025

Choose a reason for hiding this comment

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

Thanks for the update! Just to clarify — shouldn't this be _XLA_GREATER_EQUAL_2_7 instead? Pls let me know if I’m missing something here.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you very much for the review and suggestions!

Yes, the old methods were deprecated in 2.7, but the new methods were introduced already in 2.1. You could see it here: https://github.com/pytorch/xla/releases/tag/v2.1.0, MR#5011

  1. world_size: https://github.com/pytorch/xla/pull/5011/files#diff-b5151ded18fbfdf1debacf597819bd88f7954b7f67f951fa8e131340805e5df5R129
  2. global_ordinal: https://github.com/pytorch/xla/pull/5011/files#diff-b5151ded18fbfdf1debacf597819bd88f7954b7f67f951fa8e131340805e5df5R152
  3. local_ordinal: https://github.com/pytorch/xla/pull/5011/files#diff-b5151ded18fbfdf1debacf597819bd88f7954b7f67f951fa8e131340805e5df5R162

I did not want to introduce more flags when the current ones should do the job, hence this usage of 2.1.

I also just now ran this on Google Collab just to check if there were any differences in version 2.1, but it seems that the outputs are identical:

import torch_xla
from torch_xla import runtime as xr
import torch_xla.core.xla_model as xm

print("TORCH XLA VERSION: ", torch_xla.__version__)

assert xr.world_size() == xm.xrt_world_size()
assert xr.local_ordinal() == xm.get_local_ordinal()
assert xr.global_ordinal() == xm.get_ordinal()

Choose a reason for hiding this comment

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

Oh, got it! Thanks a lot for the clarification, @AlexandrByzov.
I mistakenly assumed those methods got introduced in 2.7 — my bad.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for adding this question, it would provide a context to this decision for others too!

Co-authored-by: Bhimraj Yadav <bhimrajyadav977@gmail.com>
Co-authored-by: Bhimraj Yadav <bhimrajyadav977@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fabric lightning.fabric.Fabric
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support PyTorch/XLA 2.7
3 participants