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

Class WrappedFunction

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

Wraps a tf V1 piece of code in a function.

Source from the content-addressed store, hash-verified

216
217# TODO(allenl): make this trackable
218class WrappedFunction(function.ConcreteFunction):
219 """Wraps a tf V1 piece of code in a function."""
220
221 def __init__(self, fn_graph, variable_holder, attrs=None, signature=None):
222 self._variable_holder = variable_holder
223 _lift_unlifted_variables(fn_graph, variable_holder)
224 # We call __init__ after lifting variables so that the function's signature
225 # properly reflects the new captured inputs.
226 for f in fn_graph.as_graph_def().library.function:
227 context.context().add_function_def(f)
228 super(WrappedFunction, self).__init__(
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

Callers 3

pruneMethod · 0.85
__init__Method · 0.85
wrap_functionFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected