Skip to content
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

Mock Fabric os.environ variables to be sure to have a SingleDeviceStrategy Fabric object #250

Merged
merged 2 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 10 additions & 1 deletion sheeprl/utils/fabric.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from unittest import mock

from lightning.fabric import Fabric
from lightning.fabric.accelerators import XLAAccelerator
from lightning.fabric.strategies import SingleDeviceStrategy, SingleDeviceXLAStrategy
Expand All @@ -23,4 +25,11 @@ def get_single_device_fabric(fabric: Fabric) -> Fabric:
checkpoint_io=None,
precision=fabric._precision,
)
return Fabric(strategy=strategy)
with mock.patch.dict("os.environ") as mocked_os_environ:
mocked_os_environ.pop("LT_DEVICES", None)
mocked_os_environ.pop("LT_STRATEGY", None)
mocked_os_environ.pop("LT_NUM_NODES", None)
mocked_os_environ.pop("LT_PRECISION", None)
mocked_os_environ.pop("LT_ACCELERATOR", None)
fabric = Fabric(strategy=strategy)
return fabric
13 changes: 13 additions & 0 deletions tests/test_utils/test_fabric.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from lightning import Fabric
from lightning.fabric.strategies import SingleDeviceStrategy

from sheeprl.utils.fabric import get_single_device_fabric


def test_get_single_device_fabric():
fabric = Fabric(devices=2, accelerator="cpu", precision=16)
single_device_fabric = get_single_device_fabric(fabric)
assert single_device_fabric.device == fabric.device
assert single_device_fabric._precision == fabric._precision
assert single_device_fabric.accelerator == fabric.accelerator
assert isinstance(single_device_fabric.strategy, SingleDeviceStrategy)
Loading