Spaces:
Running
Running
File size: 4,980 Bytes
f2c15d5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from utils import activation_memory, param_grads_opt"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [],
"source": [
"def activation_memory(\n",
" a, # attention heads\n",
" b, # micro batch size\n",
" h, # hidden dimension size\n",
" h_ff, # feedforward dimension size (often h_ff = 4h)\n",
" L, # number of layers\n",
" s, # sequence length\n",
" mixed=True,\n",
" recomputation=\"none\",\n",
" ff_activation=\"relu\"\n",
" ):\n",
" \n",
" # https://arxiv.org/pdf/2205.05198\n",
" if mixed:\n",
" bytes_per_value = 2 \n",
" else:\n",
" bytes_per_value = 4\n",
"\n",
" one_layer_attention = s * b * h * (bytes_per_value * 5 + 1) + ((2 * bytes_per_value + 1) * a * s * s * b) # eq (2)\n",
"\n",
" if ff_activation == \"relu\":\n",
" one_layer_feedforward = (s * b * h * bytes_per_value + (s * b * h_ff * bytes_per_value) # inputs of 1st/2nd linear layers\n",
" + s * b * h) # dropout\n",
" elif ff_activation == \"gelu\":\n",
" one_layer_feedforward = (s * b * h * bytes_per_value + (s * b * h_ff * bytes_per_value) # inputs of 1st/2nd linear layers\n",
" + s * b * h_ff * bytes_per_value # inputs of activation function (not really necessary for Relu)\n",
" + s * b * h) # dropout\n",
" elif ff_activation == \"swiglu\":\n",
" one_layer_feedforward = (s * b * h * bytes_per_value + (s * b * h_ff * bytes_per_value) # inputs of input/output linear layers\n",
" + s * b * h_ff * bytes_per_value * 3 # inputs of activation function\n",
" + s * b * h) # dropout (note that dropout is lower-precision - boolean)\n",
"\n",
"\n",
" layer_norm = s * b * h * bytes_per_value\n",
"\n",
" if recomputation == \"none\":\n",
" one_layer = one_layer_attention + one_layer_feedforward + 2 * layer_norm # eq (2)\n",
" elif recomputation ==\"selective\":\n",
" one_layer = s * b * h * 34 # eq (6)\n",
" elif recomputation ==\"full\":\n",
" one_layer = s * b * h * 2\n",
" else:\n",
" raise ValueError()\n",
" \n",
" input_dropout = s * b * h # section 4.3\n",
"\n",
" total = L * one_layer + input_dropout\n",
" \n",
" return total\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [],
"source": [
"a = 16\n",
"b = 3\n",
"h = 1024\n",
"h_ff = 4 * h\n",
"L = 1\n",
"s = 7 # 128000\n",
"recomputation = \"none\"\n",
"mixed = True\n",
"ff_activation = \"swiglu\"\n"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1086960"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"activation_memory(a=a, b=b, h=h, h_ff=h_ff, L=L, s=s, recomputation=recomputation, mixed=mixed, ff_activation=ff_activation)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"from math import log\n",
"\n",
"def format_bytes(bytes):\n",
" sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']\n",
" if bytes == 0:\n",
" return '0 Bytes'\n",
" i = int(log(bytes, 1024))\n",
" print(i)\n",
" p = 1024 ** i\n",
" s = round(bytes / p, 2)\n",
" return f\"{s} {sizes[i]}\"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n"
]
},
{
"data": {
"text/plain": [
"'22.13 TB'"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"format_bytes(activation_memory(a, b, h, L, s, recomputation))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "jupyter",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.14"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|