o
    %ݫi                     @   sT   d Z ddlmZ ddlmZ ddlZddlZeG dd dZeG dd dZ	dS )	ap  Configuration and utility classes for classes for Dynamic Chunk Training, as
often used for the training of streaming-capable models in speech recognition.

The definition of Dynamic Chunk Training is based on that of the following
paper, though a lot of the literature refers to the same definition:
https://arxiv.org/abs/2012.05481

Authors
* Sylvain de Langen 2023
    )	dataclass)OptionalNc                   @   sN   e Zd ZU dZeed< 	 dZee ed< 	 defddZ	dee fdd	Z
dS )
DynChunkTrainConfigzDynamic Chunk Training configuration object for use with transformers,
    often in ASR for streaming.

    This object may be used both to configure masking at training time and for
    run-time configuration of DynChunkTrain-ready models.
    
chunk_sizeNleft_context_sizereturnc                 C   s
   | j du S )zlReturns true if the left context is infinite (i.e. any chunk can
        attend to any past frame).
        N)r   self r
   \/home/ubuntu/.local/lib/python3.10/site-packages/speechbrain/utils/dynamic_chunk_training.pyis_infinite_left_context+   s   
z,DynChunkTrainConfig.is_infinite_left_contextc                 C   s   | j du rdS | j| j  S )zReturns the number of left context *frames* (not chunks).
        If ``None``, the left context is infinite.
        See also the ``left_context_size`` field.
        N)r   r   r   r
   r
   r   left_context_size_frames1   s   
z,DynChunkTrainConfig.left_context_size_frames)__name__
__module____qualname____doc__int__annotations__r   r   boolr   r   r
   r
   r
   r   r      s   
 r   c                   @   s   e Zd ZU dZeed< 	 eed< 	 eed< 	 eed< 	 eed< 	 eed< 	 dZee	 ed	< 	 dZ
ee	 ed
< 	 dd Zdd ZdS ) DynChunkTrainConfigRandomSamplera\  Helper class to generate a DynChunkTrainConfig at runtime depending on the current
    stage.

    Example
    -------
    >>> from speechbrain.core import Stage
    >>> from speechbrain.utils.dynamic_chunk_training import DynChunkTrainConfig
    >>> from speechbrain.utils.dynamic_chunk_training import DynChunkTrainConfigRandomSampler
    >>> # for the purpose of this example, we test a scenario with a 100%
    >>> # chance of the (24, None) scenario to occur
    >>> sampler = DynChunkTrainConfigRandomSampler(
    ...     chunkwise_prob=1.0,
    ...     chunk_size_min=24,
    ...     chunk_size_max=24,
    ...     limited_left_context_prob=0.0,
    ...     left_context_chunks_min=16,
    ...     left_context_chunks_max=16,
    ...     test_config=DynChunkTrainConfig(32, 16),
    ...     valid_config=None
    ... )
    >>> one_train_config = sampler(Stage.TRAIN)
    >>> one_train_config
    DynChunkTrainConfig(chunk_size=24, left_context_size=None)
    >>> one_train_config.is_infinite_left_context()
    True
    >>> sampler(Stage.TEST)
    DynChunkTrainConfig(chunk_size=32, left_context_size=16)
    chunkwise_probchunk_size_minchunk_size_maxlimited_left_context_probleft_context_chunks_minleft_context_chunks_maxNtest_configvalid_configc                 C   s   t d |k S )a"  Samples a random boolean with a probability, in a way that depends on
        PyTorch's RNG seed.

        Arguments
        ---------
        prob : float
            Probability (0..1) to return True (False otherwise).

        Returns
        -------
        The sampled boolean
           )torchranditem)r	   probr
   r
   r   _sample_bool~   s   z-DynChunkTrainConfigRandomSampler._sample_boolc                 C   s   |t jjjkr7| | jr5t| j| j	d d
 }| | jr.t| j| jd d
 }nd}t||S dS |t jjjkrA| jS |t jjjkrK| jS td| )aM  In training stage, samples a random DynChunkTrain configuration.
        During validation or testing, returns the relevant configuration.

        Arguments
        ---------
        stage : speechbrain.core.Stage
            Current stage of training or evaluation.
            In training mode, a random DynChunkTrainConfig will be sampled
            according to the specified probabilities and ranges.
            During evaluation, the relevant DynChunkTrainConfig attribute will
            be picked.

        Returns
        -------
        The appropriate configuration
        r   r   NzUnsupported stage found )sbcoreStageTRAINr$   r   r    randintr   r   r"   r   r   r   r   TESTr   VALIDr   AttributeError)r	   stager   left_context_chunksr
   r
   r   __call__   s2   
z)DynChunkTrainConfigRandomSampler.__call__)r   r   r   r   floatr   r   r   r   r   r   r$   r/   r
   r
   r
   r   r   <   s(   
 r   )
r   dataclassesr   typingr   r    speechbrainr%   r   r   r
   r
   r
   r   <module>   s    $