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 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
9bdedd0
bugfix: add support for global_ordinal, local_ordinal, world_size in xla
Jun 3, 2025
4e8e86c
docs: update changelog
Jun 3, 2025
ee41f82
feat: add tests for world_size, global_ordinal, local_ordinal
Jun 15, 2025
13cf1d0
fix: remove set local rank
Jun 15, 2025
85bbdce
Update tests/tests_fabric/plugins/environments/test_xla.py
AlexandrByzov Jun 20, 2025
140ac0b
Update tests/tests_fabric/plugins/environments/test_xla.py
AlexandrByzov Jun 20, 2025
39bd1b6
Merge branch 'master' into bugfix/20852_add-support-for-xla2.7-which-…
AlexandrByzov Jun 29, 2025
e013cb2
Merge branch 'master' into bugfix/20852_add-support-for-xla2.7-which-…
AlexandrByzov Jul 19, 2025
a2e66b1
Merge branch 'master' into bugfix/20852_add-support-for-xla2.7-which-…
deependujha Aug 4, 2025
4e00bd4
Merge branch 'master' into bugfix/20852_add-support-for-xla2.7-which-…
bhimrazy Aug 4, 2025
3c1c5b0
Merge branch 'master' into bugfix/20852_add-support-for-xla2.7-which-…
bhimrazy Aug 4, 2025
968050e
Merge branch 'master' into bugfix/20852_add-support-for-xla2.7-which-…
bhimrazy Aug 5, 2025
4a54af6
Merge branch 'master' into bugfix/20852_add-support-for-xla2.7-which-…
Borda Aug 5, 2025
81ac311
Merge branch 'master' into bugfix/20852_add-support-for-xla2.7-which-…
Borda Aug 8, 2025
a53a2e1
apply suggestions: split tests
bhimrazy Aug 11, 2025
0f27e24
Merge branch 'master' into bugfix/20852_add-support-for-xla2.7-which-…
Borda Aug 12, 2025
4e7aebd
Apply suggestions from code review
Borda Aug 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/lightning/fabric/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

-

### Fixed

- Fix XLA strategy to add support for for global_ordinal, local_ordinal, world_size which came instead of deprecated methods ([#20852](https://github.com/Lightning-AI/pytorch-lightning/issues/20852))

---

Expand Down
15 changes: 15 additions & 0 deletions src/lightning/fabric/plugins/environments/xla.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ def world_size(self) -> int:
The output is cached for performance.
"""
if _XLA_GREATER_EQUAL_2_1:
from torch_xla import runtime as xr

return xr.world_size()

import torch_xla.core.xla_model as xm

return xm.xrt_world_size()
Expand All @@ -82,6 +87,11 @@ def global_rank(self) -> int:
The output is cached for performance.
"""
if _XLA_GREATER_EQUAL_2_1:
from torch_xla import runtime as xr

return xr.global_ordinal()

import torch_xla.core.xla_model as xm

return xm.get_ordinal()
Expand All @@ -98,6 +108,11 @@ def local_rank(self) -> int:
The output is cached for performance.
"""
if _XLA_GREATER_EQUAL_2_1:
from torch_xla import runtime as xr

return xr.local_ordinal()

import torch_xla.core.xla_model as xm

return xm.get_local_ordinal()
Expand Down
28 changes: 28 additions & 0 deletions tests/tests_fabric/plugins/environments/test_xla.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,31 @@ def test_detect(monkeypatch):

monkeypatch.setattr(lightning.fabric.accelerators.xla.XLAAccelerator, "is_available", lambda: True)
assert XLAEnvironment.detect()


@mock.patch.dict(os.environ, {}, clear=True)
def test_attributes_from_xla_greater_21_used(xla_available, monkeypatch):
"""Test XLA environment attributes when using XLA runtime >= 2.1."""
monkeypatch.setattr(lightning.fabric.accelerators.xla, "_XLA_GREATER_EQUAL_2_1", True)
monkeypatch.setattr(lightning.fabric.plugins.environments.xla, "_XLA_GREATER_EQUAL_2_1", True)

env = XLAEnvironment()

with (
mock.patch("torch_xla.runtime.world_size", return_value=4),
mock.patch("torch_xla.runtime.global_ordinal", return_value=2),
mock.patch("torch_xla.runtime.local_ordinal", return_value=1),
):
env.world_size.cache_clear()
env.global_rank.cache_clear()
env.local_rank.cache_clear()

assert env.world_size() == 4
assert env.global_rank() == 2
assert env.local_rank() == 1

env.set_world_size(100)
assert env.world_size() == 4

env.set_global_rank(100)
assert env.global_rank() == 2
Loading