data_type.h 4.0 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

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

namespace paddle {
namespace framework {

Y
Yu Yang 已提交
25 26 27 28 29 30
template <typename T>
struct DataTypeTrait {};

// Stub handle for void
template <>
struct DataTypeTrait<void> {
31 32 33
  constexpr static proto::VarType::Type DataType() {
    return proto::VarType::RAW;
  }
Y
Yu Yang 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
};

#define _ForEachDataTypeHelper_(callback, cpp_type, proto_type) \
  callback(cpp_type, ::paddle::framework::proto::VarType::proto_type);

#define _ForEachDataType_(callback)                                     \
  _ForEachDataTypeHelper_(callback, float, FP32);                       \
  _ForEachDataTypeHelper_(callback, ::paddle::platform::float16, FP16); \
  _ForEachDataTypeHelper_(callback, double, FP64);                      \
  _ForEachDataTypeHelper_(callback, int, INT32);                        \
  _ForEachDataTypeHelper_(callback, int64_t, INT64);                    \
  _ForEachDataTypeHelper_(callback, bool, BOOL);                        \
  _ForEachDataTypeHelper_(callback, uint8_t, UINT8);                    \
  _ForEachDataTypeHelper_(callback, int16_t, INT16);                    \
  _ForEachDataTypeHelper_(callback, int8_t, INT8)

50 51 52 53 54 55
#define _ForEachDataTypeSmall_(callback)           \
  _ForEachDataTypeHelper_(callback, float, FP32);  \
  _ForEachDataTypeHelper_(callback, double, FP64); \
  _ForEachDataTypeHelper_(callback, int, INT32);   \
  _ForEachDataTypeHelper_(callback, int64_t, INT64);

56 57 58 59
#define DefineDataTypeTrait(cpp_type, proto_type)                           \
  template <>                                                               \
  struct DataTypeTrait<cpp_type> {                                          \
    constexpr static proto::VarType::Type DataType() { return proto_type; } \
Y
Yu Yang 已提交
60 61 62 63 64 65
  }

_ForEachDataType_(DefineDataTypeTrait);

#undef DefineDataTypeTrait

Y
yuyang18 已提交
66 67
extern proto::VarType::Type ToDataType(std::type_index type);
extern std::type_index ToTypeIndex(proto::VarType::Type type);
Y
yuyang18 已提交
68

Y
Yu Yang 已提交
69
template <typename Visitor>
70
inline void VisitDataType(proto::VarType::Type type, Visitor visitor) {
Y
Yu Yang 已提交
71 72 73 74 75 76 77 78 79 80 81
#define VisitDataTypeCallback(cpp_type, proto_type) \
  do {                                              \
    if (type == proto_type) {                       \
      visitor.template apply<cpp_type>();           \
      return;                                       \
    }                                               \
  } while (0)

  _ForEachDataType_(VisitDataTypeCallback);
#undef VisitDataTypeCallback
  PADDLE_THROW("Not supported %d", type);
Y
Yu Yang 已提交
82 83
}

84 85 86 87 88 89 90 91 92 93 94 95 96 97
template <typename Visitor>
inline void VisitDataTypeSmall(proto::VarType::Type type, Visitor visitor) {
#define VisitDataTypeCallbackSmall(cpp_type, proto_type) \
  do {                                                   \
    if (type == proto_type) {                            \
      visitor.template apply<cpp_type>();                \
      return;                                            \
    }                                                    \
  } while (0)

  _ForEachDataTypeSmall_(VisitDataTypeCallbackSmall);
#undef VisitDataTypeCallbackSmall
}

Y
yuyang18 已提交
98
extern std::string DataTypeToString(const proto::VarType::Type type);
Y
Yu Yang 已提交
99
extern size_t SizeOfType(proto::VarType::Type type);
C
chengduoZH 已提交
100
inline std::ostream& operator<<(std::ostream& out,
101
                                const proto::VarType::Type& type) {
C
chengduoZH 已提交
102 103 104
  out << DataTypeToString(type);
  return out;
}
Y
Yu Yang 已提交
105 106
}  // namespace framework
}  // namespace paddle