U
    d-                     @   s<  d dl mZmZmZmZ d dlmZmZmZm	Z	m
Z
mZmZmZmZmZ d dlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z! da"edddZ#eddd	d
Z$eddZ%eeeeeef dddZ&ee'dddZ(ee'dddZ)ee'dddZ*ee'dddZ+G dd dZ,G dd dZ-dS )    )ListUnionTupleOptional)
TypeBaseTyBaseTypeOptionalTypeListTypeOperatorNameFunctionSchemaReturnTensorOptionsArgumentsArgument)CTypeBaseCppType	BaseCTypeOptionalCType
NamedCTypedeviceTlayoutTVectorCTypeboolTlongTdoubleT	ListCTypestringTscalarTscalarTypeTmemoryFormatTSymIntTNreturnc                   C   s   t stdt S )NzHThe value type needs to be set with setValueT() in run_gen_lazy_tensor())_valueTNotImplementedError r%   r%   5/tmp/pip-unpacked-wheel-ua33x9lu/torchgen/api/lazy.py	getValueT&   s
    r'   )valr"   c                 C   s   | a d S N)r#   )r(   r%   r%   r&   	setValueT0   s    r*   ztorch::lazyValue)typr"   c                 C   s  t | tr| jtjkr"tt S | jtjkr8tt S | jtjkrLtt	S | jtj
kr`ttS | jtjkrvtt S | jtjkrttS | jtjkrttS | jtjkrttS | jtjkrttS | jtjkrttS | jtjk rttS tdt|  nt | trtt| jS t | trtt| jdkrLt ttt S t| jdkrdtt!S t"t| jS ntdt|  dS )a  
    This function takes a type from NativeFunctions and converts it for use with
    lazy tensor codegen.

    Type conversion for lazy currently consists of
     (1) changing at::Tensors into lazy::Values
     (2) wrapping everything in a BaseCType
     (3) making cpp-reference types into cpp-value types (e.g. vector instead of IntArrayRef)

    (1) converts at::Tensors to lazy::Values (which wrap lazy::Nodes, with which Lazy IR represents tensors.)
    There is special handling for Optional[Tensor] or List[Tensor], etc- hence 'tensor-like'

    This is incomplete- there are assertions in places that it's expected to need to add
    more types as the codegen is used with more operators.
    zTODO add support for type zTensor?Tensorzunrecognized type N)#
isinstancer   namer   r-   r   r'   ScalarZ
ScalarTyper   intr   SymIntboolr   floatr   strr   ZDevicer   ZLayoutr   ZMemoryFormatr   AssertionErrorreprr	   r   process_ir_typeelemr
   r   tensorListValueTr   r,   r%   r%   r&   r8   :   sB    


r8   c                 C   sH   t | tr*| jt kp(| jtkp(| jtkS t | tttfrDt	| j
S dS )z
    Given a type, determine if it is a Value-like type.  This is equivalent to
    being Tensor-like, but assumes the type has already been transformed.
    F)r.   r   typer'   r   r    r   r   r   isValueTyper9   r;   r%   r%   r&   r=   v   s
    
 
r=   c                 C   s   t | to| jtjkS r)   )r.   r   r/   r   r2   r;   r%   r%   r&   isSymIntType   s    r>   c                 C   s2   t | tr| jtjkS t | ttfr.t| jS dS )a  
    Given a type, determine if it is a c10::scalar which we will wrap in a lazy Value.
    Since we literally change the type from scalarT to valueT, information is lost.
    This function helps build a list of wrapped scalars to save that information
    F)	r.   r   r/   r   r0   r	   r
   isWrappedScalarTyper9   r;   r%   r%   r&   r?      s
    

r?   c                 C   s.   t | tr| jtjkS t | tr*t| jS dS )NF)r.   r   r/   r   	Generatorr	   isGeneratorTyper9   r;   r%   r%   r&   rA      s
    


rA   c                   @   sj   e Zd ZU eed< eed< ee ed< eed< eed< eed< eed< e	dd	d
Z
eedddZdS )LazyArgumentr/   	orig_type
lazy_type_is_wrapped_scalaris_generatoris_symint_or_listis_lazy_value)argc                 C   s   |j | _ |j| _t|jt| _t|j| _| jrF| js>tdd | _	nt
|j| _	t|j| _t|j| _| j ozt| j| _d S )Nz>We expect all generators are optional since currently they are)r/   r<   rC   r.   r	   Zis_optionalrA   rF   r6   rD   r8   r?   rE   r>   rG   r=   	lazy_typerH   )selfrI   r%   r%   r&   __init__   s    zLazyArgument.__init__r!   c                 C   s    | j d k	std| j | j S )Nz3Attempted to access lazy_type for invalid argument )rD   r6   r/   rK   r%   r%   r&   rJ      s
    
zLazyArgument.lazy_typeN)__name__
__module____qualname__r5   __annotations__r   r   r   r3   r   rL   propertyrJ   r%   r%   r%   r&   rB      s   
rB   c                   @   s  e Zd ZU ded< eedf ed< eedf ed< ed ed< dZee ed	< e	d
ddZ
eedddZeedddZeedddZd!eeeeeee dddZeee dddZeee dddZeee dddZeee ddd ZdS )"LazyIrSchemar   r/   .positional_argskeyword_args)r   .returnsNgenerator_arg)funcc                 C   s  g }dD ]b}|dkr>|j jd k	r>t|j dj}|t| qt|j |d k	r|dd t|j |D  qt|| _g }dD ]t}t|j |}|d k	r~t	|t
r| }|D ]0}t|jr| jd kstdt|j|j| _q|dd |D  q~t|| _|j| _|j| _d S )N)Zpre_self_positionalself_argZpost_self_positionalrY   c                 S   s   g | ]}t |qS r%   rB   .0rI   r%   r%   r&   
<listcomp>   s     z)LazyIrSchema.__init__.<locals>.<listcomp>)Zpre_tensor_options_kwarg_onlyZtensor_optionsZpost_tensor_options_kwarg_onlyoutz)We expect there is only one generator argc                 S   s   g | ]}t |qS r%   rZ   r[   r%   r%   r&   r]      s     )	argumentsrY   getattrargumentappendrB   extendtuplerT   r.   r   allrA   r<   rW   r6   r   r/   rU   rV   )rK   rX   rT   Z	arg_fieldrI   rU   Z	curr_argsr%   r%   r&   rL      s6    



zLazyIrSchema.__init__r!   c                 C   s4   | j j  d| j j  }ddd |dD S )z
        Return camel-case version of op in node.

        Note: This function also appends any `overload_name` in the operation.
        For example, if the op is `bitwise_and.Tensor`, the returned name
        will be `BitwiseAndTensor`.
        _ c                 s   s   | ]}|  pd V  qdS )rg   N)
capitalize)r\   wordr%   r%   r&   	<genexpr>  s     z)LazyIrSchema.node_name.<locals>.<genexpr>)r/   Zoverload_namelowerjoinsplit)rK   Zop_namer%   r%   r&   	node_name   s    	zLazyIrSchema.node_namec                 C   s
   | j j  S r)   )r/   rM   r%   r%   r&   	aten_name  s    zLazyIrSchema.aten_namec                 C   s   | j j j S r)   )r/   baserM   r%   r%   r&   	base_name  s    zLazyIrSchema.base_nameTF)
positionalkeywordvaluesscalars	generatorr"   c                    sv   g }|r| | j |r$| | j |r4|r4 r4|S |rJ|rJdd |D S |r\dd |D S |rr fdd|D S g S )Nc                 S   s   g | ]}|j s|qS r%   )rF   r\   ar%   r%   r&   r]   %  s      z.LazyIrSchema.filtered_args.<locals>.<listcomp>c                 S   s   g | ]}|j r|qS r%   )rH   rw   r%   r%   r&   r]   '  s      c                    s    g | ]}|j s s|js|qS r%   )rH   rF   rw   rv   r%   r&   r]   )  s
     )rc   rT   rU   )rK   rr   rs   rt   ru   rv   argsr%   ry   r&   filtered_args  s     
zLazyIrSchema.filtered_argsc                 C   s   | j dddddS NTFrr   rs   rt   ru   r{   rM   r%   r%   r&   positional_values1  s       zLazyIrSchema.positional_valuesc                 C   s   | j dddddS r|   r~   rM   r%   r%   r&   positional_scalars7  s       zLazyIrSchema.positional_scalarsc                 C   s   | j dddddS NFTr}   r~   rM   r%   r%   r&   keyword_values=  s       zLazyIrSchema.keyword_valuesc                 C   s   | j dddddS r   r~   rM   r%   r%   r&   keyword_scalarsC  s       zLazyIrSchema.keyword_scalars)TTTTF)rN   rO   rP   rQ   r   rB   rW   r   r   r   rL   rR   r5   rn   ro   rq   r3   r   r{   r   r   r   r   r%   r%   r%   r&   rS      sB   
#     "rS   ).typingr   r   r   r   Ztorchgen.modelr   r   r   r	   r
   r   r   r   r   r   Ztorchgen.api.typesr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r#   r'   r*   r:   r8   r3   r=   r>   r?   rA   rB   rS   r%   r%   r%   r&   <module>   s   0L

<+