Skip to content

Cleanup torch::jit::script::Module API for accessing attributes/parameters/submodules.#27260

Closed
ZolotukhinM wants to merge 21 commits intogh/ZolotukhinM/128/basefrom
gh/ZolotukhinM/128/head
Closed

Cleanup torch::jit::script::Module API for accessing attributes/parameters/submodules.#27260
ZolotukhinM wants to merge 21 commits intogh/ZolotukhinM/128/basefrom
gh/ZolotukhinM/128/head

Conversation

@ZolotukhinM
Copy link

@ZolotukhinM ZolotukhinM commented Oct 2, 2019

Stack from ghstack:

This PR has the following changes:

  • Slot class is removed. In all use cases except lower_graph we really
    just needed the attribute name and thus having an extra layer of
    abstraction through Slot only made the code harder to understand.
  • get_parameters, get_attributes, get_modules, and get_slots now return
    a list of <name, item> pairs instead of a list of Slots.

Differential Revision: D17728910

Implement them directly over module-object methods and not use Slots
when possible to avoid extra layer of abstraction and make the code more
explicit.

[ghstack-poisoned]
@pytorchbot pytorchbot added oncall: jit Add this issue/PR to JIT oncall triage queue module: cpp Related to C++ API module: internals Related to internal abstractions in c10 and ATen module: pybind Related to our Python bindings / interactions with other Python libraries labels Oct 2, 2019
ZolotukhinM pushed a commit that referenced this pull request Oct 2, 2019
Implement them directly over module-object methods and not use Slots
when possible to avoid extra layer of abstraction and make the code more
explicit.

ghstack-source-id: 4b2d42f
Pull Request resolved: #27260
@ZolotukhinM ZolotukhinM requested review from suo and zdevito October 2, 2019 23:47
Implement them directly over module-object methods and not use Slots
when possible to avoid extra layer of abstraction and make the code more
explicit.

Differential Revision: [D17728910](https://our.internmc.facebook.com/intern/diff/D17728910)

[ghstack-poisoned]
ZolotukhinM pushed a commit that referenced this pull request Oct 3, 2019
Implement them directly over module-object methods and not use Slots
when possible to avoid extra layer of abstraction and make the code more
explicit.

ghstack-source-id: 216f482
Pull Request resolved: #27260
Copy link
Contributor

@zdevito zdevito left a comment

Choose a reason for hiding this comment

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

I have mixed feelings about this because it removes Slot, which was a useful abstraction for getting an LValue for parts of a class/module. I understand it is weird to have them only for modules, but I would hope just promote them into something that works on class objects as well rather than delete them. Eventually, we might want something like my_class.attr("foo").set(ivalue) to work for users.

@ZolotukhinM
Copy link
Author

Yeah, I actually felt that it was in a half-finished state too, but was interested in the feedback anyway. Let me try to do something to the API of the three classes to see if we can find a better approach.

My main motivation to kill Slot was that it creates an extra layer that actually could be replaced with just functions making the code easier to comprehend. When I first read the code it seemed confusing to me to have both attributes, whose "indexes" are strings, and slots, whose indexes are integers. All in all, it seems that there is a duplicated functionality there and that's what I'm trying to cleanup.

@ZolotukhinM ZolotukhinM changed the title Refactor torch::jit::script::Module::find_ methods. Cleanup torch::jit::script::Module API for accessing attributes/parameters/submodules. Oct 4, 2019
…butes/parameters/submodules."

This PR has the following changes:
- Slot class is removed. In all use cases except `lower_graph` we really
just needed the attribute name and thus having an extra layer of
abstraction through Slot only made the code harder to understand.
- get_parameters, get_attributes, get_modules, and get_slots now return
a list of <name, item> pairs instead of a list of Slots.

Differential Revision: [D17728910](https://our.internmc.facebook.com/intern/diff/D17728910)

[ghstack-poisoned]
@ZolotukhinM ZolotukhinM requested a review from zdevito October 4, 2019 21:33
@zdevito
Copy link
Contributor

zdevito commented Oct 4, 2019

There is a real distinction between functions that work on the Type (e.g. get me the slot index for "foo") and those that work on the object (get me the value of "foo"). So they cannot be removed entirely. But I do think we could do a better job making their APIs consistent so the distinction is clear.

@ZolotukhinM
Copy link
Author

There is a real distinction between functions that work on the Type (e.g. get me the slot index for "foo") and those that work on the object (get me the value of "foo").

That's exactly why I wanted to get rid of Slot which makes you to have both. In all except one use-cases the function worked either on the type or on the object (not both), and thus there really was no need in having Slot around - we could directly work on the type or on the object. The only exception is lower_graph and frankly I think it now needs to be factored out from the Module API into a stand-alone path.

…butes/parameters/submodules."

This PR has the following changes:
- Slot class is removed. In all use cases except `lower_graph` we really
just needed the attribute name and thus having an extra layer of
abstraction through Slot only made the code harder to understand.
- get_parameters, get_attributes, get_modules, and get_slots now return
a list of <name, item> pairs instead of a list of Slots.

Differential Revision: [D17728910](https://our.internmc.facebook.com/intern/diff/D17728910)

[ghstack-poisoned]
ZolotukhinM pushed a commit that referenced this pull request Oct 4, 2019
…eters/submodules.

This PR has the following changes:
- Slot class is removed. In all use cases except `lower_graph` we really
just needed the attribute name and thus having an extra layer of
abstraction through Slot only made the code harder to understand.
- get_parameters, get_attributes, get_modules, and get_slots now return
a list of <name, item> pairs instead of a list of Slots.

ghstack-source-id: 18bba0b
Pull Request resolved: #27260
…butes/parameters/submodules."

This PR has the following changes:
- Slot class is removed. In all use cases except `lower_graph` we really
just needed the attribute name and thus having an extra layer of
abstraction through Slot only made the code harder to understand.
- get_parameters, get_attributes, get_modules, and get_slots now return
a list of <name, item> pairs instead of a list of Slots.

Differential Revision: [D17728910](https://our.internmc.facebook.com/intern/diff/D17728910)

[ghstack-poisoned]
ZolotukhinM pushed a commit that referenced this pull request Oct 5, 2019
…eters/submodules.

This PR has the following changes:
- Slot class is removed. In all use cases except `lower_graph` we really
just needed the attribute name and thus having an extra layer of
abstraction through Slot only made the code harder to understand.
- get_parameters, get_attributes, get_modules, and get_slots now return
a list of <name, item> pairs instead of a list of Slots.

ghstack-source-id: 5529d30
Pull Request resolved: #27260
…butes/parameters/submodules."

This PR has the following changes:
- Slot class is removed. In all use cases except `lower_graph` we really
just needed the attribute name and thus having an extra layer of
abstraction through Slot only made the code harder to understand.
- get_parameters, get_attributes, get_modules, and get_slots now return
a list of <name, item> pairs instead of a list of Slots.

Differential Revision: [D17728910](https://our.internmc.facebook.com/intern/diff/D17728910)

[ghstack-poisoned]
ZolotukhinM pushed a commit that referenced this pull request Oct 8, 2019
…eters/submodules.

This PR has the following changes:
- Slot class is removed. In all use cases except `lower_graph` we really
just needed the attribute name and thus having an extra layer of
abstraction through Slot only made the code harder to understand.
- get_parameters, get_attributes, get_modules, and get_slots now return
a list of <name, item> pairs instead of a list of Slots.

ghstack-source-id: d40f0a2
Pull Request resolved: #27260
…butes/parameters/submodules."

This PR has the following changes:
- Slot class is removed. In all use cases except `lower_graph` we really
just needed the attribute name and thus having an extra layer of
abstraction through Slot only made the code harder to understand.
- get_parameters, get_attributes, get_modules, and get_slots now return
a list of <name, item> pairs instead of a list of Slots.

Differential Revision: [D17728910](https://our.internmc.facebook.com/intern/diff/D17728910)

[ghstack-poisoned]
Copy link
Member

@suo suo left a comment

Choose a reason for hiding this comment

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

Looks good overall, left some comments inline.

if (fn->name() == basename) {
return Method(module_object(), fn);
}
c10::optional<size_t> find_parameter(const std::string& name) const;
Copy link
Member

Choose a reason for hiding this comment

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

I don't like this change; it complicates to a common operation (looking up stuff on a module), and it seems like if get_x returns T, find_x should return optional<T> for least surprise.

size_t num_slots() const {
return module_object()->slots().size();
}
c10::optional<EntityType> entity_type(const std::string& name) const {
Copy link
Member

Choose a reason for hiding this comment

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

Should these two methods be private? EntityType shouldn't be a public concept I think

Copy link
Author

Choose a reason for hiding this comment

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

Unfortunately, it's now used in slot_iterator_impl, so I had to keep it public. Do you mind if I take a look at how this can be cleaned-up in a follow-up?

…butes/parameters/submodules."

This PR has the following changes:
- Slot class is removed. In all use cases except `lower_graph` we really
just needed the attribute name and thus having an extra layer of
abstraction through Slot only made the code harder to understand.
- get_parameters, get_attributes, get_modules, and get_slots now return
a list of <name, item> pairs instead of a list of Slots.

Differential Revision: [D17728910](https://our.internmc.facebook.com/intern/diff/D17728910)

[ghstack-poisoned]
…butes/parameters/submodules."

This PR has the following changes:
- Slot class is removed. In all use cases except `lower_graph` we really
just needed the attribute name and thus having an extra layer of
abstraction through Slot only made the code harder to understand.
- get_parameters, get_attributes, get_modules, and get_slots now return
a list of <name, item> pairs instead of a list of Slots.

Differential Revision: [D17728910](https://our.internmc.facebook.com/intern/diff/D17728910)

[ghstack-poisoned]
Copy link
Author

@ZolotukhinM ZolotukhinM left a comment

Choose a reason for hiding this comment

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

Updated, please take another look!

size_t num_slots() const {
return module_object()->slots().size();
}
c10::optional<EntityType> entity_type(const std::string& name) const {
Copy link
Author

Choose a reason for hiding this comment

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

Unfortunately, it's now used in slot_iterator_impl, so I had to keep it public. Do you mind if I take a look at how this can be cleaned-up in a follow-up?

@ZolotukhinM ZolotukhinM requested a review from suo October 15, 2019 00:04
…butes/parameters/submodules."

This PR has the following changes:
- Slot class is removed. In all use cases except `lower_graph` we really
just needed the attribute name and thus having an extra layer of
abstraction through Slot only made the code harder to understand.
- get_parameters, get_attributes, get_modules, and get_slots now return
a list of <name, item> pairs instead of a list of Slots.

Differential Revision: [D17728910](https://our.internmc.facebook.com/intern/diff/D17728910)

[ghstack-poisoned]
ZolotukhinM pushed a commit that referenced this pull request Oct 15, 2019
…eters/submodules.

Pull Request resolved: #27260

This PR has the following changes:
- Slot class is removed. In all use cases except `lower_graph` we really
just needed the attribute name and thus having an extra layer of
abstraction through Slot only made the code harder to understand.
- get_parameters, get_attributes, get_modules, and get_slots now return
a list of <name, item> pairs instead of a list of Slots.

Differential Revision: [D17728910](https://our.internmc.facebook.com/intern/diff/D17728910/)
ghstack-source-id: 2a65af2
struct slot_list_impl;
using slot_list = slot_list_impl<Slot>;
using module_list = slot_list_impl<Module>;
using module_list = slot_list_impl<std::pair<std::string, Module>>;
Copy link
Member

Choose a reason for hiding this comment

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

My comment here got eaten somehow: this is a readability regression, as the fields name and value have replaced by first and second. I don't think it's a huge deal since first and second are STL convention, but it could be worth retaining a small struct for naming here.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree, but do think this is a big regression in understandability of the API. I think use of std::pair and std::tuple are almost always wrong. We should at the very least have a struct with names and not replace a bunch of types with auto in the code.

Copy link
Contributor

@zdevito zdevito left a comment

Choose a reason for hiding this comment

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

I think this change is fine as long as we replace the std::pair with a struct.

map[module.module_object()] = qconfig;

for (script::Slot s : module.get_module_slots()) {
for (auto p : module.get_modules()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This code is less understandable than before. It replaced a named type with auto, and replaced informative function names with first and second.

Copy link
Author

Choose a reason for hiding this comment

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

This code is less understandable than before. It replaced a named type with auto, and replaced informative function names with first and second.

Sure, I'll replace pairs with structs.

But that said, I am not completely sold on "code with pairs is less readable than code with a custom arbitrary type". Take into account that you are already familiar with the code, and you know what Slot is - that's why that version is more readable to you. However, for new people (e.g. for me) code with .first and .second is much easier to understand because I immediately know what it means in contrast to a code that uses Slot with 10 methods to operate on its state.

Having a simple structure with no methods seems to be a good choice in the middle though.

for (auto submod : get_modules()) {
submod.apply(fn);
for (auto p : get_modules()) {
p.second.apply(fn);
Copy link
Contributor

Choose a reason for hiding this comment

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

What happened?

struct slot_list_impl;
using slot_list = slot_list_impl<Slot>;
using module_list = slot_list_impl<Module>;
using module_list = slot_list_impl<std::pair<std::string, Module>>;
Copy link
Contributor

Choose a reason for hiding this comment

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

I agree, but do think this is a big regression in understandability of the API. I think use of std::pair and std::tuple are almost always wrong. We should at the very least have a struct with names and not replace a bunch of types with auto in the code.

…butes/parameters/submodules."

This PR has the following changes:
- Slot class is removed. In all use cases except `lower_graph` we really
just needed the attribute name and thus having an extra layer of
abstraction through Slot only made the code harder to understand.
- get_parameters, get_attributes, get_modules, and get_slots now return
a list of <name, item> pairs instead of a list of Slots.

Differential Revision: [D17728910](https://our.internmc.facebook.com/intern/diff/D17728910)

[ghstack-poisoned]
ZolotukhinM pushed a commit that referenced this pull request Oct 15, 2019
…eters/submodules.

Pull Request resolved: #27260

This PR has the following changes:
- Slot class is removed. In all use cases except `lower_graph` we really
just needed the attribute name and thus having an extra layer of
abstraction through Slot only made the code harder to understand.
- get_parameters, get_attributes, get_modules, and get_slots now return
a list of <name, item> pairs instead of a list of Slots.

Differential Revision: [D17728910](https://our.internmc.facebook.com/intern/diff/D17728910/)
ghstack-source-id: d40a3d5
…butes/parameters/submodules."

This PR has the following changes:
- Slot class is removed. In all use cases except `lower_graph` we really
just needed the attribute name and thus having an extra layer of
abstraction through Slot only made the code harder to understand.
- get_parameters, get_attributes, get_modules, and get_slots now return
a list of <name, item> pairs instead of a list of Slots.

Differential Revision: [D17728910](https://our.internmc.facebook.com/intern/diff/D17728910)

[ghstack-poisoned]
…butes/parameters/submodules."

This PR has the following changes:
- Slot class is removed. In all use cases except `lower_graph` we really
just needed the attribute name and thus having an extra layer of
abstraction through Slot only made the code harder to understand.
- get_parameters, get_attributes, get_modules, and get_slots now return
a list of <name, item> pairs instead of a list of Slots.

Differential Revision: [D17728910](https://our.internmc.facebook.com/intern/diff/D17728910)

[ghstack-poisoned]
ZolotukhinM pushed a commit that referenced this pull request Oct 15, 2019
…eters/submodules.

Pull Request resolved: #27260


This PR has the following changes:
- Slot class is removed. In all use cases except `lower_graph` we really
just needed the attribute name and thus having an extra layer of
abstraction through Slot only made the code harder to understand.
- get_parameters, get_attributes, get_modules, and get_slots now return
a list of <name, item> pairs instead of a list of Slots.

Differential Revision: [D17728910](https://our.internmc.facebook.com/intern/diff/D17728910/)
ghstack-source-id: 91969875
…butes/parameters/submodules."

This PR has the following changes:
- Slot class is removed. In all use cases except `lower_graph` we really
just needed the attribute name and thus having an extra layer of
abstraction through Slot only made the code harder to understand.
- get_parameters, get_attributes, get_modules, and get_slots now return
a list of <name, item> pairs instead of a list of Slots.

Differential Revision: [D17728910](https://our.internmc.facebook.com/intern/diff/D17728910)

[ghstack-poisoned]
ZolotukhinM pushed a commit that referenced this pull request Oct 16, 2019
…eters/submodules.

Pull Request resolved: #27260

This PR has the following changes:
- Slot class is removed. In all use cases except `lower_graph` we really
just needed the attribute name and thus having an extra layer of
abstraction through Slot only made the code harder to understand.
- get_parameters, get_attributes, get_modules, and get_slots now return
a list of <name, item> pairs instead of a list of Slots.

Differential Revision: [D17728910](https://our.internmc.facebook.com/intern/diff/D17728910/)
ghstack-source-id: 907ecb9
…butes/parameters/submodules."

This PR has the following changes:
- Slot class is removed. In all use cases except `lower_graph` we really
just needed the attribute name and thus having an extra layer of
abstraction through Slot only made the code harder to understand.
- get_parameters, get_attributes, get_modules, and get_slots now return
a list of <name, item> pairs instead of a list of Slots.

Differential Revision: [D17728910](https://our.internmc.facebook.com/intern/diff/D17728910)

[ghstack-poisoned]
ZolotukhinM pushed a commit that referenced this pull request Oct 16, 2019
…eters/submodules.

Pull Request resolved: #27260

This PR has the following changes:
- Slot class is removed. In all use cases except `lower_graph` we really
just needed the attribute name and thus having an extra layer of
abstraction through Slot only made the code harder to understand.
- get_parameters, get_attributes, get_modules, and get_slots now return
a list of <name, item> pairs instead of a list of Slots.

Differential Revision: [D17728910](https://our.internmc.facebook.com/intern/diff/D17728910/)
ghstack-source-id: d1d2494
zdevito pushed a commit to zdevito/ATen that referenced this pull request Oct 17, 2019
…eters/submodules. (#27260)

Summary:
Pull Request resolved: pytorch/pytorch#27260

This PR has the following changes:
- Slot class is removed. In all use cases except `lower_graph` we really
just needed the attribute name and thus having an extra layer of
abstraction through Slot only made the code harder to understand.
- get_parameters, get_attributes, get_modules, and get_slots now return
a list of <name, item> pairs instead of a list of Slots.

Differential Revision: D17728910

Test Plan: Imported from OSS

Pulled By: ZolotukhinM

fbshipit-source-id: 94781611752dd88e7fddfe8b8e0252d6ec32ba68
@facebook-github-bot
Copy link
Contributor

@ZolotukhinM merged this pull request in 2265cdd.

@facebook-github-bot facebook-github-bot deleted the gh/ZolotukhinM/128/head branch October 28, 2019 22:07
thiagocrepaldi pushed a commit to thiagocrepaldi/pytorch that referenced this pull request Feb 4, 2020
…eters/submodules. (pytorch#27260)

Summary:
Pull Request resolved: pytorch#27260

This PR has the following changes:
- Slot class is removed. In all use cases except `lower_graph` we really
just needed the attribute name and thus having an extra layer of
abstraction through Slot only made the code harder to understand.
- get_parameters, get_attributes, get_modules, and get_slots now return
a list of <name, item> pairs instead of a list of Slots.

Differential Revision: D17728910

Test Plan: Imported from OSS

Pulled By: ZolotukhinM

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

Labels

Merged module: cpp Related to C++ API module: custom-operators custom operators, custom ops, custom-operators, custom-ops module: internals Related to internal abstractions in c10 and ATen module: pybind Related to our Python bindings / interactions with other Python libraries oncall: jit Add this issue/PR to JIT oncall triage queue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants