This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
/
dot.cpp
210 lines (178 loc) · 7.43 KB
/
dot.cpp
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//*****************************************************************************
// Copyright 2017-2020 Intel Corporation
//
// 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.
//*****************************************************************************
#include <functional>
#include <memory>
#include "ngraph/axis_vector.hpp"
#include "ngraph/graph_util.hpp"
#include "ngraph/op/broadcast.hpp"
#include "ngraph/op/dot.hpp"
#include "ngraph/op/reshape.hpp"
#include "ngraph/shape.hpp"
using namespace std;
using namespace ngraph;
constexpr NodeTypeInfo op::v0::Dot::type_info;
op::v0::Dot::Dot(const Output<Node>& arg0, const Output<Node>& arg1)
: Dot(arg0, arg1, 0, false)
{
}
op::v0::Dot::Dot(const Output<Node>& arg0,
const Output<Node>& arg1,
size_t reduction_axes_count,
bool has_reduction_axes_count)
: Op({arg0, arg1})
, m_reduction_axes_count(reduction_axes_count)
, m_has_reduction_axes_count(has_reduction_axes_count)
{
constructor_validate_and_infer_types();
}
void op::v0::Dot::validate_and_infer_types()
{
element::Type result_et;
NODE_VALIDATION_CHECK(
this,
element::Type::merge(result_et, get_input_element_type(0), get_input_element_type(1)),
"Arguments do not have the same element type (arg0 element type: ",
get_input_element_type(0),
", arg1 element type: ",
get_input_element_type(1),
").");
const PartialShape& arg0_shape = get_input_partial_shape(0);
const PartialShape& arg1_shape = get_input_partial_shape(1);
// If an explicit value was not passed for reduction axis count at construction time, we have
// some extra work to do.
//
// - If one of the arguments is known to be scalar, the count is 0.
// - If both of the arguments are known to be nonscalar, the count is 1.
// - Otherwise, the count is unknown.
bool reduction_axes_ambiguous = !m_has_reduction_axes_count;
if (reduction_axes_ambiguous)
{
if (arg0_shape.rank().same_scheme(0) || arg1_shape.rank().same_scheme(0))
{
m_reduction_axes_count = 0;
reduction_axes_ambiguous = false;
}
else if (arg0_shape.rank().is_static() && arg1_shape.rank().is_static())
{
m_reduction_axes_count = 1;
reduction_axes_ambiguous = false;
}
}
PartialShape result_shape;
NODE_VALIDATION_CHECK(this,
reduction_axes_ambiguous || arg0_shape.rank().is_dynamic() ||
m_reduction_axes_count <= arg0_shape.rank().get_length(),
"Reduction axes count (",
m_reduction_axes_count,
") is too large (arg0 shape: ",
arg0_shape,
", arg1 shape: ",
arg1_shape,
").");
NODE_VALIDATION_CHECK(this,
reduction_axes_ambiguous || arg1_shape.rank().is_dynamic() ||
m_reduction_axes_count <= arg1_shape.rank().get_length(),
"Reduction axes count (",
m_reduction_axes_count,
") is too large (arg0 shape: ",
arg0_shape,
", arg1 shape: ",
arg1_shape,
").");
if (!reduction_axes_ambiguous && arg0_shape.rank().is_static() && arg1_shape.rank().is_static())
{
for (size_t i = 0; i < m_reduction_axes_count; i++)
{
size_t axis_index_arg0 = arg0_shape.rank().get_length() - m_reduction_axes_count + i;
size_t axis_index_arg1 = i;
NODE_VALIDATION_CHECK(
this,
arg0_shape[axis_index_arg0].compatible(arg1_shape[axis_index_arg1]),
"Paired axes (axis ",
axis_index_arg0,
" from arg0, axis ",
axis_index_arg1,
" from arg1) do not have same length (arg0 shape: ",
arg0_shape,
", arg1 shape: ",
arg1_shape,
", reduction axes count: ",
m_reduction_axes_count,
").");
}
std::vector<Dimension> result_dims(arg0_shape.rank().get_length() +
arg1_shape.rank().get_length() -
2 * m_reduction_axes_count);
size_t i = 0;
for (size_t j = 0; j < arg0_shape.rank().get_length() - m_reduction_axes_count; j++)
{
result_dims[i++] = arg0_shape[j];
}
for (size_t j = m_reduction_axes_count; j < arg1_shape.rank().get_length(); j++)
{
result_dims[i++] = arg1_shape[j];
}
result_shape = PartialShape(result_dims);
}
else
{
result_shape = PartialShape::dynamic();
}
set_output_type(0, result_et, result_shape);
}
shared_ptr<op::v0::Reshape> make_reshape_axes_to_front(const Output<Node>& n,
const Shape& front_shape,
const Shape& back_shape)
{
AxisVector input_order;
Shape output_shape;
for (size_t i = 0; i < back_shape.size(); i++)
{
input_order.push_back(front_shape.size() + i);
output_shape.push_back(back_shape[i]);
}
for (size_t i = 0; i < front_shape.size(); i++)
{
input_order.push_back(i);
output_shape.push_back(front_shape[i]);
}
return make_shared<op::v0::Reshape>(n, input_order, output_shape);
}
void op::v0::Dot::generate_adjoints(autodiff::Adjoints& adjoints, const OutputVector& deltas)
{
auto delta = deltas.at(0);
auto x = input_value(0);
auto y = input_value(1);
auto x_shape = x.get_shape(); // shape IJ
auto y_shape = y.get_shape(); // shape JK
auto delta_shape = delta.get_shape(); // shape IK
Shape I_shape;
Shape J_shape;
Shape K_shape;
I_shape.insert(I_shape.begin(), x_shape.begin(), x_shape.end() - m_reduction_axes_count);
J_shape.insert(J_shape.begin(), y_shape.begin(), y_shape.begin() + m_reduction_axes_count);
K_shape.insert(K_shape.begin(), y_shape.begin() + J_shape.size(), y_shape.end());
auto y_reshaped = make_reshape_axes_to_front(y, J_shape, K_shape); // KJ
auto delta_dot_y_reshaped = make_shared<Dot>(delta, y_reshaped, K_shape.size()); // IK.KJ->IJ
adjoints.add_delta(x, delta_dot_y_reshaped);
auto x_reshaped = make_reshape_axes_to_front(x, I_shape, J_shape); // JI
auto x_reshaped_dot_delta = make_shared<Dot>(x_reshaped, delta, I_shape.size()); // JI.IK->JK
adjoints.add_delta(y, x_reshaped_dot_delta);
}
shared_ptr<Node> op::v0::Dot::get_default_value() const
{
return ngraph::make_constant_from_string("0", get_output_element_type(0), get_output_shape(0));
}