|
|
|
@ -5,7 +5,7 @@ import paddle.v2.framework.proto.framework_pb2 as framework_pb2
|
|
|
|
|
def get_all_op_protos():
|
|
|
|
|
"""
|
|
|
|
|
Get all registered op proto from PaddlePaddle C++ end.
|
|
|
|
|
:return: list of OpProto
|
|
|
|
|
:return: A list of registered OpProto.
|
|
|
|
|
"""
|
|
|
|
|
protostrs = core.get_all_op_protos()
|
|
|
|
|
ret_values = []
|
|
|
|
@ -21,8 +21,8 @@ def is_str(s):
|
|
|
|
|
|
|
|
|
|
class OpDescCreationMethod(object):
|
|
|
|
|
"""
|
|
|
|
|
A Functor object converting the user's input(only keyword arguments are
|
|
|
|
|
supported) to OpDesc based on the OpProto.
|
|
|
|
|
Convert the user's input(only keyword arguments are supported) to OpDesc
|
|
|
|
|
based on the OpProto.
|
|
|
|
|
|
|
|
|
|
:param op_proto: The OpProto object.
|
|
|
|
|
:type op_proto: op_proto_pb2.OpProto
|
|
|
|
@ -37,7 +37,7 @@ class OpDescCreationMethod(object):
|
|
|
|
|
def __call__(self, *args, **kwargs):
|
|
|
|
|
"""
|
|
|
|
|
Convert user's input to OpDesc. Only keyword arguments are supported.
|
|
|
|
|
:return: OpDesc based on user input
|
|
|
|
|
:return: The OpDesc based on user input.
|
|
|
|
|
:rtype: op_desc_pb2.OpDesc
|
|
|
|
|
"""
|
|
|
|
|
if len(args) != 0:
|
|
|
|
@ -54,7 +54,7 @@ class OpDescCreationMethod(object):
|
|
|
|
|
"Input %s expects only one input, but %d are given." %
|
|
|
|
|
(input_parameter.name, len(input_arguments)))
|
|
|
|
|
|
|
|
|
|
ipt = op_desc.inputs.add()
|
|
|
|
|
ipt = op_desc.inputs.add()
|
|
|
|
|
ipt.parameter = input_parameter.name
|
|
|
|
|
ipt.arguments.extend(input_arguments)
|
|
|
|
|
|
|
|
|
@ -68,7 +68,7 @@ class OpDescCreationMethod(object):
|
|
|
|
|
"Output %s expects only one output, but %d are given." %
|
|
|
|
|
(output_parameter.name, len(output_arguments)))
|
|
|
|
|
|
|
|
|
|
out = op_desc.outputs.add()
|
|
|
|
|
out = op_desc.outputs.add()
|
|
|
|
|
out.parameter = output_parameter.name
|
|
|
|
|
out.arguments.extend(output_arguments)
|
|
|
|
|
|
|
|
|
@ -106,12 +106,13 @@ class OpDescCreationMethod(object):
|
|
|
|
|
"A not supported attribute type: %s." % (
|
|
|
|
|
str(attr.type)))
|
|
|
|
|
|
|
|
|
|
return op_desc
|
|
|
|
|
return op_desc
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def any_is_true(generator):
|
|
|
|
|
"""
|
|
|
|
|
Reduce a bool array to one. If any of them is True, then return True.
|
|
|
|
|
Reduce a boolean array to a single boolean parameter. If any element in
|
|
|
|
|
the array is True, this function will return True, otherwise False.
|
|
|
|
|
"""
|
|
|
|
|
for flag in generator:
|
|
|
|
|
if flag:
|
|
|
|
@ -130,7 +131,7 @@ class OpInfo(object):
|
|
|
|
|
|
|
|
|
|
def create_op_creation_method(op_proto):
|
|
|
|
|
"""
|
|
|
|
|
Generate op creation method for an OpProto
|
|
|
|
|
Generate op creation method for an OpProto.
|
|
|
|
|
"""
|
|
|
|
|
method = OpDescCreationMethod(op_proto)
|
|
|
|
|
|
|
|
|
@ -145,27 +146,28 @@ def create_op_creation_method(op_proto):
|
|
|
|
|
outputs=[var.name for var in op_proto.outputs],
|
|
|
|
|
attrs=[attr.name for attr in op_proto.attrs])
|
|
|
|
|
|
|
|
|
|
class OperatorFactory(object):
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.op_methods = dict()
|
|
|
|
|
|
|
|
|
|
class OperatorFactory(object):
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.op_methods = dict()
|
|
|
|
|
|
|
|
|
|
for op_proto in get_all_op_protos():
|
|
|
|
|
method = create_op_creation_method(op_proto)
|
|
|
|
|
self.op_methods[method.name] = method
|
|
|
|
|
|
|
|
|
|
def __call__(self, *args, **kwargs):
|
|
|
|
|
if 'type' in kwargs:
|
|
|
|
|
if "type" in kwargs:
|
|
|
|
|
if len(args) != 0:
|
|
|
|
|
raise ValueError(
|
|
|
|
|
("All PaddlePaddle arguments should be keyword "
|
|
|
|
|
"arguments except the argument \"type\"."))
|
|
|
|
|
t = kwargs.pop('type')
|
|
|
|
|
"Except the argument \"type\","
|
|
|
|
|
"all of the other arguments should be keyword arguments.")
|
|
|
|
|
t = kwargs.pop("type")
|
|
|
|
|
else:
|
|
|
|
|
if len(args) != 1:
|
|
|
|
|
raise ValueError(
|
|
|
|
|
("All PaddlePaddle arguments should be keyword "
|
|
|
|
|
"arguments except the argument \"type\"."))
|
|
|
|
|
t = args[0]
|
|
|
|
|
"Except the argument \"type\","
|
|
|
|
|
"all of the other arguments should be keyword arguments.")
|
|
|
|
|
t = args[0]
|
|
|
|
|
|
|
|
|
|
return self.get_op_info(t).method(**kwargs)
|
|
|
|
|
|
|
|
|
@ -189,7 +191,7 @@ def create_op_creation_method(op_proto):
|
|
|
|
|
|
|
|
|
|
class __RecurrentOp__(object):
|
|
|
|
|
__proto__ = None
|
|
|
|
|
type = 'recurrent'
|
|
|
|
|
type = "recurrent"
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
# cache recurrent_op's proto
|
|
|
|
@ -199,8 +201,8 @@ class __RecurrentOp__(object):
|
|
|
|
|
self.__proto__ = op_proto
|
|
|
|
|
|
|
|
|
|
def __call__(self, *args, **kwargs):
|
|
|
|
|
if self.type not in args and 'type' not in kwargs:
|
|
|
|
|
kwargs['type'] = self.type
|
|
|
|
|
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)
|
|
|
|
@ -208,5 +210,5 @@ class __RecurrentOp__(object):
|
|
|
|
|
return core.RecurrentOp.create(proto.SerializeToString())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Operator = OperatorFactory() # Default global factory
|
|
|
|
|
Operator = OperatorFactory() # The default global factory
|
|
|
|
|
RecurrentOp = __RecurrentOp__()
|
|
|
|
|