
    sg                        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y)    )annotations)	dataclass)CallableSequenceTYPE_CHECKING)ArgumentBaseTyBaseTypeListTypeNativeFunctionOptionalTypeType)BindingCType
NamedCTypez
	c                J    | j                   j                  j                         S )N)funcnameunambiguous_name)fs    S/var/www/html/venv/lib/python3.12/site-packages/torchgen/executorch/api/unboxing.pyr   r      s    66;;''))    T)frozenc                      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	y)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_genc                   t        t        |            D cg c]  }d||   j                   d| d }}g }|D ]  }t        |j                  t
              st        d|       |j                  }| j                  |j                  |j                  |j                        \  }}}	}
|j                  |
       |j                  |	       |j                  |j                  |              ||fS c c}w )NzEValue& z
 = *stack[z];z7Unexpected argument type, expecting `Argument` but got mutable)rangelenr   
isinstanceargumentr   	Exceptionargumenttype_evalue_converttypeis_writeextendappend	with_name)selfargsi	code_listbinding_listargr#   unboxed_name_codedecls              r   convert_argumentszUnboxing.convert_arguments:   s     HMSQUYGWX!xQ~Zs"=X	X 	=CcllH5McUS  "%H*.*J*Jx}}h6G6G +K +'L!T4 T"T"l ;<	= Y&& Ys   C0Fr   c               ~   | j                  |||      j                  }t        |t              r| d}| j	                  |||      \  }}nmt        |t
              r| d}| j                  ||||      \  }}n?t        |t              r| d}| j                  ||||      \  }}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)arg_nameout_namectype_opt_out)r9   r:   tr;   	_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+   r=   r9   r   r;   r:   r3   r4   s           r   r%   z$Unboxing.argumenttype_evalue_convertN   s     &&q'&JOOa""5)H11!HE 2 JD$ <("8,H55!H 6 JD$ 8$"9-H11!H 2 JD$ %aSXJ?  d**r   c           	     `    |j                          d| d| d|j                  d       dgg fS )N  = z.to<T	strip_refz>();)cpp_type)r+   r9   r:   r;   s       r   r?   zUnboxing._gen_code_base_typeq   sH     ~~ (3xjU^^VZ^=[<\\`a
 	r   c                    | d}| j                  |j                  |      \  }}}}	d| d| d|j                  d       dj                  d      |	fS )	N_opt_in

    auto rD   z.toOptional<TrE   z>();
            
)r%   elemrG   split)
r+   r9   r:   r=   r;   in_nameres_name	base_typeres_coder4   s
             r   r@   z Unboxing._gen_code_optional_typex   s~     Jg&.2.N.NFFG/
+)Xt

3xjY-?-?$-?-O,P Q 
 	
r   c                   | d}| d}g }| j                  |j                  |      \  }}	}
}t        |j                  t              rR|j                  j                  t
        j                  k(  r+|j                  d| d| dj                  d             ||fS t        |j                  t              ry|j                  j                  t
        j                  k(  s'|j                  j                  t
        j                  k(  r+|j                  d| d| dj                  d             ||fS t        |j                  t              rR|j                  j                  t
        j                  k(  r+|j                  d| d| dj                  d             ||fS t        |j                  t              rt|j                  j                  t
        j                  k(  rM|j                  d	|j                   d
| d| d| d| d| d| d| d| d| dj                  d             ||fS t        |j                  t              rt        |j                  j                  t              rq|j                  j                  j                  t
        j                  k(  r@|j                  d| d| d| d| d| d| d| d| d| dj                  d             ||fS |dz   }|j                  d|	j!                  d       d
| d       |j                  d| d| dt"        j%                  |
       d| d| d |j!                  d       d!| d"| d#j                  d             ||fS )$N_list_in_elemrJ   rD   z!.toTensorList();
                rK   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<TrE   ;z
    for (EValue z) {
        z	
        z);
    }
    rC   (z);
                )r%   rL   r"   r
   r   r	   Tensorr(   rM   intSymIntfloatboolsizer   r)   rG   	connectorjoin)r+   r9   r:   r=   r;   rN   	elem_namer3   rO   	res_ctyperQ   r4   vec_names                r   rA   zUnboxing._gen_code_list_type   s    Jh'j&	.2.N.NFFI/
+)Xt affh'AFFKK6==,HKK

3xj !Ef TzY )FFKK6::%)EKK

3xj !ER TzE )affkkV\\.IKK

3xj !EB Tzu )affkkV[[.HKK&&H: &Yc( +Ry !Ji	{ # Zs8*  Ep TzK qvv|,166;;1  FMM1KKYc( ((0z 2+Ry !Jk) % Zs8* 
 EB Tz!  &(HKKy11D1AB"XJaP KK2gY '			!" #		
+hZ (
^^d^#$AhZq
 ; E
 Tzr   N)r,   zSequence[Binding]returnztuple[list[Binding], list[str]])r=   r   r9   strr   r\   rc   z'tuple[str, CType, list[str], list[str]])r9   rd   r:   rd   r;   r   rc   tuple[list[str], list[str]])
r9   rd   r:   rd   r=   r   r;   r   rc   re   )
r9   rd   r:   rd   r=   r   r;   r   rc   re   )
__name__
__module____qualname____doc____annotations__r5   r%   r?   r@   rA    r   r   r   r      s    * '%'	('* :?!+!+!$!+26!+	0!+F'*38	$

'*
/;
DI
	$
 ^^'*^/7^@E^	$^r   r   N)r   r   rc   rd   )
__future__r   dataclassesr   typingr   r   r   torchgen.modelr   r	   r
   r   r   r   r   torchgen.api.typesr   r   r   r^   r   r   rk   r   r   <module>rq      sX    " ! 4 4   == 	* $H H Hr   