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

split the vector_view for conditional compilation, test=develop (#3898)

* split the vector_view, test=develop

* update OpDataTypeTrait, test=develop

* update traits.h, test=develop

* update fbs op_desc, test=develop

* remove header files, test=develop

* add comments, test=develop

* fix dependencies, test=develop
上级 af514209
......@@ -17,5 +17,6 @@
#include "lite/model_parser/base/block_desc.h"
#include "lite/model_parser/base/op_desc.h"
#include "lite/model_parser/base/program_desc.h"
#include "lite/model_parser/base/traits.h"
#include "lite/model_parser/base/var_desc.h"
#include "lite/utils/all.h"
......@@ -15,56 +15,12 @@
#pragma once
#include <string>
#include <vector>
#include "lite/model_parser/base/traits.h"
#include "lite/utils/string.h"
namespace paddle {
namespace lite {
// The AttrType is used to make the proto::AttrType portable.
enum class OpAttrType {
INT = 0,
FLOAT = 1,
STRING = 2,
INTS = 3,
FLOATS = 4,
STRINGS = 5,
BOOLEAN = 6,
BOOLEANS = 7,
BLOCK = 8,
LONG = 9,
BLOCKS = 10,
LONGS = 11,
UNK,
};
template <OpAttrType Type>
struct OpAttrTypeTrait;
template <typename T>
struct OpDataTypeTrait;
#define TYPE_TRAIT_IMPL(T, type__) \
template <> \
struct OpAttrTypeTrait<OpAttrType::T> { \
typedef type__ DT; \
}; \
template <> \
struct OpDataTypeTrait<type__> { \
static constexpr OpAttrType AT = OpAttrType::T; \
static constexpr const char* ATN = #T; \
};
TYPE_TRAIT_IMPL(INT, int32_t);
TYPE_TRAIT_IMPL(FLOAT, float);
TYPE_TRAIT_IMPL(STRING, std::string);
TYPE_TRAIT_IMPL(BOOLEAN, bool);
TYPE_TRAIT_IMPL(LONG, int64_t);
TYPE_TRAIT_IMPL(INTS, std::vector<int>);
TYPE_TRAIT_IMPL(FLOATS, std::vector<float>);
TYPE_TRAIT_IMPL(STRINGS, std::vector<std::string>);
TYPE_TRAIT_IMPL(LONGS, std::vector<int64_t>);
#undef TYPE_TRAIT_IMPL
class OpDescReadAPI {
public:
virtual std::string Type() const = 0;
......
// Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
//
// 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.
#pragma once
#include <string>
#include <vector>
namespace paddle {
namespace lite {
// The AttrType is used to make the proto::AttrType portable.
enum class OpAttrType {
INT = 0,
FLOAT = 1,
STRING = 2,
INTS = 3,
FLOATS = 4,
STRINGS = 5,
BOOLEAN = 6,
BOOLEANS = 7,
BLOCK = 8,
LONG = 9,
BLOCKS = 10,
LONGS = 11,
UNK,
};
struct Standard {};
struct Flatbuffers {};
template <typename T, typename U>
class VectorView;
template <typename T, typename U = Standard>
struct OpDataTypeTrait;
#define ATTR_TYPE_TRAIT_IMPL(T, type__) \
template <typename U> \
struct OpDataTypeTrait<type__, U> { \
typedef type__ ET; \
typedef type__ RT; \
static constexpr OpAttrType AT = OpAttrType::T; \
static constexpr const char* ATN = #T; \
};
#define ATTR_VECTOR_TYPE_TRAIT_IMPL(T, type__) \
template <typename U> \
struct OpDataTypeTrait<std::vector<type__>, U> { \
typedef type__ ET; \
typedef VectorView<type__, U> RT; \
static constexpr OpAttrType AT = OpAttrType::T; \
static constexpr const char* ATN = #T; \
};
ATTR_TYPE_TRAIT_IMPL(BLOCK, int16_t);
ATTR_TYPE_TRAIT_IMPL(INT, int32_t);
ATTR_TYPE_TRAIT_IMPL(FLOAT, float);
ATTR_TYPE_TRAIT_IMPL(STRING, std::string);
ATTR_TYPE_TRAIT_IMPL(BOOLEAN, bool);
ATTR_TYPE_TRAIT_IMPL(LONG, int64_t);
ATTR_VECTOR_TYPE_TRAIT_IMPL(INTS, int32_t);
ATTR_VECTOR_TYPE_TRAIT_IMPL(FLOATS, float);
ATTR_VECTOR_TYPE_TRAIT_IMPL(STRINGS, std::string);
ATTR_VECTOR_TYPE_TRAIT_IMPL(LONGS, int64_t);
#undef ATTR_TYPE_TRAIT_IMPL
#undef ATTR_VECTOR_TYPE_TRAIT_IMPL
} // namespace lite
} // namespace paddle
// Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
//
// 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.
#pragma once
#include <string>
#include <type_traits>
#include <vector>
#include "lite/model_parser/base/traits.h"
#include "lite/utils/cp_logging.h"
namespace paddle {
namespace lite {
namespace vector_view {
template <typename T, typename U = void>
struct ElementTraits {
typedef T element_type;
};
template <typename T, typename U>
struct VectorTraits;
template <typename T>
struct VectorTraits<T, Standard> {
typedef std::vector<T> vector_type;
typedef typename vector_type::const_iterator const_iterator;
typedef typename vector_type::const_reference const_reference;
typedef const_reference subscript_return_type;
};
} // namespace vector_view
// In the process of optimizing the performance of model loading, we found
// that it was necessary to reduce the copying and construction of STL
// containers. So use VectorView to simulate the operation of STL containers
// without copying, such as iteration and subscripting.
//
// Currently, VectorView is applicable to STL vector and Flatbuffers Vector.
// We used the template Traits to unify the behavior of the two, and provided
// an implicit conversion operator from VectorView to STL vector. Please use
// implicit conversion with caution because it will bring significant overhead.
template <typename T, typename U = Flatbuffers>
class VectorView {
public:
typedef vector_view::VectorTraits<T, U> Traits;
explicit VectorView(typename Traits::vector_type const* 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(); }
operator std::vector<T>() {
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);
}
return tmp;
}
~VectorView() = default;
private:
typename Traits::vector_type const* cvec_;
};
} // namespace lite
} // namespace paddle
......@@ -9,6 +9,8 @@ lite_fbs_library(fbs_op_desc SRCS op_desc.cc FBS_DEPS framework_fbs_header)
lite_fbs_library(fbs_var_desc SRCS var_desc.cc FBS_DEPS framework_fbs_header)
lite_fbs_library(fbs_block_desc SRCS block_desc.cc FBS_DEPS framework_fbs_header)
lite_fbs_library(fbs_program_desc SRCS program_desc.cc FBS_DEPS framework_fbs_header)
lite_fbs_library(vector_view SRCS vector_view.cc FBS_DEPS framework_fbs_header)
lite_cc_test(test_vector_view SRCS vector_view_test.cc DEPS vector_view)
lite_cc_test(test_vector_view SRCS vector_view_test.cc)
if (TARGET test_vector_view)
add_dependencies(test_vector_view framework_fbs_header)
endif()
......@@ -37,67 +37,47 @@ std::string OpDesc::GetAttr<std::string>(size_t idx) const {
}
template <>
std::vector<std::string> OpDesc::GetAttr<std::vector<std::string>>(
const std::string& name) const {
lite::VectorView<std::string, Flatbuffers>
OpDesc::GetAttr<std::vector<std::string>>(const std::string& name) const {
const auto& it = desc_->attrs()->LookupByKey(name.c_str());
CHECK(it) << "Attr " << name << "does not exist.";
std::vector<std::string> res;
if (it->strings()) {
res.reserve(it->strings()->size());
for (const auto& v : *it->strings()) {
res.push_back(v->str());
}
}
return res;
return VectorView<std::string>(it->strings());
}
template <>
std::vector<std::string> OpDesc::GetAttr<std::vector<std::string>>(
VectorView<std::string, Flatbuffers> OpDesc::GetAttr<std::vector<std::string>>(
size_t idx) const {
const auto& it = desc_->attrs()->Get(idx);
CHECK(it) << "Attr " << idx << "does not exist.";
std::vector<std::string> res;
if (it->strings()) {
res.reserve(it->strings()->size());
for (const auto& v : *it->strings()) {
res.push_back(v->str());
}
}
return res;
return VectorView<std::string>(it->strings());
}
#define GET_ATTR_IMPL(T, fb_f__) \
template <> \
T OpDesc::GetAttr<T>(const std::string& name) const { \
typename lite::OpDataTypeTrait<T, Flatbuffers>::RT OpDesc::GetAttr<T>( \
const std::string& name) const { \
const auto& it = desc_->attrs()->LookupByKey(name.c_str()); \
return it->fb_f__(); \
} \
template <> \
T OpDesc::GetAttr<T>(size_t idx) const { \
typename lite::OpDataTypeTrait<T, Flatbuffers>::RT OpDesc::GetAttr<T>( \
size_t idx) const { \
const auto& it = desc_->attrs()->Get(idx); \
return it->fb_f__(); \
}
#define GET_ATTRS_IMPL(T, fb_f__) \
template <> \
T OpDesc::GetAttr<T>(const std::string& name) const { \
typename lite::OpDataTypeTrait<T, Flatbuffers>::RT OpDesc::GetAttr<T>( \
const std::string& name) const { \
const auto& it = desc_->attrs()->LookupByKey(name.c_str()); \
T res; \
res.reserve(it->fb_f__()->size()); \
for (const auto& v : *it->fb_f__()) { \
res.push_back(v); \
} \
return res; \
return typename lite::OpDataTypeTrait<T, Flatbuffers>::RT(it->fb_f__()); \
} \
template <> \
T OpDesc::GetAttr<T>(size_t idx) const { \
typename lite::OpDataTypeTrait<T, Flatbuffers>::RT OpDesc::GetAttr<T>( \
size_t idx) const { \
const auto& it = desc_->attrs()->Get(idx); \
T res; \
res.reserve(it->fb_f__()->size()); \
for (const auto& v : *it->fb_f__()) { \
res.push_back(v); \
} \
return res; \
return typename lite::OpDataTypeTrait<T, Flatbuffers>::RT(it->fb_f__()); \
}
GET_ATTR_IMPL(int32_t, i);
......
......@@ -20,6 +20,7 @@
#include "lite/model_parser/base/op_desc.h"
#include "lite/model_parser/flatbuffers/framework_generated.h"
#include "lite/model_parser/flatbuffers/vector_view.h"
#include "lite/utils/all.h"
namespace paddle {
......@@ -116,10 +117,11 @@ class OpDesc : public OpDescReadAPI {
}
template <typename T>
T GetAttr(const std::string& name) const;
typename lite::OpDataTypeTrait<T, Flatbuffers>::RT GetAttr(
const std::string& name) const;
template <typename T>
T GetAttr(size_t idx) const;
typename lite::OpDataTypeTrait<T, Flatbuffers>::RT GetAttr(size_t idx) const;
OpDesc() = delete;
......
// Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
//
// 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 "lite/model_parser/flatbuffers/vector_view.h"
namespace paddle {
namespace lite {} // namespace lite
} // namespace paddle
......@@ -15,22 +15,13 @@
#pragma once
#include <string>
#include <type_traits>
#include <vector>
#include "flatbuffers/flatbuffers.h"
#include "lite/utils/cp_logging.h"
#include "lite/model_parser/base/vector_view.h"
namespace paddle {
namespace lite {
namespace fbs {
struct Flatbuffers {};
struct Standand {};
template <typename T, typename U = void>
struct ElementTraits {
typedef T element_type;
};
namespace vector_view {
template <typename T>
struct ElementTraits<T*,
......@@ -43,9 +34,6 @@ struct ElementTraits<std::string, void> {
typedef flatbuffers::Offset<flatbuffers::String> element_type;
};
template <typename T, typename U>
struct VectorTraits;
template <typename T>
struct VectorTraits<T, Flatbuffers> {
typedef flatbuffers::Vector<typename ElementTraits<T>::element_type>
......@@ -56,42 +44,6 @@ struct VectorTraits<T, Flatbuffers> {
typedef value_type subscript_return_type;
};
template <typename T>
struct VectorTraits<T, Standand> {
typedef std::vector<T> vector_type;
typedef typename vector_type::const_iterator const_iterator;
typedef typename vector_type::const_reference const_reference;
typedef const_reference subscript_return_type;
};
template <typename T, typename U = Flatbuffers>
class VectorView {
public:
typedef VectorTraits<T, U> Traits;
explicit VectorView(typename Traits::vector_type const* 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(); }
operator std::vector<T>() {
VLOG(10) << "Copying elements out of VectorView will damage performance.";
std::vector<T> tmp;
tmp.reserve(cvec_->size());
for (auto val : *cvec_) {
tmp.push_back(val);
}
return tmp;
}
~VectorView() = default;
private:
typename Traits::vector_type const* cvec_;
};
struct FBSStrIterator {
typedef flatbuffers::VectorIterator<
flatbuffers::Offset<flatbuffers::String>,
......@@ -143,19 +95,25 @@ struct FBSStrIterator {
VI iter_;
};
} // namespace vector_view
template <>
class VectorView<std::string, Flatbuffers> {
public:
typedef VectorTraits<std::string, Flatbuffers> Traits;
typedef vector_view::VectorTraits<std::string, Flatbuffers> Traits;
explicit VectorView(typename Traits::vector_type const* cvec) {
cvec_ = cvec;
}
std::string operator[](size_t i) const { return cvec_->operator[](i)->str(); }
FBSStrIterator begin() const { return FBSStrIterator(cvec_->begin()); }
FBSStrIterator end() const { return FBSStrIterator(cvec_->end()); }
vector_view::FBSStrIterator begin() const {
return vector_view::FBSStrIterator(cvec_->begin());
}
vector_view::FBSStrIterator end() const {
return vector_view::FBSStrIterator(cvec_->end());
}
size_t size() const { return cvec_->size(); }
operator std::vector<std::string>() {
VLOG(10) << "Copying elements out of VectorView will damage performance.";
VLOG(5) << "Copying elements out of VectorView will damage performance.";
std::vector<std::string> tmp;
tmp.reserve(cvec_->size());
for (auto val : *cvec_) {
......@@ -169,6 +127,5 @@ class VectorView<std::string, Flatbuffers> {
typename Traits::vector_type const* cvec_;
};
} // namespace fbs
} // namespace lite
} // namespace paddle
......@@ -24,7 +24,7 @@ namespace lite {
TEST(VectorView, std_vector) {
std::vector<int64_t> vector{1, 2, 3};
fbs::VectorView<int64_t, fbs::Standand> vector_view(&vector);
VectorView<int64_t, Standard> vector_view(&vector);
size_t i = 0;
for (const auto& value : vector_view) {
EXPECT_EQ(value, vector[i]);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册