-
Notifications
You must be signed in to change notification settings - Fork 546
/
OnnxAttrs.hpp
59 lines (49 loc) · 1.36 KB
/
OnnxAttrs.hpp
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
/*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <NvInfer.h>
#include <onnx/onnx_pb.h>
#include <unordered_map>
#include <vector>
#include "ImporterContext.hpp"
class OnnxAttrs
{
template <typename T>
using string_map = std::unordered_map<std::string, T>;
typedef string_map<::ONNX_NAMESPACE::AttributeProto const*> AttrMap;
AttrMap _attrs;
onnx2trt::ImporterContext* mCtx;
public:
explicit OnnxAttrs(::ONNX_NAMESPACE::NodeProto const& onnx_node, onnx2trt::ImporterContext* ctx)
: mCtx{ctx}
{
for (auto const& attr : onnx_node.attribute())
{
_attrs.insert({attr.name(), &attr});
}
}
bool count(std::string const& key) const
{
return _attrs.count(key);
}
::ONNX_NAMESPACE::AttributeProto const* at(std::string key) const
{
if (!_attrs.count(key))
{
throw std::out_of_range("Attribute not found: " + key);
}
return _attrs.at(key);
}
::ONNX_NAMESPACE::AttributeProto::AttributeType type(std::string const& key) const
{
return this->at(key)->type();
}
template <typename T>
T get(std::string const& key) const;
template <typename T>
T get(std::string const& key, T const& default_value) const
{
return _attrs.count(key) ? this->get<T>(key) : default_value;
}
};