|
|
|
@ -4,8 +4,8 @@ import paddle.v2.framework.proto.framework_pb2 as framework_pb2
|
|
|
|
|
|
|
|
|
|
def get_all_op_protos():
|
|
|
|
|
"""
|
|
|
|
|
Get all registered op proto from Paddle C++
|
|
|
|
|
:return: list of OpProto
|
|
|
|
|
Get all registered op proto from PaddlePaddle C++ end.
|
|
|
|
|
: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 to convert user input(use key word args) to OpDesc based on
|
|
|
|
|
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
|
|
|
|
@ -30,17 +30,18 @@ class OpDescCreationMethod(object):
|
|
|
|
|
|
|
|
|
|
def __init__(self, op_proto):
|
|
|
|
|
if not isinstance(op_proto, framework_pb2.OpProto):
|
|
|
|
|
raise TypeError("Argument should be OpProto")
|
|
|
|
|
raise TypeError(
|
|
|
|
|
"Type of op_proto should be OpProto in PaddlePaddle.")
|
|
|
|
|
self.__op_proto__ = op_proto
|
|
|
|
|
|
|
|
|
|
def __call__(self, *args, **kwargs):
|
|
|
|
|
"""
|
|
|
|
|
Convert user input to OpDesc. Only key-word args are supported.
|
|
|
|
|
:return: OpDesc based on user input
|
|
|
|
|
Convert user's input to OpDesc. Only keyword arguments are supported.
|
|
|
|
|
:return: The OpDesc based on user input.
|
|
|
|
|
:rtype: op_desc_pb2.OpDesc
|
|
|
|
|
"""
|
|
|
|
|
if len(args) != 0:
|
|
|
|
|
raise ValueError("Only keyword arguments is supported by Paddle")
|
|
|
|
|
raise ValueError("Only keyword arguments are supported.")
|
|
|
|
|
op_desc = framework_pb2.OpDesc()
|
|
|
|
|
|
|
|
|
|
for input_parameter in self.__op_proto__.inputs:
|
|
|
|
@ -49,8 +50,9 @@ class OpDescCreationMethod(object):
|
|
|
|
|
input_arguments = [input_arguments]
|
|
|
|
|
|
|
|
|
|
if not input_parameter.duplicable and len(input_arguments) > 1:
|
|
|
|
|
raise ValueError("Input %s only accepts one input, but give %d"
|
|
|
|
|
% (input_parameter.name, len(input_arguments)))
|
|
|
|
|
raise ValueError(
|
|
|
|
|
"Input %s expects only one input, but %d are given." %
|
|
|
|
|
(input_parameter.name, len(input_arguments)))
|
|
|
|
|
|
|
|
|
|
ipt = op_desc.inputs.add()
|
|
|
|
|
ipt.parameter = input_parameter.name
|
|
|
|
@ -63,7 +65,7 @@ class OpDescCreationMethod(object):
|
|
|
|
|
|
|
|
|
|
if not output_parameter.duplicable and len(output_arguments) > 1:
|
|
|
|
|
raise ValueError(
|
|
|
|
|
"Output %s only accepts one output, but give %d" %
|
|
|
|
|
"Output %s expects only one output, but %d are given." %
|
|
|
|
|
(output_parameter.name, len(output_arguments)))
|
|
|
|
|
|
|
|
|
|
out = op_desc.outputs.add()
|
|
|
|
@ -100,15 +102,17 @@ class OpDescCreationMethod(object):
|
|
|
|
|
pair.first = p[0]
|
|
|
|
|
pair.second = p[1]
|
|
|
|
|
else:
|
|
|
|
|
raise NotImplementedError("Not support attribute type " +
|
|
|
|
|
str(attr.type))
|
|
|
|
|
raise NotImplementedError(
|
|
|
|
|
"A not supported attribute type: %s." % (
|
|
|
|
|
str(attr.type)))
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
@ -127,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)
|
|
|
|
|
|
|
|
|
@ -146,20 +150,23 @@ def create_op_creation_method(op_proto):
|
|
|
|
|
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 Paddle argument should be key-word "
|
|
|
|
|
"argument except type")
|
|
|
|
|
t = kwargs.pop('type')
|
|
|
|
|
raise ValueError(
|
|
|
|
|
"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 Paddle argument should be key-word "
|
|
|
|
|
"argument except type")
|
|
|
|
|
raise ValueError(
|
|
|
|
|
"Except the argument \"type\","
|
|
|
|
|
"all of the other arguments should be keyword arguments.")
|
|
|
|
|
t = args[0]
|
|
|
|
|
|
|
|
|
|
return self.get_op_info(t).method(**kwargs)
|
|
|
|
@ -169,7 +176,7 @@ class OperatorFactory(object):
|
|
|
|
|
|
|
|
|
|
def get_op_info(self, t):
|
|
|
|
|
if t not in self.op_methods:
|
|
|
|
|
raise ValueError("operator %s is not registered", t)
|
|
|
|
|
raise ValueError("The operator: %s is not registered." % t)
|
|
|
|
|
return self.op_methods.get(t)
|
|
|
|
|
|
|
|
|
|
def get_op_input_names(self, type):
|
|
|
|
@ -184,7 +191,7 @@ class OperatorFactory(object):
|
|
|
|
|
|
|
|
|
|
class __RecurrentOp__(object):
|
|
|
|
|
__proto__ = None
|
|
|
|
|
type = 'recurrent'
|
|
|
|
|
type = "recurrent"
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
# cache recurrent_op's proto
|
|
|
|
@ -194,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)
|
|
|
|
@ -203,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__()
|
|
|
|
|