Skip to content

Commit fefbe1f

Browse files
authored
Merge branch 'main' into torch-compile
2 parents 02c90d7 + 42aaa33 commit fefbe1f

File tree

57 files changed

+2094
-1037
lines changed

Some content is hidden

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

57 files changed

+2094
-1037
lines changed

.ci/docker/requirements.txt

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ tqdm==4.66.1
1313
numpy==1.24.4
1414
matplotlib
1515
librosa
16-
torch==2.3
16+
torch==2.4
1717
torchvision
1818
torchtext
1919
torchdata
@@ -28,9 +28,9 @@ tensorboard
2828
jinja2==3.1.3
2929
pytorch-lightning
3030
torchx
31-
torchrl==0.3.0
32-
tensordict==0.3.0
33-
ax-platform
31+
torchrl==0.5.0
32+
tensordict==0.5.0
33+
ax-platform>==0.4.0
3434
nbformat>==5.9.2
3535
datasets
3636
transformers
@@ -68,4 +68,4 @@ pygame==2.1.2
6868
pycocotools
6969
semilearn==0.3.2
7070
torchao==0.0.3
71-
segment_anything==1.0
71+
segment_anything==1.0

.jenkins/build.sh

+2-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ sudo apt-get install -y pandoc
2121

2222
#Install PyTorch Nightly for test.
2323
# Nightly - pip install --pre torch torchvision torchaudio -f https://download.pytorch.org/whl/nightly/cu102/torch_nightly.html
24-
# Install 2.2 for testing - uncomment to install nightly binaries (update the version as needed).
24+
# Install 2.4 to merge all 2.4 PRs - uncomment to install nightly binaries (update the version as needed).
2525
# pip uninstall -y torch torchvision torchaudio torchtext torchdata
26-
# pip3 install torch==2.3.0 torchvision torchaudio --no-cache-dir --index-url https://download.pytorch.org/whl/test/cu121
26+
# pip3 install torch==2.4.0 torchvision torchaudio --no-cache-dir --index-url https://download.pytorch.org/whl/test/cu124
2727

2828
# Install two language tokenizers for Translation with TorchText tutorial
2929
python -m spacy download en_core_web_sm
@@ -36,7 +36,6 @@ awsv2 configure set default.s3.multipart_threshold 5120MB
3636
if [[ "${JOB_TYPE}" == "worker" ]]; then
3737
# Step 1: Remove runnable code from tutorials that are not supposed to be run
3838
python $DIR/remove_runnable_code.py beginner_source/aws_distributed_training_tutorial.py beginner_source/aws_distributed_training_tutorial.py || true
39-
# python $DIR/remove_runnable_code.py advanced_source/ddp_pipeline_tutorial.py advanced_source/ddp_pipeline_tutorial.py || true
4039
# Temp remove for mnist download issue. (Re-enabled for 1.8.1)
4140
# python $DIR/remove_runnable_code.py beginner_source/fgsm_tutorial.py beginner_source/fgsm_tutorial.py || true
4241
# python $DIR/remove_runnable_code.py intermediate_source/spatial_transformer_tutorial.py intermediate_source/spatial_transformer_tutorial.py || true

.jenkins/validate_tutorials_built.py

-4
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
"intermediate_source/fx_conv_bn_fuser",
3030
"intermediate_source/_torch_export_nightly_tutorial", # does not work on release
3131
"advanced_source/super_resolution_with_onnxruntime",
32-
"advanced_source/python_custom_ops", # https://github.com/pytorch/pytorch/issues/127443
33-
"advanced_source/ddp_pipeline", # requires 4 gpus
3432
"advanced_source/usb_semisup_learn", # fails with CUDA OOM error, should try on a different worker
3533
"prototype_source/fx_graph_mode_ptq_dynamic",
3634
"prototype_source/vmap_recipe",
@@ -55,8 +53,6 @@
5553
"intermediate_source/flask_rest_api_tutorial",
5654
"intermediate_source/text_to_speech_with_torchaudio",
5755
"intermediate_source/tensorboard_profiler_tutorial", # reenable after 2.0 release.
58-
"intermediate_source/inductor_debug_cpu", # reenable after 2942
59-
"beginner_source/onnx/onnx_registry_tutorial", # reenable after 2941 is fixed.
6056
"intermediate_source/torch_export_tutorial" # reenable after 2940 is fixed.
6157
]
6258

_static/css/custom.css

+20
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,23 @@
7171
.sd-card:hover:after {
7272
transform: scaleX(1);
7373
}
74+
75+
.card-prerequisites:hover {
76+
transition: none;
77+
border: none;
78+
}
79+
80+
.card-prerequisites:hover:after {
81+
transition: none;
82+
transform: none;
83+
}
84+
85+
.card-prerequisites:after {
86+
display: block;
87+
content: '';
88+
border-bottom: none;
89+
background-color: #fff;
90+
transform: none;
91+
transition: none;
92+
transform-origin: none;
93+
}
Loading
423 KB
Loading
3.52 KB
Loading
-22.1 KB
Binary file not shown.
19 KB
Loading

_static/img/pinmem/pinmem.png

72 KB
Loading
81.2 KB
Loading
81.4 KB
Loading
85.4 KB
Loading
90.6 KB
Loading

advanced_source/coding_ddpg.py

+18-16
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@
182182
# Later, we will see how the target parameters should be updated in TorchRL.
183183
#
184184

185-
from tensordict.nn import TensorDictModule
185+
from tensordict.nn import TensorDictModule, TensorDictSequential
186186

187187

188188
def _init(
@@ -290,12 +290,11 @@ def _loss_actor(
290290
) -> torch.Tensor:
291291
td_copy = tensordict.select(*self.actor_in_keys)
292292
# Get an action from the actor network: since we made it functional, we need to pass the params
293-
td_copy = self.actor_network(td_copy, params=self.actor_network_params)
293+
with self.actor_network_params.to_module(self.actor_network):
294+
td_copy = self.actor_network(td_copy)
294295
# get the value associated with that action
295-
td_copy = self.value_network(
296-
td_copy,
297-
params=self.value_network_params.detach(),
298-
)
296+
with self.value_network_params.detach().to_module(self.value_network):
297+
td_copy = self.value_network(td_copy)
299298
return -td_copy.get("state_action_value")
300299

301300

@@ -317,7 +316,8 @@ def _loss_value(
317316
td_copy = tensordict.clone()
318317

319318
# V(s, a)
320-
self.value_network(td_copy, params=self.value_network_params)
319+
with self.value_network_params.to_module(self.value_network):
320+
self.value_network(td_copy)
321321
pred_val = td_copy.get("state_action_value").squeeze(-1)
322322

323323
# we manually reconstruct the parameters of the actor-critic, where the first
@@ -332,9 +332,8 @@ def _loss_value(
332332
batch_size=self.target_actor_network_params.batch_size,
333333
device=self.target_actor_network_params.device,
334334
)
335-
target_value = self.value_estimator.value_estimate(
336-
tensordict, target_params=target_params
337-
).squeeze(-1)
335+
with target_params.to_module(self.actor_critic):
336+
target_value = self.value_estimator.value_estimate(tensordict).squeeze(-1)
338337

339338
# Computes the value loss: L2, L1 or smooth L1 depending on `self.loss_function`
340339
loss_value = distance_loss(pred_val, target_value, loss_function=self.loss_function)
@@ -717,7 +716,7 @@ def get_env_stats():
717716
ActorCriticWrapper,
718717
DdpgMlpActor,
719718
DdpgMlpQNet,
720-
OrnsteinUhlenbeckProcessWrapper,
719+
OrnsteinUhlenbeckProcessModule,
721720
ProbabilisticActor,
722721
TanhDelta,
723722
ValueOperator,
@@ -776,15 +775,18 @@ def make_ddpg_actor(
776775
# Exploration
777776
# ~~~~~~~~~~~
778777
#
779-
# The policy is wrapped in a :class:`~torchrl.modules.OrnsteinUhlenbeckProcessWrapper`
778+
# The policy is passed into a :class:`~torchrl.modules.OrnsteinUhlenbeckProcessModule`
780779
# exploration module, as suggested in the original paper.
781780
# Let's define the number of frames before OU noise reaches its minimum value
782781
annealing_frames = 1_000_000
783782

784-
actor_model_explore = OrnsteinUhlenbeckProcessWrapper(
783+
actor_model_explore = TensorDictSequential(
785784
actor,
786-
annealing_num_steps=annealing_frames,
787-
).to(device)
785+
OrnsteinUhlenbeckProcessModule(
786+
spec=actor.spec.clone(),
787+
annealing_num_steps=annealing_frames,
788+
).to(device),
789+
)
788790
if device == torch.device("cpu"):
789791
actor_model_explore.share_memory()
790792

@@ -1168,7 +1170,7 @@ def ceil_div(x, y):
11681170
)
11691171

11701172
# update the exploration strategy
1171-
actor_model_explore.step(current_frames)
1173+
actor_model_explore[1].step(current_frames)
11721174

11731175
collector.shutdown()
11741176
del collector

advanced_source/cpp_custom_ops.rst

+7-5
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ Custom C++ and CUDA Operators
88
.. grid:: 2
99

1010
.. grid-item-card:: :octicon:`mortar-board;1em;` What you will learn
11+
:class-card: card-prerequisites
1112

12-
* How to integrate custom operators written in C++/CUDA with PyTorch
13-
* How to test custom operators using ``torch.library.opcheck``
13+
* How to integrate custom operators written in C++/CUDA with PyTorch
14+
* How to test custom operators using ``torch.library.opcheck``
1415

1516
.. grid-item-card:: :octicon:`list-unordered;1em;` Prerequisites
17+
:class-card: card-prerequisites
1618

17-
* PyTorch 2.4 or later
18-
* Basic understanding of C++ and CUDA programming
19+
* PyTorch 2.4 or later
20+
* Basic understanding of C++ and CUDA programming
1921

2022
PyTorch offers a large library of operators that work on Tensors (e.g. torch.add, torch.sum, etc).
2123
However, you may wish to bring a new custom operator to PyTorch. This tutorial demonstrates the
@@ -415,4 +417,4 @@ Conclusion
415417
In this tutorial, we went over the recommended approach to integrating Custom C++
416418
and CUDA operators with PyTorch. The ``TORCH_LIBRARY/torch.library`` APIs are fairly
417419
low-level. For more information about how to use the API, see
418-
`The Custom Operators Manual <https://pytorch.org/docs/main/notes/custom_operators.html>`_.
420+
`The Custom Operators Manual <https://pytorch.org/tutorials/advanced/custom_ops_landing_page.html#the-custom-operators-manual>`_.

advanced_source/cpp_export.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ minimal ``CMakeLists.txt`` to build it could look as simple as:
203203
204204
add_executable(example-app example-app.cpp)
205205
target_link_libraries(example-app "${TORCH_LIBRARIES}")
206-
set_property(TARGET example-app PROPERTY CXX_STANDARD 14)
206+
set_property(TARGET example-app PROPERTY CXX_STANDARD 17)
207207
208208
The last thing we need to build the example application is the LibTorch
209209
distribution. You can always grab the latest stable release from the `download

advanced_source/cpp_extension.rst

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ Custom C++ and CUDA Extensions
22
==============================
33
**Author**: `Peter Goldsborough <https://www.goldsborough.me/>`_
44

5+
.. warning::
6+
7+
This tutorial is deprecated as of PyTorch 2.4. Please see :ref:`custom-ops-landing-page`
8+
for the newest up-to-date guides on extending PyTorch with Custom C++/CUDA Extensions.
59

610
PyTorch provides a plethora of operations related to neural networks, arbitrary
711
tensor algebra, data wrangling and other purposes. However, you may still find
@@ -225,7 +229,7 @@ Instead of:
225229
Currently open issue for nvcc bug `here
226230
<https://github.com/pytorch/pytorch/issues/69460>`_.
227231
Complete workaround code example `here
228-
<https://github.com/facebookresearch/pytorch3d/commit/cb170ac024a949f1f9614ffe6af1c38d972f7d48>`_.
232+
<https://github.com/facebookresearch/pytorch3d/commit/cb170ac024a949f1f9614ffe6af1c38d972f7d48>`_.
229233

230234
Forward Pass
231235
************

advanced_source/custom_ops_landing_page.rst

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.. _custom-ops-landing-page:
22

3-
PyTorch Custom Operators Landing Page
4-
=====================================
3+
PyTorch Custom Operators
4+
===========================
55

66
PyTorch offers a large library of operators that work on Tensors (e.g. ``torch.add``,
77
``torch.sum``, etc). However, you may wish to bring a new custom operation to PyTorch
@@ -10,26 +10,27 @@ In order to do so, you must register the custom operation with PyTorch via the P
1010
`torch.library docs <https://pytorch.org/docs/stable/library.html>`_ or C++ ``TORCH_LIBRARY``
1111
APIs.
1212

13-
TL;DR
14-
-----
13+
1514

1615
Authoring a custom operator from Python
1716
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1817

1918
Please see :ref:`python-custom-ops-tutorial`.
2019

2120
You may wish to author a custom operator from Python (as opposed to C++) if:
21+
2222
- you have a Python function you want PyTorch to treat as an opaque callable, especially with
23-
respect to ``torch.compile`` and ``torch.export``.
23+
respect to ``torch.compile`` and ``torch.export``.
2424
- you have some Python bindings to C++/CUDA kernels and want those to compose with PyTorch
25-
subsystems (like ``torch.compile`` or ``torch.autograd``)
25+
subsystems (like ``torch.compile`` or ``torch.autograd``)
2626

2727
Integrating custom C++ and/or CUDA code with PyTorch
2828
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2929

3030
Please see :ref:`cpp-custom-ops-tutorial`.
3131

3232
You may wish to author a custom operator from C++ (as opposed to Python) if:
33+
3334
- you have custom C++ and/or CUDA code.
3435
- you plan to use this code with ``AOTInductor`` to do Python-less inference.
3536

0 commit comments

Comments
 (0)