
    Vh              	       z    d dl mZ d dlZd dlmc mZ d dlmZ dgZ	dedeej                  eeeef   f   defdZy)	    )UnionN)ExportedProgrammove_to_device_passeplocationreturnc           	      `   dt         j                  dt        t         j                  t        t        t        t        f   f   dt        fd| j
                  j                         D ]  \  }}t        |t         j                  j                        rXt         j                  j                  |j                   |j                              |j                        | j                  |<   |j                   |j                              | j                  |<    | j                  j                         D ]O  \  }}t        |t         j                        s!|j                   |j                              | j                  |<   Q | j                   j"                  D ]  }d|j$                  v r0|j$                  j'                         } |d         |d<   ||_        t)        j*                  fd|j,                  j/                  d            |j,                  d<    | j1                          | S )a  
    Move the exported program to the given device.

    Args:
        ep (ExportedProgram): The exported program to move.
        location (Union[torch.device, str, Dict[str, str]]): The device to move the exported program to.
            If a string, it is interpreted as a device name.
            If a dict, it is interpreted as a mapping from
            the existing device to the intended one

    Returns:
        ExportedProgram: The moved exported program.
    curr_devicer   r   c                     t        |t              r4t        |       |j                         v r|t        |          S t        |       S t        |      S N)
isinstancedictstrkeys)r
   r   s     L/home/dcms/DCMS/lib/python3.12/site-packages/torch/export/passes/__init__.py_get_new_devicez,move_to_device_pass.<locals>._get_new_device   sF     h%;8==?2K 011;''x=     devicec                     t        | t        j                        r"| j                   | j                              S | S r   )r   torchTensortor   )vr   r   s    r   <lambda>z%move_to_device_pass.<locals>.<lambda>?   s4    !U\\* dd?188X>?  r   val)r   r   r   r   r   
state_dictitemsr   nn	Parameterr   requires_grad_state_dict	constantsr   
_constantsgraphnodeskwargscopypytreetree_mapmetagetvalidate)r   r   kr   noder&   r   s    `    @r   r   r      s   "
!\\
!c4S>9:
! 

! ##% J1a++, % 2 2_QXXx89!BNN1
 !"_QXXx%H IBNN1J ""$ I1a& ttOAHHh$GHBMM!I  
t{{"[[%%'F.vh/?JF8 DK!?? IIMM% 	
		%
 KKMIr   )typingr   r   torch.utils._pytreeutils_pytreer(   torch.export.exported_programr   __all__r   r   r   r    r   r   <module>r6      sS      $ $ 9 !
!;;#(sDcN)J#K;;r   