Skip to content
Closed
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
16 changes: 16 additions & 0 deletions test/dynamo/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7511,6 +7511,22 @@ def fn(target):
res = opt_func(a)
self.assertIsInstance(res, torch.Tensor)

def test_torch_device_dtype_class(self):
class Foo:
def __init__(
self,
dtype: torch.dtype = torch.float,
device: torch.device = torch.device("cpu"),
) -> None:
self.value = torch.tensor(10, dtype=dtype, device=device)

def fn():
return Foo().value + 1

opt_func = torch._dynamo.optimize("eager", nopython=True)(fn)
res = opt_func()
self.assertEqual(res, 11.0)

def test_itertools_repeat(self):
counters.clear()

Expand Down
2 changes: 1 addition & 1 deletion torch/_dynamo/variables/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1848,7 +1848,7 @@ def __call__(self, tx, value) -> VariableTracker:
return SourcelessBuilder.wrap_constant_literal(value)
elif is_builtin_callable(value):
return BuiltinVariable(value)
elif is_allowed(value):
elif is_allowed(value) or isinstance(value, (torch.device, torch.dtype)):
if is_user_defined_allowed(value):
self.tx.output.has_user_defined_allowed_in_graph = True
return TorchVariable(value)
Copy link
Collaborator

@jon-chuang jon-chuang Oct 29, 2023

Choose a reason for hiding this comment

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

I don't think torch.device/torch.dtype are TorchVariables.

They are ConstantVariables

You need to change the definition of ConstantVariable.is_literal

Copy link
Member Author

Choose a reason for hiding this comment

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

The existing tests for torch.device show it as a TorchVariable

device = torch.device("cpu")
expected_variable = TorchVariable(device)
self.assertEqual(expected_variable.python_type(), type(device))

Is torch.device a TorchVariable but torch.dtype should be a constant?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm that's weird, as variablebuilder seems to wrap it as a constantvariable. I guess both are technically fine, but changing is_literal can get rid of the special-casing here.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Contributor

@yanboliang yanboliang Oct 30, 2023

Choose a reason for hiding this comment

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

torch.device/torch.dtype should be ConstantVariable. Another evidence on top of what @jon-chuang mentioned:

torch.device,
torch.dtype,

Expand Down