How does this character’s string representation (__repr__()) differ from its printed representation?
repr() 将它显示为 '\x00',而直接打印时它通常不可见。
What happens when this character occurs in text? It may be helpful to play around with the following in your Python interpreter and see if it matches your expectations:
>>>chr(0)
>>>print(chr(0))
>>>"this is a test"+chr(0) +"string"
>>>print("this is a test"+chr(0)+"string")
这是 Unicode 码点 U+0000(NUL)。它在 Python 字符串中是一个正常存在的字符,但直接打印时通常不可见;将其视为 C 字符串结尾是 C 语言及相关 API 的约定,并不意味着 Python 字符串会在这里截断。
What are some reasons to prefer training our tokenizer on UTF-8 encoded bytes, rather than UTF-16 or UTF-32? It may be helpful to compare the output of these encodings for various input strings.
Consider the following (incorrect) function, which is intended to decode a UTF-8 byte string into a Unicode string. Why is this function incorrect? Provide an example of an input byte string that yields incorrect results.
Train a byte-level BPE tokenizer on the TinyStories dataset, using a maximum vocabulary size of 10,000. Make sure to add the TinyStories <|endoftext|> special token to the vocabulary. Serialize the resulting vocabulary and merges to disk for further inspection. How much time and memory did training take? What is the longest token in the vocabulary? Does it make sense?
Consider a GPT-2 XL-sized model using our assignment architecture. How many trainable parameters would our model have? Assuming each parameter is represented using single-precision floating point, how much memory is required to just load this model? Suppose we constructed our model using this configuration:
vocab_size: 50,257
context_length: 1,024
num_layers: 48
d_model: 1,600
num_heads: 25
d_ff: 4,288 (the nearest multiple of 64 to 8/3 × 1,600)
Identify the matrix multiplies required to complete a forward pass of our GPT-2 XL-shaped model. How many FLOPs do these matrix multiplies require in total? Assume that our input sequence has context_length tokens.
Repeat your analysis with GPT-2 small (12 layers, 768 d_model, 12 heads), GPT-2 medium (24 layers, 1024 d_model, 16 heads), and GPT-2 large (36 layers, 1280 d_model, 20 heads). As the model size increases, which parts of the Transformer LM take up proportionally more or less of the total FLOPs?
head 数量本身不会改变总计算量:拆分或合并 head 后,投影维度总和仍为 d。随着层数和 d 增大,Block 内的投影与 MLP 均按更高阶增长;由于 dff∝d,MLP 的占比会上升。反之,固定 T 时注意力的 O(T2d) 占比下降;最终 logits 投影的占比也因它不随层数增长而下降。
Take GPT-2 XL and increase the context length to 16,384. How does the total FLOPs for one forward pass change? How does the relative contribution of FLOPs of the model components change?
Assume we are using float32 for every tensor. How much peak memory does running AdamW require? Decompose your answer based on the memory usage of the parameters, activations, gradients, and optimizer state. Express your answer in terms of the batch_size and the model hyperparameters (vocab_size, context_length, num_layers, d_model, num_heads). Assume d_ff = 8/3 * d_model.
For simplicity, when calculating memory usage of activations, consider only the following components:
Transformer block
RMSNorm(s)
Multi-head self-attention sublayer: QKV projections, QKT matrix multiply, softmax, weighted sum of values, output projection.
Position-wise feed-forward (SwiGLU): w1, w2, SiLU on the gate branch, element-wise product, w3
Instantiate your answer for a GPT-2 XL-shaped model to get an expression that only depends on the batch_size. What is the maximum batch size you can use and still fit within 80GB memory?
Model FLOPs utilization (MFU) is defined as the ratio of observed throughput (tokens per second) relative to the hardware’s theoretical peak FLOP throughput . An NVIDIA H100 GPU has a theoretical peak of 495 teraFLOP/s for “float32” (actually TensorFloat-32, which in reality is “bfloat19”) operations. Assuming you are able to get 50% MFU, how long would it take to train a GPT-2 XL for 400K steps and a batch size of 1024 on a single H100? Assume that the backward pass has twice the FLOPs of the forward pass.