未验证 提交 f42d4105 编写于 作者: 石晓伟 提交者: GitHub

read-only modification of cppDesc, test=develop (#3965)

* read-only modification of cppDesc, test=develop

* update io.cc, test=develop

* fix the code style, test=develop
上级 89a0ecd1
......@@ -109,9 +109,10 @@ std::vector<std::string> LightPredictor::GetOutputNames() {
}
// append the names of inputs and outputs into input_names_ and output_names_
void LightPredictor::PrepareFeedFetch() {
auto current_block = cpp_program_desc_.GetBlock<cpp::BlockDesc>(0);
std::vector<cpp::OpDesc*> feeds;
std::vector<cpp::OpDesc*> fetchs;
const cpp::ProgramDesc& prog = cpp_program_desc_;
auto current_block = prog.GetBlock<cpp::BlockDesc>(0);
std::vector<cpp::OpDesc const*> feeds;
std::vector<cpp::OpDesc const*> fetchs;
for (size_t i = 0; i < current_block->OpsSize(); i++) {
auto op = current_block->GetOp<cpp::OpDesc>(i);
if (op->Type() == "feed") {
......@@ -180,6 +181,7 @@ void LightPredictor::BuildRuntimeProgram(const cpp::ProgramDesc& prog) {
}
void LightPredictor::DequantizeWeight() {
const cpp::ProgramDesc& cpp_desc = cpp_program_desc_;
#define PROCESS_CONV2D_DATA() \
for (int64_t i = 0; i < ch; ++i) { \
for (int64_t j = 0; j < offset; ++j) { \
......@@ -207,8 +209,8 @@ void LightPredictor::DequantizeWeight() {
};
Tensor tmp_tensor;
for (size_t i = 0; i < cpp_program_desc_.BlocksSize(); i++) {
auto* block = cpp_program_desc_.GetBlock<cpp::BlockDesc>(i);
for (size_t i = 0; i < cpp_desc.BlocksSize(); i++) {
auto* block = cpp_desc.GetBlock<cpp::BlockDesc>(i);
for (size_t k = 0; k < block->OpsSize(); ++k) {
auto* op_desc = block->GetOp<cpp::OpDesc>(k);
if (is_weight_quantized_op(op_desc)) {
......
......@@ -57,21 +57,35 @@ class VectorView {
public:
typedef vector_view::VectorTraits<T, U> Traits;
explicit VectorView(typename Traits::vector_type const* cvec) {
CHECK(cvec);
cvec_ = cvec;
}
typename Traits::subscript_return_type operator[](size_t i) const {
return cvec_->operator[](i);
}
typename Traits::const_iterator begin() const { return cvec_->begin(); }
typename Traits::const_iterator end() const { return cvec_->end(); }
size_t size() const { return cvec_->size(); }
typename Traits::const_iterator begin() const {
if (!cvec_) {
return typename Traits::const_iterator();
}
return cvec_->begin();
}
typename Traits::const_iterator end() const {
if (!cvec_) {
return typename Traits::const_iterator();
}
return cvec_->end();
}
size_t size() const {
if (!cvec_) {
return 0;
}
return cvec_->size();
}
operator std::vector<T>() const {
VLOG(5) << "Copying elements out of VectorView will damage performance.";
std::vector<T> tmp;
tmp.reserve(cvec_->size());
for (auto val : *cvec_) {
tmp.push_back(val);
tmp.reserve(size());
for (size_t i = 0; i < size(); ++i) {
tmp.push_back(cvec_->operator[](i));
}
return tmp;
}
......
......@@ -234,7 +234,7 @@ void OpAttrsCppToAny(const cpp::OpDesc &cpp_desc, OpDescType *any_desc) {
template <> \
void TransformBlockDescCppToAny<NT::T>(const cpp::T &cpp_desc, \
NT::T *any_desc) { \
auto desc = cpp_desc; \
const cpp::T &desc = cpp_desc; \
any_desc->SetIdx(desc.Idx()); \
any_desc->SetParentIdx(desc.ParentIdx()); \
any_desc->SetForwardBlockIdx(desc.ForwardBlockIdx()); \
......
......@@ -15,20 +15,21 @@
#include "lite/model_parser/flatbuffers/io.h"
#include <memory>
#include <utility>
#include <vector>
namespace paddle {
namespace lite {
namespace fbs {
void LoadModel(const std::string& path, ProgramDesc* prog) {
CHECK(prog);
FILE* file = fopen(path.c_str(), "rb");
fseek(file, 0, SEEK_END);
int64_t size = ftell(file);
int64_t length = ftell(file);
rewind(file);
char* data = new char[size];
size = fread(data, 1, size, file);
std::vector<char> buf(length);
CHECK(fread(buf.data(), 1, length, file));
fclose(file);
std::unique_ptr<char[]> buf(data);
prog->Init(std::move(buf));
}
......
......@@ -62,7 +62,7 @@ class OpDesc : public OpDescAPI {
std::vector<std::string> Output(const std::string& param) const override {
const auto& var = desc_->outputs()->LookupByKey(param.c_str());
std::vector<std::string> args_vec;
if (var->arguments()) {
if (var && var->arguments()) {
args_vec.reserve(var->arguments()->size());
for (const auto& out : *var->arguments()) {
args_vec.push_back(out->str());
......@@ -169,8 +169,7 @@ class OpDesc : public OpDescAPI {
}
bool HasOutput(const std::string& param) const {
NotImplemented();
return false;
return !Output(param).empty();
}
const std::map<std::string, Any>& attrs() const {
......
......@@ -29,16 +29,25 @@ namespace fbs {
class ProgramDesc : public ProgramDescAPI {
public:
ProgramDesc() = default;
explicit ProgramDesc(std::unique_ptr<const char[]> buf) {
Init(std::move(buf));
explicit ProgramDesc(const std::vector<char>& buf) { Init(buf); }
explicit ProgramDesc(std::vector<char>&& buf) {
Init(std::forward<std::vector<char>>(buf));
}
size_t BlocksSize() const override { return desc_->blocks()->size(); }
void Init(const std::vector<char>& buf) {
CHECK(buf.data());
buf_ = buf;
InitProgramDesc();
}
void Init(std::unique_ptr<const char[]> buf) {
CHECK(buf.get() != nullptr);
void Init(std::vector<char>&& buf) {
CHECK(buf.data());
buf_ = std::move(buf);
desc_ = proto::GetProgramDesc(buf_.get());
InitProgramDesc();
}
void InitProgramDesc() {
desc_ = proto::GetProgramDesc(buf_.data());
blocks_.reserve(BlocksSize());
for (size_t idx = 0; idx < BlocksSize(); ++idx) {
blocks_.push_back(BlockDesc(desc_->blocks()->Get(idx)));
......@@ -46,12 +55,12 @@ class ProgramDesc : public ProgramDescAPI {
}
void CopyFrom(const ProgramDesc& other) {
size_t length = strlen(static_cast<const char*>(other.raw_buf()));
std::unique_ptr<char[]> buf(new char[length]);
memcpy(buf.get(), other.raw_buf(), length);
Init(std::move(buf));
buf_ = other.buf();
Init(buf_);
}
size_t BlocksSize() const override { return desc_->blocks()->size(); }
template <typename T>
T const* GetBlock(int32_t idx) const;
......@@ -72,11 +81,11 @@ class ProgramDesc : public ProgramDescAPI {
proto::ProgramDesc const* raw_desc() const { return desc_; }
const void* raw_buf() const { return buf_.get(); }
const std::vector<char>& buf() const { return buf_; }
private:
proto::ProgramDesc const* desc_;
std::unique_ptr<const char[]> buf_;
std::vector<char> buf_;
std::vector<BlockDesc> blocks_;
private:
......
......@@ -51,6 +51,7 @@ struct FBSStrIterator {
flatbuffers::Offset<flatbuffers::String>>::return_type>
VI;
FBSStrIterator() = default;
explicit FBSStrIterator(const VI& iter) { iter_ = iter; }
const VI& raw_iter() const { return iter_; }
......@@ -104,20 +105,21 @@ class VectorView<std::string, Flatbuffers> {
explicit VectorView(typename Traits::vector_type const* cvec) {
cvec_ = cvec;
}
std::string operator[](size_t i) const {
CHECK(cvec_);
return cvec_->operator[](i)->str();
}
std::string operator[](size_t i) const { return cvec_->operator[](i)->str(); }
vector_view::FBSStrIterator begin() const {
CHECK(cvec_);
if (!cvec_) {
return vector_view::FBSStrIterator();
}
return vector_view::FBSStrIterator(cvec_->begin());
}
vector_view::FBSStrIterator end() const {
CHECK(cvec_);
if (!cvec_) {
return vector_view::FBSStrIterator();
}
return vector_view::FBSStrIterator(cvec_->end());
}
size_t size() const {
if (cvec_ == nullptr) {
if (!cvec_) {
return 0;
}
return cvec_->size();
......@@ -126,10 +128,8 @@ class VectorView<std::string, Flatbuffers> {
VLOG(5) << "Copying elements out of VectorView will damage performance.";
std::vector<std::string> tmp;
tmp.reserve(size());
if (cvec_ != nullptr) {
for (auto val : *cvec_) {
tmp.push_back(val->str());
}
for (size_t i = 0; i < size(); ++i) {
tmp.push_back(cvec_->operator[](i)->str());
}
return tmp;
}
......
......@@ -3,4 +3,4 @@ lite_cc_library(cpp_var_desc SRCS var_desc.cc)
lite_cc_library(cpp_block_desc SRCS block_desc.cc)
lite_cc_library(cpp_program_desc SRCS program_desc.cc)
set(cpp_wrapper cpp_op_desc cpp_var_desc cpp_block_desc cpp_program_desc PARENT_SCOPE)
set(cpp_wrapper cpp_program_desc cpp_block_desc cpp_var_desc cpp_op_desc PARENT_SCOPE)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册