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

Method prune

tensorflow/python/eager/wrap_function.py:231–361  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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

Callers 15

testImportedHashTableMethod · 0.80
testPruneMethod · 0.80
testPruneRaggedMethod · 0.80
testVariableLiftingMethod · 0.80
testPruneCapturesMethod · 0.80
testGradientsOfPruneMethod · 0.80
testPruneOperationsMethod · 0.80
_wrap_functionMethod · 0.80
function_from_graph_defFunction · 0.80
restore_variablesMethod · 0.80

Calls 10

add_captureMethod · 0.95
WrappedFunctionClass · 0.85
as_graph_elementMethod · 0.80
is_tensorMethod · 0.80
flattenMethod · 0.45
as_defaultMethod · 0.45
extendMethod · 0.45
set_shapeMethod · 0.45
valuesMethod · 0.45
_add_functionMethod · 0.45

Tested by 11

testImportedHashTableMethod · 0.64
testPruneMethod · 0.64
testPruneRaggedMethod · 0.64
testVariableLiftingMethod · 0.64
testPruneCapturesMethod · 0.64
testGradientsOfPruneMethod · 0.64
testPruneOperationsMethod · 0.64
test_custom_pruningMethod · 0.64
test_cond_pruneMethod · 0.64
test_to_protoMethod · 0.64