Vectors, Matrices & Tensor Shapes
The last lesson did all of its counting with one number: 8.03 billion parameters, two bytes each, 16 GB. Open the download and the pile turns out to be organized. Llama 3.1 8B's weights unpack into exactly 291 named arrays, each a block of numbers with a declared shape, and every amount memory and throughput arithmetic worked with is a product of those shapes.
This lesson teaches you to read shapes the way the lesson on memory and throughput arithmetic read datasheets. By the end you should be able to look at a shape and know how many numbers it holds and what they cost in bytes, tell a model's two populations of numbers apart, and check whether a matrix multiplication is even possible before anything runs.
Reading a shape
A vector is a list of numbers, and its shape is one length: [4096] is a vector of 4,096 numbers. A matrix is a grid, and its shape is two lengths, rows first: [128256, 4096] is 128,256 rows of 4,096 numbers each. Past two axes the everyday words run out, so the field uses one word for all of these. A tensor is an array with any number of axes, and a vector and a matrix are just its one-axis and two-axis cases.
The reading rules are short. Rank is the number of axes, the shape lists the length of each, and the element count is the product of the shape, so [128256, 4096] holds 525,336,576 numbers. Bytes are one multiplication further, the counting frame from memory and throughput arithmetic applied per tensor: element count × byte width, and at two bytes per number that matrix costs about 1.05 GB.
Two vocabulary warnings, both cheap to absorb now. Mathematics loads the word tensor with extra structure; machine learning does not, and here a tensor is nothing more than a shaped block of numbers. And the word dimension gets used both ways in practice: a vector of 4,096 numbers is called 4,096-dimensional even though, as a tensor, it has a single axis, so this course says axes when it means the shape sense.
Parameters and activations
A running model holds two populations of numbers, and they behave differently enough that the rest of the course names them constantly. The first population is the file: the parameters, the tensors whose shapes were fixed the day training ended. They are identical for every request, and their total is the 16 GB that memory and throughput arithmetic priced.
The second population does not exist until a request arrives. As the model works through an input, each layer produces fresh intermediate values and hands them to the next layer, and those working values are the activations. They are made, consumed, and overwritten as the pass proceeds; nothing in the file stores them.
Activations come in one standard shape: [batch, seq, hidden]. Batch counts how many sequences the model is processing at once, the sharing the lesson on inference engineering introduced. Seq counts the tokens in each sequence. Hidden is the model's working width: inside Llama 3.1 8B, every token, whatever its text, is represented as a vector of 4,096 numbers, and that width is called the model's hidden size.
The contrast that matters is who sets the shape. Parameter shapes are printed in the model's configuration before any request exists; activation shapes follow the input. A five-token prompt makes an activation tensor of [1, 5, 4096]: 20,480 numbers, about 41 KB. A batch of eight 2,048-token sequences makes [8, 2048, 4096]: about 134 MB, over three thousand times larger, out of the same frozen model.
One caution before those numbers look reassuringly small next to 16 GB: the tensors above are the transient values of a single pass through the layers. The growing per-request state that the lesson on inference engineering said a GPU must hold and re-read across a whole generation is built from activations too, and its accounting, which decides how many requests fit on a GPU, belongs to prefill, decode, and KV memory.
The shapes inside Llama 3.1 8B
Nobody learns a model's shapes by opening tensors one at a time. Every model ships a configuration file alongside its weights, part of the artifact the inference stack unpacked, and the shape of every parameter tensor in the file follows from a few integers in it:
Four fields you can already read: hidden_size is the working width from the last section, intermediate_size the widest internal width, num_hidden_layers the depth of the repeated stack, and vocab_size the number of entries in the token vocabulary. The two head counts can wait; a shape can sit in front of you before its purpose does. The official repository asks for a license click, and an ungated mirror serves the identical files if you want to look now.
The inventory starts at the model's two ends, with a pair of matrices nothing else in the file outsizes. The first holds one row of 4,096 numbers for every entry in the vocabulary, the row a token starts as: the embedding matrix, [128256, 4096]. At the far end, a matrix of exactly the same shape maps the model's final 4,096-wide vectors back over the vocabulary. Between them, the two hold roughly an eighth of the model's parameters.
The remaining parameters, just under 7 billion, sit in the 32 repeated layers. Every matrix in them takes its axes from the config, mostly 4096 and 14336 with the head counts supplying the rest; the only parameters that are not matrices are small scale vectors of shape [4096]. The widest matrices connect widths 4,096 and 14,336, and one of them, the up projection, takes each token's vector from width 4,096 to width 14,336. Lesson the transformer architecture refresher walks the architecture those layers implement; this lesson only needs the shapes.
| Tensor | Shape | Copies | Numbers each | Bytes at BF16 |
|---|---|---|---|---|
| Embedding matrix | [128256, 4096] | 1 | 525,336,576 | ~1.05 GB |
| Output matrix | [128256, 4096] | 1 | 525,336,576 | ~1.05 GB |
| Up projection | [4096, 14336] | 32 | 58,720,256 | ~117 MB |
| Norm scale | [4096] | 65 | 4,096 | ~8 KB |
The shapes also settle an account the lesson on memory and throughput arithmetic left rounded. Add the element counts of all 291 tensors and the total is exactly 8,030,261,248 numbers; at two bytes each, 16,060,522,496 bytes of weight data, the total the file's own index records. The napkin and the artifact agree to the byte, because shape arithmetic is how the file is laid out.
Matrix multiplication and its cost
Nearly everything a model does to activations is matrix multiplication, matmul in the field's shorthand. What the operation computes can wait. What it does to shapes cannot, because shapes decide whether a matmul is possible at all and what it costs.
The rule is one line: [m, k] × [k, n] → [m, n]. The inner axes must match and disappear; the outer axes survive. The multiplication pairs each row of the left tensor with each column of the right along that shared axis of length k, and if the lengths differ there is nothing to pair.
Run the five-token prompt through the up projection, letting the five tokens stand as five rows: [5, 4096] × [4096, 14336]. The inner 4096s agree, and out comes [5, 14336], five tokens now 14,336 numbers wide. Only a matrix shaped [14336, n] can legally come next, and each layer carries one, the down projection [14336, 4096], which returns the tokens to width 4,096.
The cost falls out of the same three numbers. Each of the m × n output numbers consumes the whole shared axis: k multiplications and k − 1 additions, call it 2k operations. The total is 2 × m × k × n FLOPs, with multiplications and additions counted separately, which is the convention behind every FLOPs figure in this course.
Push a single token through the up projection and the formula gives 2 × 1 × 4096 × 14336, about 117 million FLOPs, against the matrix's 58,720,256 parameters. Two FLOPs per parameter: each weight is used in one multiplication and, with the accumulate, one addition. That is the seed of the 2 FLOPs per parameter figure memory and throughput arithmetic borrowed; the full per-token account, every matrix in every layer, is from prefill, decode, and KV memory.
Notice what the row count touches and what it does not. Five tokens cost five times the FLOPs of one, but the weight matrix is the same 117 MB whether one row or two thousand pass through it. Compute scales with the rows; weight bytes do not. The observation from inference engineering that requests can share one read of the weights is sitting in the shape arithmetic.
Shape errors
The most common failure in day-to-day model code is a shape that refuses to line up. Every layer boundary is a shape contract, a matmul enforces its side mechanically, and two things break the contract constantly: transposed matrices and batch axes.
Transposition first. [4096, 14336] and [14336, 4096] hold the same 58,720,256 numbers and are not interchangeable, and the field stores matrices both ways. In the Llama 3.1 8B weights file, the up projection this lesson has been writing as [4096, 14336] is stored as [14336, 4096], output width first; the runtime applies the transpose. Print a stored tensor and you may be looking at the mirror of the shape the math uses.
The batch axis is the other classic. Code written for [seq, hidden] meets a tensor shaped [batch, seq, hidden], or the reverse, and a matrix that would have lined up is paired against the wrong axis. A missing or extra leading axis is often the first bug you hit in an unfamiliar model codebase.
The saving grace is that frameworks fail loudly and specifically. PyTorch, which this course adopts in lesson Python and PyTorch setup, refuses with both shapes in the message: mat1 and mat2 shapes cannot be multiplied (5x4096 and 14336x4096). That message is an invitation to run this lesson's check by hand: write the two shapes down, compare the inner axes, and the fix is nearly always a transpose or a reshaped batch axis away.
A model is a fixed bag of parameter tensors whose shapes come from a config file, plus a stream of activation tensors whose shapes follow the input. Every amount on the napkin from memory and throughput arithmetic is a product over those shapes: bytes from element count times byte width, FLOPs from a matmul's three numbers. The next lesson goes inside the multiplication: the dot product behind every output number, the softmax that turns rows of numbers into weights, and the attention arithmetic assembled from the two.