Skip to content

Conversation

ttanpcs
Copy link

@ttanpcs ttanpcs commented Jun 13, 2022

Stack from ghstack (oldest at bottom):

  • Added aliasing checks in SchemaCheckMode that raise a RuntimeError if an argument aliases an output that is not specified by the matching schema
  • Added python bindings for isAliasOf: a method that checks if two tensors are aliases of each other, before_set: which returns the before sets and after_set: which returns the after sets.
  • Created IncorrectAliasTensor as a subclass of torch.Tensor that purposefully aliases incorrectly
  • Tested that alias checks in SchemaCheckMode work for both correct and incorrect situations

Note that the cases of inputs aliasing with each other, outputs aliasing with each other, and cases where the before and after sets are not equivalent (this should only occur when the after set is the wildcard set) are not implemented. These cases will be addressed in a later pr.

@facebook-github-bot
Copy link
Contributor

facebook-github-bot commented Jun 13, 2022

🔗 Helpful links

✅ No Failures (0 Pending)

As of commit f17a432 (more details on the Dr. CI page):

Expand to see more

💚 💚 Looks good so far! There are no failures yet. 💚 💚


This comment was automatically generated by Dr. CI (expand for details).

Please report bugs/suggestions to the (internal) Dr. CI Users group.

Click here to manually regenerate this comment.

@facebook-github-bot facebook-github-bot added the oncall: jit Add this issue/PR to JIT oncall triage queue label Jun 13, 2022
@ttanpcs ttanpcs requested review from Gamrix, Krovatkin, bdhirsh, davidberard98 and eellison and removed request for davidberard98 June 13, 2022 23:26
Copy link
Contributor

@davidberard98 davidberard98 left a comment

Choose a reason for hiding this comment

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

Overall looks good! Just had one major comment, and a few minor suggestions

after = tree_flatten(arguments.get(name))[0]
if (any([has_mutated(i, j) for i, j in zip(before, after)]) and not argument.is_mutable):
raise RuntimeError(f"Argument {name} is not defined as mutable but was mutated")
for i in before:
Copy link
Contributor

Choose a reason for hiding this comment

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

very minor nit: i feel like i is usually the name of an index, maybe rename it b? totally not important though, you can ignore if you don't want to change it.

# This TorchDispatchTensor Subclass is used to test incorrectly aliasing ops
# This is done by aliasing the output to the first argument for all ops in INCORRECT_OPS

class IncorrectAliasTensor(torch.Tensor):
Copy link
Contributor

Choose a reason for hiding this comment

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

awesome! This could be pretty useful in the future too if we get around to fixing batch_norm (in which case we would need to find a new way to test mutation)

- Added aliasing checks in SchemaCheckMode that raise a RuntimeError if an argument aliases an output that is not specified by the matching schema
- Added python bindings for isAliasOf: a method that checks if two tensors are aliases of each other, before_set: which returns the before sets and after_set: which returns the after sets.
- Created IncorrectAliasTensor as a subclass of torch.Tensor that purposefully aliases incorrectly
- Tested that alias checks in SchemaCheckMode work for both correct and incorrect situations

**Note that the cases of inputs aliasing with each other, outputs aliasing with each other, and cases where the before and after sets are not equivalent (this should only occur when the after set is the wildcard set) are not implemented. These cases will be addressed in a later pr.**

[ghstack-poisoned]
ttanpcs pushed a commit that referenced this pull request Jun 14, 2022
ghstack-source-id: 3dc05af
Pull Request resolved: #79474
@ttanpcs ttanpcs requested a review from davidberard98 June 14, 2022 00:37
py::call_guard<py::gil_scoped_release>());

m.def("_is_alias_of", [](const at::Tensor& self, const at::Tensor& other) {
return IValue(self).isAliasOf(IValue(other));
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: any reason why we're converting the tensors to IValues first? There's also a Tensor::is_alias_of method.

"instead.")

# This TorchDispatchTensor Subclass is used to test incorrectly aliasing ops
# This is done by aliasing the output to the first argument for all ops in INCORRECT_OPS
Copy link
Contributor

Choose a reason for hiding this comment

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

minor nit: the comment says that this subclass is used to test incorrectly aliasing ops, but technically it's SchemaCheckMode that's responsible for that. It looks like the main purpose of this class is to test that SchemaCheckMode behaves as expected, by "simulating" an op with an incorrect schema and asserting that the mode picks up the error.

Do you mind updating the description a bit?

Can you make it a bit clearer in this comment that the purpose of this class is basically as a test case for schema_check_mode? It doesn't look like it's act


# Tests that an exception is raised for a mismatching alias
def test_alias_check_fail(self):
with self.assertRaises(RuntimeError):
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: for best practice, can you use self.assertRaisesRegex(RuntimeError, "my expected error message")?

- Added aliasing checks in SchemaCheckMode that raise a RuntimeError if an argument aliases an output that is not specified by the matching schema
- Added python bindings for isAliasOf: a method that checks if two tensors are aliases of each other, before_set: which returns the before sets and after_set: which returns the after sets.
- Created IncorrectAliasTensor as a subclass of torch.Tensor that purposefully aliases incorrectly
- Tested that alias checks in SchemaCheckMode work for both correct and incorrect situations

**Note that the cases of inputs aliasing with each other, outputs aliasing with each other, and cases where the before and after sets are not equivalent (this should only occur when the after set is the wildcard set) are not implemented. These cases will be addressed in a later pr.**

[ghstack-poisoned]
ttanpcs pushed a commit that referenced this pull request Jun 16, 2022
ghstack-source-id: 39d5a14
Pull Request resolved: #79474
- Added aliasing checks in SchemaCheckMode that raise a RuntimeError if an argument aliases an output that is not specified by the matching schema
- Added python bindings for isAliasOf: a method that checks if two tensors are aliases of each other, before_set: which returns the before sets and after_set: which returns the after sets.
- Created IncorrectAliasTensor as a subclass of torch.Tensor that purposefully aliases incorrectly
- Tested that alias checks in SchemaCheckMode work for both correct and incorrect situations

**Note that the cases of inputs aliasing with each other, outputs aliasing with each other, and cases where the before and after sets are not equivalent (this should only occur when the after set is the wildcard set) are not implemented. These cases will be addressed in a later pr.**

[ghstack-poisoned]
@ttanpcs ttanpcs requested a review from bdhirsh June 17, 2022 04:24
Copy link
Contributor

@davidberard98 davidberard98 left a comment

Choose a reason for hiding this comment

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

LGTM! just had one comment, please add that and then you can merge once CI is green

return bool(len(lhs_argument.before_set & rhs_argument.before_set))

def unwrap(e):
if isinstance(e, torch.Tensor) and not type(e) == torch.Tensor:
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: could you add a comment about why you need this? (e.g. to handle IncorrectAliasTensor)

@ttanpcs
Copy link
Author

ttanpcs commented Jun 17, 2022

@pytorchbot merge

@pytorchmergebot
Copy link
Collaborator

@pytorchbot successfully started a merge job. Check the current status here

@github-actions
Copy link
Contributor

Hey @goldenxuett.
You've committed this PR, but it does not have both a 'release notes: ...' and 'topics: ...' label. Please add one of each to the PR. The 'release notes: ...' label should represent the part of PyTorch that this PR changes (fx, autograd, distributed, etc) and the 'topics: ...' label should represent the kind of PR it is (not user facing, new feature, bug fix, perf improvement, etc). The list of valid labels can be found here for the 'release notes: ...' and here for the 'topics: ...'.
For changes that are 'topic: not user facing' there is no need for a release notes label.

facebook-github-bot pushed a commit that referenced this pull request Jun 20, 2022
Summary:
Pull Request resolved: #79474

Approved by: https://github.com/davidberard98

Test Plan: contbuild & OSS CI, see https://hud.pytorch.org/commit/pytorch/pytorch/1432a3d6acc7ac58269e5ea7ef6b2373e9a3dbf6

Reviewed By: malfet

Differential Revision: D37278870

Pulled By: goldenxuett

fbshipit-source-id: 2316e8ac4e368fedc8c0d6dd9a4ffb28bd33571c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla signed Merged oncall: jit Add this issue/PR to JIT oncall triage queue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants