|
| 1 | +--- |
| 2 | +title: Set up and run GPT-2 baseline |
| 3 | +weight: 3 |
| 4 | + |
| 5 | +### FIXED, DO NOT MODIFY |
| 6 | +layout: learningpathall |
| 7 | +--- |
| 8 | + |
| 9 | +## Prepare the environment |
| 10 | + |
| 11 | +Use an Arm Linux target, such as an Arm Neoverse cloud instance. The results in this Learning Path were collected on a Graviton 3 instance based on Neoverse V1 running Ubuntu 24.04 LTS. If you have not configured Arm Performix yet, complete setup and target connection using the [Arm Performix install guide](/install-guides/performix/). |
| 12 | + |
| 13 | +Install build prerequisites and clone the GPT-2 example repository: |
| 14 | + |
| 15 | +```bash |
| 16 | +sudo apt update |
| 17 | +sudo apt install -y git g++ cmake python3 python3-venv |
| 18 | +git clone --recurse-submodules https://github.com/arm-education/GPT-2-Example.git |
| 19 | +cd GPT-2-Example |
| 20 | +git checkout tags/v0.0.2 |
| 21 | +``` |
| 22 | + |
| 23 | +## Export GPT-2 model assets |
| 24 | + |
| 25 | +The C++ runtime expects exported model binaries. Create a Python virtual environment, install dependencies, and export GPT-2 Medium weights and vocabulary: |
| 26 | + |
| 27 | +This Learning Path uses [openai-community/gpt2-medium on Hugging Face](https://huggingface.co/openai-community/gpt2-medium), which corresponds to the GPT-2 Medium model from the original OpenAI GPT-2 release in 2019. The model has 355 million parameters, and in this workflow it runs with unquantized FP32 (32-bit floating-point) weights. |
| 28 | + |
| 29 | +```bash |
| 30 | +python3 -m venv venv |
| 31 | +source venv/bin/activate |
| 32 | +pip install -r src/requirements.txt |
| 33 | +python3 src/export_gpt2.py --model gpt2-medium |
| 34 | +``` |
| 35 | + |
| 36 | +This creates: |
| 37 | + |
| 38 | +- `models/gpt2-medium/weights.bin` |
| 39 | +- `models/gpt2-medium/vocab.bin` |
| 40 | + |
| 41 | +## Review the source code |
| 42 | + |
| 43 | +The `src/gpt2.cpp` file implements the end-to-end GPT-2 inference loop. Each generated token triggers a forward pass over all 24 transformer layers. Inside each layer, `matmul` is called multiple times: for the query/key/value projection, the attention output projection, and both feed-forward layers. It is called once more at the end for logits projection over the vocabulary: |
| 44 | + |
| 45 | +```cpp |
| 46 | +// Attention QKV projection |
| 47 | +matmul(s.qkv.data(), s.xb.data(), |
| 48 | + w.c_attn_w.data()+(size_t)l*3*E*E, |
| 49 | + w.c_attn_b.data()+(size_t)l*3*E, E, 3*E); |
| 50 | + |
| 51 | +// FFN expand |
| 52 | +matmul(s.mlp_h.data(), s.xb.data(), |
| 53 | + w.mlp_fc_w.data()+(size_t)l*4*E*E, |
| 54 | + w.mlp_fc_b.data()+(size_t)l*4*E, E, 4*E); |
| 55 | + |
| 56 | +// Logits projection (vocab_size x n_embd) |
| 57 | +matmul(s.logits.data(), s.x.data(), w.wte.data(), nullptr, E, cfg.vocab_size); |
| 58 | +``` |
| 59 | +
|
| 60 | +The `matmul` dispatch in `gpt2.cpp` selects a kernel at compile time based on a preprocessor flag: |
| 61 | +
|
| 62 | +```cpp |
| 63 | +static void matmul(float *out, const float *x, const float *W, const float *b, |
| 64 | + int n_in, int n_out) { |
| 65 | +#if defined(GPT2_KERNEL_NEON) |
| 66 | + kernels::matmul_neon(out, x, W, b, n_in, n_out); |
| 67 | +#elif defined(GPT2_KERNEL_SVE) |
| 68 | + kernels::matmul_sve(out, x, W, b, n_in, n_out); |
| 69 | +#elif defined(GPT2_KERNEL_USER) |
| 70 | + kernels::matmul_user(out, x, W, b, n_in, n_out); |
| 71 | +#else |
| 72 | + kernels::matmul_ref(out, x, W, b, n_in, n_out); |
| 73 | +#endif |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +The baseline kernel (`src/kernels/matmul_ref.cpp`) is a straightforward scalar nested for loop: for each output row, it walks the weight matrix row and accumulates a dot product with the input vector: |
| 78 | + |
| 79 | +```cpp |
| 80 | +void matmul_ref(float *out, const float *x, const float *W, const float *b, |
| 81 | + int n_in, int n_out) { |
| 82 | + for (int i = 0; i < n_out; i++) { |
| 83 | + float acc = b ? b[i] : 0.f; |
| 84 | + const float *row = W + (size_t)i * n_in; |
| 85 | + for (int j = 0; j < n_in; j++) acc += row[j] * x[j]; |
| 86 | + out[i] = acc; |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
| 90 | +
|
| 91 | +This scalar implementation can leave NEON and SVE vector units underused if the compiler cannot efficiently autovectorize it. Because `matmul` is called hundreds of times per token, explicitly optimizing this kernel guarantees SIMD execution where most of the available compute is spent. |
| 92 | +
|
| 93 | +## Build and run the baseline |
| 94 | +
|
| 95 | +Configure and build the project with CMake. The project uses `-O2 -g`, which keeps optimization enabled while preserving debug symbols for profiling. |
| 96 | +
|
| 97 | +```bash |
| 98 | +cmake -S . -B build -DBUILD_USER_MATMUL=ON |
| 99 | +cmake --build build --parallel |
| 100 | +``` |
| 101 | + |
| 102 | +Run the scalar baseline binary: |
| 103 | + |
| 104 | +```bash |
| 105 | +./build/gpt2 --model gpt2-medium "Once upon a time" -n 20 |
| 106 | +``` |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | +## What you've learned and what's next |
| 111 | + |
| 112 | +You now have a working baseline binary and model files. Next, you will use the Instruction Mix recipe in Arm Performix to inspect static disassembly and dynamic runtime behavior. |
0 commit comments