MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / copy_op_handler

Function copy_op_handler

tensorflow/contrib/graph_editor/transform.py:131–191  ·  view source on GitHub ↗

Copy a `tf.Operation`. Args: info: Transform._TmpInfo instance. op: the `tf.Operation` to be copied. new_inputs: The new inputs for this op. copy_shape: also copy the shape of the tensor nodedef_fn: If provided, a function that will be run on the NodeDef and should retur

(info, op, new_inputs, copy_shape=False, nodedef_fn=None)

Source from the content-addressed store, hash-verified

129
130
131def copy_op_handler(info, op, new_inputs, copy_shape=False, nodedef_fn=None):
132 """Copy a `tf.Operation`.
133
134 Args:
135 info: Transform._TmpInfo instance.
136 op: the `tf.Operation` to be copied.
137 new_inputs: The new inputs for this op.
138 copy_shape: also copy the shape of the tensor
139 nodedef_fn: If provided, a function that will be run on the NodeDef
140 and should return a mutated NodeDef before a new Operation is created.
141 This is useful as certain features cannot be set on the Operation and
142 must be modified in NodeDef.
143
144 Returns:
145 A `(op, op_outputs)` tuple containing the transformed op and its outputs.
146 """
147 # The `new_inputs` was added to this function. For compatibility reason,
148 # let's raise an error if `new_inputs` is a boolean.
149 if isinstance(new_inputs, bool):
150 raise TypeError("the `new_inputs` argument must be an iterable.")
151
152 # pylint: disable=protected-access
153
154 # Clone the node def:
155 node_def_ = deepcopy(op.node_def)
156
157 # Transform name:
158 name_ = info.new_name(op.name)
159 name_ = info.graph_.unique_name(name_)
160 node_def_.name = name_
161
162 # Mutate NodeDef if requested:
163 if nodedef_fn is not None:
164 node_def_ = nodedef_fn(node_def_)
165
166 # Copy the other inputs needed for initialization
167 output_types_ = op._output_types[:]
168 input_types_ = op._input_types[:]
169
170 # Make a copy of the op_def too.
171 # Its unique to every _type_ of Operation.
172 op_def_ = deepcopy(op.op_def)
173
174 # Initialize a new Operation instance
175 op_ = tf_ops.Operation(node_def_, info.graph_, new_inputs, output_types_,
176 [], input_types_, None, op_def_)
177
178 # copy the shape over
179 if copy_shape:
180 for t, t_ in zip(op.outputs, op_.outputs):
181 t_.set_shape(t.get_shape())
182
183 # Original op cannot be finalised here yet. Because some ops require this
184 # attribute to exist, we will create a dummy original_op first and then
185 # later finalise it with the actual original_op when all the ops have
186 # been copied.
187 # TODO(fkp): Stop worrying about _original_op and remove this code?
188 if op._original_op:

Callers

nothing calls this directly

Calls 5

unique_nameMethod · 0.80
new_nameMethod · 0.45
OperationMethod · 0.45
set_shapeMethod · 0.45
get_shapeMethod · 0.45

Tested by

no test coverage detected