TensorFlow distribution strategies
Quick answer
TensorFlow distribution strategies coordinate replica execution, variable placement and reduction. MirroredStrategy is intended for synchronous replicas on one machine; MultiWorkerMirroredStrategy extends synchronous data parallelism across workers; parameter-server strategies use different coordination semantics. Examples use small synthetic inputs and Python or local CPU PyTorch. ByteMind execution is unverified.
Compute equal replica batch size
global_batch=12; replicas=3assert global_batch%replicas==0print(global_batch//replicas)Expected output (locally tested): 4. Equal splitting requires a divisible batch in this simple fixture.
How it works
Create model and optimizer variables inside strategy.scope() for the selected strategy. A custom loop typically uses strategy.run(replica_step, args=...) for replica-local work and strategy.reduce for reporting aggregated values. With per-example losses and a known global batch size, tf.nn.compute_average_loss divides each replica loss sum by that global count, and synchronous gradient aggregation sums the contributions. For a short tail, explicitly choose a drop-remainder policy or an actual-count normalization consistent across ranks; a fixed nominal denominator changes the last step scale. Account separately for regularization and framework loss reduction. Keras fit handles many distribution details automatically, but custom loops must specify them. A global batch of 12 on three equal replicas yields four samples each. For two replicas with counts 2 and 3 and loss sums 4 and 9, normalized contributions 4/5 and 9/5 sum to 2.6. Examples simulate these values without TensorFlow; installed runtime and exact APIs must be confirmed before running a real strategy.
Sum normalized loss contributions
from fractions import Fractionlosses=[4,9]; counts=[2,3]contributions=[Fraction(s,sum(counts)) for s in losses]print([str(x) for x in contributions],str(sum(contributions)))Expected output (locally tested): ['4/5', '9/5'] 13/5. The SUM of normalized contributions equals the global sample mean.
Common mistakes
Never average already global-normalized replica contributions a second time. Do not assume every strategy has identical synchronization semantics or that scope alone distributes arbitrary Python code. Avoid conflating per-replica and global batch size.
Practice and next steps
Write down the replica loss contributions for a short batch of five when the nominal global batch is eight. Explain the difference between a fixed denominator and actual count.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
TensorFlow distribution strategies — knowledge check
Apply tensorflow distribution strategies to small examples and debugging scenarios.
Where should distributed model variables be created?
What invokes a replica-local function?
How combine globally normalized contributions?
Which strategy targets synchronous replicas on one machine?
What is the effect of fixed nominal denominator on short tail?