
    Χg                        d dl mZ d dlmZ d dlmZmZmZ d dlm	Z	m
Z
mZmZmZmZmZ er
d dlmZmZmZ dZddZ ed           G d d                      ZdS )    )annotations)	dataclass)CallableSequenceTYPE_CHECKING)ArgumentBaseTyBaseTypeListTypeNativeFunctionOptionalTypeType)BindingCType
NamedCTypez
	fr   returnstrc                >    | j         j                                        S )N)funcnameunambiguous_name)r   s    \/var/www/html/ai-engine/env/lib/python3.11/site-packages/torchgen/executorch/api/unboxing.pyr   r      s    6;'')))    T)frozenc                  L    e Zd ZU dZded<   ddZd	d
ddZddZd dZd!dZ	dS )"Unboxinga  
    Takes a sequence of Bindings and unbox EValues to these Bindings. Return generated code that performs correct unboxing.
    A sample generated code:
    // aten::mul.out(Tensor self, Tensor other, *, Tensor(a!) out) -> Tensor(a!)
    void mul_out(EValue** stack) {
        EValue& self = *stack[0];
        EValue& other = *stack[1];
        EValue& out = *stack[2];
        const torch::executor::Tensor & self_base = self.to<torch::executor::Tensor>();
        const torch::executor::Tensor & other_base = other.to<torch::executor::Tensor>();
        torch::executor::Tensor & out_base = out.to<torch::executor::Tensor>();

        EXECUTORCH_SCOPE_PROF("native_call_mul.out");
        torch::executor::mul_outf(self_base, other_base, out_base);


    }
    zCallable[..., NamedCType]argument_type_genargsSequence[Binding]r   tuple[list[Binding], list[str]]c                   fdt          t                              D             }g }D ]}t          |j        t                    st          d|           |j        }|                     |j        |j        |j	                  \  }}}}	|
                    |	           |
                    |           |                    |                    |                     ||fS )Nc                8    g | ]}d |         j          d| dS )zEValue& z
 = *stack[z];)r   ).0ir   s     r   
<listcomp>z.Unboxing.convert_arguments.<locals>.<listcomp>=   s3    XXX!=Q=====XXXr   z7Unexpected argument type, expecting `Argument` but got mutable)rangelen
isinstanceargumentr   	Exceptionargumenttype_evalue_converttyper   is_writeextendappend	with_name)
selfr   	code_listbinding_listargr,   unboxed_name_codedecls
    `        r   convert_argumentszUnboxing.convert_arguments:   s    YXXXuSQUYYGWGWXXX	 	= 	=CclH55 ScSS   "%H*.*J*Jx}h6G +K + +'L!T4 T"""T"""l ; ;<<<<Y&&r   Fr'   tr   arg_namer   r(   bool'tuple[str, CType, list[str], list[str]]c                  |                      |||          j        }t          |t                    r!| d}|                     |||          \  }}nt          |t
                    r"| d}|                     ||||          \  }}nLt          |t                    r"| d}|                     ||||          \  }}nt          d| d|           ||||fS )	a  
        Takes in the type, name and mutability corresponding to an argument, and generates a tuple of:
        (1) the C++ code necessary to unbox the argument
        (2) A Binding corresponding to the newly created unboxed variable, including variable name and its CType
        :param t: a `Type` of an argument
        :param arg_name: argument name
        :param mutable: boolean for whether this argument type is mutable
        :return: unboxed result
        )r(   binds_base)r>   out_namectype_opt_out)r>   rD   r=   rE   	_list_outzCannot handle type z. arg_name: )
r   r/   r+   r
   _gen_code_base_typer   _gen_code_optional_typer   _gen_code_list_typer-   )r4   r=   r>   r(   rE   rD   r:   r;   s           r   r.   z$Unboxing.argumenttype_evalue_convertN   s6    &&q'&JJOa"" 	")))H11!HE 2  JD$$ <(( 	",,,H55!H 6  JD$$ 8$$ 	"---H11!H 2  JD$$ ?a??X??   d**r   rD   rE   r   tuple[list[str], list[str]]c           	     p    |                                  d| d| d|                     d           dgg fS )N  = z.to<T	strip_refz>();)cpp_type)r4   r>   rD   rE   s       r   rH   zUnboxing._gen_code_base_typeq   sS     ~~aa(aaxaaU^^VZ^=[=[aaa
 	r   r   c                    | d}|                      |j        |          \  }}}}	d| d| d|                    d           d                    d          |	fS )	N_opt_in

    auto rN   z.toOptional<TrO   z>();
            
)r.   elemrQ   split)
r4   r>   rD   r=   rE   in_nameres_name	base_typeres_coder;   s
             r   rI   z Unboxing._gen_code_optional_typex   s     &&&.2.N.NFG/
 /
+)Xt
  -6-?-?$-?-O-O    
 	
r   r   c                v   | d}| d}g }|                      |j        |          \  }}	}
}t          |j        t                    rK|j        j        t
          j        k    r1|                    d| d| d                    d                     nt          |j        t                    re|j        j        t
          j	        k    s|j        j        t
          j
        k    r1|                    d| d| d                    d                     n&t          |j        t                    rK|j        j        t
          j        k    r1|                    d| d| d                    d                     nt          |j        t                    rh|j        j        t
          j        k    rN|                    d	|j         d
| d| d| d| d| d| d| d| d| d                    d                     n?t          |j        t                    rt          |j        j        t                    rd|j        j        j        t
          j        k    rE|                    d| d| d| d| d| d| d| d| d| d                    d                     n|dz   }|                    d|	                    d           d
| d           |                    d| d| dt"                              |
           d| d| d |                    d           d!| d"| d#                    d                     ||fS )$N_list_in_elemrT   rN   z!.toTensorList();
                rU   z.toIntList();
                z!.toDoubleList();
                z&
#ifdef USE_ATEN_LIB
std::array<bool, z> z;
auto z(.toBoolList();
size_t _i = 0;
for (auto z: z) {
    z	[_i++] = z;
}
#else
auto z&.toBoolList();
#endif
                z
#ifdef USE_ATEN_LIB
auto z@.toListOptionalTensor();
c10::List<::std::optional<at::Tensor>> z;
for (auto z.push_back(z);
}
#else
auto z0.toListOptionalTensor();
#endif
                _veczstd::vector<TrO   ;z
    for (EValue z) {
        z	
        z);
    }
    rM   (z);
                )r.   rV   r+   r
   r   r	   Tensorr1   rW   intSymIntfloatr?   sizer   r2   rQ   	connectorjoin)r4   r>   rD   r=   rE   rX   	elem_namer:   rY   	res_ctyper[   r;   vec_names                r   rJ   zUnboxing._gen_code_list_type   s    '''&&&	.2.N.NFI/
 /
+)Xt afh'' S	AFK6=,H,HKK
   E     )) K	FK6:%%)E)EKK
   E     )) A	afkV\.I.IKK
   E     )) 9	afkV[.H.HKK& $     
  
      "       E    & qv|,,%	16;11%	  FM11KK

 

 
 )1
 
 	
 
  	
 

 
 

 $
 
 
 
 
 
 
 E    "  &(HKKPy11D1AAPPXPPP   KK % 		!	!  
  ' 
 ^^d^## 
 '/ 
 2:   E 
 
 
 Tzr   N)r   r    r   r!   )r=   r   r>   r   r(   r?   r   r@   )r>   r   rD   r   rE   r   r   rK   )
r>   r   rD   r   r=   r   rE   r   r   rK   )
r>   r   rD   r   r=   r   rE   r   r   rK   )
__name__
__module____qualname____doc____annotations__r<   r.   rH   rI   rJ    r   r   r   r      s          *   ' ' ' '* :?!+ !+ !+ !+ !+ !+F   
 
 
 
 ^ ^ ^ ^ ^ ^r   r   N)r   r   r   r   )
__future__r   dataclassesr   typingr   r   r   torchgen.modelr   r	   r
   r   r   r   r   torchgen.api.typesr   r   r   rg   r   r   rq   r   r   <module>rw      s:   " " " " " " ! ! ! ! ! ! 4 4 4 4 4 4 4 4 4 4                   >========== 	* * * * $H H H H H H H H H Hr   