|
| 1 | +// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "paddle/fluid/framework/data_type.h" |
| 16 | + |
| 17 | +namespace paddle { |
| 18 | +namespace framework { |
| 19 | + |
| 20 | +struct DataTypeMap { |
| 21 | + std::unordered_map<std::type_index, proto::VarType::Type> cpp_to_proto_; |
| 22 | + std::unordered_map<proto::VarType::Type, std::type_index> proto_to_cpp_; |
| 23 | + std::unordered_map<proto::VarType::Type, std::string> proto_to_str_; |
| 24 | +}; |
| 25 | + |
| 26 | +static DataTypeMap g_data_type_map_; |
| 27 | + |
| 28 | +template <typename T> |
| 29 | +static inline void RegisterType(proto::VarType::Type proto_type, |
| 30 | + const std::string &name) { |
| 31 | + g_data_type_map_.proto_to_cpp_.emplace(proto_type, typeid(T)); |
| 32 | + g_data_type_map_.cpp_to_proto_.emplace(typeid(T), proto_type); |
| 33 | + g_data_type_map_.proto_to_str_.emplace(proto_type, name); |
| 34 | +} |
| 35 | + |
| 36 | +static int RegisterAllTypes() { |
| 37 | +#define RegType(cc_type, proto_type) RegisterType<cc_type>(proto_type, #cc_type) |
| 38 | + |
| 39 | + RegType(platform::float16, proto::VarType::FP16); |
| 40 | + RegType(float, proto::VarType::FP32); |
| 41 | + RegType(double, proto::VarType::FP64); |
| 42 | + RegType(int, proto::VarType::INT32); |
| 43 | + RegType(int64_t, proto::VarType::INT64); |
| 44 | + RegType(bool, proto::VarType::BOOL); |
| 45 | + |
| 46 | +#undef RegType |
| 47 | + return 0; |
| 48 | +} |
| 49 | + |
| 50 | +static std::once_flag register_once_flag_; |
| 51 | + |
| 52 | +proto::VarType::Type ToDataType(std::type_index type) { |
| 53 | + std::call_once(register_once_flag_, RegisterAllTypes); |
| 54 | + auto it = g_data_type_map_.cpp_to_proto_.find(type); |
| 55 | + if (it != g_data_type_map_.cpp_to_proto_.end()) { |
| 56 | + return it->second; |
| 57 | + } |
| 58 | + PADDLE_THROW("Not support %s as tensor type", type.name()); |
| 59 | +} |
| 60 | + |
| 61 | +std::type_index ToTypeIndex(proto::VarType::Type type) { |
| 62 | + std::call_once(register_once_flag_, RegisterAllTypes); |
| 63 | + auto it = g_data_type_map_.proto_to_cpp_.find(type); |
| 64 | + if (it != g_data_type_map_.proto_to_cpp_.end()) { |
| 65 | + return it->second; |
| 66 | + } |
| 67 | + PADDLE_THROW("Not support proto::VarType::Type(%d) as tensor type", |
| 68 | + static_cast<int>(type)); |
| 69 | +} |
| 70 | + |
| 71 | +std::string DataTypeToString(const proto::VarType::Type type) { |
| 72 | + std::call_once(register_once_flag_, RegisterAllTypes); |
| 73 | + auto it = g_data_type_map_.proto_to_str_.find(type); |
| 74 | + if (it != g_data_type_map_.proto_to_str_.end()) { |
| 75 | + return it->second; |
| 76 | + } |
| 77 | + PADDLE_THROW("Not support proto::VarType::Type(%d) as tensor type", |
| 78 | + static_cast<int>(type)); |
| 79 | +} |
| 80 | + |
| 81 | +} // namespace framework |
| 82 | +} // namespace paddle |
0 commit comments