tf.distribute.get_replica_context
Stay organized with collections
Save and categorize content based on your preferences.
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
ReplicaContext
object);
- switches to cross-replica context (in which case this will return
None
) when entering a with tf.distribute.Strategy.scope():
block;
- switches to a (non-default) replica context inside
strategy.run(fn, ...)
;
- if
fn
calls get_replica_context().merge_call(merge_fn, ...)
, then
inside merge_fn
you are back in the cross-replica context (and again
this function will return None
).
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.ReplicaContext object when in a replica context
scope, else None .
Within a particular block, exactly one of these two things will be true:
get_replica_context() returns non-None , or
tf.distribute.is_cross_replica_context() returns True.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2023-10-06 UTC.
[{
"type": "thumb-down",
"id": "missingTheInformationINeed",
"label":"Missing the information I need"
},{
"type": "thumb-down",
"id": "tooComplicatedTooManySteps",
"label":"Too complicated / too many steps"
},{
"type": "thumb-down",
"id": "outOfDate",
"label":"Out of date"
},{
"type": "thumb-down",
"id": "samplesCodeIssue",
"label":"Samples / code issue"
},{
"type": "thumb-down",
"id": "otherDown",
"label":"Other"
}]
[{
"type": "thumb-up",
"id": "easyToUnderstand",
"label":"Easy to understand"
},{
"type": "thumb-up",
"id": "solvedMyProblem",
"label":"Solved my problem"
},{
"type": "thumb-up",
"id": "otherUp",
"label":"Other"
}]
{"lastModified": "Last updated 2023-10-06 UTC."}
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2023-10-06 UTC."],[],[]]