a
    h                     @   sb   d dl 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 )    )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 )NnapiModulezTorch 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__ K/var/www/auris/lib/python3.9/site-packages/torch/backends/_nnapi/prepare.pyr      s    

zNnapiModule.__init__)argsc                 C   s`   | j d u sJ | 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>4       z$NnapiModule.init.<locals>.<listcomp>)r   r
   preparer   r	   r   torchclasses_nnapiCompilationZinit2r   r   )r   r   r   r   r   r   init0   s    zNnapiModule.init)r   returnc              	   C   s4  | j d u r| | | j }|d us&J dd | jD }t|t| jksLJ 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sJ tt| jD ]F}| j| }|dv rq|dkr&|| dddd||< qt	dq|S )	Nc                 S   s   g | ]}t |qS r   )r   Z
empty_like)r   outr   r   r   r   D   r   z'NnapiModule.forward.<locals>.<listcomp>r   r   r      zInvalid mem_fmt)r   r   )r   r#   r	   lenr   rangeappendr   Zpermute
ValueError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__listTensornnModuleintboolr   jit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 rd}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__)r/   r0   r1   r2   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>   r   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@      r   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_nnapia   sD    

rP   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)configrK   )Zdtypec                   @   s   e Zd ZdZdS )z-process_for_nnapi.<locals>.ShapeComputeModulezCode-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)r/   r0   r1   r2   r   r   r   r   ShapeComputeModule   s   rR   z\def prepare(self, ser_model: torch.Tensor, args: List[torch.Tensor]) -> List[torch.Tensor]:
c                 S   s   g | ]}d | dqS )z    rB   r   )r   liner   r   r   r      r   z%process_for_nnapi.<locals>.<listcomp>rA   )r   r:   freeze
isinstancer5   r   Zserialize_modelZtensorint32r6   r7   rD   rF   rE   )rG   rH   rI   rJ   rK   r   rM   r   r   Zshape_compute_linesrN   rL   rR   r
   Zreal_shape_compute_linesr   r   r   rC      s:    
rC   )NNF)typingr   r   Z torch.backends._nnapi.serializerr   Z ANEURALNETWORKS_PREFER_LOW_POWERZ)ANEURALNETWORKS_PREFER_FAST_SINGLE_ANSWERZ&ANEURALNETWORKS_PREFER_SUSTAINED_SPEEDr6   r7   r   rP   rC   r   r   r   r   <module>   s   V
< 