From fb3b63438df7f89932c051fa09b2cf0872139129 Mon Sep 17 00:00:00 2001 From: Ashwini Khade Date: Tue, 23 Apr 2019 11:11:35 -0700 Subject: [PATCH] Add python api for graph optimization level (#882) --- .../python/onnxruntime_pybind_state.cc | 122 ++++++++++-------- 1 file changed, 65 insertions(+), 57 deletions(-) diff --git a/onnxruntime/python/onnxruntime_pybind_state.cc b/onnxruntime/python/onnxruntime_pybind_state.cc index 4347295653..644c361fa7 100644 --- a/onnxruntime/python/onnxruntime_pybind_state.cc +++ b/onnxruntime/python/onnxruntime_pybind_state.cc @@ -261,13 +261,13 @@ void addGlobalMethods(py::module& m) { #ifdef onnxruntime_PYBIND_EXPORT_OPSCHEMA m.def( - "get_all_operator_schema", + "get_all_operator_schema", []() -> const std::vector { return ONNX_NAMESPACE::OpSchemaRegistry::get_all_schemas_with_history(); }, "Return a vector of OpSchema all registed operators" ); -#endif +#endif } @@ -380,7 +380,18 @@ Set this option to false if you don't want it. Default is True.)pbdoc") R"pbdoc(Applies to session load, initialization, etc. Default is 0.)pbdoc") .def_readwrite("session_thread_pool_size", &SessionOptions::session_thread_pool_size, R"pbdoc(How many threads in the session thread pool. Default is 0 to let onnxruntime choose. -This parameter is unused unless *enable_sequential_execution* is false.)pbdoc"); +This parameter is unused unless *enable_sequential_execution* is false.)pbdoc") + .def_property_readonly("graph_optimization_level", + [](const SessionOptions* options) -> uint32_t { + return static_cast(options->graph_optimization_level); + }, + R"pbdoc(Graph optimization level for this session.)pbdoc") + .def("set_graph_optimization_level", + [](SessionOptions* options, uint32_t level) -> void { + options->graph_optimization_level = static_cast(level); + }, + R"pbdoc(Graph optimization level for this session. 0 disables all optimizations. +Whereas 1 enables basic optimizations and 2 enables all optimizations.)pbdoc"); py::class_(m, "RunOptions", R"pbdoc(Configuration information for a single Run.)pbdoc") .def(py::init()) @@ -410,52 +421,50 @@ including arg name, arg type (contains both type and shape).)pbdoc") return *(na.Type()); }, "node type") - .def( - "__str__", [](const onnxruntime::NodeArg& na) -> std::string { - std::ostringstream res; - res << "NodeArg(name='" << na.Name() << "', type='" << *(na.Type()) << "', shape="; - auto shape = na.Shape(); - std::vector arr; - if (shape == nullptr || shape->dim_size() == 0) { - res << "[]"; - } else { - res << "["; - for (int i = 0; i < shape->dim_size(); ++i) { - if (shape->dim(i).has_dim_value()) { - res << shape->dim(i).dim_value(); - } else if (shape->dim(i).has_dim_param()) { - res << "None"; - } - if (i < shape->dim_size() - 1) { - res << ", "; - } - } - res << "]"; + .def("__str__", [](const onnxruntime::NodeArg& na) -> std::string { + std::ostringstream res; + res << "NodeArg(name='" << na.Name() << "', type='" << *(na.Type()) << "', shape="; + auto shape = na.Shape(); + std::vector arr; + if (shape == nullptr || shape->dim_size() == 0) { + res << "[]"; + } else { + res << "["; + for (int i = 0; i < shape->dim_size(); ++i) { + if (shape->dim(i).has_dim_value()) { + res << shape->dim(i).dim_value(); + } else if (shape->dim(i).has_dim_param()) { + res << "None"; } - res << ")"; + if (i < shape->dim_size() - 1) { + res << ", "; + } + } + res << "]"; + } + res << ")"; - return std::string(res.str()); - }, - "converts the node into a readable string") - .def_property_readonly( - "shape", [](const onnxruntime::NodeArg& na) -> std::vector { - auto shape = na.Shape(); - std::vector arr; - if (shape == nullptr || shape->dim_size() == 0) { - return arr; - } + return std::string(res.str()); + }, + "converts the node into a readable string") + .def_property_readonly("shape", [](const onnxruntime::NodeArg& na) -> std::vector { + auto shape = na.Shape(); + std::vector arr; + if (shape == nullptr || shape->dim_size() == 0) { + return arr; + } - arr.resize(shape->dim_size()); - for (int i = 0; i < shape->dim_size(); ++i) { - if (shape->dim(i).has_dim_value()) { - arr[i] = py::cast(shape->dim(i).dim_value()); - } else if (shape->dim(i).has_dim_param()) { - arr[i] = py::none(); - } - } - return arr; - }, - "node shape (assuming the node holds a tensor)"); + arr.resize(shape->dim_size()); + for (int i = 0; i < shape->dim_size(); ++i) { + if (shape->dim(i).has_dim_value()) { + arr[i] = py::cast(shape->dim(i).dim_value()); + } else if (shape->dim(i).has_dim_param()) { + arr[i] = py::none(); + } + } + return arr; + }, + "node shape (assuming the node holds a tensor)"); py::class_(m, "SessionObjectInitializer"); py::class_(m, "InferenceSession", R"pbdoc(This is the main class used to run a model.)pbdoc") @@ -470,16 +479,15 @@ including arg name, arg type (contains both type and shape).)pbdoc") InitializeSession(sess); }, R"pbdoc(Load a model saved in ONNX format.)pbdoc") - .def( - "read_bytes", [](InferenceSession* sess, const py::bytes& serializedModel) { - std::istringstream buffer(serializedModel); - auto status = sess->Load(buffer); - if (!status.IsOK()) { - throw std::runtime_error(status.ToString().c_str()); - } - InitializeSession(sess); - }, - R"pbdoc(Load a model serialized in ONNX format.)pbdoc") + .def("read_bytes", [](InferenceSession* sess, const py::bytes& serializedModel) { + std::istringstream buffer(serializedModel); + auto status = sess->Load(buffer); + if (!status.IsOK()) { + throw std::runtime_error(status.ToString().c_str()); + } + InitializeSession(sess); + }, + R"pbdoc(Load a model serialized in ONNX format.)pbdoc") .def("run", [](InferenceSession* sess, std::vector output_names, std::map pyfeeds, RunOptions* run_options = nullptr) -> std::vector { NameMLValMap feeds; for (auto _ : pyfeeds) { @@ -585,7 +593,7 @@ PYBIND11_MODULE(onnxruntime_pybind11_state, m) { #ifdef onnxruntime_PYBIND_EXPORT_OPSCHEMA addOpSchemaSubmodule(m); #endif - + } } // namespace python