U
    ©Ê¹js  ã                   @   sf   d dl mZmZ d dlZd dlmZ d ZdZdZG dd„ dej	j
ƒZdddedfd	d
„Zddd„ZdS )é    )ÚListÚOptionalN)Ú_NnapiSerializeré   é   c                       s®   e Zd ZU dZeejjj e	d< e
ej e	d< e
ej e	d< ejjeje
ej e
e e
e eedœ‡ fdd„Zejje
ej dœd	d
„ƒZe
ej e
ej dœdd„Z‡  ZS )ÚNnapiModulezÏTorch Module that wraps an NNAPI Compilation.

    This module handles preparing the weights, initializing the
    NNAPI TorchBind object, and adjusting the memory formats
    of all inputs and outputs.
    ÚcompÚweightsÚout_templates)Úshape_compute_moduleÚ	ser_modelr	   Úinp_mem_fmtsÚout_mem_fmtsÚcompilation_preferenceÚrelax_f32_to_f16c                    sD   t ƒ  ¡  || _|| _|| _|| _|| _g | _d | _|| _	|| _
d S ©N)ÚsuperÚ__init__r   r   r	   r   r   r
   r   r   r   )Úselfr   r   r	   r   r   r   r   ©Ú	__class__© ú\/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torch/backends/_nnapi/prepare.pyr      s    

zNnapiModule.__init__)Úargsc                 C   s`   | j d kst‚| j | j|¡| _dd„ | jD ƒ| _tjj	 
¡ }| | j| j| j| j¡ || _ d S )Nc                 S   s   g | ]}|  ¡ ‘qS r   )Ú
contiguous)Ú.0Úwr   r   r   Ú
<listcomp>1   s     z$NnapiModule.init.<locals>.<listcomp>)r   ÚAssertionErrorr   Úpreparer   r
   r	   ÚtorchÚclassesÚ_nnapiÚCompilationZinit2r   r   )r   r   r   r   r   r   Úinit-   s    üzNnapiModule.init)r   Úreturnc              	   C   s4  | j d kr|  |¡ | j }|d k	s&t‚dd„ | jD ƒ}t|ƒt| jƒksLt‚g }tt|ƒƒD ]Z}| j| }|dkr†| ||  ¡ ¡ q\|dkr®| ||  	dddd¡ ¡ ¡ q\t
dƒ‚q\| ||¡ t|ƒt| jƒksÚt‚tt| jƒƒD ]F}| j| }|dk�rqè|dk�r&||  	dddd¡||< qèt
dƒ‚qè|S )	Nc                 S   s   g | ]}t  |¡‘qS r   )r    Z
empty_like)r   Úoutr   r   r   r   A   s     z'NnapiModule.forward.<locals>.<listcomp>r   r   r   é   zInvalid mem_fmt)r   r   )r   r$   r   r
   Úlenr   ÚrangeÚappendr   ZpermuteÚ	ExceptionÚrunr   )r   r   r   ZoutsZ
fixed_argsÚidxÚfmtr   r   r   Úforward<   s0    


 




zNnapiModule.forward)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r    r!   r"   r#   Ú__annotations__r   ÚTensorÚnnÚModuleÚintÚboolr   ÚjitZexportr$   r/   Ú__classcell__r   r   r   r   r      s   
ør   Fc              	   C   s¼   t | ||||ƒ\}}}	}
}}t|||	|
|||ƒ}G dd„ dtjjƒ}||ƒ}tj |¡}d dd„ tt	|ƒƒD ƒ¡}|dk r‚d}nd d	d„ t|ƒD ƒ¡}| 
d
|› d|› d|› d�¡ |S )Nc                       s    e Zd ZdZ‡ fdd„Z‡  ZS )z5convert_model_to_nnapi.<locals>.NnapiInterfaceWrappera0  NNAPI list-ifying and de-list-ifying wrapper.

        NNAPI always expects a list of inputs and provides a list of outputs.
        This module allows us to accept inputs as separate arguments.
        It returns results as either a single tensor or tuple,
        matching the original module.
        c                    s   t ƒ  ¡  || _d S r   )r   r   Úmod)r   r<   r   r   r   r   …   s    
z>convert_model_to_nnapi.<locals>.NnapiInterfaceWrapper.__init__)r0   r1   r2   r3   r   r;   r   r   r   r   ÚNnapiInterfaceWrapper|   s   r=   z, c                 s   s   | ]}d |› �V  qdS )Zarg_Nr   ©r   r-   r   r   r   Ú	<genexpr>Œ   s     z)convert_model_to_nnapi.<locals>.<genexpr>r   z
retvals[0]Ú c                 s   s   | ]}d |› d�V  qdS )zretvals[z], Nr   r>   r   r   r   r?   �   s     zdef forward(self, z):
    retvals = self.mod([z])
    return Ú
)Úprocess_for_nnapir   r    r6   r7   r:   ÚscriptÚjoinr)   r(   Údefine)ÚmodelÚinputsÚ
serializerÚreturn_shapesÚuse_int16_for_qint16r   r   r   Úser_model_tensorÚused_weightsr   r   Úretval_countZnnapi_modelr=   Zwrapper_model_pyZwrapper_modelÚarg_listZret_exprr   r   r   Úconvert_model_to_nnapi^   sD        ÿùù
ÿrO   c                 C   s®   t j | ¡} t|t jƒr|g}|p,td |d�}| | ||¡\}}}}}	}
t j|t jd�}G dd„ dt j	j
ƒ}t j |ƒ ¡}dgdd„ |	D ƒ }| d |¡¡ ||||||
fS )	N)ÚconfigrJ   )Zdtypec                   @   s   e Zd ZdZdS )z-process_for_nnapi.<locals>.ShapeComputeModulezÚCode-gen-ed module for tensor shape computation

        module.prepare will mutate ser_model according to the computed operand
        shapes, based on the shapes of args.  Returns a list of output templates.
        N)r0   r1   r2   r3   r   r   r   r   ÚShapeComputeModule°   s   rQ   z\def prepare(self, ser_model: torch.Tensor, args: List[torch.Tensor]) -> List[torch.Tensor]:
c                 S   s   g | ]}d |› d�‘qS )z    rA   r   )r   Úliner   r   r   r   ¼   s     z%process_for_nnapi.<locals>.<listcomp>r@   )r    r:   ÚfreezeÚ
isinstancer5   r   Zserialize_modelZtensorZint32r6   r7   rC   rE   rD   )rF   rG   rH   rI   rJ   r   rL   r   r   Zshape_compute_linesrM   rK   rQ   r   Zreal_shape_compute_linesr   r   r   rB   ™   s<     ÿ
ù	ÿþúrB   )NNF)Útypingr   r   r    Z torch.backends._nnapi.serializerr   Z ANEURALNETWORKS_PREFER_LOW_POWERZ)ANEURALNETWORKS_PREFER_FAST_SINGLE_ANSWERZ&ANEURALNETWORKS_PREFER_SUSTAINED_SPEEDr6   r7   r   rO   rB   r   r   r   r   Ú<module>   s    Vù
<     ÿ