Skip to content
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
3 changes: 3 additions & 0 deletions codeflash/verification/comparator.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,9 @@ def comparator(orig: Any, new: Any, superset_obj=False) -> bool: # noqa: ANN001
if isinstance(orig, torch.dtype):
return orig == new

if isinstance(orig, torch.device):
return orig == new

if HAS_PYRSISTENT:
import pyrsistent # type: ignore # noqa: PGH003

Expand Down
44 changes: 44 additions & 0 deletions tests/test_comparator.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,50 @@ def test_torch():
assert not comparator(gg, ii)


def test_torch_device():
try:
import torch # type: ignore
except ImportError:
pytest.skip()

# Test torch.device comparisons - same device type
a = torch.device("cpu")
b = torch.device("cpu")
assert comparator(a, b)

# Test different device types
c = torch.device("cpu")
d = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
if torch.cuda.is_available():
assert not comparator(c, d)

# Test device with index
e = torch.device("cpu")
f = torch.device("cpu")
assert comparator(e, f)

# Test cuda devices with different indices (if multiple GPUs available)
if torch.cuda.is_available() and torch.cuda.device_count() > 1:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

this will not run in CI, I have a way of mocking it

g = torch.device("cuda:0")
h = torch.device("cuda:0")
i = torch.device("cuda:1")
assert comparator(g, h)
assert not comparator(g, i)

# Test cuda device with and without explicit index
if torch.cuda.is_available():
j = torch.device("cuda:0")
k = torch.device("cuda", 0)
assert comparator(j, k)

# Test meta device
l = torch.device("meta")
m = torch.device("meta")
n = torch.device("cpu")
assert comparator(l, m)
assert not comparator(l, n)


def test_jax():
try:
import jax.numpy as jnp
Expand Down
Loading