Skip to content

Commit

Permalink
Merge pull request #6 from mvafin/input_model/in_out
Browse files Browse the repository at this point in the history
Implement getInputs and getOutputs
  • Loading branch information
nosovmik authored Apr 14, 2021
2 parents 0778c7e + 7605a09 commit e01d1dc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
#pragma once

#include <frontend_manager/frontend_manager.hpp>
#include <fstream>
#include <string>

#include "place.hpp"

Expand All @@ -31,8 +29,7 @@ class NGRAPH_API InputModelPDPD : public InputModel
class InputModelPDPDImpl;
std::shared_ptr<InputModelPDPDImpl> _impl;

//template<typename T>
std::vector<float> getWeight(const std::string& name, int64_t tensor_length);
std::vector<float> readWeight(const std::string& name, int64_t tensor_length);
std::vector<std::shared_ptr<OpPlacePDPD>> getOpPlaces(int i) const;
std::map<std::string, std::shared_ptr<VarPlacePDPD>> getVarPlaces(int i) const;
size_t getBlockNumber() const;
Expand Down
6 changes: 2 additions & 4 deletions ngraph/frontend/paddlepaddle/src/frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ std::shared_ptr<opset6::Constant> FrontEndPDPD::read_tensor(std::shared_ptr<VarP
auto tensor_length = std::accumulate(
tensor.dims().cbegin(), tensor.dims().cend(), 1, std::multiplies<int64_t>());
// TODO: implement for other types
auto tensor_data = model->getWeight(_var->name(), tensor_length);
auto tensor_data = model->readWeight(_var->name(), tensor_length);

auto shape = std::vector<size_t>(tensor.dims().cbegin(), tensor.dims().cend());
return opset6::Constant::create(element::f32, Shape(shape), tensor_data);
Expand Down Expand Up @@ -128,9 +128,7 @@ std::shared_ptr<Function>
for (int i = 0; i < model->getBlockNumber(); i++) {
const auto& op_places = model->getOpPlaces(i);
const auto& var_places = model->getVarPlaces(i);
for (int j = 0; j < op_places.size(); j++) {
std::cerr << "Observing index i = " << j << "\n";
const auto &op_place = op_places[j];
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") {
Expand Down
40 changes: 28 additions & 12 deletions ngraph/frontend/paddlepaddle/src/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@

#include "../include/paddlepaddle_frontend/model.hpp"

#include <fstream>
#include "framework.pb.h"
#include "utility.hpp"
#include "decoder.hpp"

namespace ngraph {
namespace frontend {

class InputModelPDPD::InputModelPDPDImpl {
class InputModelPDPD::InputModelPDPDImpl {
std::shared_ptr<paddle::framework::proto::ProgramDesc> fw_ptr;
std::vector<std::vector<std::shared_ptr<OpPlacePDPD>>> op_places;
std::vector<std::map<std::string, std::shared_ptr<VarPlacePDPD>>> var_places;
std::shared_ptr<paddle::framework::proto::ProgramDesc> fw_ptr;
std::vector<Place::Ptr> inputs;
std::vector<Place::Ptr> outputs;
std::ifstream weights_stream;
bool weights_composed = false;
public:
Expand All @@ -30,7 +34,7 @@ class InputModelPDPD::InputModelPDPDImpl {
void setPartialShape (Place::Ptr place, const ngraph::PartialShape&);

template<typename T>
std::vector<T> getWeight(const std::string& name, int64_t tensor_length);
std::vector<T> readWeight(const std::string& name, int64_t tensor_length);
std::vector<std::shared_ptr<OpPlacePDPD>> getOpPlaces(int i) const { return op_places[i]; }
std::map<std::string, std::shared_ptr<VarPlacePDPD>> getVarPlaces(int i) const { return var_places[i]; }
size_t getBlockNumber() const { return op_places.size(); }
Expand Down Expand Up @@ -64,8 +68,7 @@ InputModelPDPD::InputModelPDPDImpl::InputModelPDPDImpl(const std::string& _path)
}

op_places.push_back(std::vector<std::shared_ptr<OpPlacePDPD>>());
for (int i = 0; i < block.ops_size(); i++) {
auto& op = block.ops()[i];
for (const auto& op : block.ops()) {
auto op_place = std::make_shared<OpPlacePDPD>(OpPlacePDPD(&op));
for (const auto &output : op.outputs()) {
std::vector<std::weak_ptr<VarPlacePDPD>> out_vars;
Expand All @@ -86,13 +89,27 @@ InputModelPDPD::InputModelPDPDImpl::InputModelPDPDImpl(const std::string& _path)
}
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& dims = tensor_desc.dims();
var_place->shape = ngraph::PartialShape(std::vector<ngraph::Dimension>(dims.begin(), dims.end()));
inputs.push_back(std::dynamic_pointer_cast<Place>(var_place));
} else if (op.type() == "fetch") {
auto var_place = op_place->inputs.at("X")[0].lock();
outputs.push_back(std::dynamic_pointer_cast<Place>(var_place));
}
op_places.back().push_back(op_place);
}
}
}

template<typename T>
std::vector<T> InputModelPDPD::InputModelPDPDImpl::getWeight(const std::string& name, int64_t tensor_length) {
std::vector<T> InputModelPDPD::InputModelPDPDImpl::readWeight(const std::string& name, int64_t tensor_length) {
std::vector<T> tensor_data(tensor_length, 0);

std::ifstream is;
Expand All @@ -119,11 +136,11 @@ std::vector<T> InputModelPDPD::InputModelPDPDImpl::getWeight(const std::string&
}

std::vector<Place::Ptr> InputModelPDPD::InputModelPDPDImpl::getInputs () const {
NOT_IMPLEMENTED("getInputs");
return inputs;
}

std::vector<Place::Ptr> InputModelPDPD::InputModelPDPDImpl::getOutputs () const {
NOT_IMPLEMENTED("getOutputs");
return outputs;
}

Place::Ptr InputModelPDPD::InputModelPDPDImpl::getPlaceByTensorName (const std::string& tensorName) const {
Expand All @@ -150,15 +167,14 @@ void InputModelPDPD::InputModelPDPDImpl::setDefaultShape (Place::Ptr place, cons
NOT_IMPLEMENTED("setDefaultShape");
}

void InputModelPDPD::InputModelPDPDImpl::setPartialShape (Place::Ptr place, const ngraph::PartialShape&) {
void InputModelPDPD::InputModelPDPDImpl::setPartialShape (Place::Ptr place, const ngraph::PartialShape& p_shape) {
NOT_IMPLEMENTED("setPartialShape");
}

InputModelPDPD::InputModelPDPD (const std::string& _path) : _impl{std::make_shared<InputModelPDPDImpl>(_path)} {}

//template<typename T>
std::vector<float> InputModelPDPD::getWeight(const std::string& name, int64_t tensor_length) {
return _impl->getWeight<float>(name, tensor_length);
std::vector<float> InputModelPDPD::readWeight(const std::string& name, int64_t tensor_length) {
return _impl->readWeight<float>(name, tensor_length);
}

std::vector<std::shared_ptr<OpPlacePDPD>> InputModelPDPD::getOpPlaces(int i) const {
Expand Down

0 comments on commit e01d1dc

Please sign in to comment.