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

Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions src/lightning/fabric/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

-


### Changed

- Raise ValueError when seed is `out-of-bounds` or `cannot be cast to int` ([#21029](https://github.com/Lightning-AI/pytorch-lightning/pull/21029))


### Fixed

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


- fix: remove extra `name` parameter in accelerator registry decorator ([#20975](https://github.com/Lightning-AI/pytorch-lightning/pull/20975))


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
61 changes: 61 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,64 @@ 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)
@mock.patch("lightning.fabric.accelerators.xla._XLA_GREATER_EQUAL_2_1", True)
@mock.patch("lightning.fabric.plugins.environments.xla._XLA_GREATER_EQUAL_2_1", True)
def test_world_size_from_xla_runtime_greater_2_1(xla_available):
"""Test that world_size uses torch_xla.runtime when XLA >= 2.1."""
env = XLAEnvironment()

with mock.patch("torch_xla.runtime.world_size", return_value=4) as mock_world_size:
env.world_size.cache_clear()
assert env.world_size() == 4
mock_world_size.assert_called_once()


@mock.patch.dict(os.environ, {}, clear=True)
@mock.patch("lightning.fabric.accelerators.xla._XLA_GREATER_EQUAL_2_1", True)
@mock.patch("lightning.fabric.plugins.environments.xla._XLA_GREATER_EQUAL_2_1", True)
def test_global_rank_from_xla_runtime_greater_2_1(xla_available):
"""Test that global_rank uses torch_xla.runtime when XLA >= 2.1."""
env = XLAEnvironment()

with mock.patch("torch_xla.runtime.global_ordinal", return_value=2) as mock_global_ordinal:
env.global_rank.cache_clear()
assert env.global_rank() == 2
mock_global_ordinal.assert_called_once()


@mock.patch.dict(os.environ, {}, clear=True)
@mock.patch("lightning.fabric.accelerators.xla._XLA_GREATER_EQUAL_2_1", True)
@mock.patch("lightning.fabric.plugins.environments.xla._XLA_GREATER_EQUAL_2_1", True)
def test_local_rank_from_xla_runtime_greater_2_1(xla_available):
"""Test that local_rank uses torch_xla.runtime when XLA >= 2.1."""
env = XLAEnvironment()

with mock.patch("torch_xla.runtime.local_ordinal", return_value=1) as mock_local_ordinal:
env.local_rank.cache_clear()
assert env.local_rank() == 1
mock_local_ordinal.assert_called_once()


@mock.patch.dict(os.environ, {}, clear=True)
@mock.patch("lightning.fabric.accelerators.xla._XLA_GREATER_EQUAL_2_1", True)
@mock.patch("lightning.fabric.plugins.environments.xla._XLA_GREATER_EQUAL_2_1", True)
def test_setters_readonly_when_xla_runtime_greater_2_1(xla_available):
"""Test that set_world_size and set_global_rank don't affect values when using XLA runtime >= 2.1."""
env = XLAEnvironment()

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

# Values should come from XLA runtime and not be affected by setters
env.set_world_size(100)
assert env.world_size() == 4

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