o
    0h                     @  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nameunambiguous_name)r    r   T/var/www/vscode/kcb/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   is_writeextendappend	with_name)
selfr   	code_listbinding_listargr(   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$   binds_base)r:   out_namectype_opt_out)r:   r?   r9   r@   	_list_outzCannot handle type z. arg_name: )
r   r+   r'   r   _gen_code_base_typer   _gen_code_optional_typer	   _gen_code_list_typer)   )r0   r9   r:   r$   r@   r?   r6   r7   r   r   r   r*   P   s(   





z$Unboxing.argumenttype_evalue_convertr?   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	strip_refz>();)cpp_type)r0   r:   r?   r@   r   r   r   rC   s   s   &zUnboxing._gen_code_base_typer   c           
      C  sJ   | d}|  |j|\}}}}	d| d| d|jdd dd|	fS )	N_opt_in

    auto rH   z.toOptional<TrI   z>();
            
)r*   elemrK   split)
r0   r:   r?   r9   r@   in_nameres_name	base_typeres_coder7   r   r   r   rD   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 )$N_list_in_elemrM   rH   z!.toTensorList();
                rN   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<TrI   ;z
    for (EValue z) {
        z	
        z);
    }
    rG   (z);
                )r*   rO   r'   r   r   r   Tensorr-   rP   intSymIntfloatr;   sizer   r.   rK   	connectorjoin)r0   r:   r?   r9   r@   rQ   	elem_namer6   rR   	res_ctyperT   r7   vec_namer   r   r   rE      s   

G?9		2


	zUnboxing._gen_code_list_typeN)r   r   r   r   )r9   r   r:   r   r$   r;   r   r<   )r:   r   r?   r   r@   r   r   rF   )
r:   r   r?   r   r9   r   r@   r   r   rF   )
r:   r   r?   r   r9   r	   r@   r   r   rF   )
__name__
__module____qualname____doc____annotations__r8   r*   rC   rD   rE   r   r   r   r   r      s   
 

#
r   N)r   r
   r   r   )
__future__r   dataclassesr   typingr   r   torchgen.modelr   r   r   r	   r
   r   r   collections.abcr   torchgen.api.typesr   r   r   r_   r   r   r   r   r   r   <module>   s    $
