o
    Ne1                     @   s   d dl mZ d dlZd dlZd dlm  mZ d dlm  m	  m
Z
 dd Zdd ZG dd deZG d	d
 d
eZdd ZG dd deZG dd deZG dd deZG dd deZe Ze Ze Ze ZdS )    )print_functionNc                  C   s6   t  } g }| D ]}tjt|}|| q|S )zk
    Get all registered op proto from PaddlePaddle C++ end.
    :return: A list of registered OpProto.
    )coreget_all_op_protosframework_pb2OpProtoZ
FromStringsixbinary_typeappend)Z	protostrsZ
ret_valuesZpbstrop_proto r   ?D:\Projects\ConvertPro\env\Lib\site-packages\paddle/fluid/op.pyr      s   r   c                 C   s   t | tjS N)
isinstancer   string_types)sr   r   r   is_str%      r   c                   @   s,   e Zd ZdZdd Zdd Zedd ZdS )	OpDescCreationMethodz
    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
    c                 C   s,   t |tjs
td|| _t|j| _d S )Nz3Type of op_proto should be OpProto in PaddlePaddle.)	r   r   r   	TypeError__op_proto__r   get_op_extra_attrstype__extra_attrs__selfr
   r   r   r   __init__2   s   zOpDescCreationMethod.__init__c                 O   s  t |dkr
tdt }| jjD ]3}||jg }t|r"|g}|j	s6t |dkr6td|jt |f |j
 }|j|_|j| q| jjD ]3}||jg }t|rZ|g}|j	snt |dkrntd|jt |f |j
 }	|j|	_|	j| qJ| jj|_| jjD ]}
|
jrq||
jd}|dur:|j
 }|
j|_|
j|_t|tjr| }|
jtjkr||_q|
jtjkr||_q|
jtjkr||_q|
jtjkr||_q|
jtjkr||_q|
jtj kr|j!| q|
jtj"kr|j#| q|
jtj$kr
|j%| q|
jtj&kr|j'| q|
jtj(kr&|j)| q|
jtj*kr1||_+qt,dt-|
j q| j./ D ]\}}||d}|durt0t12|j|}|j
 }||_||_t|tjrn| }|tjkry||_q@|tjkr||_q@|tjkr||_q@|tjkr||_q@|tjkr||_q@|tj kr|j!| q@|tj"kr|j#| q@|tj$kr|j%| q@|tj&kr|j'| q@|tj(kr|j)| q@t,dt-| q@|S )z
        Convert user's input to OpDesc. Only keyword arguments are supported.
        :return: The OpDesc based on user input.
        :rtype: op_desc_pb2.OpDesc
        r   z%Only keyword arguments are supported.   z2Input %s expects only one input, but %d are given.z4Output %s expects only one output, but %d are given.Nz#A not supported attribute type: %s.)3len
ValueErrorr   ZOpDescr   inputsgetnamer   
duplicableaddZ	parameter	argumentsextendoutputsr   attrs	generatedr   npZndarraytolistINTiFLOATfLONGlSTRINGr   BOOLEANbZINTSZintsZFLOATSZfloatsZSTRINGSstringsZBOOLEANSZboolsZLONGSZlongsZFLOAT64Zfloat64NotImplementedErrorstrr   itemsintr   Zget_attrtibute_type)r   argskwargsZop_descZinput_parameterZinput_argumentsZiptZoutput_parameterZoutput_argumentsoutattrZuser_defined_attrnew_attrZ	attr_nameZdefalut_valZ	attr_typer   r   r   __call__9   s   




 






!zOpDescCreationMethod.__call__c                 C   s   | D ]}|r	 dS qdS )z
        Reduce a boolean array to a single boolean parameter. If any element in
        the array is True, this function will return True, otherwise False.
        TFr   )	generatorflagr   r   r   any_is_true   s
   z OpDescCreationMethod.any_is_trueN)__name__
__module____qualname____doc__r   r>   staticmethodrA   r   r   r   r   r   )   s    rr   c                   @   s   e Zd Zdd ZdS )OpInfoc                 C   s(   || _ || _|| _|| _|| _|| _d S r   )r!   methodr   r&   r'   extra_attrs)r   r!   rH   r   r&   r'   rI   r   r   r   r      s   
zOpInfo.__init__N)rB   rC   rD   r   r   r   r   r   rG      s    rG   c              	      sh   t |   fdd}t| j}t|| jdd | jD dd | jD dd | jD dd | D dS )	z5
    Generate op creation method for an OpProto.
    c                     s    | i |}t j| S r   )r   OperatorcreateSerializeToString)r9   r:   ZopdescrH   r   r   __impl__   s   z+create_op_creation_method.<locals>.__impl__c                 S      g | ]}|j |jfqS r   r!   r"   .0varr   r   r   
<listcomp>       z-create_op_creation_method.<locals>.<listcomp>c                 S   rO   r   rP   rQ   r   r   r   rT      rU   c                 S   s   g | ]}|j qS r   )r!   )rR   r<   r   r   r   rT      s    c                 S   s   g | ]}|qS r   r   )rR   itemr   r   r   rT      s    )rH   r!   r   r&   r'   rI   )	r   r   r   r   rG   r   r&   r'   keys)r
   rN   Zextra_attrs_mapr   rM   r   create_op_creation_method   s   rX   c                   @   s\   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd ZdS )OperatorFactoryc                 C   s,   t  | _t D ]}t|}|| j|j< qd S r   )dict
op_methodsr   rX   r!   )r   r
   rH   r   r   r   r      s
   
zOperatorFactory.__init__c                 O   sZ   d|v rt |dkrtd|d}nt |dkrtd|d }| |jdi |S )Nr   r   zRExcept the argument "type",all of the other arguments should be keyword arguments.r   r   )r   r   popget_op_inforH   )r   r9   r:   tr   r   r   r>      s   zOperatorFactory.__call__c                 C   s   t | j S r   )listr[   rW   )r   r   r   r   types   s   zOperatorFactory.typesc                 C   s"   || j vrtd| | j |S )Nz#The operator: %s is not registered.)r[   r   r    )r   r^   r   r   r   r]      s   
zOperatorFactory.get_op_infoc                 C      dd |  |jD S )Nc                 S      g | ]}|d  qS r   r   rR   xr   r   r   rT          z6OperatorFactory.get_op_input_names.<locals>.<listcomp>r]   r   r   r   r   r   r   get_op_input_names      z"OperatorFactory.get_op_input_namesc                 C      |  |jS r   rg   rh   r   r   r   get_op_inputs   r   zOperatorFactory.get_op_inputsc                 C   ra   )Nc                 S   rb   rc   r   rd   r   r   r   rT      rf   z7OperatorFactory.get_op_output_names.<locals>.<listcomp>r]   r&   rh   r   r   r   get_op_output_names   rj   z#OperatorFactory.get_op_output_namesc                 C   rk   r   rm   rh   r   r   r   get_op_outputs  r   zOperatorFactory.get_op_outputsc                 C   rk   r   )r]   r'   rh   r   r   r   get_op_attr_names  r   z!OperatorFactory.get_op_attr_namesc                 C   rk   r   )r]   rI   rh   r   r   r   get_op_extra_attr_names  r   z'OperatorFactory.get_op_extra_attr_namesN)rB   rC   rD   r   r>   r`   r]   ri   rl   rn   ro   rp   rq   r   r   r   r   rY      s    rY   c                   @   $   e Zd ZdZdZdd Zdd ZdS )__RecurrentOp__NZ	recurrentc                 C   0   | j d u rt D ]}|j| jkr|| _ qd S d S r   	__proto__r   r   r   r   r   r   r        

z__RecurrentOp__.__init__c                 O   D   | j |vrd|vr| j |d< t| j}||i |}tj| S Nr   )r   r   rv   r   RecurrentOprK   rL   r   r9   r:   Zcreate_methodprotor   r   r   r>     
   

z__RecurrentOp__.__call__rB   rC   rD   rv   r   r   r>   r   r   r   r   rs     
    rs   c                   @   rr   )__DynamicRecurrentOp__NZdynamic_recurrentc                 C   rt   r   ru   r   r   r   r   r   %  rw   z__DynamicRecurrentOp__.__init__c                 O   rx   ry   )r   r   rv   r   DynamicRecurrentOprK   rL   r{   r   r   r   r>   ,  r}   z__DynamicRecurrentOp__.__call__r~   r   r   r   r   r   !  r   r   c                   @   rr   )
__CondOp__NZcondc                 C   rt   r   ru   r   r   r   r   r   :  rw   z__CondOp__.__init__c                 O   rx   ry   )r   r   rv   r   CondOprK   rL   r{   r   r   r   r>   A  r}   z__CondOp__.__call__r~   r   r   r   r   r   6  r   r   )
__future__r   numpyr)   r   Zpaddle.fluid.coreZfluidr   Z paddle.fluid.proto.framework_pb2r|   r   r   r   objectr   rG   rX   rY   rs   r   r   rJ   rz   r   r   r   r   r   r   <module>   s&    4
