Skip to content

Commit 69c758f

Browse files
authored
Merge branch 'main' into leslie/update_link
2 parents 4ab17a8 + a66464b commit 69c758f

37 files changed

+1546
-100
lines changed

.ci/docker/requirements.txt

Lines changed: 4 additions & 3 deletions
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,8 +28,9 @@ tensorboard
2828
jinja2==3.1.3
2929
pytorch-lightning
3030
torchx
31-
torchrl==0.3.0
32-
tensordict==0.3.0
31+
# TODO: use stable 0.5 when released
32+
-e git+https://github.com/pytorch/rl.git#egg=torchrl
33+
-e git+https://github.com/pytorch/tensordict.git#egg=tensordict
3334
ax-platform
3435
nbformat>==5.9.2
3536
datasets

.jenkins/build.sh

Lines changed: 2 additions & 2 deletions
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

.jenkins/validate_tutorials_built.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +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
3332
"advanced_source/usb_semisup_learn", # fails with CUDA OOM error, should try on a different worker
3433
"prototype_source/fx_graph_mode_ptq_dynamic",
3534
"prototype_source/vmap_recipe",
@@ -54,8 +53,6 @@
5453
"intermediate_source/flask_rest_api_tutorial",
5554
"intermediate_source/text_to_speech_with_torchaudio",
5655
"intermediate_source/tensorboard_profiler_tutorial", # reenable after 2.0 release.
57-
"intermediate_source/inductor_debug_cpu", # reenable after 2942
58-
"beginner_source/onnx/onnx_registry_tutorial", # reenable after 2941 is fixed.
5956
"intermediate_source/torch_export_tutorial" # reenable after 2940 is fixed.
6057
]
6158

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

Lines changed: 18 additions & 16 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,4 +417,4 @@ Conclusion
417417
In this tutorial, we went over the recommended approach to integrating Custom C++
418418
and CUDA operators with PyTorch. The ``TORCH_LIBRARY/torch.library`` APIs are fairly
419419
low-level. For more information about how to use the API, see
420-
`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_extension.rst

Lines changed: 5 additions & 1 deletion
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

Lines changed: 7 additions & 6 deletions
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

advanced_source/dispatcher.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Registering a Dispatched Operator in C++
22
========================================
33

4+
.. warning::
5+
6+
This tutorial is deprecated as of PyTorch 2.4. Please see :ref:`custom-ops-landing-page`
7+
for the newest up-to-date guides on extending PyTorch with Custom Operators.
8+
49
The dispatcher is an internal component of PyTorch which is responsible for
510
figuring out what code should actually get run when you call a function like
611
``torch::add``. This can be nontrivial, because PyTorch operations need

advanced_source/python_custom_ops.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,5 +260,5 @@ def f(x):
260260
# For more detailed information, see:
261261
#
262262
# - `the torch.library documentation <https://pytorch.org/docs/stable/library.html>`_
263-
# - `the Custom Operators Manual <https://pytorch.org/docs/main/notes/custom_operators.html>`_
263+
# - `the Custom Operators Manual <https://pytorch.org/tutorials/advanced/custom_ops_landing_page.html#the-custom-operators-manual>`_
264264
#

advanced_source/torch_script_custom_ops.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Extending TorchScript with Custom C++ Operators
22
===============================================
33

4+
.. warning::
5+
6+
This tutorial is deprecated as of PyTorch 2.4. Please see :ref:`custom-ops-landing-page`
7+
for the newest up-to-date guides on PyTorch Custom Operators.
8+
49
The PyTorch 1.0 release introduced a new programming model to PyTorch called
510
`TorchScript <https://pytorch.org/docs/master/jit.html>`_. TorchScript is a
611
subset of the Python programming language which can be parsed, compiled and

beginner_source/knowledge_distillation_tutorial.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ def train_knowledge_distillation(teacher, student, train_loader, epochs, learnin
352352
# Cosine loss minimization run
353353
# ----------------------------
354354
# Feel free to play around with the temperature parameter that controls the softness of the softmax function and the loss coefficients.
355-
# In neural networks, it is easy to include to include additional loss functions to the main objectives to achieve goals like better generalization.
355+
# In neural networks, it is easy to include additional loss functions to the main objectives to achieve goals like better generalization.
356356
# Let's try including an objective for the student, but now let's focus on their hidden states rather than their output layers.
357357
# Our goal is to convey information from the teacher's representation to the student by including a naive loss function,
358358
# whose minimization implies that the flattened vectors that are subsequently passed to the classifiers have become more *similar* as the loss decreases.

beginner_source/onnx/onnx_registry_tutorial.py

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ def forward(self, input_x, input_y):
9999
# NOTE: All attributes must be annotated with type hints.
100100
@onnxscript.script(custom_aten)
101101
def custom_aten_add(input_x, input_y, alpha: float = 1.0):
102-
alpha = opset18.CastLike(alpha, input_y)
103102
input_y = opset18.Mul(input_y, alpha)
104103
return opset18.Add(input_x, input_y)
105104

@@ -130,9 +129,9 @@ def custom_aten_add(input_x, input_y, alpha: float = 1.0):
130129
# graph node name is the function name
131130
assert onnx_program.model_proto.graph.node[0].op_type == "custom_aten_add"
132131
# function node domain is empty because we use standard ONNX operators
133-
assert onnx_program.model_proto.functions[0].node[3].domain == ""
132+
assert {node.domain for node in onnx_program.model_proto.functions[0].node} == {""}
134133
# function node name is the standard ONNX operator name
135-
assert onnx_program.model_proto.functions[0].node[3].op_type == "Add"
134+
assert {node.op_type for node in onnx_program.model_proto.functions[0].node} == {"Add", "Mul", "Constant"}
136135

137136

138137
######################################################################
@@ -231,33 +230,25 @@ def custom_aten_gelu(input_x, approximate: str = "none"):
231230

232231

233232
######################################################################
234-
# Let's inspect the model and verify the model uses :func:`custom_aten_gelu` instead of
235-
# :class:`aten::gelu`. Note the graph has one graph nodes for
236-
# ``custom_aten_gelu``, and inside ``custom_aten_gelu``, there is a function
237-
# node for ``Gelu`` with namespace ``com.microsoft``.
233+
# Let's inspect the model and verify the model uses op_type ``Gelu``
234+
# from namespace ``com.microsoft``.
235+
#
236+
# .. note::
237+
# :func:`custom_aten_gelu` does not exist in the graph because
238+
# functions with fewer than three operators are inlined automatically.
238239
#
239240

240241
# graph node domain is the custom domain we registered
241242
assert onnx_program.model_proto.graph.node[0].domain == "com.microsoft"
242243
# graph node name is the function name
243-
assert onnx_program.model_proto.graph.node[0].op_type == "custom_aten_gelu"
244-
# function node domain is the custom domain we registered
245-
assert onnx_program.model_proto.functions[0].node[0].domain == "com.microsoft"
246-
# function node name is the node name used in the function
247-
assert onnx_program.model_proto.functions[0].node[0].op_type == "Gelu"
244+
assert onnx_program.model_proto.graph.node[0].op_type == "Gelu"
248245

249246

250247
######################################################################
251-
# The following diagram shows ``custom_aten_gelu_model`` ONNX graph using Netron:
248+
# The following diagram shows ``custom_aten_gelu_model`` ONNX graph using Netron,
249+
# we can see the ``Gelu`` node from module ``com.microsoft`` used in the function:
252250
#
253251
# .. image:: /_static/img/onnx/custom_aten_gelu_model.png
254-
# :width: 70%
255-
# :align: center
256-
#
257-
# Inside the ``custom_aten_gelu`` function, we can see the ``Gelu`` node from module
258-
# ``com.microsoft`` used in the function:
259-
#
260-
# .. image:: /_static/img/onnx/custom_aten_gelu_function.png
261252
#
262253
# That is all we need to do. As an additional step, we can use ONNX Runtime to run the model,
263254
# and compare the results with PyTorch.

en-wordlist.txt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
ACL
23
ADI
34
AOT
@@ -50,6 +51,7 @@ DDP
5051
DDPG
5152
DDQN
5253
DLRM
54+
DMA
5355
DNN
5456
DQN
5557
DataLoaders
@@ -68,6 +70,8 @@ Ecker
6870
ExportDB
6971
FC
7072
FGSM
73+
tensordict
74+
DataLoader's
7175
FLAVA
7276
FSDP
7377
FX
@@ -139,6 +143,7 @@ MKLDNN
139143
MLP
140144
MLPs
141145
MNIST
146+
MPS
142147
MUC
143148
MacBook
144149
MacOS
@@ -219,6 +224,7 @@ STR
219224
SVE
220225
SciPy
221226
Sequentials
227+
Sharding
222228
Sigmoid
223229
SoTA
224230
Sohn
@@ -254,6 +260,7 @@ VLDB
254260
VQA
255261
VS Code
256262
ViT
263+
Volterra
257264
WMT
258265
WSI
259266
WSIs
@@ -336,11 +343,11 @@ dataset’s
336343
deallocation
337344
decompositions
338345
decorrelated
339-
devicemesh
340346
deserialize
341347
deserialized
342348
desynchronization
343349
deterministically
350+
devicemesh
344351
dimensionality
345352
dir
346353
discontiguous
@@ -384,6 +391,7 @@ hessian
384391
hessians
385392
histoencoder
386393
histologically
394+
homonymous
387395
hotspot
388396
hvp
389397
hyperparameter
@@ -459,6 +467,7 @@ optimizer's
459467
optimizers
460468
otsu
461469
overfitting
470+
pageable
462471
parallelizable
463472
parallelization
464473
parametrization
@@ -522,7 +531,6 @@ runtime
522531
runtimes
523532
scalable
524533
sharded
525-
Sharding
526534
softmax
527535
sparsified
528536
sparsifier
@@ -609,4 +617,4 @@ warmstarting
609617
warmup
610618
webp
611619
wsi
612-
wsis
620+
wsis

0 commit comments

Comments
 (0)