Create a source in a graph based on a Tensor from a different graph. This function creates a placeholder analog of `s` in a graph with the following behavior: 1) If s is a captured Tensor or Variable and handle_captures is set to True, simply capture it in the new graph as well. 2) I
(s, graph, op_map, handle_captures, inverse_captures,
base_graph)
| 143 | |
| 144 | |
| 145 | def _copy_source(s, graph, op_map, handle_captures, inverse_captures, |
| 146 | base_graph): |
| 147 | """Create a source in a graph based on a Tensor from a different graph. |
| 148 | |
| 149 | This function creates a placeholder analog of `s` in a graph with the |
| 150 | following behavior: |
| 151 | |
| 152 | 1) If s is a captured Tensor or Variable and handle_captures is set to True, |
| 153 | simply capture it in the new graph as well. |
| 154 | |
| 155 | 2) If s is a PlaceholderWithDefault whose default is a constant, preserve |
| 156 | said default in the new graph. |
| 157 | |
| 158 | 3) When applicable, copy resource variable metadata from `s` to the newly |
| 159 | created placeholder. |
| 160 | |
| 161 | Args: |
| 162 | s: The source of interest. |
| 163 | graph: The destination graph. |
| 164 | op_map: A dict mapping ops and tensors in the old graph to the new one. |
| 165 | handle_captures: A boolean indicating whether to re-capture s in the new |
| 166 | graph or simply create a vanilla placeholder. |
| 167 | inverse_captures: A dict mapping s back to the Tensor or Variable that it |
| 168 | captures. |
| 169 | base_graph: The graph being copied from. |
| 170 | """ |
| 171 | if handle_captures and s in inverse_captures: |
| 172 | copied_placeholder = graph.capture(inverse_captures[s], name=s.op.name) |
| 173 | elif s.op.type == "PlaceholderWithDefault" and _constant_inputs(s): |
| 174 | # Copy the default value to the graph. |
| 175 | default_value = s.op.inputs[0] |
| 176 | unavailable_inputs, unavailable_control_inputs = _copy_non_source( |
| 177 | op=default_value.op, graph=graph, op_map=op_map, |
| 178 | base_graph=base_graph) |
| 179 | if unavailable_inputs or unavailable_control_inputs: |
| 180 | raise AssertionError( |
| 181 | "Could not copy source node {} because it has inputs." |
| 182 | .format(default_value)) |
| 183 | |
| 184 | with ops.device(s.op.device): |
| 185 | copied_placeholder = array_ops.placeholder_with_default( |
| 186 | input=op_map[default_value], shape=s.shape, name=s.op.name) |
| 187 | else: |
| 188 | with ops.device(s.op.device): |
| 189 | copied_placeholder = array_ops.placeholder( |
| 190 | dtype=s.dtype, shape=s.shape, name=s.op.name) |
| 191 | |
| 192 | base_handle = resource_variable_ops.get_resource_handle_data(s) |
| 193 | if base_handle.shape_and_type: |
| 194 | resource_variable_ops._set_handle_shapes_and_types( # pylint: disable=protected-access |
| 195 | copied_placeholder, |
| 196 | base_handle, |
| 197 | graph_mode=True) |
| 198 | |
| 199 | op_map[s] = copied_placeholder |
| 200 | # Add an entry for the op of the source tensor so that if there are any nodes |
| 201 | # depending on that op via control dependencies it can work correctly. |
| 202 | op_map[s.op] = copied_placeholder.op |
no test coverage detected