File size: 1,492 Bytes
0070fce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import pickle
import torch


class Empty:
    pass


class RestrictedUnpickler(pickle.Unpickler):

    def find_class(self, module: str, name: str):
        if module.startswith("pytorch_lightning"):
            return Empty

        if module.startswith(("collections", "torch", "numpy", "__builtin__")):
            return super().find_class(module, name)

        raise NotImplementedError(f'"{module}.{name}" is forbidden')


class Extra:
    """

    A class for temporarily setting the global handler for when you can't explicitly call load_with_extra

    (because it's not your code making the torch.load call). The intended use is like this:



    ```

    import torch

    from modules import safe



    def handler(module, name):

        if module == "torch" and name in ["float64", "float16"]:

            return getattr(torch, name)



        return None



    with safe.Extra(handler):

        x = torch.load("model.pt")

    ```

    """

    def __init__(self, handler):
        self.handler = handler

    def __enter__(self):
        global global_extra_handler

        assert global_extra_handler is None, "already inside an Extra() block"
        global_extra_handler = self.handler

    def __exit__(self, exc_type, exc_val, exc_tb):
        global global_extra_handler

        global_extra_handler = None


load = torch.load
unsafe_torch_load = torch.load
global_extra_handler = None

Unpickler = RestrictedUnpickler