-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_flow_ops.py
394 lines (325 loc) · 12.5 KB
/
data_flow_ops.py
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
"""Data flow related Operations."""
import numpy as np
from .generic_ops import Const
from .mixins import _ShapeAsIs
from .operation import Operation
from .tensor_shape import TensorShape
class DynamicStitch(Operation):
def __init__(self, input_list, graph=None, accumulate=False, name=None):
super(DynamicStitch, self).__init__(
graph=graph, name=name, input_list=input_list
)
self._accumulate = accumulate
def _run(self, *inputs_list):
size = len(inputs_list) // 2
indices, data = inputs_list[:size], inputs_list[size:]
data = np.concatenate([
data[i].reshape((-1,) + data[i].shape[indices[i].ndim:])
for i in range(len(data))
])
indices = np.concatenate([indices[i].ravel() for i in range(len(indices))])
outputs = np.zeros((indices.max() + 1,) + data.shape[1:], dtype="float32")
for i, ind in enumerate(indices):
if self._accumulate:
outputs[ind] += data[i]
else:
outputs[ind] = data[i]
return outputs
def _grad_func(self, in_grad_tensors):
with self._graph.as_default_graph():
size = len(self._input_list) // 2
out_grad_tensors = []
for i, (indices, params) in enumerate(
zip(self._input_list[:size], in_grad_tensors * size)
):
bp_data = Gather(
input_list=[
params, indices,
Const(value=np.asarray(0, dtype="int32")).output(0)
]
)
out_grad_tensors.append(bp_data.output(0))
return out_grad_tensors
def _get_bp_indices(self):
size = len(self._input_list) // 2
bp_indices = set(range(size, size * 2))
return bp_indices
def _compute_shapes(self):
# validation
assert len(self._input_list) % 2 == 0
size = len(self._input_list) // 2
constant = None
constant_ndims = None
for indices, data in zip(self._input_list[:size], self._input_list[size:]):
if indices.shape.level > 0 and data.shape.level > 0:
assert data.shape.ndims >= indices.shape.ndims
if constant_ndims is None:
constant_ndims = data.shape.ndims - indices.shape.ndims
else:
assert data.shape.ndims - indices.shape.ndims == constant_ndims
if indices.shape.level == 2 and data.shape.level == 2:
if constant is None:
constant = TensorShape(data.shape.raw_shape[-constant_ndims:])
else:
assert constant._compatible_with(data.shape[-constant_ndims:])
constant._merge(data.shape[-constant_ndims:])
# compute shapes
if constant is not None:
return [TensorShape((None,) + constant.raw_shape)]
elif constant_ndims is not None:
return [TensorShape([None] * (constant_ndims + 1))]
else:
return [TensorShape(None)]
class Gather(Operation):
def _run(self, params, indices, axis):
outputs = np.take(params, indices, axis=axis.item())
return outputs
def _grad_func(self, in_grad_tensors):
from .array_ops import Concat, ExpandDims, Fill, Range, Slice, Transpose
from .math_ops import Add, FloorMod, Sub
with self._graph.as_default_graph():
zero_array_tensor = Const(value=np.asarray([0], dtype="int32")).output(0)
zero_scalar_tensor = Const(value=np.asarray(0, dtype="int32")).output(0)
one_array_tensor = Const(value=np.asarray([1], dtype="int32")).output(0)
one_scalar_tensor = Const(value=np.asarray(1, dtype="int32")).output(0)
op, tensor_index = (
self._input_list[0].op, self._input_list[0].tensor_index
)
mod_tensor = FloorMod(
input_list=[
self._input_list[2],
op.get_rank_tensor(tensor_index=tensor_index)
]
).output(0)
op, tensor_index = in_grad_tensors[0].op, in_grad_tensors[0].tensor_index
range0 = Range(
input_list=[
mod_tensor,
op.get_rank_tensor(tensor_index=tensor_index), one_scalar_tensor
]
)
range1 = Range(
input_list=[zero_scalar_tensor, mod_tensor, one_scalar_tensor]
)
perm = Concat(
input_list=[
zero_scalar_tensor,
range0.output(0),
range1.output(0),
]
)
transpose = Transpose(input_list=[in_grad_tensors[0], perm.output(0)])
ds = DynamicStitch(
accumulate=True,
input_list=[self._input_list[1],
transpose.output(0)]
)
rank_tensor = ds.get_rank_tensor(tensor_index=0)
sub_tensor = Sub(input_list=[rank_tensor, mod_tensor]).output(0)
range2 = Range(input_list=[sub_tensor, rank_tensor, one_scalar_tensor])
range3 = Range(
input_list=[zero_scalar_tensor, sub_tensor, one_scalar_tensor]
)
perm1 = Concat(
input_list=[
zero_scalar_tensor,
range2.output(0),
range3.output(0),
]
)
transpose1 = Transpose(input_list=[ds.output(0), perm1.output(0)])
shape_tensor = transpose1.get_shape_tensor(tensor_index=0)
ed_tensor = ExpandDims(input_list=[mod_tensor, zero_scalar_tensor]
).output(0)
slice0 = Slice(input_list=[shape_tensor, ed_tensor, one_array_tensor])
op, tensor_index = self._input_list[0].op, self._input_list[0
].tensor_index
slice1 = Slice(
input_list=[
op.get_shape_tensor(tensor_index=tensor_index), ed_tensor,
one_array_tensor
]
)
sub = Sub(input_list=[slice1.output(0), slice0.output(0)])
slice2 = Slice(input_list=[shape_tensor, zero_array_tensor, ed_tensor])
slice3 = Slice(
input_list=[
shape_tensor,
Add(input_list=[ed_tensor, one_array_tensor]).output(0),
Const(value=np.asarray([-1], dtype="int32")).output(0)
]
)
concat = Concat(
input_list=[
zero_scalar_tensor,
slice2.output(0),
sub.output(0),
slice3.output(0)
]
)
fill = Fill(input_list=[concat.output(0), zero_scalar_tensor])
concat1 = Concat(
input_list=[mod_tensor,
transpose1.output(0),
fill.output(0)]
)
out_grad_tensors = [concat1.output(0)]
return out_grad_tensors
def _get_bp_indices(self):
return [0]
def _compute_shapes(self):
# validation
if self._input_list[2].shape.level > 0:
assert self._input_list[2].shape.ndims == 0
# compute shapes
if self._input_list[0].shape.level > 0 and self._input_list[
1].shape.level > 0:
if (
self._input_list[0].shape.level == 2 and
self._input_list[1].shape.level == 2 and
hasattr(self._input_list[2].op, "_value")
):
axis = self._input_list[2].op._value.item()
params_shape = list(self._input_list[0].shape.raw_shape)
indices_shape = list(self._input_list[1].shape.raw_shape)
raw_shape = params_shape[:axis] + indices_shape + params_shape[axis +
1:]
return [TensorShape(raw_shape)]
else:
return [
TensorShape([None] * (
self._input_list[0].shape.ndims +
self._input_list[1].shape.ndims - 1
))
]
else:
return [TensorShape(None)]
class BroadcastTo(Operation):
""""""
def _run(self, inputs, target_shape):
shape = np.pad(
inputs.shape, [len(target_shape) - len(inputs.shape), 0],
constant_values=1
)
multiples = np.where(shape != target_shape, target_shape, 1)
outputs = np.tile(inputs, multiples)
return outputs
def _grad_func(self, in_grad_tensors):
from .array_ops import Reshape
from .math_ops import BroadcastGradientArgs, Sum
with self._graph.as_default_graph():
op, tensor_index = self._input_list[0].op, self._input_list[0
].tensor_index
shape_tensor = op.get_shape_tensor(tensor_index=tensor_index)
bga = BroadcastGradientArgs(
input_list=[shape_tensor, self._input_list[1]],
)
sum0 = Sum(input_list=[in_grad_tensors[0], bga.output(0)])
bp_inputs = Reshape(input_list=[sum0.output(0), shape_tensor])
out_grad_tensors = [bp_inputs.output(0)]
return out_grad_tensors
def _get_bp_indices(self):
return [0]
def _compute_shapes(self):
# validation
if hasattr(self._input_list[1].op, "_value"):
target_shape = self._input_list[1].op._value
assert target_shape.ndim == 1 and (target_shape > 0).all()
if self._input_list[0].shape.level > 0:
assert all([
x is None or x == 1 or x == y for x, y in
zip(self._input_list[0].shape[::-1], target_shape[::-1])
])
if self._input_list[1].shape.level > 0:
assert self._input_list[1].shape.ndims == 1
if self._input_list[1].shape.level == 2:
orig_ndims = self._input_list[0].shape.ndims
assert orig_ndims is None or orig_ndims <= self._input_list[1].shape[0]
# compute shapes
if hasattr(self._input_list[1].op, "_value"):
return [TensorShape(self._input_list[1].op._value.tolist())]
elif self._input_list[1].shape.level == 2:
ndims = self._input_list[1].shape[0]
return [TensorShape([None] * ndims)]
else:
return [TensorShape(None)]
class Select(Operation):
def _run(self, condition, x, y):
outputs = np.where(condition, x, y)
return outputs
def _grad_func(self, in_grad_tensors):
from .array_ops import Reshape
from .math_ops import BroadcastGradientArgs, Sum
with self._graph.as_default_graph():
op_x = self._input_list[1].op
tensor_index_x = self._input_list[1].tensor_index
op_y = self._input_list[2].op
tensor_index_y = self._input_list[2].tensor_index
shape_tensor = op_x.get_shape_tensor(tensor_index=tensor_index_x)
shape1_tensor = op_y.get_shape_tensor(tensor_index=tensor_index_y)
shape2_tensor = self.get_shape_tensor(tensor_index=0)
select = Select(
input_list=[
self._input_list[0], in_grad_tensors[0],
Const(value=np.asarray(0, dtype="float32")).output(0)
]
)
select1 = Select(
input_list=[
self._input_list[0],
Const(value=np.asarray(0, dtype="float32")).output(0),
in_grad_tensors[0]
]
)
bga = BroadcastGradientArgs(input_list=[shape_tensor, shape2_tensor])
bga1 = BroadcastGradientArgs(input_list=[shape1_tensor, shape2_tensor])
sum0 = Sum(input_list=[select.output(0), bga.output(0)])
sum1 = Sum(input_list=[select1.output(0), bga1.output(0)])
bp_x = Reshape(input_list=[sum0.output(0), shape_tensor])
bp_y = Reshape(input_list=[sum1.output(0), shape1_tensor])
out_grad_tensors = [bp_x.output(0), bp_y.output(0)]
return out_grad_tensors
def _get_bp_indices(self):
return [1, 2]
def _compute_shapes(self):
# validation
c_shape = self._input_list[0].shape
x_shape = self._input_list[1].shape
y_shape = self._input_list[2].shape
assert (
c_shape._broadcastable_with(x_shape) and
c_shape._broadcastable_with(y_shape) and
x_shape._broadcastable_with(y_shape)
)
# compute shapes
if c_shape.level > 0 and x_shape.level > 0 and y_shape.level > 0:
max_ndims = max(c_shape.ndims, x_shape.ndims, y_shape.ndims)
c_shape = [None] * (max_ndims - c_shape.ndims) + list(c_shape.raw_shape)
x_shape = [None] * (max_ndims - x_shape.ndims) + list(x_shape.raw_shape)
y_shape = [None] * (max_ndims - y_shape.ndims) + list(y_shape.raw_shape)
raw_shape = []
for c, x, y in zip(c_shape, x_shape, y_shape):
size = set([c, x, y])
if len(size) == 1 and None in size:
raw_shape.append(None)
else:
size = size - set([None])
if len(size) == 1 and 1 in size:
raw_shape.append(1)
else:
size = size - set([1])
raw_shape.append(list(size)[0])
return [TensorShape(raw_shape)]
else:
return [TensorShape(None)]
class StopGradient(Operation, _ShapeAsIs):
def _run(self, inputs):
outputs = inputs
return outputs
class Identity(Operation, _ShapeAsIs):
def _run(self, inputs):
outputs = inputs
return outputs
def _grad_func(self, in_grad_tensors):
out_grad_tensors = in_grad_tensors
return out_grad_tensors