
    sg                         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ej                  j                        Zdddedfd	Z	 dd
Zy)    )ListOptionalN)_NnapiSerializer      c                       e Zd ZU dZeej                  j                  j                     e	d<   e
ej                     e	d<   e
ej                     e	d<   dej                  j                  dej                  de
ej                     de
e   de
e   d	ed
ef fdZej"                  j$                  de
ej                     fd       Zde
ej                     de
ej                     fdZ x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_modelinp_mem_fmtsout_mem_fmtscompilation_preferencerelax_f32_to_f16c                     t         |           || _        || _        || _        || _        || _        g | _        d | _        || _	        || _
        y N)super__init__r   r   r   r   r   r   r
   r   r   )	selfr   r   r   r   r   r   r   	__class__s	           P/var/www/html/venv/lib/python3.12/site-packages/torch/backends/_nnapi/prepare.pyr   zNnapiModule.__init__   sU     	$8!"((	&<# 0    argsc                    | j                   J | j                  j                  | j                  |      | _        | j
                  D cg c]  }|j                          c}| _        t        j                  j                  j                         }|j                  | j                  | j
                  | j                  | j                         || _         y c c}w r   )r
   r   preparer   r   r   
contiguoustorchclasses_nnapiCompilationinit2r   r   )r   r   wr
   s       r   initzNnapiModule.init0   s    yy   !66>>t~~tT04=1=}}##//1

NNLL''!!		
 	 >s   Creturnc           	      `   | j                   | j                  |       | j                   }|J | j                  D cg c]  }t        j                  |       }}t        |      t        | j                        k(  sJ g }t        t        |            D ]}  }| j                  |   }|dk(  r#|j                  ||   j                                :|dk(  r5|j                  ||   j                  dddd      j                                tt        d       |j                  ||       t        |      t        | j                        k(  sJ t        t        | j                              D ]@  }| j                  |   }|dv r|dk(  r||   j                  dddd      ||<   7t        d       |S c c}w )Nr   r   r      zInvalid mem_fmt)r   r   )r
   r%   r   r   
empty_likelenr   rangeappendr   permute
ValueErrorrunr   )r   r   r
   outouts
fixed_argsidxfmts           r   forwardzNnapiModule.forward?   s   99IIdOyy151C1CD#  %DD4yC 1 12222
T# 		4C##C(C ax!!$s)"6"6"89!!$s)"3"3Aq!Q"?"J"J"LM !233		4 	T"4yC 1 12222T//01 		4C##C(C f} I--aAq9S	 !233		4 5 Es   F+)__name__
__module____qualname____doc__r   r   r    r!   r"   __annotations__r   TensornnModuleintboolr   jitexportr%   r5   __classcell__r   s   @r   r	   r	      s     5==''33
44%,,%%1#hhoo1 <<1 ell#	1
 3i1 3i1 !$1 1* YYell+  D. 43E r   r	   Fc           	         t        | ||||      \  }}}	}
}}t        |||	|
|||      } G d dt        j                  j                        } ||      }t        j
                  j                  |      }dj                  d t        t        |            D              }|dk  rd}n!dj                  d t        |      D              }|j                  d	| d
| d| d       |S )Nc                   "     e Zd ZdZ fdZ xZS )5convert_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                 0    t         |           || _        y r   )r   r   mod)r   rH   r   s     r   r   z>convert_model_to_nnapi.<locals>.NnapiInterfaceWrapper.__init__   s    GDHr   )r6   r7   r8   r9   r   rB   rC   s   @r   NnapiInterfaceWrapperrF      s    		 	r   rI   z, c              3   &   K   | ]	  }d |   yw)arg_N .0r3   s     r   	<genexpr>z)convert_model_to_nnapi.<locals>.<genexpr>   s     D#4uDs   r   z
retvals[0] c              3   (   K   | ]
  }d | d  yw)zretvals[z], NrL   rM   s     r   rO   z)convert_model_to_nnapi.<locals>.<genexpr>   s     N3XcU#.Ns   zdef forward(self, z):
    retvals = self.mod([z])
    return 
)process_for_nnapir	   r   r<   r=   r@   scriptjoinr+   r*   define)modelinputs
serializerreturn_shapesuse_int16_for_qint16r   r   r   ser_model_tensorused_weightsr   r   retval_countnnapi_modelrI   wrapper_model_pywrapper_modelarg_listret_exprs                      r   convert_model_to_nnapird   a   s     	vz=2F	
 K  -[9II$$%56MyyDs6{1CDDHa77N%:MNN
XJ '##+* -Zr	#
 r   c                 2   t         j                  j                  |       } t        |t         j                        r|g}|xs t        d |      }|j                  | ||      \  }}}}}	}
t        j                  |t         j                        } G d dt         j                  j                        }t         j                  j                   |             }dg|	D cg c]  }d| d
 c}z   }|j                  dj                  |             ||||||
fS c c}w )	N)configr[   )dtypec                       e Zd ZdZy)-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)r6   r7   r8   r9   rL   r   r   ShapeComputeModuleri      s    	r   rj   z\def prepare(self, ser_model: torch.Tensor, args: List[torch.Tensor]) -> List[torch.Tensor]:
z    rR   rP   )r   r@   freeze
isinstancer;   r   serialize_modeltensorint32r<   r=   rT   rV   rU   )rW   rX   rY   rZ   r[   r   r]   r   r   shape_compute_linesr^   r\   rj   r   linereal_shape_compute_liness                   r   rS   rS      s    IIU#E&%,,' /*> J 	""5&-@||IU[[AUXX__  !99++,>,@Ag %89T4vR9 : (@ AB 	  	:s   D)NNF)typingr   r   r    torch.backends._nnapi.serializerr    ANEURALNETWORKS_PREFER_LOW_POWER)ANEURALNETWORKS_PREFER_FAST_SINGLE_ANSWER&ANEURALNETWORKS_PREFER_SUSTAINED_SPEEDr<   r=   r	   rd   rS   rL   r   r   <module>rx      s^    "  = $%  ,- ))* &P%((// Pl A8x NS+r   