Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorflow/python/distribute/merge_call_interim.py: 50%
14 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
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 distribute_lib
17from tensorflow.python.util.tf_export import tf_export
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 distribute_lib.has_strategy():
24 return True
25 strategy = distribute_lib.get_strategy()
26 return not strategy.extended._use_merge_call() # pylint: disable=protected-access
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.
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.
38 This is an interim API which is subject to removal and does not guarantee
39 backward-compatibility.
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`.
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 distribute_lib.get_replica_context().merge_call(
54 fn, args=args, kwargs=kwargs)