
    i              	          S r SSKrSSKrSSKrSSKrSSKrSSKJrJr  SSK	J
r
Jr  SSKJr  SSKJr  SSKJrJr  SSKJr  SS	KJr  \R.                  " \5      r\ " S
 S\5      5       r  SS\S\\   S\S-  S\4S jjrg)z@Ahead-of-time Arm VGF backend built on the shared TOSA pipeline.    N)finalList)arm_get_first_delegation_tagTOSABackend)VgfCompileSpec)require_model_converter_binary)BackendDetailsPreprocessResult)CompileSpec)ExportedProgramc            
       h    \ rS rSrSr\ SS\S\S\S\4S jj5       r	\S\
S	\\   S\4S
 j5       rSrg)
VgfBackend.   zBackendDetails subclass for delegation to VGF compatible devices.

This enables encapsulated TOSA on target device and JIT compilation on
suitable platforms.

tosa_flatbuffercompile_spectag_namereturnc                 V    UR                   nUR                  5       n[        XXB5      nU$ )a  Compile a TOSA flatbuffer into a target-specific binary stream.

Args:
    tosa_flatbuffer (bytes): Serialized TOSA graph produced by
        ``TOSABackend``.
    compile_spec (VgfCompileSpec): Compile specification providing
        converter flags and artifact paths.
    tag_name (str): Optional suffix used when producing debug outputs.

Returns:
    bytes: Target-specific VGF binary stream.

)compiler_flagsget_intermediate_pathvgf_compile)r   r   r   compile_flagsartifact_pathbinarys         b/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/executorch/backends/arm/vgf/backend.py_compile_tosa_flatbuffer#VgfBackend._compile_tosa_flatbuffer7   s.    & %33$::<_]U    edge_programcompile_specsc                 N   [         R                  [        R                   S35        [        R
                  " U5      n[        R                  " U5      n[        R                  " X5      n[        U R                  5      n[        R                  UR                  X%5      n[        US9$ )a2  Lower the exported program and compile it for a VGF target.

Args:
    edge_program (ExportedProgram): Program to lower to VGF.
    compile_specs (List[CompileSpec]): Serialized VGF compile specs
        supplied by the frontend.

Returns:
    PreprocessResult: Result containing the compiled VGF binary.

z preprocess)processed_bytes)loggerinfor   __name__r   	from_listr   filter_tosa_compile_specs_preprocessr   graph_moduler   r"   r
   )r   r    r   tosa_compile_spectosa_preprocessr   r   s          r   
preprocessVgfBackend.preprocessP   s      	z**+;78%//> (AA,O &11,R/0I0IJ44++\
  77r    N) )r%   
__module____qualname____firstlineno____doc__staticmethodbytesr   strr   r   r   r   r
   r,   __static_attributes__r.   r   r   r   r   .   sw      $  
	 0 #8%#8K(#8 
#8 #8r   r   r   r   r   r   c                    [         R                  " 5        nSU S3n[        R                  R	                  XE5      n[        US5       nUR                  U 5        SSS5        U Vs/ s H$  ow(       d  M  UR                  5       (       d  M"  UPM&     nn[        5       nUS-   n	U/UQSPUPSPU	Pn
 [        R                  " U
SS	S	S
9  U(       aD  [         R#                  SU	< 35        [        R$                  " US	S9  [&        R(                  " X5        [        U	S5      R+                  5       nUsSSS5        $ ! , (       d  f       N= fs  snf ! [        R                   aZ  nSR	                  U
5      n[        SU SUR                  R                  5        SUR                  R                  5        35      eSnAff = f! , (       d  f       g= f)a  Invoke the VGF compiler to convert a TOSA flatbuffer.

Args:
    tosa_flatbuffer (bytes): Serialized TOSA graph produced by
        ``TOSABackend``.
    compile_flags (List[str]): Command-line flags forwarded to
        ``model-converter``.
    artifact_path (str | None): Directory where debug artifacts are saved.
    tag_name (str): Optional suffix used when producing debug outputs.

Returns:
    bytes: Compiled VGF binary emitted by ``model-converter``.

output_z.tosawbNz.vgfz-iz-oFT)shellcheckcapture_output zVgf compiler ('z'') failed with error:
                 z
                 Stdout:
z#Emitting debug output to: vgf_path=)exist_okrb)tempfileTemporaryDirectoryospathjoinopenwritestripr   
subprocessrunCalledProcessErrorRuntimeErrorstderrdecodestdoutr#   r$   makedirsshutilcopy2read)r   r   r   r   tmpdirtosaname	tosa_pathfconverter_binaryvgf_pathconversion_commandprocess_errorconversion_command_str	vgf_bytess                 r   r   r   w   s   ( 
	$	$	&& XJe,GGLL2	)T"aGGO$ # %2EMqQ1779ME9;v%

 
 	

 
 

	NN"%tD KK>XK@AKK5LL14(--/	K 
'	&
 #" F ,, 	%(XX.@%A"!"8!9 :%%,,./ 0'..5578: 	/ 
'	&sa   2F>D7F>&
E4EEF>0EA&F>7
E	F>F;!AF66F;;F>>
G)Nr/   )r3   loggingrC   rQ   rI   rA   typingr   r   $executorch.backends.arm.tosa.backendr   r   (executorch.backends.arm.vgf.compile_specr   +executorch.backends.arm.vgf.model_converterr   'executorch.exir.backend.backend_detailsr	   r
   +executorch.exir.backend.compile_spec_schemar   torch.export.exported_programr   	getLoggerr%   r#   r   r5   r6   r   r.   r   r   <module>rg      s    G  	    
 : 
		8	$ E8 E8 E8V !%	9999 :9 	9r   