pytorch/torch/_dynamo/variables
Steven Troxler 17fd4885aa [dynamo] Support custom dict constructor with kwargs (#112513)
Summary:

As of https://github.com/pytorch/pytorch/pull/103192, dynamo
supports code that creates OrderedDict instances using kwargs
for the key-value pairs rather than passing a dict literal.

But custom dicts (for example subclasses of OrderedDict) follow
a different codepath so that we can check for conditions such
as a custom `__init__` that need to force a graph break.

This commit allows kwargs for custom dict constructors - if the
args are empty and the class is not also a dataclass (which is
the case that, for example, a
`transformers.modeling_outputs.ModelOutput` instance will wind
up hitting) then treat the kwargs as the key-value pairs.

NOTE: For this to behave 100% correctly, we are relying on
the fact that python dicts behave like ordered dicts so that they
preserve the kwargs' ordering. Technically it is not guaranteed that
future versions of Python will respect this; if that behavior changes
we would need to ensure that dynamo uses OrderedDict for kwargs all
the way down in order to handle special cases like OrderedDict where
the kwargs' ordering does matter.

Test Plan:

```
pytest test/dynamo/test_functions.py
```

I also verified that the new test fails without the changes to
`dicts.py`.

Reviewers: yanboliang

Pull Request resolved: https://github.com/pytorch/pytorch/pull/112513
Approved by: https://github.com/yanboliang
2023-10-31 20:55:38 +00:00
..
__init__.py [5/N] Make torch context manager a TorchCtxManagerClassVariable (#111622) 2023-10-27 21:26:54 +00:00
base.py [dynamo] Remove VariableTracker.as_specialized (#112363) 2023-10-30 20:07:55 +00:00
builder.py [dynamo] Remove dead code - real_value_tensor_positive_aliases (#111911) 2023-10-30 23:10:52 +00:00
builtin.py [dynamo] Remove VariableTracker.as_specialized (#112363) 2023-10-30 20:07:55 +00:00
constant.py Revert "[user errors] compulsory case names, allow multiple (#110878)" 2023-10-10 04:44:40 +00:00
ctx_manager.py [dynamo] Replace recursively_contains with parents_tracker (#112122) 2023-10-28 06:46:48 +00:00
dicts.py [dynamo] Support custom dict constructor with kwargs (#112513) 2023-10-31 20:55:38 +00:00
distributed.py [Easy] ConstantVariable() -> .create (#109896) 2023-09-22 22:30:15 +00:00
functions.py Support calling user defined triton kernels with kernel.run (#112292) 2023-10-30 17:51:23 +00:00
higher_order_ops.py Revert "[dynamo] ExecutorchCallDelegateHigherOrderVariable - add sanity check that input and output tensors are disjoint (#111960)" 2023-10-31 20:14:20 +00:00
lists.py [dynamo] Replace recursively_contains with parents_tracker (#112122) 2023-10-28 06:46:48 +00:00
misc.py [dynamo] Remove mutation in AutogradFunctionContextVariable (#112216) 2023-10-28 06:46:48 +00:00
nn_module.py [dynamo] Expand _nonvar_fields names (#111749) 2023-10-23 02:58:16 +00:00
optimizer.py [dynamo] Replace recursively_contains with parents_tracker (#112122) 2023-10-28 06:46:48 +00:00
tensor.py [dynamo] Add support for register_post_accumulate_grad_hook (#112325) 2023-10-31 17:04:49 +00:00
torch_function.py Use pytree.arg_tree_leaves everywhere (#112394) 2023-10-31 15:57:06 +00:00
torch.py Update how Dynamo decides to graph break on an OpOverloadPacket (#112200) 2023-10-31 19:10:37 +00:00
user_defined.py [dynamo] fix None routing bug during var_getattr on UDO (#111614) 2023-10-29 01:57:43 +00:00