Caffe2 - C++ API
A deep learning, cross platform ML framework
helper.h
1 #pragma once
2 
3 #include "caffe2/core/common.h"
4 #include "onnx/onnx_pb.h"
5 
6 #include <set>
7 #include <string>
8 #include <unordered_set>
9 
10 namespace caffe2 {
11 namespace onnx {
12 
13 using ::ONNX_NAMESPACE::AttributeProto;
14 using ::ONNX_NAMESPACE::NodeProto;
15 
16 // \brief This class generates unique dummy names
17 class CAFFE2_API DummyName {
18  public:
19  std::string NewDummyName();
20 
21  void Reset(const std::unordered_set<std::string>& used_names);
22 
23  void AddName(const std::string& new_used) {
24  used_names_.insert(new_used);
25  }
26 
27  private:
28  std::unordered_set<std::string> used_names_;
29  size_t counter_{0};
30 };
31 
32 ::ONNX_NAMESPACE::TypeProto ExtraTypeProto(
33  const ::ONNX_NAMESPACE::TensorProto& tensor);
34 
35 inline AttributeProto MakeAttribute(
36  const std::string& name,
37  const std::vector<int64_t>& vals) {
38  AttributeProto attr;
39  attr.set_name(name);
40  for (const auto v : vals) {
41  attr.add_ints(v);
42  }
43  attr.set_type(AttributeProto::INTS);
44  return attr;
45 }
46 
47 inline AttributeProto MakeAttribute(
48  const std::string& name,
49  const std::vector<float>& vals) {
50  AttributeProto attr;
51  attr.set_name(name);
52  for (const auto v : vals) {
53  attr.add_floats(v);
54  }
55  attr.set_type(AttributeProto::FLOATS);
56  return attr;
57 }
58 
59 inline AttributeProto MakeAttribute(const std::string& name, int64_t val) {
60  AttributeProto attr;
61  attr.set_name(name);
62  attr.set_i(val);
63  attr.set_type(AttributeProto::INT);
64  return attr;
65 }
66 
67 inline AttributeProto MakeAttribute(
68  const std::string& name,
69  const std::string& val) {
70  AttributeProto attr;
71  attr.set_name(name);
72  attr.set_s(val);
73  attr.set_type(AttributeProto::STRING);
74  return attr;
75 }
76 
77 inline AttributeProto MakeAttribute(
78  const std::string& name,
79  ::ONNX_NAMESPACE::TensorProto& val) {
80  AttributeProto attr;
81  attr.set_name(name);
82  attr.mutable_t()->CopyFrom(val);
83  attr.set_type(AttributeProto::TENSOR);
84  return attr;
85 }
86 
87 template <class T>
88 ::ONNX_NAMESPACE::TensorProto MakeTensor(
89  const string& name,
90  const std::vector<T>& v,
91  const ::ONNX_NAMESPACE::TensorProto_DataType& data_type_) {
92  ::ONNX_NAMESPACE::TensorProto ret;
93  ret.set_name(name);
94  ret.add_dims(v.size());
95  ret.set_data_type(data_type_);
96  ret.mutable_raw_data()->assign(
97  reinterpret_cast<const char*>(v.data()), v.size() * sizeof(T));
98  return ret;
99 }
100 
101 CAFFE2_API NodeProto MakeNode(
102  const std::string& type,
103  const std::vector<std::string>& inputs,
104  const std::vector<std::string>& outputs,
105  const std::vector<AttributeProto>& attributes,
106  const std::string& name = "");
107 
108 inline NodeProto MakeNode(
109  const std::string& type,
110  const std::vector<std::string>& inputs,
111  const std::vector<std::string>& outputs,
112  const std::string& name = "") {
113  return MakeNode(type, inputs, outputs, {}, name);
114 }
115 
116 } // namespace onnx
117 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13