MCPcopy Create free account
hub / github.com/pytorch/pytorch / test_pickle

Method test_pickle

test/test_modules.py:215–243  ·  view source on GitHub ↗
(self, device, dtype, module_info, training)

Source from the content-addressed store, hash-verified

213
214 @modules(module_db)
215 def test_pickle(self, device, dtype, module_info, training):
216 # Test that module can be pickled and unpickled.
217 module_cls = module_info.module_cls
218 module_inputs = module_info.module_inputs_func(module_info, device=device, dtype=dtype,
219 requires_grad=False, training=training)
220 for module_input in module_inputs:
221 if module_input.forward_input is None:
222 continue
223
224 args, kwargs = module_input.constructor_input.args, module_input.constructor_input.kwargs
225
226 with freeze_rng_state():
227 # === Instantiate the module. ===
228 args, kwargs = module_input.constructor_input.args, module_input.constructor_input.kwargs
229 m = module_cls(*args, **kwargs)
230 m.to(device).to(dtype)
231 m.train(training)
232
233 # === Do forward pass. ===
234 args, kwargs = module_input.forward_input.args, module_input.forward_input.kwargs
235 output = m(*args, **kwargs)
236
237 # === Check unpickled module gives the same output. ===
238 with tempfile.TemporaryFile() as f:
239 torch.save(m, f)
240 f.seek(0)
241 m_copy = torch.load(f)
242 output_from_copy = m_copy(*args, **kwargs)
243 self.assertEqual(output, output_from_copy)
244
245 @skipMeta
246 @modules([module_info for module_info in module_db

Callers

nothing calls this directly

Calls 8

freeze_rng_stateFunction · 0.90
mFunction · 0.85
seekMethod · 0.80
toMethod · 0.45
trainMethod · 0.45
saveMethod · 0.45
loadMethod · 0.45
assertEqualMethod · 0.45

Tested by

no test coverage detected