theseus_layer.py 5.4 KB
Newer Older
W
weishengyu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
from abc import ABC
from paddle import nn
import re


class Identity(nn.Layer):
    def __init__(self):
        super(Identity, self).__init__()

    def forward(self, inputs):
        return inputs


W
dbg  
weishengyu 已提交
14
class TheseusLayer(nn.Layer):
W
weishengyu 已提交
15
    def __init__(self, *args, **kwargs):
W
weishengyu 已提交
16
        super(TheseusLayer, self).__init__()
W
dbg  
weishengyu 已提交
17
        self.res_dict = {}
W
weishengyu 已提交
18

19 20
    # stop doesn't work when stop layer has a parallel branch.
    def stop_after(self, stop_layer_name: str):
W
weishengyu 已提交
21 22 23 24
        after_stop = False
        for layer_i in self._sub_layers:
            if after_stop:
                self._sub_layers[layer_i] = Identity()
25 26 27 28 29 30
                continue
            layer_name = self._sub_layers[layer_i].full_name()
            if layer_name == stop_layer_name:
                after_stop = True
                continue
            if isinstance(self._sub_layers[layer_i], TheseusLayer):
D
dongshuilong 已提交
31 32
                after_stop = self._sub_layers[layer_i].stop_after(
                    stop_layer_name)
33 34
        return after_stop

W
weishengyu 已提交
35
    def update_res(self, return_patterns):
W
weishengyu 已提交
36
        if not return_patterns or isinstance(self, WrapLayer):
W
weishengyu 已提交
37
            return
38 39
        for layer_i in self._sub_layers:
            layer_name = self._sub_layers[layer_i].full_name()
W
weishengyu 已提交
40
            if isinstance(self._sub_layers[layer_i], (nn.Sequential, nn.LayerList)):
W
weishengyu 已提交
41
                self._sub_layers[layer_i] = wrap_theseus(self._sub_layers[layer_i], self.res_dict)
W
weishengyu 已提交
42
                self._sub_layers[layer_i].update_res(return_patterns)
W
weishengyu 已提交
43 44 45 46
            else:
                for return_pattern in return_patterns:
                    if re.match(return_pattern, layer_name):
                        if not isinstance(self._sub_layers[layer_i], TheseusLayer):
W
weishengyu 已提交
47 48 49 50
                            self._sub_layers[layer_i] = wrap_theseus(self._sub_layers[layer_i], self.res_dict)
                        else:
                            self._sub_layers[layer_i].res_dict = self.res_dict

W
weishengyu 已提交
51 52
                        self._sub_layers[layer_i].register_forward_post_hook(
                            self._sub_layers[layer_i]._save_sub_res_hook)
W
weishengyu 已提交
53 54 55
            if isinstance(self._sub_layers[layer_i], TheseusLayer):
                self._sub_layers[layer_i].res_dict = self.res_dict
                self._sub_layers[layer_i].update_res(return_patterns)
W
weishengyu 已提交
56

W
weishengyu 已提交
57
    def _save_sub_res_hook(self, layer, input, output):
W
dbg  
weishengyu 已提交
58
        self.res_dict[layer.full_name()] = output
W
weishengyu 已提交
59

W
weishengyu 已提交
60 61 62 63 64 65
    def _return_dict_hook(self, layer, input, output):
        res_dict = {"output": output}
        for res_key in list(self.res_dict):
            res_dict[res_key] = self.res_dict.pop(res_key)
        return res_dict

W
weishengyu 已提交
66 67 68
    def replace_sub(self, layer_name_pattern, replace_function, recursive=True):
        for layer_i in self._sub_layers:
            layer_name = self._sub_layers[layer_i].full_name()
W
weishengyu 已提交
69
            if re.match(layer_name_pattern, layer_name):
W
weishengyu 已提交
70
                self._sub_layers[layer_i] = replace_function(self._sub_layers[layer_i])
D
dongshuilong 已提交
71
            if recursive:
W
weishengyu 已提交
72 73
                if isinstance(self._sub_layers[layer_i], TheseusLayer):
                    self._sub_layers[layer_i].replace_sub(
D
dongshuilong 已提交
74
                        layer_name_pattern, replace_function, recursive)
W
weishengyu 已提交
75 76 77
                elif isinstance(self._sub_layers[layer_i], (nn.Sequential, nn.LayerList)):
                    for layer_j in self._sub_layers[layer_i]._sub_layers:
                        self._sub_layers[layer_i]._sub_layers[layer_j].replace_sub(
D
dongshuilong 已提交
78
                            layer_name_pattern, replace_function, recursive)
W
weishengyu 已提交
79 80 81 82 83 84 85 86 87 88 89 90

    '''
    example of replace function:
    def replace_conv(origin_conv: nn.Conv2D):
        new_conv = nn.Conv2D(
            in_channels=origin_conv._in_channels,
            out_channels=origin_conv._out_channels,
            kernel_size=origin_conv._kernel_size,
            stride=2
        )
        return new_conv

D
dongshuilong 已提交
91
        '''
W
weishengyu 已提交
92 93 94


class WrapLayer(TheseusLayer):
W
weishengyu 已提交
95
    def __init__(self, sub_layer, res_dict=None):
W
weishengyu 已提交
96 97 98
        super(WrapLayer, self).__init__()
        self.sub_layer = sub_layer
        self.name = sub_layer.full_name()
W
weishengyu 已提交
99 100
        if res_dict is not None:
            self.res_dict = res_dict
W
weishengyu 已提交
101 102 103 104 105

    def full_name(self):
        return self.name

    def forward(self, *inputs, **kwargs):
W
dbg  
weishengyu 已提交
106
        return self.sub_layer(*inputs, **kwargs)
W
weishengyu 已提交
107

W
weishengyu 已提交
108 109 110 111 112
    def update_res(self, return_patterns):
        if not return_patterns or not isinstance(self.sub_layer, (nn.Sequential, nn.LayerList)):
            return
        for layer_i in self.sub_layer._sub_layers:
            if isinstance(self.sub_layer._sub_layers[layer_i], (nn.Sequential, nn.LayerList)):
W
weishengyu 已提交
113
                self.sub_layer._sub_layers[layer_i] = wrap_theseus(self.sub_layer._sub_layers[layer_i], self.res_dict)
W
weishengyu 已提交
114
                self.sub_layer._sub_layers[layer_i].update_res(return_patterns)
W
weishengyu 已提交
115 116
            elif isinstance(self.sub_layer._sub_layers[layer_i], TheseusLayer):
                self.sub_layer._sub_layers[layer_i].res_dict = self.res_dict
W
dbg  
weishengyu 已提交
117

W
weishengyu 已提交
118 119 120
            layer_name = self.sub_layer._sub_layers[layer_i].full_name()
            for return_pattern in return_patterns:
                if re.match(return_pattern, layer_name):
W
dbg  
weishengyu 已提交
121
                    self.sub_layer._sub_layers[layer_i].register_forward_post_hook(
W
dbg  
weishengyu 已提交
122
                        self._sub_layers[layer_i]._save_sub_res_hook)
W
weishengyu 已提交
123

W
weishengyu 已提交
124 125 126 127
            if isinstance(self.sub_layer._sub_layers[layer_i], TheseusLayer):
                self.sub_layer._sub_layers[layer_i].update_res(return_patterns)


W
weishengyu 已提交
128 129
def wrap_theseus(sub_layer, res_dict=None):
    wrapped_layer = WrapLayer(sub_layer, res_dict)
W
weishengyu 已提交
130
    return wrapped_layer