Caffe2 - C++ API
A deep learning, cross platform ML framework
types.cc
1 #include "caffe2/core/types.h"
2 #include "caffe2/core/typeid.h"
3 
4 #include <atomic>
5 #include <memory>
6 #include <string>
7 #include <vector>
8 
9 namespace caffe2 {
10 
11 CAFFE_KNOWN_TYPE(float);
12 CAFFE_KNOWN_TYPE(int);
13 CAFFE_KNOWN_TYPE(std::string);
14 CAFFE_KNOWN_TYPE(bool);
15 CAFFE_KNOWN_TYPE(uint8_t);
16 CAFFE_KNOWN_TYPE(int8_t);
17 CAFFE_KNOWN_TYPE(uint16_t);
18 CAFFE_KNOWN_TYPE(int16_t);
19 CAFFE_KNOWN_TYPE(int64_t);
20 CAFFE_KNOWN_TYPE(float16);
21 CAFFE_KNOWN_TYPE(double);
22 CAFFE_KNOWN_TYPE(char);
23 CAFFE_KNOWN_TYPE(std::unique_ptr<std::mutex>);
24 CAFFE_KNOWN_TYPE(std::unique_ptr<std::atomic<bool>>);
25 CAFFE_KNOWN_TYPE(std::vector<int64_t>);
26 CAFFE_KNOWN_TYPE(std::vector<unsigned long>);
27 CAFFE_KNOWN_TYPE(bool*);
28 CAFFE_KNOWN_TYPE(char*);
29 CAFFE_KNOWN_TYPE(int*);
30 
31 #ifdef CAFFE2_UNIQUE_LONG_TYPEMETA
32 CAFFE_KNOWN_TYPE(long);
33 CAFFE_KNOWN_TYPE(std::vector<long>);
34 #endif // CAFFE2_UNIQUE_LONG_TYPEMETA
35 
36 TensorProto::DataType TypeMetaToDataType(const TypeMeta& meta) {
37  static_assert(sizeof(int) == 4,
38  "int in this compiler does not equal to 4 bytes.");
39  static std::map<CaffeTypeId, TensorProto::DataType> data_type_map {
40  {TypeMeta::Id<float>(), TensorProto_DataType_FLOAT},
41  {TypeMeta::Id<int>(), TensorProto_DataType_INT32},
42  // BYTE does not have a type meta to proto mapping: we should
43  // always use uint8_t when serializing. BYTE is kept for backward
44  // compatibility.
45  // {TypeMeta::Id<>(), TensorProto_DataType_BYTE},
46  {TypeMeta::Id<string>(), TensorProto_DataType_STRING},
47  {TypeMeta::Id<bool>(), TensorProto_DataType_BOOL},
48  {TypeMeta::Id<uint8_t>(), TensorProto_DataType_UINT8},
49  {TypeMeta::Id<int8_t>(), TensorProto_DataType_INT8},
50  {TypeMeta::Id<uint16_t>(), TensorProto_DataType_UINT16},
51  {TypeMeta::Id<int16_t>(), TensorProto_DataType_INT16},
52  {TypeMeta::Id<int64_t>(), TensorProto_DataType_INT64},
53  {TypeMeta::Id<float16>(), TensorProto_DataType_FLOAT16},
54  {TypeMeta::Id<double>(), TensorProto_DataType_DOUBLE},
55  };
56  const auto it = data_type_map.find(meta.id());
57  return (it == data_type_map.end()
58  ? TensorProto_DataType_UNDEFINED : it->second);
59 }
60 
61 const TypeMeta& DataTypeToTypeMeta(const TensorProto::DataType& dt) {
62  static std::map<TensorProto::DataType, TypeMeta> type_meta_map{
63  {TensorProto_DataType_FLOAT, TypeMeta::Make<float>()},
64  {TensorProto_DataType_INT32, TypeMeta::Make<int>()},
65  {TensorProto_DataType_STRING, TypeMeta::Make<std::string>()},
66  {TensorProto_DataType_BOOL, TypeMeta::Make<bool>()},
67  {TensorProto_DataType_UINT8, TypeMeta::Make<uint8_t>()},
68  {TensorProto_DataType_INT8, TypeMeta::Make<int8_t>()},
69  {TensorProto_DataType_UINT16, TypeMeta::Make<uint16_t>()},
70  {TensorProto_DataType_INT16, TypeMeta::Make<int16_t>()},
71  {TensorProto_DataType_INT64, TypeMeta::Make<int64_t>()},
72  {TensorProto_DataType_FLOAT16, TypeMeta::Make<float16>()},
73  {TensorProto_DataType_DOUBLE, TypeMeta::Make<double>()},
74  };
75  const auto it = type_meta_map.find(dt);
76  if (it == type_meta_map.end()) {
77  throw std::runtime_error("Unknown data type.");
78  }
79  return it->second;
80 }
81 
82 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...