Skip to content

Commit

Permalink
[dynamo] ExecutorchCallDelegateHigherOrderVariable - add sanity che…
Browse files Browse the repository at this point in the history
…ck that input and output tensors are disjoint (pytorch#111960)

Fixes pytorch#111917

Pull Request resolved: pytorch#111960
Approved by: https://github.com/zou3519
  • Loading branch information
jon-chuang authored and pytorchmergebot committed Oct 28, 2023
1 parent 3080fd8 commit 25f06ee
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
12 changes: 12 additions & 0 deletions test/dynamo/test_higher_order_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -3446,6 +3446,18 @@ def test_override_fallthrough_dispatch_key(self):
[test_op.py_kernels[key]() for key in default_keys],
)

def test_non_aliasing_util(self):
from torch._dynamo.variables.higher_order_ops import _assert_tensors_nonaliasing

a = [torch.tensor(1), {"a": torch.tensor(1)}]
b = (torch.tensor(1),)
_assert_tensors_nonaliasing(a, b)

with self.assertRaisesRegex(
AssertionError, "inputs to function body cannot alias outputs"
):
_assert_tensors_nonaliasing(a, a)


if __name__ == "__main__":
from torch._dynamo.test_case import run_tests
Expand Down
19 changes: 19 additions & 0 deletions torch/_dynamo/variables/higher_order_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ def only_consist_of(var, types):
return False


def _assert_tensors_nonaliasing(inputs, outputs):
input_tensor_ids = {
id(t) for t in pytree.tree_leaves(inputs) if isinstance(t, torch.Tensor)
}
output_tensor_ids = {
id(t) for t in pytree.tree_leaves(outputs) if isinstance(t, torch.Tensor)
}
assert input_tensor_ids.isdisjoint(
output_tensor_ids
), "inputs to function body cannot alias outputs"


def validate_args_and_maybe_create_graph_inputs(
sub_args, tracer, tx, manually_set_subgraph_inputs
):
Expand Down Expand Up @@ -710,7 +722,14 @@ def call_function(
real_sub_args = pytree.tree_map_only(
torch.fx.Proxy, lambda a: get_real_value(a.node, tx.output), p_args
)

example_res = lowered_module.original_module(*real_sub_args)

# NOTE [Guaranteeing the 1-1 correspondence of FakeTensors and real tensors]:
# executorch modules promise not to alias inputs and outputs.
# Thus, output FakeTensors will correctly not alias input FakeTensors.
_assert_tensors_nonaliasing(real_sub_args, example_res)

example_value = deepcopy_to_fake_tensor(example_res, tx.fake_mode)

p_args = (lowered_node,) + p_args
Expand Down

0 comments on commit 25f06ee

Please sign in to comment.