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