File size: 11,513 Bytes
87b3d4b
 
 
 
 
 
96b7b35
 
 
 
87b3d4b
 
 
 
 
96b7b35
 
 
 
87b3d4b
 
 
96b7b35
 
 
87b3d4b
 
 
 
 
 
 
 
 
 
96b7b35
87b3d4b
 
 
96b7b35
 
 
87b3d4b
 
 
 
96b7b35
87b3d4b
 
 
 
 
96b7b35
 
 
 
87b3d4b
 
 
96b7b35
 
 
 
 
87b3d4b
 
 
 
 
 
96b7b35
87b3d4b
 
 
96b7b35
87b3d4b
 
 
 
96b7b35
87b3d4b
 
 
 
 
96b7b35
87b3d4b
 
 
 
 
 
 
 
 
96b7b35
 
 
 
 
 
 
 
 
 
 
87b3d4b
 
 
 
 
 
96b7b35
 
 
 
87b3d4b
 
 
96b7b35
87b3d4b
 
 
 
96b7b35
87b3d4b
 
 
 
 
96b7b35
87b3d4b
 
 
 
96b7b35
87b3d4b
 
96b7b35
87b3d4b
 
96b7b35
 
87b3d4b
 
 
 
 
96b7b35
87b3d4b
 
 
 
 
 
96b7b35
 
 
 
 
 
87b3d4b
 
 
 
96b7b35
87b3d4b
 
 
 
 
96b7b35
87b3d4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96b7b35
87b3d4b
 
 
 
96b7b35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87b3d4b
 
 
 
 
 
96b7b35
 
 
 
87b3d4b
 
 
96b7b35
 
 
 
 
87b3d4b
 
 
 
96b7b35
87b3d4b
 
 
96b7b35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87b3d4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96b7b35
 
 
 
 
87b3d4b
 
 
 
96b7b35
 
87b3d4b
 
 
 
96b7b35
87b3d4b
96b7b35
87b3d4b
 
96b7b35
87b3d4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96b7b35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "axYlcDTznci4"
      },
      "source": [
        "# Faster Foundation Models with `torch.compile`"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "B-yw8KMWsjfY"
      },
      "source": [
        "## Introduction to `torch.compile()`"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "AmmT4aDnqgOB"
      },
      "source": [
        "This guide aims to provide a benchmark on the inference speed-ups introduced with `torch.compile()` with no reduction in model performance for foundation models in 🤗 Transformers.\n",
        "\n",
        "Most used `torch.compile` modes are following:\n",
        "\n",
        "- \"default\" is the default mode, which is a good balance between performance and overhead\n",
        "\n",
        "- \"reduce-overhead\" reduces the overhead of python with CUDA graphs, useful for small batches, consumes a lot of memory. As of now only works for CUDA only graphs which do not mutate inputs.\n",
        "\n",
        "If you have a lot of memory to use, the best speed-up is through `reduce-overhead`. How much speed-up one can get depends on the model, so in this tutorial we will check the most used foundation models."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "5sCfbPTn7wBE"
      },
      "source": [
        "## OWLv2\n",
        "\n",
        "OWLv2 is a zero-shot object detection model released by Google Brain. We will load base version."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "joeX3J315K0G"
      },
      "source": [
        "Let's load the model and processor for OWLv2."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "id": "Ztfcdqkul62z"
      },
      "outputs": [],
      "source": [
        "from PIL import Image\n",
        "import requests\n",
        "\n",
        "url = 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg'\n",
        "image = Image.open(requests.get(url, stream=True).raw)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "84npPHCQpHZ6",
        "outputId": "f30c41c7-b897-460d-d2a4-a1276bf2263e"
      },
      "outputs": [
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "/usr/local/lib/python3.10/dist-packages/huggingface_hub/utils/_token.py:89: UserWarning: \n",
            "The secret `HF_TOKEN` does not exist in your Colab secrets.\n",
            "To authenticate with the Hugging Face Hub, create a token in your settings tab (https://huggingface.co/settings/tokens), set it as secret in your Google Colab and restart your session.\n",
            "You will be able to reuse this secret in all of your notebooks.\n",
            "Please note that authentication is recommended but still optional to access public models or datasets.\n",
            "  warnings.warn(\n"
          ]
        }
      ],
      "source": [
        "from transformers import AutoProcessor, Owlv2ForObjectDetection\n",
        "import torch\n",
        "import numpy as np\n",
        "\n",
        "processor = AutoProcessor.from_pretrained(\"google/owlv2-base-patch16-ensemble\")\n",
        "model = Owlv2ForObjectDetection.from_pretrained(\"google/owlv2-base-patch16-ensemble\").to(\"cuda\")\n",
        "\n",
        "texts = [[\"a photo of a bee\", \"a photo of a bird\"]]\n",
        "inputs = processor(text=texts, images=image, return_tensors=\"pt\").to(\"cuda\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "3AedkjLu5PRo"
      },
      "source": [
        "We can now get to benchmarking. We will benchmark the model itself and the compiled model."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "RQQSEgkQtXEV",
        "outputId": "8003590b-c4bc-4b3d-9b1b-dade853b8dd8"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "255.7331792195638\n"
          ]
        }
      ],
      "source": [
        "starter, ender = torch.cuda.Event(enable_timing=True), torch.cuda.Event(enable_timing=True)\n",
        "repetitions = 30\n",
        "timings=np.zeros((repetitions,1))\n",
        "\n",
        "for _ in range(10):\n",
        "    _ = model(**inputs)\n",
        "\n",
        "with torch.no_grad():\n",
        "    for rep in range(repetitions):\n",
        "        torch.cuda.synchronize()\n",
        "        starter.record()\n",
        "        output = model(**inputs)\n",
        "        ender.record()\n",
        "        torch.cuda.synchronize()\n",
        "        curr_time = starter.elapsed_time(ender)\n",
        "        timings[rep] = curr_time\n",
        "\n",
        "mean_syn = np.sum(timings) / repetitions\n",
        "print(mean_syn)\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "bEZiNgaupOx6",
        "outputId": "e5d47875-1e40-4997-e533-94bf0ff34d14"
      },
      "outputs": [
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "/usr/lib/python3.10/multiprocessing/popen_fork.py:66: RuntimeWarning: os.fork() was called. os.fork() is incompatible with multithreaded code, and JAX is multithreaded, so this will likely lead to a deadlock.\n",
            "  self.pid = os.fork()\n",
            "/usr/local/lib/python3.10/dist-packages/torch/_inductor/compile_fx.py:124: UserWarning: TensorFloat32 tensor cores for float32 matrix multiplication available but not enabled. Consider setting `torch.set_float32_matmul_precision('high')` for better performance.\n",
            "  warnings.warn(\n",
            "skipping cudagraphs due to skipping cudagraphs due to cpu device. Found from : \n",
            "   File \"/usr/local/lib/python3.10/dist-packages/transformers/models/owlv2/modeling_owlv2.py\", line 1711, in forward\n",
            "    pred_boxes = self.box_predictor(image_feats, feature_map)\n",
            "  File \"/usr/local/lib/python3.10/dist-packages/transformers/models/owlv2/modeling_owlv2.py\", line 1374, in box_predictor\n",
            "    box_bias = self.box_bias.to(feature_map.device)\n",
            "\n"
          ]
        },
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "154.6884775797526\n"
          ]
        }
      ],
      "source": [
        "starter, ender = torch.cuda.Event(enable_timing=True), torch.cuda.Event(enable_timing=True)\n",
        "timings=np.zeros((repetitions,1))\n",
        "\n",
        "compiled_model = torch.compile(model, mode=\"reduce-overhead\").to(\"cuda\")\n",
        "\n",
        "for _ in range(30):\n",
        "  with torch.no_grad():\n",
        "    _ = compiled_model(**inputs)\n",
        "\n",
        "\n",
        "with torch.no_grad():\n",
        "    for rep in range(repetitions):\n",
        "        torch.cuda.synchronize()\n",
        "        starter.record()\n",
        "        output = compiled_model(**inputs)\n",
        "        ender.record()\n",
        "        torch.cuda.synchronize()\n",
        "        curr_time = starter.elapsed_time(ender)\n",
        "        timings[rep] = curr_time\n",
        "\n",
        "mean_syn = np.sum(timings) / repetitions\n",
        "print(mean_syn)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "d_0d7DwN6gBt"
      },
      "source": [
        "We got nearly 40 percent speed-up! You can also increase the batch size and see how much further speed-up you can get."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "metadata": {
        "id": "exKoOptB61UL"
      },
      "outputs": [],
      "source": [
        "texts = [[\"a photo of a bee\", \"a photo of a bird\"] for _ in range(8)]\n",
        "images = [image for _ in range(8)]\n",
        "inputs = processor(text=texts, images=image, return_tensors=\"pt\").to(\"cuda\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 12,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "EFj9Pgra7Km8",
        "outputId": "5fefb8c0-9e86-478c-e9e2-0dbc0fa8a37b"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "269.3023401896159\n"
          ]
        }
      ],
      "source": [
        "starter, ender = torch.cuda.Event(enable_timing=True), torch.cuda.Event(enable_timing=True)\n",
        "repetitions = 30\n",
        "timings=np.zeros((repetitions,1))\n",
        "\n",
        "for _ in range(10):\n",
        "    _ = model(**inputs)\n",
        "\n",
        "with torch.no_grad():\n",
        "    for rep in range(repetitions):\n",
        "        torch.cuda.synchronize()\n",
        "        starter.record()\n",
        "        output = model(**inputs)\n",
        "        ender.record()\n",
        "        torch.cuda.synchronize()\n",
        "        curr_time = starter.elapsed_time(ender)\n",
        "        timings[rep] = curr_time\n",
        "\n",
        "mean_syn = np.sum(timings) / repetitions\n",
        "print(mean_syn)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 13,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "OuQZmgTK7UCo",
        "outputId": "7184eb1d-b545-4bb6-b544-3effd5c2545a"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "159.77137603759766\n"
          ]
        }
      ],
      "source": [
        "starter, ender = torch.cuda.Event(enable_timing=True), torch.cuda.Event(enable_timing=True)\n",
        "timings=np.zeros((repetitions,1))\n",
        "\n",
        "compiled_model = torch.compile(model, mode=\"reduce-overhead\").to(\"cuda\")\n",
        "\n",
        "for _ in range(30):\n",
        "  with torch.no_grad():\n",
        "    _ = compiled_model(**inputs)\n",
        "\n",
        "\n",
        "with torch.no_grad():\n",
        "    for rep in range(repetitions):\n",
        "        torch.cuda.synchronize()\n",
        "        starter.record()\n",
        "        output = compiled_model(**inputs)\n",
        "        ender.record()\n",
        "        torch.cuda.synchronize()\n",
        "        curr_time = starter.elapsed_time(ender)\n",
        "        timings[rep] = curr_time\n",
        "\n",
        "mean_syn = np.sum(timings) / repetitions\n",
        "print(mean_syn)"
      ]
    }
  ],
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "gpuType": "L4",
      "machine_shape": "hm",
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}