
    j                    r    d dl 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mZ dd
ZdddZ	 	 dddZdS )    )annotations)partial)PathN)PosePose26)LOGGERYAMLmodeltorch.nn.Modulereturnc           	         ddl }|                                 D ][}t          |t                    s|                    t          t          t          |          t          u           |          |_	        \| S )zRApply ExecuTorch-specific model patches required for export/runtime compatibility.r   N)	is_pose26)
typesmodules
isinstancer   
MethodTyper   _executorch_kpts_decodetyper   kpts_decode)r
   r   ms      h/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/ultralytics/utils/export/executorch.pyexecutorch_wrapperr      sv    LLL]]__ k k!T"" 	((1HTXYZT[T[_eTe)f)f)fhijjL    Fkptstorch.Tensorr   boolc                   | j         d         }|j        d         } |j        |g| j         dR  }| j        d         }| j        d         }|r|ddddddf         |z   n|ddddddf         dz  |dz
  z   |z  }|d	k    r8t          j        ||dddddd	f                                         fd          }|                    || j        d          S )
zKDecode pose keypoints for ExecuTorch export with XNNPACK-safe broadcasting.   r   )NNN   g       @g      ?   )		kpt_shapeshapeviewanchorsstridestorchcatsigmoidnk)	selfr   r   ndimbsyr%   r&   as	            r   r   r      s   >!D	AB	"*t~*r***A l:&Gl:&G$-	X!AAAqqq"1"H+

AaaaBQBhK#4ESV4W\ccAqyyIq!AAAqqq!A#I,..00115566"dgr"""r    file
Path | strsample_inputmetadatadict | Noneprefixstrc                j   ddl m} ddlm} ddlm} t          j        d| d|j         d           t          |          }t          t          |                              |j        d                    }|                    d	d	
           ||                    d          j        z  }	 |t           j                            | |f           |            g                                          }
|	                    |
j                   |t+          j        |dz  |           t          |          S )a  Export a PyTorch model to ExecuTorch format.

    Args:
        model (torch.nn.Module): The PyTorch model to export.
        file (Path | str): Source model file path used to derive output names.
        sample_input (torch.Tensor): Example input tensor for tracing/export.
        metadata (dict | None, optional): Optional metadata to save as YAML.
        prefix (str, optional): Prefix for log messages.

    Returns:
        (str): Path to the exported ExecuTorch model directory.
    r   )version)XnnpackPartitioner)to_edge_transform_and_lower
z! starting export with ExecuTorch z..._executorch_modelT)parentsexist_okz.pte)partitionerNzmetadata.yaml)
executorchr9   9executorch.backends.xnnpack.partition.xnnpack_partitionerr:   executorch.exirr;   r   info__version__r   r7   replacesuffixmkdirwith_suffixnamer'   exportto_executorchwrite_bytesbufferr	   save)r
   r1   r3   r4   r6   executorch_versionr:   r;   
output_dirpte_file
et_programs              r   torch2executorchrT   (   sT   & 988888\\\\\\;;;;;;
KaVaa>P>\aaabbb::Dc$ii''5HIIJJJTD111D,,V4499H,,EL?33''))*   moo  *+++	*.999z??r   )r
   r   r   r   )F)r   r   r   r   r   r   )Nr0   )r
   r   r1   r2   r3   r   r4   r5   r6   r7   r   r7   )
__future__r   	functoolsr   pathlibr   r'   ultralytics.nn.modulesr   r   ultralytics.utilsr   r	   r   r   rT    r   r   <module>r[      s    # " " " " "              / / / / / / / / * * * * * * * *   # # # # #& !' ' ' ' ' ' 'r   