o
    ॵiJ                     @   sL   d dl ZG dd deZG dd deZG dd deZG dd	 d	eZdS )
    Nc                   @   $   e Zd Zdd Zdd Zdd ZdS )Samplerc                 C   s   d S N selfr   r   ^/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/preprocessors/nlp/space/sampler.py__init__      zSampler.__init__c                 C      t r   NotImplementedErrorr   r   r   r   __len__   r
   zSampler.__len__c                 C   r   r   r   r   r   r   r   __iter__   r
   zSampler.__iter__N__name__
__module____qualname__r	   r   r   r   r   r   r   r      s    r   c                   @   r   )SequentialSamplerc                 C   s
   || _ d S r   )datasetr   r   r   r   r   r	      s   zSequentialSampler.__init__c                 C   
   t | jS r   lenr   r   r   r   r   r         
zSequentialSampler.__len__c                 C   s   t tt| S r   )iterranger   r   r   r   r   r      s   zSequentialSampler.__iter__Nr   r   r   r   r   r      s    r   c                   @   r   )RandomSamplerc                 C   s   || _ d| _d S )Nr   )r   epochr   r   r   r   r	   !   s   zRandomSampler.__init__c                 C   r   r   r   r   r   r   r   r   &   r   zRandomSampler.__len__c                 C   s0   t j| j |  jd7  _tt jt| S )N   )nprandomseedr   r   permutationr   r   r   r   r   r   )   s   zRandomSampler.__iter__Nr   r   r   r   r   r      s    r   c                   @   s*   e Zd ZdZd
ddZdd Zdd Zd	S )SortedSamplerz= Sorted Sampler.
    Sort each block of examples by key.
    srcc                    s    |_ |_ fdd_d S )Nc                    s   t jj|    S r   )r   samplerr   )idxkeyr   r   r   <lambda>7   s    z(SortedSampler.__init__.<locals>.<lambda>)r&   sort_pool_sizer)   )r   r&   r+   r)   r   r(   r   r	   4   s   zSortedSampler.__init__c                 C   r   r   )r   r&   r   r   r   r   r   :   r   zSortedSampler.__len__c                 c   s~    g }| j D ]}|| t|| jkr%t|| jd}|D ]}|V  qg }qt|dkr;t|| jd}|D ]}|V  q5d S d S )N)r)   r   )r&   appendr   r+   sortedr)   )r   poolr'   ir   r   r   r   =   s    

zSortedSampler.__iter__N)r%   )r   r   r   __doc__r	   r   r   r   r   r   r   r$   /   s
    
r$   )numpyr    objectr   r   r   r$   r   r   r   r   <module>   s
   