A variable creation hook which adds Trackable dependencies. Set for example during a `Template`'s first wrapped function execution. Ensures that (a) `template` depends on any trackable objects using their own `capture_dependencies` scope inside this scope which create variables, and
(next_creator,
name,
initial_value,
trackable_parent=None,
**kwargs)
| 537 | name_prefix = template.variable_scope.name |
| 538 | |
| 539 | def _trackable_custom_creator(next_creator, |
| 540 | name, |
| 541 | initial_value, |
| 542 | trackable_parent=None, |
| 543 | **kwargs): |
| 544 | """A variable creation hook which adds Trackable dependencies. |
| 545 | |
| 546 | Set for example during a `Template`'s first wrapped function |
| 547 | execution. Ensures that (a) `template` depends on any trackable |
| 548 | objects using their own `capture_dependencies` scope inside this scope which |
| 549 | create variables, and (b) that any variables not in a more deeply nested |
| 550 | scope are added as dependencies directly. |
| 551 | |
| 552 | The `trackable_parent` argument is passed between custom creators but |
| 553 | ignored when the variable object itself is created. This argument indicates |
| 554 | (if not `None`) that a more deeply nested scope has already added the |
| 555 | variable as a dependency, and that parent scopes should add a dependency on |
| 556 | that object rather than on the variable directly. |
| 557 | |
| 558 | Args: |
| 559 | next_creator: See `variable_scope.variable_creator_scope`; the next |
| 560 | creator in the chain. |
| 561 | name: The (full, scope-influenced) name of the variable. The `name_prefix` |
| 562 | itself is stripped for the purposes of object-based dependency tracking, |
| 563 | but scopes opened within this scope are respected. |
| 564 | initial_value: See `variable_scope.variable_creator_scope`. Taken |
| 565 | explicitly so the argument can be re-named and used with |
| 566 | `Trackable._add_variable_with_custom_getter`. |
| 567 | trackable_parent: If not None, a more deeply nested trackable object and |
| 568 | its name prefix which were passed to `capture_dependencies` to add a |
| 569 | dependency on (rather than depending on the variable directly). |
| 570 | **kwargs: Passed through to the next creator. |
| 571 | |
| 572 | Returns: |
| 573 | The output of `next_creator`: the fetched/created variable object. |
| 574 | """ |
| 575 | |
| 576 | def _call_next_creator_renaming_initializer(initializer, **inner_kwargs): |
| 577 | inner_kwargs.pop("name") # Ignored; this is the scope-stripped name which |
| 578 | # we don't want to propagate. |
| 579 | return next_creator(initial_value=initializer, name=name, **inner_kwargs) |
| 580 | |
| 581 | if name is not None and name.startswith(name_prefix): |
| 582 | scope_stripped_name = name[len(name_prefix) + 1:] |
| 583 | if not trackable_parent: |
| 584 | return template._add_variable_with_custom_getter( # pylint: disable=protected-access |
| 585 | initializer=initial_value, |
| 586 | name=scope_stripped_name, |
| 587 | getter=_call_next_creator_renaming_initializer, |
| 588 | # Disable error checking for Trackable. Exceptions are instead |
| 589 | # raised if necessary when the object-based saver tries to |
| 590 | # save/restore the object. |
| 591 | overwrite=True, |
| 592 | trackable_parent=(template, name_prefix), |
| 593 | **kwargs) |
| 594 | else: |
| 595 | parent_object, parent_name_prefix = trackable_parent |
| 596 | template._track_trackable( # pylint: disable=protected-access |
nothing calls this directly
no test coverage detected