(self, *args, **kwargs)
| 989 | # update_non_slot() function (like OptimizerV2._finish), which can |
| 990 | # update several non-slot variables in one call. |
| 991 | def _assign_func(self, *args, **kwargs): |
| 992 | with _enter_or_assert_strategy(self._distribute_strategy): |
| 993 | f = kwargs.pop("f") |
| 994 | if distribution_strategy_context.in_cross_replica_context(): |
| 995 | update_device = distribute_lib.get_update_device() |
| 996 | if update_device is not None: |
| 997 | # We are calling an assign function on the mirrored variable in an |
| 998 | # update context. |
| 999 | v = self.get(device=update_device) |
| 1000 | return f(v, *args, **kwargs) |
| 1001 | |
| 1002 | # We are calling assign on the mirrored variable in cross replica |
| 1003 | # context, use `strategy.extended.update()` to update the variable. |
| 1004 | return self._distribute_strategy.extended.update( |
| 1005 | self, f, args=args, kwargs=kwargs) |
| 1006 | else: |
| 1007 | _assert_replica_context(self._distribute_strategy) |
| 1008 | # We are calling an assign function on the mirrored variable in replica |
| 1009 | # context. |
| 1010 | # We reduce the value we want to assign/add/sub. More details about how |
| 1011 | # we handle the different use cases can be found in the _reduce method. |
| 1012 | # We call the function on each of the mirrored variables with the |
| 1013 | # reduced value. |
| 1014 | if self._aggregation == vs.VariableAggregation.NONE: |
| 1015 | raise ValueError(_aggregation_error_msg.format( |
| 1016 | variable_type="MirroredVariable")) |
| 1017 | |
| 1018 | def merge_fn(strategy, value, *other_args, **other_kwargs): |
| 1019 | v = _apply_aggregation(strategy, value, self._aggregation, self) |
| 1020 | return strategy.extended.update( |
| 1021 | self, f, args=(v,) + other_args, kwargs=other_kwargs) |
| 1022 | |
| 1023 | return distribution_strategy_context.get_replica_context().merge_call( |
| 1024 | merge_fn, args=args, kwargs=kwargs) |
| 1025 | |
| 1026 | def assign_sub(self, *args, **kwargs): |
| 1027 | assign_sub_fn = lambda var, *a, **kw: var.assign_sub(*a, **kw) |
no test coverage detected