diff --git a/ngraph/frontend/paddlepaddle/include/paddlepaddle_frontend/frontend.hpp b/ngraph/frontend/paddlepaddle/include/paddlepaddle_frontend/frontend.hpp index c690f84a9dfd95..67ba8dff5f1005 100644 --- a/ngraph/frontend/paddlepaddle/include/paddlepaddle_frontend/frontend.hpp +++ b/ngraph/frontend/paddlepaddle/include/paddlepaddle_frontend/frontend.hpp @@ -26,9 +26,9 @@ namespace frontend { class NGRAPH_API FrontEndPDPD : public FrontEnd { - std::shared_ptr convert_model(const std::shared_ptr& model) const; - std::shared_ptr read_tensor(std::shared_ptr place, - std::shared_ptr model) const; + static std::shared_ptr convert_model(const std::shared_ptr& model); + static std::shared_ptr read_tensor(const std::shared_ptr& place, + const std::shared_ptr& model); public: FrontEndPDPD () diff --git a/ngraph/frontend/paddlepaddle/include/paddlepaddle_frontend/model.hpp b/ngraph/frontend/paddlepaddle/include/paddlepaddle_frontend/model.hpp index 41044249f85a16..4c38f86bf8579a 100644 --- a/ngraph/frontend/paddlepaddle/include/paddlepaddle_frontend/model.hpp +++ b/ngraph/frontend/paddlepaddle/include/paddlepaddle_frontend/model.hpp @@ -31,19 +31,19 @@ class NGRAPH_API InputModelPDPD : public InputModel std::vector readWeight(const std::string& name, int64_t tensor_length); std::vector> getOpPlaces(int i) const; - std::map> getVarPlaces(int i) const; + std::map> getVarPlaces(int i) const; size_t getBlockNumber() const; public: - InputModelPDPD (const std::string& _path); - std::vector getInputs () const; - std::vector getOutputs () const; - Place::Ptr getPlaceByTensorName (const std::string& tensorName) const; - void overrideAllOutputs (const std::vector& outputs); - void overrideAllInputs (const std::vector& inputs); - void extractSubgraph (const std::vector& inputs, const std::vector& outputs); - void setDefaultShape (Place::Ptr place, const ngraph::Shape&); - void setPartialShape (Place::Ptr place, const ngraph::PartialShape&); + explicit InputModelPDPD (const std::string& _path); + std::vector getInputs () const override; + std::vector getOutputs () const override; + Place::Ptr getPlaceByTensorName (const std::string& tensorName) const override; + void overrideAllOutputs (const std::vector& outputs) override; + void overrideAllInputs (const std::vector& inputs) override; + void extractSubgraph (const std::vector& inputs, const std::vector& outputs) override; + void setDefaultShape (Place::Ptr place, const ngraph::Shape&) override; + void setPartialShape (Place::Ptr place, const ngraph::PartialShape&) override; }; } // namespace frontend diff --git a/ngraph/frontend/paddlepaddle/include/paddlepaddle_frontend/place.hpp b/ngraph/frontend/paddlepaddle/include/paddlepaddle_frontend/place.hpp index e113607bd3e0a1..3312dac4a6829e 100644 --- a/ngraph/frontend/paddlepaddle/include/paddlepaddle_frontend/place.hpp +++ b/ngraph/frontend/paddlepaddle/include/paddlepaddle_frontend/place.hpp @@ -1,5 +1,5 @@ //***************************************************************************** -// Copyright 2017-2021 Intel Corporation +// Copyright 2021 Intel Corporation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -17,46 +17,177 @@ #pragma once #include +#include + +namespace paddle { +namespace framework { +namespace proto { + +class OpDesc; +class VarDesc; + +} // proto +} // framework +} // paddle namespace ngraph { namespace frontend { -class PlacePDPD : public Place -{ - // TODO +class TensorPlacePDPD; +class OpPlacePDPD; + +class PlacePDPD : public Place { +public: + PlacePDPD(const InputModel& input_model, const std::vector& names) + : m_input_model(input_model), + m_names(names) { + } + + explicit PlacePDPD(const InputModel& input_model) : PlacePDPD(input_model, std::vector{}) { + } + + ~PlacePDPD() override = default; + + bool isInput() const override; + + bool isOutput() const override; + + bool isEqual(Ptr another) const override { return this == another.get(); } + + std::vector getNames() const override { return m_names; } + +private: + std::vector m_names; + const InputModel& m_input_model; }; -class InPortPlacePDPD : public PlacePDPD -{ - // TODO +class InPortPlacePDPD : public PlacePDPD { +public: + explicit InPortPlacePDPD(const InputModel& input_model) + : PlacePDPD(input_model) { + } + + void setOp(const std::weak_ptr& op) { + m_op = op; + } + + void addSourceTensor(const std::weak_ptr& source_tensor) { + m_source_tensors.push_back(source_tensor); + } + + std::vector> getSourceTensors() const; + + std::shared_ptr getSourceTensor(int idx) const override; + + std::shared_ptr getSourceTensorPDPD(int idx) const; + + std::shared_ptr getOp(); +private: + std::vector> m_source_tensors; + std::weak_ptr m_op; }; -class OutPortPlacePDPD : public PlacePDPD -{ - // TODO +class OutPortPlacePDPD : public PlacePDPD { +public: + explicit OutPortPlacePDPD(const InputModel& input_model) + : PlacePDPD(input_model) { + } + + void setOp(const std::weak_ptr& op) { m_op = op; } + + void addTargetTensor(const std::weak_ptr& target_tensor) { + m_target_tensors.push_back(target_tensor); + } + + std::shared_ptr getTargetTensor(int idx) const override; + + std::shared_ptr getTargetTensorPDPD(int idx) const; + + std::vector> getTargetTensors() const; +private: + std::weak_ptr m_op; + std::vector> m_target_tensors; }; -class VarPlacePDPD; -class OpPlacePDPD : public PlacePDPD -{ +class OpPlacePDPD : public PlacePDPD { public: - InputModel* model; - const void* op; // TODO: make it cleaner - std::map>> outputs; - std::map>> inputs; - OpPlacePDPD(const void* _op) : op(_op) {} + OpPlacePDPD(const InputModel& input_model, + const std::vector& names, + const std::shared_ptr& op_desc); + + OpPlacePDPD(const InputModel& input_model, + const std::shared_ptr& op_desc); + + void addInPort(const std::shared_ptr& input, const std::string& name) { + m_input_ports[name] = input; + } + + void addOutPort(const std::shared_ptr& output, const std::string& name) { + m_output_ports[name] = output; + } + + const std::map>& getOutputPorts() const { + return m_output_ports; + } + + const std::map>& getInputPorts() const { + return m_input_ports; + } + + std::shared_ptr getOutputPortByName(const std::string& name) { + return m_output_ports[name]; + } + + std::shared_ptr getInputPortByName(const std::string& name) { + return m_input_ports[name]; + } + + const std::shared_ptr& getDesc() const { return m_op_desc; } + +private: + std::shared_ptr m_op_desc; + std::map> m_input_ports; + std::map> m_output_ports; }; -class VarPlacePDPD : public PlacePDPD -{ +class TensorPlacePDPD : public PlacePDPD { public: - InputModel* model; - const void* var; // TODO: make it cleaner - std::vector> producing_ops; // should never have more than 1 element - std::vector> consuming_ops; - PartialShape shape; - element::Type type; - VarPlacePDPD(const void* _var) : var(_var) {} + TensorPlacePDPD(const InputModel& input_model, + const std::vector& names, + const std::shared_ptr& var_desc); + + TensorPlacePDPD(const InputModel& input_model, + const std::shared_ptr& var_desc); + + void addProducingPort(const std::shared_ptr& out_port) { + m_producing_ports.push_back(out_port); + } + + void addConsumingPort(const std::shared_ptr& in_port) { + m_consuming_ports.push_back(in_port); + } + + std::vector getConsumingPorts () const override; + + Ptr getProducingPort () const override; + + const PartialShape& getPartialShape() const { return m_pshape; } + + const element::Type& getElementType() const { return m_type; } + + void setPartialShape(const PartialShape& pshape) { m_pshape = pshape; } + + void setElementType(const element::Type& type) { m_type = type; } + + const std::shared_ptr& getDesc() const { return m_var_desc; } + +private: + std::shared_ptr m_var_desc; + PartialShape m_pshape; + element::Type m_type; + + std::vector> m_producing_ports; + std::vector> m_consuming_ports; }; } // namespace frontend diff --git a/ngraph/frontend/paddlepaddle/src/utility.hpp b/ngraph/frontend/paddlepaddle/include/paddlepaddle_frontend/utility.hpp similarity index 96% rename from ngraph/frontend/paddlepaddle/src/utility.hpp rename to ngraph/frontend/paddlepaddle/include/paddlepaddle_frontend/utility.hpp index 8c16273a7d3c80..00343a5aa3c85b 100644 --- a/ngraph/frontend/paddlepaddle/src/utility.hpp +++ b/ngraph/frontend/paddlepaddle/include/paddlepaddle_frontend/utility.hpp @@ -18,8 +18,6 @@ #include -#include - namespace ngraph { namespace frontend { diff --git a/ngraph/frontend/paddlepaddle/src/frontend.cpp b/ngraph/frontend/paddlepaddle/src/frontend.cpp index 4e1771afdcd604..c3864913ac4d76 100644 --- a/ngraph/frontend/paddlepaddle/src/frontend.cpp +++ b/ngraph/frontend/paddlepaddle/src/frontend.cpp @@ -27,51 +27,40 @@ #include "framework.pb.h" -#include "../include/paddlepaddle_frontend/model.hpp" +#include #include -#include +#include -#include "utility.hpp" +#include #include "decoder.hpp" #include "node_context.hpp" #include "op_table.hpp" #include +using namespace ngraph::opset7; namespace ngraph { namespace frontend { namespace pdpd { -std::shared_ptr -make_ng_node(std::map> &nodes, - const std::shared_ptr& place, - const std::map& CREATORS_MAP) { - auto op = (paddle::framework::proto::OpDesc*)place->op; +std::shared_ptr make_ng_node(std::map>& nodes, + const std::shared_ptr& op_place, + const std::map& CREATORS_MAP) { + const auto& op = op_place->getDesc(); std::cout << "Making node: " << op->type() << std::endl; MY_ASSERT(CREATORS_MAP.find(op->type()) != CREATORS_MAP.end(), "No creator found"); - std::map inputs_preproc; - for (const auto &item : place->inputs) { - inputs_preproc[item.first] = OutputVector(); - for (auto& var_place : item.second) { - // TODO: refactor to not search every time - auto var = (paddle::framework::proto::VarDesc*)var_place.lock()->var; - inputs_preproc[item.first].push_back(nodes[var->name()]); + std::map named_inputs; + const auto& input_ports = op_place->getInputPorts(); + for (const auto& name_to_port : input_ports) { + for (int idx = 0; idx < name_to_port.second->getSourceTensors().size(); ++idx) { + const auto& var_desc = name_to_port.second->getSourceTensorPDPD(idx)->getDesc(); + named_inputs[name_to_port.first].push_back(nodes[var_desc->name()]); } } - // TODO: Temporary repacking data to fit new creator API based on OutputVector instead of direct - // TODO: nodes manipulation. - - NamedInputs named_inputs; - for(const auto& input: inputs_preproc) - { - for(const auto& node: input.second) - named_inputs[input.first].push_back(node); - } - OutputVector outputs = CREATORS_MAP.at(op->type())(NodeContext(*op, named_inputs)); return outputs[0].get_node_shared_ptr(); } @@ -85,24 +74,24 @@ bool endsWith(const std::string &str, const std::string &suffix) { } // namespace pdpd -std::shared_ptr FrontEndPDPD::read_tensor(std::shared_ptr place, - std::shared_ptr model) const +std::shared_ptr FrontEndPDPD::read_tensor(const std::shared_ptr& tensor_place, + const std::shared_ptr& model) { - auto _var = (paddle::framework::proto::VarDesc*)place->var; - std::cout << "Reading tensor " << _var->name() << std::endl; - MY_ASSERT(_var->type().type() == paddle::framework::proto::VarType::LOD_TENSOR); - auto tensor = _var->type().lod_tensor().tensor(); - auto tensor_length = std::accumulate( + const auto& var_desc = tensor_place->getDesc(); + std::cout << "Reading tensor " << var_desc->name() << std::endl; + MY_ASSERT(var_desc->type().type() == paddle::framework::proto::VarType::LOD_TENSOR); + const auto& tensor = var_desc->type().lod_tensor().tensor(); + const auto& tensor_length = std::accumulate( tensor.dims().cbegin(), tensor.dims().cend(), 1, std::multiplies()); // TODO: implement for other types - auto tensor_data = model->readWeight(_var->name(), tensor_length); + auto tensor_data = model->readWeight(var_desc->name(), tensor_length); - auto shape = std::vector(tensor.dims().cbegin(), tensor.dims().cend()); - return opset6::Constant::create(element::f32, Shape(shape), tensor_data); + std::vector shape(tensor.dims().cbegin(), tensor.dims().cend()); + return Constant::create(element::f32, Shape(shape), tensor_data); } std::shared_ptr - FrontEndPDPD::convert_model(const std::shared_ptr& model) const + FrontEndPDPD::convert_model(const std::shared_ptr& model) { std::cout << "Convert Model Start" << std::endl; @@ -113,7 +102,7 @@ std::shared_ptr const auto& global_var_places = model->getVarPlaces(0); for (const auto& name_var : global_var_places) { - auto var = (paddle::framework::proto::VarDesc*)name_var.second->var; + const auto& var = name_var.second->getDesc(); if (pdpd::endsWith(name_var.first, "feed") || pdpd::endsWith(name_var.first, "fetch")) continue; if (!var->persistable()) @@ -123,54 +112,48 @@ std::shared_ptr std::cout << "Reading consts finished" << std::endl; std::map CREATORS_MAP = pdpd::get_supported_ops(); - for (int i = 0; i < model->getBlockNumber(); i++) { const auto& op_places = model->getOpPlaces(i); - const auto& var_places = model->getVarPlaces(i); - for (const auto &op_place : op_places) { - auto op = (paddle::framework::proto::OpDesc*)op_place->op; - std::cerr << "Observing " << op->type() << "\n"; - if (op->type() == "feed") { - auto out_var = op_place->outputs.at("Out")[0]; - auto var = (paddle::framework::proto::VarDesc*)out_var.lock()->var; - MY_ASSERT(var->type().type() == paddle::framework::proto::VarType::LOD_TENSOR); - auto tensor_desc = var->type().lod_tensor().tensor(); - auto dtype = tensor_desc.data_type(); - std::vector shape; + for (const auto& op_place : op_places) { + const auto& op_type = op_place->getDesc()->type(); + std::cerr << "Observing " << op_type << "\n"; + if (op_type == "feed") { + const auto& var_desc = op_place->getOutputPortByName("Out")->getTargetTensorPDPD(0)->getDesc(); + MY_ASSERT(var_desc->type().type() == paddle::framework::proto::VarType::LOD_TENSOR); + const auto& tensor_desc = var_desc->type().lod_tensor().tensor(); + const auto& dtype = tensor_desc.data_type(); + const auto& dims = tensor_desc.dims(); + // set all -1 dims to 1 // TODO: remove when input shape can be specified - for (auto dim : tensor_desc.dims()) { - if (dim >= 0) { - shape.push_back(dim); - } else { - shape.push_back(1); - } + std::vector shape(tensor_desc.dims_size(), 1); + for (int idx = 0; idx < shape.size(); ++idx) { + if (dims[idx] >= 0) + shape[idx] = dims[idx]; } - auto param = std::make_shared(TYPE_MAP[dtype], - ngraph::Shape(shape)); - param->set_friendly_name(var->name()); - nodes_dict[var->name()] = param; + + auto param = std::make_shared(TYPE_MAP[dtype], ngraph::Shape(shape)); + param->set_friendly_name(var_desc->name()); + nodes_dict[var_desc->name()] = param; parameter_nodes.push_back(param); std::cout << "Parameter created" << std::endl; - } else if (op->type() == "fetch") { + } else if (op_type == "fetch") { // TODO: resolve names for multiple outputs from one node - auto in_var = op_place->inputs.at("X")[0]; - auto var = (paddle::framework::proto::VarDesc*)in_var.lock()->var; - auto input_var_name = var->name(); - auto result = std::make_shared(nodes_dict.at(input_var_name)); + const auto& in_var = op_place->getInputPortByName("X")->getSourceTensorPDPD(0)->getDesc(); + const auto& input_var_name = in_var->name(); + auto result = std::make_shared(nodes_dict.at(input_var_name)); result->set_friendly_name(input_var_name + "/Result"); result_nodes.push_back(result); } else { - auto node = pdpd::make_ng_node(nodes_dict, op_place, CREATORS_MAP); + const auto& node = pdpd::make_ng_node(nodes_dict, op_place, CREATORS_MAP); // set layer name by the name of first output var - auto& first_output_var_place = op_place->outputs.begin()->second[0]; - const auto& var = static_cast(first_output_var_place.lock()->var); - node->set_friendly_name(var->name()); + const auto& first_output_var = op_place->getOutputPorts().begin()->second->getTargetTensorPDPD(0)->getDesc(); + node->set_friendly_name(first_output_var->name()); std::cerr << "Named with " << node->get_friendly_name() << "\n"; - for (const auto &item : op_place->outputs) { - for (size_t idx = 0; idx < item.second.size(); ++idx) { - auto var = (paddle::framework::proto::VarDesc *)item.second[idx].lock()->var; + for (const auto &name_to_port : op_place->getOutputPorts()) { + for (size_t idx = 0; idx < name_to_port.second->getTargetTensors().size(); ++idx) { + const auto& var = name_to_port.second->getTargetTensorPDPD(idx)->getDesc(); nodes_dict[var->name()] = node->output(idx); } } diff --git a/ngraph/frontend/paddlepaddle/src/model.cpp b/ngraph/frontend/paddlepaddle/src/model.cpp index a5ddbe2994a792..a2698aa4c3ace5 100644 --- a/ngraph/frontend/paddlepaddle/src/model.cpp +++ b/ngraph/frontend/paddlepaddle/src/model.cpp @@ -2,28 +2,23 @@ // SPDX-License-Identifier: Apache-2.0 // -#include "../include/paddlepaddle_frontend/model.hpp" +#include +#include #include #include "framework.pb.h" -#include "utility.hpp" #include "decoder.hpp" namespace ngraph { namespace frontend { +using namespace paddle::framework::proto; + class InputModelPDPD::InputModelPDPDImpl { - std::shared_ptr fw_ptr; - std::vector>> op_places; - std::vector>> var_places; - std::vector inputs; - std::vector outputs; - std::ifstream weights_stream; - bool weights_composed = false; public: std::string path; - InputModelPDPDImpl (const std::string& _path); + InputModelPDPDImpl (const std::string& _path, const InputModel& input_model); std::vector getInputs () const; std::vector getOutputs () const; Place::Ptr getPlaceByTensorName (const std::string& tensorName) const; @@ -35,12 +30,25 @@ class InputModelPDPD::InputModelPDPDImpl { template std::vector readWeight(const std::string& name, int64_t tensor_length); - std::vector> getOpPlaces(int i) const { return op_places[i]; } - std::map> getVarPlaces(int i) const { return var_places[i]; } - size_t getBlockNumber() const { return op_places.size(); } + std::vector> getOpPlaces(int i) const { return op_places_blocks[i]; } + std::map> getVarPlaces(int i) const { return var_places_blocks[i]; } + size_t getBlockNumber() const { return op_places_blocks.size(); } + +private: + std::vector>> op_places_blocks; + std::vector>> var_places_blocks; + std::shared_ptr fw_ptr; + std::ifstream weights_stream; + bool weights_composed = false; + const InputModel& m_input_model; + std::vector inputs; + std::vector outputs; }; -InputModelPDPD::InputModelPDPDImpl::InputModelPDPDImpl(const std::string& _path) : path(_path), fw_ptr{std::make_shared()} { +InputModelPDPD::InputModelPDPDImpl::InputModelPDPDImpl(const std::string& _path, const InputModel& input_model) + : path(_path), + fw_ptr{std::make_shared()}, + m_input_model(input_model) { std::string ext = ".pdmodel"; std::string model_file(path); if (path.length() >= ext.length() && (0 == path.compare(path.length() - ext.length(), ext.length(), ext))) @@ -61,49 +69,61 @@ InputModelPDPD::InputModelPDPDImpl::InputModelPDPDImpl(const std::string& _path) std::cout << "Model Parsed: " << fw_ptr->ParseFromIstream(&pb_stream) << std::endl; std::cout << "Blocks number: " << fw_ptr->blocks().size() << std::endl; - for (const auto& block : fw_ptr->blocks()) { - var_places.push_back(std::map>()); - for (int i = 0; i < block.vars().size(); i++) { - var_places.back()[block.vars()[i].name()] = std::make_shared(VarPlacePDPD(&(block.vars()[i]))); + + const int cnt_of_blocks = fw_ptr->blocks_size(); + const auto& blocks = fw_ptr->blocks(); + var_places_blocks.resize(cnt_of_blocks); + op_places_blocks.resize(cnt_of_blocks); + + for (int block_idx = 0; block_idx < cnt_of_blocks; block_idx++) { + const auto& block = blocks[block_idx]; + auto& var_place_block = var_places_blocks[block_idx]; + auto& op_place_block = op_places_blocks[block_idx]; + + for (const auto& var : block.vars()) { + var_place_block[var.name()] = std::make_shared(m_input_model, std::make_shared(var)); } - op_places.push_back(std::vector>()); for (const auto& op : block.ops()) { - auto op_place = std::make_shared(OpPlacePDPD(&op)); + auto op_place = std::make_shared(m_input_model, std::make_shared(op)); + op_place_block.push_back(op_place); + for (const auto &output : op.outputs()) { - std::vector> out_vars; - for (auto& var_name : output.arguments()) { - auto& var = var_places.back().at(var_name); - var->producing_ops.push_back(op_place); - out_vars.push_back(var); + auto out_port = std::make_shared(m_input_model); + op_place->addOutPort(out_port, output.parameter()); + out_port->setOp(op_place); + for (const auto &var_name : output.arguments()) { + const auto& tensor = var_place_block.at(var_name); + tensor->addProducingPort(out_port); + out_port->addTargetTensor(tensor); } - op_place->outputs[output.parameter()] = out_vars; } - std::map> inputs_dict; + for (const auto &input : op.inputs()) { - std::vector> in_vars; - for (auto& var_name : input.arguments()) { - auto& var = var_places.back().at(var_name); - var->consuming_ops.push_back(op_place); - in_vars.push_back(var); + auto in_port = std::make_shared(m_input_model); + op_place->addInPort(in_port, input.parameter()); + in_port->setOp(op_place); + for (const auto &var_name : input.arguments()) { + const auto& tensor = var_place_block.at(var_name); + tensor->addConsumingPort(in_port); + in_port->addSourceTensor(tensor); } - op_place->inputs[input.parameter()] = in_vars; } + // Determine outputs and inputs if (op.type() == "feed") { - auto var_place = op_place->outputs.at("Out")[0].lock(); - auto var = (paddle::framework::proto::VarDesc*)var_place->var; - const auto& tensor_desc = var->type().lod_tensor().tensor(); - const auto& dtype = tensor_desc.data_type(); - var_place->type = TYPE_MAP[dtype]; + const auto& place = op_place->getOutputPortByName("Out")->getTargetTensor(0); + const auto& var_place = std::dynamic_pointer_cast(place); + const auto& tensor_desc = var_place->getDesc()->type().lod_tensor().tensor(); const auto& dims = tensor_desc.dims(); - var_place->shape = ngraph::PartialShape(std::vector(dims.begin(), dims.end())); - inputs.push_back(std::dynamic_pointer_cast(var_place)); + + var_place->setElementType(TYPE_MAP[tensor_desc.data_type()]); + var_place->setPartialShape(PartialShape(std::vector(dims.begin(), dims.end()))); + inputs.push_back(place); } else if (op.type() == "fetch") { - auto var_place = op_place->inputs.at("X")[0].lock(); - outputs.push_back(std::dynamic_pointer_cast(var_place)); + auto place = op_place->getInputPortByName("X")->getSourceTensor(0); + outputs.push_back(place); } - op_places.back().push_back(op_place); } } } @@ -144,7 +164,7 @@ std::vector InputModelPDPD::InputModelPDPDImpl::getOutputs () const } Place::Ptr InputModelPDPD::InputModelPDPDImpl::getPlaceByTensorName (const std::string& tensorName) const { - for (auto var_places_in_block : var_places) { + for (const auto& var_places_in_block : var_places_blocks) { if (var_places_in_block.count(tensorName)) return var_places_in_block.at(tensorName); } @@ -171,7 +191,7 @@ void InputModelPDPD::InputModelPDPDImpl::setPartialShape (Place::Ptr place, cons NOT_IMPLEMENTED("setPartialShape"); } -InputModelPDPD::InputModelPDPD (const std::string& _path) : _impl{std::make_shared(_path)} {} +InputModelPDPD::InputModelPDPD (const std::string& _path) : _impl{std::make_shared(_path, *this)} {} std::vector InputModelPDPD::readWeight(const std::string& name, int64_t tensor_length) { return _impl->readWeight(name, tensor_length); @@ -181,7 +201,7 @@ std::vector> InputModelPDPD::getOpPlaces(int i) con return _impl->getOpPlaces(i); } -std::map> InputModelPDPD::getVarPlaces(int i) const { +std::map> InputModelPDPD::getVarPlaces(int i) const { return _impl->getVarPlaces(i); } diff --git a/ngraph/frontend/paddlepaddle/src/node_context.hpp b/ngraph/frontend/paddlepaddle/src/node_context.hpp index ca7afe1b63ad6e..33a815b340234b 100644 --- a/ngraph/frontend/paddlepaddle/src/node_context.hpp +++ b/ngraph/frontend/paddlepaddle/src/node_context.hpp @@ -16,7 +16,7 @@ #pragma once #include "decoder.hpp" -#include "utility.hpp" +#include namespace ngraph { namespace frontend { diff --git a/ngraph/frontend/paddlepaddle/src/op/matmul.cpp b/ngraph/frontend/paddlepaddle/src/op/matmul.cpp index dd32d4310882c8..d7ac841f29f6ba 100644 --- a/ngraph/frontend/paddlepaddle/src/op/matmul.cpp +++ b/ngraph/frontend/paddlepaddle/src/op/matmul.cpp @@ -16,7 +16,7 @@ #include #include "matmul.hpp" -#include "utility.hpp" +#include namespace ngraph { namespace frontend { diff --git a/ngraph/frontend/paddlepaddle/src/op/mul.cpp b/ngraph/frontend/paddlepaddle/src/op/mul.cpp index 9fcc8a9cf3ad2c..2cd896cc83206f 100644 --- a/ngraph/frontend/paddlepaddle/src/op/mul.cpp +++ b/ngraph/frontend/paddlepaddle/src/op/mul.cpp @@ -15,7 +15,7 @@ //***************************************************************************** #include -#include "utility.hpp" +#include #include "mul.hpp" namespace ngraph { diff --git a/ngraph/frontend/paddlepaddle/src/op/reshape2.cpp b/ngraph/frontend/paddlepaddle/src/op/reshape2.cpp index 92b7bc891f4506..776da63f9aa713 100644 --- a/ngraph/frontend/paddlepaddle/src/op/reshape2.cpp +++ b/ngraph/frontend/paddlepaddle/src/op/reshape2.cpp @@ -16,7 +16,7 @@ #include #include "reshape2.hpp" -#include "utility.hpp" +#include namespace ngraph { namespace frontend { diff --git a/ngraph/frontend/paddlepaddle/src/op/softmax.cpp b/ngraph/frontend/paddlepaddle/src/op/softmax.cpp index a11cc13a750c1e..0667bfb1aec4b1 100644 --- a/ngraph/frontend/paddlepaddle/src/op/softmax.cpp +++ b/ngraph/frontend/paddlepaddle/src/op/softmax.cpp @@ -16,7 +16,7 @@ #include #include "softmax.hpp" -#include "utility.hpp" +#include namespace ngraph { namespace frontend { diff --git a/ngraph/frontend/paddlepaddle/src/op/split.cpp b/ngraph/frontend/paddlepaddle/src/op/split.cpp index f1bcbf1534ad85..1ebbf8587ad30c 100644 --- a/ngraph/frontend/paddlepaddle/src/op/split.cpp +++ b/ngraph/frontend/paddlepaddle/src/op/split.cpp @@ -16,7 +16,7 @@ #include #include "split.h" -#include "utility.hpp" +#include namespace ngraph { namespace frontend { diff --git a/ngraph/frontend/paddlepaddle/src/place.cpp b/ngraph/frontend/paddlepaddle/src/place.cpp new file mode 100644 index 00000000000000..36a35c1c15beef --- /dev/null +++ b/ngraph/frontend/paddlepaddle/src/place.cpp @@ -0,0 +1,141 @@ +//***************************************************************************** +// Copyright 2021 Intel Corporation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +//***************************************************************************** + + +#include +#include "framework.pb.h" + +using namespace ngraph; +using namespace frontend; + +bool PlacePDPD::isInput() const { + const auto &model_ins = m_input_model.getInputs(); + + const auto cmp = [this](const Place::Ptr &p) { + return p.get() == this; + }; + return std::find_if(model_ins.begin(), model_ins.end(), cmp) != model_ins.end(); +} + +bool PlacePDPD::isOutput() const { + const auto &model_outs = m_input_model.getOutputs(); + const auto cmp = [this](const Place::Ptr &p) { + return p.get() == this; + }; + return std::find_if(model_outs.begin(), model_outs.end(), cmp) != model_outs.end(); +} + +OpPlacePDPD::OpPlacePDPD(const InputModel &input_model, const std::vector &names, + const std::shared_ptr &op_desc) + : PlacePDPD(input_model, names), + m_op_desc(op_desc) { + +} + +OpPlacePDPD::OpPlacePDPD(const InputModel &input_model, + const std::shared_ptr &op_desc) + : OpPlacePDPD(input_model, {}, op_desc) { +} + +TensorPlacePDPD::TensorPlacePDPD(const InputModel &input_model, const std::vector &names, + const std::shared_ptr &var_desc) + : PlacePDPD(input_model, names), + m_var_desc(var_desc) { +} + +TensorPlacePDPD::TensorPlacePDPD(const InputModel &input_model, + const std::shared_ptr &var_desc) + : TensorPlacePDPD(input_model, {var_desc->name()}, var_desc) { +} + +std::vector TensorPlacePDPD::getConsumingPorts() const { + std::vector consuming_ports; + for (const auto & consuming_port: m_consuming_ports) { + if (const auto& locked = consuming_port.lock()) { + consuming_ports.push_back(locked); + } else { + MY_ASSERT(false, "Consuming Port has expired."); + } + } + return consuming_ports; +} + +Place::Ptr TensorPlacePDPD::getProducingPort() const { + MY_ASSERT(m_producing_ports.size() > 1, "Only one producing port is supported."); + if (const auto& producing_port = m_producing_ports[0].lock()) { + return producing_port; + } + MY_ASSERT(false, "Producing Port has expired."); +} + +std::shared_ptr InPortPlacePDPD::getSourceTensor(int idx) const { + if (const auto& tensor = m_source_tensors[idx].lock()) { + return tensor; + } + MY_ASSERT(false, "Source Tensor has expired."); +} + +std::shared_ptr InPortPlacePDPD::getSourceTensorPDPD(int idx) const { + if (const auto& tensor = m_source_tensors[idx].lock()) { + return tensor; + } + MY_ASSERT(false, "Source Tensor has expired."); +} + +std::shared_ptr InPortPlacePDPD::getOp() { + if (const auto& op = m_op.lock()) { + return op; + } + MY_ASSERT(false, "Operation has expired."); +} + +std::vector> InPortPlacePDPD::getSourceTensors() const { + std::vector> source_tensors; + for (const auto & tensor: m_source_tensors) { + if (const auto& locked = tensor.lock()) { + source_tensors.push_back(locked); + } else { + MY_ASSERT(false, "Source Tensor has expired."); + } + } + return source_tensors; +} + +std::shared_ptr OutPortPlacePDPD::getTargetTensor(int idx) const { + if (const auto& target_tensor = m_target_tensors.at(idx).lock()) { + return target_tensor; + } + MY_ASSERT(false, "Target Tensor has expired."); +} + +std::shared_ptr OutPortPlacePDPD::getTargetTensorPDPD(int idx) const { + if (const auto& target_tensor = m_target_tensors.at(idx).lock()) { + return target_tensor; + } + MY_ASSERT(false, "Target Tensor has expired."); +} + +std::vector> OutPortPlacePDPD::getTargetTensors() const { + std::vector> target_tensors; + for (const auto & tensor: m_target_tensors) { + if (const auto& locked = tensor.lock()) { + target_tensors.push_back(locked); + } else { + MY_ASSERT(false, "Target Tensor has expired."); + } + } + return target_tensors; +}