liveBook · Manning

Gradient accumulation

As explained in Building Reliable AI Systems

A technique that sums gradients across multiple mini-batches before updating model parameters.

Gradient accumulation sums gradients across multiple mini-batches before updating parameters, helping train with a larger effective batch size when GPU memory is limited. section 5.3.4

training_args = TrainingArguments(   #1
    per_device_train_batch_size=1,
    gradient_accumulation_steps=4,
    num_train_epochs=1,
    learning_rate=2e-4,
    fp16=True,
    logging_steps=10,
    output_dir="./finetuned-llama2-bitext",
    save_strategy="no"
)

A training configuration uses a per-device batch size of 1 and accumulates gradients over 4 steps, for an effective batch size of 4. section 5.3.4

How Building Reliable AI Systems covers it

How deep it goes
Defines the memory-saving mechanism and gives a batch-size-one, four-step configuration.
Examples
Uses a per-device batch size of 1 with accumulation over 4 steps to reach an effective batch size of 4.
What it assumes
Introduces mini-batches, batch size, and parameter updates while defining gradient accumulation.
What you can do after
Apply it through Transformers TrainingArguments by accumulating four steps at a per-device batch size of one.
How it connects
Connects gradient accumulation to mini-batches, batch size, and parameter updates.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Reliable AI Systems. The section links point to the parts of the book it draws on.