Skip to content

Commit

Permalink
Implement paddle frontend methods for partial conversion (#6784)
Browse files Browse the repository at this point in the history
* Implement paddle frontend methods for partial conversion

* Apply feedback

* Fix codestyle

* Remove normalize implementation and change convert signature for partialModel case

* Apply review feedback

* Apply review feedback

* Fix onnx importer convert signature

* Fix codestyle

* Add test of unsupported op

* Fix code style

* Remove "normalize()" tests

* Fix onnx tests

* Fix build

* Fix merge conflicts
  • Loading branch information
mvafin authored Jul 30, 2021
1 parent 0861a5c commit 5920cf8
Show file tree
Hide file tree
Showing 26 changed files with 679 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ TEST(PDPD_Reader_Tests, ImportBasicModelToCore) {
"RefPDPDFunction");
const FunctionsComparator func_comparator = FunctionsComparator::with_default().enable(FunctionsComparator::NAMES);
const FunctionsComparator::Result res = func_comparator(function, reference);
ASSERT_TRUE(res.valid);
ASSERT_TRUE(res.valid) << res.message;
}

#if defined(ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32)
Expand Down Expand Up @@ -79,6 +79,6 @@ TEST(PDPD_Reader_Tests, ImportBasicModelToCoreWstring) {
"RefPDPDFunction");
const FunctionsComparator func_comparator = FunctionsComparator::with_default().enable(FunctionsComparator::NAMES);
const FunctionsComparator::Result res = func_comparator(function, reference);
ASSERT_TRUE(res.valid);
ASSERT_TRUE(res.valid) << res.message;
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ namespace ngraph

/// \brief Completely convert the remaining, not converted part of a function.
/// \param partiallyConverted partially converted nGraph function
/// \return fully converted nGraph function
virtual std::shared_ptr<ngraph::Function>
convert(std::shared_ptr<ngraph::Function> partially_converted) const;
virtual void convert(std::shared_ptr<ngraph::Function> partially_converted) const;

/// \brief Convert only those parts of the model that can be converted leaving others
/// as-is. Converted parts are not normalized by additional transformations; normalize
Expand Down
2 changes: 1 addition & 1 deletion ngraph/frontend/frontend_manager/src/frontend_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ std::shared_ptr<ngraph::Function> FrontEnd::convert(InputModel::Ptr model) const
FRONT_END_NOT_IMPLEMENTED(convert);
}

std::shared_ptr<ngraph::Function> FrontEnd::convert(std::shared_ptr<ngraph::Function>) const
void FrontEnd::convert(std::shared_ptr<ngraph::Function>) const
{
FRONT_END_NOT_IMPLEMENTED(convert);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ namespace ngraph
{
public:
std::shared_ptr<ngraph::Function> convert(InputModel::Ptr model) const override;
std::shared_ptr<ngraph::Function>
convert(std::shared_ptr<ngraph::Function> partially_converted) const override;
void convert(std::shared_ptr<ngraph::Function> partially_converted) const override;
std::shared_ptr<ngraph::Function> decode(InputModel::Ptr model) const override;

protected:
Expand Down
5 changes: 2 additions & 3 deletions ngraph/frontend/onnx/frontend/src/frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ std::shared_ptr<ngraph::Function> FrontEndONNX::convert(InputModel::Ptr model) c
return model_onnx->convert();
}

std::shared_ptr<ngraph::Function>
FrontEndONNX::convert(std::shared_ptr<ngraph::Function> partially_converted) const
void FrontEndONNX::convert(std::shared_ptr<ngraph::Function> partially_converted) const
{
return onnx_import::convert_decoded_function(partially_converted);
onnx_import::convert_decoded_function(partially_converted);
}

std::shared_ptr<ngraph::Function> FrontEndONNX::decode(InputModel::Ptr model) const
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,8 @@ namespace ngraph
/// \brief Converts a nGraph function (onnx model decoded to function with
/// ONNXFrameworkNode(s))
/// to a complete function with actual compute operations
///
/// \return A nGraph function.
ONNX_IMPORTER_API
std::shared_ptr<Function> convert_decoded_function(std::shared_ptr<Function> function);
void convert_decoded_function(std::shared_ptr<Function> function);
} // namespace onnx_import

} // namespace ngraph
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace ngraph
decode_to_framework_nodes(std::shared_ptr<ONNX_NAMESPACE::ModelProto> model_proto,
const std::string& model_path);

std::shared_ptr<Function> convert_decoded_function(std::shared_ptr<Function> function);
void convert_decoded_function(std::shared_ptr<Function> function);
} // namespace detail
} // namespace onnx_import
} // namespace ngraph
4 changes: 2 additions & 2 deletions ngraph/frontend/onnx/onnx_import/src/onnx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ namespace ngraph
op_name, version, domain == "ai.onnx" ? "" : domain);
}

std::shared_ptr<Function> convert_decoded_function(std::shared_ptr<Function> function)
void convert_decoded_function(std::shared_ptr<Function> function)
{
return detail::convert_decoded_function(function);
detail::convert_decoded_function(function);
}

} // namespace onnx_import
Expand Down
4 changes: 1 addition & 3 deletions ngraph/frontend/onnx/onnx_import/src/utils/onnx_internal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ namespace ngraph
}
}

std::shared_ptr<Function> convert_decoded_function(std::shared_ptr<Function> function)
void convert_decoded_function(std::shared_ptr<Function> function)
{
for (const auto& node : function->get_ordered_ops())
{
Expand All @@ -87,8 +87,6 @@ namespace ngraph
}
detail::remove_dangling_parameters(function);
detail::remove_dangling_results(function);

return function;
}

void apply_transformations(ONNX_NAMESPACE::ModelProto& model_proto,
Expand Down
2 changes: 1 addition & 1 deletion ngraph/frontend/paddlepaddle/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ endif()
link_system_libraries(${TARGET_NAME} PRIVATE ${Protobuf_LITE_LIBRARIES})

target_link_libraries(${TARGET_NAME} PRIVATE ngraph::frontend_manager::static
PRIVATE ngraph::builder)
PRIVATE ngraph::builder inference_engine_transformations)

add_clang_format_target(${TARGET_NAME}_clang FOR_TARGETS ${TARGET_NAME}
EXCLUDE_PATTERNS ${PROTO_SRCS} ${PROTO_HDRS})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace ngraph
{
namespace frontend
{
class OpPlacePDPD;

class PDPD_API FrontEndPDPD : public FrontEnd
{
public:
Expand All @@ -22,6 +24,25 @@ namespace ngraph
/// \return fully converted nGraph function
std::shared_ptr<Function> convert(InputModel::Ptr model) const override;

/// \brief Completely convert the remaining, not converted part of a function.
/// \param partiallyConverted partially converted nGraph function
void convert(std::shared_ptr<Function> partiallyConverted) const override;

/// \brief Convert only those parts of the model that can be converted leaving others
/// as-is. Converted parts are not normalized by additional transformations; normalize
/// function or another form of convert function should be called to finalize the
/// conversion process.
/// \param model Input model
/// \return partially converted nGraph function
std::shared_ptr<Function> convert_partially(InputModel::Ptr model) const override;

/// \brief Convert operations with one-to-one mapping with decoding nodes.
/// Each decoding node is an nGraph node representing a single FW operation node with
/// all attributes represented in FW-independent way.
/// \param model Input model
/// \return nGraph function after decoding
std::shared_ptr<Function> decode(InputModel::Ptr model) const override;

protected:
/// \brief Check if FrontEndPDPD can recognize model from given parts
/// \param params Can be path to folder which contains __model__ file or path to
Expand All @@ -40,7 +61,10 @@ namespace ngraph

private:
static std::shared_ptr<Function>
convert_model(const std::shared_ptr<InputModelPDPD>& model);
convert_each_node(const std::shared_ptr<InputModelPDPD>& model,
std::function<std::map<std::string, OutputVector>(
const std::map<std::string, Output<Node>>&,
const std::shared_ptr<OpPlacePDPD>&)> func);
};

} // namespace frontend
Expand Down
60 changes: 60 additions & 0 deletions ngraph/frontend/paddlepaddle/src/decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,31 @@ namespace ngraph
return output_names;
}

size_t DecoderPDPDProto::get_output_size() const
{
size_t res = 0;
for (const auto& output : op_place->get_desc().outputs())
{
res += output.arguments().size();
}
return res;
}

std::map<std::string, std::vector<ngraph::element::Type>>
DecoderPDPDProto::get_output_type_map() const
{
std::map<std::string, std::vector<ngraph::element::Type>> output_types;
for (const auto& out_port_pair : op_place->get_output_ports())
{
for (const auto& p_place : out_port_pair.second)
{
output_types[out_port_pair.first].push_back(
p_place->get_target_tensor_pdpd()->get_element_type());
}
}
return output_types;
}

ngraph::element::Type
DecoderPDPDProto::get_out_port_type(const std::string& port_name) const
{
Expand Down Expand Up @@ -135,5 +160,40 @@ namespace ngraph
" Expected number: 0 or 1");
return attrs;
}

namespace
{
inline std::map<std::string, OutputVector> map_for_each_input_impl(
const google::protobuf::RepeatedPtrField<paddle::framework::proto::OpDesc_Var>& c,
const std::function<Output<Node>(const std::string&, size_t)>& func)
{
size_t idx = 0;
std::map<std::string, OutputVector> res;
for (const auto& port : c)
{
std::vector<Output<Node>> v;
v.reserve(port.arguments_size());
for (const auto& inp : port.arguments())
{
v.push_back(func(inp, idx++));
}
res.emplace(std::make_pair(port.parameter(), v));
}
return res;
}
} // namespace

std::map<std::string, OutputVector> DecoderPDPDProto::map_for_each_input(
const std::function<Output<Node>(const std::string&, size_t)>& func) const
{
return map_for_each_input_impl(op_place->get_desc().inputs(), func);
}

std::map<std::string, OutputVector> DecoderPDPDProto::map_for_each_output(
const std::function<Output<Node>(const std::string&, size_t)>& func) const
{
return map_for_each_input_impl(op_place->get_desc().outputs(), func);
}

} // namespace frontend
} // namespace ngraph
10 changes: 10 additions & 0 deletions ngraph/frontend/paddlepaddle/src/decoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,20 @@ namespace ngraph

std::vector<pdpd::OutPortName> get_output_names() const override;

size_t get_output_size() const override;

ngraph::element::Type get_out_port_type(const std::string& port_name) const override;

std::string get_op_type() const override;

std::map<std::string, std::vector<ngraph::element::Type>> get_output_type_map() const;

std::map<std::string, OutputVector> map_for_each_input(
const std::function<Output<Node>(const std::string&, size_t)>& func) const;

std::map<std::string, OutputVector> map_for_each_output(
const std::function<Output<Node>(const std::string&, size_t)>& func) const;

private:
std::vector<paddle::framework::proto::OpDesc_Attr>
decode_attribute_helper(const std::string& name) const;
Expand Down
Loading

0 comments on commit 5920cf8

Please sign in to comment.