Learning a second domain
A pretrained language model is often trained further on new data: a newer crawl, code, a language or a field it saw little of. Continual pretraining does this without starting over, and its cost is forgetting: the loss on the first domain rises while the second is learned. Retraining from scratch on both would avoid it at the price of all the compute already spent, so the practical question is how to continue.
This tab runs it at small scale. The shared small GPT of the Pretraining tabs (4 layers of width 256, 16.08M parameters) is first trained for 1,000 steps on TinyStories (domain A) with warmup and cosine decay from 4e-3 to 4e-4, the way any of these tabs trains it. From that checkpoint, with its weights, AdamW's two moments and the TinyStories batch generator, it trains 1,000 more steps on Wikipedia (domain B, wikitext-103), six ways. Every 25 steps, and every 10 for the first 200 steps after the switch, each run is measured on 128 validation windows of each domain, which is what the Both domains through the switch panel plots.
The forgetting curve
At the switch the model is good at stories and bad at encyclopedia text: 2.153 nats on TinyStories and 8.756 on Wikipedia, which it never saw. Continuing on Wikipedia at the rate the first phase ended at, 4e-4, its Wikipedia loss falls to 5.506 in 1,000 steps. Its TinyStories loss rises from the first step: by 0.21 nats after 10 steps, 0.40 after 20 and 0.58 after 50, and it keeps rising to 4.030 at the end, nearly 1.9 nats worse than at the switch. The The first 200 steps on Wikipedia panel shows how fast this happens; nothing in the new data is hostile to stories, it simply never asks for them, and every update spent on Wikipedia moves weights the stories relied on.
The A TinyStories story panel shows where the loss goes. On one validation story, 92 of the 96 tokens shown are predicted worse at the end than at the switch, while 81 of the 96 tokens of a Wikipedia article are predicted better. Over all 128 TinyStories windows, the tokens that suffer most are the ones that make a story a story: "Once", which opens almost every story, goes from 0.17 nats to 11.1, the end-of-text token between stories rises by 7.5, and the names Lily, Spot and Tim by 5.8 to 6.8 (the Most forgotten tokens table). The weights moved since the switch bars measure the cause: by the end each block's weights have moved by 22 % to 28 % of their size, the embedding by 33 %.
Replay
The simplest defence is replay: keep a share of every batch from the old data. With 3 of the 32 windows (about 10 %) from TinyStories, the story loss still jumps at the switch, by 0.16 nats in 10 steps, but then stops, and ends at 2.365, 0.21 above where it started instead of 1.88. With 10 of 32 (about 30 %) it ends at 2.190, within 0.04. The most forgotten token with 10 % replay is "Spot", 1.1 nats worse, where without replay it was "Once", 10.9 worse. The price is paid on the new domain, because fewer windows of each batch are Wikipedia: 5.563 with 10 % replay and 5.708 with 30 %, against 5.506 without.
Rewarming the learning rate
The first phase ended at a tenth of its peak learning rate, and a small learning rate learns slowly. Rewarming raises it again, here back to 4e-3 over 100 steps, then decays it with a cosine over the remaining 900, as the Learning rate panel shows. Gupta et al. and Ibrahim et al. studied this choice for large models.
It does what it is for on the new domain: the Wikipedia loss ends at 4.978 without replay, against 5.506 at the low rate. It also forgets faster and more, and moves the weights about three times as far: 74 % to 92 % of each block's size by the end, against 22 % to 28 % at the low rate. Without replay the TinyStories loss is 3.05 after 20 steps, peaks at 5.191 at step 1,475 and ends at 4.914, 0.88 nats worse than at the low rate. With replay the high learning rate first pushes the story loss up, to 3.017 with 10 % and 2.698 with 30 %, and the decay then brings it back down, to 2.550 and 2.216: the peak of forgetting follows the peak of the learning rate.
Put together in the Forgetting against learning panel, rewarming with replay beats staying low on both domains at once: rewarming with 30 % replay ends at 2.216 on TinyStories and 5.204 on Wikipedia, better on both than staying low with 10 % replay (2.365 and 5.563); rewarming with 10 % replay, at 2.550 and 5.040, is better on both than staying low without replay. Ibrahim et al. reached the same recipe for large models: rewarm, re-decay and replay a little.
Where it came from
McCloskey and Cohen named catastrophic interference in 1989: a network trained on a second task overwrites what the first task needed. Replay of earlier data is the oldest remedy and Rolnick et al. showed how far a little of it goes; Kirkpatrick et al.'s elastic weight consolidation protects the weights the old task relies on instead. For language models, Gururangan et al. showed that continuing pretraining on a domain pays off on that domain's tasks, and Gupta et al. and Ibrahim et al. studied how to warm the learning rate back up and how much to replay. Merity et al. built the wikitext corpus used as the second domain. The WSD tab shows the other half of the schedule question, how decaying a learning rate from a flat stretch finishes a run; the Arch ladder tab trains the same model shape.
Papers
- Simple and Scalable Strategies to Continually Pre-train Large Language Models Shows that rewarming, re-decaying and replaying a small share of old data match retraining from scratch on both datasets.
- Continual Pre-Training of Large Language Models: How to (re)warm your model? Measures how rewarming the learning rate trades loss on the new data against forgetting the old.
- Catastrophic Interference in Connectionist Networks: The Sequential Learning Problem The first account of catastrophic forgetting: training on new items overwrites old ones.
- Experience Replay for Continual Learning Replaying a small buffer of old data greatly reduces forgetting, as the replay runs here show.
- Overcoming catastrophic forgetting in neural networks Elastic weight consolidation: slowing the weights the old task depends on instead of replaying it.
- Don't Stop Pretraining: Adapt Language Models to Domains and Tasks Continued pretraining on a domain's text improves that domain's tasks.
- Pointer Sentinel Mixture Models Introduces the wikitext-103 corpus, the Wikipedia domain here.