pytorch/torch/_dynamo/variables
voznesenskym 3e4d14702a On grad access, check if grad has changed and update stored example grad as needed (#112811)
Fixes https://github.com/pytorch/pytorch/issues/112446

This is a doozy of a PR, there's a few important things to keep in mind here:

1) We MUST lift all tensors accessed via attrs to inputs, getattr is a no go in the graph, it violates the aot_autograd contract. Furthermore, aot_autograd does not know how to apply in-place ops to intermediary tensors that are attributes (aka from getattr) anyway. Views from ops are fine.

2) `.grad` access handling in dynamo peeks at the underlying value, the real tensor, because re-piping FakeTensors already made with this fake_mode through builder anew is a no go.

3) We have no proper mechanism for updating the hint / grapharg.example (the real value in (2) above) midway through trace

Therefore, what we need to do is reconcile the difference in grad stashed on grapharg.example. The easiest way to do this is lazily, upon .grad access, by reading the new value off the right fake tensors. We can then make a tensor using that data as a hint to VariableBuilder to make the right VariableTracker. Note that the example value used here (torch.zeros) in the PR, is a dummy value only used as a tracing hint, it does not leak out into real runtime code.

Alternatively, we could implement accumulate_grad_ in python...

Pull Request resolved: https://github.com/pytorch/pytorch/pull/112811
Approved by: https://github.com/jansel
2023-11-08 05:45:00 +00:00
..
__init__.py [dynamo] Add LazyVariableTracker (#111306) 2023-11-07 19:55:19 +00:00
base.py [dynamo] Remove VariableTracker.propagate (#111726) 2023-11-07 19:55:19 +00:00
builder.py [dynamo] use APIs to use device interface instead of raw object in dynamo capture (#113000) 2023-11-08 01:45:00 +00:00
builtin.py On grad access, check if grad has changed and update stored example grad as needed (#112811) 2023-11-08 05:45:00 +00:00
constant.py [dynamo] Remove VariableTracker.propagate (#111726) 2023-11-07 19:55:19 +00:00
ctx_manager.py [dynamo] Remove VariableTracker.propagate (#111726) 2023-11-07 19:55:19 +00:00
dicts.py [dynamo] Avoid eager imports of classes with custom VariableTrackers (#112319) 2023-11-07 22:45:54 +00:00
distributed.py [dynamo] Remove VariableTracker.propagate (#111726) 2023-11-07 19:55:19 +00:00
functions.py [dynamo] Remove VariableTracker.propagate (#111726) 2023-11-07 19:55:19 +00:00
higher_order_ops.py [dynamo] Eagerly install guards (#111415) 2023-11-07 19:55:19 +00:00
iter.py [dynamo] Remove VariableTracker.add_options (#111725) 2023-11-07 19:55:19 +00:00
lazy.py [dynamo] Remove VariableTracker.propagate (#111726) 2023-11-07 19:55:19 +00:00
lists.py [dynamo] Remove VariableTracker.propagate (#111726) 2023-11-07 19:55:19 +00:00
misc.py [dynamo] Remove incorrect sources (#112961) 2023-11-07 22:01:40 +00:00
nn_module.py [dynamo] Remove VariableTracker.propagate (#111726) 2023-11-07 19:55:19 +00:00
optimizer.py [dynamo] Eagerly install guards (#111415) 2023-11-07 19:55:19 +00:00
tensor.py [dynamo] Remove VariableTracker.propagate (#111726) 2023-11-07 19:55:19 +00:00
torch_function.py [dynamo] Eagerly install guards (#111415) 2023-11-07 19:55:19 +00:00
torch.py [dynamo] use APIs to use device interface instead of raw object in dynamo capture (#113000) 2023-11-08 01:45:00 +00:00
user_defined.py [dynamo] Avoid eager imports of classes with custom VariableTrackers (#112319) 2023-11-07 22:45:54 +00:00