
    ϑi	^                       S SK Jr  S SKrS SKrS SKrS SKrS SKrS SKrS SKJ	r	J
r
JrJrJrJrJr  S SKrSSKJrJrJr  SSKJrJr  SSKJrJrJr  SS	KJrJr  SS
KJ r J!r!J"r"  \	(       aK  S SK#r#S SK$J%r%J&r&  S SK'Jr(  S SKJ)r)  S SK*J+r+  S SK,J-r-  S SK.J/r/  S SK0J1r1  \" S5      r2\" S5      r3 " S S\5      r4Sr5Sq6Sq7Sq8SS jr9S r:S r; " S S5      r< " S S5      r=g)     )annotationsN)TYPE_CHECKINGAnyAnyStrCallableProtocolTypeVaroverload   )_current_expected_place_get_paddle_place_get_paddle_place_list)corein_dynamic_mode   )BatchSamplerIterableDatasetSubset)DistributedBatchSampler_InfiniteIterableSampler)_DataLoaderIterMultiProcess_DataLoaderIterSingleProcess_DatasetKind)MappingSequence)Tensor)	PlaceLike)_Place)_DataLoaderIterBase)Dataset_K_Vc                      \ rS rSr\    S	S j5       r\S
S j5       r\SS j5       r\    SS j5       r\SS j5       rSrg)
_CollateFnB   c                    g N selfbatchs     P/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/io/reader.py__call___CollateFn.__call__C   s      #    c                    g r'   r(   r)   s     r,   r-   r.   H       ?Br/   c                    g r'   r(   r)   s     r,   r-   r.   K   r1   r/   c                    g r'   r(   r)   s     r,   r-   r.   N   s     "r/   c                    g r'   r(   r)   s     r,   r-   r.   S   s    KNr/   r(   N)r+   z5Sequence[npt.NDArray[Any]] | Sequence[numbers.Number]returnznpt.NDArray[Any])r+   zSequence[Tensor]r5   r   )r+   zSequence[AnyStr]r5   r   )r+   zSequence[Mapping[_K, _V]]r5   zMapping[_K, _V])r+   zSequence[Sequence[_V]]r5   zSequence[_V])__name__
__module____qualname____firstlineno__r
   r-   __static_attributes__r(   r/   r,   r$   r$   B   st    		#N	#	# 
	# 
B 
B	B 
B		"2	"	" 
	" 
N 
Nr/   r$   <   F  c                    U q Uqg r'   )USE_AUTOTUNETUNING_STEPS)use_autotunetuning_stepss     r,   set_autotune_configrB   a   s    LLr/   c                     [        U 5      S:X  a  [        $ [        U 5      S:X  a  [        U S   [        5      (       d   eU S   qg )Nr   r   )lenUSE_PINNED_MEMORY
isinstancebool)argss    r,   use_pinned_memoryrI   h   s>    
4yA~  4yA~*T!Wd";";;; Gr/   c                   [        U [        [        45      (       d  U /n / nU  H[  n[        U[        R                  5      (       d(  [        R                  " 5       nUR                  U5        UnUR                  U5        M]     U$ r'   )rF   listtupler   Place	set_placeappend)placesretptmps       r,   _convert_placesrT   q   sh    ftUm,,
C!TZZ((**,CMM!A

1  Jr/   c                  >    \ rS rSrS rS rS rS rS rS r	S r
S	rg
)AuToTune   c                J    Xl         [        R                  " 5       S-  U l        g )Nr   )loadermultiprocessing	cpu_countmax_num_worker)r*   rY   s     r,   __init__AuToTune.__init__   s    -779A=r/   c                   [         (       a  U R                  5       (       d  U R                  R                  $ U R	                  5       nUc  U R                  R                  $ [
        R
                  " 5       n[        R                  " S5        [        R                  " S[        U R                  R                  5      -   5        Sn[        S5      n[        R                  " S[        U R                  5      -   5        SnXPR                  :  a  XQl        U R                  U5      nUS-  U:  a  UnUnO&U R                  UUUU R                  5      nXs:X  a  OIUn[        R                  " S[        U5      -   S-   [        U5      -   5        US	-  nXPR                  :  a  M  [        R                  " S
[        U5      -   5        [        R                  " S[        [
        R
                  " 5       U-
  5      -   S-   5        U$ )Nz(========= DataLoader Auto Tune =========zUser config for DataLoader: r   infz"Tuning Range for num_workers: 0 ~ g      ?znum_workers:  avg_cost: r   z'auto_tune dataLoader best_num_workers: z AutoTuning Cost for DataLoader: z seconds)r>   need_autotunerY   num_workersget_autotune_loadertimeloggingdebugstrfloatr\   evaluate_reader_costis_bestinfo)r*   auto_tune_loaderauto_tune_startbest_num_workersmin_costrc   avg_cost
update_nums           r,   r-   AuToTune.__call__   s   d&8&8&:&:;;***  335#;;*** ))+@A*S1H1H-II	
 <03t7J7J3KK	
 ///+6(001ABH$)##. !\\$$''	
 1'1$MMk"#  h-  1K/ ///0 	5<L8MM	
 	.$))+/01	
  r/   c                V    [         R                  S:X  d  [         R                  S:X  a  gg)Ndarwinwin32FT)sysplatformr*   s    r,   rb   AuToTune.need_autotune   s    <<8#s||w'>r/   c                v    [        U[        -  [        U5      5      n[        U[	        [        U5      5      S9nU$ )N)indices)minr?   rD   r   rK   range)r*   dataset
batch_sizenum_samplessub_datasets        r,   get_sub_datasetAuToTune.get_sub_dataset   s3    *|3S\BWd53E.FGr/   c           	         [         R                   " U R                  5      nU R                  R                  R                  n[	        U R                  R                  [
        R                  R                  5      (       a  U R                  R                  R                  nU R                  X25      n[
        R                  R                  UUU R                  R                  R                  U R                  R                  R                  U R                  R                  R                  U R                  R                  R                  S9Ul        U$ [	        U R                  R                  [
        R                  R                  5      (       a  U R                  R                  R                  R                   nU R                  X25      n[
        R                  R                  UUU R                  R                  R                  S9Ul        U$ S nU$ )Nr   r   num_replicasrankshuffle	drop_last)r   r   r   )copyrY   batch_samplerr   rF   paddleior   r   r   nranks
local_rankr   r   r   samplerdata_source)r*   rY   r   r   r   s        r,   rd   AuToTune.get_autotune_loader   s   4;;'[[..99
KK%%vyy'H'H
 
 kk//77G..wCK#)99#D#D#%![[66==[[..991199++33== $E $F $  116993I3IJJkk//77CCG..wCK#)99#9#9#%++33== $: $F   Fr/   c                n   / nSn[         R                   " 5       n[        U5       HA  u  pVUR                  [         R                   " 5       U-
  5        [         R                   " 5       nMC     [        U5      S:  a  [	        USS  5      [        USS  5      -  nU$ [	        USS  5      [        USS  5      -  nU$ )Nr   r   )re   	enumeraterO   rD   sum)r*   readercostsrq   startidatas          r,   rj   AuToTune.evaluate_reader_cost   s    		 (GALLu,-IIKE ) u:>59~E!"I6H  59~E!"I6Hr/   c                $   SnUS-   nSnXd:  a  US:  a{  X`R                   l        U R                  U5      n[        R                  " S[        U5      -   S-   [        U5      -   5        US-  nXS-  U-  :  a  U$ US-  nUS-  nXd:  a  US:  a  M{  U$ )Nr   r      zfor back num_workers: ra   gffffff?g?)rY   rc   rj   rf   rg   rh   )	r*   r   best_workers	best_timenum_work_boundarysteprc   boundaryre   s	            r,   rk   AuToTune.is_best   s    "Q&-$(&1KK#,,V4DMM(k"#  d) AID$&11""q H -$( r/   )rY   r\   N)r6   r7   r8   r9   r]   r-   rb   r   rd   rj   rk   r:   r(   r/   r,   rV   rV      s&    >8 t
8r/   rV   c                  L   \ rS rSr% SrS\S'   S\S'   S\S'   S\S	'   S\S
'   S\S'   S\S'   S\S'   S\S'   S\S'   S\S'   S\S'   S\S'   S\S'   S\S'   S\S'                   S"                                   S#S jjrS$S jrS%S jrS%S  jr	S!r
g)&
DataLoaderi	  a   
DataLoader provides an iterator which iterates given dataset
once by the batch_sampler.

DataLoader supports single-process and multi-process data loading,
multi-process workers will be used to load data asynchronously if
:attr:`num_workers` is set as a positive number.

DataLoader supports map-style dataset and iterable-style dataset.

For map-style dataset(can get a sample from dataset with a given
index), please see :code:`paddle.io.Dataset`.

For iterable-style dataset(get samples from dataset iteratively,
like a Python iterator), please see :code:`paddle.io.IterableDataset`.

For :code:`batch_sampler` please see :code:`paddle.io.BatchSampler`

Notes:
    GPU tensor operation is not supported in subprocess currently,
    please don't use GPU tensor operations in pipeline which will
    be performed in subprocess, such as dataset transforms, collate_fn,
    etc. Numpy array and CPU tensor operation is supported.

**Disable automatic batching**

In certain cases such as some NLP tasks, instead of automatic batching,
handling batching manually in dataset is needed by users. For these
cases, automatic batching is disabled if both :attr:`batch_size` and
:attr:`batch_sampler` is set as None, each data got from :attr:`dataset`
should be batched data and will be processed with function define by
:attr:`collate_fn` or :attr:`default_collate_fn`.


Notes:
    When automatic batching is disabled, :attr:`default_collate_fn` will
    do nothing to data from dataset.


Args:
    dataset(Dataset): the dataset to load data from, should be an
        instance of subclass of :code:`paddle.io.Dataset` or
        :code:`paddle.io.IterableDataset`.
    feed_list (list(Tensor)|tuple(Tensor)|None, optional): feed Tensor list.
        The Tensors should be created by :code:`paddle.static.data()`.
        :attr:`feed_list` must be set if :attr:`return_list` is
        False. Default None.
    places(list(Place)|tuple(Place)|list(str)|None, optional): a list of Place,
        to put data onto, :attr:`places` can be None, if
        :attr:`places` is None, default place(CPUPlace or CUDAPlace(0))
        will be used. Default None. If ``places`` is list of string,
        the string in the list can be ``cpu``, ``gpu:x`` and ``gpu_pinned``,
        where ``x`` is the index of the GPUs.
    return_list (bool, optional): whether the return value on each device is
        presented as a list. If :attr:`return_list=False`, the return
        value on each device would be a dict of str -> Tensor, where
        the key of the dict is the name of each fed Tensors. If
        :attr:`return_list=True`, the return value on each device would
        be a list(Tensor). :attr:`return_list` can only be True
        in dynamic graph mode. Default True.
    batch_sampler(BatchSampler|None, optional): an instance of `paddle.io.BatchSampler`
        to generate batch indices to draw samples from :attr:`dataset`
        and combine a batch. Default None.
    batch_size(int|None, optional): sample number in a mini-batch, a substitution
        parameter for :attr:`batch_sampler`, if :attr:`batch_sampler`
        is not set, a default `paddle.io.BatchSampler` will be used
        and initialize by :attr:`batch_size`, :attr:`shuffle` and
        :attr:`drop_last`. Default 1.
    shuffle(bool, optional): whether to shuffle indices order before generate
        batch indices, a substitution parameter for :attr:`batch_sampler`
        see :attr:`batch_size`. Default False.
    drop_last(bool, optional): whether drop the last incomplete batch dataset size
        is not divisible by the batch size, a substitution parameter
        for :attr:`batch_sampler`, see :attr:`batch_size`. Default False
    collate_fn(Callable|None, optional): function to generate mini-batch data by merging
        the sample list, None for only stack each fields of sample in axis
        0(same as :attr::`np.stack(..., axis=0)`). Default None
    num_workers(int, optional): the number of subprocess to load data, 0 for no
        subprocess used and loading data in main process. Default 0
    use_buffer_reader (bool, optional): whether to use buffered reader.
        If use_buffer_reader=True, the DataLoader would prefetch
        batch data asynchronously, so it would speed up data feeding
        and occupies a little more CPU or GPU memory, i.e., the memory
        of one batch input data. Default True.
    reader_buffer_size (int, optional): This option takes effect only
        when use_buffer_reader is set to True. It specifies the number of
        batches the buffer reader prefetches in advance. Note that
        Increasing this value will result in a linear increase in CPU or GPU memory usage.
        Default 2.
    prefetch_factor (int, optional): Number of batch data the DataLoader would prefetch
        if use_buffer_reader=True. Default 2.
    use_shared_memory (bool, optional): whether to use shared memory to speed up
        putting data into inter-process queue, set :attr:`use_shared_memory`
        as True only when the shared memory space on your machine(e.g.
        space of '/dev/shm' on Linux operating system) is large enough.
        Shared memory will only be enabled in multi-process mode(num_workers
        > 0). Default True.
    timeout(int, optional): the timeout value for getting data form output queue
        of subprocesses. Default 0.
    worker_init_fn(Callable|None, optional): init function which will be called with
        worker id on each subprocess starting if not set as None. Default
        None.
    persistent_workers(bool, optional): whether to keep the workers in the DataLoader. Default False.

Returns:
    DataLoader: an iterable object for data iterating, each element of the generated data is a Tensor.

Examples:

    .. code-block:: python

        >>> # doctest: +SOLO('can not use multiprocessing testing `paddle.io.DataLoader`')
        >>> import numpy as np

        >>> import paddle
        >>> import paddle.nn as nn
        >>> import paddle.nn.functional as F
        >>> from paddle.io import Dataset, BatchSampler, DataLoader

        >>> BATCH_NUM = 20
        >>> BATCH_SIZE = 16
        >>> EPOCH_NUM = 4

        >>> IMAGE_SIZE = 784
        >>> CLASS_NUM = 10

        >>> # define a random dataset
        >>> class RandomDataset(Dataset):  # type: ignore[type-arg]
        ...     def __init__(self, num_samples):
        ...         self.num_samples = num_samples
        ...
        ...     def __getitem__(self, idx):
        ...         image = np.random.random([IMAGE_SIZE]).astype('float32')
        ...         label = np.random.randint(0, CLASS_NUM - 1, (1, )).astype('int64')
        ...         return image, label
        ...
        ...     def __len__(self):
        ...         return self.num_samples
        ...
        >>> dataset = RandomDataset(BATCH_NUM * BATCH_SIZE)

        >>> class SimpleNet(nn.Layer):
        ...     def __init__(self):
        ...         super().__init__()
        ...         self.fc = nn.Linear(IMAGE_SIZE, CLASS_NUM)
        ...
        ...     def forward(self, image, label=None):
        ...         return self.fc(image)
        ...
        >>> simple_net = SimpleNet()
        >>> opt = paddle.optimizer.SGD(learning_rate=1e-3,
        ...                             parameters=simple_net.parameters())
        ...
        >>> loader = DataLoader(dataset,
        ...                     batch_size=BATCH_SIZE,
        ...                     shuffle=True,
        ...                     drop_last=True,
        ...                     num_workers=2)
        ...
        >>> for e in range(EPOCH_NUM):
        ...     for i, (image, label) in enumerate(loader()):
        ...         out = simple_net(image)
        ...         loss = F.cross_entropy(out, label)
        ...         avg_loss = paddle.mean(loss)
        ...         avg_loss.backward()
        ...         opt.minimize(avg_loss)
        ...         simple_net.clear_gradients()
        ...         print("Epoch {} batch {}: loss = {}".format(e, i, np.mean(loss.numpy())))

Notes:
    For reading iterable dataset with multiprocess Dataloader,
    please see :code:`paddle.io.IterableDataset`
rG   return_list_CollateFn | None
collate_fnuse_buffer_readerintreader_buffer_sizeprefetch_factorCallable[[int], None] | Noneworker_init_fnDataset[Any]r   Sequence[Tensor] | None	feed_listzlist[_Place]rP   rc   r   dataset_kinduse_shared_memorytimeoutz.BatchSampler | _InfiniteIterableSampler | Noner   r   auto_collate_batchNc           	        X@l         Xl        Xl        Xl        Xl        UU l        Xl        U(       d  [        5       (       d
  Uc   S5       eX l        Uc
  [        5       n[        U[        [        45      (       a  [        U5      nO[        U5      n[        U5      U l        U
S:  d   S5       eU
S:  a@  ["        R$                  S:X  d  ["        R$                  S:X  a  [&        R(                  " S5        Sn
Xl        US:  d   S5       eXl        U(       a  U
S:X  a  SU l        US:  d   S	5       eXl        [        U[0        5      (       a9  [2        R4                  U l        U(       a  [9        S
U 35      eUb  [9        S5      eO[2        R:                  U l        Ub)  US:X  a  U(       d  U(       a   S5       eXPl        S U l        O\Uc  S U l        S U l        OJUS:  d   S5       eX`l        [        U[0        5      (       a  [A        X5      U l        O[C        UUUUS9U l        [D        RF                  RH                  RJ                  RM                  5       (       Gas  [D        RF                  RN                  RP                  RS                  5       nUcP  [D        RF                  RU                  5       n[D        RF                  RW                  [        [Y        SU5      5      S/S9nSURZ                  ;  a  [9        S5      eUR]                  S[D        RF                  R_                  5       5      nURa                  S5      n[c        U R<                  R>                  U-  5      U l        [        U R<                  [@        5      (       a  SnSnO,U R<                  Rd                  nU R<                  Rf                  n[i        UU R>                  UUUUS9U l        Xl3        U R<                  S LU l5        SU l6        [        5       (       a  [o        5       c  SO	[o        5       U l6        UU l8        S U l9        [u        U 5      Rw                  5       U l        g )Nz.feed_list should be set when return_list=Falser   z*num_workers should be a non-negative valueru   rv   zDataLoader with multi-process mode is not supported on MacOs and Windows currently. Please use single-process mode with num_workers = 0 insteadz*prefetch_factor should be a positive valueFz&timeout should be a non-negative valuez5IterableDataset not support shuffle, but got shuffle=z0IterableDataset expect unspecified batch_samplerr   zJbatch_size/shuffle/drop_last should not be set when batch_sampler is givenzMbatch_size should be None or a positive value when batch_sampler is not given)r   r   r   r   dp)	dim_namesz;Auto-DP mode requires the mesh to include a 'dp' dimension.r   T)<r   r   r   r   r   r   r   r   r   r   rF   rK   rL   r   r   rT   rP   rw   rx   warningswarnrc   r   r   r   r   ITERr   
ValueErrorMAPr   r   r   r   r   distributedauto_parallelauto_dp_utilsin_auto_dp_modefleetautoget_meshget_world_sizeProcessMeshr~   r   get_rank_by_dim_and_process_idget_rankget_dim_sizer   r   r   r   r   
pin_memoryrI   _persistent_workers	_iteratorrV   r-   )r*   r   r   rP   r   r   r   r   r   r   rc   r   r   r   r   r   r   persistent_workersmesh	word_sizedp_rankdp_world_sizes                         r,   r]   DataLoader.__init__  s   ( '$!2"4.,?#4#4( @( #>,.FftUm,,+F3F&v.F%f-aM!MM?LLH$(?MMO K&"P$PP"!2!1%*D"!|EEE|g// , 1 1D KG9U  ( F  )
 !- 0 0D$?79 )D "/"DO!%D"DO> -> )O'?33%=&" &2#)#'	&" ++99IIKK%%++0099;D|"..==?	))55q),-$ 6  4>>) Q  99f((113G !--d3M!$"4"4"?"?-"OPDO$,,.FGG!	,,44 ..88	!8??*#"D #"&"4"4D"@)+39J9L O $6 #D>224r/   c                    U R                   [        R                  :X  a  [        S5      eU R                  (       a  [        U R                  5      $ [        U R                  5      $ )Nz'length of IterableDataset not supported)r   r   r   r   r   rD   r   r   ry   s    r,   __len__DataLoader.__len__b  sK     1 11FGG&&4--..4<<((r/   c                   U R                   S:X  a  [        U 5      $ U R                  (       aO  U R                  c  [	        U 5      U l        U R                  $ U R                  R                  5         U R                  $ [	        U 5      $ )Nr   )rc   r   r   r   r   _resetry   s    r,   __iter__DataLoader.__iter__k  sm    q /55%%~~%!<T!B >>! %%'>>!.t44r/   c                "    U R                  5       $ r'   )r   ry   s    r,   r-   DataLoader.__call__w  s    }}r/   )r   r   r   r   r   r   r   r   r   r   rc   r   rP   r   r   r   r   r   r   r   )NNTNr   FFNr   Tr   r   Tr   NF)$r   r   r   r   rP   z&PlaceLike | Sequence[PlaceLike] | Noner   rG   r   zBatchSampler | Noner   r   r   rG   r   rG   r   r   rc   r   r   rG   r   r   r   r   r   rG   r   r   r   r   r   rG   r5   None)r5   r   )r5   r   )r6   r7   r8   r9   __doc____annotations__r]   r   r   r-   r:   r(   r/   r,   r   r   	  su   l\ !!00&&LAAO
 .29= -1(,"&"# "&7;#(%W5W5 +W5 7	W5
 W5 +W5 W5 W5 W5 &W5 W5  W5  W5 W5  W5  !W5" 5#W5$ !%W5& 
'W5r)
5r/   r   )r<   )>
__future__r   r   rf   rZ   rw   re   r   typingr   r   r   r   r   r	   r
   r   base.frameworkr   r   r   	frameworkr   r   
dataloaderr   r   r   dataloader.batch_samplerr   r   dataloader.dataloader_iterr   r   r   numberscollections.abcr   r   numpy.typingnptr   paddle._typingr   paddle._typing.device_liker   $paddle.io.dataloader.dataloader_iterr   paddle.io.dataloader.datasetr    r!   r"   r$   QUEUE_GET_TIMEOUTrE   r>   r?   rB   rI   rT   rV   r   r(   r/   r,   <module>r      s    #    
      
 . = =  1(1H4	B	BOX O.    $F FRo or/   