Skip to content

[ExecuTorch][#9638] Introduce Protected Method Getter in Extension.Module #10384

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

Merged
merged 1 commit into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions extension/module/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,5 +302,15 @@ runtime::Error Module::set_output(
output_tensor.mutable_data_ptr(), output_tensor.nbytes(), output_index);
}

ET_NODISCARD inline runtime::Result<Method*> Module::get_method(
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need to expose the Method? Can we make some ivars protected instead and let subclasses work with them directly, like this class itself?
Also, there's no get_-prefix naming convention for this class, it should be named just "method()", if really needed.

Copy link
Contributor

@zhenyan-zhang-meta zhenyan-zhang-meta Apr 24, 2025

Choose a reason for hiding this comment

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

Method is needed for a child class. I think I can move the function to the child class since the methods_ is already protected and the parent class is not using the function I introduced.

Copy link
Contributor

Choose a reason for hiding this comment

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

@shoumikhin that should my fault i misread the scope of method list as private so I asked to put the function here.
Since it is protected, making it as a child’s private function should be a better choice.

Copy link
Contributor

Choose a reason for hiding this comment

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

Now worries, you should be able to access Method via methods_.at(method_name).method in subclasses indeed.

const std::string& method_name) {
ET_CHECK_OR_RETURN_ERROR(
methods_.count(method_name) > 0,
InvalidArgument,
"no such method in program: %s",
method_name.c_str());
return methods_[method_name].method.get();
}

} // namespace extension
} // namespace executorch
10 changes: 10 additions & 0 deletions extension/module/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,16 @@ class Module {
std::unique_ptr<NamedDataMap> data_map_;

protected:
/**
* Get a method by method name.
*
* @param[in] method_name The name of the method to get.
*
* @returns A Result object containing either a pointer to the requested
* method or an error to indicate failure.
*/
ET_NODISCARD inline runtime::Result<Method*> get_method(
const std::string& method_name);
std::unordered_map<std::string, MethodHolder> methods_;

friend class ExecuTorchJni;
Expand Down
Loading