未验证 提交 36764563 编写于 作者: xiebaiyuan's avatar xiebaiyuan 提交者: GitHub

Merge pull request #1539 from hjchen2/backup

Replace C++ typeid with self-defined type_id to avoid compiler difference, and fix gpu bug
/* Copyright (c) 2018 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
#ifdef PADDLE_EXECUTOR_MULTITHREAD
#include <string>
#include <unordered_map>
#include <vector>
#include "framework/operator.h"
namespace paddle_mobile {
class depCore {
public:
template <typename Dtype>
void analysisDep(
const std::vector<std::shared_ptr<framework::OperatorBase<Dtype>>>& ops) {
std::unordered_map<std::string, int> vars;
size_t nop = ops.size();
deps.resize(nop);
next.resize(nop);
for (size_t i = 0; i < nop; i++) {
const auto& op = ops[i];
for (const auto& kv : op->Inputs()) {
for (const auto& v : kv.second) {
if (vars.find(v) == vars.end()) {
continue;
}
int di = vars[v];
if (di == i) {
continue;
}
if (std::find(deps[i].begin(), deps[i].end(), di) != deps[i].end()) {
continue;
}
deps[i].push_back(di);
next[di].push_back(i);
}
}
for (const auto& kv : op->Outputs()) {
for (const auto& v : kv.second) {
vars[v] = i;
}
}
}
}
const std::vector<int>& getNext(int i) { return next[i]; }
const std::vector<int>& getDeps(int i) { return deps[i]; }
std::vector<std::vector<int>> deps;
std::vector<std::vector<int>> next;
};
} // namespace paddle_mobile
#endif
/* Copyright (c) 2018 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
#define EXPORT __attribute__((visibility("default")))
......@@ -15,32 +15,173 @@ limitations under the License. */
#pragma once
#include <functional>
#include <map>
#include <string>
#include <vector>
#include "framework/attribute.h"
#include "framework/scope.h"
namespace paddle_mobile {
typedef enum {
_void = 0,
_float,
_int,
_uint16_t,
_double,
_int64_t,
_size_t,
_int16_t,
_int8_t,
_uint8_t,
_bool,
_string,
_floats = 100,
_ints,
_int64_ts,
_size_ts,
_bools,
_strings,
_const_float = 200,
_const_int,
_block = 300,
_tensor,
_lod_tensor,
_blocks,
_tensors,
_lod_tensors,
_p_block = 400,
_p_tensor,
_p_lod_tensor,
_p_blocks,
_p_tensors,
_p_lod_tensors,
_scopes = 500,
_selected_rows,
_dim0 = 600,
_dim1,
_dim2,
_dim3,
_dim4,
_dim5,
_dim6,
_dim7,
_dim8,
_dim9,
#ifdef PADDLE_MOBILE_CL
_cl_image,
#endif
} kTypeId_t;
template <typename T>
struct TypeIdWrapper {
inline std::string name();
inline kTypeId_t hash_code();
};
template <typename T>
struct type_id {
const kTypeId_t hash_code() const { return TypeIdWrapper<T>().hash_code(); }
const std::string name() const { return TypeIdWrapper<T>().name(); }
template <typename OtherType>
bool operator==(const type_id<OtherType> &operand) const {
return this->hash_code() == operand.hash_code();
}
};
#define OVERIDE_TYPEID_OPERATOR(oprand) \
template <typename T> \
inline bool operator oprand(const kTypeId_t &t0, const type_id<T> &t1) { \
return t0 oprand t1.hash_code(); \
} \
template <typename T> \
inline bool operator oprand(const type_id<T> &t0, const kTypeId_t &t1) { \
return t1 oprand t0.hash_code(); \
}
OVERIDE_TYPEID_OPERATOR(==)
OVERIDE_TYPEID_OPERATOR(!=)
namespace framework {
template <typename Dtype>
class OperatorBase;
class OpDesc;
class BlockDesc;
class InferShapeContext;
class Tensor;
class LoDTensor;
class SelectedRows;
class Scope;
#ifdef PADDLE_MOBILE_CL
class CLImage;
#endif
template <int>
struct Dim;
} // namespace framework
using VariableNameMap = std::map<std::string, std::vector<std::string>>;
#define REGISTER_TYPE_ID(Type, TypeName) \
template <> \
struct TypeIdWrapper<Type> { \
inline std::string name() { return std::string(#TypeName); } \
inline kTypeId_t hash_code() { return kTypeId_t::TypeName; } \
};
REGISTER_TYPE_ID(void, _void)
REGISTER_TYPE_ID(float, _float)
REGISTER_TYPE_ID(int, _int)
REGISTER_TYPE_ID(uint16_t, _uint16_t)
REGISTER_TYPE_ID(double, _double)
REGISTER_TYPE_ID(int64_t, _int64_t)
REGISTER_TYPE_ID(size_t, _size_t)
REGISTER_TYPE_ID(int16_t, _int16_t)
REGISTER_TYPE_ID(int8_t, _int8_t)
REGISTER_TYPE_ID(uint8_t, _uint8_t)
REGISTER_TYPE_ID(bool, _bool)
REGISTER_TYPE_ID(std::string, _string)
REGISTER_TYPE_ID(std::vector<float>, _floats)
REGISTER_TYPE_ID(std::vector<int>, _ints)
REGISTER_TYPE_ID(std::vector<int64_t>, _int64_ts)
REGISTER_TYPE_ID(std::vector<size_t>, _size_ts)
REGISTER_TYPE_ID(std::vector<bool>, _bools)
REGISTER_TYPE_ID(std::vector<std::string>, _strings)
REGISTER_TYPE_ID(float const, _const_float)
REGISTER_TYPE_ID(int const, _const_int)
REGISTER_TYPE_ID(framework::BlockDesc, _block)
REGISTER_TYPE_ID(framework::Tensor, _tensor)
REGISTER_TYPE_ID(framework::LoDTensor, _lod_tensor)
REGISTER_TYPE_ID(std::vector<framework::BlockDesc>, _blocks)
REGISTER_TYPE_ID(std::vector<framework::Tensor>, _tensors)
REGISTER_TYPE_ID(std::vector<framework::LoDTensor>, _lod_tensors)
REGISTER_TYPE_ID(framework::BlockDesc *, _p_block)
REGISTER_TYPE_ID(framework::Tensor *, _p_tensor)
REGISTER_TYPE_ID(framework::LoDTensor *, _p_lod_tensor)
REGISTER_TYPE_ID(std::vector<framework::BlockDesc *>, _p_blocks)
REGISTER_TYPE_ID(std::vector<framework::Tensor *>, _p_tensors)
REGISTER_TYPE_ID(std::vector<framework::LoDTensor *>, _p_lod_tensors)
REGISTER_TYPE_ID(std::vector<framework::Scope *>, _scopes);
REGISTER_TYPE_ID(framework::SelectedRows, _selected_rows)
REGISTER_TYPE_ID(framework::Dim<0>, _dim0)
REGISTER_TYPE_ID(framework::Dim<1>, _dim1)
REGISTER_TYPE_ID(framework::Dim<2>, _dim2)
REGISTER_TYPE_ID(framework::Dim<3>, _dim3)
REGISTER_TYPE_ID(framework::Dim<4>, _dim4)
REGISTER_TYPE_ID(framework::Dim<5>, _dim5)
REGISTER_TYPE_ID(framework::Dim<6>, _dim6)
REGISTER_TYPE_ID(framework::Dim<7>, _dim7)
REGISTER_TYPE_ID(framework::Dim<8>, _dim8)
REGISTER_TYPE_ID(framework::Dim<9>, _dim9)
#ifdef PADDLE_MOBILE_CL
REGISTER_TYPE_ID(framework::CLImage, _cl_image)
#endif
} // namespace paddle_mobile
template <typename Dtype>
using OpCreator = std::function<framework::OperatorBase<Dtype> *(
const std::string & /*type*/, const VariableNameMap & /*inputs*/,
const VariableNameMap & /*outputs*/,
const framework::AttributeMap & /*attrs*/, framework::Scope * /*scope*/)>;
namespace std {
using InferVarTypeFN = std::function<void(const framework::OpDesc & /*op_desc*/,
framework::BlockDesc * /*block*/)>;
template <>
struct hash<paddle_mobile::kTypeId_t> {
size_t operator()(const paddle_mobile::kTypeId_t &t) const {
return std::hash<int>{}(static_cast<int>(t));
}
};
using InferShapeFN = std::function<void(framework::InferShapeContext *)>;
}; // namespace paddle_mobile
} // namespace std
......@@ -14,6 +14,7 @@ limitations under the License. */
#pragma once
#include <map>
#include <string>
#include <unordered_map>
#include <utility>
......@@ -211,4 +212,6 @@ extern std::unordered_map<
std::string, std::pair<std::vector<std::string>, std::vector<std::string>>>
op_input_output_key;
typedef std::map<std::string, std::vector<std::string>> VariableNameMap;
} // namespace paddle_mobile
......@@ -19,6 +19,7 @@ limitations under the License. */
#include <string>
#include "common/enforce.h"
#include "common/log.h"
#include "common/type_define.h"
namespace paddle_mobile {
......@@ -33,11 +34,11 @@ struct VariantHelper {
? sizeof(F)
: VariantHelper<Ts...>::size;
inline static void Destroy(size_t id, void *data) {
if (id == typeid(F).hash_code()) {
inline static void Destroy(kTypeId_t type, void *data) {
if (type == type_id<F>()) {
reinterpret_cast<F *>(data)->~F();
} else {
VariantHelper<Ts...>::Destroy(id, data);
VariantHelper<Ts...>::Destroy(type, data);
}
}
};
......@@ -45,11 +46,11 @@ struct VariantHelper {
template <typename F>
struct VariantHelper<F> {
static const size_t size = sizeof(F);
inline static void Destroy(size_t id, void *data) {
if (id == typeid(F).hash_code()) {
// reinterpret_cast<F*>(data)->~F();
inline static void Destroy(kTypeId_t type, void *data) {
if (type == type_id<F>()) {
// reinterpret_cast<F*>(data)->~F();
} else {
// std::cout << "未匹配到 " << std::endl;
// std::cout << "未匹配到 " << std::endl;
}
}
};
......@@ -57,7 +58,7 @@ struct VariantHelper<F> {
template <size_t size>
class RawData {
public:
char data[size];
char data[size]; // NOLINT
RawData() {}
RawData(const RawData &raw_data) { memcpy(data, raw_data.data, size); }
......@@ -69,32 +70,33 @@ class RawData {
template <typename... Ts>
struct Variant {
Variant() : type_(invalid_type()) {}
Variant(const Variant &variant) {
type_id = variant.type_id;
data = variant.data;
type_ = variant.type_;
data_ = variant.data_;
}
Variant() : type_id(invalid_type()) {}
~Variant() {
// helper::Destroy(type_id, &data);
virtual ~Variant() {
// helper::Destroy(type_id, &data);
}
template <typename T, typename... Args>
void Set(Args &&... args) {
helper::Destroy(type_id, data.data);
new (data.data) T(std::forward<Args>(args)...);
type_id = typeid(T).hash_code();
helper::Destroy(type_, data_.data);
new (data_.data) T(std::forward<Args>(args)...);
type_ = type_id<T>().hash_code();
}
void SetString(std::string &string) {
helper::Destroy(type_id, data.data);
type_id = typeid(std::string).hash_code();
strcpy(data.data, string.c_str());
void SetString(const std::string &string) {
helper::Destroy(type_, data_.data);
type_ = type_id<std::string>().hash_code();
strcpy(data_.data, string.c_str()); // NOLINT
}
std::string GetString() const {
if (type_id == typeid(std::string).hash_code()) {
return std::string(data.data);
if (type_ == type_id<std::string>()) {
return std::string(data_.data);
} else {
PADDLE_MOBILE_THROW_EXCEPTION(
" bad cast in variant data type not a string ");
......@@ -104,28 +106,25 @@ struct Variant {
template <typename T>
T &Get() const {
if (type_id == typeid(std::string).hash_code()) {
if (type_ == type_id<std::string>()) {
PADDLE_MOBILE_THROW_EXCEPTION(
"Please use getString to get an string (to avoid of an issue with "
"gcc "
"stl lib with string copy)");
exit(0);
} else if (type_id == typeid(T).hash_code()) {
return *const_cast<T *>(reinterpret_cast<const T *>(data.data));
} else {
PADDLE_MOBILE_THROW_EXCEPTION(" bad cast in variant");
exit(0);
return *const_cast<T *>(reinterpret_cast<const T *>(data_.data));
}
}
size_t TypeId() const { return type_id; }
kTypeId_t TypeId() const { return type_; }
private:
static inline size_t invalid_type() { return typeid(void).hash_code(); }
static inline kTypeId_t invalid_type() { return type_id<void>().hash_code(); }
typedef VariantHelper<Ts...> helper;
size_t type_id;
kTypeId_t type_ = type_id<void>().hash_code();
// todo use an anto size to suite this.
RawData<64> data;
RawData<64> data_;
};
template <typename T>
......
......@@ -28,8 +28,8 @@ namespace fpga {
void format_image(framework::Tensor *image_tensor) {
auto dims = image_tensor->dims();
auto channel = dims[1], height = dims[2], width = dims[3];
std::type_index input_type = image_tensor->type();
if (input_type == typeid(float)) {
kTypeId_t input_type = image_tensor->type();
if (input_type == type_id<float>()) {
auto data_ptr = image_tensor->data<float>();
auto external_ptr = reinterpret_cast<float *>(image_tensor->external_data);
float *p_data = external_ptr == nullptr ? data_ptr : external_ptr;
......@@ -51,7 +51,7 @@ void format_image(framework::Tensor *image_tensor) {
}
void format_ofm(framework::Tensor *ofm_tensor) {
if (ofm_tensor->type() == typeid(float)) {
if (ofm_tensor->type() == type_id<float>()) {
format_fp32_ofm(ofm_tensor);
} else {
format_fp16_ofm(ofm_tensor);
......@@ -72,7 +72,7 @@ void format_fp16_ofm(framework::Tensor *ofm_tensor) {
auto p = fpga_malloc(memory_size);
// memset(p, 0, memory_size);
ofm_tensor->reset_data_ptr(p);
ofm_tensor->set_type(typeid(half));
ofm_tensor->set_type(type_id<half>().hash_code());
ofm_tensor->fpga_data_num = memory_size / sizeof(half);
fpga::fpga_flush(p, memory_size);
}
......@@ -92,7 +92,7 @@ void format_fp16_ofm(framework::Tensor *ofm_tensor, framework::DDim dims) {
auto p = fpga_malloc(memory_size);
// memset(p, 0, memory_size);
ofm_tensor->reset_data_ptr(p);
ofm_tensor->set_type(typeid(half));
ofm_tensor->set_type(type_id<half>().hash_code());
ofm_tensor->fpga_data_num = memory_size / sizeof(half);
fpga::fpga_flush(p, memory_size);
}
......@@ -112,7 +112,7 @@ void format_fp32_ofm(framework::Tensor *ofm_tensor) {
auto p = fpga_malloc(memory_size);
// memset(p, 0, memory_size);
ofm_tensor->reset_data_ptr(p);
ofm_tensor->set_type(typeid(float));
ofm_tensor->set_type(type_id<float>().hash_code());
ofm_tensor->fpga_data_num = memory_size / sizeof(float);
fpga::fpga_flush(p, memory_size);
}
......@@ -171,7 +171,7 @@ void format_filter(framework::Tensor *filter_tensor, float max_value,
filter::format_filter(&new_data, num, channel, height, width, group_num,
max_value);
filter_tensor->reset_data_ptr(new_data);
filter_tensor->set_type(typeid(int8_t));
filter_tensor->set_type(type_id<int8_t>().hash_code());
}
void format_dwconv_filter(framework::Tensor *filter_tensor, float *scale_ptr) {
auto dims = filter_tensor->dims();
......@@ -182,7 +182,7 @@ void format_dwconv_filter(framework::Tensor *filter_tensor, float *scale_ptr) {
fpga_copy(new_data, data_ptr, memory_size);
filter::format_dwconv_filter(&new_data, num, height, width, scale_ptr);
filter_tensor->reset_data_ptr(new_data);
filter_tensor->set_type(typeid(int16_t));
filter_tensor->set_type(type_id<int16_t>().hash_code());
}
void format_DWDconv_filter(framework::Tensor *filter_tensor, float *scale_ptr,
......@@ -207,7 +207,7 @@ void format_DWDconv_filter(framework::Tensor *filter_tensor, float *scale_ptr,
// framework::make_ddim({num, 1, height, width});
// filter_tensor->Resize(dims_new);
filter_tensor->reset_data_ptr(new_data);
filter_tensor->set_type(typeid(int16_t));
filter_tensor->set_type(type_id<int16_t>().hash_code());
}
void format_fc_filter(framework::Tensor *filter_tensor, float max_value) {
......@@ -222,7 +222,7 @@ void format_fc_filter(framework::Tensor *filter_tensor, float max_value) {
filter::format_fc_filter(&new_data, num, channel, height, width, 1,
max_value);
filter_tensor->reset_data_ptr(new_data);
filter_tensor->set_type(typeid(int8_t));
filter_tensor->set_type(type_id<int8_t>().hash_code());
}
void format_deconv_filter(framework::Tensor *filter_tensor, float max_value,
int group_num, int stride) {
......@@ -249,7 +249,7 @@ void format_deconv_filter(framework::Tensor *filter_tensor, float max_value,
framework::make_ddim({num, channel, height, width});
filter_tensor->Resize(dims_new);
filter_tensor->reset_data_ptr(new_data);
filter_tensor->set_type(typeid(int8_t));
filter_tensor->set_type(type_id<int8_t>().hash_code());
}
void format_bias_scale_array(float **bias_scale_array,
......@@ -273,7 +273,7 @@ void format_concat_output(framework::Tensor *out, int height, int width,
auto ddim = framework::make_ddim({1, sum_channel, height, width});
out->Resize(ddim);
out->reset_data_ptr(data_ptr);
out->set_type(typeid(half));
out->set_type(type_id<half>().hash_code());
}
void format_conv_data(framework::Tensor *filter_tensor,
framework::Tensor *ofm_tensor, float **bs_ptr,
......
......@@ -128,31 +128,30 @@ class Attribute {
template <typename Vistor>
static typename Vistor::type_t ApplyVistor(Vistor vistor, Attribute attr) {
if (attr.variant_.TypeId() == typeid(int).hash_code()) { // NOLINT
if (attr.variant_.TypeId() == type_id<int>()) { // NOLINT
return vistor(attr.variant_.Get<int>());
} else if (attr.variant_.TypeId() == typeid(float).hash_code()) { // NOLINT
} else if (attr.variant_.TypeId() == type_id<float>()) { // NOLINT
return vistor(attr.variant_.Get<float>());
} else if (attr.variant_.TypeId() == typeid(string).hash_code()) {
} else if (attr.variant_.TypeId() == type_id<string>()) {
return vistor(attr.variant_.GetString());
} else if (attr.variant_.TypeId() == typeid(vector<int>).hash_code()) {
} else if (attr.variant_.TypeId() == type_id<vector<int>>()) {
return vistor(attr.variant_.Get<vector<int>>());
} else if (attr.variant_.TypeId() == typeid(vector<float>).hash_code()) {
} else if (attr.variant_.TypeId() == type_id<vector<float>>()) {
return vistor(attr.variant_.Get<vector<float>>());
} else if (attr.variant_.TypeId() == typeid(vector<string>).hash_code()) {
} else if (attr.variant_.TypeId() == type_id<vector<string>>()) {
return vistor(attr.variant_.Get<vector<string>>());
} else if (attr.variant_.TypeId() == typeid(bool).hash_code()) { // NOLINT
} else if (attr.variant_.TypeId() == type_id<bool>()) { // NOLINT
return vistor(attr.variant_.Get<bool>());
} else if (attr.variant_.TypeId() == typeid(vector<bool>).hash_code()) {
} else if (attr.variant_.TypeId() == type_id<vector<bool>>()) {
return vistor(attr.variant_.Get<vector<bool>>());
} else if (attr.variant_.TypeId() == typeid(int64_t).hash_code()) {
} else if (attr.variant_.TypeId() == type_id<int64_t>()) {
return vistor(attr.variant_.Get<int64_t>());
} else if (attr.variant_.TypeId() ==
typeid(framework::BlockDesc *).hash_code()) {
} else if (attr.variant_.TypeId() == type_id<framework::BlockDesc *>()) {
return vistor(attr.variant_.Get<framework::BlockDesc *>());
} else if (attr.variant_.TypeId() ==
typeid(vector<framework::BlockDesc *>).hash_code()) {
type_id<vector<framework::BlockDesc *>>()) {
return vistor(attr.variant_.Get<vector<framework::BlockDesc *>>());
} else if (attr.variant_.TypeId() == typeid(vector<int64_t>).hash_code()) {
} else if (attr.variant_.TypeId() == type_id<vector<int64_t>>()) {
return vistor(attr.variant_.Get<vector<int64_t>>());
} else {
PADDLE_MOBILE_THROW_EXCEPTION("type not support");
......
......@@ -53,12 +53,12 @@ class CLTensor : TensorBase {
int64_t size = numel() * sizeof(T);
holder_.reset(new PlaceholderImpl(
size, reinterpret_cast<void *>(const_cast<T *>(data)), typeid(T),
context_, command_queue_));
size, reinterpret_cast<void *>(const_cast<T *>(data)),
type_id<T>().hash_code(), context_, command_queue_));
return reinterpret_cast<cl_mem>(holder_->ptr());
}
inline cl_mem mutable_data(std::type_index type) {
inline cl_mem mutable_data(kTypeId_t type) {
if (holder_ != nullptr) {
holder_->set_type(type);
}
......@@ -77,7 +77,7 @@ class CLTensor : TensorBase {
*/
template <typename T>
inline cl_mem mutable_data() {
return reinterpret_cast<cl_mem>(mutable_data(typeid(T)));
return reinterpret_cast<cl_mem>(mutable_data(type_id<T>().hash_code()));
}
/**
......@@ -132,7 +132,7 @@ class CLTensor : TensorBase {
void *host_ptr_ = nullptr;
struct PlaceholderImpl : public Placeholder {
PlaceholderImpl(size_t size, void *input, std::type_index type,
PlaceholderImpl(size_t size, void *input, kTypeId_t type,
cl_context context, cl_command_queue command_queue)
: ptr_(clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
size, reinterpret_cast<void *>(input), NULL)),
......@@ -142,7 +142,7 @@ class CLTensor : TensorBase {
context_(context),
command_queue_(command_queue) {}
PlaceholderImpl(size_t size, std::type_index type, cl_context context,
PlaceholderImpl(size_t size, kTypeId_t type, cl_context context,
cl_command_queue command_queue)
: ptr_(clCreateBuffer(context, CL_MEM_READ_WRITE, size, NULL, NULL)),
size_(size),
......@@ -155,9 +155,9 @@ class CLTensor : TensorBase {
virtual void *ptr() const { return static_cast<void *>(ptr_.get()); }
virtual std::type_index type() const { return type_; }
virtual kTypeId_t type() const { return type_; }
virtual void set_type(std::type_index type) { type_ = type; }
virtual void set_type(kTypeId_t type) { type_ = type; }
virtual void resize(size_t size) {
if (size > capatity_) {
......@@ -175,7 +175,7 @@ class CLTensor : TensorBase {
size_t capatity_;
/* the current type of memory */
std::type_index type_;
kTypeId_t type_;
cl_context context_;
cl_command_queue command_queue_;
......
......@@ -16,17 +16,17 @@ limitations under the License. */
#include <stdint.h>
#include <string>
#include <unordered_map>
#include "common/type_define.h"
namespace paddle_mobile {
namespace framework {
struct DataTypeMap {
std::unordered_map<std::type_index,
_PaddleMobile__Framework__Proto__VarType__Type>
std::unordered_map<kTypeId_t, _PaddleMobile__Framework__Proto__VarType__Type>
cpp_to_proto_;
std::unordered_map<int, std::type_index> proto_to_cpp_;
std::unordered_map<int, kTypeId_t> proto_to_cpp_;
std::unordered_map<int, std::string> proto_to_str_;
std::unordered_map<std::type_index, size_t> cpp_to_size_;
std::unordered_map<kTypeId_t, size_t> cpp_to_size_;
};
static DataTypeMap* InitDataTypeMap();
......@@ -42,10 +42,11 @@ template <typename T>
static inline void RegisterType(
DataTypeMap* map, _PaddleMobile__Framework__Proto__VarType__Type proto_type,
const std::string& name) {
map->proto_to_cpp_.emplace(static_cast<int>(proto_type), typeid(T));
map->cpp_to_proto_.emplace(typeid(T), proto_type);
map->proto_to_cpp_.emplace(static_cast<int>(proto_type),
type_id<T>().hash_code());
map->cpp_to_proto_.emplace(type_id<T>().hash_code(), proto_type);
map->proto_to_str_.emplace(static_cast<int>(proto_type), name);
map->cpp_to_size_.emplace(typeid(T), sizeof(T));
map->cpp_to_size_.emplace(type_id<T>().hash_code(), sizeof(T));
}
static DataTypeMap* InitDataTypeMap() {
......@@ -70,17 +71,15 @@ static DataTypeMap* InitDataTypeMap() {
return retv;
}
_PaddleMobile__Framework__Proto__VarType__Type ToDataType(
std::type_index type) {
_PaddleMobile__Framework__Proto__VarType__Type ToDataType(kTypeId_t type) {
auto it = gDataTypeMap().cpp_to_proto_.find(type);
if (it != gDataTypeMap().cpp_to_proto_.end()) {
return it->second;
}
PADDLE_MOBILE_THROW_EXCEPTION("Not support %s as tensor type", type.name());
PADDLE_MOBILE_THROW_EXCEPTION("Not support %d as tensor type", type);
}
std::type_index ToTypeIndex(
_PaddleMobile__Framework__Proto__VarType__Type type) {
kTypeId_t ToTypeIndex(_PaddleMobile__Framework__Proto__VarType__Type type) {
auto it = gDataTypeMap().proto_to_cpp_.find(static_cast<int>(type));
if (it != gDataTypeMap().proto_to_cpp_.end()) {
return it->second;
......
......@@ -15,18 +15,17 @@ limitations under the License. */
#pragma once
#include <string>
#include <typeindex>
#include "common/enforce.h"
#include "common/type_define.h"
#include "framework/framework.pb-c.h"
namespace paddle_mobile {
namespace framework {
extern _PaddleMobile__Framework__Proto__VarType__Type ToDataType(
std::type_index type);
extern std::type_index ToTypeIndex(
_PaddleMobile__Framework__Proto__VarType__Type type);
_PaddleMobile__Framework__Proto__VarType__Type ToDataType(kTypeId_t type);
kTypeId_t ToTypeIndex(_PaddleMobile__Framework__Proto__VarType__Type type);
inline _PaddleMobile__Framework__Proto__VarType__Type ToDataType(int type) {
return static_cast<_PaddleMobile__Framework__Proto__VarType__Type>(type);
......
......@@ -22,7 +22,7 @@ limitations under the License. */
#include "common/enforce.h"
#include "common/variant.h"
#include "dim.h"
#include "framework/dim.h"
namespace paddle_mobile {
namespace framework {
......@@ -40,25 +40,25 @@ struct DDim {
template <typename Vistor>
static typename Vistor::type_t ApplyVistor(Vistor vistor, const DDim &d) {
if (d.var.TypeId() == typeid(Dim<0>).hash_code()) {
if (d.var.TypeId() == type_id<Dim<0>>()) {
return vistor(d.var.Get<Dim<0>>());
} else if (d.var.TypeId() == typeid(Dim<1>).hash_code()) {
} else if (d.var.TypeId() == type_id<Dim<1>>()) {
return vistor(d.var.Get<Dim<1>>());
} else if (d.var.TypeId() == typeid(Dim<2>).hash_code()) {
} else if (d.var.TypeId() == type_id<Dim<2>>()) {
return vistor(d.var.Get<Dim<2>>());
} else if (d.var.TypeId() == typeid(Dim<3>).hash_code()) {
} else if (d.var.TypeId() == type_id<Dim<3>>()) {
return vistor(d.var.Get<Dim<3>>());
} else if (d.var.TypeId() == typeid(Dim<4>).hash_code()) {
} else if (d.var.TypeId() == type_id<Dim<4>>()) {
return vistor(d.var.Get<Dim<4>>());
} else if (d.var.TypeId() == typeid(Dim<5>).hash_code()) {
} else if (d.var.TypeId() == type_id<Dim<5>>()) {
return vistor(d.var.Get<Dim<5>>());
} else if (d.var.TypeId() == typeid(Dim<6>).hash_code()) {
} else if (d.var.TypeId() == type_id<Dim<6>>()) {
return vistor(d.var.Get<Dim<6>>());
} else if (d.var.TypeId() == typeid(Dim<7>).hash_code()) {
} else if (d.var.TypeId() == type_id<Dim<7>>()) {
return vistor(d.var.Get<Dim<7>>());
} else if (d.var.TypeId() == typeid(Dim<8>).hash_code()) {
} else if (d.var.TypeId() == type_id<Dim<8>>()) {
return vistor(d.var.Get<Dim<8>>());
} else if (d.var.TypeId() == typeid(Dim<9>).hash_code()) {
} else if (d.var.TypeId() == type_id<Dim<9>>()) {
return vistor(d.var.Get<Dim<9>>());
} else {
PADDLE_MOBILE_ENFORCE(false, " dim not support");
......
......@@ -62,7 +62,7 @@ Executor<Device, T>::Executor(const Program<Device> &program,
use_optimize_ ? program_.optimizeProgram : program_.originProgram;
PADDLE_MOBILE_ENFORCE(program_desc_ != nullptr,
"program_desc_ should not be nullptr");
#ifndef PADDLE_MOBILE_FPGA
#if !defined(PADDLE_MOBILE_FPGA) && !defined(PADDLE_MOBILE_CL)
pass::MemoryOptPass()(program_desc_.get(), program_.scope.get());
#endif
// resize feed and fetch list
......@@ -302,25 +302,9 @@ bool Executor<Device, T>::varInputMemory(
const std::shared_ptr<VarDesc> &var_desc, Variable *var) const {
#ifdef PADDLE_MOBILE_FPGA
framework::LoDTensor *tensor = var->template GetMutable<LoDTensor>();
tensor->init(typeid(float));
tensor->init(type_id<float>().hash_code());
return true;
#endif
auto TypeId = [](const VarType_Type &type) -> std::type_index {
switch (type) {
case VARTYPE_TYPE_BOOL:
return typeid(bool);
case VARTYPE_TYPE_FP32:
return typeid(float);
case VARTYPE_TYPE_INT8:
return typeid(int8_t);
case VARTYPE_TYPE_INT32:
return typeid(int);
case VARTYPE_TYPE_INT64:
return typeid(int64_t);
default:
PADDLE_MOBILE_THROW_EXCEPTION("got unhandled var type `%d`", type);
}
};
auto type = var_desc->Type();
if (type == VARTYPE_TYPE_LOD_TENSOR) {
......@@ -390,13 +374,6 @@ void Executor<Device, T>::SetInput(const Tensor &input,
framework::LoDTensor &target =
feed_var->template GetMutable<framework::LoDTensorArray>()->at(index);
if (config_.load_when_predict) {
if (input_dim_last_ != input.dims()) {
InitNoPersistableMemory(input);
input_dim_last_ = input.dims();
}
}
target.Resize(input.dims());
target.ShareDataWith(input);
}
......@@ -412,13 +389,6 @@ void Executor<Device, T>::SetInput(const LoDTensor &input,
framework::LoDTensor &target =
feed_var->template GetMutable<framework::LoDTensorArray>()->at(index);
if (config_.load_when_predict) {
if (input_dim_last_ != input.dims()) {
InitNoPersistableMemory(input);
input_dim_last_ = input.dims();
}
}
target.Resize(input.dims());
target.ShareDataWith(input);
target.set_lod(input.lod());
......
......@@ -268,6 +268,9 @@ LOAD_OP1(sequence_expand, CPU);
#ifdef SEQUENCE_POOL_OP
LOAD_OP1(sequence_pool, CPU);
#endif
#ifdef SEQUENCE_SOFTMAX_OP
LOAD_OP1(sequence_softmax, CPU);
#endif
#ifdef LOG_OP
LOAD_OP1(log, CPU);
#endif
......
......@@ -12,52 +12,12 @@ 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 "lod_tensor.h"
#include "framework/lod_tensor.h"
#include <algorithm>
namespace paddle_mobile {
namespace framework {
// std::ostream &operator<<(std::ostream &os, const LoD &lod) {
// os << "{";
// for (auto &v : lod) {
// os << "{";
// bool is_first = true;
// for (auto &i : v) {
// if (is_first) {
// os << i;
// is_first = false;
// } else {
// os << ", " << i;
// }
// }
// os << "}";
// }
// os << "}";
//
// return os;
//}
//
// std::ostream &operator<<(std::ostream &os, const LoDTensor &t) {
// PADDLE_MOBILE_ENFORCE(t.type().hash_code() == typeid(float).hash_code(),
// "t.type() is not float");
// os << "dim: " << t.dims() << "\n";
// os << "lod: " << t.lod() << "\n";
// // only print first ten elements
// int64_t size = t.numel() < 10 ? t.numel() : 10;
// for (int64_t i = 0; i < size; ++i) {
// os << t.data<float>()[i] << " ";
// }
//
// return os;
//}
// std::string LoDToString(const LoD &lod) {
// std::ostringstream stream;
// stream << lod;
// return stream.str();
//}
LoD SliceInLevel(const LoD &in, size_t level, size_t elem_begin,
size_t elem_end) {
PADDLE_MOBILE_ENFORCE(level < in.size(), "level should >= in.size()");
......
......@@ -211,17 +211,17 @@ inline Print &operator<<(Print &printer, const LoDTensor &tensor) {
stride = stride > 0 ? stride : 1;
#ifndef PADDLE_MOBILE_FPGA
for (int i = 0; i < tensor.numel(); i += stride) {
if (tensor.type() == typeid(float)) {
if (tensor.type() == type_id<float>()) {
printer << tensor.data<float>()[i] << " ";
} else if (tensor.type() == typeid(int32_t)) {
} else if (tensor.type() == type_id<int32_t>()) {
printer << tensor.data<int32_t>()[i] << " ";
} else if (tensor.type() == typeid(int64_t)) {
} else if (tensor.type() == type_id<int64_t>()) {
printer << tensor.data<int64_t>()[i] << " ";
} else if (tensor.type() == typeid(int8_t)) {
} else if (tensor.type() == type_id<int8_t>()) {
printer << static_cast<int>(tensor.data<int8_t>()[i]) << " ";
} else if (tensor.type() == typeid(int32_t)) {
} else if (tensor.type() == type_id<int32_t>()) {
printer << tensor.data<int32_t>()[i] << " ";
} else if (tensor.type() == typeid(bool)) {
} else if (tensor.type() == type_id<bool>()) {
printer << tensor.data<bool>()[i] << " ";
}
}
......
......@@ -17,7 +17,6 @@
#include <algorithm>
#include <initializer_list>
#include <vector>
#include "framework/tensor.h"
#include "framework/tensor_util.h"
......@@ -198,7 +197,7 @@ class Vector {
}
size_t capacity() const {
return cpu_vec_.memory_size() / SizeOfType(typeid(T));
return cpu_vec_.memory_size() / SizeOfType(type_id<T>().hash_code());
}
// reserve data
......
......@@ -14,13 +14,24 @@ limitations under the License. */
#pragma once
#include <functional>
#include <string>
#include "common/log.h"
#include "common/type_define.h"
#include "framework/scope.h"
namespace paddle_mobile {
namespace framework {
template <typename Dtype>
class OperatorBase;
template <typename Dtype>
using OpCreator = std::function<framework::OperatorBase<Dtype> *(
const std::string & /*type*/, const VariableNameMap & /*inputs*/,
const VariableNameMap & /*outputs*/,
const framework::AttributeMap & /*attrs*/, framework::Scope * /*scope*/)>;
template <typename Dtype>
struct OpInfo {
OpCreator<Dtype> creator_;
......@@ -79,8 +90,6 @@ class OpInfoMap {
private:
OpInfoMap() = default;
std::unordered_map<std::string, OpInfo<Dtype>> map_;
// DISABLE_COPY_AND_ASSIGN(OpInfoMap);
};
} // namespace framework
......
......@@ -14,6 +14,7 @@ limitations under the License. */
#pragma once
#include <functional>
#include <map>
#include <string>
#include <utility>
......
......@@ -14,6 +14,8 @@ limitations under the License. */
#pragma once
#include <memory>
#include <vector>
#include "framework/framework.pb-c.h"
#include "framework/program/op_desc.h"
#include "framework/program/var_desc.h"
......@@ -26,8 +28,8 @@ class BlockDesc {
friend class Node;
friend class ProgramOptimize;
BlockDesc() {}
BlockDesc(PaddleMobile__Framework__Proto__BlockDesc *desc);
BlockDesc(const BlockDesc &block_desc)
explicit BlockDesc(PaddleMobile__Framework__Proto__BlockDesc *desc);
explicit BlockDesc(const BlockDesc &block_desc)
: index_(block_desc.index_), parent_index_(block_desc.parent_index_) {
for (auto &op_desc : block_desc.ops_) {
std::shared_ptr<OpDesc> copy_op_desc = std::make_shared<OpDesc>(*op_desc);
......
......@@ -18,7 +18,8 @@ limitations under the License. */
#include <vector>
#include "common/log.h"
#include "common/type_define.h"
#include "common/types.h"
#include "framework/attribute.h"
#include "framework/framework.pb-c.h"
namespace paddle_mobile {
......
......@@ -16,6 +16,7 @@ limitations under the License. */
#include <cinttypes>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>
......
......@@ -72,7 +72,7 @@ void ProgramDesc::Description(std::string header) const {
}
}
for (auto &attr : op->GetAttrMap()) {
if (attr.first == "op_callstack") continue;
if (attr.first == "op_callstack" || attr.first == "sub_block") continue;
LOG(kLOG_DEBUG2) << "attr name: " << attr.first;
LOG(kLOG_DEBUG3) << "argument - " << attr.second;
}
......
......@@ -19,8 +19,6 @@ limitations under the License. */
#include <fstream>
#include <memory>
#include <string>
#include <type_traits>
#include <typeindex>
#include <vector>
#include "common/enforce.h"
......@@ -83,7 +81,7 @@ class Tensor : public TensorBase {
return *this;
}
inline void *mutable_data(std::type_index type) {
inline void *mutable_data(const kTypeId_t type) {
if (holder_ != nullptr) {
holder_->set_type(type);
}
......@@ -108,7 +106,7 @@ class Tensor : public TensorBase {
template <typename T>
inline T *mutable_data() {
static_assert(std::is_pod<T>::value, "T must be POD");
return reinterpret_cast<T *>(mutable_data(typeid(T)));
return reinterpret_cast<T *>(mutable_data(type_id<T>().hash_code()));
}
/**
......@@ -165,9 +163,9 @@ class Tensor : public TensorBase {
check_memory_size();
PADDLE_MOBILE_ENFORCE(
(std::is_same<T, void>::value ||
holder_->type().hash_code() == typeid(T).hash_code()),
"Tensor holds the wrong type, it holds %s, requested %s",
this->holder_->type().name(), typeid(T).name());
holder_->type() == type_id<T>().hash_code()),
"Tensor holds the wrong type, it holds %d, requested %d",
this->holder_->type(), type_id<T>().hash_code());
return reinterpret_cast<T *>(reinterpret_cast<uintptr_t>(holder_->ptr()) +
offset_);
......@@ -179,9 +177,9 @@ class Tensor : public TensorBase {
check_memory_size();
PADDLE_MOBILE_ENFORCE(
(std::is_same<T, void>::value ||
holder_->type().hash_code() == typeid(T).hash_code()),
"Tensor holds the wrong type, it holds %s, requested %s",
this->holder_->type().name(), typeid(T).name());
holder_->type() == type_id<T>().hash_code()),
"Tensor holds the wrong type, it holds %d, requested %d",
this->holder_->type(), type_id<T>().hash_code());
return reinterpret_cast<const T *>(
reinterpret_cast<uintptr_t>(holder_->ptr()) + offset_);
......@@ -189,7 +187,7 @@ class Tensor : public TensorBase {
private:
struct PlaceholderImpl : public Placeholder {
PlaceholderImpl(size_t size, std::type_index type)
PlaceholderImpl(size_t size, const kTypeId_t type)
: ptr_(static_cast<uint8_t *>(memory::Alloc(size)),
memory::PODDeleter<uint8_t>()),
size_(size),
......@@ -203,9 +201,9 @@ class Tensor : public TensorBase {
virtual void *ptr() const { return static_cast<void *>(ptr_.get()); }
virtual std::type_index type() const { return type_; }
virtual kTypeId_t type() const { return type_; }
virtual void set_type(std::type_index type) { type_ = type; }
virtual void set_type(const kTypeId_t type) { type_ = type; }
virtual void resize(size_t size) {
if (size > capatity_) {
......@@ -223,7 +221,7 @@ class Tensor : public TensorBase {
size_t capatity_;
/* the current type of memory */
std::type_index type_;
kTypeId_t type_;
};
#ifdef PADDLE_MOBILE_FPGA
......@@ -231,13 +229,13 @@ class Tensor : public TensorBase {
inline void reset_data_ptr(void *p) {
((PlaceholderImpl *)(holder_.get()))->ptr_.reset((uint8_t *)p); // NOLINT
}
inline void set_type(std::type_index type) { holder_->set_type(type); }
inline void set_type(const kTypeId_t type) { holder_->set_type(type); }
inline void *get_data() {
return (
void *)(((PlaceholderImpl *)(holder_.get()))->ptr_.get()); // NOLINT
}
inline void *init(std::type_index type) {
inline void *init(const kTypeId_t type) {
if (holder_ != nullptr) {
holder_->set_type(type);
}
......@@ -265,15 +263,15 @@ inline Print &operator<<(Print &printer, const Tensor &tensor) {
stride = stride > 0 ? stride : 1;
#ifndef PADDLE_MOBILE_FPGA
for (int i = 0; i < tensor.numel(); i += stride) {
if (tensor.type() == typeid(float)) {
if (tensor.type() == type_id<float>()) {
printer << tensor.data<float>()[i] << " ";
} else if (tensor.type() == typeid(int32_t)) {
} else if (tensor.type() == type_id<int32_t>()) {
printer << tensor.data<int32_t>()[i] << " ";
} else if (tensor.type() == typeid(int64_t)) {
} else if (tensor.type() == type_id<int64_t>()) {
printer << tensor.data<int64_t>()[i] << " ";
} else if (tensor.type() == typeid(int8_t)) {
} else if (tensor.type() == type_id<int8_t>()) {
printer << static_cast<int>(tensor.data<int8_t>()[i]) << " ";
} else if (tensor.type() == typeid(int32_t)) {
} else if (tensor.type() == type_id<int32_t>()) {
printer << tensor.data<int32_t>()[i] << " ";
}
}
......
......@@ -14,10 +14,8 @@ limitations under the License. */
#pragma once
#include <type_traits>
#include <typeindex>
#include "common/enforce.h"
#include "common/type_define.h"
#include "common/types.h"
#include "framework/ddim.h"
......@@ -29,8 +27,8 @@ struct SizeOfTypeFunctor;
template <typename T>
struct SizeOfTypeFunctor<T> {
size_t operator()(std::type_index type) const {
if (typeid(T).hash_code() == type.hash_code()) {
size_t operator()(const kTypeId_t type) const {
if (type_id<T>().hash_code() == type) {
return sizeof(T);
} else {
return 0UL;
......@@ -40,12 +38,12 @@ struct SizeOfTypeFunctor<T> {
template <>
struct SizeOfTypeFunctor<> {
size_t operator()(std::type_index type) const { return 0UL; }
size_t operator()(const kTypeId_t type) const { return 0UL; }
};
template <typename HEAD, typename... TAIL>
struct SizeOfTypeFunctor<HEAD, TAIL...> {
size_t operator()(std::type_index type) const {
size_t operator()(const kTypeId_t type) const {
SizeOfTypeFunctor<HEAD> head;
size_t head_size = head(type);
if (head_size != 0) {
......@@ -56,13 +54,13 @@ struct SizeOfTypeFunctor<HEAD, TAIL...> {
}
};
static inline size_t SizeOfType(std::type_index type) {
static inline size_t SizeOfType(const kTypeId_t type) {
SizeOfTypeFunctor<int8_t, int, half, float, double, int16_t, int64_t, bool,
size_t>
functor;
size_t size = functor(type);
PADDLE_MOBILE_ENFORCE(size != 0UL, "Cannot get size of type %s", type.name());
PADDLE_MOBILE_ENFORCE(size != 0UL, "Cannot get size of type %d", type);
return size;
}
......@@ -78,7 +76,7 @@ class TensorBase {
/*! Return the numel of the memory block. */
inline int64_t numel() const { return product(dims_); }
std::type_index type() const {
kTypeId_t type() const {
PADDLE_MOBILE_ENFORCE(
holder_ != nullptr,
"Tensor not initialized yet when Tensor::type() is called.")
......@@ -114,9 +112,9 @@ class TensorBase {
virtual size_t size() const = 0;
virtual std::type_index type() const = 0;
virtual kTypeId_t type() const = 0;
virtual void set_type(std::type_index type) = 0;
virtual void set_type(kTypeId_t type) = 0;
virtual void resize(size_t size) = 0;
};
......
......@@ -16,13 +16,10 @@ limitations under the License. */
#include <memory>
#include <string>
#include <typeindex>
#include <typeinfo>
#include "../common/variant.h"
#include "common/variant.h"
namespace paddle_mobile {
namespace framework {
using std::string;
class Variable {
public:
......@@ -33,7 +30,7 @@ class Variable {
template <typename T>
const T GetValue() const {
if (typeid(T) == typeid(std::string)) {
if (type_id<T>().hash_code() == type_id<std::string>().hash_code()) {
PADDLE_MOBILE_THROW_EXCEPTION(
"Please use getString to get an string (to avoid of an issue with "
"gcc "
......@@ -60,38 +57,40 @@ class Variable {
template <typename T>
bool IsType() const {
return holder_ != nullptr && holder_->Type() == typeid(T);
return holder_ != nullptr && holder_->Type() == type_id<T>().hash_code();
}
void Clear() { holder_.reset(); }
std::type_index Type() const { return holder_->Type(); }
kTypeId_t Type() const { return holder_->Type(); }
private:
struct Placeholder {
Placeholder() = default;
virtual ~Placeholder() = default;
virtual const std::type_info &Type() const = 0;
virtual kTypeId_t Type() const = 0;
virtual void *Ptr() const = 0;
};
template <typename T>
struct PlaceholderImp : public Placeholder {
explicit PlaceholderImp(T *ptr) : ptr_(ptr), type_(typeid(T)) {}
explicit PlaceholderImp(T *ptr)
: ptr_(ptr), type_(type_id<T>().hash_code()) {}
virtual const std::type_info &Type() const { return type_; }
virtual void *Ptr() const override {
return static_cast<void *>(ptr_.get());
}
kTypeId_t Type() const override { return type_; }
void *Ptr() const override { return static_cast<void *>(ptr_.get()); }
std::unique_ptr<T> ptr_;
const std::type_info &type_;
kTypeId_t type_;
};
Variant<int, bool, string, float, double> variant;
std::unique_ptr<Placeholder> holder_;
friend class Scope;
string name_;
Variant<int, bool, std::string, float, double> variant;
std::unique_ptr<Placeholder> holder_;
std::string name_;
};
} // namespace framework
} // namespace paddle_mobile
......@@ -128,7 +128,7 @@ void ConvertTensors(const framework::Tensor &src, PaddleTensor *des) {
des->layout = src.layout == framework::LAYOUT_HWC ? LAYOUT_HWC : LAYOUT_CHW;
auto num = src.numel();
if (src.type() == typeid(float)) {
if (src.type() == type_id<float>()) {
des->data.Reset(const_cast<float *>(src.data<float>()),
num * sizeof(float));
} else {
......@@ -143,7 +143,7 @@ void PaddleMobilePredictor<Device, T>::FeedPaddleTensors(
auto num = inputs.size();
std::vector<framework::Tensor> tensors(num, framework::Tensor());
for (int i = 0; i < num; i++) {
tensors[i].init(typeid(float));
tensors[i].init(type_id<float>().hash_code());
ConvertPaddleTensors(inputs[i], &tensors[i]);
}
paddle_mobile_->FeedTensorData(tensors);
......
......@@ -24,8 +24,8 @@ limitations under the License. */
#include <cassert>
#include <memory>
#include <string>
#include <typeindex>
#include <vector>
#include "common/type_define.h"
namespace paddle_mobile {
......@@ -88,7 +88,7 @@ struct PaddleTensor {
// TODO(Superjomn) for LoD support, add a vector<vector<int>> field if needed.
PaddleBuf data; // blob of data.
PaddleDType dtype;
std::type_index dtypeid = typeid(float);
kTypeId_t dtypeid;
LayoutType layout;
};
......
......@@ -14,8 +14,6 @@ limitations under the License. */
#ifdef BEAM_SEARCH_DECODE_OP
#pragma once
#include "operators/beam_search_decode_op.h"
namespace paddle_mobile {
......
......@@ -14,8 +14,6 @@ limitations under the License. */
#ifdef BEAM_SEARCH_OP
#pragma once
#include "operators/beam_search_op.h"
namespace paddle_mobile {
......
......@@ -192,10 +192,10 @@ bool LessThanKernel<CPU, float>::Init(CompareParam<CPU> *param) {
template <>
void LessThanKernel<CPU, float>::Compute(const CompareParam<CPU> &param) {
if (param.input_x_->type() == typeid(int64_t)) {
if (param.input_x_->type() == type_id<int64_t>().hash_code()) {
CompareCompute<int64_t, LESS_THAN>()(param.input_x_, param.input_y_,
param.axis_, param.output_);
} else if (param.input_x_->type() == typeid(float)) {
} else if (param.input_x_->type() == type_id<float>().hash_code()) {
CompareCompute<float, LESS_THAN>()(param.input_x_, param.input_y_,
param.axis_, param.output_);
} else {
......
......@@ -27,7 +27,7 @@ bool ConcatKernel<CPU, float>::Init(ConcatParam<CPU> *param) {
template <>
void ConcatKernel<CPU, float>::Compute(const ConcatParam<CPU> &param) {
if (param.Inputs()[0]->type() == typeid(int8_t)) {
if (param.Inputs()[0]->type() == type_id<int8_t>().hash_code()) {
ConcatCompute<int8_t>(param);
} else {
ConcatCompute<float>(param);
......
......@@ -28,7 +28,7 @@ void InitBaseConvKernel(ConvParam<CPU> *param) {
bool depth5x5 = conv5x5 && param->Groups() == param->Input()->dims()[1] &&
param->Input()->dims()[1] == param->Output()->dims()[1];
if (param->Filter()->type() == typeid(int8_t)) {
if (param->Filter()->type() == type_id<int8_t>().hash_code()) {
#ifndef __aarch64__
if (depth3x3 && param->Strides()[0] < 3 &&
param->Strides()[0] == param->Strides()[1]) {
......
......@@ -34,7 +34,7 @@ void DequantizeKernel<CPU, float>::Compute(const DequantizeParam<CPU> &param) {
LoDTensor *output = param.output_;
float activation_scale = param.activation_scale_->data<float>()[0];
float weight_scale = param.weight_scale_;
const int32_t *x = input->data<const int32_t>();
const int32_t *x = input->data<int32_t>();
float *y = output->mutable_data<float>();
size_t size = output->numel();
// float scale = 1.f / (activation_scale * weight_scale);
......
......@@ -36,7 +36,7 @@ inline float32_t vmaxvq_f32(float32x4_t r) {
template <RoundType R>
inline void QuantizeOffline(const Tensor *input, const float scale,
const float max_abs, Tensor *output) {
const float *x = input->data<const float>();
const float *x = input->data<float>();
int8_t *y = output->mutable_data<int8_t>();
size_t remain = input->numel();
#if defined(__ARM_NEON__) || defined(__ARM_NEON)
......@@ -88,7 +88,7 @@ inline void QuantizeOffline(const Tensor *input, const float scale,
template <RoundType R>
inline void QuantizeOnline(const Tensor *input, const float scale,
Tensor *output) {
const float *x = input->data<const float>();
const float *x = input->data<float>();
int8_t *y = output->mutable_data<int8_t>();
size_t remain = input->numel();
#if defined(__ARM_NEON__) || defined(__ARM_NEON)
......@@ -143,7 +143,7 @@ static void Quantize(const Tensor *input, const float max_abs,
float find_abs_max(const Tensor *input) {
float max_abs = 0.f;
const float *x = input->data<const float>();
const float *x = input->data<float>();
size_t remain = input->numel();
#if defined(__ARM_NEON__) || defined(__ARM_NEON)
size_t loop = remain >> 4;
......
......@@ -28,7 +28,9 @@ void WriteToArrayKernel<CPU, float>::Compute(
const WriteToArrayParam<CPU> &param) {
int64_t offset = param.index_->data<int64_t>()[0];
if (offset >= param.output_->size()) {
param.output_->resize(offset + 1);
while (param.output_->size() <= offset) {
param.output_->emplace_back();
}
}
framework::LoDTensor *out_tensor = &(param.output_->at(offset));
......
......@@ -126,13 +126,13 @@ void Transpose2Kernel<CPU, float>::Compute(const Transpose2Param<CPU> &param) {
const std::vector<int> &axis = param.Axis();
bool shuffle_channel = IsShuffleChannel(axis);
if (shuffle_channel) {
if (param.InputX()->type() == typeid(int8_t)) {
if (param.InputX()->type() == type_id<int8_t>().hash_code()) {
ShuffleChannelCompute<int8_t>(param);
} else {
ShuffleChannelCompute<float>(param);
}
} else {
if (param.InputX()->type() == typeid(int8_t)) {
if (param.InputX()->type() == type_id<int8_t>().hash_code()) {
Transpose2Compute<int8_t>(param);
} else {
Transpose2Compute<float>(param);
......
......@@ -35,6 +35,7 @@ class StepExecutor {
auto op_handler = framework::OpRegistry<CPU>::CreateOp(
op_desc->Type(), op_desc->GetInputs(), op_desc->GetOutputs(),
op_desc->GetAttrMap(), scope_);
op_handler->Init();
ops_of_block_[i] = op_handler;
}
}
......
......@@ -37,7 +37,7 @@ void MulCompute(const MulParam<CPU> &param) {
if (out_dim.size() != 2) {
out->Resize({x_matrix.dims()[0], y_matrix.dims()[1]});
}
if (param.InputX()->type() == typeid(int8_t)) {
if (param.InputX()->type() == type_id<int8_t>().hash_code()) {
out->mutable_data<int32_t>();
math::MatMul<int8_t, int32_t>(x_matrix, false, y_matrix, false,
static_cast<float>(1), out,
......
......@@ -144,7 +144,7 @@ void SumCompute(const SumParam<CPU> &param) {
}
} else {
PADDLE_MOBILE_THROW_EXCEPTION(
"Unexpected branch, output variable type is %s", outvar->Type().name());
"Unexpected branch, output variable type is %d", outvar->Type());
}
}
} // namespace operators
......
......@@ -25,7 +25,7 @@ template <>
bool ElementwiseAddKernel<FPGA, float>::Init(ElementwiseAddParam<FPGA> *param) {
auto *input_y = const_cast<LoDTensor *>(param->InputY());
auto *out = param->Out();
if (input_y->type() != typeid(float)) {
if (input_y->type() != type_id<float>()) {
paddle_mobile::fpga::ActivationType activation_enable =
paddle_mobile::fpga::NONE;
int16_t leaky_relu_negative_slope = 0;
......@@ -62,11 +62,10 @@ bool ElementwiseAddKernel<FPGA, float>::Init(ElementwiseAddParam<FPGA> *param) {
param->SetFpgaArgs(ewaddArgs);
} else {
param->float_input_x.Resize(param->InputX()->dims());
param->float_input_x.init(typeid(float));
param->float_input_x.init(type_id<float>().hash_code());
fpga::format_fp32_ofm(&(param->float_input_x));
param->float_out.Resize(param->InputX()->dims());
// param->float_out.init(typeid(float));
param->float_out.mutable_data<float>(param->InputX()->dims());
fpga::format_fp32_ofm(&(param->float_out));
......@@ -118,7 +117,7 @@ template <>
void ElementwiseAddKernel<FPGA, float>::Compute(
const ElementwiseAddParam<FPGA> &param) {
auto input_y = const_cast<LoDTensor *>(param.InputY());
if (input_y->type() != typeid(float)) {
if (input_y->type() != type_id<float>()) {
fpga::ComputeFpgaEWAdd(param.FpgaArgs());
} else {
auto input_x = const_cast<LoDTensor *>(param.InputX());
......
......@@ -27,11 +27,11 @@ struct MulFunctor {
template <>
bool ElementwiseMulKernel<FPGA, float>::Init(ElementwiseMulParam<FPGA> *param) {
param->float_input_x.Resize(param->InputX()->dims());
param->float_input_x.init(typeid(float));
param->float_input_x.init(type_id<float>().hash_code());
fpga::format_fp32_ofm(&(param->float_input_x));
param->float_out.Resize(param->InputX()->dims());
param->float_out.init(typeid(float));
param->float_out.init(type_id<float>().hash_code());
fpga::format_fp32_ofm(&(param->float_out));
auto *out = param->Out();
......
......@@ -23,7 +23,7 @@ bool FeedKernel<FPGA, float>::Init(FeedParam<FPGA> *param) {
int col = param->Col();
DLOG << "col = " << col;
auto input = const_cast<LoDTensor *>(&param->InputX()->at(col));
input->init(typeid(float));
input->init(type_id<float>().hash_code());
input->Resize(output->dims());
if (output->dims().size() != 4) {
......@@ -39,12 +39,12 @@ void FeedKernel<FPGA, float>::Compute(const FeedParam<FPGA> &param) {
auto output = param.Out();
int col = param.Col();
auto input = const_cast<LoDTensor *>(&param.InputX()->at(col));
std::type_index input_type = input->type();
kTypeId_t input_type = input->type();
if (input_type == typeid(float)) {
input->init(typeid(float));
} else { // input_type == typeid(int8_t)
input->init(typeid(int8_t));
if (input_type == type_id<float>()) {
input->init(type_id<float>().hash_code());
} else {
input->init(type_id<int8_t>().hash_code());
}
input->Resize(output->dims());
......@@ -62,7 +62,7 @@ void FeedKernel<FPGA, float>::Compute(const FeedParam<FPGA> &param) {
fpga::format_image(input);
auto output_ptr = output->data<half>();
fpga::BypassArgs args = {fpga::DATA_TYPE_FP32};
if (input_type == typeid(float)) {
if (input_type == type_id<float>()) {
auto input_ptr = input->data<float>();
auto external_ptr = reinterpret_cast<float *>(input->external_data);
float *p_data = external_ptr == nullptr ? input_ptr : external_ptr;
......@@ -81,7 +81,7 @@ void FeedKernel<FPGA, float>::Compute(const FeedParam<FPGA> &param) {
args.output.scale_address = output->scale;
fpga::PerformBypass(args);
input->external_data = nullptr;
} else { // input_type == typeid(int8_t)
} else {
auto input_ptr = input->data<int8_t>();
auto external_ptr = reinterpret_cast<int8_t *>(input->external_data);
int8_t *p_data = external_ptr == nullptr ? input_ptr : external_ptr;
......
......@@ -21,10 +21,10 @@ bool FetchKernel<FPGA, float>::Init(FetchParam<FPGA> *param) {
int col = param->Col();
DLOG << "col = " << col;
auto output = &(param->Out()->at(col));
if (input->type() == typeid(float)) {
if (input->type() == type_id<float>()) {
return true;
}
output->init(typeid(float));
output->init(type_id<float>().hash_code());
output->Resize(input->dims());
fpga::format_fp32_ofm(output);
int outC = 1;
......@@ -78,7 +78,7 @@ void FetchKernel<FPGA, float>::Compute(const FetchParam<FPGA> &param) {
auto input = const_cast<LoDTensor *>(param.InputX());
int col = param.Col();
auto output = &param.Out()->at(col);
if (input->type() == typeid(float)) {
if (input->type() == type_id<float>()) {
output->ShareDataWith(*input);
return;
}
......
......@@ -28,7 +28,7 @@ bool PoolKernel<FPGA, float>::Init(PoolParam<FPGA> *param) {
vector<int> paddings = param->Paddings();
std::string pooling_type = param->PoolingType();
if (input->type() == typeid(float)) {
if (input->type() == type_id<float>()) {
int channels = input->dims()[1];
int height = input->dims()[2];
int width = input->dims()[3];
......@@ -70,7 +70,7 @@ template <>
void PoolKernel<FPGA, float>::Compute(const PoolParam<FPGA> &param) {
auto *input = const_cast<LoDTensor *>(param.Input());
if (input->type() == typeid(float)) {
if (input->type() == type_id<float>()) {
auto *output = param.Output();
auto in = input->data<float>();
auto N = input->dims()[0];
......
......@@ -37,11 +37,11 @@ bool ProposalKernel<FPGA, float>::Init(ProposalParam<FPGA> *param) {
param->float_bbox = std::make_shared<Tensor>();
param->float_bbox->Resize(param->bbox_deltas_->dims());
param->float_bbox->init(typeid(float));
param->float_bbox->init(type_id<float>().hash_code());
fpga::format_fp32_ofm(param->float_bbox.get());
param->float_score = std::make_shared<Tensor>();
param->float_score->Resize(param->scores_->dims());
param->float_score->init(typeid(float));
param->float_score->init(type_id<float>().hash_code());
fpga::format_fp32_ofm(param->float_score.get());
auto input = param->bbox_deltas_;
......@@ -437,7 +437,6 @@ void ProposalKernel<FPGA, float>::Compute(const ProposalParam<FPGA> &param) {
bbox_height = (uint32_t)(input_bbox->dims()[2]);
bbox_width = (uint32_t)(input_bbox->dims()[3]);
// score_tmp->init(typeid(half));
std::shared_ptr<Tensor> score_tmp = std::make_shared<Tensor>();
score_tmp->Resize(param.scores_->dims());
score_tmp->mutable_data<half>();
......
......@@ -25,7 +25,7 @@ bool SliceKernel<FPGA, float>::Init(SliceParam<FPGA>* param) {
fpga::format_fp16_ofm(output);
DLOG << "input: " << param->input_;
DLOG << "output: " << param->output_;
if (param->input_->type() != typeid(half)) {
if (param->input_->type() != type_id<half>()) {
DLOG << "wrong type";
}
return true;
......
......@@ -26,7 +26,7 @@ bool SoftmaxKernel<FPGA, float>::Init(SoftmaxParam<FPGA> *param) {
auto dims = framework::vectorize(input->dims());
half *input_ptr;
auto out = param->Out();
if (input->type() == typeid(float)) {
if (input->type() == type_id<float>()) {
out->Resize(framework::make_ddim(dims));
out->mutable_data<float>(framework::make_ddim(dims));
} else {
......@@ -50,7 +50,7 @@ bool SoftmaxKernel<FPGA, float>::Init(SoftmaxParam<FPGA> *param) {
if (channel != 2) { // Use CPU
out->Resize(framework::make_ddim(dims));
out->mutable_data<float>(framework::make_ddim(dims));
float_input->init(typeid(float));
float_input->init(type_id<float>().hash_code());
float_input->mutable_data<float>(framework::make_ddim(dims));
// fpga::format_fp32_ofm(float_input);
// fpga::format_fp32_ofm(out);
......@@ -91,7 +91,7 @@ bool SoftmaxKernel<FPGA, float>::Init(SoftmaxParam<FPGA> *param) {
template <>
void SoftmaxKernel<FPGA, float>::Compute(const SoftmaxParam<FPGA> &param) {
auto *in_x = (param.InputX());
if (in_x->type() == typeid(half)) {
if (in_x->type() == type_id<half>()) {
fpga::PerformBypass(param.FpgaArgs());
if (param.FpgaArgs().output.activation.activation_type != fpga::SOFTMAX) {
Tensor *out = param.Out();
......
......@@ -14,8 +14,6 @@ limitations under the License. */
#if defined(__ARM_NEON__) || defined(__ARM_NEON)
#pragma once
#include "operators/math/gemm/cblas.h"
#include "operators/math/gemm/executor.h"
#include "operators/math/gemm/strategy.h"
......
......@@ -14,8 +14,6 @@ limitations under the License. */
#ifdef ONE_HOT_OP
#pragma once
#include "operators/one_hot_op.h"
namespace paddle_mobile {
......
......@@ -19,6 +19,7 @@ limitations under the License. */
#include "common/log.h"
#include "common/type_define.h"
#include "common/types.h"
#include "framework/attribute.h"
#include "framework/lod_tensor.h"
#include "framework/scope.h"
#include "framework/tensor.h"
......
......@@ -104,7 +104,7 @@ void dump_stride_float(std::string filename,
void dump_stride(std::string filename,
paddle_mobile::PaddleTensor input_tensor) {
if (input_tensor.dtypeid == typeid(float)) {
if (input_tensor.dtypeid == type_id<float>().hash_code()) {
dump_stride_float(filename, input_tensor);
} else {
std::cout << "only support dumping float data" << std::endl;
......@@ -156,13 +156,13 @@ int main() {
std::cout << "Finishing initializing data" << std::endl;
struct PaddleTensor t_img_info, t_img;
t_img_info.dtypeid = typeid(float);
t_img_info.dtypeid = type_id<float>().hash_code();
t_img_info.layout = LAYOUT_HWC;
t_img_info.shape = std::vector<int>({1, 3});
t_img_info.name = "Image information";
t_img_info.data.Reset(img_info, 3 * sizeof(float));
t_img.dtypeid = typeid(float);
t_img.dtypeid = type_id<float>().hash_code();
// quantize(&img, img_length);
// t_img.dtypeid = typeid(int8_t);
t_img.layout = LAYOUT_HWC;
......@@ -209,7 +209,7 @@ int main() {
std::cout << "Finishing initializing data" << std::endl;
struct PaddleTensor t_img1;
t_img1.dtypeid = typeid(float);
t_img1.dtypeid = type_id<float>().hash_code();
t_img1.layout = LAYOUT_HWC;
t_img1.shape = std::vector<int>({1, 14, 14, 144});
t_img1.name = "Image information";
......
......@@ -96,7 +96,7 @@ void dump_stride_float(std::string filename, PaddleTensor input_tensor) {
}
void dump_stride(std::string filename, PaddleTensor input_tensor) {
if (input_tensor.dtypeid == typeid(float)) {
if (input_tensor.dtypeid == type_id<float>().hash_code()) {
dump_stride_float(filename, input_tensor);
} else {
std::cout << "only support dumping float data" << std::endl;
......@@ -131,7 +131,7 @@ int main() {
std::cout << "Finishing initializing data" << std::endl;
struct PaddleTensor t_img;
t_img.dtype = FLOAT32;
t_img.dtypeid = typeid(float);
t_img.dtypeid = type_id<float>().hash_code();
// quantize(&img, img_length);
// t_img.dtype = INT8;
// t_img.dtypeid = typeid(int8_t);
......
......@@ -20,8 +20,8 @@ limitations under the License. */
#include <iostream>
#include "../../src/io/paddle_inference_api.h"
using namespace paddle_mobile;
using namespace paddle_mobile::fpga;
using namespace paddle_mobile; // NOLINT
using namespace paddle_mobile::fpga; // NOLINT
static const char *g_image = "../models/rfcn/data.bin";
static const char *g_model = "../models/rfcn/model";
......@@ -86,7 +86,7 @@ int main() {
struct PaddleTensor t_img1;
t_img1.dtypeid = typeid(float);
t_img1.dtypeid = type_id<float>().hash_code();
t_img1.layout = LAYOUT_HWC;
t_img1.shape = std::vector<int>({1, 224, 224, 3});
t_img1.name = "Image information";
......@@ -117,13 +117,13 @@ int main() {
std::cout << "Finishing initializing data" << std::endl;
struct PaddleTensor t_img_info, t_img;
t_img.dtypeid = typeid(float);
t_img.dtypeid = type_id<float>().hash_code();
t_img_info.layout = LAYOUT_HWC;
t_img_info.shape = std::vector<int>({1, 3});
t_img_info.name = "Image information";
t_img_info.data.Reset(img_info, 3 * sizeof(float));
t_img.dtypeid = typeid(float);
t_img.dtypeid = type_id<float>().hash_code();
t_img.layout = LAYOUT_HWC;
t_img.shape = std::vector<int>({1, 432, 1280, 3});
t_img.name = "Image information";
......
......@@ -95,7 +95,7 @@ void dump_stride_float(std::string filename, PaddleTensor input_tensor) {
}
void dump_stride(std::string filename, PaddleTensor input_tensor) {
if (input_tensor.dtypeid == typeid(float)) {
if (input_tensor.dtypeid == type_id<float>().hash_code()) {
dump_stride_float(filename, input_tensor);
} else {
std::cout << "only support dumping float data" << std::endl;
......@@ -131,10 +131,10 @@ int main() {
std::cout << "Finishing initializing data" << std::endl;
struct PaddleTensor t_img;
// t_img.dtype = FLOAT32;
// t_img.dtypeid = typeid(float);
// t_img.dtypeid = type_id<float>().hash_code();
quantize(&img, img_length);
t_img.dtype = INT8;
t_img.dtypeid = typeid(int8_t);
t_img.dtypeid = type_id<int8_t>().hash_code();
t_img.layout = LAYOUT_HWC;
t_img.shape = std::vector<int>({1, 256, 416, 3});
t_img.name = "Image information";
......
......@@ -19,7 +19,7 @@ limitations under the License. */
namespace paddle_mobile {
void dequantize(const Tensor* input, const float scale, Tensor* output) {
const int32_t* x = input->data<const int32_t>();
const int32_t* x = input->data<int32_t>();
float* y = output->mutable_data<float>();
size_t size = output->numel();
for (size_t i = 0; i < size; ++i) {
......
......@@ -20,8 +20,8 @@ namespace paddle_mobile {
template <typename Itype, typename Otype>
int TestGruOp(int in_channels, int out_channels, std::string opname) {
int input_c = in_channels;
int output_c = out_channels;
size_t input_c = in_channels;
size_t output_c = out_channels;
paddle_mobile::framework::LoD lod{{0, input_c}};
int batch_size = lod.size();
framework::DDim input_shape = framework::make_ddim({input_c, output_c * 3});
......
......@@ -65,7 +65,7 @@ static void quantize(const Tensor *input, const float scale, Tensor *output) {
int output_w = output->dims()[3];
size_t input_spatial = input_h * input_w;
size_t output_spatial = output_h * output_w;
const float *x = input->data<const float>();
const float *x = input->data<float>();
int8_t *y = output->mutable_data<int8_t>();
for (int nc = 0; nc < batch_size * channels; ++nc) {
......@@ -81,7 +81,7 @@ static void quantize(const Tensor *input, const float scale, Tensor *output) {
static float find_abs_max(const Tensor *input) {
float max_abs = 0.f;
const float *x = input->data<const float>();
const float *x = input->data<float>();
size_t size = input->numel();
for (size_t i = 0; i < size; ++i) {
float value = std::abs(x[i]);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册