TensorTonic
Study Plans
Study Plan
Gradient Accumulation Equivalence
Language Models
Easy

Perform one SGD update using gradients accumulated across microbatches of a linear model. The parameter vector w has shape (D). Each microbatch contains an input matrix with D columns and one target per row. The full-batch mean squared error is

L=1N∑m∑i=1Nm(Xm,iw−ym,i)2.L=\frac{1}{N}\sum_m\sum_{i=1}^{N_m}(X_{m,i}w-y_{m,i})^2.L=N1​m∑​i=1∑Nm​​(Xm,i​w−ym,i​)2. wnew=w−η∇wL.w_{\mathrm{new}}=w-\eta\nabla_w L.wnew​=w−η∇w​L.

Here D is the feature count, m selects a microbatch, i selects one of its examples, N is the total example count, and its subscript m denotes a microbatch's size. X is the input, y is the target, L is the loss, eta is lr, and the gradient is taken with respect to w. Each example contributes equally, even when microbatch sizes differ.

Return a Python dictionary with new_param holding the updated parameter tensor and full_grad holding the accumulated full-batch gradient. Both values must be PyTorch tensors of shape (D), preserving param's dtype and device.

Loading visualization...

Examples

Input: param = [1,-1], microbatch_inputs = [[[1,0],[0,1]],[[1,1]]], microbatch_targets = [[0,0],[1]], lr = 0.1

Output: {"new_param":[1,-0.866667],"full_grad":[0,-1.333333]}

Explanation: The three examples produce the full-batch gradient [0,-1.333333]. One SGD update with learning rate 0.1 gives [1,-0.866667].

Input: param = [0], microbatch_inputs = [[[1],[2]],[[3],[4]]], microbatch_targets = [[1,2],[3,4]], lr = 0.1

Output: {"new_param":[1.5],"full_grad":[-15]}

Input: param = [2], microbatch_inputs = [[[1],[2]]], microbatch_targets = [[2,4]], lr = 0.25

Output: {"new_param":[2],"full_grad":[0]}

Hint 1

Tensor.detach and Tensor.clone can isolate a fresh gradient-tracking parameter.

Hint 2

Tensor.backward accumulates into a leaf tensor's grad buffer.

Hint 3

Tensor.shape gives the example count of each microbatch.

Requirements

  • Use PyTorch autograd with one backward pass per microbatch and exactly one SGD update.
  • Preserve the shared float32 or float64 input dtype and device; public cases and custom Run inputs use float32.
  • Leave the parameter, its existing gradient, and both microbatch collections unchanged.

Constraints

  • param is a one-dimensional PyTorch tensor of length D, where D is positive.
  • microbatch_inputs and microbatch_targets are equally sized Python lists of PyTorch tensors.
  • Each input matrix has D columns and one target per row; the total number of examples is positive.
  • All tensors share a float32 or float64 dtype and device and contain finite values.
  • lr is a finite nonnegative Python number.
Try Similar Problems
Mixed-Precision Training Memory AccountantEasyNamed-Dimension Batched Attention ScoresEasyTransformer Training FLOP EstimatorEasyOrthogonalize a Muon Momentum UpdateEasyPerform Stable Exact DeduplicationEasyPatchify a Batch of ImagesEasyBuild an Assistant-Only SFT Loss MaskEasyRMSNorm Forward PassEasyMasked Language ModelingEasyNext Sentence PredictionEasyImplement Prefix Cache Matching and ReuseMediumN-gram Language ModelMedium

Accepts: array

Accepts: array

Accepts: array

Accepts: number

You must run your code first.

Gradient Accumulation Equivalence

Language Models
Easy

Perform one SGD update using gradients accumulated across microbatches of a linear model. The parameter vector w has shape (D). Each microbatch contains an input matrix with D columns and one target per row. The full-batch mean squared error is

L=1N∑m∑i=1Nm(Xm,iw−ym,i)2.L=\frac{1}{N}\sum_m\sum_{i=1}^{N_m}(X_{m,i}w-y_{m,i})^2.L=N1​m∑​i=1∑Nm​​(Xm,i​w−ym,i​)2. wnew=w−η∇wL.w_{\mathrm{new}}=w-\eta\nabla_w L.wnew​=w−η∇w​L.

Here D is the feature count, m selects a microbatch, i selects one of its examples, N is the total example count, and its subscript m denotes a microbatch's size. X is the input, y is the target, L is the loss, eta is lr, and the gradient is taken with respect to w. Each example contributes equally, even when microbatch sizes differ.

Return a Python dictionary with new_param holding the updated parameter tensor and full_grad holding the accumulated full-batch gradient. Both values must be PyTorch tensors of shape (D), preserving param's dtype and device.

Loading visualization...

Examples

Input: param = [1,-1], microbatch_inputs = [[[1,0],[0,1]],[[1,1]]], microbatch_targets = [[0,0],[1]], lr = 0.1

Output: {"new_param":[1,-0.866667],"full_grad":[0,-1.333333]}

Explanation: The three examples produce the full-batch gradient [0,-1.333333]. One SGD update with learning rate 0.1 gives [1,-0.866667].

Input: param = [0], microbatch_inputs = [[[1],[2]],[[3],[4]]], microbatch_targets = [[1,2],[3,4]], lr = 0.1

Output: {"new_param":[1.5],"full_grad":[-15]}

Input: param = [2], microbatch_inputs = [[[1],[2]]], microbatch_targets = [[2,4]], lr = 0.25

Output: {"new_param":[2],"full_grad":[0]}

Hint 1

Tensor.detach and Tensor.clone can isolate a fresh gradient-tracking parameter.

Hint 2

Tensor.backward accumulates into a leaf tensor's grad buffer.

Hint 3

Tensor.shape gives the example count of each microbatch.

Requirements

  • Use PyTorch autograd with one backward pass per microbatch and exactly one SGD update.
  • Preserve the shared float32 or float64 input dtype and device; public cases and custom Run inputs use float32.
  • Leave the parameter, its existing gradient, and both microbatch collections unchanged.

Constraints

  • param is a one-dimensional PyTorch tensor of length D, where D is positive.
  • microbatch_inputs and microbatch_targets are equally sized Python lists of PyTorch tensors.
  • Each input matrix has D columns and one target per row; the total number of examples is positive.
  • All tensors share a float32 or float64 dtype and device and contain finite values.
  • lr is a finite nonnegative Python number.
Try Similar Problems
Mixed-Precision Training Memory AccountantEasyNamed-Dimension Batched Attention ScoresEasyTransformer Training FLOP EstimatorEasyOrthogonalize a Muon Momentum UpdateEasyPerform Stable Exact DeduplicationEasyPatchify a Batch of ImagesEasyBuild an Assistant-Only SFT Loss MaskEasyRMSNorm Forward PassEasyMasked Language ModelingEasyNext Sentence PredictionEasyImplement Prefix Cache Matching and ReuseMediumN-gram Language ModelMedium

Accepts: array

Accepts: array

Accepts: array

Accepts: number

You must run your code first.