
    Pi                        d dl Z d dlZd dlZd dlmZmZmZmZ d dlZd dl	Zd dl
mZ d dlmZmZ d dlmZmZ d dlmZ d dlmZmZ ded	ej        d
ededej        dej        deeef         dee         fdZ G d dee                   ZdS )    N)AnyDictOptionalUnion)
pin_memory)BaseNodeT)ExceptionWrapperStartupExceptionWrapper)_SingleThreadedMapper)MonotonicIndexSnapshotStoresourceqsnapshot_storesnapshot_frequency	semaphore
stop_event	device_iddevicec           	      ~   t                      	 	 ddt          dt          t          t          t
          t          f         t          f                  ffd}	 t          j	        d           t          j
                            d           |dk    r t          j                            |           n|d	k    r t          j                            |           nh|t          j                                        k    rFt#          t          t          j                                                  }	|	                    |           t%          |t&                    r|d
k    sJ d|                                 |                                            n:# t,          $ r- t          d|           }
                    |
           Y dS w xY wd
}|                                s|                    dd          s,	 t3          |           }t5          ||          }|dz  }d}|d
k    r||z  d
k    r|                                 } ||d|           nS# t6          $ r}
|
} ||d           Y d}
~
dS d}
~
wt,          $ r$ t9          d|           } ||d           Y dS w xY w|                                dS dS )zThis is fork of from torch.utils.data._utils.pin_memory import _pin_memory_loop
    to remove the index tuples.

    This setting is thread local, and prevents the copy in pin_memory from
    consuming all CPU cores.
    TNblocksnapshotc                                                      }|r                    ||                               | |f|           d S )N)r   versionr   )getappendput)itemr   r   _idxidxr   r   s       n/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torchdata/nodes/pin_memory.py_putz_pin_memory_loop.<locals>._put+   sT    
 wwyy 	C!!8T!BBB	tTl%(((((       pt_data_pincudaxpur   z5snapshot_frequency must be non-negative integer! Got )r   z'in _pin_memory_loop startup for device )whereg?)blockingtimeoutF)r   r   r   zin _pin_memory_loop for device )TN)r   boolr   r   r   strr   r   torchset_num_threadsmultiprocessing_set_thread_namer(   
set_devicer)   _C_get_privateuse1_backend_namegetattr
isinstanceintappend_initial_snapshot
state_dict	Exceptionis_setacquirenextr   StopIterationr
   )r   r   r   r   r   r   r   r   r$   custom_device_modeyieldedr    r   r"   s    ``           @r#   _pin_memory_looprC      sG   " 

C MQ) )) 5c3h1H!HIJ) ) ) ) ) ) ) )a   ..}===VJ!!),,,,u__I  ++++ux==???? 'ux/U/U/W/W X X((333 )3//	X4F!4K4K4KWCUWW 5L4KK..8I8I8K8K.LLLL   #*_T]*_*_```...:::
 G!!   $ << 		<<DdF++DqLGH!A%%'4F*F!*K*K!,,..DUX66666 	 	 	DDU####EEEEE 	 	 	#*WI*W*WXXXDDU####EE	 !!     s2   D7F 3G
	G
<AI 
J$I33-J$#J$c                        e Zd ZdZ	 	 ddee         dedef fdZdd	e	e
eef                  f fd
Zd Zde
eef         fdZ xZS )	PinMemoryau  Pins the data of the underlying node to a device. This is backed by torch.utils.data._utils.pin_memory._pin_memory_loop.

    Args:
        source (BaseNode[T]): The source node to pin the data from.
        pin_memory_device (str): The device to pin the data to. Default is "".
        snapshot_frequency (int): The frequency at which to snapshot the state of the source node. Default is
            1, which means that the state of the source node will be snapshotted after every item. If set
            to a higher value, the state of the source node will be snapshotted after every snapshot_frequency
            items.
     r&   r   pin_memory_devicer   c                 >   t                                                       || _        || _        t	          |          dk    rd | _        n|| _        | j        dk    r$t          j                                        | _	        n| j        t          j
                                        k    rKt          t          t          j
                                                  }|                                | _	        n#t          j                                        | _	        d | _        d S )Nr   r)   )super__init__r   r   len_pin_memory_devicer/   r)   current_device_current_devicer4   r5   r6   r(   _it)selfr   rG   r   r@   	__class__s        r#   rJ   zPinMemory.__init__m   s     	"4 !!Q&&&*D##&7D#"e++#(9#;#;#=#=D  $(N(N(P(PPP 'ux/U/U/W/W X X#4#C#C#E#ED  #(:#<#<#>#>D 7;r%   Ninitial_statec           	         t                                          |           | j        | j                                         | `t	          | j        dt          j        t          | j	        | j
                  | j        |          | _        d S )Nr&   )r   r   )r   prefetch_factorworkerr   rR   )rI   resetrO   	_shutdownr   r   	functoolspartialrC   rN   rL   r   )rP   rR   rQ   s     r#   rV   zPinMemory.reset   s    m$$$8H   (;$ ..  
  $6'

 

 

r%   c                 *    t          | j                  S N)r>   rO   rP   s    r#   r>   zPinMemory.next   s    DH~~r%   returnc                 4    | j                                         S r[   )rO   	get_stater\   s    r#   r_   zPinMemory.get_state   s    x!!###r%   )rF   r&   r[   )__name__
__module____qualname____doc__r   r	   r.   r8   rJ   r   r   r   rV   r>   r_   __classcell__)rQ   s   @r#   rE   rE   a   s        	 	 "$"#	< << <  	< < < < < <0
 
8DcN#; 
 
 
 
 
 
"  $4S> $ $ $ $ $ $ $ $r%   rE   )rX   queue	threadingtypingr   r   r   r   r/   torch.multiprocessing"torch.utils.data._utils.pin_memoryr   torchdata.nodes.base_noder   r	   !torchdata.nodes.exception_wrapperr
   r   torchdata.nodes.mapr   torchdata.nodes.snapshot_storer   r   Queuer8   BoundedSemaphoreEventr.   rC   rE    r%   r#   <module>rr      s            - - - - - - - - - - - -      9 9 9 9 9 9 1 1 1 1 1 1 1 1 W W W W W W W W 5 5 5 5 5 5 H H H H H H H HFF{F "F 	F
 )F F S#XF SMF F F FR9$ 9$ 9$ 9$ 9$ 9$ 9$ 9$ 9$ 9$r%   