下列文本提取自本地原版 PDF。图表和公式的准确布局请对照 PDF。
7.2 TinyStories
We are going to start with a very simple dataset (TinyStories; R. Eldan et al. [1]) where models will train
quickly, and we can see some interesting behaviors. The instructions for getting this dataset are in
Section 1. An example of what this dataset looks like is below.
Example (tinystories_example): One example from TinyStories
Once upon a time there was a little boy named Ben. Ben loved to explore the world around him.
He saw many amazing things, like beautiful vases that were on display in a store. One day, Ben
was walking through the store when he came across a very special vase. When Ben saw it he was
amazed! He said, “Wow, that is a really amazing vase! Can I buy it?” The shopkeeper smiled and
said, “Of course you can. You can take it home and show all your friends how amazing it is!” So
Ben took the vase home and he was so proud of it! He called his friends over and showed them
the amazing vase. All his friends thought the vase was beautiful and couldn’t believe how lucky
Ben was. And that’s how Ben found an amazing vase in the store!
7.2.1 Hyperparameter tuning
We will tell you some very basic hyperparameters to start with and ask you to find some settings for
others that work well.
Vocab size 10000. Typical vocabulary sizes are in the tens to hundreds of thousands. You should vary
this and see how the vocabulary and model behavior change.
Context length 256. Simple datasets such as TinyStories might not need long sequence lengths, but for
the later OpenWebText data, you may want to vary this. Try varying this and seeing the impact on both
the per-iteration runtime and the final perplexity.
d_model 512. This is slightly smaller than the 768 dimensions used in many small Transformer papers, but
this will make things faster.
d_ff 1344. This is roughly 83 𝑑model while being a multiple of 64, which is good for GPU performance.
RoPE theta parameter Θ 10000.
Number of layers and heads 4 layers, 16 heads. Together, this will give about 17M non-embedding
parameters which is a fairly small Transformer.
Total tokens processed 327,680,000 (your batch size × total step count × context length should equal
roughly this value).
You should do some trial and error to find good defaults for the following other hyperparameters:
learning rate, learning rate warmup, other AdamW hyperparameters (𝛽1 , 𝛽2 , 𝜀), and weight decay. You
can find some typical choices of such hyperparameters in D. P. Kingma et al. [22].
7.2.2 Putting it together
Now you can put everything together by getting a trained BPE tokenizer, tokenizing the training dataset,
and running this in the training loop that you wrote. Important note: If your implementation is correct
and efficient, the above hyperparameters should result in a roughly 20–30 minute runtime on 1 B200
GPU. If you have runtimes that are much longer, please check and make sure your dataloading,
checkpointing, or validation loss code is not bottlenecking your runtimes and that your implementation is
properly batched.
7.2.3 Tips and tricks for debugging model architectures
We highly recommend getting comfortable with your IDE’s built-in debugger (e.g., VSCode/Zed), which
will save you time compared to debugging with print statements. If you use a text editor, you can use
something like ipdb. A few other good practices when debugging model architectures are:
• A common first step when developing any neural net architecture is to overfit to a single minibatch. If
your implementation is correct, you should be able to quickly drive the training loss to near-zero.
• Set debug breakpoints in various model components, and inspect the shapes of intermediate tensors to
make sure they match your expectations.
• Monitor the norms of activations, model weights, and gradients to make sure they are not exploding or
vanishing.
Problem (learning_rate): Tune the learning rate (2 B200 hrs) (3 points)
The learning rate is one of the most important hyperparameters to tune. Taking the base model
you’ve trained, answer the following questions:
(a) Perform a hyperparameter sweep over the learning rates and report the final losses (or note
divergence if the optimizer diverges).
Deliverable: Learning curves associated with multiple learning rates. Explain your
hyperparameter search strategy.
Deliverable: A model with validation loss (per-token) on TinyStories of at most 1.45
Low-Resource Tip: Train for a few steps on CPU or Apple Silicon
If you are running on cpu or mps, you should instead reduce the total tokens processed
count to 40, 000, 000, which will be sufficient to produce reasonably fluent text. You
may also increase the target validation loss from 1.45 to 2.00.
Running our solution code with a tuned learning rate on an M4 Max chip and 36 GB
of RAM, we use batch size × total step count × context length = 32 × 5000 × 256 =
40, 960, 000 tokens, which takes 1 hour and 22 minutes on cpu and 36 minutes on mps.
At step 5000, we achieve a validation loss of 1.80.
Some additional tips:
• When using 𝑁 training steps, we suggest adjusting the cosine learning rate decay
schedule to terminate its decay (i.e., reach the minimum learning rate) at precisely
step 𝑁 .
• When using mps, do not use TF32 kernels, i.e., do not set
torch.set_float32_matmul_precision('high')
as you might with cuda devices. We tried enabling TF32 kernels with mps (torch
version 2.9.0) and found the backend sometimes uses silently broken kernels that
cause unstable training.
• You can speed up training by JIT-compiling your model with torch.compile.
Specifically:
‣ On cpu, compile your model with
model = torch.compile(model)
‣ On mps, you can somewhat optimize the backward pass using
model = torch.compile(model, backend="aot_eager")
Compilation with Inductor is not supported on mps as of torch version 2.9.0.
(b) Folk wisdom is that the best learning rate is “at the edge of stability.” Investigate how the
point at which learning rates diverge is related to your best learning rate.
Deliverable: Learning curves of increasing learning rate which include at least one divergent
run and an analysis of how this relates to convergence rates.
Now let’s vary the batch size and see what happens to training. Batch sizes are important – they let us
get higher efficiency from our GPUs by doing larger matrix multiplies, but is it true that we always want
batch sizes to be large? Let’s run some experiments to find out.
Problem (batch_size_experiment): Batch size variations (1 B200 hr) (1 point)
Vary your batch size all the way from 1 to the GPU memory limit. Try at least a few batch sizes
in between, including typical sizes like 64 and 128.
Deliverable: Learning curves for runs with different batch sizes. The learning rates should be
optimized again if necessary.
Deliverable: A few sentences discussing your findings on batch sizes and their impacts on
training.
With your decoder in hand, we can now generate text! We will generate from the model and see how
good it is. As a reference, you should get outputs that look at least as good as the example below.
Example (ts_generate_example): Sample output from a TinyStories language model
Once upon a time, there was a pretty girl named Lily. She loved to eat gum, especially the big
black one. One day, Lily’s mom asked her to help cook dinner. Lily was so excited! She loved to
help her mom. Lily’s mom made a big pot of soup for dinner. Lily was so happy and said, “Thank
you, Mommy! I love you.” She helped her mom pour the soup into a big bowl. After dinner, Lily’s
mom made some yummy soup. Lily loved it! She said, “Thank you, Mommy! This soup is so
yummy!” Her mom smiled and said, “I’m glad you like it, Lily.” They finished cooking and
continued to cook together. The end.
Low-Resource Tip: Generate text on CPU or Apple Silicon
If instead you used the low-resource configuration with 40M tokens processed, you should see
generations that still resemble English but are not as fluent as above. For example, our sample
output from a TinyStories language model trained on 40M tokens is below:
Once upon a time, there was a little girl named Sue. Sue had a tooth that she loved very much. It
was his best head. One day, Sue went for a walk and met a ladybug! They became good friends
and played on the path together.
“Hey, Polly! Let’s go out!” said Tim. Sue looked at the sky and saw that it was difficult to find a
way to dance shining. She smiled and agreed to help the talking!“
As Sue watched the sky moved, what it was. She
Here is the precise problem statement and what we ask for:
Problem (generate): Generate text (1 point)
Using your decoder and your trained checkpoint, report the text generated by your model. You
may need to manipulate decoder parameters (temperature, top-p, etc.) to get fluent outputs.
Deliverable: Text dump of at least 256 tokens of text (or until the first <|endoftext|> token),
and a brief comment on the fluency of this output and at least two factors which affect how good
or bad this output is.
本章讨论
在 GitHub 查看记录 ↗