Skip to content

Conversation

@wconstab
Copy link
Contributor

@wconstab wconstab commented Jun 30, 2020

Define static script implementation of len and contains on any subclass derived from a type such as ModuleList, Sequential, or ModuleDict. Implement getitem for classes derived from ModuleDict.

@wconstab wconstab requested a review from eellison June 30, 2020 16:56
@wconstab wconstab requested a review from apaszke as a code owner June 30, 2020 16:56
@facebook-github-bot facebook-github-bot added the oncall: jit Add this issue/PR to JIT oncall triage queue label Jun 30, 2020
@dr-ci
Copy link

dr-ci bot commented Jun 30, 2020

💊 CI failures summary and remediations

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


  • 3/3 failures possibly* introduced in this PR
    • 1/3 non-CircleCI failure(s)

🕵️ 2 new failures recognized by patterns

The following CI failures do not appear to be due to upstream breakages:

See CircleCI build pytorch_xla_linux_bionic_py3_6_clang9_build (1/2)

Step: "(Optional) Merge target branch" (full log | diagnosis details | 🔁 rerun)

Automatic merge failed; fix conflicts and then commit the result.
CONFLICT (add/add): Merge conflict in .circleci/cimodel/data/simple/docker_definitions.py 
Auto-merging .circleci/cimodel/data/simple/docker_definitions.py 
CONFLICT (add/add): Merge conflict in .circleci/cimodel/data/simple/bazel_definitions.py 
Auto-merging .circleci/cimodel/data/simple/bazel_definitions.py 
CONFLICT (add/add): Merge conflict in .circleci/cimodel/data/simple/android_definitions.py 
Auto-merging .circleci/cimodel/data/simple/android_definitions.py 
CONFLICT (add/add): Merge conflict in .circleci/cimodel/data/pytorch_build_definitions.py 
Auto-merging .circleci/cimodel/data/pytorch_build_definitions.py 
CONFLICT (add/add): Merge conflict in .circleci/cimodel/data/pytorch_build_data.py 
Auto-merging .circleci/cimodel/data/pytorch_build_data.py 
Automatic merge failed; fix conflicts and then commit the result. 

See CircleCI build pytorch_linux_xenial_py3_6_gcc5_4_build (2/2)

Step: "(Optional) Merge target branch" (full log | diagnosis details | 🔁 rerun)

Automatic merge failed; fix conflicts and then commit the result.
CONFLICT (add/add): Merge conflict in .circleci/cimodel/data/simple/docker_definitions.py 
Auto-merging .circleci/cimodel/data/simple/docker_definitions.py 
CONFLICT (add/add): Merge conflict in .circleci/cimodel/data/simple/bazel_definitions.py 
Auto-merging .circleci/cimodel/data/simple/bazel_definitions.py 
CONFLICT (add/add): Merge conflict in .circleci/cimodel/data/simple/android_definitions.py 
Auto-merging .circleci/cimodel/data/simple/android_definitions.py 
CONFLICT (add/add): Merge conflict in .circleci/cimodel/data/pytorch_build_definitions.py 
Auto-merging .circleci/cimodel/data/pytorch_build_definitions.py 
CONFLICT (add/add): Merge conflict in .circleci/cimodel/data/pytorch_build_data.py 
Auto-merging .circleci/cimodel/data/pytorch_build_data.py 
Automatic merge failed; fix conflicts and then commit the result. 

ci.pytorch.org: 1 failed


This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.

Please report bugs/suggestions on the GitHub issue tracker or post in the (internal) Dr. CI Users group.

See how this bot performed.

This comment has been revised 22 times.

@wconstab wconstab changed the title [WIP] Wconstab/40603 subclass fix __len__, __contains__, getitem inherited from interface class derived from nn container Jun 30, 2020
@wconstab wconstab self-assigned this Jun 30, 2020
@wconstab wconstab changed the title fix __len__, __contains__, getitem inherited from interface class derived from nn container fix __len__, __contains__, getitem inherited from interface class derived from nn container (closes #40603) Jun 30, 2020
Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@wconstab has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@wconstab
Copy link
Contributor Author

wconstab commented Jul 1, 2020

hmm, failing backward_compatibility, no idea why. Will have to take a look at this later.

Jul 01 00:27:26 The PR is introducing backward incompatible changes to the operator library. Please contact PyTorch team to confirm whether this change is wanted or not.
Jul 01 00:27:26
Jul 01 00:27:26 Broken ops: [
Jul 01 00:27:26 aten::view_as(Tensor self, Tensor other) -> (Tensor)
Jul 01 00:27:26 aten::reshape_as(Tensor self, Tensor other) -> (Tensor)
Jul 01 00:27:26 aten::pin_memory(Tensor self) -> (Tensor)
Jul 01 00:27:26 aten::reshape(Tensor self, int[] shape) -> (Tensor)
Jul 01 00:27:26 aten::detach(Tensor self) -> (Tensor)
Jul 01 00:27:26 aten::expand_as(Tensor self, Tensor other) -> (Tensor)
Jul 01 00:27:26 aten::flatten.DimnameList(Tensor self, str[] dims, str out_dim) -> (Tensor)
Jul 01 00:27:26 aten::flatten.named_out_dim(Tensor self, int start_dim, int end_dim, str out_dim) -> (Tensor)
Jul 01 00:27:26 aten::flatten.using_ints(Tensor self, int start_dim=0, int end_dim=-1) -> (Tensor)
Jul 01 00:27:26 aten::flatten.using_names(Tensor self, str start_dim, str end_dim, str out_dim) -> (Tensor)
Jul 01 00:27:26 aten::contiguous(Tensor self, *, int memory_format=0) -> (Tensor)
Jul 01 00:27:26 ]

Copy link
Contributor

@eellison eellison left a comment

Choose a reason for hiding this comment

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

Cool! Looks great! a few small small comments here or there (for nits feel free to not take the suggestion obviously). I think we should create a new test file for this test and some of the existing ones.

The two issues you've fixed here are in the same class of issues, but dont really have a dependence on each other. In the future it would nice to use ghstack to cover similar cases...

test/test_jit.py Outdated
assert self.moduledict['blah'] == "blah", "this is a keyerror"

with self.assertRaisesRegex(RuntimeError, "Key Error, blah"):
b = BadModule()
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe just invoke torch.jit.script(BadModule()) here to show that it fails at compilation and not runtime

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@eellison what do you mean by using ghstack? i'm only vaguely familiar with that tool, not sure how to use it

Copy link
Contributor

Choose a reason for hiding this comment

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

https://github.com/ezyang/ghstack

If you search internally there should be a good number of docs on how to use it.

return getSugaredDict(loc, m)->getModules()->getitem(loc, m, idx);
} else if (
concreteType_->getIterableModuleKind() == IterableModuleKind::DICT) {
if (idx->type()->kind() == c10::TypeKind::StringType) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: maybe more idiomatic to do type->cast<StringType>(), maybe put toIValue(idx) in conditional to reduce nesting

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yup. actually, i think i can totally drop the StringType check, as toIValue does a type() == StringType inside, and will return none otherwise. see latest rev once i push.

auto key = keys_iter->tup_.at(i);
auto key_str = toIValue(key->asValue(loc, m))->toStringRef();
if (key_str == idx_str) {
std::shared_ptr<SugaredValue> module_sugared_value =
Copy link
Contributor

Choose a reason for hiding this comment

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

since we're returning SugaredValuePtr here, there's no need to std::dynamic_pointer_cast< ModuleValue>., can just return module_values_iter->tup_.at(i)

test/test_jit.py Outdated
with self.assertRaisesRegex(RuntimeError, "'int' object is not iterable"):
M()

def test_module_interface_special_methods(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

With this test, and test_sequential_intermediary_types, test_moduledict, test_custom_container_forward, test_script_module_list_sequential, test_script_modulelist_index, it's probably worth splitting this off to a separate test file: test/jit/test_module_containers

concrete_type_store.methods_compiled.add(concrete_type)

# Special handling so methods like __len__ work in script methods on classes derived from containers
if isinstance(nn_module, (torch.nn.ModuleList, torch.nn.Sequential, torch.nn.ModuleDict)) and \
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe worth checking that the module doesn't override the ipmlementation in the ModuleList/Sequential/ModuleDict

Copy link
Contributor Author

Choose a reason for hiding this comment

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

what do you mean? i think i actually handled this case correctly, although implicitly, by doing this block of code right after the '# Compile methods if necessary' block. IIUC, if they override len it gets compiled in that block, then my block sees len exist and bails. I did confirm this in one particular test case which overrides len.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think of someone overrides len but doesn’t add @export to it, it won’t be present on the cpp module when we do the check on the cpp module and we’ll still do the custom compilation

@wconstab
Copy link
Contributor Author

wconstab commented Jul 1, 2020

cool i'll make those changes. thanks!

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@wconstab has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@wconstab has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@wconstab has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@wconstab merged this pull request in 8ecd4f3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

4 participants