Skip to content

Commit 1efcd62

Browse files
authored
Merge branch 'main' into fix/3855-deprecated-inplace-ops
2 parents bdb3186 + 1655d0a commit 1efcd62

9 files changed

Lines changed: 893 additions & 50 deletions

File tree

beginner_source/blitz/cifar10_tutorial.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"""
5858
import torch
5959
import torchvision
60-
import torchvision.transforms as transforms
60+
from torchvision.transforms import v2
6161

6262
########################################################################
6363
# The output of torchvision datasets are PILImage images of range [0, 1].
@@ -69,9 +69,10 @@
6969
# BrokenPipeError or RuntimeError related to multiprocessing, try setting
7070
# the num_worker of torch.utils.data.DataLoader() to 0.
7171

72-
transform = transforms.Compose(
73-
[transforms.ToTensor(),
74-
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
72+
transform = v2.Compose([
73+
v2.ToImage(),
74+
v2.ToDtype(torch.float32, scale=True),
75+
v2.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
7576

7677
batch_size = 4
7778

@@ -191,7 +192,7 @@ def forward(self, x):
191192
########################################################################
192193
# Let's quickly save our trained model:
193194

194-
PATH = './cifar_net.pth'
195+
PATH = './cifar_net.pt'
195196
torch.save(net.state_dict(), PATH)
196197

197198
########################################################################
@@ -302,7 +303,7 @@ def forward(self, x):
302303
# Let's first define our device as the first visible cuda device if we have
303304
# CUDA available:
304305

305-
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
306+
device = torch.device(torch.accelerator.current_accelerator().type if torch.accelerator.is_available() else 'cpu')
306307

307308
# Assuming that we are on a CUDA machine, this should print a CUDA device:
308309

@@ -355,9 +356,9 @@ def forward(self, x):
355356
# - `Discuss PyTorch on the Forums`_
356357
# - `Chat with other users on Slack`_
357358
#
358-
# .. _Train a state-of-the-art ResNet network on imagenet: https://github.com/pytorch/examples/tree/master/imagenet
359-
# .. _Train a face generator using Generative Adversarial Networks: https://github.com/pytorch/examples/tree/master/dcgan
360-
# .. _Train a word-level language model using Recurrent LSTM networks: https://github.com/pytorch/examples/tree/master/word_language_model
359+
# .. _Train a state-of-the-art ResNet network on imagenet: https://github.com/pytorch/examples/tree/main/imagenet
360+
# .. _Train a face generator using Generative Adversarial Networks: https://github.com/pytorch/examples/tree/main/dcgan
361+
# .. _Train a word-level language model using Recurrent LSTM networks: https://github.com/pytorch/examples/tree/main/word_language_model
361362
# .. _More examples: https://github.com/pytorch/examples
362363
# .. _More tutorials: https://github.com/pytorch/tutorials
363364
# .. _Discuss PyTorch on the Forums: https://discuss.pytorch.org/

beginner_source/examples_tensor/polynomial_tensor.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@
1919
"""
2020

2121
import torch
22-
import math
23-
2422

2523
dtype = torch.float
2624
device = torch.device("cpu")
2725
# device = torch.device("cuda:0") # Uncomment this to run on GPU
2826

2927
# Create random input and output data
30-
x = torch.linspace(-math.pi, math.pi, 2000, device=device, dtype=dtype)
28+
x = torch.linspace(-torch.pi, torch.pi, 2000, device=device, dtype=dtype)
3129
y = torch.sin(x)
3230

3331
# Randomly initialize weights

beginner_source/introyt/captumyt.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,7 @@
106106
- Matplotlib version 3.3.4, since Captum currently uses a Matplotlib
107107
function whose arguments have been renamed in later versions
108108
109-
To install Captum in an Anaconda or pip virtual environment, use the
110-
appropriate command for your environment below:
111-
112-
With ``conda``:
113-
114-
.. code-block:: sh
115-
116-
conda install pytorch torchvision captum flask-compress matplotlib=3.3.4 -c pytorch
117-
118-
With ``pip``:
109+
To install Captum in a virtual environment, use:
119110
120111
.. code-block:: sh
121112

beginner_source/introyt/tensorboardyt_tutorial.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,6 @@
2424
To run this tutorial, you’ll need to install PyTorch, TorchVision,
2525
Matplotlib, and TensorBoard.
2626
27-
With ``conda``:
28-
29-
.. code-block:: sh
30-
31-
conda install pytorch torchvision -c pytorch
32-
conda install matplotlib tensorboard
33-
3427
With ``pip``:
3528
3629
.. code-block:: sh

beginner_source/understanding_leaf_vs_nonleaf_tutorial.py

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,26 @@
8787
# \cdots \cdot
8888
# \frac{\partial \mathbf{f}_1}{\partial \mathbf{x}}
8989
#
90-
# .. figure:: /_static/img/understanding_leaf_vs_nonleaf/comp-graph-1.png
91-
# :alt: Computational graph after forward pass
92-
#
93-
# Computational graph after forward pass
94-
#
90+
# .. mermaid::
91+
#
92+
# graph TD
93+
#
94+
# x["x<br/>is_leaf=True<br/>requires_grad=False<br/>retains_grad=False<br/>grad=None"]
95+
# W["W<br/>is_leaf=True<br/>requires_grad=True<br/>retains_grad=False<br/>grad=None"]
96+
# b["b<br/>is_leaf=True<br/>requires_grad=True<br/>retains_grad=False<br/>grad=None"]
97+
# matmul["x @ W"]
98+
# z["z = x @ W + b<br/>is_leaf=False<br/>requires_grad=True<br/>retains_grad=False<br/>grad=None"]
99+
# relu["y_pred = relu(z)<br/>is_leaf=False<br/>requires_grad=True<br/>retains_grad=False<br/>grad=None"]
100+
# y["y<br/>is_leaf=True<br/>requires_grad=False<br/>retains_grad=False<br/>grad=None"]
101+
# loss["loss = mse(y_pred, y)<br/>is_leaf=False<br/>requires_grad=True<br/>retains_grad=False<br/>grad=None"]
102+
#
103+
# x --> matmul
104+
# W --> matmul
105+
# matmul --> z
106+
# b --> z
107+
# z --> relu
108+
# relu --> loss
109+
# y --> loss
95110
# PyTorch considers a node to be a *leaf* if it is not the result of a
96111
# tensor operation with at least one input having ``requires_grad=True``
97112
# (e.g. ``x``, ``W``, ``b``, and ``y``), and everything else to be
@@ -260,11 +275,26 @@
260275
# convention, this attribute will print ``False`` for any leaf node, even
261276
# if it requires its gradient.
262277
#
263-
# .. figure:: /_static/img/understanding_leaf_vs_nonleaf/comp-graph-2.png
264-
# :alt: Computational graph after backward pass
265-
#
266-
# Computational graph after backward pass
267-
#
278+
# .. mermaid::
279+
#
280+
# graph TD
281+
#
282+
# x["x<br/>is_leaf=True<br/>requires_grad=False<br/>retains_grad=False<br/>grad=None"]
283+
# W["W<br/>is_leaf=True<br/>requires_grad=True<br/>retains_grad=False<br/>grad=torch.Tensor"]
284+
# b["b<br/>is_leaf=True<br/>requires_grad=True<br/>retains_grad=False<br/>grad=torch.Tensor"]
285+
# matmul["x @ W"]
286+
# z["z = x @ W + b<br/>is_leaf=False<br/>requires_grad=True<br/>retains_grad=True<br/>grad=torch.Tensor"]
287+
# relu["y_pred = relu(z)<br/>is_leaf=False<br/>requires_grad=True<br/>retains_grad=True<br/>grad=torch.Tensor"]
288+
# y["y<br/>is_leaf=True<br/>requires_grad=True<br/>retains_grad=False<br/>grad=None"]
289+
# loss["loss = mse(y_pred, y)<br/>is_leaf=False<br/>requires_grad=True<br/>retains_grad=True<br/>grad=torch.Tensor"]
290+
#
291+
# x --> matmul
292+
# W --> matmul
293+
# matmul --> z
294+
# b --> z
295+
# z --> relu
296+
# relu --> loss
297+
# y --> loss
268298
# If you call ``retain_grad()`` on a leaf tensor, it results in a no-op
269299
# since leaf tensors already retain their gradients by default (when
270300
# ``requires_grad=True``).

index.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ Welcome to PyTorch Tutorials
103103
:link: intermediate/pinmem_nonblock.html
104104
:tags: Getting-Started
105105

106+
.. customcarditem::
107+
:header: Data Loading Optimization in PyTorch
108+
:card_description: Optimize DataLoader configuration with num_workers, pin_memory, persistent_workers for maximum training throughput.
109+
:image: _static/img/thumbnails/cropped/generic-pytorch-logo.png
110+
:link: intermediate/intermediate_data_loading_tutorial.html
111+
:tags: Getting-Started,Best-Practice
112+
106113
.. customcarditem::
107114
:header: Understanding requires_grad, retain_grad, Leaf, and Non-leaf Tensors
108115
:card_description: Learn the subtleties of requires_grad, retain_grad, leaf, and non-leaf tensors

0 commit comments

Comments
 (0)