data_type.h 8.7 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 <iostream>
17
#include <string>
Y
Yu Yang 已提交
18
#include <typeindex>
19

W
wanghuancoder 已提交
20
#include "paddle/fluid/framework/framework.pb.h"
21
#include "paddle/fluid/platform/bfloat16.h"
22
#include "paddle/fluid/platform/complex.h"
W
wanghuancoder 已提交
23
#include "paddle/fluid/platform/enforce.h"
24
#include "paddle/fluid/platform/float16.h"
25
#include "paddle/pten/kernels/funcs/eigen/extensions.h"
Y
Yu Yang 已提交
26 27 28 29

namespace paddle {
namespace framework {

H
hlygit66666 已提交
30 31 32 33 34 35
template <typename T>
struct IsComplex : public std::false_type {};

template <typename T>
struct IsComplex<platform::complex<T>> : public std::true_type {};

Y
Yu Yang 已提交
36 37 38 39 40 41
template <typename T>
struct DataTypeTrait {};

// Stub handle for void
template <>
struct DataTypeTrait<void> {
42 43 44
  constexpr static proto::VarType::Type DataType() {
    return proto::VarType::RAW;
  }
Y
Yu Yang 已提交
45 46 47 48 49
};

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

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#define _ForEachDataType_(callback)                                      \
  _ForEachDataTypeHelper_(callback, float, FP32);                        \
  _ForEachDataTypeHelper_(callback, ::paddle::platform::float16, FP16);  \
  _ForEachDataTypeHelper_(callback, ::paddle::platform::bfloat16, BF16); \
  _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);                       \
  _ForEachDataTypeHelper_(callback, ::paddle::platform::complex<float>,  \
                          COMPLEX64);                                    \
  _ForEachDataTypeHelper_(callback, ::paddle::platform::complex<double>, \
                          COMPLEX128);
65

66 67 68 69 70 71 72 73 74
#define _ForEachDataTypeSmall_(callback)                                 \
  _ForEachDataTypeHelper_(callback, float, FP32);                        \
  _ForEachDataTypeHelper_(callback, double, FP64);                       \
  _ForEachDataTypeHelper_(callback, int, INT32);                         \
  _ForEachDataTypeHelper_(callback, int64_t, INT64);                     \
  _ForEachDataTypeHelper_(callback, ::paddle::platform::complex<float>,  \
                          COMPLEX64);                                    \
  _ForEachDataTypeHelper_(callback, ::paddle::platform::complex<double>, \
                          COMPLEX128);
75

76 77 78 79 80
// For the use of thrust, as index-type elements can be only integers.
#define _ForEachDataTypeTiny_(callback)          \
  _ForEachDataTypeHelper_(callback, int, INT32); \
  _ForEachDataTypeHelper_(callback, int64_t, INT64);

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
// It's only for DataParallel in HIP, bf16 not support in HIP.
#define _ForEachDataTypeForHIP_(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);                       \
  _ForEachDataTypeHelper_(callback, ::paddle::platform::complex<float>,  \
                          COMPLEX64);                                    \
  _ForEachDataTypeHelper_(callback, ::paddle::platform::complex<double>, \
                          COMPLEX128);

97 98 99 100
#define DefineDataTypeTrait(cpp_type, proto_type)                           \
  template <>                                                               \
  struct DataTypeTrait<cpp_type> {                                          \
    constexpr static proto::VarType::Type DataType() { return proto_type; } \
Y
Yu Yang 已提交
101 102 103 104 105 106
  }

_ForEachDataType_(DefineDataTypeTrait);

#undef DefineDataTypeTrait

Y
yuyang18 已提交
107 108
extern proto::VarType::Type ToDataType(std::type_index type);
extern std::type_index ToTypeIndex(proto::VarType::Type type);
Y
yuyang18 已提交
109

Y
Yu Yang 已提交
110
template <typename Visitor>
111
inline void VisitDataType(proto::VarType::Type type, Visitor visitor) {
Y
Yu Yang 已提交
112 113 114 115 116 117 118 119 120 121
#define VisitDataTypeCallback(cpp_type, proto_type) \
  do {                                              \
    if (type == proto_type) {                       \
      visitor.template apply<cpp_type>();           \
      return;                                       \
    }                                               \
  } while (0)

  _ForEachDataType_(VisitDataTypeCallback);
#undef VisitDataTypeCallback
122 123 124
  PADDLE_THROW(platform::errors::Unimplemented(
      "Not supported proto::VarType::Type(%d) as data type.",
      static_cast<int>(type)));
Y
Yu Yang 已提交
125 126
}

127 128 129 130 131 132 133 134 135 136 137 138 139 140
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
}

141 142 143 144 145 146 147 148 149 150 151 152 153 154
template <typename Visitor>
inline void VisitDataTypeTiny(proto::VarType::Type type, Visitor visitor) {
#define VisitDataTypeCallbackTiny(cpp_type, proto_type) \
  do {                                                  \
    if (type == proto_type) {                           \
      visitor.template apply<cpp_type>();               \
      return;                                           \
    }                                                   \
  } while (0)

  _ForEachDataTypeTiny_(VisitDataTypeCallbackTiny);
#undef VisitDataTypeCallbackTiny
}

155 156 157 158 159 160 161 162 163 164 165 166 167 168
template <typename Visitor>
inline void VisitDataTypeForHIP(proto::VarType::Type type, Visitor visitor) {
#define VisitDataTypeCallbackHIP(cpp_type, proto_type) \
  do {                                                 \
    if (type == proto_type) {                          \
      visitor.template apply<cpp_type>();              \
      return;                                          \
    }                                                  \
  } while (0)

  _ForEachDataTypeForHIP_(VisitDataTypeCallbackHIP);
#undef VisitDataTypeCallbackHIP
}

Y
yuyang18 已提交
169
extern std::string DataTypeToString(const proto::VarType::Type type);
Y
Yu Yang 已提交
170
extern size_t SizeOfType(proto::VarType::Type type);
C
chengduoZH 已提交
171
inline std::ostream& operator<<(std::ostream& out,
172
                                const proto::VarType::Type& type) {
C
chengduoZH 已提交
173 174 175
  out << DataTypeToString(type);
  return out;
}
176 177 178 179 180 181 182 183 184

extern inline bool IsComplexType(const proto::VarType::Type type) {
  return (type == proto::VarType::COMPLEX64 ||
          type == proto::VarType::COMPLEX128);
}

extern proto::VarType::Type PromoteTypesIfComplexExists(
    const proto::VarType::Type type_a, const proto::VarType::Type type_b);

185 186 187 188 189 190 191 192
extern inline proto::VarType::Type ToComplexType(proto::VarType::Type t) {
  switch (t) {
    case proto::VarType::FP32:
      return proto::VarType::COMPLEX64;
    case proto::VarType::FP64:
      return proto::VarType::COMPLEX128;
    default:
      PADDLE_THROW(platform::errors::Unimplemented(
193
          "Unknown real value data type (%s), now only support float32 and "
194 195 196 197 198
          "float64.",
          DataTypeToString(t)));
  }
}

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
extern inline proto::VarType::Type ToRealType(proto::VarType::Type t) {
  switch (t) {
    case proto::VarType::COMPLEX64:
      return proto::VarType::FP32;
    case proto::VarType::COMPLEX128:
      return proto::VarType::FP64;
    default:
      PADDLE_THROW(platform::errors::Unimplemented(
          "Unknown complex value data type (%s), now only support complex64 "
          "and "
          "complex128.",
          DataTypeToString(t)));
  }
}

Y
Yu Yang 已提交
214 215
}  // namespace framework
}  // namespace paddle