From 52346aee23c8d7250cd0d59620e4f73589caaeee Mon Sep 17 00:00:00 2001 From: zhenyanzhang Date: Tue, 22 Apr 2025 16:12:12 -0700 Subject: [PATCH] [ExecuTorch][#9638] Introduce Protected Method Getter in Extension.Module # Context This issue is a step of https://github.com/pytorch/executorch/discussions/9638. In the discussion, we want to unblock having `extension/Module` as the single source of implementation, which means that `pybindings/PyModule` should use `extension/Module` rather than its own. Although we are decouping method getter from `pybindings` implementation, method getter itself is still needed. To keep having the method getter while not exposing it, we can create a protected method getter and confine it's usage inside child classes that we are about to create. # Proposal Add a protected `get_method` to `extension.Module`, taking method name string as an input. Differential Revision: [D73473766](https://our.internmc.facebook.com/intern/diff/D73473766/) ghstack-source-id: 279669340 Pull Request resolved: https://github.com/pytorch/executorch/pull/10374 --- extension/module/module.cpp | 10 ++++++++++ extension/module/module.h | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/extension/module/module.cpp b/extension/module/module.cpp index 6c534b8d560..ec01323edc7 100644 --- a/extension/module/module.cpp +++ b/extension/module/module.cpp @@ -302,5 +302,15 @@ runtime::Error Module::set_output( output_tensor.mutable_data_ptr(), output_tensor.nbytes(), output_index); } +ET_NODISCARD inline runtime::Result Module::get_method( + 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 diff --git a/extension/module/module.h b/extension/module/module.h index 73c7328ee0a..8cedb79c06e 100644 --- a/extension/module/module.h +++ b/extension/module/module.h @@ -493,6 +493,16 @@ class Module { std::unique_ptr 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 get_method( + const std::string& method_name); std::unordered_map methods_; friend class ExecuTorchJni;