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