o
    GÿÆi  ã                   @   sh   d dl mZ d dl mZ d dlZd dlZd dlZd dlmZ d dlmZ ddlm	Z	 G dd	„ d	e
ƒZdS )
é    )ÚList)ÚTupleN)ÚFsa)ÚDecodeStateInfoé   )ÚDenseFsaVecc                   @   sX   e Zd Zdededededededdfd	d
„Zdedee	 de
eee	 f fdd„ZdS )ÚOnlineDenseIntersecterÚdecoding_graphÚnum_streamsÚsearch_beamÚoutput_beamÚmin_active_statesÚmax_active_statesÚreturnNc                 C   s,   || _ |j| _t | j j|||||¡| _dS )ay  Create a new online intersecter object.
        Args:
          decoding_graph:
            The decoding graph used in this intersecter.
          num_streams:
            How many streams can this intersecter handle parallelly.
          search_beam:
            Decoding beam, e.g. 20.  Smaller is faster, larger is more exact
            (less pruning). This is the default value; it may be modified by
            ``min_active_states`` and ``max_active_states``.
          output_beam:
            Pruning beam for the output of intersection (vs. best path);
            equivalent to kaldi's lattice-beam.  E.g. 8.
          min_active_states:
            Minimum number of FSA states that are allowed to be active on any
            given frame for any given intersection/composition task. This is
            advisory, in that it will try not to have fewer than this number
            active. Set it to zero if there is no constraint.
          max_active_states:
            Maximum number of FSA states that are allowed to be active on any
            given frame for any given intersection/composition task. This is
            advisory, in that it will try not to exceed that but may not always
            succeed. You can use a very large number if no constraint is needed.
        Examples:
          .. code-block:: python
            # create a ``OnlineDenseIntersecter`` which can handle 2 streams.
            intersecter = k2.OnlineDenseIntersecter(...,num_streams=2,...)
            # decode_states stores the info of decoding states for each
            # sequences, suppose there are 3 sequences.
            decode_states = [None] * 3
            # now, we want to decode first chunk of sequence 1 and sequence 2
            # gather their history decoding states from ``decode_states``
            current_decode_states = [decode_states[1], decode_states[2]]
            lattice, new_decode_states = intersecter.decode(
                dense_fsas_12,
                current_decode_states
            )
            # we can get the partial results from ``lattice`` here.
            # update the decoding states of sequence 1 and sequence 2
            decode_states[1] = new_decode_states[0]
            decode_states[2] = new_decode_states[1]
            # then, we want to decode first chunk of sequence 0 and second chunk
            # of sequence 1.
            # gather their history decoding states from ``decode_states``
            current_decode_states = [decode_states[0], decode_states[1]]
            lattice, new_decode_states = intersecter.decode(
                dense_fsas_01,
                current_decode_states
            )
            # we can get the partial results from ``lattice`` here.
            # update the decoding states of sequence 1 and sequence 2
            decode_states[0] = new_decode_states[0]
            decode_states[1] = new_decode_states[1]
            ...
        N)r	   ÚdeviceÚ_k2r   ÚarcsÚintersecter)Úselfr	   r
   r   r   r   r   © r   úO/home/ubuntu/.local/lib/python3.10/site-packages/k2/online_dense_intersecter.pyÚ__init__   s   @
úzOnlineDenseIntersecter.__init__Ú
dense_fsasÚdecode_statesc                 C   s0   | j  |j|¡\}}}tj | j||¡}||fS )a  Does intersection/composition for current chunk of nnet_output(given
        by a DenseFsaVec), sequences in every chunk may come from different
        sources.
        Args:
          dense_fsas:
            The neural-net output, with each frame containing the log-likes of
            each modeling unit.
          decode_states:
            A list of history decoding states for current batch of sequences,
            its length equals to ``dense_fsas.dim0()`` (i.e. batch size).
            Each element in ``decode_states`` belongs to the sequence at the
            corresponding position in current batch.
            For a new sequence(i.e. has no history states), just put ``None``
            at the corresponding position.
        Return:
          Return a tuple containing an Fsa and a List of new decoding states.
          The Fsa which has 3 axes(i.e. (batch, state, arc)) contains the output
          lattices. See the example in the constructor to get more info about
          how to use the list of new decoding states.
        )r   ÚdecodeÚdense_fsa_vecÚk2ÚutilsÚfsa_from_unary_function_tensorr	   )r   r   r   Ú
ragged_arcÚarc_mapÚnew_decode_statesÚout_fsar   r   r   r   i   s   
ÿÿzOnlineDenseIntersecter.decode)Ú__name__Ú
__module__Ú__qualname__r   ÚintÚfloatr   r   r   r   r   r   r   r   r   r   r      s.    þýüûúù
øKÿÿþr   )Útypingr   r   r   Útorchr   r   r   r   r   Úobjectr   r   r   r   r   Ú<module>   s   