a
    dT                     @   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   i/var/www/html/stable-diffusion-webui/venv/lib/python3.9/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       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)r3   out_namectypeZ_opt_out)r3   r5   r2   r6   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,   r2   r3   r!   r6   r5   r/   r0   r   r   r   r'   H   s$    





z$Unboxing.argumenttype_evalue_convert)r3   r5   r6   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,   r3   r5   r6   r   r   r   r7   i   s    &zUnboxing._gen_code_base_type)r3   r5   r2   r6   r   c           
   
   C   sX   | d}|  |j|\}}}}	d|jdd d| d| d|jdd d	d	|	fS )
NZ_opt_in
    Tr<   r:   r;   z.toOptional<z>();
            
)r'   elemr=   split)
r,   r3   r5   r2   r6   in_nameres_nameZ	base_typeres_coder0   r   r   r   r8   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<   r:   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=   rA   intZSymIntfloatboolr   r*   	connectorjoin)r,   r3   r5   r2   r6   rB   Z	elem_namer/   rC   Z	res_ctyperD   r0   Zvec_namer   r   r   r9      s    







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