↓Skip to main content

Notes from working through ARENA

Background
#

I started on the ARENA program towards the end of my master’s, when by chance I opened an email from Zurich AI Safety, whose homepage eventually led me to ARENA’s website. I had focused on Deep Learning since the end of my undergraduate, but in hindsight, the ARENA program was incredibly valuable to fill the needed gaps for AI Safety and shift my focus from vision to language models.

Fundamentals
#

ARENA is a build-first curriculum covering fundamentals, transformers, RL and LLM evaluations in four chapters (Note: They recently added even more content, which I did not have the chance to fully check out yet). ARENA was the main source I followed, though later I also found Jacob Hilton’s deep learning curriculum, which I used as additional curated collection of exercises and reading material.

And even when going through their “Fundamentals” section, I learned new things. One was einops. I had seen it in open-source replications from lucidrains before, but hadn’t invested time to understand it. The other was cross-entropy loss, which I had come across countless times, but never cared to learn where the formula originates from. Learning this fascinated me enough to write a short post on it. And for optimizers, this visual explainer is the most intuitive way I have seen this explained to date.

Transformer
#

This is where I first diverged from ARENA, because I wanted to make sure, I get this. To get the context right, what helped me frame is understand what was the reason behind why this architecture happened when it happened. The article on Seq2Seq with Attention from Jay Alammar explains on how the field went from a simple encoder-decoder stack with a single state vector to per-token states and attention. Both of which are the groundwork for transformers. Once I understood the “why” it happened, what really helped me to get the “how” is starting over from a completly blank repository. And to then fill in the gaps one by one. So I read and re-read the Attention Is All You Need paper over and over again to get all the technical details down. The main thing I got away from this is a very intuitive understanding of the attention mechanism: For every token (query), look at all the keys of the tokens that came before (including the current) and take the dot product. Which is just a similarity score of the two. We softmax this to get a probability distribution and then sum up across the values (=that hold the actual content we are interested in).

$$ \text{Attention}(Q, K, V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V $$

I wanted to stay as close to the original and trained on the WMT 2014 EN-DE dataset that was used in the paper. I also used sentencepiece over the more popular (and easier to use) tiktoken library. In fact I found little to no good documentation besides a Kaparthy video - see below. I trained this on a single RTX 3090 for 32 hours.

In the end I also implemented beam search here, and noticed improvements both on single examples and on the final BLEU metric where I got similar results to the reported values in the original paper. Full implementation is on github.

Model Decoding Strategy BLEU
Transformer (Vaswani et al.) Beam search 27.30
My implementation Greedy 26.20
My implementation Beam search 27.45

Key Resources
#

Some more thoughts regarding these resources. They are great, but some of them are also traps. E.g. The Illustrated Transformer to me did never really 100% click until I implemented it from scratch. And I looked at this article before every ML exam in uni. Doing stuff >>> reading about other people doing stuff.

GPT
#

After having a working Transformer, the natural next step was to see how we go from this to LLMs. Because before ChatGPT, there were GPT-1, 2 and 3. Reading through these three papers, it’s not really about the architecture. Decoder-only at a basic level is even simpler than the Encoder-Decoder from Attention Is All You Need. It’s already visible here that it’s now mostly about training regime and scale.

Scaling Laws
Early signs of scaling laws (Fig. 1 from GPT-2)

Again, I wanted to get it, so also started over and build GPT-2 from scratch. Architecture wise there were not many changes needed. To actually train it, and make it more efficient I borrowed a couple of tricks that Kaparthy’s Let’s reproduce GPT-2 (124M) highlights. The main one being to use the 10B sample of the FineWeb-Edu dataset. Also to use torch.compile, mixed precision and FlashAttention.

But the biggest learning from this project came from a subtle bug I introduced. In the GPT-2 paper, they mention that LayerNorm was moved to the input of each sub-block. In my initial implementation, I wrote this:

def forward(self, x):
    x = self.ln1(x)
    sub1 = self.self_attention(x)
    ...
    x = x + sub1

which makes the information exchange via the residual stream harder than it should be and leads to worse results. My intuition regarding the residual stream is from the Mathematical Framework for Transformer Circuits. In the end I evaluated my implementation on HellaSwag. It was worse, but still in a similar ballpark as the GPT-2 checkpoint I used to compare against (training took around a day on a RTX 4090):

Model HellaSwag Accuracy
Original GPT-2 small 31.45%
My implementation 29.82%

Key Resources
#

Mechanistic Interpretability
#

This is also were I started reading more of the Transformer Circuits Thread. For me, the most influential articles here are the earlier works. Largely because they lay the foundation of understanding that one should have. One, A Mathematical Framework for Transformer Circuits, which brings up the residual stream and how information processing works in the transformer via QK and OV circuits. Two, In-Context Learning and Induction Heads that showcases that these circuits are capabilities learned in the training regime - and we can even pinpoint at what point they happen. Toy Models of Superposition why mech interp can be so difficult in practice. Also a shoutout to the original Distill circuits work on InceptionV1. This is real beautiful work and I’d be excited to explore CNNs more at one point like Liv Gordon did in her The Missing Curve Detectors of InceptionV1.

Working through Chapter 1 from ARENA, the material first went over TransformerLens. This is a tool that can be used to manipulate model activations - read them out, swap them, change them, insert them back in, all of this stuff. Makes you feel like a crazy doctor on an (artificial) brain. What I liked most here was working through induction heads in a small 2-layer model myself. These toy models are super clean - and unlike a lot of other mech interp easily analyzable.

Then Chapter 1 goes into SAEs. Which is about untangling superposition into sparse features and finding out which activate - when and where. Tools the community here uses and that are introduced are SAELens and Neuronpedia. This is no longer on toy models and I found these hard to analyze/interpret personally.

From my point of view, the field has moved on though from looking at SAEs as the interp solution. First, GDM & Neel Nanda heavily updated towards pragmatic interp and linear probe work and showcased why in Negative Results for SAEs On Downstream Tasks and Deprioritising SAE Research. Second, I saw recent works on Activation Oracles (this has already been added to the new ARENA material!) which I find super promising. Especially (edit April 20) from the Mythos system card, in an unsupervised setting the Activation Verbalizers.

Key Resources
#

RL
#

In my opinion the OG idea of RL - agents learning through interaction - and self-play is absolutely beautiful. I also think it’s pretty hard to wrap your head around. Somewhat. But anything can be done with enough motivation, and my motivation hereby was RLHF. To be fair, also here I didn’t start from scratch. I had already covered the basics such as MDPs, Bellman equations, TD Learning and DQN throughout university. So it was pretty easy for me to follow the first half of the ARENA Chapter 2, which goes through exactly that. PPO was new for me. I followed ARENA here too, but after going working on this chapter, I was still confused.

So I again decided to start from a blank state and reimplement from scratch, rather than filling out the already pre-made skeleton-code in their notebooks. Which is also the exercise in RL chapter in Jacob Hilton’s curriculum.

CartPole Lunar Lander CoinRun
Feels good watching them succeed. My trained agents playing CartPole, Lunar Lander, and CoinRun.

The full implementation from me is as before on github. What really helped here was to get all the details right. Because this was for me the difference between the agent learning, and just getting stuck early. These PPO implementation details helped a lot to grasp these details and this one how to debug RL surfaced bugs quickly.

The last piece missing is then RLHF (ARENA_2.4). We take our pretrained LM, collect human preference data, train a reward model on those preferences - often the reward model is of similar size than the LM, and can be the same, just with a different head, and fine-tune the language model with RL against that reward model. So the model learns to mimic what human (labeller)s want.

ARENA 2.4 has you actually implement this end-to-end on a small LM. The reward function we used was “maximize the number of periods in the output” - without a KL coefficient against the original model, the policy just collapses completely and spams …….. With the KL term on it gets way more interesting: the model shifts towards shorter sentences, text full of URLs, and abbreviations like “U.S.” or “Ph.D.” appearing a lot more than they should. The reward hacking now has to happen within the original distribution.

Key Resources
#

Basics:
#
PPO Implementation + Literature:
#
RLHF
#

LLM Evals
#

In ARENA Chapter 3 I worked through LLM evals using Inspect. Inspect organizes an evaluation into subcomponents: task, solver, scorer. So it’s pretty OOP - you design the different components separately and then they interact - and they can easily be decoupled. I found this way more interesting than I expected, because it required a lot of thinking about how I actually want to design the eval, and I liked the feeling of Inspect too.

ARENA uses alignment faking as the motivating case study here. I like ARENA here, as it pushes you onto thinking about how much the results in your eval actually matter. How should anyone update from these “findings”. In general I think this is always important to keep in mind, why what we do/show etc. actually matters.

Key Resources
#

Next Steps
#

As for my next steps, I now feel I’m ready to start keeping up with SOTA across the different subfields in safety. I have started reading Twitter, various company research science blogs (especially Anthropic and OAI), and follow new work as it comes out - rather than catch up. Also, I have been advised by people whose opinion I value, that I need to get out and build things myself rather than just re-implementing. So will start doing this more too.