"This notebook stores a bunch of analysis about a Transformer, e.g. estimates the number of FLOPs, parameters, peak memory footprint, checkpoint size, etc."
"We can also estimate the ratio of our GPU memory that will be taken up just by the weights and the buffers inside the AdamW optimizer"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"memory ratio taken up just for parameters: 3.73%\n"
]
}
],
"source": [
"gpu_memory = 40e9 # 40 GB A100 GPU, roughly\n",
"print(f\"memory ratio taken up just for parameters: {params_and_buffers_bytes / gpu_memory * 100:.2f}%\")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"i.e. not that much of the memory for this tiny model, most of the memory is activations (forward and backward). This of course changes dramatically for larger and larger models."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's estimate FLOPs for a single forward pass."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"name flops ratio (%) \n",
"attention/kqv 3623878656 1.2426\n",
"attention/scores 1610612736 0.5522\n",
"attention/reduce 1610612736 0.5522\n",
"attention/proj 1207959552 0.4142\n",
"attention 8053063680 2.7612\n",
"mlp/ffw1 4831838208 1.6567\n",
"mlp/ffw2 4831838208 1.6567\n",
"mlp 9663676416 3.3135\n",
"block 17716740096 6.0747\n",
"transformer 212600881152 72.8963\n",
"dense 79047426048 27.1037\n",
"forward_total 291648307200 100.0000\n",
"backward_total 583296614400 200.0000\n",
"total 874944921600 300.0000\n"
]
}
],
"source": [
"def flops():\n",
" # we only count Weight FLOPs, all other layers (LayerNorm, Softmax, etc) are effectively irrelevant\n",
" # we count actual FLOPs, not MACs. Hence 2* all over the place\n",
" # basically for any matrix multiply A (BxC) @ B (CxD) -> (BxD) flops are 2*B*C*D\n",
"Ok they are quite similar, giving some confidence that my math in flops() function was ~ok. Now, A100 is cited at 312TFLOPS bfloat16 on tensor cores. So what is our model flops utilization (MFU)? I trained the model above with a batch_size of 20 and grad_accum of 5, which runs in about 755ms on a single A100 GPU. We get:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"fraction of A100 used: 37.14%\n"
]
}
],
"source": [
"# here is what we currently roughly measure\n",
"batch_size = 20 * 5 # 5 is grad_accum, so total batch size is 100\n",
"measured_time = 0.755 # in seconds per iteration\n",
"# A100 is cited to be 312 TFLOPS of bloat16 running on tensor cores\n",
"a100_flops_promised = 312e12\n",
"\n",
"# the fraction of the A100 that we are using:\n",
"print(f\"fraction of A100 used: {flops_achieved / a100_flops_promised * 100:.2f}%\")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"For reference, we'd prefer to be somewhere around 50%+, and not just for a single GPU but for an entire DDP run. So we still have some work to do, but at least we're within a factor of ~2X of what is achievable with this GPU."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"time needed to train the model: 3.46 days\n"
]
}
],
"source": [
"# Finally let's check out the 6ND approximation as total cost of training in FLOPs\n",
"model_size = params()['total'] # this is number of parameters, N\n",
"tokens_num = 300e9 # 300B tokens, this is dataset size in tokens, D\n",
"a100_flops = 312e12 # 312 TFLOPS\n",
"assumed_mfu = 0.3 # assume this model flops utilization (take the current 37% from above and add some DDP overhead)\n",
"This is not a bad estimate at all. I trained this model and it converged in roughly 4 days. Btw as a good reference for where 6ND comes from and some intuition around it I recommend [Dzmitry's post](https://medium.com/@dzmitrybahdanau/the-flops-calculus-of-language-model-training-3b19c1f025e4)."
"Now, FLOPs are just one constraint, the other that we have to keep a close track of is the memory bandwidth. TODO estimate LOAD/STORE costs of our model later."