
    zj	^                       d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlm	Z	m
Z
mZmZmZmZmZ d dlZddlmZmZmZ ddlmZmZ ddlmZmZmZ dd	lmZmZ dd
lm Z m!Z!m"Z" e	rTd dl#Z#d dl$m%Z%m&Z& d dl'mZ( d dlm)Z) d dl*m+Z+ d dl,m-Z- d dl.m/Z/ d dl0m1Z1  ed          Z2 ed          Z3 G d de          Z4dZ5da6da7da8ddZ9d Z:d Z; G d d          Z< G d d          Z=dS )     )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                      e Zd Zedd            Zedd            Zedd            Zedd            Zedd            ZdS )
_CollateFnbatch5Sequence[npt.NDArray[Any]] | Sequence[numbers.Number]returnnpt.NDArray[Any]c                    d S N selfr%   s     `/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/paddle/io/reader.py__call__z_CollateFn.__call__C   s	      #s    Sequence[Tensor]r   c                    d S r*   r+   r,   s     r.   r/   z_CollateFn.__call__H       ?Bsr0   Sequence[AnyStr]r   c                    d S r*   r+   r,   s     r.   r/   z_CollateFn.__call__K   r3   r0   Sequence[Mapping[_K, _V]]Mapping[_K, _V]c                    d S r*   r+   r,   s     r.   r/   z_CollateFn.__call__N   s	     "cr0   Sequence[Sequence[_V]]Sequence[_V]c                    d S r*   r+   r,   s     r.   r/   z_CollateFn.__call__S   s    KN3r0   N)r%   r&   r'   r(   )r%   r1   r'   r   )r%   r4   r'   r   )r%   r6   r'   r7   )r%   r9   r'   r:   )__name__
__module____qualname__r
   r/   r+   r0   r.   r$   r$   B   s        		# 	# 	# 
	# 
BBB 
B	BBB 
B		" 	" 	" 
	" 
NNN 
NNNr0   r$   <   F  c                    | a |ad S r*   )USE_AUTOTUNETUNING_STEPS)use_autotunetuning_stepss     r.   set_autotune_configrF   a   s    LLLLr0   c                     t          |           dk    rt          S t          |           dk    rt          | d         t                    sJ | d         ad S )Nr   r   )lenUSE_PINNED_MEMORY
isinstancebool)argss    r.   use_pinned_memoryrM   h   sM    
4yyA~~  4yyA~~*T!Wd";";~~; Gr0   c                   t          | t          t          f          s| g} g }| D ][}t          |t          j                  s*t          j                    }|                    |           |}|                    |           \|S r*   )rJ   listtupler   Place	set_placeappend)placesretptmps       r.   _convert_placesrX   q   s    ftUm,, 
C  !TZ(( 	*,,CMM!A

1Jr0   c                  8    e Zd Zd Zd Zd Zd Zd Zd Zd Z	dS )	AuToTunec                J    || _         t          j                    dz  | _        d S )Nr   )loadermultiprocessing	cpu_countmax_num_worker)r-   r\   s     r.   __init__zAuToTune.__init__   s%    -799A=r0   c                   t           r|                                 s| j        j        S |                                 }|| j        j        S t          j                    }t          j        d           t          j        dt          | j        j                  z              d}t          d          }t          j        dt          | j
                  z              d}|| j
        k     r||_        |                     |          }|dz  |k    r|}|}n&|                     |||| j
                  }||k    rnI|}t          j        dt          |          z   dz   t          |          z              |d	z  }|| j
        k     t          j        d
t          |          z              t          j        dt          t          j                    |z
            z   dz              |S )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)rB   need_autotuner\   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_costre   avg_cost
update_nums           r.   r/   zAuToTune.__call__   s    	+d&8&8&:&: 	+;**  3355#;** )++@AAA*S1H-I-II	
 	
 	
 <<03t7J3K3KK	
 	
 	
 D///+6(001ABBH$))##.  !\\$$'	 
 !111'1$Mk""#  h--    1K/ D///0 	5<L8M8MM	
 	
 	
 	.$)++/001	
 	
 	
  r0   c                J    t           j        dk    st           j        dk    rdS dS )Ndarwinwin32FT)sysplatformr-   s    r.   rd   zAuToTune.need_autotune   s%    <8##s|w'>'>54r0   c                    t          |t          z  t          |                    }t          |t	          t          |                              }|S )N)indices)minrC   rH   r   rO   range)r-   dataset
batch_sizenum_samplessub_datasets        r.   get_sub_datasetzAuToTune.get_sub_dataset   sD    *|3S\\BBWd53E3E.F.FGGGr0   c                   t          j         | j                  }| j        j        j        }t	          | j        j        t
          j        j                  r| j        j        j        }| 	                    ||          }t
          j                            ||| j        j        j
        | j        j        j        | j        j        j        | j        j        j                  |_        nt	          | j        j        t
          j        j                  rc| j        j        j        j        }| 	                    ||          }t
          j                            ||| j        j        j                  |_        nd }|S )Nr   r   num_replicasrankshuffle	drop_last)r   r   r   )copyr\   batch_samplerr   rJ   paddleior   r   r   nranks
local_rankr   r   r   samplerdata_source)r-   r\   r   r   r   s        r.   rf   zAuToTune.get_autotune_loader   s<   4;''[.9
K%vy'H
 
 	 k/7G..w
CCK#)9#D#D#%![6=[.919+3= $E $ $F   1693IJJ 		k/7CG..w
CCK#)9#9#9#%+3= $: $ $F   Fr0   c                   g }d}t          j                     }t          |          D ]A\  }}|                    t          j                     |z
             t          j                     }Bt          |          dk    r0t	          |dd                    t          |dd                    z  }n/t	          |dd                    t          |dd                    z  }|S )Nr   r   )rg   	enumeraterS   rH   sum)r-   readercostsrs   startidatas          r.   rl   zAuToTune.evaluate_reader_cost   s    	 (( 	  	 GAtLLu,---IKKEEu::>>59~~E!""I6HH59~~E!""I6Hr0   c                2   d}|dz   }d}||k     r|dk     r|| j         _        |                     |          }t          j        dt          |          z   dz   t          |          z              |dz  }||dz  |z  k     r|S |dz  }|dz  }||k     r|dk     |S )Nr   r      zfor back num_workers: rc   gffffff?g?)r\   re   rl   rh   ri   rj   )	r-   r   best_workers	best_timenum_work_boundarystepre   boundaryrg   s	            r.   rm   zAuToTune.is_best   s    "Q&---$((&1DK#,,V44DM(k""#  d))   AIDi$&111""q H ---$(( r0   N)
r<   r=   r>   r`   r/   rd   r   rf   rl   rm   r+   r0   r.   rZ   rZ      s~        > > >8  8  8 t    
  8      r0   rZ   c                      e Zd ZU dZded<   ded<   ded<   ded<   ded	<   d
ed<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   ded<   	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d-d.d(Zd/d)Zd0d+Zd0d,ZdS )1
DataLoaderal"  
    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`
    rK   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]rT   re   r   dataset_kinduse_shared_memorytimeoutz.BatchSampler | _InfiniteIterableSampler | Noner   r   auto_collate_batchNTr   Fr   r   &PlaceLike | Sequence[PlaceLike] | NoneBatchSampler | Noner   r   persistent_workersr'   Nonec                p   || _         |	| _        || _        || _        || _        || _        || _        |st                      s|
J d            || _        |t                      }t          |t          t          f          rt          |          }nt          |          }t          |          | _        |
dk    s
J d            |
dk    r6t"          j        dk    st"          j        dk    rt'          j        d           d}
|
| _        |dk    s
J d            || _        |r|
dk    rd| _        |dk    s
J d	            || _        t          |t0                    r7t2          j        | _        |rt9          d
|           |t9          d          nt2          j        | _        |#|dk    r|s|r
J d            || _        d | _        nk|d | _        d | _        nZ|dk    s
J d            || _        t          |t0                    rtA          ||          | _        ntC          ||||          | _        tD          j#        j$        j%        &                                r[tD          j#        j'        j(        )                                }|[tD          j#        *                                }tD          j#        +                    t          tY          d|                    dg          }d|j-        vrt9          d          |.                    dtD          j#        /                                          }|0                    d          }tc          | j        j        |z            | _        t          | j        t@                    rd}d}n| j        j2        }| j        j3        }ti          || j        ||||          | _        || _3        | j        d u| _5        d| _6        t                      r#to                      dnto                      | _6        || _8        d | _9        tu          |           ;                                | _        d S )Nz.feed_list should be set when return_list=Falser   z*num_workers should be a non-negative valuerv   rw   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   rJ   rO   rP   r   r   rX   rT   rx   ry   warningswarnre   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_memoryrM   _persistent_workers	_iteratorrZ   r/   )r-   r   r   rT   r   r   r   r   r   r   re   r   r   r   r   r   r   r   mesh	word_sizedp_rankdp_world_sizes                         r.   r`   zDataLoader.__init__  s   ( '$!2"4., 	?#4#4 	((@ )(( #>,..FftUm,, 	/+F33FF&v..F%f--a!M??LH$$(?(?MO   K&"""$P"""!2 	+!1!1%*D"!|||E|||g// 	1 , 1D  UGUU   ( F   )
 !- 0D$??7?9??) #?D "/D"DOO!%D"DOO>>>- ">> )DO'?33 
%=Z& &"" &2#)#'	& & &" +9IIKK  	%+099;;D|".==??	)55q),,--$ 6   4>)) Q   99f(1133 G !--d33M!$"4"?-"OPPDO$,.FGG 9!		,4 .8	!8?*#" " "D #"&"4D"@ 	)++39J9L9L O $6 #D>>2244r0   c                    | j         t          j        k    rt          d          | j        rt          | j                  S t          | j                  S )Nz'length of IterableDataset not supported)r   r   r   r   r   rH   r   r   rz   s    r.   __len__zDataLoader.__len__b  sN     111FGGG& )4-...4<(((r0   r   c                    | j         dk    rt          |           S | j        r<| j        t	          |           | _        n| j                                         | j        S t	          |           S )Nr   )re   r   r   r   r   _resetrz   s    r.   __iter__zDataLoader.__iter__k  sl    q  /555% 	5~%!<T!B!B%%'''>!.t444r0   c                *    |                                  S r*   )r   rz   s    r.   r/   zDataLoader.__call__w  s    }}r0   )NNTNr   FFNr   Tr   r   Tr   NF)$r   r   r   r   rT   r   r   rK   r   r   r   r   r   rK   r   rK   r   r   re   r   r   rK   r   r   r   r   r   rK   r   r   r   r   r   rK   r'   r   )r'   r   )r'   r   )	r<   r=   r>   __doc____annotations__r`   r   r   r/   r+   r0   r.   r   r   	  sl        l l\ !!!!0000&&&&LLLAAAAOOO
 .29= -1(,"&"# "&7;#(%W5 W5 W5 W5 W5r) ) ) )
5 
5 
5 
5     r0   r   )r@   )>
__future__r   r   rh   r]   rx   rg   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_TIMEOUTrI   rB   rC   rF   rM   rX   rZ   r   r+   r0   r.   <module>r      s   # " " " " "       



                             
 . - - - - - - - = = = = = = = = = =                  !ONNN11111111((((((111111HHHHHH444444	B	BO O O O OX O O O.          $ $ $  F F F F F F F FRo o o o o o o o o or0   