Llama from scratch, or how to implement a paper without crying
For AI learners like me, here&#x27;s an attempt to <i>briefly</i> explain some of the terms and concepts in this blog post, in the rough order they appear.<p>A token is a unique integer identifier for a piece of text. The simplest tokenization scheme is just Unicode where one character gets one integer, however LLMs have a limited number of token IDs available for use (the vocabulary), so a more common approach is to glue characters together into common fragments. This post just uses the subset of ASCII needed by TinyShakespeare.<p>The &quot;loss function&quot; is just a measure of how similar the model&#x27;s prediction is to the ground truth. Lower loss = better predictions. Different tasks have different loss functions, e.g. edit distance might be one (but not a good one). During training you compute the loss and will generally visualize it on a chart. Whilst the line is heading downwards your NN is getting better, so you can keep training.<p>PyTorch is a library for working with neural networks and tensors. A tensor is either a single number (0 dimensions, a scalar), an array of numbers (1 dimension, a vector), or a multi-dimensional array of numbers where the 2-dimensional case is called a matrix. But a tensor can have any number of dimensions. PyTorch has a relatively large amount of magic going on in it via reflection and other things, so don&#x27;t expect the code to make much intuitive sense. It&#x27;s building a computation graph that can be later executed on the GPU (or CPU). The tutorial is easy to read!<p>A neural network is a set of neurons, each of which has a number called the bias, and connections between them each of which has an associated weight. Numbers (activations) flow from an input neuron through the connections whilst being adjusted by the weights to arrive at an output neuron, those numbers are then summed then multiplied by the bias before being emitted again to the next layer. The weights and biases are the network parameters and encode its knowledge.<p>A linear layer is a set of input neurons connected to a set of output neurons, where every input is connected to every output. It&#x27;s one of the simplest kinds of neural network structure. If you ever saw a diagram of a neural network pre-2010 it probably looked like that. The size of the input and output layers can be different.<p>ReLU is an activation function. It&#x27;s just Math.max(0, x) i.e. it sets all negative numbers to zero. These are placed on the outputs of a neuron and are one of those weird mathematical hacks where I can&#x27;t really explain why it&#x27;s needed, but introducing &quot;kinks&quot; in the function helps the network learn. Exactly what &quot;kinks&quot; work best is an open area of exploration and later the author will replace ReLU with a newer more complicated function.<p>Gradients are kind of numeric diffs computed during training that are used to update the model and make it more accurate.<p>Batch normalization is a way to process the numbers as they flow through the network, which helps the network learn better.<p>Positional encodings help the network understand the positions of tokens relative to each other, expressed in the form of a vector.<p>The `@` infix operator in Python is an alias for the __matmul__ method and is used as a shorthand for matrix multiplication (there are linear algebra courses on YouTube that are quite good if you want to learn this in more detail).<p>An epoch is a complete training run of the dataset. NNs need to be shown the data many times to fully learn, so you repeat the dataset. A batch is how many of the items in the dataset are fed to the network before updating the parameters. These sorts of numbers are called hyperparameters, because they&#x27;re things you can fiddle with but the word parameters was already used for weights&#x2F;biases.<p>Attention is the magic that makes LLMs work. There are good explanations elsewhere, but briefly it processes all the input tokens in parallel to compute some intermediate tensors, and those are then used in a second stage to emit a series of output tokens.