
    Vh5                     j   U d Z ddlZddlZddlZddlmZ ddlmZmZm	Z	 ddl
Z
ddlmZ ddlmZmZmZmZ erddlmZ erddlZdd	lmZmZmZ  G d
 d      Zn
 G d d      Zdaed   ed<    G d d      Zdee   fdZ	  ed       G d d             Z	  ed       G d d             Zd Z d Z!y)z"Contains definitions of the methods used by the _BaseDataLoaderIter workers.

These **needs** to be in global scope since Py2 doesn't support serializing
static methods.
    N)	dataclass)OptionalTYPE_CHECKINGUnion)ExceptionWrapper   )	HAS_NUMPY
IS_WINDOWSMP_STATUS_CHECK_INTERVALsignal_handling)Dataset)BOOLDWORDHANDLEc                       e Zd ZddZd Zy)ManagerWatchdogNc                 X   t        j                         | _        t        j                  dd      | _        t        t        t        f| j
                  j                  _	        t        | j
                  j                  _        t        t        f| j
                  j                  _	        t        | j
                  j                  _        d}| j
                  j                  |d| j                        | _        | j                  s't        j                  t        j                               d| _        y )Nkernel32T)use_last_errori   r   F)osgetppidmanager_pidctypesWinDLLr   r   r   OpenProcessargtypesr   restypeWaitForSingleObjectmanager_handleWinErrorget_last_errormanager_dead)selfSYNCHRONIZEs     N/home/dcms/DCMS/lib/python3.12/site-packages/torch/utils/data/_utils/worker.py__init__zManagerWatchdog.__init__   s    !zz|D #MM*TJDM27u1EDMM%%.06DMM%%-:@%DMM--68=DMM--5 %K"&--";";Q 0 0#D &&oof&;&;&=>> %D    c                     | j                   s.| j                  j                  | j                  d      dk(  | _         | j                    S )Nr   )r"   r   r   r   r#   s    r%   is_alivezManagerWatchdog.is_alive4   sF    $$ MM55d6I6I1MQRR ! ((((r'   returnN__name__
__module____qualname__r&   r*    r'   r%   r   r      s    	&*	)r'   r   c                       e Zd ZddZd Zy)r   Nc                 D    t        j                         | _        d| _        y )NF)r   r   r   r"   r)   s    r%   r&   zManagerWatchdog.__init__?   s    !zz|D %Dr'   c                     | j                   s&t        j                         | j                  k7  | _         | j                    S N)r"   r   r   r   r)   s    r%   r*   zManagerWatchdog.is_aliveC   s3    $$$&JJLD4D4D$D!((((r'   r+   r-   r1   r'   r%   r   r   >   s    	&	)r'   
WorkerInfo_worker_infoc                   X     e Zd ZU eed<   eed<   eed<   ded<   dZd Z fdZd	 Z xZ	S )
r6   idnum_workersseedr   datasetFc                     |j                         D ]  \  }}t        | ||        t        |j                               | _        d| _        y )NT)itemssetattrtuplekeys_WorkerInfo__keys_WorkerInfo__initialized)r#   kwargskvs       r%   r&   zWorkerInfo.__init__S   sA    LLN 	 DAqD!Q	 FKKM*!r'   c                     | j                   r#t        d| j                  j                   d      t        |   ||      S )NzCannot assign attributes to z objects)rC   RuntimeError	__class__r.   super__setattr__)r#   keyvalrI   s      r%   rK   zWorkerInfo.__setattr__Y   sD    .t~~/F/F.GxP  w"3,,r'   c           	          | j                   D cg c]  }| dt        | |        }}| j                  j                   ddj	                  |       dS c c}w )N=(z, ))rB   getattrrI   r.   join)r#   rE   r>   s      r%   __repr__zWorkerInfo.__repr__`   sZ    48KK@qA3aa()*@@..))*!DIIe,<+=Q?? As   A)
r.   r/   r0   int__annotations__rC   r&   rK   rT   __classcell__)rI   s   @r%   r6   r6   L   s.    G
IM"-@r'   r,   c                      t         S )a  Returns the information about the current
    :class:`~torch.utils.data.DataLoader` iterator worker process.

    When called in a worker, this returns an object guaranteed to have the
    following attributes:

    * :attr:`id`: the current worker id.
    * :attr:`num_workers`: the total number of workers.
    * :attr:`seed`: the random seed set for the current worker. This value is
      determined by main process RNG and the worker id. See
      :class:`~torch.utils.data.DataLoader`'s documentation for more details.
    * :attr:`dataset`: the copy of the dataset object in **this** process. Note
      that this will be a different object in a different process than the one
      in the main process.

    When called in the main process, this returns ``None``.

    .. note::
       When used in a :attr:`worker_init_fn` passed over to
       :class:`~torch.utils.data.DataLoader`, this method can be useful to
       set up each worker process differently, for instance, using ``worker_id``
       to configure the ``dataset`` object to only read a specific fraction of a
       sharded dataset, or use ``seed`` to seed other libraries used in dataset
       code.
    )r7   r1   r'   r%   get_worker_inforY   e   s
    4 r'   T)frozenc                       e Zd ZU eed<   y)_IterableDatasetStopIteration	worker_idN)r.   r/   r0   rU   rV   r1   r'   r%   r\   r\      s    Nr'   r\   c                   "    e Zd ZU dZee   ed<   y)_ResumeIterationNr;   )r.   r/   r0   r;   r   rU   rV   r1   r'   r%   r_   r_      s    D(3-r'   r_   c           	         d}dd}d}dddd|| z  | d	z	  d
g}d
gdz  }|fd}fd}t        t        |            D ]  }	 |||	         ||	<    t        t        |            D ]9  }
t        t        |            D ]   }|
|k7  s	 |||    |||
               ||<   " ; |}g }t        d      D ];  }||   }||z  z  }||z  z  }||z  z  }||z	  z  z  }|j                  |       = |S )NiװCl   u=& l   y iXl   y isI   l        r      c                 N    | z  z  } z  z  | z  z  } | | z	  z  z  } | S r5   r1   )valueMASK32MULT_AXSHIFThash_const_As    r%   hashz_generate_state.<locals>.hash   sH    %/$v-7%/%6/*f4r'   c                 N    | z  z  }|z  z  }||z
  z  }||z	  z  z  }|S r5   r1   )	xyresult_xresult_yresultrf   
MIX_MULT_L
MIX_MULT_Rrh   s	        r%   mixz_generate_state.<locals>.mix   sG    Nf,Nf,X%/Ff,-7r'   )rangelenappend)	base_seedr]   INIT_AINIT_BMULT_Bentropypoolrj   rs   ii_srci_dsthash_const_Bstatedata_valrf   rq   rr   rg   rh   ri   s                  @@@@@@r%   _generate_stater      s^   FFFFJJFF)f,i2oqAG37DL 3t9 #wqz"Q# s4y! B3t9% 	BE~!$u+tDK/@AU	BB
 LEq ;|+v5$v-7|+v5F 23v=X Lr'   c                 *   	 t        j                          t        j                  j	                  d       t        j
                  d       ||
z   }t        j                  |       t        j                  |       t        r+t        ||
      }dd l}|j                  j                  |       ddlm} ddlm} t        j                          }t#        ||      r|J |j                  |        |||      }t%        |
|||      addlm} d }	 |	 |	|
       |j+                  | ||||      }d
}t1               }|j3                         r	 |j5                  t6              }t#        |t<              ri|j?                  |d f       d
}t#        ||      r2|j                  J |j                  |j                          |||      }|j+                  | ||||      }||jA                         s|sJ nZ|jA                         s|r|\  }}||}d }n	 jC                  |      }|j?                  ||f       ~~~~|j3                         r|jA                         r!|jM                          |jO                          y y # t,        $ r t/        d|
 	      }Y kw xY w# t8        j:                  $ r Y yw xY w# t,        $ rF}t#        |tD              r| |jF                  k(  rtI        |
      }d}nt/        d|
 	      }Y d }~d }~ww xY w# tJ        $ r Y w xY w)Npt_data_workerr   r   )IterDataPipe)apply_random_seed)r9   r:   r;   r<   )_DatasetKindzin DataLoader worker process )whereF)timeoutT)(r   _set_worker_signal_handlerstorchmultiprocessing_set_thread_nameset_num_threadsrandomr;   manual_seedr	   r   numpytorch.utils.datar   torch.utils.data.graph_settingsr   	Generator
isinstancer6   r7   r   create_fetcher	Exceptionr   r   r*   getr   queueEmptyr_   putis_setfetchStopIterationIterabler\   KeyboardInterruptcancel_join_threadclose)dataset_kindr<   index_queue
data_queue
done_eventauto_collation
collate_fn	drop_lastrw   init_fnr]   r:   persistent_workersshared_seedr;   np_seednpr   r   
shared_rngr   init_exceptionfetcheriteration_endwatchdogridxindexdataes                                 r%   _worker_loopr      s   &| 	335../?@a 9$D$%i;GIINN7#1E__&
g|,***"";/'<G "kg
 	2
	"	""11g~z9G( "$!OO,DOE !-.4y) %g|466---**1662/DG '55 '>:y !((*m;;""$ JC)%!%"==/D$ NNC;'c5!o !v %%' a  	-5i[AN	0 ;; D ! "1m4(L,A,AA<YG )-
  0$A)"M &  s   D	L I; +L 	J B/L J4  'L ;JL JL J1-L 0J11L 4	L=<K>9L >LL 	LL)"__doc__r   r   r   dataclassesr   typingr   r   r   r   torch._utilsr    r	   r
   r   r   r   r   r   ctypes.wintypesr   r   r   r   r7   rV   r6   rY   r\   r_   r   r   r1   r'   r%   <module>r      s    
   ! 1 1  ) N N (33
) )@) ) (,h|$ +@ @2*- : @ $   L $  D1hRr'   