Skip to content

Commit acdc91b

Browse files
svekarsc-p-i-o
authored andcommitted
Add weights_only=True to torch.load (#3012)
* Add weights_only=True to torch.load
1 parent 9d97d8f commit acdc91b

22 files changed

+50
-38
lines changed

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

intermediate_source/autograd_saved_tensors_hooks_tutorial.py

+3-3
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

+1-1
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/tiatoolbox_tutorial.rst

+1-1
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

+2-1
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

+3-3
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

+1-1
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

+1-1
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

+1-1
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

+4-4
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

+1-1
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
######################################################################

recipes_source/recipes/saving_and_loading_a_general_checkpoint.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def forward(self, x):
131131
model = Net()
132132
optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)
133133

134-
checkpoint = torch.load(PATH)
134+
checkpoint = torch.load(PATH, weights_only=True)
135135
model.load_state_dict(checkpoint['model_state_dict'])
136136
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
137137
epoch = checkpoint['epoch']

recipes_source/recipes/saving_and_loading_models_for_inference.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def forward(self, x):
117117

118118
# Load
119119
model = Net()
120-
model.load_state_dict(torch.load(PATH))
120+
model.load_state_dict(torch.load(PATH, weights_only=True))
121121
model.eval()
122122

123123

recipes_source/recipes/saving_multiple_models_in_one_file.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def forward(self, x):
128128
optimModelA = optim.SGD(modelA.parameters(), lr=0.001, momentum=0.9)
129129
optimModelB = optim.SGD(modelB.parameters(), lr=0.001, momentum=0.9)
130130

131-
checkpoint = torch.load(PATH)
131+
checkpoint = torch.load(PATH, weights_only=True)
132132
modelA.load_state_dict(checkpoint['modelA_state_dict'])
133133
modelB.load_state_dict(checkpoint['modelB_state_dict'])
134134
optimizerA.load_state_dict(checkpoint['optimizerA_state_dict'])

recipes_source/recipes/warmstarting_model_using_parameters_from_a_different_model.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def forward(self, x):
124124
# are loading into.
125125
#
126126

127-
netB.load_state_dict(torch.load(PATH), strict=False)
127+
netB.load_state_dict(torch.load(PATH, weights_only=True), strict=False)
128128

129129

130130
######################################################################

0 commit comments

Comments
 (0)