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