
    j=                        d dl Z d dlZd dlZd dlmZmZ d dlmZ d dlm	Z	 d dl
Z
d dlmZmZ d dl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 d	lmZmZmZm Z m!Z!m"Z"m#Z# e
j$        %                    d
ddg          d             Z&e
j$        %                    d
ddg          d             Z'e
j$        (                    e! d          e
j$        %                    d
ddg          d                         Z)e
j$        j*        e
j$        (                    e! d          e
j$        %                    dd  eeddgddgddgddgddgdg          D                       d                                     Z+e
j$        j*        e
j$        %                    dd  eeddgdgdgddgddgddgddg          D                       d                         Z,e
j$        j*        e
j$        %                    dd  eeddgdgddgddgddgddg          D                       d                         Z-e
j$        j*        e
j$        (                    e d          e
j$        (                    e d          e
j$        (                    ej.        d          e
j$        (                    eoeoedk    d           e
j$        %                    d!d"  eeddgddgddgddgdgddg          D                       d#                                                                         Z/e
j$        j*        e
j$        (                    ej0         pe d$          e
j$        (                    e ped%          e
j$        %                    dd&  eedgddgddgdgddgddg          D                       d'                                                 Z1e
j$        (                    e d          e
j$        (                    ed(          e
j$        (                    eoed)          e
j$        (                    ej.        d          d*                                                 Z2e
j$        (                    ej0         d+          e
j$        (                    e d,          e
j$        (                    e d-          d.                                     Z3e
j$        (                    dd/          e
j$        (                    e d0          d1                         Z4e
j$        (                    dd2          d3             Z5e
j$        j*        e
j$        (                    e d4          d5                         Z6e
j$        j*        e
j$        (                    e d4          e
j$        %                    d6d7  eeddgddgddgddg          D                       d8                                     Z7e
j$        j*        e
j$        (                    e  d9          d:                         Z8e
j$        j*        e
j$        (                    e  d9          e
j$        %                    d; e9 eeddgdg                              d<                                     Z:e
j$        (                    e# d=          e
j$        (                    ej;         d>          e
j$        (                    e d?          e
j$        (                    ed@          dA                                                 Z<e
j$        j*        e
j$        (                    e" dB          e
j$        (                    e dC          e
j$        (                    ej=         dD          dE                                                 Z>e
j$        (                    ej0         pe# dF          e
j$        (                    edG          dH                         Z?e
j$        j*        e
j$        (                    ej0         pe# dF          e
j$        (                    edG          e
j$        %                    dIe          dJ                                                 Z@dS )K    N)redirect_stderrredirect_stdout)product)Path)MODELSOURCE)YOLO)	TASK2DATA
TASK2MODELTASKS)ARM64IS_RASPBERRYPILINUXMACOSMACOS_VERSIONWINDOWSchecks)
TORCH_1_10
TORCH_1_11
TORCH_1_13	TORCH_2_0	TORCH_2_1	TORCH_2_8	TORCH_2_9end2endFTc                     t          t                                        ddd|           } t          |          t          d           dS )zOTest YOLO model export to TorchScript format for compatibility and correctness.torchscriptF    )formatoptimizeimgszr   r!   Nr	   r   exportr   r   files     W/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/tests/test_exports.pytest_export_torchscriptr(      sH     ;;]U"V]^^DDJJvR          c                     t          t                                        ddd|           } t          |          t          d           dS )z8Test YOLO model export to ONNX format with dynamic axes.onnxTr   )r   dynamicr!   r   r"   Nr#   r%   s     r'   test_export_onnxr-      sG     ;;VTWUUDDJJvR      r)   zOpenVINO requires torch>=2.1)reasonc                 J   t          t                                        dd|           }t          rRt	          |          }|                    |                    |j         dt          j	                                         } t          |          t          d           dS )zFTest YOLO export to OpenVINO format for model inference compatibility.openvinor   )r   r!   r   -r"   N)r	   r   r$   r   r   rename	with_stemstemuuiduuid4r   r%   s     r'   test_export_openvinor7   !   s     ;;Zr7KKD JDzz{{4>>TY*G*G*G*GHHIIDJJvR      r)   z.task, dynamic, int8, half, batch, nms, end2endc           	      P    g | ]#\  }}}}}}}|r|s|d k    r||r||||||||f$S classify .0taskr,   int8halfbatchnmsr   s           r'   
<listcomp>rC   1   sr       :D'4uc7 
 
 %)J$6$63$6G$6PS$6	 
wdE38 %7$6$6r)         c                    t          t          |                                        dd||||t          |          ||	  	        }t          rRt          |          }|                    |                    |j         dt          j
                                         } t          |          t          g|z  |rdnd|           t          j        |d           d	S )
zQTest YOLO model export to OpenVINO under various configuration matrix conditions.r0   r   )	r   r!   r,   r?   r@   rA   datarB   r   r1   @   )r!   rA   Tignore_errorsN)r	   r   r$   r
   r   r   r2   r3   r4   r5   r6   r   shutilrmtreer>   r,   r?   r@   rA   rB   r   r&   s           r'   test_export_openvino_matrixrN   -   s     
4 !!((t_ ) 
 
D  JDzz{{4>>TY*G*G*G*GHHIIDJJx%W'<rr"EJJJJ
M$d++++++r)   z8task, dynamic, int8, half, batch, simplify, nms, end2endc           
      f    g | ].\  }}}}}}}}|r|s|d k    r||rt            |r|$||||||||f/S r9   )r   )	r=   r>   r,   r?   r@   rA   simplifyrB   r   s	            r'   rC   rC   R   s       DD'4uhW 
 
 %)J$6$63$6C$6PZ$6`g$6lo$6	 
wdE8S'B %7$6$6r)   c                    t          t          |                                        dd|||||||	  	        } t          |          t          g|z  |rdnd           t	          |                                           dS )zKTest YOLO export to ONNX format with various configurations and parameters.r+   r   )	r   r!   r,   r?   r@   rA   rP   rB   r   rH   r"   Nr	   r   r$   r   r   unlink)	r>   r,   r?   r@   rA   rP   rB   r   r&   s	            r'   test_export_onnx_matrixrT   O   s     
4 !!(( ) 
 
D DJJx%W'<rr"====JJr)   c           	      H    g | ]\  }}}}}}}|d k    r||r||||||||f S r9   r;   r<   s           r'   rC   rC   n   s]       :D'4uc7 ####S#	 
wdE38 $##r)   c           
          t          t          |                                        dd||||||          } t          |          t          g|z  |rdnd           t	          |                                           dS )zITest YOLO model export to TorchScript format under varied configurations.r   r   r   r!   r,   r?   r@   rA   rB   r   rH   r"   NrR   rM   s           r'   test_export_torchscript_matrixrX   k   s     
4 !!((BdUZ`cmt )  D DJJx%W'<rr"====JJr)   z(CoreML inference only supported on macOSz"CoreML export requires torch>=1.11z#CoreML not supported in Python 3.1315z.CoreML YOLO26 matrix test crashes on macOS 15+z.task, dynamic, int8, half, nms, batch, end2endc           	      h    g | ]/\  }}}}}}}|r|s!|d k    r|s|r|s|dk    r|"|r|&|||||||f0S )detectr:   r;   )r=   r>   r,   r?   r@   rB   rA   r   s           r'   rC   rC      s     
 
 
:D'4sE7 

 
 !!c! "  "### $  $ 
wdC8 $##r)   c           
          t          t          |                                        dd||||||          } t          |          t          g|z  d           t	          j        |           dS )zHTest YOLO export to CoreML format with various parameter configurations.coremlr   rW   r"   Nr	   r   r$   r   rK   rL   )r>   r,   r?   r@   rB   rA   r   r&   s           r'   test_export_coreml_matrixr_      s}    . 
4 !!(( ) 	 	D DJJx%r****
M$r)   z3TFLite export requires Python>=3.10 and torch>=1.13zUTest disabled as TF suffers from install conflicts on Windows, macOS and Raspberry Pic           	      t    g | ]5\  }}}}}}}|r|s'|d k    r|t           r||rt          (|r|,|||||||f6S r9   )r   r   r<   s           r'   rC   rC      s       :D'4uc7   
""s" # # #& #  #   # 
wdE38 #""r)   c           
          t          t          |                                        dd||||||          } t          |          t          g|z  d           t	          |                                           dS )zLTest YOLO export to TFLite format considering various export configurations.tfliter   rW   r"   NrR   rM   s           r'   test_export_tflite_matrixrc      s    4 
4 !!((r7DPU[^ho )  D DJJx%r****JJr)   zCoreML not supported on Windowsz%CoreML not supported on aarch64 Linuxc                  p   t          j                    t          j                    }} t          |           5  t          |          5  t	          t
                                        ddd           t          rHt	          t
                                        dd          } t	          |          t          d           ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   | 	                                |	                                z   }d|vsJ d	|             d
|vs
J d            dS )z7Test YOLO export to CoreML format and check for errors.r]   Tr   )r   rB   r!   r   r!   r"   NErrorzCoreML export produced errors: z%You will not be able to run predict()z!CoreML export has predict() error)
ioStringIOr   r   r	   r   r$   r   r   getvalue)stdoutstderrr&   outputs       r'   test_export_coremlrm      s    []]BKMMFF		 	  ) )/&"9"9 ) )U(B??? 	);;%%XR%@@DDJJvR((((	) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) __!2!22F&   "LF"L"L   2&@@@Be@@@@@s6   C#A:C C#C	C#C	C##C'*C'z#TFLite export requires Python>=3.10z"TFLite export requires torch>=1.13zGTest disabled as TF suffers from install conflicts on Windows and macOSc                      t          t                    } |                     dd          } t          |          t          d           dS )zRTest YOLO export to TFLite format under specific OS and Python version conditions.rb   r   re   r"   Nr#   modelr&   s     r'   test_export_tfliterq      sE    
 KKE<<xr<22DDJJvR      r)   zTest disabledz6TF suffers from install conflicts on Windows and macOSc                      t          t                    } |                     dd          } t          |          t          d           dS )z8Test YOLO export to TensorFlow's Protobuf (*.pb) format.pbr   re   r"   Nr#   ro   s     r'   test_export_pbrt      sE     KKE<<t2<..DDJJvR      r)   zITest disabled as Paddle protobuf and ONNX protobuf requirements conflict.c                  X    t          t                                        dd           dS )zGTest YOLO export to Paddle format, noting protobuf conflicts with ONNX.paddler   re   N)r	   r   r$   r;   r)   r'   test_export_paddlerw      s)     	KKhb11111r)   zMNN export requires torch>=1.10c                      t          t                                        dd          }  t          |           t          d           dS )zaTest YOLO export to MNN format (WARNING: MNN test must precede NCNN test or CI error on Windows).mnnr   re   r"   Nr#   r&   s    r'   test_export_mnnr{      sC     ;;U"55DDJJvR      r)   z task, int8, half, batch, end2endc                 0    g | ]\  }}}}}|r||||||fS r;   r;   )r=   r>   r?   r@   rA   r   s         r'   rC   rC      sM       ,D$eW 	tT5'*  r)   c                     t          t          |                                        dd||||          } t          |          t          g|z  d           t	          |                                           dS )zITest YOLO export to MNN format considering various export configurations.ry   r   )r   r!   r?   r@   rA   r   r"   NrR   )r>   r?   r@   rA   r   r&   s         r'   test_export_mnn_matrixr~      st     
4 !!((RdQU]bls(ttDDJJx%r****JJr)   z-NCNN inference causes segfault on PyTorch<2.0c                      t          t                                        dd          }  t          |           t          d           dS )z Test YOLO export to NCNN format.ncnnr   re   r"   Nr#   rz   s    r'   test_export_ncnnr     sC     ;;V266DDJJvR      r)   ztask, half, batchc                     t          t          |                                        dd||          } t          |          t          g|z  d           t	          j        |d           dS )zJTest YOLO export to NCNN format considering various export configurations.r   r   )r   r!   r@   rA   r"   TrI   Nr^   )r>   r@   rA   r&   s       r'   test_export_ncnn_matrixr     sk    
 
4 !!((btSX(YYDDJJx%r****
M$d++++++r)   z IMX export requires torch>=2.9.0zRequires Python>=3.9z"IMX export only supported on LinuxzSTest disabled as IMX export suffers from OOM (Out of Memory) on Raspberry Pi 5 16GBc                      t          d          } |                     dd          } t          |          t          d           dS )zTest YOLO export to IMX format.z
yolo11n.ptimxr   re   r"   N)r	   r$   r   ro   s     r'   test_export_imxr     sG     E<<uB<//DDJJvR      r)   z$Axelera export requires torch>=2.8.0z&Axelera export only supported on Linuxz#Axelera export requires Python 3.10c                      t          t                                        ddd          } t          |                                           sJ d|              t          j        | d           dS )	z#Test YOLO export to Axelera format.axelerarH   z
coco8.yaml)r   r!   rG   z,Axelera export failed, directory not found: TrI   N)r	   r   r$   r   existsrK   rL   rz   s    r'   test_export_axelerar   *  sn     ;;Yb|LLD::UU Ut U UUU
M$d++++++r)   z&Requires Python>=3.10 and Torch>=2.9.0zSkipping test on Windowsc                      t          t                                        dd          } t          |                                           sJ d|              t          |           t          t                                        d          j        z  }|                                sJ d|             t          |           dz  }|                                sJ d|             t          j        | d	
           dS )z,Test YOLO model export to ExecuTorch format.
executorchr   re   z/ExecuTorch export failed, directory not found: .ptez ExecuTorch .pte file not found: metadata.yamlz$ExecuTorch metadata.yaml not found: TrI   N)	r	   r   r$   r   r   with_suffixnamerK   rL   )r&   pte_filemetadata_files      r'   test_export_executorchr   7  s     ;;\<<D::XX XRV X XXXDzzDKK33F;;@@H??KKKKKKKJJ0M!!YY#Y-#Y#YYY!
M$d++++++r)   r>   c                 .   t          t          |                                        dd          }t          |                                          sJ d|  d|             t          t          |                                        d          j        }t          |          |z  }|                                sJ d|  d|             t          |          d	z  }|                                sJ d
|  d|             t          j        |d           dS )z=Test YOLO export to ExecuTorch format for various task types.r   r   re   z#ExecuTorch export failed for task 'z', directory not found: r   z)ExecuTorch .pte file not found for task 'z': r   z-ExecuTorch metadata.yaml not found for task 'TrI   N)	r	   r   r$   r   r   r   r   rK   rL   )r>   r&   
model_namer   r   s        r'   test_export_executorch_matrixr   G  s"    
4 !!((B(GGD::jj jd j jdh j jjjj&''33F;;@JDzzJ&H??]]]$]]S[]]]]JJ0M!!kk#kSW#k#k\i#k#kkk!
M$d++++++r)   )Arg   rK   r5   
contextlibr   r   	itertoolsr   pathlibr   pytesttestsr   r   ultralyticsr	   ultralytics.cfgr
   r   r   ultralytics.utilsr   r   r   r   r   r   r   ultralytics.utils.torch_utilsr   r   r   r   r   r   r   markparametrizer(   r-   skipifr7   slowrN   rT   rX   IS_PYTHON_3_13r_   IS_PYTHON_MINIMUM_3_10rc   rm   rq   rt   rw   r{   r~   r   listr   IS_PYTHON_MINIMUM_3_9r   IS_PYTHON_3_10r   r   r   r;   r)   r'   <module>r      s   
			   7 7 7 7 7 7 7 7                            8 8 8 8 8 8 8 8 8 8 a a a a a a a a a a a a a a a a a a x x x x x x x x x x x x x x x x x x UDM22! ! 32! UDM22! ! 32! 	M*HIIUDM22! ! 32 JI! 	M*HII4 >EgD%=4-$AuX\W]?
 ?
  	 	, ,	 	 JI ,* > HOD%=5'E7QFT5MDRW=[_afZgI
 I
  	 	 	 	 " 4 >EgE4=5'E4=1a&4-RVX]Q^?
 ?
  	 	 	 	  I&PQQ
N+OPPF)2WXX	5m5 5>n    4
 
>EgD%=4-$uPQsUY[`Ta?
 ?

 
 
     YX QP RQ *  %%7Z@u    Ib    4 >EgE7T5MD%=1#e}tUZm?
 ?
     ! 	   0 
N+OPPG$EFFEOe,STTF)2WXXf f YX UT GF QPf  55>cdd
N+OPPI&opp! ! qp QP ed! D11I&^__! ! `_ 21! D!lmm2 2 nm2
 
N+LMM! ! NM ! 
N+LMM& 07e}tUZm^_ab]cfjlqer0s0s      NM  	M*YZZ! ! [Z ! 	M*YZZ,dd7754-RSQT3U3U.V.VWW, , XW [Z , 	M*LMM44=STTI&JKKp   ! !  LK UT NM! 	M*PQQI&NOO--6[\\, , ]\ PO RQ , 55FYOwxxG$>??, , @? yx, 55FYOwxxG$>??'', , (' @? yx , , ,r)   