TensorTonic
Study Plans
Study Plan
Synchronize Data-Parallel Gradients
Distributed ComputingOptimization
Medium

Implement one synchronous data-parallel SGD step over replicated parameters. Let RRR be the rank count, rrr index ranks, jjj index parameters, gr,jg_{r,j}gr,j​ be a local gradient, θj\theta_jθj​ be the shared original parameter, and η\etaη be learning_rate. A missing gradient contributes zero. The mean gradient gˉj\bar g_jgˉ​j​ is:

gˉj=1R∑r=0R−1gr,j.\bar g_j=\frac{1}{R}\sum_{r=0}^{R-1}g_{r,j}.gˉ​j​=R1​r=0∑R−1​gr,j​.

The updated parameter θj′\theta'_jθj′​ is:

θj′=θj−ηgˉj.\theta'_j=\theta_j-\eta\bar g_j.θj′​=θj​−ηgˉ​j​.

Mean gradients are stored in each parameter's dtype before use in the float32 update. Return a Python dictionary with mean_gradients, a list of one PyTorch tensor per parameter, and updated_replicas, a rank-ordered list of parameter-tensor lists. Returned tensors preserve the corresponding parameter's shape, dtype, and device; updated replicas are independent copies.

Loading visualization...

Examples

Input: parameter_replicas = [[[1,2]],[[1,2]]], gradient_replicas = [[[2,4]],[[4,6]]], learning_rate = 0.1

Output: {"mean_gradients":[[3,5]],"updated_replicas":[[[0.7,1.5]],[[0.7,1.5]]]}

Explanation: The local gradients average to [3,5], and the same out-of-place SGD update is copied to both ranks.

Input: parameter_replicas = [[[1, -1]], [[1, -1]]], gradient_replicas = [[[2, 4]], [[0, 0]]], learning_rate = 0.5

Output: {"mean_gradients": [[1, 2]], "updated_replicas": [[[0.5, -2]], [[0.5, -2]]]}

Input: parameter_replicas and gradient_replicas each contain 3 ranks with tensor shapes (2,) and (2, 2), learning_rate = 0.25

Output: {"mean_gradients": [[1, 1], [[2, 2], [2, 2]]], "updated_replicas": 3 parameter lists, each containing tensors of shapes (2,) and (2, 2)}

Hint 1

torch.zeros_like can create an accumulator with a chosen dtype.

Hint 2

Tensor.to converts a tensor to the required storage dtype.

Hint 3

Tensor.clone creates independent tensor storage.

Requirements

  • Use PyTorch with float32 gradient accumulation and update arithmetic.
  • Include every rank in the mean, including ranks with a missing gradient.
  • Preserve all input tensors and return independent updated replicas.

Constraints

  • parameter_replicas and gradient_replicas are nonempty rank-ordered Python lists containing matching parameter lists.
  • Every rank contains at least one PyTorch parameter tensor; corresponding replicas have identical values, shapes, dtypes, and devices.
  • A gradient is None or a tensor matching its parameter's shape, dtype, and device.
  • Tensors contain finite float32 or bfloat16 values and may be noncontiguous.
  • learning_rate is a finite, nonnegative Python number.
Try Similar Problems
Simulate Distributed CollectivesMediumReconstruct a Column-Parallel MLP LayerMediumCalculate Collective Effective BandwidthEasyGenerate a Warmup-Stable-Decay ScheduleMediumMeasure Pipeline Bubble UtilizationMediumRecover an IsoFLOP Compute-Optimal FrontierHardOrthogonalize a Muon Momentum UpdateEasyMixed-Precision Training Memory AccountantEasyNesterov MomentumMediumBatch vs Stochastic vs Mini-Batch GDMediumBradley-Terry Reward Model LossMediumRLOO (REINFORCE Leave-One-Out)Medium

Accepts: array

Accepts: array

Accepts: number

You must run your code first.

Synchronize Data-Parallel Gradients

Distributed ComputingOptimization
Medium

Implement one synchronous data-parallel SGD step over replicated parameters. Let RRR be the rank count, rrr index ranks, jjj index parameters, gr,jg_{r,j}gr,j​ be a local gradient, θj\theta_jθj​ be the shared original parameter, and η\etaη be learning_rate. A missing gradient contributes zero. The mean gradient gˉj\bar g_jgˉ​j​ is:

gˉj=1R∑r=0R−1gr,j.\bar g_j=\frac{1}{R}\sum_{r=0}^{R-1}g_{r,j}.gˉ​j​=R1​r=0∑R−1​gr,j​.

The updated parameter θj′\theta'_jθj′​ is:

θj′=θj−ηgˉj.\theta'_j=\theta_j-\eta\bar g_j.θj′​=θj​−ηgˉ​j​.

Mean gradients are stored in each parameter's dtype before use in the float32 update. Return a Python dictionary with mean_gradients, a list of one PyTorch tensor per parameter, and updated_replicas, a rank-ordered list of parameter-tensor lists. Returned tensors preserve the corresponding parameter's shape, dtype, and device; updated replicas are independent copies.

Loading visualization...

Examples

Input: parameter_replicas = [[[1,2]],[[1,2]]], gradient_replicas = [[[2,4]],[[4,6]]], learning_rate = 0.1

Output: {"mean_gradients":[[3,5]],"updated_replicas":[[[0.7,1.5]],[[0.7,1.5]]]}

Explanation: The local gradients average to [3,5], and the same out-of-place SGD update is copied to both ranks.

Input: parameter_replicas = [[[1, -1]], [[1, -1]]], gradient_replicas = [[[2, 4]], [[0, 0]]], learning_rate = 0.5

Output: {"mean_gradients": [[1, 2]], "updated_replicas": [[[0.5, -2]], [[0.5, -2]]]}

Input: parameter_replicas and gradient_replicas each contain 3 ranks with tensor shapes (2,) and (2, 2), learning_rate = 0.25

Output: {"mean_gradients": [[1, 1], [[2, 2], [2, 2]]], "updated_replicas": 3 parameter lists, each containing tensors of shapes (2,) and (2, 2)}

Hint 1

torch.zeros_like can create an accumulator with a chosen dtype.

Hint 2

Tensor.to converts a tensor to the required storage dtype.

Hint 3

Tensor.clone creates independent tensor storage.

Requirements

  • Use PyTorch with float32 gradient accumulation and update arithmetic.
  • Include every rank in the mean, including ranks with a missing gradient.
  • Preserve all input tensors and return independent updated replicas.

Constraints

  • parameter_replicas and gradient_replicas are nonempty rank-ordered Python lists containing matching parameter lists.
  • Every rank contains at least one PyTorch parameter tensor; corresponding replicas have identical values, shapes, dtypes, and devices.
  • A gradient is None or a tensor matching its parameter's shape, dtype, and device.
  • Tensors contain finite float32 or bfloat16 values and may be noncontiguous.
  • learning_rate is a finite, nonnegative Python number.
Try Similar Problems
Simulate Distributed CollectivesMediumReconstruct a Column-Parallel MLP LayerMediumCalculate Collective Effective BandwidthEasyGenerate a Warmup-Stable-Decay ScheduleMediumMeasure Pipeline Bubble UtilizationMediumRecover an IsoFLOP Compute-Optimal FrontierHardOrthogonalize a Muon Momentum UpdateEasyMixed-Precision Training Memory AccountantEasyNesterov MomentumMediumBatch vs Stochastic vs Mini-Batch GDMediumBradley-Terry Reward Model LossMediumRLOO (REINFORCE Leave-One-Out)Medium

Accepts: array

Accepts: array

Accepts: number

You must run your code first.