Skip to content

Commit e86f2aa

Browse files
Dinesh Mandalapumeta-codesync[bot]
authored andcommitted
Fix fbcode//pearl:test_pearl - test_device_movement (pearl.test.unit.with_pytorch.test_transition.TestTransitionBatch) (T280640184)
Differential Revision: D112857309 fbshipit-source-id: e55069fdaab2428c8de712adccbdedf0cb2c3ad8
1 parent 9dc5839 commit e86f2aa

1 file changed

Lines changed: 20 additions & 17 deletions

File tree

test/unit/with_pytorch/test_transition.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -72,30 +72,33 @@ def test_device_movement(self) -> None:
7272
Test that to() method correctly moves terminated
7373
and truncated to the specified device.
7474
"""
75-
# Skip test if CUDA is not available
76-
if not torch.cuda.is_available():
77-
self.skipTest("CUDA not available")
78-
7975
batch = TransitionBatch(
8076
state=self.state,
8177
action=self.action,
8278
reward=self.reward,
8379
next_state=self.next_state,
8480
)
8581

86-
# Move batch to CUDA
87-
cuda_batch = batch.to(torch.device("cuda"))
88-
89-
# Check that terminated and truncated are moved to CUDA
90-
self.assertEqual(cuda_batch.terminated.device.type, "cuda")
91-
self.assertEqual(cuda_batch.truncated.device.type, "cuda")
92-
93-
# Move batch back to CPU
94-
cpu_batch = cuda_batch.to(torch.device("cpu"))
95-
96-
# Check that terminated and truncated are moved back to CPU
97-
self.assertEqual(cpu_batch.terminated.device.type, "cpu")
98-
self.assertEqual(cpu_batch.truncated.device.type, "cpu")
82+
# The "meta" device is always available and exercises the same
83+
# device-movement code path as an accelerator without requiring a GPU.
84+
target_device = torch.device("meta")
85+
moved_batch = batch.to(target_device)
86+
87+
# Check that terminated and truncated are moved to the target device
88+
self.assertEqual(moved_batch.terminated.device.type, target_device.type)
89+
self.assertEqual(moved_batch.truncated.device.type, target_device.type)
90+
91+
# Additionally test CUDA path when hardware is available
92+
if torch.cuda.is_available():
93+
cuda_batch = TransitionBatch(
94+
state=self.state,
95+
action=self.action,
96+
reward=self.reward,
97+
next_state=self.next_state,
98+
)
99+
cuda_batch = cuda_batch.to(torch.device("cuda"))
100+
self.assertEqual(cuda_batch.terminated.device.type, "cuda")
101+
self.assertEqual(cuda_batch.truncated.device.type, "cuda")
99102

100103
def test_filter_batch_by_bootstrap_mask(self) -> None:
101104
"""Test that filter_batch_by_bootstrap_mask correctly filters terminated and truncated."""

0 commit comments

Comments
 (0)