Extract a subgraph of this function's underlying graph. Wraps the subgraph in a new `WrappedFunction` object. Args: feeds: Input tensors to the subgraph to extract, as `Tensor` objects. fetches: Possibly-nested Python data structure containing information about outputs
(self, feeds, fetches, name=None, input_signature=None)
| 229 | fn_graph, attrs=attrs, signature=signature) |
| 230 | |
| 231 | def prune(self, feeds, fetches, name=None, input_signature=None): |
| 232 | """Extract a subgraph of this function's underlying graph. |
| 233 | |
| 234 | Wraps the subgraph in a new `WrappedFunction` object. |
| 235 | |
| 236 | Args: |
| 237 | feeds: Input tensors to the subgraph to extract, as `Tensor` objects. |
| 238 | fetches: Possibly-nested Python data structure containing information |
| 239 | about outputs of the target subgraph. Each entry can either be a |
| 240 | `Tensor` object (for data outputs), an `Operation` object (for control |
| 241 | outputs), or a `TensorInfo` proto. Any additional shape/dtype |
| 242 | information provided in a `TensorInfo` and not present in the original |
| 243 | graph will be added to the returned subgraph. |
| 244 | name: (optional) Name to give to the underlying `FuncGraph` of the |
| 245 | returned object. If no name is provided, the graph's name will be |
| 246 | `"pruned"`. |
| 247 | input_signature: (optional) possibly-nested Python data structure |
| 248 | containing `TensorSpec` objects, with which to populate the returned |
| 249 | functions's `FuncGraph`'s `structured_input_signature` field. |
| 250 | |
| 251 | Returns: |
| 252 | A new `WrappedFunction` object containing a copy of the portion of this |
| 253 | object's graph that goes from `feeds` to `fetches`. |
| 254 | """ |
| 255 | # TODO(b/129646028): Add support for CompositeTensors. |
| 256 | name = name or "pruned" |
| 257 | flat_feeds = nest.flatten(feeds, expand_composites=True) |
| 258 | flat_feeds = [self.graph.as_graph_element(t) for t in flat_feeds] |
| 259 | for f in flat_feeds: |
| 260 | if not isinstance(f, ops.Tensor): |
| 261 | raise ValueError("Feeds must be tensors.") |
| 262 | |
| 263 | # Ignoring all feeds that are captures allows prune to be called |
| 264 | # using wrapped_func.inputs even when it uses variables |
| 265 | internal_captures = object_identity.ObjectIdentitySet( |
| 266 | self.graph.internal_captures) |
| 267 | flat_feeds = [f for f in flat_feeds if f not in internal_captures] |
| 268 | |
| 269 | operation_fetches = [] |
| 270 | tensor_fetches = [] |
| 271 | tensor_infos = [] |
| 272 | |
| 273 | def _fetch_preprocesing_callback(fetch): |
| 274 | """Extract out lists of ops, tensors, and tensor type info. |
| 275 | |
| 276 | Turns TensorInfos into Tensors in the original `fetches` structure. |
| 277 | Also extracts ops from `fetches`. |
| 278 | |
| 279 | Args: |
| 280 | fetch: The fetch to preprocess: Tensor, TensorInfo, or Operation, or |
| 281 | string identifying a Tensor or Operation. |
| 282 | |
| 283 | Returns: |
| 284 | `fetch` converted to a Tensor. |
| 285 | """ |
| 286 | if isinstance(fetch, ops.Operation): |
| 287 | operation_fetches.append(fetch) |
| 288 | return fetch |