U
    5-eT                     @   s   d dl mZ d dlmZmZmZ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 dZeedddZed	d
G dd dZdS )    )	dataclass)CallableListSequenceTuple)BindingCType
NamedCType)ArgumentBaseTyBaseTypeListTypeNativeFunctionOptionalTypeTypez
	)freturnc                 C   s   | j j S )N)funcnameZunambiguous_name)r    r   a/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/torchgen/executorch/api/unboxing.pyr      s    r   T)frozenc                
   @   s   e Zd ZU dZedef ed< ee e	e
e e
e f dddZddeeee	eee
e e
e f d	d
dZeeee	e
e e
e f dddZeeeee	e
e e
e f dddZeeeee	e
e e
e f d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);


    }
    .argument_type_gen)argsr   c           
         s    fddt t D }g } D ]f}t|jts@td| |j}| j|j|j|j	d\}}}}	|
|	 |
| ||| q"||fS )Nc                    s$   g | ]}d  | j  d| dqS )zEValue& z
 = *stack[z];)r   ).0ir   r   r   
<listcomp>7   s     z.Unboxing.convert_arguments.<locals>.<listcomp>z7Unexpected argument type, expecting `Argument` but got mutable)rangelen
isinstanceargumentr
   	Exceptionargumenttype_evalue_converttyper   Zis_writeextendappend	with_name)
selfr   Z	code_listZbinding_listargr$   Zunboxed_name_codedeclr   r   r   convert_arguments4   s"      

zUnboxing.convert_argumentsFr   )targ_namer    r   c                C   s   | j |||dj}t|tr<| d}| j|||d\}}nlt|trh| d}| j||||d\}}n@t|tr| d}| j||||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    Zbinds_base)r2   out_namectypeZ_opt_out)r2   r4   r1   r5   Z	_list_outzCannot handle type z. arg_name: )
r   r'   r#   r   _gen_code_base_typer   _gen_code_optional_typer   _gen_code_list_typer%   )r+   r1   r2   r    r5   r4   r.   r/   r   r   r   r&   H   s4    

  

   

   z$Unboxing.argumenttype_evalue_convert)r2   r4   r5   r   c              	   C   s.   |   d| d| d|j dd dgg fS )N  = z.to<TZ	strip_refz>();)cpp_type)r+   r2   r4   r5   r   r   r   r6   i   s    &zUnboxing._gen_code_base_type)r2   r4   r1   r5   r   c           
   
   C   sX   | d}|  |j|\}}}}	d|jdd d| d| d|jdd d	d	|	fS )
NZ_opt_in
    Tr;   r9   r:   z.toOptional<z>();
            
)r&   elemr<   split)
r+   r2   r4   r1   r5   in_nameres_nameZ	base_typeres_coder/   r   r   r   r7   p   s$    
 

z Unboxing._gen_code_optional_typec                 C   sd  | d}| d}g }|  |j|\}}	}
}t|jtrx|jjtjkrx|d|jdd d| d| d	d	 nt|jtr|jjtj
ks|jjtjkr|d|jdd d| d| d
	d	 nt|jtr|jjtjkr|d|jdd d| d| d	d	 n>t|jtrj|jjtjkrj|d|jdd d| d| d	d	 nt|jtrt|jjtr|jjjtjkr|d| d| d| d| d| d| d| d| d| d	d	 nx|d }|d|	jdd d| d |d| d| dt|
 d| d| d|jdd d| d| d	d	 ||fS )NZ_list_in_elemr=   Tr;   r9   r:   z!.toTensorList();
                r>   z.toIntList();
                z!.toDoubleList();
                z.toBoolList();
                z=
#ifdef USE_ATEN_LIB
at::ArrayRef<c10::optional<at::Tensor>> z>.toListOptionalTensor();
c10::List<c10::optional<at::Tensor>> z;
for (auto z: z) {
    z.push_back(zY);
}
#else
torch::executor::ArrayRef<torch::executor::optional<torch::executor::Tensor>> z0.toListOptionalTensor();
#endif
                Z_veczstd::vector<z> ;z
    for (EValue z) {
        z	
        z);
    }
    (z);
                )r&   r?   r#   r   r   r   ZTensorr(   r<   r@   intZSymIntfloatboolr   r)   	connectorjoin)r+   r2   r4   r1   r5   rA   Z	elem_namer.   rB   Z	res_ctyperC   r/   Zvec_namer   r   r   r8      s    

 






zUnboxing._gen_code_list_typeN)__name__
__module____qualname____doc__r   r	   __annotations__r   r   r   r   strr0   r   rI   r   r&   r6   r   r7   r   r8   r   r   r   r   r      sB   
  "        r   N)dataclassesr   typingr   r   r   r   Ztorchgen.api.typesr   r   r	   Ztorchgen.modelr
   r   r   r   r   r   r   rJ   rQ   r   r   r   r   r   r   <module>   s   $
