Skip to content

Commit 439eced

Browse files
committed
Automated tutorials push
1 parent eaecaca commit 439eced

File tree

188 files changed

+15422
-17005
lines changed

Some content is hidden

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

188 files changed

+15422
-17005
lines changed

_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": "2257e813",
37+
"id": "78549cf8",
3838
"metadata": {},
3939
"outputs": [],
4040
"source": [
@@ -50,7 +50,7 @@
5050
},
5151
{
5252
"cell_type": "markdown",
53-
"id": "4229a50a",
53+
"id": "334b099e",
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": "7d03b8e9",
34+
"id": "4eb5052c",
3535
"metadata": {},
3636
"outputs": [],
3737
"source": [
@@ -47,7 +47,7 @@
4747
},
4848
{
4949
"cell_type": "markdown",
50-
"id": "bb240c29",
50+
"id": "6ca084e5",
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": "b39ee222",
37+
"id": "b8797209",
3838
"metadata": {},
3939
"outputs": [],
4040
"source": [
@@ -50,7 +50,7 @@
5050
},
5151
{
5252
"cell_type": "markdown",
53-
"id": "b3c105e6",
53+
"id": "4f3d0736",
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": "beda72ee",
38+
"id": "468a12ad",
3939
"metadata": {},
4040
"outputs": [],
4141
"source": [
@@ -51,7 +51,7 @@
5151
},
5252
{
5353
"cell_type": "markdown",
54-
"id": "e850402a",
54+
"id": "ba87fbfd",
5555
"metadata": {},
5656
"source": [
5757
"\n",
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"""
2+
(beta) Utilizing Torch Function modes with torch.compile
3+
============================================================
4+
5+
**Author:** `Michael Lazos <https://github.com/mlazos>`_
6+
"""
7+
8+
#########################################################
9+
# This recipe covers how to use a key torch extensibility point,
10+
# torch function modes, in tandem with ``torch.compile`` to override
11+
# the behavior of torch operators, also know as **ops**, at trace time, with no runtime overhead.
12+
#
13+
# .. note::
14+
#
15+
# This recipe requires PyTorch 2.7.0 or later.
16+
17+
18+
#####################################################################
19+
# Rewriting a torch op (torch.add -> torch.mul)
20+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21+
# For this example, we'll use torch function modes to rewrite occurences
22+
# of addition with multiply instead. This type of override can be common
23+
# if a certain backend has a custom implementation that should be dispatched
24+
# for a given op.
25+
import torch
26+
27+
# exit cleanly if we are on a device that doesn't support ``torch.compile``
28+
if torch.cuda.get_device_capability() < (7, 0):
29+
print("Exiting because torch.compile is not supported on this device.")
30+
import sys
31+
sys.exit(0)
32+
33+
from torch.overrides import BaseTorchFunctionMode
34+
35+
# Define our mode, Note: ``BaseTorchFunctionMode``
36+
# implements the actual invocation of func(..)
37+
class AddToMultiplyMode(BaseTorchFunctionMode):
38+
def __torch_function__(self, func, types, args=(), kwargs=None):
39+
if func == torch.Tensor.add:
40+
func = torch.mul
41+
42+
return super().__torch_function__(func, types, args, kwargs)
43+
44+
@torch.compile()
45+
def test_fn(x, y):
46+
return x + y * x # Note: infix operators map to torch.Tensor.* methods
47+
48+
x = torch.rand(2, 2)
49+
y = torch.rand_like(x)
50+
51+
with AddToMultiplyMode():
52+
z = test_fn(x, y)
53+
54+
assert torch.allclose(z, x * y * x)
55+
56+
# The mode can also be used within the compiled region as well like this:
57+
58+
@torch.compile()
59+
def test_fn(x, y):
60+
with AddToMultiplyMode():
61+
return x + y * x # Note: infix operators map to torch.Tensor.* methods
62+
63+
x = torch.rand(2, 2)
64+
y = torch.rand_like(x)
65+
z = test_fn(x, y)
66+
67+
assert torch.allclose(z, x * y * x)
68+
69+
######################################################################
70+
# Conclusion
71+
# ~~~~~~~~~~
72+
# In this recipe we demonstrated how to override the behavior of ``torch.*`` operators
73+
# using torch function modes from within ``torch.compile``. This enables users to utilize
74+
# the extensibility benefits of torch function modes without the runtime overhead
75+
# of calling torch function on every op invocation.
76+
#
77+
# * See `Extending Torch API with Modes <https://pytorch.org/docs/stable/notes/extending.html#extending-all-torch-api-with-modes>`__ for other examples and background on Torch Function modes.

_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": "ff465b13",
40+
"id": "1de66073",
4141
"metadata": {},
4242
"outputs": [],
4343
"source": [
@@ -53,7 +53,7 @@
5353
},
5454
{
5555
"cell_type": "markdown",
56-
"id": "4f85e802",
56+
"id": "517c081c",
5757
"metadata": {},
5858
"source": [
5959
"\n",
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {
7+
"collapsed": false
8+
},
9+
"outputs": [],
10+
"source": [
11+
"# For tips on running notebooks in Google Colab, see\n",
12+
"# https://pytorch.org/tutorials/beginner/colab\n",
13+
"%matplotlib inline"
14+
]
15+
},
16+
{
17+
"cell_type": "markdown",
18+
"metadata": {},
19+
"source": [
20+
"(beta) Utilizing Torch Function modes with torch.compile\n",
21+
"========================================================\n",
22+
"\n",
23+
"**Author:** [Michael Lazos](https://github.com/mlazos)\n"
24+
]
25+
},
26+
{
27+
"cell_type": "markdown",
28+
"metadata": {},
29+
"source": [
30+
"This recipe covers how to use a key torch extensibility point,\n",
31+
"\n",
32+
": torch function modes, in tandem with `torch.compile` to override the\n",
33+
" behavior of torch operators, also know as **ops**, at trace time,\n",
34+
" with no runtime overhead.\n",
35+
"\n",
36+
"<div style=\"background-color: #54c7ec; color: #fff; font-weight: 700; padding-left: 10px; padding-top: 5px; padding-bottom: 5px\"><strong>NOTE:</strong></div>\n",
37+
"\n",
38+
"<div style=\"background-color: #f3f4f7; padding-left: 10px; padding-top: 10px; padding-bottom: 10px; padding-right: 10px\">\n",
39+
"\n",
40+
"<p>This recipe requires PyTorch 2.7.0 or later.</p>\n",
41+
"\n",
42+
"</div>\n",
43+
"\n"
44+
]
45+
},
46+
{
47+
"cell_type": "markdown",
48+
"metadata": {},
49+
"source": [
50+
"Rewriting a torch op (torch.add -\\> torch.mul)\n",
51+
"==============================================\n",
52+
"\n",
53+
"For this example, we\\'ll use torch function modes to rewrite occurences\n",
54+
"of addition with multiply instead. This type of override can be common\n",
55+
"if a certain backend has a custom implementation that should be\n",
56+
"dispatched for a given op.\n"
57+
]
58+
},
59+
{
60+
"cell_type": "code",
61+
"execution_count": null,
62+
"metadata": {
63+
"collapsed": false
64+
},
65+
"outputs": [],
66+
"source": [
67+
"import torch\n",
68+
"\n",
69+
"# exit cleanly if we are on a device that doesn't support ``torch.compile``\n",
70+
"if torch.cuda.get_device_capability() < (7, 0):\n",
71+
" print(\"Exiting because torch.compile is not supported on this device.\")\n",
72+
" import sys\n",
73+
" sys.exit(0)\n",
74+
"\n",
75+
"from torch.overrides import BaseTorchFunctionMode\n",
76+
"\n",
77+
"# Define our mode, Note: ``BaseTorchFunctionMode``\n",
78+
"# implements the actual invocation of func(..)\n",
79+
"class AddToMultiplyMode(BaseTorchFunctionMode):\n",
80+
" def __torch_function__(self, func, types, args=(), kwargs=None):\n",
81+
" if func == torch.Tensor.add:\n",
82+
" func = torch.mul\n",
83+
"\n",
84+
" return super().__torch_function__(func, types, args, kwargs)\n",
85+
"\n",
86+
"@torch.compile()\n",
87+
"def test_fn(x, y):\n",
88+
" return x + y * x # Note: infix operators map to torch.Tensor.* methods\n",
89+
"\n",
90+
"x = torch.rand(2, 2)\n",
91+
"y = torch.rand_like(x)\n",
92+
"\n",
93+
"with AddToMultiplyMode():\n",
94+
" z = test_fn(x, y)\n",
95+
"\n",
96+
"assert torch.allclose(z, x * y * x)\n",
97+
"\n",
98+
"# The mode can also be used within the compiled region as well like this:\n",
99+
"\n",
100+
"@torch.compile()\n",
101+
"def test_fn(x, y):\n",
102+
" with AddToMultiplyMode():\n",
103+
" return x + y * x # Note: infix operators map to torch.Tensor.* methods\n",
104+
"\n",
105+
"x = torch.rand(2, 2)\n",
106+
"y = torch.rand_like(x)\n",
107+
"z = test_fn(x, y)\n",
108+
"\n",
109+
"assert torch.allclose(z, x * y * x)"
110+
]
111+
},
112+
{
113+
"cell_type": "markdown",
114+
"metadata": {},
115+
"source": [
116+
"Conclusion\n",
117+
"==========\n",
118+
"\n",
119+
"In this recipe we demonstrated how to override the behavior of `torch.*`\n",
120+
"operators using torch function modes from within `torch.compile`. This\n",
121+
"enables users to utilize the extensibility benefits of torch function\n",
122+
"modes without the runtime overhead of calling torch function on every op\n",
123+
"invocation.\n",
124+
"\n",
125+
"- See [Extending Torch API with\n",
126+
" Modes](https://pytorch.org/docs/stable/notes/extending.html#extending-all-torch-api-with-modes)\n",
127+
" for other examples and background on Torch Function modes.\n"
128+
]
129+
}
130+
],
131+
"metadata": {
132+
"kernelspec": {
133+
"display_name": "Python 3",
134+
"language": "python",
135+
"name": "python3"
136+
},
137+
"language_info": {
138+
"codemirror_mode": {
139+
"name": "ipython",
140+
"version": 3
141+
},
142+
"file_extension": ".py",
143+
"mimetype": "text/x-python",
144+
"name": "python",
145+
"nbconvert_exporter": "python",
146+
"pygments_lexer": "ipython3",
147+
"version": "3.10.12"
148+
}
149+
},
150+
"nbformat": 4,
151+
"nbformat_minor": 0
152+
}

_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": "b6c75a4e",
38+
"id": "0cef40b6",
3939
"metadata": {},
4040
"outputs": [],
4141
"source": [
@@ -51,7 +51,7 @@
5151
},
5252
{
5353
"cell_type": "markdown",
54-
"id": "b26293ff",
54+
"id": "77d7e33e",
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": "8b819073",
37+
"id": "96039037",
3838
"metadata": {},
3939
"outputs": [],
4040
"source": [
@@ -50,7 +50,7 @@
5050
},
5151
{
5252
"cell_type": "markdown",
53-
"id": "8aa35bc7",
53+
"id": "7b537661",
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": "90c9c9d8",
37+
"id": "2e6fa5b0",
3838
"metadata": {},
3939
"outputs": [],
4040
"source": [
@@ -50,7 +50,7 @@
5050
},
5151
{
5252
"cell_type": "markdown",
53-
"id": "c7bda98f",
53+
"id": "c0c84734",
5454
"metadata": {},
5555
"source": [
5656
"\n",

0 commit comments

Comments
 (0)