下列文本提取自本地原版 PDF。图表和公式的准确布局请对照 PDF。
1 Assignment Overview
In this assignment, you will build all the components needed to train a standard Transformer language
model (LM) from scratch and train some models.
What you will implement
1. Byte-pair encoding (BPE) tokenizer (Section 2)
2. Transformer language model (LM) (Section 3)
3. The cross-entropy loss function and the AdamW optimizer (Section 4)
4. The training loop, with support for serializing and loading model and optimizer state (Section 5)
What you will run
1. BPE tokenizer training on the TinyStories dataset.
2. Trained tokenizer encoding on the dataset to convert it into a sequence of integer IDs.
3. Transformer LM training on the TinyStories dataset.
4. Sample generation and evaluation of perplexity using the trained Transformer LM.
5. Model training on OpenWebText, and submit your attained perplexities to a leaderboard.
What you can use
We expect you to build each component from scratch. In particular, you may not use any definitions from
torch.nn, torch.nn.functional, or torch.optim except for the following:
• torch.nn.Parameter
• Container classes in torch.nn (e.g., Module, ModuleList, Sequential, etc.).1
• The torch.optim.Optimizer base class
You may use any other PyTorch definitions. If you would like to use a function or class and are not sure
whether it is permitted, feel free to ask on Slack. When in doubt, consider if using it compromises the
“from-scratch” ethos of the assignment.
Statement on AI tools
AI can solve many parts of the assignments fully autonomously. This makes it harder to deeply engage
with, and learn from, the course material.
The use of AI tools is permitted for answering high-level conceptual questions, or for providing low-level
programming documentation like function signatures and library APIs. However, AI tools are not
permitted for implementing any part of any assignment. This includes both coding agents (e.g., Cursor
Agents, Codex, Claude Code) and AI autocomplete (e.g., Cursor Tab, GitHub Copilot). When using an
AI agent, make sure it uses the AGENTS.md file provided. The prompt should also be included when
using chatbots.
1
See pytorch.org/docs/stable/nn.html#containers for a full list.
We strongly encourage you to disable AI autocomplete (e.g., Cursor Tab, GitHub Copilot) in your IDE
when completing assignments (though non-AI autocomplete, e.g., autocompleting function names is
totally fine). Previous students have highlighted that disabling AI autocomplete made it easier to engage
deeply with the material.
For the full AI Policy, see this document.
What the code looks like
The assignment code, as well as this writeup, are available on GitHub at:
github.com/stanford-cs336/assignment1-basics
Please git clone the repository. If there are any updates, we will notify you so you can git pull to get
the latest.
1. cs336_basics/*: This is where you write your code. Note that there’s no code in here—you can do
whatever you want from scratch!
2. adapters.py: There is a set of functionality that your code must have. For each piece of functionality
(e.g., scaled dot product attention), fill out its implementation (e.g.,
run_scaled_dot_product_attention) by simply invoking your code. Note: your changes to adapters.py
should not contain any substantive logic; this is glue code.
3. test_*.py: This contains all the tests that you must pass (e.g., test_scaled_dot_product_attention),
which will invoke the hooks defined in adapters.py. Don’t edit the test files.
How to submit
In order to submit, run make_submission.sh to construct a submission zip file. Make sure to add any
additional files to the list of exclusions in the script if you have large data files or checkpoints you don’t
want to include in your submission zip.
You will submit the following files to Gradescope:
• writeup.pdf: Answer all the written questions. Please typeset your responses.
• code.zip: Contains all the code you’ve written.
To submit to the leaderboard, submit a PR to:
github.com/stanford-cs336/assignment1-basics-leaderboard
See the README.md in the leaderboard repository for detailed submission instructions.
Where to get datasets
This assignment will use two pre-processed datasets: TinyStories [R. Eldan et al., 2023] and
OpenWebText [A. Gokaslan et al., 2019]. Both datasets are single, large plaintext files.
If you are doing the assignment with the class, you can find instructions for downloading the dataset in
the compute guide.
If you are following along at home, you can download these files with the commands inside the README.md.
Low-Resource Tip: Init
Throughout the course’s assignment handouts, we will give advice for working through parts of
the assignment with fewer or no GPU resources. For example, we will sometimes suggest
downscaling your dataset or model size, or explain how to run training code on a Mac
integrated GPU or CPU. You’ll find these “low-resource tips” in a blue box (like this one). Even if
you are an enrolled Stanford student with access to the course machines, these tips may help you
iterate faster and save time, so we recommend reading them!
Low-Resource Tip: Assignment 1 on Apple Silicon or CPU
With the staff solution code, we can train an LM to generate reasonably fluent text on an Apple
M4 Max chip with 36 GB RAM, in under 5 minutes on Metal GPU (MPS) and about 30 minutes
using the CPU. If these words don’t mean much to you, don’t worry! Just know that if you have a
reasonably up-to-date laptop and your implementation is correct and efficient, you will be able to
train a small LM that generates simple children’s stories with decent fluency.
Later in the assignment, we will explain what changes to make if you are on CPU or MPS.
本章讨论
在 GitHub 查看记录 ↗