tf.distribute.get_replica_context
| View source on GitHub | 
Returns the current tf.distribute.ReplicaContext or None.
tf.distribute.get_replica_context()
Returns None if in a cross-replica context.
Note that execution:
- starts in the default (single-replica) replica context (this function will return the default ReplicaContextobject);
- switches to cross-replica context (in which case this will return None) when entering awith tf.distribute.Strategy.scope():block;
- switches to a (non-default) replica context inside strategy.run(fn, ...);
- if fncallsget_replica_context().merge_call(merge_fn, ...), then insidemerge_fnyou are back in the cross-replica context (and again this function will returnNone).
Most tf.distribute.Strategy methods may only be executed in a cross-replica context, in a replica context you should use the API of the tf.distribute.ReplicaContext object returned by this method instead.
assert tf.distribute.get_replica_context() is not None  # default
with strategy.scope():
  assert tf.distribute.get_replica_context() is None
  def f():
    replica_context = tf.distribute.get_replica_context()  # for strategy
    assert replica_context is not None
    tf.print("Replica id: ", replica_context.replica_id_in_sync_group,
             " of ", replica_context.num_replicas_in_sync)
  strategy.run(f)
  
| Returns | |
|---|---|
| The current tf.distribute.ReplicaContextobject when in a replica context scope, elseNone.Within a particular block, exactly one of these two things will be true: 
 | 
    © 2020 The TensorFlow Authors. All rights reserved.
Licensed under the Creative Commons Attribution License 3.0.
Code samples licensed under the Apache 2.0 License.
    https://www.tensorflow.org/versions/r2.4/api_docs/python/tf/distribute/get_replica_context