topology.py 4.0 KB
Newer Older
Q
qiaolongfei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

Q
qiaolongfei 已提交
15 16 17 18 19
import collections

from paddle.proto.ModelConfig_pb2 import ModelConfig

import layer as v2_layer
Q
qiaolongfei 已提交
20 21 22 23

__all__ = ['Topology']


D
update  
dangqingqing 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36
def __flatten__(lis):
    """
    Given a list, possibly nested to any level, return it flattened.
    """
    new_lis = []
    for item in lis:
        if isinstance(item, collections.Sequence):
            new_lis.extend(__flatten__(item))
        else:
            new_lis.append(item)
    return new_lis


37
def __bfs_travel__(callback, *layers):
D
update  
dangqingqing 已提交
38
    layers = __flatten__(layers)
39 40 41 42
    for each_layer in layers:
        __break__ = callback(each_layer)
        if __break__:
            return
43 44
        __layers__ = each_layer.__parent_layers__.values() + \
                     each_layer.extra_parent()
45
        __bfs_travel__(callback, *__layers__)
46 47


Q
qiaolongfei 已提交
48 49 50 51 52 53
class Topology(object):
    """
    Topology is used to store the information about all layers
    and network configs.
    """

54 55 56 57 58 59 60 61 62 63
    def __init__(self, layers, extra_layers=None):
        def __check__(layers):
            if not isinstance(layers, collections.Sequence):
                __check_layer_type__(layers)
                layers = [layers]
            for layer in layers:
                __check_layer_type__(layer)
            return layers

        layers = __check__(layers)
Q
qiaolongfei 已提交
64
        self.layers = layers
65 66 67 68
        if extra_layers is not None:
            extra_layers = __check__(extra_layers)

        self.__model_config__ = v2_layer.parse_network(
D
dangqingqing 已提交
69
            layers, extra_layers=extra_layers)
D
bug fix  
dangqingqing 已提交
70 71 72 73

        if extra_layers is not None:
            self.layers.extend(extra_layers)

Q
qiaolongfei 已提交
74
        assert isinstance(self.__model_config__, ModelConfig)
Q
qiaolongfei 已提交
75

Q
qiaolongfei 已提交
76 77 78 79 80
    def use_sparse_updater(self):
        """
        check if any parameter require to use sparse_update
        :return:
        """
81
        use_sparse = False
Q
qiaolongfei 已提交
82 83
        for parameter in self.__model_config__.parameters:
            if parameter.sparse_update or parameter.sparse_remote_update:
84 85 86
                use_sparse = True
                break
        return use_sparse
Q
qiaolongfei 已提交
87

Q
qiaolongfei 已提交
88
    def proto(self):
Q
qiaolongfei 已提交
89 90 91
        return self.__model_config__

    def get_layer(self, name):
Q
qiaolongfei 已提交
92 93 94 95 96
        """
        get v2.Layer Class instance by layer name
        :param name:
        :return:
        """
97
        result_layer = [None]
Q
qiaolongfei 已提交
98

99 100 101 102 103
        def __impl__(l):
            if l.name == name:
                result_layer[0] = l
                return True  # break
            return False
Q
qiaolongfei 已提交
104

105 106 107
        __bfs_travel__(__impl__, *self.layers)
        if result_layer[0] is None:
            raise ValueError("No such layer %s" % name)
Q
qiaolongfei 已提交
108 109
        return result_layer[0]

Q
qiaolongfei 已提交
110
    def data_layers(self):
Q
qiaolongfei 已提交
111 112 113 114
        """
        get all data layer
        :return:
        """
115
        data_layers = dict()
Q
qiaolongfei 已提交
116

117 118 119
        def __impl__(l):
            if isinstance(l, v2_layer.DataLayerV2):
                data_layers[l.name] = l
Q
qiaolongfei 已提交
120

121
        __bfs_travel__(__impl__, *self.layers)
Q
qiaolongfei 已提交
122 123
        return data_layers

Q
qiaolongfei 已提交
124 125
    def data_type(self):
        """
Q
qiaolongfei 已提交
126 127
        get data_type from proto, such as:
        [('image', dense_vector(768)), ('label', integer_value(10))]
Q
qiaolongfei 已提交
128
        """
D
dangqingqing 已提交
129
        data_layers = self.data_layers()
130 131
        return [(nm, data_layers[nm].type)
                for nm in self.proto().input_layer_names]
Q
qiaolongfei 已提交
132

Q
qiaolongfei 已提交
133 134 135 136 137 138
    def get_layer_proto(self, name):
        for layer in self.__model_config__.layers:
            if layer.name == name:
                return layer
        return None

Q
qiaolongfei 已提交
139

Q
qiaolongfei 已提交
140 141 142
def __check_layer_type__(layer):
    if not isinstance(layer, v2_layer.LayerV2):
        raise ValueError('layer should have type paddle.layer.Layer')