Model and Inference EngineeringTraining data and objectivesPrincipal
Why does gradient accumulation fail to preserve the negative set in an in-batch contrastive loss?
By Amit Kumar3 min readWorked answer includedReviewed 28 Sept 2026
The question
Interview question
A retriever originally encodes 256 query-document pairs in one forward pass. It switches to sixteen microbatches of 16 and accumulates gradients before one optimizer step, but quality falls. Explain the changed similarity denominators, design a comparison on fixed data, and evaluate gradient caching or mined negatives with false-negative and replay constraints.
Draft your approach first
Take a few minutes before you read the answer. Name the constraint that matters most, the failure you are designing for and how you would know it worked.
Reveal the worked answerWorked answer
Because the batch is part of the loss, not merely the optimizer schedule. For a query, an in-batch contrastive loss compares its positive document with the other documents in the same similarity matrix. With 256 pairs in the forward pass, there are up to 255 other document candidates. With 16, there are 15. Sixteen separately computed losses do not equal one loss on a 256 by 256 similarity matrix, even if their gradients are accumulated and the optimizer steps once. The denominator, the hard competitors, and therefore the possible gradient directions have changed.
A hypothetical row with equal similarity logits assigns its positive document probability 1/256 in the large matrix and 1/16 in a microbatch. The corresponding cross-entropy losses are ln(256), about 5.545, and ln(16), about 2.773. The positive-logit derivative is probability minus one, while each competing logit contributes its own probability. Both candidate identities and these coefficients change. This equal-logit fixture demonstrates the loss boundary. Actual parameter-gradient directions also depend on the embedding vectors and cannot be inferred solely from those two loss values.
I would make this visible with one frozen 256-pair batch. Run the original loss once, then the 16-way microbatch recipe on exactly those pairs. Compare the similarity matrices and which negative each query finds hardest. If the first recipe also gathers embeddings across data-parallel ranks, verify whether the new recipe still does so at the same boundary. A distributed gather can expand the candidates within one forward pass. It does not automatically join examples from future accumulation steps.
To preserve the large candidate set without encoding all 256 pairs with activations live at once, a gradient-cache approach can encode mini-batches, construct the larger embedding-level loss, then replay mini-batches for parameter gradients. Sentence Transformers documents its cached multiple-negatives loss for this memory tradeoff. The cached embeddings and replayed encoder work must describe the same model state and intended stochastic computation. Do not step the optimizer between building the embedding-level loss and replaying its parameter gradients. Check dropout replay, augmentations and cross-rank gradient flow in the chosen implementation. Or choose a smaller candidate set deliberately and compensate with mined negatives, after measuring whether that actually recovers the desired retrieval behavior. Watch for false negatives too. Another query's valid document is not necessarily a safe negative, and simply increasing the matrix can amplify that problem.
If someone argues that both runs process exactly 256 pairs per optimizer step, I would agree with the arithmetic and show the two denominators. The optimizer sees accumulated gradients from 16 different classification problems, not the gradient of one 256-way problem. We doubled gradient accumulation after losing GPUs. Why did the training recipe change? studies accumulation changing a language-model training recipe through token weighting and schedules. Here the identity of the negative examples inside a contrastive objective changes before the optimizer sees any gradient.
Related questions
Keep practising model and inference engineering
Follow another question in this area, or search the complete library.
Get my essays on AI engineering and distributed systems by email. Subscribe to ArchCrux