1# Copyright 2021 The TensorFlow Authors. All Rights Reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================== 15"""A module for interm merge-call related internal APIs.""" 16from tensorflow.python.distribute import distribution_strategy_context 17from tensorflow.python.util.tf_export import tf_export 18 19 20@tf_export("__internal__.distribute.strategy_supports_no_merge_call", v1=[]) 21def strategy_supports_no_merge_call(): 22 """Returns if the current `Strategy` can operate in pure replica context.""" 23 if not distribution_strategy_context.has_strategy(): 24 return True 25 strategy = distribution_strategy_context.get_strategy() 26 return not strategy.extended._use_merge_call() # pylint: disable=protected-access 27 28 29@tf_export("__internal__.distribute.interim.maybe_merge_call", v1=[]) 30def maybe_merge_call(fn, strategy, *args, **kwargs): 31 """Maybe invoke `fn` via `merge_call` which may or may not be fulfilled. 32 33 The caller of this utility function requests to invoke `fn` via `merge_call` 34 at `tf.distribute.Strategy`'s best efforts. It is `tf.distribute`'s internal 35 whether the request is honored, depending on the `Strategy`. See 36 `tf.distribute.ReplicaContext.merge_call()` for more information. 37 38 This is an interim API which is subject to removal and does not guarantee 39 backward-compatibility. 40 41 Args: 42 fn: the function to be invoked. 43 strategy: the `tf.distribute.Strategy` to call `fn` with. 44 *args: the positional arguments to be passed in to `fn`. 45 **kwargs: the keyword arguments to be passed in to `fn`. 46 47 Returns: 48 The return value of the `fn` call. 49 """ 50 if strategy_supports_no_merge_call(): 51 return fn(strategy, *args, **kwargs) 52 else: 53 return distribution_strategy_context.get_replica_context().merge_call( 54 fn, args=args, kwargs=kwargs) 55