Initializes an eager defined function. Args: name: str, the name for the created function. graph: Graph, the graph containing the operations in the function inputs: the tensors in the graph to be used as inputs to the function outputs: the tensors in the graph which will
(self, name, graph, inputs, outputs, attrs)
| 379 | """ |
| 380 | |
| 381 | def __init__(self, name, graph, inputs, outputs, attrs): |
| 382 | """Initializes an eager defined function. |
| 383 | |
| 384 | Args: |
| 385 | name: str, the name for the created function. |
| 386 | graph: Graph, the graph containing the operations in the function |
| 387 | inputs: the tensors in the graph to be used as inputs to the function |
| 388 | outputs: the tensors in the graph which will be outputs to the function |
| 389 | attrs: dict mapping names of attributes to their AttrValue values |
| 390 | """ |
| 391 | input_ops = set(arg.op for arg in inputs) |
| 392 | operations = [op for op in graph.get_operations() if op not in input_ops] |
| 393 | |
| 394 | graph_output_names = graph._output_names # pylint: disable=protected-access |
| 395 | if (graph_output_names is not None and |
| 396 | all(ops.tensor_id(t) in graph_output_names for t in outputs)): |
| 397 | output_names = [ |
| 398 | compat.as_bytes(graph_output_names[ops.tensor_id(t)]) for t in outputs |
| 399 | ] |
| 400 | if len(set(output_names)) != len(output_names): |
| 401 | # There are duplicate names for some reason, probably an invalid |
| 402 | # signature. Revert to auto-naming. |
| 403 | output_names = [] |
| 404 | else: |
| 405 | output_names = [] |
| 406 | fn = pywrap_tensorflow.TF_GraphToFunction_wrapper( |
| 407 | graph._c_graph, # pylint: disable=protected-access |
| 408 | compat.as_str(name), |
| 409 | False, |
| 410 | [o._c_op for o in operations], # pylint: disable=protected-access |
| 411 | [t._as_tf_output() for t in inputs], # pylint: disable=protected-access |
| 412 | [t._as_tf_output() for t in outputs], # pylint: disable=protected-access |
| 413 | output_names, |
| 414 | [o._c_op for o in graph.control_outputs], # pylint: disable=protected-access |
| 415 | [], # control_output_names |
| 416 | None, |
| 417 | compat.as_str("")) |
| 418 | |
| 419 | for name, attr_value in attrs.items(): |
| 420 | serialized = attr_value.SerializeToString() |
| 421 | # TODO(iga): this creates and deletes a new TF_Status for every attr. |
| 422 | # It might be worth creating a convenient way to re-use status. |
| 423 | pywrap_tensorflow.TF_FunctionSetAttrValueProto( |
| 424 | fn, compat.as_str(name), serialized) |
| 425 | |
| 426 | # TODO(apassos) avoid creating a FunctionDef (specially to grab the |
| 427 | # signature, but also in general it's nice not to depend on it. |
| 428 | with c_api_util.tf_buffer() as buffer_: |
| 429 | pywrap_tensorflow.TF_FunctionToFunctionDef(fn, buffer_) |
| 430 | proto_data = pywrap_tensorflow.TF_GetBuffer(buffer_) |
| 431 | function_def = function_pb2.FunctionDef() |
| 432 | function_def.ParseFromString(compat.as_bytes(proto_data)) |
| 433 | self.name = compat.as_bytes(function_def.signature.name) |
| 434 | with ops.init_scope(): |
| 435 | if context.executing_eagerly(): |
| 436 | context.ensure_initialized() |
| 437 | context.add_function(fn) |
| 438 | self._function_deleter = _EagerDefinedFunctionDeleter(self.name) |
nothing calls this directly
no test coverage detected