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

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 17

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

namespace paddle {
namespace framework {

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

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

Y
Yu Yang 已提交
58
template <typename Visitor>
59 60
inline void VisitDataType(proto::DataType type, Visitor visitor) {
  using namespace paddle::framework::proto;
Y
Yu Yang 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73
  switch (type) {
    case DataType::FP32:
      visitor.template operator()<float>();
      break;
    case DataType::FP64:
      visitor.template operator()<double>();
      break;
    case DataType::INT32:
      visitor.template operator()<int>();
      break;
    case DataType::INT64:
      visitor.template operator()<int64_t>();
      break;
74 75 76
    case DataType::BOOL:
      visitor.template operator()<bool>();
      break;
Y
Yu Yang 已提交
77 78 79 80 81
    default:
      PADDLE_THROW("Not supported");
  }
}

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

inline std::ostream& operator<<(std::ostream& out,
                                const proto::DataType& type) {
  out << DataTypeToString(type);
  return out;
}

Y
Yu Yang 已提交
110 111
}  // namespace framework
}  // namespace paddle