
    jG                        d dl Z d dlZ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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mZ d d	l m!Z! d d
l"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z* d Z+ej,        -                    dddg          d             Z.ej,        -                    dddg          d             Z/d Z0ej,        1                    e' d          ej,        -                    dddg          d                         Z2ej,        j3        ej,        1                    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                                     Z4ej,        j3        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                         Z5ej,        j3        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                         Z6ej,        j3        ej,        1                    e d          ej,        1                    e$ d           ej,        1                    ej7        d!          ej,        1                    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&                                                                         Z8ej,        j3        ej,        1                    ej9         pe% d'          ej,        1                    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*                                                 Z:ej,        1                    e$ d           ej,        1                    ed+          ej,        1                    eoed,          ej,        1                    ej7        d!          d-                                                 Z;ej,        1                    ej9         d.          ej,        1                    e% d/          ej,        1                    e d0          d1                                     Z<ej,        1                    dd2          ej,        1                    e d3          d4                         Z=ej,        1                    dd5          d6             Z>ej,        j3        ej,        1                    e# d7          d8                         Z?ej,        j3        ej,        1                    e# d7          ej,        -                    d9d:  e	eddgddgddgddg          D                       d;                                     Z@ej,        j3        ej,        1                    e& d<          d=                         ZAej,        j3        ej,        1                    e& d<          ej,        -                    d> eB e	eddgdg                              d?                                     ZCej,        1                    e) d@          ej,        1                    ejD         dA          ej,        1                    e dB          ej,        1                    edC          dD                                                 ZEej,        1                    ej9         pe) dE          ej,        1                    edF          dG                         ZFej,        j3        ej,        1                    ej9         pe) dE          ej,        1                    edF          ej,        -                    dHe          dI                                                 ZGej,        j3        ej,        1                    e( pe*dJ          ej,        1                    e peoedK          ej,        1                    edL          dM                                                 ZHej,        j3        ej,        1                    e pedN          ej,        1                    ejI         dO          dP                                     ZJdS )Q    N)redirect_stderrredirect_stdout)product)Path)MODELSOURCE)YOLO)	TASK2DATA
TASK2MODELTASKS)ARM64	IS_DOCKERIS_RASPBERRYPILINUXMACOSMACOS_VERSIONWINDOWSchecks)
torch2onnx)
TORCH_1_10
TORCH_1_11
TORCH_1_13	TORCH_2_0	TORCH_2_1	TORCH_2_8	TORCH_2_9
TORCH_2_12c                 P    t           r| dk    rt          j        d           dS dS dS )zRSkip semantic segmentation export tests on Raspberry Pi due to memory constraints.semanticzYSemantic segmentation export tests are skipped on Raspberry Pi due to memory constraints.N)r   pytestskip)tasks    U/var/www/html/Carbon-Document/venv/lib/python3.11/site-packages/tests/test_exports.pyskip_rpi_semanticr$       s>     q$*,,opppppq q,,    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     r#   test_export_torchscriptr2   &   sH     ;;]U"V]^^DDJJvR      r%   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.   r0   s     r#   test_export_onnxr6   -   sG     ;;VTWUUDDJJvR      r%   c                   	 ddg t          j                    		fd}|                     t          j        d|           dt
          ffdfdt          d          D             }|D ]}|                                 |D ]}|                                 rJ d             d	k    sJ d
             dS )z9Ensure ONNX exports do not overlap across worker threads.r   c                      5  dz  t                    d d d            n# 1 swxY w Y   t          j        d           5  dz  d d d            d S # 1 swxY w Y   d S )N   g?)maxtimesleep)argskwargsactive
max_active
state_locks     r#   fake_exportzBtest_torch2onnx_serializes_concurrent_exports.<locals>.fake_export;   s     	1 	1aKFZ00J	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	
4 	 	aKF	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   &**AA!Ar/   indexc           
         	 t          t          j                                        t          j        dddd          t          d|  dz                       d S # t          $ r }                    |           Y d }~d S d }~ww xY w)Nr9         zexport-z.onnx)r   torchnnIdentityzerosstr	Exceptionappend)rC   errorerrorstmp_paths     r#   export_modelzCtest_torch2onnx_serializes_concurrent_exports.<locals>.export_modelF   s    	!ux((**EK1a,C,CSTj^cTjTjTjIjEkEklllll 	! 	! 	!MM%         	!s   AA 
B%B  Bc                 >    g | ]}t          j        |f           S ))targetr=   )	threadingThread).0irQ   s     r#   
<listcomp>zAtest_torch2onnx_serializes_concurrent_exports.<locals>.<listcomp>L   s+    RRRAy|1$???RRRr%      zConcurrent export errors: r9   z&Expected max 1 concurrent export, got N)	rT   LocksetattrrG   r4   intrangestartjoin)
monkeypatchrP   rB   threadsthreadr?   rO   rQ   r@   rA   s
    `   @@@@@r#   -test_torch2onnx_serializes_concurrent_exportsrc   4   s'   FJF!!J       
Hk:::!C ! ! ! ! ! ! ! SRRRqRRRG    <<<F<<<<:???QZQQ?????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   r0   s     r#   test_export_openvinorm   V   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 rV   r"   r5   int8halfbatchnmsr&   s           r#   rX   rX   f   sr       :D'4uc7 
 
 %)J$6$63$6G$6PS$6	 
wdE38 %7$6$6r%   r9      c                    t          |            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.rf   r)   )	r*   r,   r5   rs   rt   ru   datarv   r&   rg   @   )r,   ru   Tignore_errorsN)r$   r	   r   r/   r
   r   r   rh   ri   rj   rk   rl   r   shutilrmtreer"   r5   rs   rt   ru   rv   r&   r1   s           r#   test_export_openvino_matrixr   b   s     d
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 ro   )r   )	rV   r"   r5   rs   rt   ru   simplifyrv   r&   s	            r#   rX   rX      s       DD'4uhW 
 
 %)J$6$63$6C$6PZ$6`g$6lo$6	 
wdE8S'B %7$6$6r%   c                     t          |            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.r4   r)   )	r*   r,   r5   rs   rt   ru   r   rv   r&   rz   r-   Nr$   r	   r   r/   r   r   unlink)	r"   r5   rs   rt   ru   r   rv   r&   r1   s	            r#   test_export_onnx_matrixr      s     d
4 !!(( ) 
 
D DJJx%W'<rr"====JJr%   c           	      H    g | ]\  }}}}}}}|d k    r||r||||||||f S ro   rq   rr   s           r#   rX   rX      s]       :D'4uc7 ####S#	 
wdE38 $##r%   c           
         t          |            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,   r5   rs   rt   ru   rv   r&   rz   r-   Nr   r   s           r#   test_export_torchscript_matrixr      s     d
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 )detectrp   rq   )rV   r"   r5   rs   rt   rv   ru   r&   s           r#   rX   rX      s     
 
 
:D'4sE7 

 
 !!c! "  "### $  $ 
wdC8 $##r%   c           
          t          |            t          t          |                                        dd||||||          } t          |          t          g|z  d           t          j        |           dS )zHTest YOLO export to CoreML format with various parameter configurations.coremlr)   r   r-   Nr$   r	   r   r/   r   r}   r~   )r"   r5   rs   rt   rv   ru   r&   r1   s           r#   test_export_coreml_matrixr      s    . d
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 ro   )r   r   rr   s           r#   rX   rX      s       :D'4uc7   
""s" # # #& #  #   # 
wdE38 #""r%   c           
         t          |            t          t          |                                        dd||||||          } t          |          t          g|z  d           t          |                                           dS )zLTest YOLO export to TFLite format considering various export configurations.tfliter)   r   r-   Nr   r   s           r#   test_export_tflite_matrixr      s    4 d
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*   rv   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stderrr1   outputs       r#   test_export_coremlr      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.r   r)   r   r-   Nr.   modelr1   s     r#   test_export_tfliter     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)   r   r-   Nr.   r   s     r#   test_export_pbr     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)   r   N)r	   r   r/   rq   r%   r#   test_export_paddler   &  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)   r   r-   Nr.   r1   s    r#   test_export_mnnr   ,  sC     ;;U"55DDJJvR      r%   z task, int8, half, batch, end2endc                 0    g | ]\  }}}}}|r||||||fS rq   rq   )rV   r"   rs   rt   ru   r&   s         r#   rX   rX   8  sM       ,D$eW 	tT5'*  r%   c                    t          |            t          t          |                                        dd||||          } t          |          t          g|z  d           t          |                                           dS )zITest YOLO export to MNN format considering various export configurations.r   r)   )r*   r,   rs   rt   ru   r&   r-   Nr   )r"   rs   rt   ru   r&   r1   s         r#   test_export_mnn_matrixr   4  s     d
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)   r   r-   Nr.   r   s    r#   test_export_ncnnr   F  sC     ;;V266DDJJvR      r%   ztask, half, batchc                     t          |            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,   rt   ru   r-   Tr{   Nr   )r"   rt   ru   r1   s       r#   test_export_ncnn_matrixr   N  sy    
 d
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)   r   r-   N)r	   r/   r   r   s     r#   test_export_imxr   Y  sG     E<<uB<//DDJJvR      r%   z&Requires Python>=3.10 and Torch>=2.9.0zSkipping test on Windowsc                     t          t                                        dd          } t          |                                           sJ d|              t          |           dz  }|                                sJ d|             t          |           dz  }|                                sJ d|             t          j        | d	
           dS )z,Test YOLO model export to ExecuTorch format.
executorchr)   r   z/ExecuTorch export failed, directory not found: 	model.ptez ExecuTorch .pte file not found: metadata.yamlz$ExecuTorch metadata.yaml not found: Tr{   Nr	   r   r/   r   existsr}   r~   )r1   pte_filemetadata_files      r#   test_export_executorchr   g  s     ;;\<<D::XX XRV X XXXDzzK'H??KKKKKKKJJ0M!!YY#Y-#Y#YYY!
M$d++++++r%   r"   c                    t          |            t          t          |                                        dd          }t	          |                                          sJ d|  d|             t	          |          d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)   r   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 'Tr{   N)r$   r	   r   r/   r   r   r}   r~   )r"   r1   r   r   s       r#   test_export_executorch_matrixr   w  s    d
4 !!((B(GGD::jj jd j jdh j jjjDzzK'H??]]]$]]S[]]]]JJ0M!!kk#kSW#k#k\i#k#kkk!
M$d++++++r%   z+Axelera export requires 2.8.0<=torch<2.12.0zNAxelera export is only supported on Linux and is not supported on ARM64 DockerzFTest disabled due to OOM (Out of Memory) issues on Raspberry Pi 5 16GBc                      t          t                                        ddd          } t          |                                           sJ d|              t          j        | d           dS )	z#Test YOLO export to Axelera format.axelerarz   z
coco8.yaml)r*   r,   ry   z,Axelera export failed, directory not found: Tr{   Nr   r   s    r#   test_export_axelerar     sn     ;;Yb|LLD::UU Ut U UUU
M$d++++++r%   z0DeepX export only supported on non-aarch64 Linuxz=Requires Python>=3.12 for CI validation due to torch upgradesc                      t          t                                        dd          } t          |                                           sJ d|              t          j        | d           dS )z!Test YOLO export to DeepX format.deepxr)   r   z*DeepX export failed, directory not found: Tr{   Nr   r   s    r#   test_export_deepxr     sl     ;;WB77D::SS ST S SSS
M$d++++++r%   )Kr   r}   rT   r;   rk   
contextlibr   r   	itertoolsr   pathlibr   r    rG   testsr   r   ultralyticsr	   ultralytics.cfgr
   r   r   ultralytics.utilsr   r   r   r   r   r   r   r   ultralytics.utils.export.enginer   ultralytics.utils.torch_utilsr   r   r   r   r   r   r   r   r$   markparametrizer2   r6   rc   skipifrm   slowr   r   r   IS_PYTHON_3_13r   IS_PYTHON_MINIMUM_3_10r   r   r   r   r   r   r   r   listr   IS_PYTHON_MINIMUM_3_9r   r   r   r   IS_PYTHON_MINIMUM_3_12r   rq   r%   r#   <module>r      s   
			        7 7 7 7 7 7 7 7                             8 8 8 8 8 8 8 8 8 8 l l l l l l l l l l l l l l l l l l l l 6 6 6 6 6 6	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	q q q UDM22! ! 32! UDM22! ! 32!R R RD 	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! 55FYOwxxG$>??, , @? yx, 55FYOwxxG$>??'', , (' @? yx , 	M/Z8effI&%%I[    N+stt, , ut	  gf , I&/abb%%.m   , ,  cb 
, , ,r%   