-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathops.py
More file actions
570 lines (503 loc) · 24 KB
/
Copy pathops.py
File metadata and controls
570 lines (503 loc) · 24 KB
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
# Copyright 2021 Alibaba Group Holding Limited. All Rights Reserved.
#
# 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.
# =============================================================================
"""Operator functions for doing parallelism."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import copy
from distutils.version import LooseVersion as Version
from tensorflow.core.protobuf import control_flow_pb2
from tensorflow.python.framework.function import _DefinedFunction
from tensorflow.python.framework.versions import __version__
from tensorflow.python.framework import dtypes
from tensorflow.python.ops import control_flow_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.framework import ops
from tensorflow.python.ops import array_ops
from tensorflow.python.util import compat
from epl.communicators.collective_communicator import CollectiveCommunicator
from epl.env import Env
from epl.ir.graph import Graph
from epl.ir.phase import ModelPhase
from epl.ir.operation import Operation as EplOperation
from epl.ir.tensor import Tensor as EplTensor
from epl.utils import constant, common
class Colocate(object):
"""Colocate operations."""
def __init__(self, colocate_obj):
if isinstance(colocate_obj, ops.Tensor):
colocate_obj = Graph.get().get_tensor_by_name(colocate_obj.name).op
elif isinstance(colocate_obj, ops.Operation):
colocate_obj = Graph.get().get_operation_by_name(colocate_obj.name)
if not isinstance(colocate_obj, (EplTensor, EplOperation)):
raise RuntimeError("Colocate only supports tensor and operation as input, but got {}".format(colocate_obj))
self._taskgraph = colocate_obj.taskgraph
self._phase = colocate_obj.phase
self._graph = self._taskgraph.graph
self._device = colocate_obj.device
def __enter__(self):
self._graph.colocate_taskgraph = self._taskgraph
self._graph.colocate_phase = self._phase
self._graph.colocate_device = self._device
def __exit__(self, unused_exception_type, unused_exception_value,
unused_traceback):
self._graph.colocate_taskgraph = None
self._graph.colocate_phase = None
self._graph.colocate_device = None
def node_clone_for_pipeline(graph, orig_op, micro_batch_idx, device):
"""Clone a operation to 'device' from 'orig_op' for pipeline."""
micro_batch_prefix = common.get_micro_batch_prefix(micro_batch_idx)
# get node def
node_def = copy.deepcopy(orig_op.node_def)
node_def.name = micro_batch_prefix + node_def.name
frame_name = node_def.attr.get('frame_name')
if frame_name:
node_def.attr.get('frame_name').s = compat.as_bytes(micro_batch_prefix, constant.ENCODING) + frame_name.s
op_def = copy.deepcopy(orig_op.op_def)
output_types = orig_op.output_types[:]
input_types = orig_op.input_types[:]
graph.unready_inputs_cache[node_def.name] = dict()
graph.unready_control_inputs_cache[node_def.name] = dict()
# get inputs
inputs = []
for inp_idx, inp in enumerate(orig_op.inputs):
if graph.is_dataset_type(orig_op) or \
graph.is_dataset_related(inp.producer) or \
graph.is_vars_related(inp.producer) or \
graph.is_global_step_related(inp):
name = inp.name
else:
name = micro_batch_prefix + inp.name
if name in graph.tensors:
inputs.append(graph.get_tensor_by_name(name).primitive_obj)
else:
tensor = graph.get_tensor_by_name(inp.name)
if tensor.producer.get_control_flow_context() is not None:
graph.original_context_cache[tensor.producer.name] = tensor.producer.get_control_flow_context()
tensor.producer.set_control_flow_context(None)
inputs.append(tensor.primitive_obj)
graph.unready_inputs_cache[node_def.name][inp_idx] = name
# get control inputs
control_inputs = []
old_control_inputs = list(orig_op.control_inputs)
for c_inp in old_control_inputs:
c_inp = c_inp if isinstance(c_inp, ops.Operation) else c_inp.producer
c_inp = graph.get_operation_by_name(c_inp.name)
if graph.is_dataset_related(orig_op) or \
graph.is_dataset_related(c_inp) or \
graph.is_vars_related(c_inp) or \
graph.is_global_step_related(c_inp):
name = c_inp.name
else:
name = micro_batch_prefix + c_inp.name
if name in graph.operations:
control_inputs.append(graph.get_operation_by_name(name).primitive_obj)
else:
op = graph.get_operation_by_name(c_inp.name)
if op.get_control_flow_context() is not None:
graph.original_context_cache[op.name] = op.get_control_flow_context()
op.set_control_flow_context(None)
control_inputs.append(op.primitive_obj)
graph.unready_control_inputs_cache[node_def.name][op.name] = name
if not graph.unready_inputs_cache[node_def.name]:
del graph.unready_inputs_cache[node_def.name]
if not graph.unready_control_inputs_cache[node_def.name]:
del graph.unready_control_inputs_cache[node_def.name]
with ModelPhase(orig_op.phase):
graph.current_cloned_taskgraph = orig_op.taskgraph
new_op = ops.Operation(node_def,
ops.get_default_graph(),
inputs,
output_types,
control_inputs,
input_types,
None,
op_def=op_def)
new_op._set_device(device) # pylint: disable=protected-access
def node_clone_for_replicas(graph, orig_op, replica_idx, device):
"""Clone a operation to 'device' from 'orig_op' for data parallelism."""
replica_prefix = common.get_replica_prefix(replica_idx)
# get node def
node_def = copy.deepcopy(orig_op.node_def)
loc_attr = node_def.attr.get('_class')
node_def.name = replica_prefix + node_def.name
if loc_attr:
for idx in range(len(loc_attr.list.s)):
loc_attr.list.s[idx] = loc_attr.list.s[idx].replace(b'@', b'@' + compat.as_bytes(replica_prefix, constant.ENCODING))
for attr_name in ["frame_name", "shared_name"]:
attr_value = node_def.attr.get(attr_name)
if attr_value and attr_value.s:
node_def.attr.get(attr_name).s = compat.as_bytes(replica_prefix, constant.ENCODING) + attr_value.s
if graph.clone_dataset_related_ops and \
graph.is_dataset_related(orig_op) and \
node_def.attr.get('value') and \
node_def.attr.get("value").tensor and \
node_def.attr.get("value").tensor.dtype in constant.INPUT_FILE_TYPE:
value_name = node_def.attr.get("value").tensor.string_val
new_value_name = list()
for value in value_name:
replica_value_name = True
for data_format in constant.PAI_DATA_FORMAT:
if value.startswith(data_format):
replica_value_name = False
break
if replica_value_name:
value = replica_prefix + value
new_value_name.append(value)
node_def.attr.get('value').tensor.string_val[:] = new_value_name
op_def = copy.deepcopy(orig_op.op_def)
output_types = orig_op.output_types[:]
input_types = orig_op.input_types[:]
graph.unready_inputs_cache[node_def.name] = dict()
graph.unready_control_inputs_cache[node_def.name] = dict()
# get inputs
inputs = []
for inp_idx, inp in enumerate(orig_op.inputs):
name = inp.name \
if ((not graph.clone_dataset_related_ops) and graph.is_dataset_related(inp)) \
else (replica_prefix + inp.name)
if name in graph.tensors:
inputs.append(graph.get_tensor_by_name(name).primitive_obj)
else:
tensor = graph.get_tensor_by_name(inp.name)
if tensor.producer.get_control_flow_context() is not None:
graph.original_context_cache[tensor.producer.name] = tensor.producer.get_control_flow_context()
tensor.producer.set_control_flow_context(None)
inputs.append(tensor.primitive_obj)
graph.unready_inputs_cache[node_def.name][inp_idx] = name
# get control inputs
control_inputs = []
old_control_inputs = list(orig_op.control_inputs)
for c_inp in old_control_inputs:
name = c_inp.name \
if (graph.is_dataset_related(c_inp) and \
(not graph.clone_dataset_related_ops)) \
else (replica_prefix + c_inp.name)
if name in graph.operations:
control_inputs.append(graph.get_operation_by_name(name).primitive_obj)
else:
op = graph.get_operation_by_name(c_inp.name)
if op.get_control_flow_context() is not None:
graph.original_context_cache[op.name] = op.get_control_flow_context()
op.set_control_flow_context(None)
control_inputs.append(op.primitive_obj)
graph.unready_control_inputs_cache[node_def.name][op.name] = name
if not graph.unready_inputs_cache[node_def.name]:
del graph.unready_inputs_cache[node_def.name]
if not graph.unready_control_inputs_cache[node_def.name]:
del graph.unready_control_inputs_cache[node_def.name]
with ModelPhase(orig_op.phase):
with ops.device(device):
graph.current_cloned_taskgraph = orig_op.taskgraph
new_op = ops.Operation(node_def,
ops.get_default_graph(),
inputs,
output_types,
control_inputs,
input_types,
None,
op_def=op_def)
new_op._set_device(device) # pylint: disable=protected-access
def context_def_clone(graph, context_def, is_while_context, replica_idx,
micro_batch_idx):
"""Clone control flow context from context_def
with replica_idx and micto_batch_idx."""
def fetch_prefix(tensor_name):
"""Fetch prefix for tensor in new_context_def."""
op = graph.get_tensor_by_name(tensor_name).producer
if graph.current_model_phase == ModelPhase.MICRO_BATCH_CLONE:
prefix = micro_batch_prefix if graph.need_clone(op) else ""
elif graph.current_model_phase == ModelPhase.REPLICATED:
prefix = replica_prefix if graph.need_clone(op) else ""
with ModelPhase(ModelPhase.MICRO_BATCH_CLONE):
prefix += micro_batch_prefix if graph.need_clone(op) else ""
else:
raise RuntimeError(
"ModelPhase {} is not supported for context def cloning.".format(
graph.current_model_phase))
return prefix
if context_def.context_name.find('global_step') >= 0:
return None
if context_def.context_name not in graph.control_flow_context_map:
graph.control_flow_context_map[context_def.context_name] = {}
else:
if micro_batch_idx in \
graph.control_flow_context_map[context_def.context_name]:
return None
replica_prefix = common.get_replica_prefix(replica_idx)
micro_batch_prefix = common.get_micro_batch_prefix(micro_batch_idx)
prefix = replica_prefix + micro_batch_prefix
new_context_def = control_flow_pb2.WhileContextDef() if is_while_context \
else control_flow_pb2.CondContextDef()
new_context_def.context_name = prefix + context_def.context_name
new_context_def.pivot_name = \
fetch_prefix(context_def.pivot_name) + context_def.pivot_name
if is_while_context:
new_context_def.parallel_iterations = context_def.parallel_iterations
if context_def.maximum_iterations_name:
new_context_def.maximum_iterations_name = fetch_prefix(context_def.maximum_iterations_name) + context_def.maximum_iterations_name
new_context_def.back_prop = context_def.back_prop
new_context_def.swap_memory = context_def.swap_memory
new_context_def.pivot_for_pred_name = fetch_prefix(context_def.pivot_for_pred_name) + context_def.pivot_for_pred_name
new_context_def.pivot_for_body_name = fetch_prefix(context_def.pivot_for_body_name) + context_def.pivot_for_body_name
new_context_def.loop_exit_names.extend(
[fetch_prefix(name) + name for name in context_def.loop_exit_names])
new_context_def.loop_enter_names.extend([(fetch_prefix(name) + name) for name in context_def.loop_enter_names])
else:
new_context_def.pred_name = fetch_prefix(context_def.pred_name) + context_def.pred_name
new_context_def.branch = context_def.branch
values_def = control_flow_pb2.ValuesDef()
values_def.values.extend([(fetch_prefix(name) + name) for name in context_def.values_def.values])
for k, v in list(context_def.values_def.external_values.items()):
values_def.external_values[fetch_prefix(k) + k] = fetch_prefix(v) + v
new_context_def.values_def.MergeFrom(values_def)
for nested in context_def.nested_contexts:
new_nested = new_context_def.nested_contexts.add()
if nested.while_ctxt.context_name:
while_ctxt = context_def_clone(graph, nested.while_ctxt, True, replica_idx, micro_batch_idx)
if while_ctxt is not None:
new_nested.while_ctxt.CopyFrom(while_ctxt)
new_context = control_flow_ops.WhileContext(context_def=while_ctxt)
graph.control_flow_context_map[
nested.while_ctxt.context_name][micro_batch_idx] = new_context
elif nested.cond_ctxt.context_name:
cond_ctxt = context_def_clone(graph, nested.cond_ctxt, False, replica_idx, micro_batch_idx)
if cond_ctxt is not None:
new_nested.cond_ctxt.CopyFrom(cond_ctxt)
new_context = control_flow_ops.CondContext(context_def=cond_ctxt)
graph.control_flow_context_map[
nested.cond_ctxt.context_name][micro_batch_idx] = new_context
else:
raise TypeError(
"Type of {} is not supported for nested control flow context.".
format(nested))
return new_context_def
def function_clone(graph, orig_func, cloned_func_name, target_device):
"""Clone function for a micro batch or a replica."""
with ops.device(target_device):
if Version(__version__) >= Version("1.12.0") and Version(__version__) < Version("1.14.0"):
new_func = _DefinedFunction(func=orig_func.func,
argnames=orig_func.arg_names,
input_types=orig_func.input_types,
func_name=cloned_func_name,
grad_func=orig_func.grad_func,
python_grad_func=orig_func.python_grad_func,
out_names=orig_func.out_names,
shape_func=orig_func.shape_func,
capture_by_value=orig_func.capture_by_value)
elif Version(__version__) < Version("2.0"):
new_func = _DefinedFunction(
func=orig_func.func,
argnames=orig_func.arg_names,
input_types=orig_func.input_types,
func_name=cloned_func_name,
grad_func=orig_func.grad_func,
python_grad_func=orig_func.python_grad_func,
out_names=orig_func.out_names,
shape_func=orig_func.shape_func,
capture_by_value=orig_func.capture_by_value,
whitelisted_stateful_ops=orig_func.whitelisted_stateful_ops,
capture_resource_var_by_value=orig_func.capture_resource_var_by_value)
new_func.add_to_graph(ops.get_default_graph())
new_func = graph.get_function_by_name(cloned_func_name)
new_func.is_dataset_related = orig_func.is_dataset_related
new_func.taskgraph = orig_func.taskgraph
orig_func.taskgraph.add_functions(new_func)
def control_flow_context_clone(graph, replica_idx, num_micro_batch):
"""Clone control flow context for a model replica."""
for micro_batch_idx in range(num_micro_batch):
for context in ops.get_collection(ops.GraphKeys.WHILE_CONTEXT):
context_def = context.to_proto()
new_context_def = context_def_clone(graph, context_def, True, replica_idx, micro_batch_idx)
if new_context_def is None:
continue
new_context = control_flow_ops.WhileContext(context_def=new_context_def)
graph.control_flow_context_map[context_def.context_name][micro_batch_idx] = new_context
for context in ops.get_collection(ops.GraphKeys.COND_CONTEXT):
context_def = context.to_proto()
new_context_def = context_def_clone(graph, context_def, False, replica_idx, micro_batch_idx)
if new_context_def is None:
continue
new_context = control_flow_ops.CondContext(context_def=new_context_def)
graph.control_flow_context_map[context_def.context_name][micro_batch_idx] = new_context
def grad_loop_state_clone_internel(graph, op_list, replica_idx, micro_batch_idx):
"""Clone grad loop state for op_list."""
for op in op_list:
op_context = op.get_control_flow_context()
context_def_list = []
if op_context is None:
continue
grad_state = op_context.grad_state
if grad_state:
context_def_list.append(grad_state.forward_context.to_proto())
context_def_list.append(grad_state.grad_context.to_proto())
out_grad_state = grad_state.outer_grad_state
if out_grad_state:
context_def_list.append(out_grad_state.forward_context.to_proto())
context_def_list.append(out_grad_state.grad_context.to_proto())
for context_def in context_def_list:
new_context_def = context_def_clone(graph, context_def, True, replica_idx, micro_batch_idx)
if new_context_def is not None:
new_context = control_flow_ops.WhileContext(context_def=new_context_def)
graph.control_flow_context_map[context_def.context_name][micro_batch_idx] = new_context
def grad_loop_state_clone(graph, replica_idx, taskgraph, num_micro_batch):
"""Clone grad loop state for a replica or multi micro-batches."""
start_micro_batch_idx = 1 if graph.current_model_phase == ModelPhase.MICRO_BATCH_CLONE else 0
for micro_batch_idx in range(start_micro_batch_idx, num_micro_batch):
op_list = taskgraph.operations.forward_operations(0, 0) + taskgraph.operations.backward_operations(0, 0)
grad_loop_state_clone_internel(graph, op_list, replica_idx, micro_batch_idx)
op_list = taskgraph.operations.apply_operations(0) + taskgraph.operations.save_and_restore_operations(0)
grad_loop_state_clone_internel(graph, op_list, replica_idx, 0)
def add_control_dependency(ops_first, ops_second):
"""Sequence `ops_first` before `ops_second`.
Given two operations, returns a pair of operations with the same behavior
except that the first returned operation will execute before the second
returned operation.
"""
if not isinstance(ops_first, list):
ops_first = [ops_first]
if not isinstance(ops_second, list):
ops_second = [ops_second]
for op in ops_second:
op.add_control_inputs(ops_first)
def create_communicator(name, devices):
"""Create a communicator with parameters from env."""
conf = Env.get().config
return CollectiveCommunicator(
name=name,
devices=devices,
max_splits=conf.communication.max_splits,
num_communicators=conf.communication.num_communicators,
enable_fp16=conf.communication.fp16,
fp16_scale=conf.communication.fp16_scale)
def create_serial_communicator(name, devices, max_splits=None):
"""Create a serial communicator with num_communicators=1.
Using for broadcast variables and so on."""
if max_splits is None:
num_splits = constant.DEFAULT_COM_MAX_SPLITS
else:
num_splits = max_splits
return CollectiveCommunicator(name=name,
devices=devices,
max_splits=num_splits,
num_communicators=1)
def create_simple_communicator(name, devices):
"""Create a simple communicator with max_splits=1.
Using for summarizing loss, acc and so on."""
return CollectiveCommunicator(name=name,
devices=devices,
max_splits=1)
def allreduce_gradients(gradients,
current_device,
all_devices,
comm_index,
mean=False,
name="GRADIENT_REDUCE"):
"""AllReduce gradients for all replicas."""
comm_name = name + "_{}"
with ops.device(current_device):
comm = create_communicator(name=comm_name.format(comm_index),
devices=all_devices)
return comm.batch_allreduce(gradients, mean=mean)
def allreduce_tensors(comm, tensors, current_device, mean=False):
"""AllReduce tensors for all replicas."""
with ops.device(current_device):
return comm.batch_allreduce(tensors, mean=mean)
def allgather_tensors(comm, tensors, current_device, mean=None):
"""Allgather tensors for all replicas."""
if mean:
raise ValueError("Mean in allgather expected None/False, while %s." % mean)
comm_tensors = []
with ops.device(current_device):
for tensor in tensors:
comm_tensors.append(comm.allgather(tensor))
return comm_tensors
def alltoall(comm, tensor, current_device):
"""AlltoAll tensors."""
conf = Env.get().config
if conf.communication.fp16 and tensor.dtype == dtypes.float32:
tensor_float16 = math_ops.cast(tensor, dtypes.float16)
with ops.device(current_device):
result = comm.alltoall(tensor_float16)
return math_ops.cast(result, dtypes.float32)
else:
with ops.device(current_device):
return comm.alltoall(tensor)
def concat_indexed_slices(tensor_list, dense_shape):
"""Concat IndexedSlices."""
values_list, indices_list = list(
zip(*[(t.values, t.indices) for t in tensor_list]))
return ops.IndexedSlices(array_ops.concat(values_list, axis=0),
array_ops.concat(indices_list, axis=0),
dense_shape=dense_shape)
def dispatch_across_consumers(products, consumers, rank, even=False):
"""Dispatch products across all consumers.
Args:
products: number of products to be dispatched.
consumers: number of consumers.
rank: rank of this consumer in all consumers.
even: dispatch across consumers evenly if True.
"""
if even:
assert products % consumers == 0, \
"Number of products expects to be divided by number of consumers" \
". while products = {}, consumers = {}.".format(products, consumers)
remainder = products % consumers
divisor = products // consumers
return (divisor + 1) if rank < remainder \
else divisor
def node_clone_for_amp(orig_op, new_type, new_type_pb, suffix):
"""Clone a operation to 'device' from 'orig_op' for amp."""
# get node def
device = orig_op.device
node_def = copy.deepcopy(orig_op.node_def)
loc_attr = node_def.attr.get('_class')
node_def.name = node_def.name + suffix + '_' + new_type.name
node_def.attr.get('T').type = new_type_pb
if loc_attr:
for idx in range(len(loc_attr.list.s)):
loc_attr.list.s[idx] = \
loc_attr.list.s[idx].replace(
b'@', b'@' + compat.as_bytes(suffix))
for attr_name in ["frame_name", "shared_name"]:
attr_value = node_def.attr.get(attr_name)
if attr_value and attr_value.s:
node_def.attr.get(attr_name).s = attr_value.s + compat.as_bytes(suffix)
op_def = copy.deepcopy(orig_op.op_def)
output_types = [new_type_pb for _ in orig_op.output_types]
inputs = [inp.primitive_obj for inp in orig_op.inputs]
new_inputs = []
input_types = []
for inp in inputs:
if inp.dtype != new_type and inp.dtype in [dtypes.float32, dtypes.float16]:
new_input = math_ops.cast(inp, new_type, name=inp.op.name + suffix + '_cast_' + new_type.name)
new_inputs.append(new_input)
input_types.append(new_type)
else:
new_inputs.append(inp)
input_types.append(inp.dtype)
control_inputs = [t for t in list(orig_op.control_inputs)]
with ModelPhase(orig_op.phase):
with ops.device(device):
new_op = ops.Operation(node_def,
ops.get_default_graph(),
new_inputs,
output_types,
control_inputs,
input_types,
None,
op_def=op_def)
new_op._set_device(device) # pylint: disable=protected-access
return new_op