forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.h
117 lines (99 loc) · 2.75 KB
/
helper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
110
111
112
113
114
115
116
117
#pragma once
#include "caffe2/core/common.h"
#include "onnx/onnx_pb.h"
#include <set>
#include <string>
#include <unordered_set>
namespace caffe2 {
namespace onnx {
using ::ONNX_NAMESPACE::AttributeProto;
using ::ONNX_NAMESPACE::NodeProto;
// \brief This class generates unique dummy names
class CAFFE2_API DummyName {
public:
std::string NewDummyName();
void Reset(const std::unordered_set<std::string>& used_names);
void AddName(const std::string& new_used) {
used_names_.insert(new_used);
}
private:
std::unordered_set<std::string> used_names_;
size_t counter_{0};
};
::ONNX_NAMESPACE::TypeProto ExtraTypeProto(
const ::ONNX_NAMESPACE::TensorProto& tensor);
inline AttributeProto MakeAttribute(
const std::string& name,
const std::vector<int64_t>& vals) {
AttributeProto attr;
attr.set_name(name);
for (const auto v : vals) {
attr.add_ints(v);
}
attr.set_type(AttributeProto::INTS);
return attr;
}
inline AttributeProto MakeAttribute(
const std::string& name,
const std::vector<float>& vals) {
AttributeProto attr;
attr.set_name(name);
for (const auto v : vals) {
attr.add_floats(v);
}
attr.set_type(AttributeProto::FLOATS);
return attr;
}
inline AttributeProto MakeAttribute(const std::string& name, int64_t val) {
AttributeProto attr;
attr.set_name(name);
attr.set_i(val);
attr.set_type(AttributeProto::INT);
return attr;
}
inline AttributeProto MakeAttribute(
const std::string& name,
const std::string& val) {
AttributeProto attr;
attr.set_name(name);
attr.set_s(val);
attr.set_type(AttributeProto::STRING);
return attr;
}
inline AttributeProto MakeAttribute(
const std::string& name,
::ONNX_NAMESPACE::TensorProto& val) {
AttributeProto attr;
attr.set_name(name);
attr.mutable_t()->CopyFrom(val);
attr.set_type(AttributeProto::TENSOR);
return attr;
}
template <class T>
::ONNX_NAMESPACE::TensorProto MakeTensor(
const string& name,
const std::vector<T>& v,
const ::ONNX_NAMESPACE::TensorProto_DataType& data_type_) {
::ONNX_NAMESPACE::TensorProto ret;
ret.set_name(name);
ret.add_dims(v.size());
ret.set_data_type(data_type_);
ret.mutable_raw_data()->assign(
reinterpret_cast<const char*>(v.data()), v.size() * sizeof(T));
return ret;
}
CAFFE2_API NodeProto MakeNode(
const std::string& type,
const std::vector<std::string>& inputs,
const std::vector<std::string>& outputs,
const std::vector<AttributeProto>& attributes,
const std::string& name = "");
inline NodeProto MakeNode(
const std::string& type,
const std::vector<std::string>& inputs,
const std::vector<std::string>& outputs,
const std::string& name = "") {
return MakeNode(type, inputs, outputs, {}, name);
}
} // namespace onnx
} // namespace caffe2