-
Notifications
You must be signed in to change notification settings - Fork 26.3k
script::Module as a view. #21814
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
script::Module as a view. #21814
Conversation
This moves all the module description logic (i.e. what members of the class are parameters) into the ClassType for the module itself. This allows us to remove all state in script::Module, making it a stateless view of the class.
script::Module as a view. This moves all the module description logic (i.e. what members of the class are parameters) into the ClassType for the module itself. This allows us to remove all state in script::Module, making it a stateless view of the class. gh-metadata: pytorch pytorch 21814 gh/zdevito/65/head
script::Module as a view. This moves all the module description logic (i.e. what members of the class are parameters) into the ClassType for the module itself. This allows us to remove all state in script::Module, making it a stateless view of the class. gh-metadata: pytorch pytorch 21814 gh/zdevito/65/head
script::Module as a view. This moves all the module description logic (i.e. what members of the class are parameters) into the ClassType for the module itself. This allows us to remove all state in script::Module, making it a stateless view of the class. gh-metadata: pytorch pytorch 21814 gh/zdevito/65/head
script::Module as a view. This moves all the module description logic (i.e. what members of the class are parameters) into the ClassType for the module itself. This allows us to remove all state in script::Module, making it a stateless view of the class. gh-metadata: pytorch pytorch 21814 gh/zdevito/65/head
torch/csrc/jit/script/module.h
Outdated
| } | ||
| at::optional<size_t> find_offset( | ||
|
|
||
| void get_or_add_slot( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems more like set_or_add_slot
| name, module->type(), module->module_object(), EntityType::MODULE); | ||
| } | ||
|
|
||
| void set_parameter(const std::string& name, at::Tensor v) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(future PR) we have a set_parameter but no set_buffer/set_attribute. Probably worth adding
| return get_method("forward")(std::move(inputs)); | ||
| } | ||
|
|
||
| void register_buffer(const std::string& name, autograd::Variable v) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May be worth dropping a note here about why buffers are treated as attributes
torch/csrc/jit/script/module.h
Outdated
| } | ||
| c10::ArrayRef<Slot> get_attributes() const { | ||
| return attributes_; | ||
| std::vector<std::shared_ptr<Module>> get_modules() const { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think is makes sense for these to be shared_ptr anymore, since Module is a value type anyway
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, but this is a big breaking API change for anyone using this API. Any call like foo.get_submodule("bar")->is_training() will need to change because the thing being returned is no longer a pointer. We should do it but it will need to be in a follow up PR to minimize the amount of API changes this one causes.
| return nullptr; | ||
| return c10::nullopt; | ||
| } | ||
| std::shared_ptr<Module> find_module(const std::string& name) const { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here
torch/csrc/jit/script/init.cpp
Outdated
| "_has_attribute", | ||
| [](Module& self, const std::string& name) -> bool { | ||
| return self.find_attribute(name); | ||
| return !!self.find_attribute(name); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
has_value?
script::Module as a view. This moves all the module description logic (i.e. what members of the class are parameters) into the ClassType for the module itself. This allows us to remove all state in script::Module, making it a stateless view of the class. We've changed the implementation of iterating over parameters/attributes/modules many times, and each time we've had to slightly break backward compatibility. This also changes the api to use a class (slot_list/iterator) to encapsulate iteration of parameters/attributes/modules. This should allow us to make implementation changes without affecting the user API. gh-metadata: pytorch pytorch 21814 gh/zdevito/65/head
script::Module as a view. This moves all the module description logic (i.e. what members of the class are parameters) into the ClassType for the module itself. This allows us to remove all state in script::Module, making it a stateless view of the class. We've changed the implementation of iterating over parameters/attributes/modules many times, and each time we've had to slightly break backward compatibility. This also changes the api to use a class (slot_list/iterator) to encapsulate iteration of parameters/attributes/modules. This should allow us to make implementation changes without affecting the user API. gh-metadata: pytorch pytorch 21814 gh/zdevito/65/head
script::Module as a view. This moves all the module description logic (i.e. what members of the class are parameters) into the ClassType for the module itself. This allows us to remove all state in script::Module, making it a stateless view of the class. We've changed the implementation of iterating over parameters/attributes/modules many times, and each time we've had to slightly break backward compatibility. This also changes the api to use a class (slot_list/iterator) to encapsulate iteration of parameters/attributes/modules. This should allow us to make implementation changes without affecting the user API. gh-metadata: pytorch pytorch 21814 gh/zdevito/65/head
script::Module as a view. This moves all the module description logic (i.e. what members of the class are parameters) into the ClassType for the module itself. This allows us to remove all state in script::Module, making it a stateless view of the class. We've changed the implementation of iterating over parameters/attributes/modules many times, and each time we've had to slightly break backward compatibility. This also changes the api to use a class (slot_list/iterator) to encapsulate iteration of parameters/attributes/modules. This should allow us to make implementation changes without affecting the user API. gh-metadata: pytorch pytorch 21814 gh/zdevito/65/head
script::Module as a view. This moves all the module description logic (i.e. what members of the class are parameters) into the ClassType for the module itself. This allows us to remove all state in script::Module, making it a stateless view of the class. We've changed the implementation of iterating over parameters/attributes/modules many times, and each time we've had to slightly break backward compatibility. This also changes the api to use a class (slot_list/iterator) to encapsulate iteration of parameters/attributes/modules. This should allow us to make implementation changes without affecting the user API. gh-metadata: pytorch pytorch 21814 gh/zdevito/65/head
script::Module as a view. This moves all the module description logic (i.e. what members of the class are parameters) into the ClassType for the module itself. This allows us to remove all state in script::Module, making it a stateless view of the class. We've changed the implementation of iterating over parameters/attributes/modules many times, and each time we've had to slightly break backward compatibility. This also changes the api to use a class (slot_list/iterator) to encapsulate iteration of parameters/attributes/modules. This should allow us to make implementation changes without affecting the user API. gh-metadata: pytorch pytorch 21814 gh/zdevito/65/head
Summary: Pull Request resolved: pytorch/pytorch#21814 ghimport-source-id: 49cfea6101ad9ca438600c465762e23252e05ff3 Test Plan: Imported from OSS Differential Revision: D15839583 Pulled By: zdevito fbshipit-source-id: ab4ef31a523b3ac1477aa7e6d4d9513e7408c560
Stack from ghstack:
This moves all the module description logic (i.e. what members
of the class are parameters) into the ClassType for the module
itself. This allows us to remove all state in script::Module,
making it a stateless view of the class.
Differential Revision: D15839583