text
stringlengths
0
2.2M
#define REQUIRE_STRING_ARG(name) \
if (FLAGS_##name.empty()) { \
std::cerr << "You must specify the flag --" #name "\n"; \
return 1; \
}
#define REQUIRE_INT_ARG(name) \
if (FLAGS_##name == -1) { \
std::cerr << "You must specify the flag --" #name "\n"; \
return 1; \
}
void printOpYAML(
std::ostream& out,
int indent,
const std::string& op_name,
bool is_used_for_training,
bool is_root_operator,
bool include_all_overloads) {
out << std::string(indent, ' ') << op_name << ":" << std::endl;
out << std::string(indent + 2, ' ')
<< "is_used_for_training: " << (is_used_for_training ? "true" : "false")
<< std::endl;
out << std::string(indent + 2, ' ')
<< "is_root_operator: " << (is_root_operator ? "true" : "false")
<< std::endl;
out << std::string(indent + 2, ' ')
<< "include_all_overloads: " << (include_all_overloads ? "true" : "false")
<< std::endl;
}
void printOpsYAML(
std::ostream& out,
const std::set<std::string>& operator_list,
bool is_used_for_training,
bool is_root_operator,
bool include_all_overloads) {
for (auto& it : operator_list) {
printOpYAML(out, 2, it, false, is_root_operator, false);
}
}
/**
* Converts a pytorch model (full/lite) to lite interpreter model for
* mobile, and additionally writes out a list of root and called
* operators.
*/
int main(int argc, char* argv[]) {
if (!c10::ParseCommandLineFlags(&argc, &argv)) {
std::cerr << "Failed to parse command line flags!" << std::endl;
return 1;
}
REQUIRE_STRING_ARG(model_input_path);
REQUIRE_STRING_ARG(build_yaml_path);
const std::string input_module_path = FLAGS_model_input_path;
std::ofstream yaml_out(FLAGS_build_yaml_path);
std::cout << "Processing: " << input_module_path << std::endl;
std::cout << "Output: " << FLAGS_build_yaml_path << std::endl;
torch::jit::mobile::TracerResult tracer_result;
try {
tracer_result = torch::jit::mobile::trace_run(FLAGS_model_input_path);
} catch (std::exception& ex) {
std::cerr
<< "ModelTracer has not been able to load the module for the following reasons:\n"
<< ex.what()
<< "\nPlease consider posting to the PyTorch with the error message."
<< std::endl;
throw ex;
}
if (tracer_result.traced_operators.size() <=
torch::jit::mobile::always_included_traced_ops.size()) {
std::cerr
<< c10::str(
"Error traced_operators size: ",
tracer_result.traced_operators.size(),
". Expected the traced operator list to be bigger then the default size ",
torch::jit::mobile::always_included_traced_ops.size(),
". Please report a bug in PyTorch.")
<< std::endl;
}
// If the op exist in both traced_ops and root_ops, leave it in root_ops only
for (const auto& root_op : tracer_result.root_ops) {
if (tracer_result.traced_operators.find(root_op) !=
tracer_result.traced_operators.end()) {
tracer_result.traced_operators.erase(root_op);
}
}
yaml_out << "include_all_non_op_selectives: true" << std::endl;
yaml_out << "operators:" << std::endl;
printOpsYAML(
yaml_out,
tracer_result.root_ops,