op.py 12.0 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
3 4 5
# 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
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13 14
# 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.

M
minqiyang 已提交
15
import numpy as np
16

17 18
import paddle.fluid.core as core
import paddle.fluid.proto.framework_pb2 as framework_pb2
Y
Yu Yang 已提交
19 20 21


def get_all_op_protos():
22
    """
23
    Get all registered op proto from PaddlePaddle C++ end.
24
    :return: A list of registered OpProto.
25
    """
Y
Yu Yang 已提交
26 27 28
    protostrs = core.get_all_op_protos()
    ret_values = []
    for pbstr in protostrs:
29
        op_proto = framework_pb2.OpProto.FromString(bytes(pbstr))
Y
Yu Yang 已提交
30 31
        ret_values.append(op_proto)
    return ret_values
32 33


Y
Yu Yang 已提交
34
def is_str(s):
35
    return isinstance(s, str)
Y
Yu Yang 已提交
36 37


38
class OpDescCreationMethod:
39
    """
40 41
    Convert the user's input(only keyword arguments are supported) to OpDesc
    based on the OpProto.
Y
Yan Chunwei 已提交
42

43 44 45 46 47
    :param op_proto: The OpProto object.
    :type op_proto: op_proto_pb2.OpProto
    """

    def __init__(self, op_proto):
Y
Yu Yang 已提交
48
        if not isinstance(op_proto, framework_pb2.OpProto):
49
            raise TypeError(
50 51
                "Type of op_proto should be OpProto in PaddlePaddle."
            )
52
        self.__op_proto__ = op_proto
53
        self.__extra_attrs__ = core.get_op_extra_attrs(op_proto.type)
54 55 56

    def __call__(self, *args, **kwargs):
        """
57
        Convert user's input to OpDesc. Only keyword arguments are supported.
58
        :return: The OpDesc based on user input.
59 60 61
        :rtype: op_desc_pb2.OpDesc
        """
        if len(args) != 0:
62
            raise ValueError("Only keyword arguments are supported.")
Y
Yu Yang 已提交
63 64 65 66 67 68 69
        op_desc = framework_pb2.OpDesc()
        for input_parameter in self.__op_proto__.inputs:
            input_arguments = kwargs.get(input_parameter.name, [])
            if is_str(input_arguments):
                input_arguments = [input_arguments]

            if not input_parameter.duplicable and len(input_arguments) > 1:
70
                raise ValueError(
71 72 73
                    "Input %s expects only one input, but %d are given."
                    % (input_parameter.name, len(input_arguments))
                )
Y
Yu Yang 已提交
74 75 76 77 78 79 80 81 82 83 84 85

            ipt = op_desc.inputs.add()
            ipt.parameter = input_parameter.name
            ipt.arguments.extend(input_arguments)

        for output_parameter in self.__op_proto__.outputs:
            output_arguments = kwargs.get(output_parameter.name, [])
            if is_str(output_arguments):
                output_arguments = [output_arguments]

            if not output_parameter.duplicable and len(output_arguments) > 1:
                raise ValueError(
86 87 88
                    "Output %s expects only one output, but %d are given."
                    % (output_parameter.name, len(output_arguments))
                )
Y
Yu Yang 已提交
89 90 91 92

            out = op_desc.outputs.add()
            out.parameter = output_parameter.name
            out.arguments.extend(output_arguments)
93 94 95 96 97 98 99 100 101 102 103 104 105

        # Types
        op_desc.type = self.__op_proto__.type

        # Attrs
        for attr in self.__op_proto__.attrs:
            if attr.generated:
                continue
            user_defined_attr = kwargs.get(attr.name, None)
            if user_defined_attr is not None:
                new_attr = op_desc.attrs.add()
                new_attr.name = attr.name
                new_attr.type = attr.type
M
minqiyang 已提交
106 107
                if isinstance(user_defined_attr, np.ndarray):
                    user_defined_attr = user_defined_attr.tolist()
Y
Yu Yang 已提交
108
                if attr.type == framework_pb2.INT:
109
                    new_attr.i = user_defined_attr
Y
Yu Yang 已提交
110
                elif attr.type == framework_pb2.FLOAT:
111
                    new_attr.f = user_defined_attr
J
JiabinYang 已提交
112 113
                elif attr.type == framework_pb2.LONG:
                    new_attr.l = user_defined_attr
Y
Yu Yang 已提交
114
                elif attr.type == framework_pb2.STRING:
115
                    new_attr.s = user_defined_attr
116
                elif attr.type == framework_pb2.BOOLEAN:
D
dangqingqing 已提交
117
                    new_attr.b = user_defined_attr
Y
Yu Yang 已提交
118
                elif attr.type == framework_pb2.INTS:
119
                    new_attr.ints.extend(user_defined_attr)
Y
Yu Yang 已提交
120
                elif attr.type == framework_pb2.FLOATS:
121
                    new_attr.floats.extend(user_defined_attr)
Y
Yu Yang 已提交
122
                elif attr.type == framework_pb2.STRINGS:
123
                    new_attr.strings.extend(user_defined_attr)
124
                elif attr.type == framework_pb2.BOOLEANS:
D
dangqingqing 已提交
125
                    new_attr.bools.extend(user_defined_attr)
S
seiriosPlus 已提交
126 127
                elif attr.type == framework_pb2.LONGS:
                    new_attr.longs.extend(user_defined_attr)
128 129
                elif attr.type == framework_pb2.FLOAT64:
                    new_attr.float64 = user_defined_attr
130
                else:
131
                    raise NotImplementedError(
132 133
                        "A not supported attribute type: %s." % (str(attr.type))
                    )
134 135 136 137
        for attr_name, defalut_val in self.__extra_attrs__.items():
            user_defined_attr = kwargs.get(attr_name, None)
            if user_defined_attr is not None:
                attr_type = int(
138 139
                    core.get_attrtibute_type(op_desc.type, attr_name)
                )
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
                new_attr = op_desc.attrs.add()
                new_attr.name = attr_name
                new_attr.type = attr_type
                if isinstance(user_defined_attr, np.ndarray):
                    user_defined_attr = user_defined_attr.tolist()
                if attr_type == framework_pb2.INT:
                    new_attr.i = user_defined_attr
                elif attr_type == framework_pb2.FLOAT:
                    new_attr.f = user_defined_attr
                elif attr_type == framework_pb2.LONG:
                    new_attr.l = user_defined_attr
                elif attr_type == framework_pb2.STRING:
                    new_attr.s = user_defined_attr
                elif attr_type == framework_pb2.BOOLEAN:
                    new_attr.b = user_defined_attr
                elif attr_type == framework_pb2.INTS:
                    new_attr.ints.extend(user_defined_attr)
                elif attr_type == framework_pb2.FLOATS:
                    new_attr.floats.extend(user_defined_attr)
                elif attr_type == framework_pb2.STRINGS:
                    new_attr.strings.extend(user_defined_attr)
                elif attr_type == framework_pb2.BOOLEANS:
                    new_attr.bools.extend(user_defined_attr)
                elif attr_type == framework_pb2.LONGS:
                    new_attr.longs.extend(user_defined_attr)
                else:
                    raise NotImplementedError(
167 168
                        "A not supported attribute type: %s." % (str(attr_type))
                    )
169 170 171 172 173 174

        return op_desc

    @staticmethod
    def any_is_true(generator):
        """
175 176
        Reduce a boolean array to a single boolean parameter. If any element in
        the array is True, this function will return True, otherwise False.
177 178 179 180 181 182 183
        """
        for flag in generator:
            if flag:
                return True
        return False


184
class OpInfo:
185
    def __init__(self, name, method, inputs, outputs, attrs, extra_attrs):
Y
Yu Yang 已提交
186 187 188 189 190
        self.name = name
        self.method = method
        self.inputs = inputs
        self.outputs = outputs
        self.attrs = attrs
191
        self.extra_attrs = extra_attrs
Y
Yu Yang 已提交
192 193


194 195
def create_op_creation_method(op_proto):
    """
196
    Generate op creation method for an OpProto.
197 198 199 200 201 202 203
    """
    method = OpDescCreationMethod(op_proto)

    def __impl__(*args, **kwargs):
        opdesc = method(*args, **kwargs)
        return core.Operator.create(opdesc.SerializeToString())

204 205
    extra_attrs_map = core.get_op_extra_attrs(op_proto.type)

206 207 208 209 210 211 212 213
    return OpInfo(
        method=__impl__,
        name=op_proto.type,
        inputs=[(var.name, var.duplicable) for var in op_proto.inputs],
        outputs=[(var.name, var.duplicable) for var in op_proto.outputs],
        attrs=[attr.name for attr in op_proto.attrs],
        extra_attrs=[item for item in extra_attrs_map.keys()],
    )
214 215


216
class OperatorFactory:
217 218 219 220 221
    def __init__(self):
        self.op_methods = dict()

        for op_proto in get_all_op_protos():
            method = create_op_creation_method(op_proto)
Y
Yu Yang 已提交
222
            self.op_methods[method.name] = method
Y
Yu Yang 已提交
223

224
    def __call__(self, *args, **kwargs):
225
        if "type" in kwargs:
226
            if len(args) != 0:
227
                raise ValueError(
228
                    "Except the argument \"type\","
229 230
                    "all of the other arguments should be keyword arguments."
                )
231
            t = kwargs.pop("type")
232 233
        else:
            if len(args) != 1:
234
                raise ValueError(
235
                    "Except the argument \"type\","
236 237
                    "all of the other arguments should be keyword arguments."
                )
238
            t = args[0]
239

Y
Yu Yang 已提交
240
        return self.get_op_info(t).method(**kwargs)
241

Y
Yu Yang 已提交
242
    def types(self):
243
        return list(self.op_methods.keys())
Y
Yu Yang 已提交
244

Y
Yu Yang 已提交
245
    def get_op_info(self, t):
246
        if t not in self.op_methods:
247
            raise ValueError("The operator: %s is not registered." % t)
248
        return self.op_methods.get(t)
249

250
    def get_op_input_names(self, type):
251
        return [x[0] for x in self.get_op_info(type).inputs]
252 253

    def get_op_inputs(self, type):
Y
Yu Yang 已提交
254
        return self.get_op_info(type).inputs
255

256
    def get_op_output_names(self, type):
257
        return [x[0] for x in self.get_op_info(type).outputs]
258 259

    def get_op_outputs(self, type):
Y
Yu Yang 已提交
260
        return self.get_op_info(type).outputs
261

262
    def get_op_attr_names(self, type):
Y
Yu Yang 已提交
263
        return self.get_op_info(type).attrs
264

265 266 267
    def get_op_extra_attr_names(self, type):
        return self.get_op_info(type).extra_attrs

268

269
class __RecurrentOp__:
Y
Yan Chunwei 已提交
270
    __proto__ = None
271
    type = "recurrent"
Y
Yan Chunwei 已提交
272 273 274 275 276 277 278 279 280

    def __init__(self):
        # cache recurrent_op's proto
        if self.__proto__ is None:
            for op_proto in get_all_op_protos():
                if op_proto.type == self.type:
                    self.__proto__ = op_proto

    def __call__(self, *args, **kwargs):
281 282
        if self.type not in args and "type" not in kwargs:
            kwargs["type"] = self.type
Y
Yan Chunwei 已提交
283 284 285 286 287 288 289
        # create proto
        create_method = OpDescCreationMethod(self.__proto__)
        proto = create_method(*args, **kwargs)
        # create rnnop
        return core.RecurrentOp.create(proto.SerializeToString())


290
class __DynamicRecurrentOp__:
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
    __proto__ = None
    type = "dynamic_recurrent"

    def __init__(self):
        # cache recurrent_op's proto
        if self.__proto__ is None:
            for op_proto in get_all_op_protos():
                if op_proto.type == self.type:
                    self.__proto__ = op_proto

    def __call__(self, *args, **kwargs):
        if self.type not in args and "type" not in kwargs:
            kwargs["type"] = self.type
        # create proto
        create_method = OpDescCreationMethod(self.__proto__)
        proto = create_method(*args, **kwargs)
        # create rnnop
        return core.DynamicRecurrentOp.create(proto.SerializeToString())


311
class __CondOp__:
Z
cond op  
zchen0211 已提交
312
    __proto__ = None
Z
zchen0211 已提交
313
    type = "cond"
Z
cond op  
zchen0211 已提交
314 315 316 317 318 319 320 321 322

    def __init__(self):
        # cache recurrent_op's proto
        if self.__proto__ is None:
            for op_proto in get_all_op_protos():
                if op_proto.type == self.type:
                    self.__proto__ = op_proto

    def __call__(self, *args, **kwargs):
Z
zchen0211 已提交
323 324
        if self.type not in args and "type" not in kwargs:
            kwargs["type"] = self.type
Z
cond op  
zchen0211 已提交
325 326 327 328 329 330 331
        # create proto
        create_method = OpDescCreationMethod(self.__proto__)
        proto = create_method(*args, **kwargs)
        # create condop
        return core.CondOp.create(proto.SerializeToString())


332
Operator = OperatorFactory()  # The default global factory
Y
Yan Chunwei 已提交
333
RecurrentOp = __RecurrentOp__()
334
DynamicRecurrentOp = __DynamicRecurrentOp__()
Z
cond op  
zchen0211 已提交
335
CondOp = __CondOp__()