Skip to content

Add a tutorial for PyTorch Flight recorder #3024

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 31 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
b2c013e
[Doc] Flight recorder tutorial
c-p-i-o Aug 20, 2024
2b69bcd
More cleanup.
c-p-i-o Aug 21, 2024
7df80d1
Update flight_recorder_tutorial.rst
fduwjj Aug 27, 2024
d4fd0d3
Add additional settings section
c-p-i-o Sep 3, 2024
23eef97
address code review comments
c-p-i-o Sep 3, 2024
f4cf1ff
add missing section
c-p-i-o Sep 3, 2024
f7db1b9
fix typo
c-p-i-o Sep 3, 2024
d86d623
Fixes
c-p-i-o Sep 3, 2024
c071784
Add flight recorder to prototype index
c-p-i-o Sep 3, 2024
aa65c84
Update prototype_index
c-p-i-o Sep 3, 2024
fb13629
Apply suggestions from code review
c-p-i-o Sep 5, 2024
2358201
More HTML and formatting fixes
c-p-i-o Sep 6, 2024
de5654a
More HTML formatting changes
c-p-i-o Sep 6, 2024
3b0c4cc
[dtensor][debug] CommDebugMode recipe (#3001)
XilunWu Aug 19, 2024
f952921
fix: rm `use_cuda` param (#3002)
shaoyuyoung Aug 19, 2024
fa03879
Add programmable Google Search to pytorch tutorials site (#2820)
svekars Aug 22, 2024
ab36383
Tutorial for AOTI Python runtime (#2997)
agunapal Aug 23, 2024
fc27f08
Create tutorial_submission_policy.md (#2995)
svekars Aug 24, 2024
9d97d8f
Removed upper-case letter/made 'download' the link text instead of 'h…
tstatler Aug 27, 2024
acdc91b
Add weights_only=True to torch.load (#3012)
svekars Aug 27, 2024
3dacf89
Fix typos in dynamic_quantization_bert_tutorial.rst (#3019)
hadh93 Aug 28, 2024
fc5a612
Improve custom ops tutorials (#3020)
zou3519 Aug 29, 2024
c1e792a
Removed outdated steps in README about running about setup.py (#3014)
tstatler Aug 29, 2024
202693f
Fix hovering over the GCS search button (#3005)
svekars Aug 29, 2024
5465f9b
Added warnings to select Pytorch mobile tutorials directing users to …
tstatler Aug 30, 2024
f45ddc2
Patched docs for torch_compile_tutorial (#2936)
ignaciobartol Aug 30, 2024
c7a99c5
Upgrade pygame version to 2.6.0 (#3025)
svekars Sep 4, 2024
deb89ba
Update intro_onnx.py (#3033)
labintsev Sep 5, 2024
a5d85ed
Fixed Rst formatting, minor text changes (#3029)
tstatler Sep 5, 2024
200c4e5
Add meta tag to torch_export_aoti_python (#3036)
svekars Sep 5, 2024
44493fb
Fix reference to dcp in loading example (#2972)
angerrp Sep 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .ci/docker/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ IMAGE_NAME="$1"
shift

export UBUNTU_VERSION="20.04"
export CUDA_VERSION="12.4.1"

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

docker build \
Expand Down
2 changes: 1 addition & 1 deletion .ci/docker/common/common_utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ conda_run() {
}

pip_install() {
as_ci_user conda run -n py_$ANACONDA_PYTHON_VERSION pip install --progress-bar off $*
as_ci_user conda run -n py_$ANACONDA_PYTHON_VERSION pip3 install --progress-bar off $*
}
8 changes: 4 additions & 4 deletions .ci/docker/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ pytorch-lightning
torchx
torchrl==0.5.0
tensordict==0.5.0
ax-platform>==0.4.0
nbformat>==5.9.2
ax-platform>=0.4.0
nbformat>=5.9.2
datasets
transformers
torchmultimodal-nightly # needs to be updated to stable as soon as it's avaialable
Expand Down Expand Up @@ -64,8 +64,8 @@ pyopengl
gymnasium[mujoco]==0.27.0
timm
iopath
pygame==2.1.2
pygame==2.6.0
pycocotools
semilearn==0.3.2
torchao==0.0.3
segment_anything==1.0
segment_anything==1.0
3 changes: 3 additions & 0 deletions .jenkins/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
"intermediate_source/model_parallel_tutorial.py": {
"needs": "linux.16xlarge.nvidia.gpu"
},
"recipes_source/torch_export_aoti_python.py": {
"needs": "linux.g5.4xlarge.nvidia.gpu"
},
"advanced_source/pendulum.py": {
"needs": "linux.g5.4xlarge.nvidia.gpu",
"_comment": "need to be here for the compiling_optimizer_lr_scheduler.py to run."
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ We use sphinx-gallery's [notebook styled examples](https://sphinx-gallery.github

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

NOTE: Before submitting a new tutorial, read [PyTorch Tutorial Submission Policy](./tutorial_submission_policy.md).

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`.
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`.
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).
Expand All @@ -31,7 +33,7 @@ If you are starting off with a Jupyter notebook, you can use [this script](https

## Building locally

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:
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:

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

Expand All @@ -40,8 +42,6 @@ The tutorial build is very large and requires a GPU. If your machine does not ha
- 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.
- 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.

> 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`.

## Building a single tutorial

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


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


Expand Down
21 changes: 21 additions & 0 deletions _static/css/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,24 @@
transition: none;
transform-origin: none;
}

.pytorch-left-menu-search input[type=text] {
background-image: none;
}

.gsc-control-cse {
padding-left: 0px !important;
padding-bottom: 0px !important;
}

.gsc-search-button .gsc-search-button-v2:focus {
border: transparent !important;
outline: none;
box-shadow: none;
}
.gsc-search-button-v2:active {
border: none !important;
}
.gsc-search-button-v2 {
border: none !important;
}
17 changes: 17 additions & 0 deletions _templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@
</script>
{%- endblock %}

{% block sidebartitle %}
{% if theme_display_version %}
{%- set nav_version = version %}
{% if READTHEDOCS and current_version %}
{%- set nav_version = current_version %}
{% endif %}
{% if nav_version %}
<div class="version">
{{ nav_version }}
</div>
{% endif %}
{% endif %}
<div class="searchbox">
<script async src="https://cse.google.com/cse.js?cx=e65585f8c3ea1440e"></script>
<div class="gcse-search"></div>
</div>
{% endblock %}

{% block footer %}
{{ super() }}
Expand Down
2 changes: 2 additions & 0 deletions advanced_source/cpp_custom_ops.rst
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ To add ``torch.compile`` support for an operator, we must add a FakeTensor kerne
known as a "meta kernel" or "abstract impl"). FakeTensors are Tensors that have
metadata (such as shape, dtype, device) but no data: the FakeTensor kernel for an
operator specifies how to compute the metadata of output tensors given the metadata of input tensors.
The FakeTensor kernel should return dummy Tensors of your choice with
the correct Tensor metadata (shape/strides/``dtype``/device).

We recommend that this be done from Python via the `torch.library.register_fake` API,
though it is possible to do this from C++ as well (see
Expand Down
3 changes: 2 additions & 1 deletion advanced_source/dynamic_quantization_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ def tokenize(self, path):
model.load_state_dict(
torch.load(
model_data_filepath + 'word_language_model_quantize.pth',
map_location=torch.device('cpu')
map_location=torch.device('cpu'),
weights_only=True
)
)

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


from typing import Sequence
Expand Down Expand Up @@ -130,6 +130,11 @@ def f(img):
# ``autograd.Function`` with PyTorch operator registration APIs can lead to (and
# has led to) silent incorrectness when composed with ``torch.compile``.
#
# If you don't need training support, there is no need to use
# ``torch.library.register_autograd``.
# If you end up training with a ``custom_op`` that doesn't have an autograd
# registration, we'll raise an error message.
#
# The gradient formula for ``crop`` is essentially ``PIL.paste`` (we'll leave the
# derivation as an exercise to the reader). Let's first wrap ``paste`` into a
# custom operator:
Expand Down Expand Up @@ -203,7 +208,7 @@ def setup_context(ctx, inputs, output):
######################################################################
# Mutable Python Custom operators
# -------------------------------
# You can also wrap a Python function that mutates its inputs into a custom
# You can also wrap a Python function that mutates its inputs into a custom
# operator.
# Functions that mutate inputs are common because that is how many low-level
# kernels are written; for example, a kernel that computes ``sin`` may take in
Expand Down
2 changes: 1 addition & 1 deletion advanced_source/static_quantization_tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ We next define several helper functions to help with model evaluation. These mos

def load_model(model_file):
model = MobileNetV2()
state_dict = torch.load(model_file)
state_dict = torch.load(model_file, weights_only=True)
model.load_state_dict(state_dict)
model.to('cpu')
return model
Expand Down
2 changes: 1 addition & 1 deletion beginner_source/basics/quickstart_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def test(dataloader, model, loss_fn):
# the state dictionary into it.

model = NeuralNetwork().to(device)
model.load_state_dict(torch.load("model.pth"))
model.load_state_dict(torch.load("model.pth", weights_only=True))

#############################################################
# This model can now be used to make predictions.
Expand Down
16 changes: 13 additions & 3 deletions beginner_source/basics/saveloadrun_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,14 @@
##########################
# To load model weights, you need to create an instance of the same model first, and then load the parameters
# using ``load_state_dict()`` method.
#
# In the code below, we set ``weights_only=True`` to limit the
# functions executed during unpickling to only those necessary for
# loading weights. Using ``weights_only=True`` is considered
# a best practice when loading weights.

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

###########################
Expand All @@ -50,9 +55,14 @@
torch.save(model, 'model.pth')

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

model = torch.load('model.pth')
model = torch.load('model.pth', weights_only=False),

########################
# .. 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.
Expand Down
2 changes: 1 addition & 1 deletion beginner_source/blitz/cifar10_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def forward(self, x):
# wasn't necessary here, we only did it to illustrate how to do so):

net = Net()
net.load_state_dict(torch.load(PATH))
net.load_state_dict(torch.load(PATH, weights_only=True))

########################################################################
# Okay, now let us see what the neural network thinks these examples above are:
Expand Down
3 changes: 1 addition & 2 deletions beginner_source/chatbot_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@
# Preparations
# ------------
#
# To start, Download the data ZIP file
# `here <https://zissou.infosci.cornell.edu/convokit/datasets/movie-corpus/movie-corpus.zip>`__
# To get started, `download <https://zissou.infosci.cornell.edu/convokit/datasets/movie-corpus/movie-corpus.zip>`__ the Movie-Dialogs Corpus zip file.

# and put in a ``data/`` directory under the current directory.
#
Expand Down
4 changes: 4 additions & 0 deletions beginner_source/deeplabv3_on_android.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ Image Segmentation DeepLabV3 on Android

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

.. warning::
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>`_.


Introduction
------------

Expand Down
2 changes: 1 addition & 1 deletion beginner_source/fgsm_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def forward(self, x):
model = Net().to(device)

# Load the pretrained model
model.load_state_dict(torch.load(pretrained_model, map_location=device))
model.load_state_dict(torch.load(pretrained_model, map_location=device, weights_only=True))

# Set the model in evaluation mode. In this case this is for the Dropout layers
model.eval()
Expand Down
5 changes: 3 additions & 2 deletions beginner_source/onnx/intro_onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@

- `ONNX <https://onnx.ai>`_ standard library
- `ONNX Script <https://onnxscript.ai>`_ library that enables developers to author ONNX operators,
functions and models using a subset of Python in an expressive, and yet simple fashion.
functions and models using a subset of Python in an expressive, and yet simple fashion
- `ONNX Runtime <https://onnxruntime.ai>`_ accelerated machine learning library.

They can be installed through `pip <https://pypi.org/project/pip/>`_:

.. code-block:: bash

pip install --upgrade onnx onnxscript
pip install --upgrade onnx onnxscript onnxruntime

To validate the installation, run the following commands:

Expand Down
16 changes: 8 additions & 8 deletions beginner_source/saving_loading_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
# .. code:: python
#
# model = TheModelClass(*args, **kwargs)
# model.load_state_dict(torch.load(PATH))
# model.load_state_dict(torch.load(PATH), weights_only=True)
# model.eval()
#
# .. note::
Expand Down Expand Up @@ -206,7 +206,7 @@
# .. code:: python
#
# # Model class must be defined somewhere
# model = torch.load(PATH)
# model = torch.load(PATH, weights_only=False)
# model.eval()
#
# This save/load process uses the most intuitive syntax and involves the
Expand Down Expand Up @@ -290,7 +290,7 @@
# model = TheModelClass(*args, **kwargs)
# optimizer = TheOptimizerClass(*args, **kwargs)
#
# checkpoint = torch.load(PATH)
# checkpoint = torch.load(PATH, weights_only=True)
# model.load_state_dict(checkpoint['model_state_dict'])
# optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
# epoch = checkpoint['epoch']
Expand Down Expand Up @@ -354,7 +354,7 @@
# optimizerA = TheOptimizerAClass(*args, **kwargs)
# optimizerB = TheOptimizerBClass(*args, **kwargs)
#
# checkpoint = torch.load(PATH)
# checkpoint = torch.load(PATH, weights_only=True)
# modelA.load_state_dict(checkpoint['modelA_state_dict'])
# modelB.load_state_dict(checkpoint['modelB_state_dict'])
# optimizerA.load_state_dict(checkpoint['optimizerA_state_dict'])
Expand Down Expand Up @@ -407,7 +407,7 @@
# .. code:: python
#
# modelB = TheModelBClass(*args, **kwargs)
# modelB.load_state_dict(torch.load(PATH), strict=False)
# modelB.load_state_dict(torch.load(PATH), strict=False, weights_only=True)
#
# Partially loading a model or loading a partial model are common
# scenarios when transfer learning or training a new complex model.
Expand Down Expand Up @@ -446,7 +446,7 @@
#
# device = torch.device('cpu')
# model = TheModelClass(*args, **kwargs)
# model.load_state_dict(torch.load(PATH, map_location=device))
# model.load_state_dict(torch.load(PATH, map_location=device, weights_only=True))
#
# When loading a model on a CPU that was trained with a GPU, pass
# ``torch.device('cpu')`` to the ``map_location`` argument in the
Expand All @@ -469,7 +469,7 @@
#
# device = torch.device("cuda")
# model = TheModelClass(*args, **kwargs)
# model.load_state_dict(torch.load(PATH))
# model.load_state_dict(torch.load(PATH, weights_only=True))
# model.to(device)
# # Make sure to call input = input.to(device) on any input tensors that you feed to the model
#
Expand Down Expand Up @@ -497,7 +497,7 @@
#
# device = torch.device("cuda")
# model = TheModelClass(*args, **kwargs)
# model.load_state_dict(torch.load(PATH, map_location="cuda:0")) # Choose whatever GPU device number you want
# model.load_state_dict(torch.load(PATH, weights_only=True, map_location="cuda:0")) # Choose whatever GPU device number you want
# model.to(device)
# # Make sure to call input = input.to(device) on any input tensors that you feed to the model
#
Expand Down
2 changes: 1 addition & 1 deletion beginner_source/transfer_learning_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def train_model(model, criterion, optimizer, scheduler, num_epochs=25):
print(f'Best val Acc: {best_acc:4f}')

# load best model weights
model.load_state_dict(torch.load(best_model_params_path))
model.load_state_dict(torch.load(best_model_params_path, weights_only=True))
return model


Expand Down
Loading
Loading