Walk a Graph and capture the subgraph between init_tensor and sources. Note: This function mutates visited_ops and op_outputs. Arguments: init_tensor: A Tensor or Operation where the subgraph terminates. sources: A set of Tensors where subgraph extraction should stop. disallowed_
(init_tensor, sources, disallowed_placeholders, visited_ops,
op_outputs, add_sources)
| 366 | # TODO(jmenick) - there is considerable duplication of functionality between |
| 367 | # this function and get_backward_walk_ops(). Need to deduplicate. |
| 368 | def map_subgraph(init_tensor, sources, disallowed_placeholders, visited_ops, |
| 369 | op_outputs, add_sources): |
| 370 | """Walk a Graph and capture the subgraph between init_tensor and sources. |
| 371 | |
| 372 | Note: This function mutates visited_ops and op_outputs. |
| 373 | |
| 374 | Arguments: |
| 375 | init_tensor: A Tensor or Operation where the subgraph terminates. |
| 376 | sources: A set of Tensors where subgraph extraction should stop. |
| 377 | disallowed_placeholders: An optional set of ops which may not appear in the |
| 378 | lifted graph. Defaults to all placeholders. |
| 379 | visited_ops: A set of operations which were visited in a prior pass. |
| 380 | op_outputs: A defaultdict containing the outputs of an op which are to be |
| 381 | copied into the new subgraph. |
| 382 | add_sources: A boolean indicating whether placeholders which are not in |
| 383 | sources should be allowed. |
| 384 | |
| 385 | Returns: |
| 386 | The set of placeholders upon which init_tensor depends and are not in |
| 387 | sources. |
| 388 | |
| 389 | Raises: |
| 390 | UnliftableError: if init_tensor depends on a placeholder which is not in |
| 391 | sources and add_sources is False. |
| 392 | """ |
| 393 | ops_to_visit = [_as_operation(init_tensor)] |
| 394 | extra_sources = object_identity.ObjectIdentitySet() |
| 395 | while ops_to_visit: |
| 396 | op = ops_to_visit.pop() |
| 397 | if op in visited_ops: |
| 398 | continue |
| 399 | visited_ops.add(op) |
| 400 | |
| 401 | should_raise = False |
| 402 | if disallowed_placeholders is not None and op in disallowed_placeholders: |
| 403 | should_raise = True |
| 404 | elif op.type == "Placeholder": |
| 405 | if disallowed_placeholders is None and not add_sources: |
| 406 | should_raise = True |
| 407 | extra_sources.update(op.outputs) |
| 408 | |
| 409 | if should_raise: |
| 410 | raise UnliftableError( |
| 411 | "Unable to lift tensor %s because it depends transitively on " |
| 412 | "placeholder %s via at least one path, e.g.: %s" |
| 413 | % (repr(init_tensor), repr(op), _path_from(op, init_tensor, sources))) |
| 414 | for inp in graph_inputs(op): |
| 415 | op_outputs[inp].add(op) |
| 416 | if inp not in visited_ops and inp not in (sources or extra_sources): |
| 417 | ops_to_visit.append(inp) |
| 418 | |
| 419 | return extra_sources |
nothing calls this directly
no test coverage detected