o
    vZh                     @  s   d dl mZ d dlmZ d dlmZmZ d dlmZm	Z	m
Z
mZmZmZmZ er8d dlmZ d dlmZmZmZ dZdddZeddG dd dZdS )    )annotations)	dataclass)CallableTYPE_CHECKING)ArgumentBaseTyBaseTypeListTypeNativeFunctionOptionalTypeType)Sequence)BindingCType
NamedCTypez
	fr
   returnstrc                 C  s   | j j S )N)funcnameZunambiguous_name)r    r   O/var/www/auris/lib/python3.10/site-packages/torchgen/executorch/api/unboxing.pyr      s   r   T)frozenc                   @  sR   e Zd ZU dZded< d"dd	Zd
dd#ddZd$ddZd%ddZ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);


    }
    zCallable[..., NamedCType]argument_type_genargsSequence[Binding]r   tuple[list[Binding], list[str]]c           
        s    fddt t D }g } D ]3}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>?   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_arguments<   s   

zUnboxing.convert_argumentsFr"   tr   arg_namer   r#   bool'tuple[str, CType, list[str], list[str]]c                C  s   | j |||dj}t|tr| d}| j|||d\}}n6t|tr4| d}| j||||d\}}n t|trJ| 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)r5   out_namectypeZ_opt_out)r5   r9   r4   r:   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.   r4   r5   r#   r:   r9   r1   r2   r   r   r   r)   P   s(   





z$Unboxing.argumenttype_evalue_convertr9   r:   r   tuple[list[str], list[str]]c              	   C  s.   |   d| d| d|j dd dgg fS )N  = z.to<TZ	strip_refz>();)cpp_type)r.   r5   r9   r:   r   r   r   r;   s   s   &zUnboxing._gen_code_base_typer   c           
      C  sJ   | d}|  |j|\}}}}	d| d| d|jdd dd|	fS )	NZ_opt_in

    auto r@   z.toOptional<TrA   z>();
            
)r)   elemrB   split)
r.   r5   r9   r4   r:   in_nameres_nameZ	base_typeres_coder2   r   r   r   r<   z   s   

z Unboxing._gen_code_optional_typer	   c                 C  sh  | d}| d}g }|  |j|\}}	}
}t|jtr7|jjtjkr7|d| d| dd ||fS t|jtr^|jjtj	ksK|jjtj
kr^|d| d| dd ||fS t|jtr~|jjtjkr~|d| d| dd ||fS t|jtr|jjtjkr|d	|j d
| d| d| d| d| d| d| d| d| dd ||fS t|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 ||fS |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_elemrC   r@   z!.toTensorList();
                rD   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
                Z_veczstd::vector<TrA   ;z
    for (EValue z) {
        z	
        z);
    }
    r?   (z);
                )r)   rE   r&   r   r   r   ZTensorr+   rF   intZSymIntfloatr6   sizer   r,   rB   	connectorjoin)r.   r5   r9   r4   r:   rG   Z	elem_namer1   rH   Z	res_ctyperI   r2   Zvec_namer   r   r   r=      s   

G?9		2


	zUnboxing._gen_code_list_typeN)r   r   r   r   )r4   r   r5   r   r#   r6   r   r7   )r5   r   r9   r   r:   r   r   r>   )
r5   r   r9   r   r4   r   r:   r   r   r>   )
r5   r   r9   r   r4   r	   r:   r   r   r>   )
__name__
__module____qualname____doc____annotations__r3   r)   r;   r<   r=   r   r   r   r   r      s   
 

#
r   N)r   r
   r   r   )
__future__r   dataclassesr   typingr   r   Ztorchgen.modelr   r   r   r	   r
   r   r   collections.abcr   Ztorchgen.api.typesr   r   r   rP   r   r   r   r   r   r   <module>   s    $
