From 711d86bb568fb669f89b5bc034a24b6b85bc67a2 Mon Sep 17 00:00:00 2001 From: yuyang18 Date: Thu, 10 May 2018 19:12:00 +0800 Subject: [PATCH 1/7] Polish data_type.h --- paddle/fluid/framework/CMakeLists.txt | 6 +- paddle/fluid/framework/data_type.cc | 82 +++++++++++++++++++++++++++ paddle/fluid/framework/data_type.h | 66 +-------------------- 3 files changed, 88 insertions(+), 66 deletions(-) create mode 100644 paddle/fluid/framework/data_type.cc diff --git a/paddle/fluid/framework/CMakeLists.txt b/paddle/fluid/framework/CMakeLists.txt index 340b891e41671..0dfcf27b1c048 100644 --- a/paddle/fluid/framework/CMakeLists.txt +++ b/paddle/fluid/framework/CMakeLists.txt @@ -5,11 +5,11 @@ proto_library(framework_proto SRCS framework.proto) cc_library(ddim SRCS ddim.cc DEPS eigen3 boost) cc_test(ddim_test SRCS ddim_test.cc DEPS ddim) nv_test(dim_test SRCS dim_test.cu DEPS ddim) - +cc_library(data_type SRCS data_type.cc DEPS framework_proto ddim) if(WITH_GPU) - nv_library(tensor SRCS tensor.cc tensor_util.cu DEPS ddim place memory device_context framework_proto) + nv_library(tensor SRCS tensor.cc tensor_util.cu DEPS place memory device_context data_type) else() - cc_library(tensor SRCS tensor.cc tensor_util.cc DEPS ddim place memory device_context framework_proto) + cc_library(tensor SRCS tensor.cc tensor_util.cc DEPS place memory device_context data_type) endif() cc_test(tensor_test SRCS tensor_test.cc DEPS tensor) diff --git a/paddle/fluid/framework/data_type.cc b/paddle/fluid/framework/data_type.cc new file mode 100644 index 0000000000000..b60c618658898 --- /dev/null +++ b/paddle/fluid/framework/data_type.cc @@ -0,0 +1,82 @@ +// 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. + +#include "paddle/fluid/framework/data_type.h" + +namespace paddle { +namespace framework { + +struct DataTypeMap { + std::unordered_map cpp_to_proto_; + std::unordered_map proto_to_cpp_; + std::unordered_map proto_to_str_; +}; + +static DataTypeMap g_data_type_map_; + +template +static inline void RegisterType(proto::VarType::Type proto_type, + const std::string &name) { + g_data_type_map_.proto_to_cpp_.emplace(proto_type, typeid(T)); + g_data_type_map_.cpp_to_proto_.emplace(typeid(T), proto_type); + g_data_type_map_.proto_to_str_.emplace(proto_type, name); +} + +static int RegisterAllTypes() { +#define RegType(cc_type, proto_type) RegisterType(proto_type, #cc_type) + + RegType(platform::float16, proto::VarType::FP16); + RegType(float, proto::VarType::FP32); + RegType(double, proto::VarType::FP64); + RegType(int, proto::VarType::INT32); + RegType(int64_t, proto::VarType::INT64); + RegType(bool, proto::VarType::BOOL); + +#undef RegType + return 0; +} + +static std::once_flag register_once_flag_; + +proto::VarType::Type ToDataType(std::type_index type) { + std::call_once(register_once_flag_, RegisterAllTypes); + auto it = g_data_type_map_.cpp_to_proto_.find(type); + if (it != g_data_type_map_.cpp_to_proto_.end()) { + return it->second; + } + PADDLE_THROW("Not support %s as tensor type", type.name()); +} + +std::type_index ToTypeIndex(proto::VarType::Type type) { + std::call_once(register_once_flag_, RegisterAllTypes); + auto it = g_data_type_map_.proto_to_cpp_.find(type); + if (it != g_data_type_map_.proto_to_cpp_.end()) { + return it->second; + } + PADDLE_THROW("Not support proto::VarType::Type(%d) as tensor type", + static_cast(type)); +} + +std::string DataTypeToString(const proto::VarType::Type type) { + std::call_once(register_once_flag_, RegisterAllTypes); + auto it = g_data_type_map_.proto_to_str_.find(type); + if (it != g_data_type_map_.proto_to_str_.end()) { + return it->second; + } + PADDLE_THROW("Not support proto::VarType::Type(%d) as tensor type", + static_cast(type)); +} + +} // namespace framework +} // namespace paddle diff --git a/paddle/fluid/framework/data_type.h b/paddle/fluid/framework/data_type.h index 2a528eb3aa562..06cc5940b75fd 100644 --- a/paddle/fluid/framework/data_type.h +++ b/paddle/fluid/framework/data_type.h @@ -22,47 +22,8 @@ limitations under the License. */ namespace paddle { namespace framework { -inline proto::VarType::Type ToDataType(std::type_index type) { - if (typeid(platform::float16).hash_code() == type.hash_code()) { - return proto::VarType::FP16; - } else if (typeid(const float).hash_code() == type.hash_code()) { - // CPPLint complains Using C-style cast. Use static_cast() instead - // One fix to this is to replace float with const float because - // typeid(T) == typeid(const T) - // http://en.cppreference.com/w/cpp/language/typeid - return proto::VarType::FP32; - } else if (typeid(const double).hash_code() == type.hash_code()) { - return proto::VarType::FP64; - } else if (typeid(const int).hash_code() == type.hash_code()) { - return proto::VarType::INT32; - } else if (typeid(const int64_t).hash_code() == type.hash_code()) { - return proto::VarType::INT64; - } else if (typeid(const bool).hash_code() == type.hash_code()) { - return proto::VarType::BOOL; - } else { - PADDLE_THROW("Not supported"); - } -} - -inline std::type_index ToTypeIndex(proto::VarType::Type type) { - switch (type) { - case proto::VarType::FP16: - return typeid(platform::float16); - case proto::VarType::FP32: - return typeid(float); - case proto::VarType::FP64: - return typeid(double); - case proto::VarType::INT32: - return typeid(int); - case proto::VarType::INT64: - return typeid(int64_t); - case proto::VarType::BOOL: - return typeid(bool); - default: - PADDLE_THROW("Not support type %d", type); - } -} - +extern proto::VarType::Type ToDataType(std::type_index type); +extern std::type_index ToTypeIndex(proto::VarType::Type type); template inline void VisitDataType(proto::VarType::Type type, Visitor visitor) { switch (type) { @@ -89,32 +50,11 @@ inline void VisitDataType(proto::VarType::Type type, Visitor visitor) { } } -inline std::string DataTypeToString(const proto::VarType::Type type) { - switch (type) { - case proto::VarType::FP16: - return "float16"; - case proto::VarType::FP32: - return "float32"; - case proto::VarType::FP64: - return "float64"; - case proto::VarType::INT16: - return "int16"; - case proto::VarType::INT32: - return "int32"; - case proto::VarType::INT64: - return "int64"; - case proto::VarType::BOOL: - return "bool"; - default: - PADDLE_THROW("Not support type %d", type); - } -} - +extern std::string DataTypeToString(const proto::VarType::Type type); inline std::ostream& operator<<(std::ostream& out, const proto::VarType::Type& type) { out << DataTypeToString(type); return out; } - } // namespace framework } // namespace paddle From c4d6daac58f89c234e8a250c4e1d2c080d83972b Mon Sep 17 00:00:00 2001 From: yuyang18 Date: Fri, 11 May 2018 12:28:25 +0800 Subject: [PATCH 2/7] Polish SizeOfType --- paddle/fluid/framework/data_type.cc | 12 ++++++++ paddle/fluid/framework/data_type.h | 3 ++ paddle/fluid/framework/tensor_impl.h | 44 ++-------------------------- 3 files changed, 17 insertions(+), 42 deletions(-) diff --git a/paddle/fluid/framework/data_type.cc b/paddle/fluid/framework/data_type.cc index b60c618658898..f322584900269 100644 --- a/paddle/fluid/framework/data_type.cc +++ b/paddle/fluid/framework/data_type.cc @@ -21,6 +21,7 @@ struct DataTypeMap { std::unordered_map cpp_to_proto_; std::unordered_map proto_to_cpp_; std::unordered_map proto_to_str_; + std::unordered_map cpp_to_size_; }; static DataTypeMap g_data_type_map_; @@ -31,11 +32,13 @@ static inline void RegisterType(proto::VarType::Type proto_type, g_data_type_map_.proto_to_cpp_.emplace(proto_type, typeid(T)); g_data_type_map_.cpp_to_proto_.emplace(typeid(T), proto_type); g_data_type_map_.proto_to_str_.emplace(proto_type, name); + g_data_type_map_.cpp_to_size_.emplace(typeid(T), sizeof(T)); } static int RegisterAllTypes() { #define RegType(cc_type, proto_type) RegisterType(proto_type, #cc_type) + // NOTE: Add your customize type here. RegType(platform::float16, proto::VarType::FP16); RegType(float, proto::VarType::FP32); RegType(double, proto::VarType::FP64); @@ -78,5 +81,14 @@ std::string DataTypeToString(const proto::VarType::Type type) { static_cast(type)); } +size_t SizeOfType(std::type_index type) { + std::call_once(register_once_flag_, RegisterAllTypes); + auto it = g_data_type_map_.cpp_to_size_.find(type); + if (it != g_data_type_map_.cpp_to_size_.end()) { + return it->second; + } + PADDLE_THROW("Not support %s as tensor type", type.name()); +} + } // namespace framework } // namespace paddle diff --git a/paddle/fluid/framework/data_type.h b/paddle/fluid/framework/data_type.h index 06cc5940b75fd..4b9f572ec5f1c 100644 --- a/paddle/fluid/framework/data_type.h +++ b/paddle/fluid/framework/data_type.h @@ -17,6 +17,7 @@ limitations under the License. */ #include #include "paddle/fluid/framework/framework.pb.h" #include "paddle/fluid/platform/enforce.h" + #include "paddle/fluid/platform/float16.h" namespace paddle { @@ -24,6 +25,7 @@ namespace framework { extern proto::VarType::Type ToDataType(std::type_index type); extern std::type_index ToTypeIndex(proto::VarType::Type type); + template inline void VisitDataType(proto::VarType::Type type, Visitor visitor) { switch (type) { @@ -51,6 +53,7 @@ inline void VisitDataType(proto::VarType::Type type, Visitor visitor) { } extern std::string DataTypeToString(const proto::VarType::Type type); +extern size_t SizeOfType(std::type_index type); inline std::ostream& operator<<(std::ostream& out, const proto::VarType::Type& type) { out << DataTypeToString(type); diff --git a/paddle/fluid/framework/tensor_impl.h b/paddle/fluid/framework/tensor_impl.h index f49d1a47a325b..0a1db7758bd9e 100644 --- a/paddle/fluid/framework/tensor_impl.h +++ b/paddle/fluid/framework/tensor_impl.h @@ -13,54 +13,14 @@ See the License for the specific language governing permissions and limitations under the License. */ #pragma once +#include "paddle/fluid/framework/data_type.h" #include "paddle/fluid/memory/memcpy.h" #include "paddle/fluid/platform/enforce.h" #include "paddle/fluid/platform/float16.h" namespace paddle { namespace framework { - -template -struct SizeOfTypeFunctor; - -template -struct SizeOfTypeFunctor { - size_t operator()(std::type_index type) const { - if (typeid(T).hash_code() == type.hash_code()) { - return sizeof(T); - } else { - return 0UL; - } - } -}; - -template <> -struct SizeOfTypeFunctor<> { - size_t operator()(std::type_index type) const { return 0UL; } -}; - -template -struct SizeOfTypeFunctor { - size_t operator()(std::type_index type) const { - SizeOfTypeFunctor head; - size_t head_size = head(type); - if (head_size != 0) { - return head_size; - } - SizeOfTypeFunctor tail; - return tail(type); - } -}; - -static inline size_t SizeOfType(std::type_index type) { - SizeOfTypeFunctor - functor; - size_t size = functor(type); - PADDLE_ENFORCE(size != 0UL, "Cannot get size of type %s", type.name()); - return size; -} - +extern size_t SizeOfType(std::type_index type); inline void Tensor::check_memory_size() const { PADDLE_ENFORCE_NOT_NULL( holder_, "Tensor holds no memory. Call Tensor::mutable_data first."); From 9f705a4bb937aade91126595a0fb0e4b994d9793 Mon Sep 17 00:00:00 2001 From: yuyang18 Date: Fri, 11 May 2018 13:20:41 +0800 Subject: [PATCH 3/7] Use int instead of VarType as unordered_map key --- paddle/fluid/framework/data_type.cc | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/paddle/fluid/framework/data_type.cc b/paddle/fluid/framework/data_type.cc index f322584900269..9e5f2558910ef 100644 --- a/paddle/fluid/framework/data_type.cc +++ b/paddle/fluid/framework/data_type.cc @@ -19,8 +19,8 @@ namespace framework { struct DataTypeMap { std::unordered_map cpp_to_proto_; - std::unordered_map proto_to_cpp_; - std::unordered_map proto_to_str_; + std::unordered_map proto_to_cpp_; + std::unordered_map proto_to_str_; std::unordered_map cpp_to_size_; }; @@ -29,9 +29,10 @@ static DataTypeMap g_data_type_map_; template static inline void RegisterType(proto::VarType::Type proto_type, const std::string &name) { - g_data_type_map_.proto_to_cpp_.emplace(proto_type, typeid(T)); + g_data_type_map_.proto_to_cpp_.emplace(static_cast(proto_type), + typeid(T)); g_data_type_map_.cpp_to_proto_.emplace(typeid(T), proto_type); - g_data_type_map_.proto_to_str_.emplace(proto_type, name); + g_data_type_map_.proto_to_str_.emplace(static_cast(proto_type), name); g_data_type_map_.cpp_to_size_.emplace(typeid(T), sizeof(T)); } @@ -63,7 +64,7 @@ proto::VarType::Type ToDataType(std::type_index type) { std::type_index ToTypeIndex(proto::VarType::Type type) { std::call_once(register_once_flag_, RegisterAllTypes); - auto it = g_data_type_map_.proto_to_cpp_.find(type); + auto it = g_data_type_map_.proto_to_cpp_.find(static_cast(type)); if (it != g_data_type_map_.proto_to_cpp_.end()) { return it->second; } @@ -73,7 +74,7 @@ std::type_index ToTypeIndex(proto::VarType::Type type) { std::string DataTypeToString(const proto::VarType::Type type) { std::call_once(register_once_flag_, RegisterAllTypes); - auto it = g_data_type_map_.proto_to_str_.find(type); + auto it = g_data_type_map_.proto_to_str_.find(static_cast(type)); if (it != g_data_type_map_.proto_to_str_.end()) { return it->second; } From b9cc896545944f17238585c3f55f14a1f6972fbd Mon Sep 17 00:00:00 2001 From: yuyang18 Date: Mon, 14 May 2018 13:29:47 +0800 Subject: [PATCH 4/7] Add includes --- paddle/fluid/framework/data_type.cc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/paddle/fluid/framework/data_type.cc b/paddle/fluid/framework/data_type.cc index 9e5f2558910ef..28bbf82bc8e25 100644 --- a/paddle/fluid/framework/data_type.cc +++ b/paddle/fluid/framework/data_type.cc @@ -13,6 +13,10 @@ // limitations under the License. #include "paddle/fluid/framework/data_type.h" +#include +#include // NOLINT +#include +#include namespace paddle { namespace framework { From 715c933d887e7c16e66e76ecc884433fff2921a4 Mon Sep 17 00:00:00 2001 From: yuyang18 Date: Mon, 14 May 2018 15:23:14 +0800 Subject: [PATCH 5/7] Change deps --- paddle/fluid/framework/CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/paddle/fluid/framework/CMakeLists.txt b/paddle/fluid/framework/CMakeLists.txt index 384f084dde0aa..ed1e70c6460b5 100644 --- a/paddle/fluid/framework/CMakeLists.txt +++ b/paddle/fluid/framework/CMakeLists.txt @@ -5,11 +5,11 @@ proto_library(framework_proto SRCS framework.proto) cc_library(ddim SRCS ddim.cc DEPS eigen3 boost) cc_test(ddim_test SRCS ddim_test.cc DEPS ddim) nv_test(dim_test SRCS dim_test.cu DEPS ddim) -cc_library(data_type SRCS data_type.cc DEPS framework_proto ddim) +cc_library(data_type SRCS data_type.cc DEPS framework_proto ddim device_context) if(WITH_GPU) - nv_library(tensor SRCS tensor.cc tensor_util.cu DEPS place memory device_context data_type) + nv_library(tensor SRCS tensor.cc tensor_util.cu DEPS place memory data_type) else() - cc_library(tensor SRCS tensor.cc tensor_util.cc DEPS place memory device_context data_type) + cc_library(tensor SRCS tensor.cc tensor_util.cc DEPS place memory data_type) endif() cc_test(tensor_test SRCS tensor_test.cc DEPS tensor) From 66e82b986928ac00e8cb26b13341cd78252207f3 Mon Sep 17 00:00:00 2001 From: yuyang18 Date: Mon, 14 May 2018 19:05:40 +0800 Subject: [PATCH 6/7] Change implementation to fit sphinx model --- paddle/fluid/framework/data_type.cc | 52 ++++++++++++++--------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/paddle/fluid/framework/data_type.cc b/paddle/fluid/framework/data_type.cc index 28bbf82bc8e25..48267e1e34b8e 100644 --- a/paddle/fluid/framework/data_type.cc +++ b/paddle/fluid/framework/data_type.cc @@ -14,7 +14,6 @@ #include "paddle/fluid/framework/data_type.h" #include -#include // NOLINT #include #include @@ -28,20 +27,27 @@ struct DataTypeMap { std::unordered_map cpp_to_size_; }; -static DataTypeMap g_data_type_map_; +static DataTypeMap* InitDataTypeMap(); +static DataTypeMap& gDataTypeMap() { + static DataTypeMap* g_data_type_map_ = InitDataTypeMap(); + return *g_data_type_map_; +} template -static inline void RegisterType(proto::VarType::Type proto_type, - const std::string &name) { - g_data_type_map_.proto_to_cpp_.emplace(static_cast(proto_type), - typeid(T)); - g_data_type_map_.cpp_to_proto_.emplace(typeid(T), proto_type); - g_data_type_map_.proto_to_str_.emplace(static_cast(proto_type), name); - g_data_type_map_.cpp_to_size_.emplace(typeid(T), sizeof(T)); +static inline void RegisterType(DataTypeMap* map, + proto::VarType::Type proto_type, + const std::string& name) { + map->proto_to_cpp_.emplace(static_cast(proto_type), typeid(T)); + map->cpp_to_proto_.emplace(typeid(T), proto_type); + map->proto_to_str_.emplace(static_cast(proto_type), name); + map->cpp_to_size_.emplace(typeid(T), sizeof(T)); } -static int RegisterAllTypes() { -#define RegType(cc_type, proto_type) RegisterType(proto_type, #cc_type) +static DataTypeMap* InitDataTypeMap() { + auto retv = new DataTypeMap(); + +#define RegType(cc_type, proto_type) \ + RegisterType(retv, proto_type, #cc_type) // NOTE: Add your customize type here. RegType(platform::float16, proto::VarType::FP16); @@ -52,24 +58,20 @@ static int RegisterAllTypes() { RegType(bool, proto::VarType::BOOL); #undef RegType - return 0; + return retv; } -static std::once_flag register_once_flag_; - proto::VarType::Type ToDataType(std::type_index type) { - std::call_once(register_once_flag_, RegisterAllTypes); - auto it = g_data_type_map_.cpp_to_proto_.find(type); - if (it != g_data_type_map_.cpp_to_proto_.end()) { + auto it = gDataTypeMap().cpp_to_proto_.find(type); + if (it != gDataTypeMap().cpp_to_proto_.end()) { return it->second; } PADDLE_THROW("Not support %s as tensor type", type.name()); } std::type_index ToTypeIndex(proto::VarType::Type type) { - std::call_once(register_once_flag_, RegisterAllTypes); - auto it = g_data_type_map_.proto_to_cpp_.find(static_cast(type)); - if (it != g_data_type_map_.proto_to_cpp_.end()) { + auto it = gDataTypeMap().proto_to_cpp_.find(static_cast(type)); + if (it != gDataTypeMap().proto_to_cpp_.end()) { return it->second; } PADDLE_THROW("Not support proto::VarType::Type(%d) as tensor type", @@ -77,9 +79,8 @@ std::type_index ToTypeIndex(proto::VarType::Type type) { } std::string DataTypeToString(const proto::VarType::Type type) { - std::call_once(register_once_flag_, RegisterAllTypes); - auto it = g_data_type_map_.proto_to_str_.find(static_cast(type)); - if (it != g_data_type_map_.proto_to_str_.end()) { + auto it = gDataTypeMap().proto_to_str_.find(static_cast(type)); + if (it != gDataTypeMap().proto_to_str_.end()) { return it->second; } PADDLE_THROW("Not support proto::VarType::Type(%d) as tensor type", @@ -87,9 +88,8 @@ std::string DataTypeToString(const proto::VarType::Type type) { } size_t SizeOfType(std::type_index type) { - std::call_once(register_once_flag_, RegisterAllTypes); - auto it = g_data_type_map_.cpp_to_size_.find(type); - if (it != g_data_type_map_.cpp_to_size_.end()) { + auto it = gDataTypeMap().cpp_to_size_.find(type); + if (it != gDataTypeMap().cpp_to_size_.end()) { return it->second; } PADDLE_THROW("Not support %s as tensor type", type.name()); From 741401e9993899e17e6a571af6e6f4d199c00e2c Mon Sep 17 00:00:00 2001 From: yuyang18 Date: Tue, 15 May 2018 11:04:44 +0800 Subject: [PATCH 7/7] Update data_type --- paddle/fluid/framework/data_type.cc | 2 ++ paddle/fluid/framework/framework.proto | 2 ++ paddle/fluid/framework/op_kernel_type_test.cc | 2 +- 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/paddle/fluid/framework/data_type.cc b/paddle/fluid/framework/data_type.cc index 48267e1e34b8e..b9c90cb0c32f3 100644 --- a/paddle/fluid/framework/data_type.cc +++ b/paddle/fluid/framework/data_type.cc @@ -56,6 +56,8 @@ static DataTypeMap* InitDataTypeMap() { RegType(int, proto::VarType::INT32); RegType(int64_t, proto::VarType::INT64); RegType(bool, proto::VarType::BOOL); + RegType(size_t, proto::VarType::SIZE_T); + RegType(int16_t, proto::VarType::INT16); #undef RegType return retv; diff --git a/paddle/fluid/framework/framework.proto b/paddle/fluid/framework/framework.proto index 96f53dc1bc874..d2558f111f491 100644 --- a/paddle/fluid/framework/framework.proto +++ b/paddle/fluid/framework/framework.proto @@ -101,6 +101,8 @@ message VarType { FP16 = 4; FP32 = 5; FP64 = 6; + // Tensor is used in C++. + SIZE_T = 19; // Other types that may need additional descriptions LOD_TENSOR = 7; diff --git a/paddle/fluid/framework/op_kernel_type_test.cc b/paddle/fluid/framework/op_kernel_type_test.cc index d37ce149ce3df..db95861c510b5 100644 --- a/paddle/fluid/framework/op_kernel_type_test.cc +++ b/paddle/fluid/framework/op_kernel_type_test.cc @@ -27,7 +27,7 @@ TEST(OpKernelType, ToString) { LibraryType::kCUDNN); ASSERT_EQ(paddle::framework::KernelTypeToString(op_kernel_type), - "data_type[float32]:data_layout[NCHW]:place[CPUPlace]:library_type[" + "data_type[float]:data_layout[NCHW]:place[CPUPlace]:library_type[" "CUDNN]"); }