Skip to content

Commit eba7f5c

Browse files
authored
Merge branch 'main' into readme-update
2 parents 026969d + 4cb73ca commit eba7f5c

25 files changed

+65
-46
lines changed

advanced_source/cpp_custom_ops.rst

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

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

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

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

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

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

Lines changed: 1 addition & 1 deletion
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/fgsm_tutorial.py

Lines changed: 1 addition & 1 deletion
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/saving_loading_models.py

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

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

intermediate_source/autograd_saved_tensors_hooks_tutorial.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ def pack_hook(tensor):
397397
return name
398398

399399
def unpack_hook(name):
400-
return torch.load(name)
400+
return torch.load(name, weights_only=True)
401401

402402

403403
######################################################################
@@ -420,7 +420,7 @@ def pack_hook(tensor):
420420
return name
421421

422422
def unpack_hook(name):
423-
tensor = torch.load(name)
423+
tensor = torch.load(name, weights_only=True)
424424
os.remove(name)
425425
return tensor
426426

@@ -462,7 +462,7 @@ def pack_hook(tensor):
462462
return temp_file
463463

464464
def unpack_hook(temp_file):
465-
return torch.load(temp_file.name)
465+
return torch.load(temp_file.name, weights_only=True)
466466

467467

468468
######################################################################

intermediate_source/ddp_tutorial.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ and elasticity support, please refer to `TorchElastic <https://pytorch.org/elast
214214
# configure map_location properly
215215
map_location = {'cuda:%d' % 0: 'cuda:%d' % rank}
216216
ddp_model.load_state_dict(
217-
torch.load(CHECKPOINT_PATH, map_location=map_location))
217+
torch.load(CHECKPOINT_PATH, map_location=map_location, weights_only=True))
218218
219219
loss_fn = nn.MSELoss()
220220
optimizer = optim.SGD(ddp_model.parameters(), lr=0.001)

intermediate_source/dynamic_quantization_bert_tutorial.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ Mac:
7979

8080
.. code:: shell
8181
82-
yes y | pip uninstall torch tochvision
82+
yes y | pip uninstall torch torchvision
8383
yes y | pip install --pre torch -f https://download.pytorch.org/whl/nightly/cu101/torch_nightly.html
8484
8585
@@ -206,7 +206,7 @@ in `examples <https://github.com/huggingface/transformers/tree/master/examples#m
206206
--save_steps 100000 \
207207
--output_dir $OUT_DIR
208208
209-
We provide the fined-tuned BERT model for MRPC task `here <https://download.pytorch.org/tutorial/MRPC.zip>`_.
209+
We provide the fine-tuned BERT model for MRPC task `here <https://download.pytorch.org/tutorial/MRPC.zip>`_.
210210
To save time, you can download the model file (~400 MB) directly into your local folder ``$OUT_DIR``.
211211

212212
2.1 Set global configurations
@@ -273,7 +273,7 @@ We load the tokenizer and fine-tuned BERT sequence classifier model
273273
2.3 Define the tokenize and evaluation function
274274
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
275275

276-
We reuse the tokenize and evaluation function from `Huggingface <https://github.com/huggingface/transformers/blob/master/examples/run_glue.py>`_.
276+
We reuse the tokenize and evaluation function from `HuggingFace <https://github.com/huggingface/transformers/blob/master/examples/run_glue.py>`_.
277277

278278
.. code:: python
279279

intermediate_source/tiatoolbox_tutorial.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ The PatchPredictor class runs a CNN-based classifier written in PyTorch.
368368
369369
# Users can load any PyTorch model architecture instead using the following script
370370
model = vanilla.CNNModel(backbone="resnet18", num_classes=9) # Importing model from torchvision.models.resnet18
371-
model.load_state_dict(torch.load(weights_path, map_location="cpu"), strict=True)
371+
model.load_state_dict(torch.load(weights_path, map_location="cpu", weights_only=True), strict=True)
372372
def preproc_func(img):
373373
img = PIL.Image.fromarray(img)
374374
img = transforms.ToTensor()(img)

prototype_source/fx_graph_mode_ptq_dynamic.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ def tokenize(self, path):
171171
model.load_state_dict(
172172
torch.load(
173173
model_data_filepath + 'word_language_model_quantize.pth',
174-
map_location=torch.device('cpu')
174+
map_location=torch.device('cpu'),
175+
weights_only=True
175176
)
176177
)
177178

prototype_source/fx_graph_mode_ptq_static.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ Download the `torchvision resnet18 model <https://download.pytorch.org/models/re
157157
158158
def load_model(model_file):
159159
model = resnet18(pretrained=False)
160-
state_dict = torch.load(model_file)
160+
state_dict = torch.load(model_file, weights_only=True)
161161
model.load_state_dict(state_dict)
162162
model.to("cpu")
163163
return model
@@ -320,15 +320,15 @@ We can now print the size and accuracy of the quantized model.
320320
# ModuleAttributeError: 'ConvReLU2d' object has no attribute '_modules'
321321
# save the whole model directly
322322
# torch.save(quantized_model, fx_graph_mode_model_file_path)
323-
# loaded_quantized_model = torch.load(fx_graph_mode_model_file_path)
323+
# loaded_quantized_model = torch.load(fx_graph_mode_model_file_path, weights_only=False)
324324
325325
# save with state_dict
326326
# torch.save(quantized_model.state_dict(), fx_graph_mode_model_file_path)
327327
# import copy
328328
# model_to_quantize = copy.deepcopy(float_model)
329329
# prepared_model = prepare_fx(model_to_quantize, {"": qconfig})
330330
# loaded_quantized_model = convert_fx(prepared_model)
331-
# loaded_quantized_model.load_state_dict(torch.load(fx_graph_mode_model_file_path))
331+
# loaded_quantized_model.load_state_dict(torch.load(fx_graph_mode_model_file_path), weights_only=True)
332332
333333
# save with script
334334
torch.jit.save(torch.jit.script(quantized_model), fx_graph_mode_model_file_path)

prototype_source/pt2e_quant_ptq.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ and rename it to ``data/resnet18_pretrained_float.pth``.
274274
275275
def load_model(model_file):
276276
model = resnet18(pretrained=False)
277-
state_dict = torch.load(model_file)
277+
state_dict = torch.load(model_file, weights_only=True)
278278
model.load_state_dict(state_dict)
279279
model.to("cpu")
280280
return model

prototype_source/pt2e_quant_qat.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ prepare the data. These steps are very similar to the ones defined in the
172172
173173
def load_model(model_file):
174174
model = resnet18(pretrained=False)
175-
state_dict = torch.load(model_file)
175+
state_dict = torch.load(model_file, weights_only=True)
176176
model.load_state_dict(state_dict)
177177
return model
178178

recipes_source/intel_neural_compressor_for_pytorch.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ In this tutorial, the LeNet model is used to demonstrate how to deal with *Intel
115115
return F.log_softmax(x, dim=1)
116116
117117
model = Net()
118-
model.load_state_dict(torch.load('./lenet_mnist_model.pth'))
118+
model.load_state_dict(torch.load('./lenet_mnist_model.pth', weights_only=True))
119119
120120
The pretrained model weight `lenet_mnist_model.pth` comes from
121121
`here <https://drive.google.com/drive/folders/1fn83DF14tWmit0RTKWRhPq5uVXt73e0h?usp=sharing>`_.

recipes_source/recipes/module_load_state_dict_tips.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ def forward(self, x):
3939
# to ``torch.load``, the ``torch.device()`` context manager and the ``assign``
4040
# keyword argument to ``nn.Module.load_state_dict()``.
4141

42-
state_dict = torch.load('checkpoint.pth', mmap=True)
42+
state_dict = torch.load('checkpoint.pth', mmap=True, weights_only=True)
4343
with torch.device('meta'):
4444
meta_m = SomeModule(1000)
4545
meta_m.load_state_dict(state_dict, assign=True)
4646

4747
#############################################################################
4848
# Compare the snippet below to the one above:
4949

50-
state_dict = torch.load('checkpoint.pth')
50+
state_dict = torch.load('checkpoint.pth', weights_only=True)
5151
m = SomeModule(1000)
5252
m.load_state_dict(state_dict)
5353

@@ -71,7 +71,7 @@ def forward(self, x):
7171
# * Waiting for the entire checkpoint to be loaded into RAM before performing, for example, some per-tensor processing.
7272

7373
start_time = time.time()
74-
state_dict = torch.load('checkpoint.pth')
74+
state_dict = torch.load('checkpoint.pth', weights_only=True)
7575
end_time = time.time()
7676
print(f"loading time without mmap={end_time - start_time}")
7777

@@ -84,7 +84,7 @@ def forward(self, x):
8484
# storages will be memory-mapped.
8585

8686
start_time = time.time()
87-
state_dict = torch.load('checkpoint.pth', mmap=True)
87+
state_dict = torch.load('checkpoint.pth', mmap=True, weights_only=True)
8888
end_time = time.time()
8989
print(f"loading time with mmap={end_time - start_time}")
9090

recipes_source/recipes/save_load_across_devices.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def forward(self, x):
9797
# Load
9898
device = torch.device('cpu')
9999
model = Net()
100-
model.load_state_dict(torch.load(PATH, map_location=device))
100+
model.load_state_dict(torch.load(PATH, map_location=device, weights_only=True))
101101

102102

103103
######################################################################

0 commit comments

Comments
 (0)