1# Copyright 2017 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"""Contains utility functions used by summary ops in distribution strategy.""" 16 17 18from tensorflow.python.distribute import distribution_strategy_context 19from tensorflow.python.framework import ops 20from tensorflow.python.framework import tensor_util 21 22 23def skip_summary(): 24 """Determines if summary should be skipped. 25 26 If using multiple replicas in distributed strategy, skip summaries on all 27 replicas except the first one (replica_id=0). 28 29 Returns: 30 True if the summary is skipped; False otherwise. 31 """ 32 33 # TODO(priyag): Add a new optional argument that will provide multiple 34 # alternatives to override default behavior. (e.g. run on last replica, 35 # compute sum or mean across replicas). 36 replica_context = distribution_strategy_context.get_replica_context() 37 if not replica_context: 38 return False 39 # TODO(b/118385803): when replica_id of _TPUReplicaContext is properly 40 # initialized, remember to change here as well. 41 replica_id = replica_context.replica_id_in_sync_group 42 if isinstance(replica_id, ops.Tensor): 43 replica_id = tensor_util.constant_value(replica_id) 44 return replica_id and replica_id > 0 45