data_type.h 3.6 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Y
Yu Yang 已提交
2

L
Luo Tao 已提交
3 4 5
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
Y
Yu Yang 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
Y
Yu Yang 已提交
8

L
Luo Tao 已提交
9 10 11 12 13
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. */
Y
Yu Yang 已提交
14 15 16

#pragma once
#include <typeindex>
Y
Yi Wang 已提交
17 18
#include "paddle/fluid/framework/framework.pb.h"
#include "paddle/fluid/platform/enforce.h"
19
#include "paddle/fluid/platform/float16.h"
Y
Yu Yang 已提交
20 21 22 23

namespace paddle {
namespace framework {

24
inline proto::VarType::Type ToDataType(std::type_index type) {
25
  using namespace paddle::framework::proto;
26 27 28
  if (typeid(platform::float16).hash_code() == type.hash_code()) {
    return proto::VarType::FP16;
  } else if (typeid(float).hash_code() == type.hash_code()) {
29
    return proto::VarType::FP32;
Y
Yu Yang 已提交
30
  } else if (typeid(double).hash_code() == type.hash_code()) {
31
    return proto::VarType::FP64;
Y
Yu Yang 已提交
32
  } else if (typeid(int).hash_code() == type.hash_code()) {
33
    return proto::VarType::INT32;
34
  } else if (typeid(int64_t).hash_code() == type.hash_code()) {
35
    return proto::VarType::INT64;
Y
Yang Yang(Tony) 已提交
36
  } else if (typeid(bool).hash_code() == type.hash_code()) {
37
    return proto::VarType::BOOL;
Y
Yu Yang 已提交
38 39 40 41 42
  } else {
    PADDLE_THROW("Not supported");
  }
}

43
inline std::type_index ToTypeIndex(proto::VarType::Type type) {
44
  using namespace paddle::framework::proto;
Y
Yu Yang 已提交
45
  switch (type) {
46 47
    case proto::VarType::FP16:
      return typeid(platform::float16);
48
    case proto::VarType::FP32:
Y
Yu Yang 已提交
49
      return typeid(float);
50
    case proto::VarType::FP64:
Y
Yu Yang 已提交
51
      return typeid(double);
52
    case proto::VarType::INT32:
Y
Yu Yang 已提交
53
      return typeid(int);
54
    case proto::VarType::INT64:
Y
Yu Yang 已提交
55
      return typeid(int64_t);
56
    case proto::VarType::BOOL:
57
      return typeid(bool);
Y
Yu Yang 已提交
58 59 60 61 62
    default:
      PADDLE_THROW("Not support type %d", type);
  }
}

Y
Yu Yang 已提交
63
template <typename Visitor>
64
inline void VisitDataType(proto::VarType::Type type, Visitor visitor) {
65
  using namespace paddle::framework::proto;
Y
Yu Yang 已提交
66
  switch (type) {
67 68 69
    case proto::VarType::FP16:
      visitor.template operator()<platform::float16>();
      break;
70
    case proto::VarType::FP32:
Y
Yu Yang 已提交
71 72
      visitor.template operator()<float>();
      break;
73
    case proto::VarType::FP64:
Y
Yu Yang 已提交
74 75
      visitor.template operator()<double>();
      break;
76
    case proto::VarType::INT32:
Y
Yu Yang 已提交
77 78
      visitor.template operator()<int>();
      break;
79
    case proto::VarType::INT64:
Y
Yu Yang 已提交
80 81
      visitor.template operator()<int64_t>();
      break;
82
    case proto::VarType::BOOL:
83 84
      visitor.template operator()<bool>();
      break;
Y
Yu Yang 已提交
85 86 87 88 89
    default:
      PADDLE_THROW("Not supported");
  }
}

90
inline std::string DataTypeToString(const proto::VarType::Type type) {
C
chengduoZH 已提交
91 92
  using namespace paddle::framework::proto;
  switch (type) {
93
    case proto::VarType::FP16:
C
chengduoZH 已提交
94
      return "float16";
95
    case proto::VarType::FP32:
C
chengduoZH 已提交
96
      return "float32";
97
    case proto::VarType::FP64:
C
chengduoZH 已提交
98
      return "float64";
99
    case proto::VarType::INT16:
C
chengduoZH 已提交
100
      return "int16";
101
    case proto::VarType::INT32:
C
chengduoZH 已提交
102
      return "int32";
103
    case proto::VarType::INT64:
C
chengduoZH 已提交
104
      return "int64";
105
    case proto::VarType::BOOL:
C
chengduoZH 已提交
106 107 108 109 110 111 112
      return "bool";
    default:
      PADDLE_THROW("Not support type %d", type);
  }
}

inline std::ostream& operator<<(std::ostream& out,
113
                                const proto::VarType::Type& type) {
C
chengduoZH 已提交
114 115 116 117
  out << DataTypeToString(type);
  return out;
}

Y
Yu Yang 已提交
118 119
}  // namespace framework
}  // namespace paddle