-
Notifications
You must be signed in to change notification settings - Fork 124
/
AttributeGetters.h
125 lines (106 loc) · 4.74 KB
/
AttributeGetters.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
118
119
120
121
122
123
124
/* Copyright © 2017-2020 ABBYY Production LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--------------------------------------------------------------------------------------------------------------*/
#pragma once
#include "TensorUtils.h"
namespace NeoOnnx {
// This file contains getters for different types of onnx attributes
template<class T>
inline void GetAttributeValue( const onnx::AttributeProto& attribute, T& /* value */, const COperator& op )
{
CheckNeoOnnxSupport( false, CString( "'" ) + attribute.name().c_str() + "' attribute's type", op );
}
template<>
inline void GetAttributeValue<int>( const onnx::AttributeProto& attribute, int& value, const COperator& op )
{
CheckOnnxProtocol( attribute.type() == onnx::AttributeProto_AttributeType_INT && attribute.has_i(),
( attribute.name() + " attribute is not an int" ).c_str(), op );
value = static_cast<int>( attribute.i() );
}
template<>
inline void GetAttributeValue<float>( const onnx::AttributeProto& attribute, float& value, const COperator& op )
{
CheckOnnxProtocol( attribute.type() == onnx::AttributeProto_AttributeType_FLOAT && attribute.has_f(),
( attribute.name() + " attribute is not a float" ).c_str(), op );
value = static_cast<float>( attribute.f() );
}
template<>
inline void GetAttributeValue<CString>( const onnx::AttributeProto& attribute, CString& value, const COperator& op )
{
CheckOnnxProtocol( attribute.type() == onnx::AttributeProto_AttributeType_STRING && attribute.has_s(),
( attribute.name() + " attribute is not a string" ).c_str(), op );
value = attribute.s().c_str();
}
template<>
inline void GetAttributeValue<CArray<int>>( const onnx::AttributeProto& attribute, CArray<int>& value, const COperator& op )
{
CheckOnnxProtocol( attribute.type() == onnx::AttributeProto_AttributeType_INTS,
( attribute.name() + " attribute is not an array of ints" ).c_str(), op );
value.Empty();
value.SetBufferSize( attribute.ints_size() );
for( int64_t element : attribute.ints() ) {
if( element >= static_cast<int64_t>( INT_MAX ) ) {
value.Add( INT_MAX );
} else if( element <= static_cast<int64_t>( INT_MIN ) ) {
value.Add( INT_MIN );
} else {
value.Add( static_cast<int>( element ) );
}
}
}
template<>
inline void GetAttributeValue<CArray<int64_t>>( const onnx::AttributeProto& attribute, CArray<int64_t>& value, const COperator& op )
{
CheckOnnxProtocol( attribute.type() == onnx::AttributeProto_AttributeType_INTS,
( attribute.name() + " attribute is not an array of ints" ).c_str(), op );
value.Empty();
value.SetBufferSize( attribute.ints_size() );
for( int64_t element : attribute.ints() ) {
value.Add( element );
}
}
template<>
inline void GetAttributeValue<CFastArray<int, 8>>( const onnx::AttributeProto& attribute, CFastArray<int, 8>& value, const COperator& op )
{
CheckOnnxProtocol( attribute.type() == onnx::AttributeProto_AttributeType_INTS,
( attribute.name() + " attribute is not an array of ints" ).c_str(), op );
for( int64_t element : attribute.ints() ) {
if( element >= static_cast<int64_t>( INT_MAX ) ) {
value.Add( INT_MAX );
} else if( element <= static_cast<int64_t>( INT_MIN ) ) {
value.Add( INT_MIN );
} else {
value.Add( static_cast<int>( element ) );
}
}
}
template<>
inline void GetAttributeValue<CPtr<CDataTensor>>( const onnx::AttributeProto& attribute, CPtr<CDataTensor>& value, const COperator& op )
{
CheckOnnxProtocol( attribute.type() == onnx::AttributeProto_AttributeType_TENSOR && attribute.has_t(),
( attribute.name() + " attribute is not a tensor" ).c_str(), op );
TBlobType resultDataType = GetBlobType( static_cast<onnx::TensorProto_DataType>( attribute.t().data_type() ) );
CTensorLayout resultLayout( attribute.t().dims().size() );
CBlobDesc desc( resultDataType );
CTensorShape resultShape;
for( int i = 0; i < attribute.t().dims().size(); ++i ) {
desc.SetDimSize( resultLayout[i], static_cast<int>( attribute.t().dims( i ) ) );
resultShape.Add( static_cast<int>( attribute.t().dims( i ) ) );
}
CPtr<CDnnBlob> resultBlob = CDnnBlob::CreateBlob( value->Data()->GetMathEngine(), resultDataType, desc );
if( resultDataType == CT_Float ) {
LoadBlobData<float>( attribute.t(), *resultBlob );
} else {
LoadBlobData<int>( attribute.t(), *resultBlob );
}
value = new CDataTensor( resultShape, resultLayout, *resultBlob );
}
} // namespace NeoOnnx