Skip to content
Closed
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
22 changes: 22 additions & 0 deletions torch/csrc/jit/mobile/import.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <ATen/core/ivalue.h>
#include <caffe2/serialize/inline_container.h>
#include <torch/csrc/jit/api/compilation_unit.h>
#include <torch/csrc/jit/mobile/interpreter.h>
#include <torch/csrc/jit/mobile/observer.h>
#include <torch/csrc/jit/runtime/instruction.h>
#include <torch/csrc/jit/serialization/import_export_constants.h>
Expand Down Expand Up @@ -677,5 +678,26 @@ void _load_extra_only_for_mobile(
deserializer.deserialize_only_extra(device, extra_files);
}

namespace mobile {

std::set<std::string> _export_operator_list(
torch::jit::mobile::Module& module) {
std::set<std::string> operator_list;
for (Method func : module.get_methods()) {
const Function& function = func.function();
const std::shared_ptr<Code> cptr = function.get_code();
// op_names below isn't a list of unique operator names. In fact
// it can contain the same operator name many many times, so we need
// to de-dup the list by adding all the operator names into
// an std::set<std::string>.
std::vector<c10::OperatorName> const& op_names = cptr->op_names_;
for (auto& op_name : op_names) {
operator_list.insert(toString(op_name));
}
}
return operator_list;
}

} // namespace mobile
} // namespace jit
} // namespace torch
17 changes: 17 additions & 0 deletions torch/csrc/jit/mobile/import.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,22 @@ void _load_extra_only_for_mobile(
const std::string& filename,
c10::optional<at::Device> device,
ExtraFilesMap& extra_files);

namespace mobile {

/**
* Given a torch::jit::mobile::Module, return a set of operator names
* (with overload name) that are used by any method in this mobile
* Mobile. This method runs through the bytecode for all methods
* in the specified model (module), and extracts all the root
* operator names. Root operators are operators that are called
* directly by the model (as opposed to non-root operators, which
* may be called transitively by the root operators).
*
*/
TORCH_API std::set<std::string> _export_operator_list(
torch::jit::mobile::Module& module);

} // namespace mobile
} // namespace jit
} // namespace torch
17 changes: 0 additions & 17 deletions torch/csrc/jit/serialization/export.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,22 +116,5 @@ TORCH_API void SetExportModuleMobileInfoConverter(
*/
TORCH_API std::vector<std::string> export_opnames(const Module& m);

namespace mobile {

class Module;
/**
* Given a torch::jit::mobile::Module, return a set of operator names
* (with overload name) that are used by any method in this mobile
* Mobile. This method runs through the bytecode for all methods
* in the specified model (module), and extracts all the root
* operator names. Root operators are operators that are called
* directly by the model (as opposed to non-root operators, which
* may be called transitively by the root operators).
*
*/
TORCH_API std::set<std::string> _export_operator_list(
torch::jit::mobile::Module& module);

} // namespace mobile
} // namespace jit
} // namespace torch
21 changes: 0 additions & 21 deletions torch/csrc/jit/serialization/export_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -658,26 +658,5 @@ std::vector<std::string> export_opnames(const script::Module& m) {
return std::vector<std::string>(names.begin(), names.end());
}

namespace mobile {

std::set<std::string> _export_operator_list(
torch::jit::mobile::Module& module) {
std::set<std::string> operator_list;
for (Method func : module.get_methods()) {
const Function& function = func.function();
const std::shared_ptr<Code> cptr = function.get_code();
// op_names below isn't a list of unique operator names. In fact
// it can contain the same operator name many many times, so we need
// to de-dup the list by adding all the operator names into
// an std::set<std::string>.
std::vector<c10::OperatorName> const& op_names = cptr->op_names_;
for (auto& op_name : op_names) {
operator_list.insert(toString(op_name));
}
}
return operator_list;
}

} // namespace mobile
} // namespace jit
} // namespace torch