Skip to content

Commit 1f33f46

Browse files
authored
Merge branch 'main' into cpio/flight_recorder
2 parents de5654a + c2a8800 commit 1f33f46

Some content is hidden

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

50 files changed

+814
-73
lines changed

.ci/docker/build.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ IMAGE_NAME="$1"
1111
shift
1212

1313
export UBUNTU_VERSION="20.04"
14+
export CUDA_VERSION="12.4.1"
1415

15-
export BASE_IMAGE="ubuntu:${UBUNTU_VERSION}"
16+
export BASE_IMAGE="nvidia/cuda:${CUDA_VERSION}-devel-ubuntu${UBUNTU_VERSION}"
1617
echo "Building ${IMAGE_NAME} Docker image"
1718

1819
docker build \

.ci/docker/common/common_utils.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ conda_run() {
2222
}
2323

2424
pip_install() {
25-
as_ci_user conda run -n py_$ANACONDA_PYTHON_VERSION pip install --progress-bar off $*
25+
as_ci_user conda run -n py_$ANACONDA_PYTHON_VERSION pip3 install --progress-bar off $*
2626
}

.ci/docker/requirements.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ pytorch-lightning
3030
torchx
3131
torchrl==0.5.0
3232
tensordict==0.5.0
33-
ax-platform>==0.4.0
34-
nbformat>==5.9.2
33+
ax-platform>=0.4.0
34+
nbformat>=5.9.2
3535
datasets
3636
transformers
3737
torchmultimodal-nightly # needs to be updated to stable as soon as it's avaialable
@@ -64,8 +64,8 @@ pyopengl
6464
gymnasium[mujoco]==0.27.0
6565
timm
6666
iopath
67-
pygame==2.1.2
67+
pygame==2.6.0
6868
pycocotools
6969
semilearn==0.3.2
7070
torchao==0.0.3
71-
segment_anything==1.0
71+
segment_anything==1.0

.jenkins/metadata.json

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
"intermediate_source/model_parallel_tutorial.py": {
2929
"needs": "linux.16xlarge.nvidia.gpu"
3030
},
31+
"recipes_source/torch_export_aoti_python.py": {
32+
"needs": "linux.g5.4xlarge.nvidia.gpu"
33+
},
3134
"advanced_source/pendulum.py": {
3235
"needs": "linux.g5.4xlarge.nvidia.gpu",
3336
"_comment": "need to be here for the compiling_optimizer_lr_scheduler.py to run."

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ We use sphinx-gallery's [notebook styled examples](https://sphinx-gallery.github
2222

2323
Here is how you can create a new tutorial (for a detailed description, see [CONTRIBUTING.md](./CONTRIBUTING.md)):
2424

25+
NOTE: Before submitting a new tutorial, read [PyTorch Tutorial Submission Policy](./tutorial_submission_policy.md).
26+
2527
1. Create a Python file. If you want it executed while inserted into documentation, save the file with the suffix `tutorial` so that the file name is `your_tutorial.py`.
2628
2. Put it in one of the `beginner_source`, `intermediate_source`, `advanced_source` directory based on the level of difficulty. If it is a recipe, add it to `recipes_source`. For tutorials demonstrating unstable prototype features, add to the `prototype_source`.
2729
3. For Tutorials (except if it is a prototype feature), include it in the `toctree` directive and create a `customcarditem` in [index.rst](./index.rst).
@@ -31,7 +33,7 @@ If you are starting off with a Jupyter notebook, you can use [this script](https
3133

3234
## Building locally
3335

34-
The tutorial build is very large and requires a GPU. If your machine does not have a GPU device, you can preview your HTML build without actually downloading the data and running the tutorial code:
36+
The tutorial build is very large and requires a GPU. If your machine does not have a GPU device, you can preview your HTML build without actually downloading the data and running the tutorial code:
3537

3638
1. Install required dependencies by running: `pip install -r requirements.txt`.
3739

@@ -40,8 +42,6 @@ The tutorial build is very large and requires a GPU. If your machine does not ha
4042
- If you have a GPU-powered laptop, you can build using `make docs`. This will download the data, execute the tutorials and build the documentation to `docs/` directory. This might take about 60-120 min for systems with GPUs. If you do not have a GPU installed on your system, then see next step.
4143
- You can skip the computationally intensive graph generation by running `make html-noplot` to build basic html documentation to `_build/html`. This way, you can quickly preview your tutorial.
4244

43-
> If you get **ModuleNotFoundError: No module named 'pytorch_sphinx_theme' make: *** [html-noplot] Error 2** from /tutorials/src/pytorch-sphinx-theme or /venv/src/pytorch-sphinx-theme (while using virtualenv), run `python setup.py install`.
44-
4545
## Building a single tutorial
4646

4747
You can build a single tutorial by using the `GALLERY_PATTERN` environment variable. For example to run only `neural_style_transfer_tutorial.py`, run:
@@ -59,8 +59,8 @@ The `GALLERY_PATTERN` variable respects regular expressions.
5959

6060

6161
## About contributing to PyTorch Documentation and Tutorials
62-
* You can find information about contributing to PyTorch documentation in the
63-
PyTorch Repo [README.md](https://github.com/pytorch/pytorch/blob/master/README.md) file.
62+
* You can find information about contributing to PyTorch documentation in the
63+
PyTorch Repo [README.md](https://github.com/pytorch/pytorch/blob/master/README.md) file.
6464
* Additional information can be found in [PyTorch CONTRIBUTING.md](https://github.com/pytorch/pytorch/blob/master/CONTRIBUTING.md).
6565

6666

_static/css/custom.css

+21
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,24 @@
9191
transition: none;
9292
transform-origin: none;
9393
}
94+
95+
.pytorch-left-menu-search input[type=text] {
96+
background-image: none;
97+
}
98+
99+
.gsc-control-cse {
100+
padding-left: 0px !important;
101+
padding-bottom: 0px !important;
102+
}
103+
104+
.gsc-search-button .gsc-search-button-v2:focus {
105+
border: transparent !important;
106+
outline: none;
107+
box-shadow: none;
108+
}
109+
.gsc-search-button-v2:active {
110+
border: none !important;
111+
}
112+
.gsc-search-button-v2 {
113+
border: none !important;
114+
}

_templates/layout.html

+17
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,23 @@
1111
</script>
1212
{%- endblock %}
1313

14+
{% block sidebartitle %}
15+
{% if theme_display_version %}
16+
{%- set nav_version = version %}
17+
{% if READTHEDOCS and current_version %}
18+
{%- set nav_version = current_version %}
19+
{% endif %}
20+
{% if nav_version %}
21+
<div class="version">
22+
{{ nav_version }}
23+
</div>
24+
{% endif %}
25+
{% endif %}
26+
<div class="searchbox">
27+
<script async src="https://cse.google.com/cse.js?cx=e65585f8c3ea1440e"></script>
28+
<div class="gcse-search"></div>
29+
</div>
30+
{% endblock %}
1431

1532
{% block footer %}
1633
{{ super() }}

advanced_source/cpp_custom_ops.rst

+2
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ To add ``torch.compile`` support for an operator, we must add a FakeTensor kerne
174174
known as a "meta kernel" or "abstract impl"). FakeTensors are Tensors that have
175175
metadata (such as shape, dtype, device) but no data: the FakeTensor kernel for an
176176
operator specifies how to compute the metadata of output tensors given the metadata of input tensors.
177+
The FakeTensor kernel should return dummy Tensors of your choice with
178+
the correct Tensor metadata (shape/strides/``dtype``/device).
177179

178180
We recommend that this be done from Python via the `torch.library.register_fake` API,
179181
though it is possible to do this from C++ as well (see

advanced_source/dynamic_quantization_tutorial.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ def tokenize(self, path):
151151
model.load_state_dict(
152152
torch.load(
153153
model_data_filepath + 'word_language_model_quantize.pth',
154-
map_location=torch.device('cpu')
154+
map_location=torch.device('cpu'),
155+
weights_only=True
155156
)
156157
)
157158

advanced_source/python_custom_ops.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def display(img):
6666
######################################################################
6767
# ``crop`` is not handled effectively out-of-the-box by
6868
# ``torch.compile``: ``torch.compile`` induces a
69-
# `"graph break" <https://pytorch.org/docs/stable/torch.compiler_faq.html#graph-breaks>`_
69+
# `"graph break" <https://pytorch.org/docs/stable/torch.compiler_faq.html#graph-breaks>`_
7070
# on functions it is unable to handle and graph breaks are bad for performance.
7171
# The following code demonstrates this by raising an error
7272
# (``torch.compile`` with ``fullgraph=True`` raises an error if a
@@ -85,9 +85,9 @@ def f(img):
8585
#
8686
# 1. wrap the function into a PyTorch custom operator.
8787
# 2. add a "``FakeTensor`` kernel" (aka "meta kernel") to the operator.
88-
# Given the metadata (e.g. shapes)
89-
# of the input Tensors, this function says how to compute the metadata
90-
# of the output Tensor(s).
88+
# Given some ``FakeTensors`` inputs (dummy Tensors that don't have storage),
89+
# this function should return dummy Tensors of your choice with the correct
90+
# Tensor metadata (shape/strides/``dtype``/device).
9191

9292

9393
from typing import Sequence
@@ -130,6 +130,11 @@ def f(img):
130130
# ``autograd.Function`` with PyTorch operator registration APIs can lead to (and
131131
# has led to) silent incorrectness when composed with ``torch.compile``.
132132
#
133+
# If you don't need training support, there is no need to use
134+
# ``torch.library.register_autograd``.
135+
# If you end up training with a ``custom_op`` that doesn't have an autograd
136+
# registration, we'll raise an error message.
137+
#
133138
# The gradient formula for ``crop`` is essentially ``PIL.paste`` (we'll leave the
134139
# derivation as an exercise to the reader). Let's first wrap ``paste`` into a
135140
# custom operator:
@@ -203,7 +208,7 @@ def setup_context(ctx, inputs, output):
203208
######################################################################
204209
# Mutable Python Custom operators
205210
# -------------------------------
206-
# You can also wrap a Python function that mutates its inputs into a custom
211+
# You can also wrap a Python function that mutates its inputs into a custom
207212
# operator.
208213
# Functions that mutate inputs are common because that is how many low-level
209214
# kernels are written; for example, a kernel that computes ``sin`` may take in

advanced_source/static_quantization_tutorial.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ We next define several helper functions to help with model evaluation. These mos
286286
287287
def load_model(model_file):
288288
model = MobileNetV2()
289-
state_dict = torch.load(model_file)
289+
state_dict = torch.load(model_file, weights_only=True)
290290
model.load_state_dict(state_dict)
291291
model.to('cpu')
292292
return model

beginner_source/basics/quickstart_tutorial.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def test(dataloader, model, loss_fn):
216216
# the state dictionary into it.
217217

218218
model = NeuralNetwork().to(device)
219-
model.load_state_dict(torch.load("model.pth"))
219+
model.load_state_dict(torch.load("model.pth", weights_only=True))
220220

221221
#############################################################
222222
# This model can now be used to make predictions.

beginner_source/basics/saveloadrun_tutorial.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,14 @@
3232
##########################
3333
# To load model weights, you need to create an instance of the same model first, and then load the parameters
3434
# using ``load_state_dict()`` method.
35+
#
36+
# In the code below, we set ``weights_only=True`` to limit the
37+
# functions executed during unpickling to only those necessary for
38+
# loading weights. Using ``weights_only=True`` is considered
39+
# a best practice when loading weights.
3540

3641
model = models.vgg16() # we do not specify ``weights``, i.e. create untrained model
37-
model.load_state_dict(torch.load('model_weights.pth'))
42+
model.load_state_dict(torch.load('model_weights.pth', weights_only=True))
3843
model.eval()
3944

4045
###########################
@@ -50,9 +55,14 @@
5055
torch.save(model, 'model.pth')
5156

5257
########################
53-
# We can then load the model like this:
58+
# We can then load the model as demonstrated below.
59+
#
60+
# As described in `Saving and loading torch.nn.Modules <pytorch.org/docs/main/notes/serialization.html#saving-and-loading-torch-nn-modules>`__,
61+
# saving ``state_dict``s is considered the best practice. However,
62+
# below we use ``weights_only=False`` because this involves loading the
63+
# model, which is a legacy use case for ``torch.save``.
5464

55-
model = torch.load('model.pth')
65+
model = torch.load('model.pth', weights_only=False),
5666

5767
########################
5868
# .. note:: This approach uses Python `pickle <https://docs.python.org/3/library/pickle.html>`_ module when serializing the model, thus it relies on the actual class definition to be available when loading the model.

beginner_source/blitz/cifar10_tutorial.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ def forward(self, x):
221221
# wasn't necessary here, we only did it to illustrate how to do so):
222222

223223
net = Net()
224-
net.load_state_dict(torch.load(PATH))
224+
net.load_state_dict(torch.load(PATH, weights_only=True))
225225

226226
########################################################################
227227
# Okay, now let us see what the neural network thinks these examples above are:

beginner_source/chatbot_tutorial.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@
8484
# Preparations
8585
# ------------
8686
#
87-
# To start, Download the data ZIP file
88-
# `here <https://zissou.infosci.cornell.edu/convokit/datasets/movie-corpus/movie-corpus.zip>`__
87+
# To get started, `download <https://zissou.infosci.cornell.edu/convokit/datasets/movie-corpus/movie-corpus.zip>`__ the Movie-Dialogs Corpus zip file.
8988

9089
# and put in a ``data/`` directory under the current directory.
9190
#

beginner_source/deeplabv3_on_android.rst

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ Image Segmentation DeepLabV3 on Android
55

66
**Reviewed by**: `Jeremiah Chung <https://github.com/jeremiahschung>`_
77

8+
.. warning::
9+
PyTorch Mobile is no longer actively supported. Please check out `ExecuTorch <https://pytorch.org/executorch-overview>`_, PyTorch’s all-new on-device inference library. You can also review our `end-to-end workflows <https://github.com/pytorch/executorch/tree/main/examples/portable#readme>`_ and review the `source code for DeepLabV3 <https://github.com/pytorch/executorch/tree/main/examples/models/deeplab_v3>`_.
10+
11+
812
Introduction
913
------------
1014

beginner_source/fgsm_tutorial.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def forward(self, x):
192192
model = Net().to(device)
193193

194194
# Load the pretrained model
195-
model.load_state_dict(torch.load(pretrained_model, map_location=device))
195+
model.load_state_dict(torch.load(pretrained_model, map_location=device, weights_only=True))
196196

197197
# Set the model in evaluation mode. In this case this is for the Dropout layers
198198
model.eval()

beginner_source/onnx/intro_onnx.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@
3939
4040
- `ONNX <https://onnx.ai>`_ standard library
4141
- `ONNX Script <https://onnxscript.ai>`_ library that enables developers to author ONNX operators,
42-
functions and models using a subset of Python in an expressive, and yet simple fashion.
42+
functions and models using a subset of Python in an expressive, and yet simple fashion
43+
- `ONNX Runtime <https://onnxruntime.ai>`_ accelerated machine learning library.
4344
4445
They can be installed through `pip <https://pypi.org/project/pip/>`_:
4546
4647
.. code-block:: bash
4748
48-
pip install --upgrade onnx onnxscript
49+
pip install --upgrade onnx onnxscript onnxruntime
4950
5051
To validate the installation, run the following commands:
5152

beginner_source/saving_loading_models.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@
153153
# .. code:: python
154154
#
155155
# model = TheModelClass(*args, **kwargs)
156-
# model.load_state_dict(torch.load(PATH))
156+
# model.load_state_dict(torch.load(PATH), weights_only=True)
157157
# model.eval()
158158
#
159159
# .. note::
@@ -206,7 +206,7 @@
206206
# .. code:: python
207207
#
208208
# # Model class must be defined somewhere
209-
# model = torch.load(PATH)
209+
# model = torch.load(PATH, weights_only=False)
210210
# model.eval()
211211
#
212212
# This save/load process uses the most intuitive syntax and involves the
@@ -290,7 +290,7 @@
290290
# model = TheModelClass(*args, **kwargs)
291291
# optimizer = TheOptimizerClass(*args, **kwargs)
292292
#
293-
# checkpoint = torch.load(PATH)
293+
# checkpoint = torch.load(PATH, weights_only=True)
294294
# model.load_state_dict(checkpoint['model_state_dict'])
295295
# optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
296296
# epoch = checkpoint['epoch']
@@ -354,7 +354,7 @@
354354
# optimizerA = TheOptimizerAClass(*args, **kwargs)
355355
# optimizerB = TheOptimizerBClass(*args, **kwargs)
356356
#
357-
# checkpoint = torch.load(PATH)
357+
# checkpoint = torch.load(PATH, weights_only=True)
358358
# modelA.load_state_dict(checkpoint['modelA_state_dict'])
359359
# modelB.load_state_dict(checkpoint['modelB_state_dict'])
360360
# optimizerA.load_state_dict(checkpoint['optimizerA_state_dict'])
@@ -407,7 +407,7 @@
407407
# .. code:: python
408408
#
409409
# modelB = TheModelBClass(*args, **kwargs)
410-
# modelB.load_state_dict(torch.load(PATH), strict=False)
410+
# modelB.load_state_dict(torch.load(PATH), strict=False, weights_only=True)
411411
#
412412
# Partially loading a model or loading a partial model are common
413413
# scenarios when transfer learning or training a new complex model.
@@ -446,7 +446,7 @@
446446
#
447447
# device = torch.device('cpu')
448448
# model = TheModelClass(*args, **kwargs)
449-
# model.load_state_dict(torch.load(PATH, map_location=device))
449+
# model.load_state_dict(torch.load(PATH, map_location=device, weights_only=True))
450450
#
451451
# When loading a model on a CPU that was trained with a GPU, pass
452452
# ``torch.device('cpu')`` to the ``map_location`` argument in the
@@ -469,7 +469,7 @@
469469
#
470470
# device = torch.device("cuda")
471471
# model = TheModelClass(*args, **kwargs)
472-
# model.load_state_dict(torch.load(PATH))
472+
# model.load_state_dict(torch.load(PATH, weights_only=True))
473473
# model.to(device)
474474
# # Make sure to call input = input.to(device) on any input tensors that you feed to the model
475475
#
@@ -497,7 +497,7 @@
497497
#
498498
# device = torch.device("cuda")
499499
# model = TheModelClass(*args, **kwargs)
500-
# model.load_state_dict(torch.load(PATH, map_location="cuda:0")) # Choose whatever GPU device number you want
500+
# model.load_state_dict(torch.load(PATH, weights_only=True, map_location="cuda:0")) # Choose whatever GPU device number you want
501501
# model.to(device)
502502
# # Make sure to call input = input.to(device) on any input tensors that you feed to the model
503503
#

beginner_source/transfer_learning_tutorial.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def train_model(model, criterion, optimizer, scheduler, num_epochs=25):
209209
print(f'Best val Acc: {best_acc:4f}')
210210

211211
# load best model weights
212-
model.load_state_dict(torch.load(best_model_params_path))
212+
model.load_state_dict(torch.load(best_model_params_path, weights_only=True))
213213
return model
214214

215215

0 commit comments

Comments
 (0)