File tree 2 files changed +16
-0
lines changed
2 files changed +16
-0
lines changed Original file line number Diff line number Diff line change @@ -383,6 +383,15 @@ def fn(x):
383
383
files = zipfile.ZipFile(buf).filelist
384
384
self.assertTrue(any(['archive/constants.pkl' == f.filename for f in files]))
385
385
386
+ def test_script_fn_pkl(self):
387
+ with self.assertRaisesRegex(pickle.PickleError, "ScriptFunction cannot be pickled"):
388
+
389
+ @torch.jit.script
390
+ def fn(x: torch.Tensor) -> torch.Tensor:
391
+ return x
392
+
393
+ pkl_fn = pickle.dumps(fn, protocol=0)
394
+
386
395
def test_restore_device(self):
387
396
class M(torch.jit.ScriptModule):
388
397
def __init__(self, cpu_device_str):
Original file line number Diff line number Diff line change 56
56
"""
57
57
set_module (ScriptFunction , "torch.jit" )
58
58
59
+ # Throws an error if a jit function is pickled.
60
+ # Helps to avoid Python crashes for Python versions 3.9.5 + when protocol 0 or 1 is given as an argument.
61
+ def _reduce (cls ):
62
+ raise pickle .PickleError ("ScriptFunction cannot be pickled" )
63
+
64
+ ScriptFunction .__reduce__ = _reduce # type: ignore[assignment]
65
+
59
66
60
67
if _enabled :
61
68
Attribute = collections .namedtuple ("Attribute" , ["value" , "type" ])
You can’t perform that action at this time.
0 commit comments