Gradient accumulation: determination of the denominator of the overall target first

A consistent cumulative gradient by sample or number of valid tokens is constructed to identify the implied weights caused by the average number of small batches.

• Step by stepCollate in 2 self-measured2 First-hand sources
Start Body
Learning objectives and first steps2 objectives . 1 first fix

Try to do it after reading

  • Accumulated gradients by sample or number of valid cards
  • Identify implied weights from small batch averages

Suggested first

  • Gradient and vector calculations

Accumulated mathematical commitment

When there is a shortage of memory, a large number of times can be broken up, a gradient for a number of small batches can be calculated and the parameters can be last updated only once. It is equivalent to a large number of sub-graders on the premise that each small batch is calculated at the same parameter and that the loss weight, reclassification and random mechanisms are consistent with the objective. (b) Not add the gradients that have been updated several times; If an optimist step is carried out in the middle, the calculation point is different.

Target first: If the total loss is an arithmetic average of the total sample loss, the average gradient of the small batch must be weighted according to the number of samples. If the objective is equal to the valid token, the weight of the valid token shall be the number of valid tokens; If each user has the same weight, another denominator is required. The changes in mask and sample weights are truly optimized statistical objects and not insignificant details of achievement.

A trap that doesn't wait for a small batch

The first small batch had two samples with an average gradient of 1; The second was a sample of only one, with an average gradient of 4. The average gradient for the total batch is 2x1+1x4)/3=2. The two average gradients were divided by two, and 2.5 were obtained, equivalent to half the weight of the last sample. Each small batch is the same size hour, and the two writings are the same.

Sequence tasks are more hidden: each batch has eight series, but the valid tokens are 80 and 240, respectively. If the objective is averaged by token, the average loss in the two instalments should be consolidated by 1:3. The relative impact of short series is increased by, for example, number of series. This may also be deliberate design, but it must be stated in the target that it cannot be accidentally adopted because of the ease of the code.

What's the situation where the equivalent is not strict

The reclassification uses small batch statistics, which differ from the forward calculation of one mass; Random discards, data enhancement and random sampling also make it possible to achieve the same step by step. They do not have to render training ineffective, but should not claim to fully recreate a large number of sub-trajectories. A small margin of error is also introduced into the floating point search and order.

Operationally, a group of accumulated emptied gradients, which are only inversed within the group, is completed with the required scalation, aggregation, cropping and updating, and is pushed forward with an updated scheduler. The original group count could not continue to be used as a denominator when the last group was less than one group, otherwise the end batch gradient was reduced. Recording the actual number of valid samples can help to replicate training behaviours at different machines and at different sequence lengths.

g=n1g1+n2g2n1+n2

Self-measurement and provocator

Two samples were 2 and 1, the mean gradients were 1 and 4, and what was the total mean?

It's two. The sum of the total gradients is 6, divided by three samples; A simple average of two mean values will get an incorrect 2.5.

Does the gradient accumulate a large number of equivalents, which mean that the same is also true for the period leading up to the reclassification?

Doesn't mean. The small batch of the re-entry statistics that relies on the front line must be analysed separately.

Sources and reconciliations

Definitions, methods and interfaces are available.

  1. PyTorch — Automatic Mixed Precision examples: gradient accumulation
  2. Stanford CS229 Lecture Notes

Figures and calculations are reorganized according to this chapter. Mathematical measurements, framework operations and real business effects are described separately in the body of the text.

← Back to the knowledge directory
Ask Max Ask about this site
Ask Max ↗

Opening the assistant…

Paper figure

Photo comments

LEAVE A NOTE

About this photograph

Connecting to the discussion board…

    Comments become public after moderation. You can delete your own comments from this browser.
    No email is required. Clearing cookies prevents this browser from claiming earlier comments.

    MAX WANG / INDEX

    Find something

    Enter a keyword, or explore the directory below.