Skip to content

Commit

Permalink
Merge pull request #2 from nosovmik/fe_pythonbindings
Browse files Browse the repository at this point in the history
Python bindings for P1 methods of frontend API
  • Loading branch information
nosovmik authored Apr 14, 2021
2 parents 30f65f1 + b7a8a8e commit 0778c7e
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 40 deletions.
4 changes: 3 additions & 1 deletion inference-engine/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,7 @@ add_custom_target(ie_libraries ALL
inference_engine_lp_transformations inference_engine_snippets)

if(NGRAPH_ONNX_IMPORT_ENABLE)
add_dependencies(ie_libraries inference_engine_onnx_reader frontend_manager)
add_dependencies(ie_libraries inference_engine_onnx_reader)
endif()

add_dependencies(ie_libraries frontend_manager)
8 changes: 6 additions & 2 deletions model-optimizer/mo/pipeline/unified.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ def moc_pipeline(argv: argparse.Namespace):
fe = fem.loadByFramework(argv.framework)
print(fe)
inputModel = fe.loadFromFile(argv.input_model)


try:
place = inputModel.getPlaceByTensorName('x')
print(place)
print(place.isEqual(None))
except Exception:
log.exception('Failed to call model API with hardcoded input name "x"')
# Wrap nGraph network to Graph for smoothly pass through the legacy code in MO.
# This trick doesn't mean that we will hold Graph forever as a wrapper, it is derived from
# NX graph and this is not required. But Graph has several methods that can be implemented for nGraph
Expand Down
2 changes: 1 addition & 1 deletion model-optimizer/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ tensorflow>=1.15.2,<2.0; python_version < "3.8"
tensorflow>=2.2; python_version >= "3.8"
mxnet>=1.0.0,<=1.7.0
networkx>=1.11
numpy>=1.14.0,<1.19.0
numpy>=1.14.0,<=1.19.4
protobuf>=3.6.1
onnx>=1.1.2
test-generator==0.1.1
Expand Down
42 changes: 22 additions & 20 deletions ngraph/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -305,38 +305,40 @@ if (NGRAPH_EXPORT_TARGETS_ENABLE)
COMPONENT ngraph)
endif()

if (NGRAPH_ONNX_IMPORT_ENABLE)
if (MSVC)
# When we build dll libraries. These flags make sure onnx and protobuf build with /MD, not /MT.
# These two options can't be mixed, because they requires link two imcompatiable runtime.
set(protobuf_WITH_ZLIB OFF CACHE BOOL "" FORCE)
set(BEFORE_ONNX_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
set(BUILD_SHARED_LIBS ON)

if(NOT DEFINED ONNX_USE_MSVC_STATIC_RUNTIME)
set(ONNX_USE_MSVC_STATIC_RUNTIME OFF)
endif()
if(NOT DEFINED protobuf_MSVC_STATIC_RUNTIME)
set(protobuf_MSVC_STATIC_RUNTIME OFF CACHE BOOL "Link protobuf to static runtime libraries" FORCE)
endif()
if (MSVC)
# When we build dll libraries. These flags make sure protobuf build with /MD, not /MT.
# These two options can't be mixed, because they requires link two incompatible runtime.
set(protobuf_WITH_ZLIB OFF CACHE BOOL "" FORCE)

if(NOT DEFINED ONNX_USE_MSVC_STATIC_RUNTIME)
set(ONNX_USE_MSVC_STATIC_RUNTIME OFF)
endif()
if(NOT DEFINED protobuf_MSVC_STATIC_RUNTIME)
set(protobuf_MSVC_STATIC_RUNTIME OFF CACHE BOOL "Link protobuf to static runtime libraries" FORCE)
endif()
endif()

set(BEFORE_ONNX_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
set(BUILD_SHARED_LIBS ON)
if (NOT NGRAPH_USE_SYSTEM_PROTOBUF)
include(cmake/external_protobuf.cmake)
else()
find_package(Protobuf 2.6.1 REQUIRED)
endif()

if (NOT NGRAPH_USE_SYSTEM_PROTOBUF)
include(cmake/external_protobuf.cmake)
else()
find_package(Protobuf 2.6.1 REQUIRED)
endif()
if (NGRAPH_ONNX_IMPORT_ENABLE)

# target onnx_proto will be shared lib, onnx static
include(cmake/external_onnx.cmake)
if (TARGET ext_protobuf)
add_dependencies(onnx ext_protobuf)
endif()
set(BUILD_SHARED_LIBS ${BEFORE_ONNX_BUILD_SHARED_LIBS})
unset(BEFORE_ONNX_BUILD_SHARED_LIBS)
endif()

set(BUILD_SHARED_LIBS ${BEFORE_ONNX_BUILD_SHARED_LIBS})
unset(BEFORE_ONNX_BUILD_SHARED_LIBS)

add_subdirectory(frontend)

if(NGRAPH_UNIT_TEST_ENABLE)
Expand Down
4 changes: 2 additions & 2 deletions ngraph/frontend/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ endif()
# TODO: Add other frontends to the list of dependencies temporary; in the produce version they should be discovered
# TODO: in runtime.
if (NGRAPH_ONNX_IMPORT_ENABLE)
target_link_libraries(frontend_manager PRIVATE onnx_importer)
target_link_libraries(frontend_manager PRIVATE onnx_common onnx_importer)
target_compile_definitions(frontend_manager PRIVATE NGRAPH_ONNX_IMPORT_ENABLE)
endif()

Expand All @@ -63,7 +63,7 @@ if (NGRAPH_TF_FRONTEND_ENABLE)
target_compile_definitions(frontend_manager PRIVATE NGRAPH_TF_FRONTEND_ENABLE)
endif()

target_link_libraries(frontend_manager PRIVATE onnx_common paddlepaddle_frontend PUBLIC ngraph)
target_link_libraries(frontend_manager PRIVATE paddlepaddle_frontend PUBLIC ngraph)

set(FRONTEND_INSTALL_INCLUDE "${NGRAPH_INSTALL_INCLUDE}/ngraph/frontend/generic")
target_include_directories(frontend_manager SYSTEM PUBLIC $<BUILD_INTERFACE:${FRONTEND_INCLUDE_DIR}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class NGRAPH_API InputModel
virtual std::vector<Place::Ptr> getOutputs () const;

/// Returns a tensor place by a tensor name following framework conventions, or nullptr if a tensor with this name doesn't exist.
virtual Place::Ptr getPlaceByTensorName (const std::string& tensorName);
virtual Place::Ptr getPlaceByTensorName (const std::string& tensorName) const;

/// Returns an operation place by a tensor name following framework conventions, or nullptr if an operation with this name doesn't exist.
virtual Place::Ptr getPlaceByOperationName (const std::string& operationName);
Expand Down
6 changes: 3 additions & 3 deletions ngraph/frontend/generic/src/frontend_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace ngraph
namespace frontend
{

#define FRONT_END_NOT_IMPLEMENTED(NAME) throw #NAME " is not implemented for this FrontEnd class";
#define FRONT_END_NOT_IMPLEMENTED(NAME) throw std::runtime_error(#NAME " is not implemented for this FrontEnd class");
#define FRONT_END_ASSERT(EXPRESSION) \
{ if (!(EXPRESSION)) throw "AssertionFailed"; }

Expand All @@ -50,7 +50,7 @@ namespace ngraph
FRONT_END_NOT_IMPLEMENTED(getOutputs);
}

Place::Ptr InputModel::getPlaceByTensorName (const std::string& tensorName)
Place::Ptr InputModel::getPlaceByTensorName (const std::string& tensorName) const
{
FRONT_END_NOT_IMPLEMENTED(getPlaceByTensorName);
}
Expand Down Expand Up @@ -285,7 +285,7 @@ namespace ngraph

InputModelONNX (const std::string& model_path) : editor(model_path) {}

Place::Ptr getPlaceByTensorName (const std::string& tensorName) override
Place::Ptr getPlaceByTensorName (const std::string& tensorName) const override
{
if(!editor.validate_tensor_name(tensorName)) {
std::cerr << " [ ERROR ] Node with name " << tensorName << " is not valid for a given model\n";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class NGRAPH_API InputModelPDPD : public InputModel
InputModelPDPD (const std::string& _path);
std::vector<Place::Ptr> getInputs () const;
std::vector<Place::Ptr> getOutputs () const;
Place::Ptr getPlaceByTensorName (const std::string& tensorName);
Place::Ptr getPlaceByTensorName (const std::string& tensorName) const;
void overrideAllOutputs (const std::vector<Place::Ptr>& outputs);
void overrideAllInputs (const std::vector<Place::Ptr>& inputs);
void extractSubgraph (const std::vector<Place::Ptr>& inputs, const std::vector<Place::Ptr>& outputs);
Expand Down
6 changes: 3 additions & 3 deletions ngraph/frontend/paddlepaddle/src/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class InputModelPDPD::InputModelPDPDImpl {
InputModelPDPDImpl (const std::string& _path);
std::vector<Place::Ptr> getInputs () const;
std::vector<Place::Ptr> getOutputs () const;
Place::Ptr getPlaceByTensorName (const std::string& tensorName);
Place::Ptr getPlaceByTensorName (const std::string& tensorName) const;
void overrideAllOutputs (const std::vector<Place::Ptr>& outputs);
void overrideAllInputs (const std::vector<Place::Ptr>& inputs);
void extractSubgraph (const std::vector<Place::Ptr>& inputs, const std::vector<Place::Ptr>& outputs);
Expand Down Expand Up @@ -126,7 +126,7 @@ std::vector<Place::Ptr> InputModelPDPD::InputModelPDPDImpl::getOutputs () const
NOT_IMPLEMENTED("getOutputs");
}

Place::Ptr InputModelPDPD::InputModelPDPDImpl::getPlaceByTensorName (const std::string& tensorName) {
Place::Ptr InputModelPDPD::InputModelPDPDImpl::getPlaceByTensorName (const std::string& tensorName) const {
for (auto var_places_in_block : var_places) {
if (var_places_in_block.count(tensorName))
return var_places_in_block.at(tensorName);
Expand Down Expand Up @@ -181,7 +181,7 @@ std::vector<Place::Ptr> InputModelPDPD::getOutputs () const {
return _impl->getOutputs();
}

Place::Ptr InputModelPDPD::getPlaceByTensorName (const std::string& tensorName) {
Place::Ptr InputModelPDPD::getPlaceByTensorName (const std::string& tensorName) const {
return _impl->getPlaceByTensorName(tensorName);
}

Expand Down
6 changes: 5 additions & 1 deletion ngraph/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ if (NGRAPH_TF_FRONTEND_ENABLE)
target_link_libraries(_${PROJECT_NAME} PRIVATE ngraph::tensorflow_frontend)
endif()

target_link_libraries(_${PROJECT_NAME} PRIVATE ngraph::ngraph ngraph::onnx_importer ngraph::frontend_manager)
if (NGRAPH_ONNX_IMPORT_ENABLE)
target_link_libraries(_${PROJECT_NAME} PRIVATE ngraph::onnx_importer)
endif()

target_link_libraries(_${PROJECT_NAME} PRIVATE ngraph::ngraph ngraph::frontend_manager)

if (TARGET ngraph::onnx_importer)
add_dependencies(_${PROJECT_NAME} ngraph::onnx_importer)
Expand Down
16 changes: 11 additions & 5 deletions ngraph/python/src/pyngraph/frontend_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ void regclass_pyngraph_FrontEnd(py::module m)
m, "FrontEnd", py::dynamic_attr());
fem.doc() = "ngraph.impl.FrontEnd wraps ngraph::frontend::FrontEnd";

fem.def("loadFromFile", &ngraph::frontend::FrontEnd::loadFromFile);
fem.def("loadFromFile", &ngraph::frontend::FrontEnd::loadFromFile,
py::arg("path"));
fem.def("convert",
static_cast<std::shared_ptr<ngraph::Function> (ngraph::frontend::FrontEnd::*)(
ngraph::frontend::InputModel::Ptr) const>(&ngraph::frontend::FrontEnd::convert));
Expand All @@ -56,12 +57,14 @@ void regclass_pyngraph_FrontEnd(py::module m)

void regclass_pyngraph_Place(py::module m)
{
py::class_<ngraph::frontend::Place, std::shared_ptr<ngraph::frontend::Place>> fem(
py::class_<ngraph::frontend::Place, std::shared_ptr<ngraph::frontend::Place>> place(
m, "Place", py::dynamic_attr());
fem.doc() = "ngraph.impl.Place wraps ngraph::frontend::Place";
place.doc() = "ngraph.impl.Place wraps ngraph::frontend::Place";

// fem.def("load", &ngraph::frontend::FrontEnd::load);
// fem.def("convert", &ngraph::frontend::FrontEnd::convert);
place.def("isInput", &ngraph::frontend::Place::isInput);
place.def("isOutput", &ngraph::frontend::Place::isOutput);
place.def("getNames", &ngraph::frontend::Place::getNames);
place.def("isEqual", &ngraph::frontend::Place::isEqual);
}

void regclass_pyngraph_InputModel(py::module m)
Expand All @@ -73,6 +76,9 @@ void regclass_pyngraph_InputModel(py::module m)
im.def("getPlaceByTensorName", &ngraph::frontend::InputModel::getPlaceByTensorName);
im.def("setPartialShape", &ngraph::frontend::InputModel::setPartialShape);
im.def("getInputs", &ngraph::frontend::InputModel::getInputs);
im.def("getOutputs", &ngraph::frontend::InputModel::getOutputs);
im.def("overrideAllInputs", &ngraph::frontend::InputModel::overrideAllInputs);
im.def("overrideAllOutputs", &ngraph::frontend::InputModel::overrideAllOutputs);
}

void regclass_pyngraph_FEC(py::module m)
Expand Down

0 comments on commit 0778c7e

Please sign in to comment.