Skip to content

Commit 9c6c2b3

Browse files
committed
Automated tutorials push
1 parent 6943c77 commit 9c6c2b3

File tree

200 files changed

+13209
-13041
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

200 files changed

+13209
-13041
lines changed

_downloads/1ac8a049de0513cb49a0e834e4c27a20/regional_compilation.py

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,37 +30,37 @@
3030
to prevent recompilations during regional compilation. In version 2.5, this flag is enabled by default.
3131
"""
3232

33-
33+
from time import perf_counter
3434

3535
######################################################################
3636
# Steps
3737
# -----
38-
#
38+
#
3939
# In this recipe, we will follow these steps:
4040
#
4141
# 1. Import all necessary libraries.
4242
# 2. Define and initialize a neural network with repeated regions.
4343
# 3. Understand the difference between the full model and the regional compilation.
4444
# 4. Measure the compilation time of the full model and the regional compilation.
45-
#
46-
# First, let's import the necessary libraries for loading our data:
47-
#
48-
#
49-
#
45+
#
46+
# First, let's import the necessary libraries for loading our data:
47+
#
48+
#
49+
#
5050

5151
import torch
5252
import torch.nn as nn
53-
from time import perf_counter
53+
5454

5555
##########################################################
5656
# Next, let's define and initialize a neural network with repeated regions.
57-
#
57+
#
5858
# Typically, neural networks are composed of repeated layers. For example, a
5959
# large language model is composed of many Transformer blocks. In this recipe,
6060
# we will create a ``Layer`` using the ``nn.Module`` class as a proxy for a repeated region.
6161
# We will then create a ``Model`` which is composed of 64 instances of this
6262
# ``Layer`` class.
63-
#
63+
#
6464
class Layer(torch.nn.Module):
6565
def __init__(self):
6666
super().__init__()
@@ -77,13 +77,16 @@ def forward(self, x):
7777
b = self.relu2(b)
7878
return b
7979

80+
8081
class Model(torch.nn.Module):
8182
def __init__(self, apply_regional_compilation):
8283
super().__init__()
8384
self.linear = torch.nn.Linear(10, 10)
8485
# Apply compile only to the repeated layers.
8586
if apply_regional_compilation:
86-
self.layers = torch.nn.ModuleList([torch.compile(Layer()) for _ in range(64)])
87+
self.layers = torch.nn.ModuleList(
88+
[torch.compile(Layer()) for _ in range(64)]
89+
)
8790
else:
8891
self.layers = torch.nn.ModuleList([Layer() for _ in range(64)])
8992

@@ -94,15 +97,16 @@ def forward(self, x):
9497
x = layer(x)
9598
return x
9699

100+
97101
####################################################
98102
# Next, let's review the difference between the full model and the regional compilation.
99-
#
100-
# In full model compilation, the entire model is compiled as a whole. This is the common approach
103+
#
104+
# In full model compilation, the entire model is compiled as a whole. This is the common approach
101105
# most users take with ``torch.compile``. In this example, we apply ``torch.compile`` to
102106
# the ``Model`` object. This will effectively inline the 64 layers, producing a
103107
# large graph to compile. You can look at the full graph by running this recipe
104108
# with ``TORCH_LOGS=graph_code``.
105-
#
109+
#
106110
#
107111

108112
model = Model(apply_regional_compilation=False).cuda()
@@ -114,19 +118,19 @@ def forward(self, x):
114118
# By strategically choosing to compile a repeated region of the model, we can compile a
115119
# much smaller graph and then reuse the compiled graph for all the regions.
116120
# In the example, ``torch.compile`` is applied only to the ``layers`` and not the full model.
117-
#
121+
#
118122

119123
regional_compiled_model = Model(apply_regional_compilation=True).cuda()
120124

121125
#####################################################
122126
# Applying compilation to a repeated region, instead of full model, leads to
123127
# large savings in compile time. Here, we will just compile a layer instance and
124128
# then reuse it 64 times in the ``Model`` object.
125-
#
129+
#
126130
# Note that with repeated regions, some part of the model might not be compiled.
127131
# For example, the ``self.linear`` in the ``Model`` is outside of the scope of
128132
# regional compilation.
129-
#
133+
#
130134
# Also, note that there is a tradeoff between performance speedup and compile
131135
# time. Full model compilation involves a larger graph and,
132136
# theoretically, offers more scope for optimizations. However, for practical
@@ -138,10 +142,11 @@ def forward(self, x):
138142
# Next, let's measure the compilation time of the full model and the regional compilation.
139143
#
140144
# ``torch.compile`` is a JIT compiler, which means that it compiles on the first invocation.
141-
# In the code below, we measure the total time spent in the first invocation. While this method is not
145+
# In the code below, we measure the total time spent in the first invocation. While this method is not
142146
# precise, it provides a good estimate since the majority of the time is spent in
143147
# compilation.
144148

149+
145150
def measure_latency(fn, input):
146151
# Reset the compiler caches to ensure no reuse between different runs
147152
torch.compiler.reset()
@@ -152,13 +157,16 @@ def measure_latency(fn, input):
152157
end = perf_counter()
153158
return end - start
154159

160+
155161
input = torch.randn(10, 10, device="cuda")
156162
full_model_compilation_latency = measure_latency(full_compiled_model, input)
157163
print(f"Full model compilation time = {full_model_compilation_latency:.2f} seconds")
158164

159165
regional_compilation_latency = measure_latency(regional_compiled_model, input)
160166
print(f"Regional compilation time = {regional_compilation_latency:.2f} seconds")
161167

168+
assert regional_compilation_latency < full_model_compilation_latency
169+
162170
############################################################################
163171
# Conclusion
164172
# -----------
@@ -167,4 +175,4 @@ def measure_latency(fn, input):
167175
# has repeated regions. This approach requires user modifications to apply `torch.compile` to
168176
# the repeated regions instead of more commonly used full model compilation. We
169177
# are continually working on reducing cold start compilation time.
170-
#
178+
#

_downloads/3195443a0ced3cabc0ad643537bdb5cd/introyt1_tutorial.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
{
3535
"cell_type": "code",
3636
"execution_count": null,
37-
"id": "4dac0422",
37+
"id": "6d7882b6",
3838
"metadata": {},
3939
"outputs": [],
4040
"source": [
@@ -50,7 +50,7 @@
5050
},
5151
{
5252
"cell_type": "markdown",
53-
"id": "d9d3c3d4",
53+
"id": "12d9f983",
5454
"metadata": {},
5555
"source": [
5656
"\n",

_downloads/4355e2cef7d17548f1e25f97a62828c4/template_tutorial.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
{
3232
"cell_type": "code",
3333
"execution_count": null,
34-
"id": "335f8dc2",
34+
"id": "cf54a5d8",
3535
"metadata": {},
3636
"outputs": [],
3737
"source": [
@@ -47,7 +47,7 @@
4747
},
4848
{
4949
"cell_type": "markdown",
50-
"id": "88467d9a",
50+
"id": "aea90132",
5151
"metadata": {},
5252
"source": [
5353
"\n",

_downloads/63a0f0fc7b3ffb15d3a5ac8db3d521ee/tensors_deeper_tutorial.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
{
3535
"cell_type": "code",
3636
"execution_count": null,
37-
"id": "dafe94d5",
37+
"id": "3070dd14",
3838
"metadata": {},
3939
"outputs": [],
4040
"source": [
@@ -50,7 +50,7 @@
5050
},
5151
{
5252
"cell_type": "markdown",
53-
"id": "88fe8d2c",
53+
"id": "cd002842",
5454
"metadata": {},
5555
"source": [
5656
"\n",

_downloads/770632dd3941d2a51b831c52ded57aa2/trainingyt.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
{
3636
"cell_type": "code",
3737
"execution_count": null,
38-
"id": "1df017eb",
38+
"id": "908900d2",
3939
"metadata": {},
4040
"outputs": [],
4141
"source": [
@@ -51,7 +51,7 @@
5151
},
5252
{
5353
"cell_type": "markdown",
54-
"id": "578bf3ae",
54+
"id": "95b9be6b",
5555
"metadata": {},
5656
"source": [
5757
"\n",

_downloads/c28f42852d456daf9af72da6c6909556/captumyt.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
{
3838
"cell_type": "code",
3939
"execution_count": null,
40-
"id": "e7fbf414",
40+
"id": "a040408b",
4141
"metadata": {},
4242
"outputs": [],
4343
"source": [
@@ -53,7 +53,7 @@
5353
},
5454
{
5555
"cell_type": "markdown",
56-
"id": "3d96f2c9",
56+
"id": "8718e844",
5757
"metadata": {},
5858
"source": [
5959
"\n",

_downloads/cbd5804c4553cb4a23dc24137bde6077/regional_compilation.ipynb

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@
5050
"</div>\n"
5151
]
5252
},
53+
{
54+
"cell_type": "code",
55+
"execution_count": null,
56+
"metadata": {
57+
"collapsed": false
58+
},
59+
"outputs": [],
60+
"source": [
61+
"from time import perf_counter"
62+
]
63+
},
5364
{
5465
"cell_type": "markdown",
5566
"metadata": {},
@@ -78,8 +89,7 @@
7889
"outputs": [],
7990
"source": [
8091
"import torch\n",
81-
"import torch.nn as nn\n",
82-
"from time import perf_counter"
92+
"import torch.nn as nn"
8393
]
8494
},
8595
{
@@ -120,13 +130,16 @@
120130
" b = self.relu2(b)\n",
121131
" return b\n",
122132
"\n",
133+
"\n",
123134
"class Model(torch.nn.Module):\n",
124135
" def __init__(self, apply_regional_compilation):\n",
125136
" super().__init__()\n",
126137
" self.linear = torch.nn.Linear(10, 10)\n",
127138
" # Apply compile only to the repeated layers.\n",
128139
" if apply_regional_compilation:\n",
129-
" self.layers = torch.nn.ModuleList([torch.compile(Layer()) for _ in range(64)])\n",
140+
" self.layers = torch.nn.ModuleList(\n",
141+
" [torch.compile(Layer()) for _ in range(64)]\n",
142+
" )\n",
130143
" else:\n",
131144
" self.layers = torch.nn.ModuleList([Layer() for _ in range(64)])\n",
132145
"\n",
@@ -238,12 +251,15 @@
238251
" end = perf_counter()\n",
239252
" return end - start\n",
240253
"\n",
254+
"\n",
241255
"input = torch.randn(10, 10, device=\"cuda\")\n",
242256
"full_model_compilation_latency = measure_latency(full_compiled_model, input)\n",
243257
"print(f\"Full model compilation time = {full_model_compilation_latency:.2f} seconds\")\n",
244258
"\n",
245259
"regional_compilation_latency = measure_latency(regional_compiled_model, input)\n",
246-
"print(f\"Regional compilation time = {regional_compilation_latency:.2f} seconds\")"
260+
"print(f\"Regional compilation time = {regional_compilation_latency:.2f} seconds\")\n",
261+
"\n",
262+
"assert regional_compilation_latency < full_model_compilation_latency"
247263
]
248264
},
249265
{

_downloads/e2e556f6b4693c2cef716dd7f40caaf6/tensorboardyt_tutorial.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
{
3636
"cell_type": "code",
3737
"execution_count": null,
38-
"id": "cf3c8e39",
38+
"id": "91ec9983",
3939
"metadata": {},
4040
"outputs": [],
4141
"source": [
@@ -51,7 +51,7 @@
5151
},
5252
{
5353
"cell_type": "markdown",
54-
"id": "a26c0941",
54+
"id": "d4edb552",
5555
"metadata": {},
5656
"source": [
5757
"\n",

_downloads/ed9d4f94afb79f7dada6742a06c486a5/autogradyt_tutorial.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
{
3535
"cell_type": "code",
3636
"execution_count": null,
37-
"id": "6a4d7c89",
37+
"id": "73b180de",
3838
"metadata": {},
3939
"outputs": [],
4040
"source": [
@@ -50,7 +50,7 @@
5050
},
5151
{
5252
"cell_type": "markdown",
53-
"id": "99be565a",
53+
"id": "f9ecb929",
5454
"metadata": {},
5555
"source": [
5656
"\n",

_downloads/fe726e041160526cf828806536922cf6/modelsyt_tutorial.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
{
3535
"cell_type": "code",
3636
"execution_count": null,
37-
"id": "3d8b7f84",
37+
"id": "d7244383",
3838
"metadata": {},
3939
"outputs": [],
4040
"source": [
@@ -50,7 +50,7 @@
5050
},
5151
{
5252
"cell_type": "markdown",
53-
"id": "3c95a1fd",
53+
"id": "2201f142",
5454
"metadata": {},
5555
"source": [
5656
"\n",

0 commit comments

Comments
 (0)