o
    }oiZ                     @   s|   d dl mZmZ d dlZd dlmZmZmZ d dlm	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 )    )OptionalTupleN)add_self_loopscompose_with_self_loopsintersect_with_self_loops)k2c                   @   sh   e Zd ZdZddedfdedededed	ejf
d
dZ	d	ejfddZ
dejdejddfddZdS )CtcTopologyCompilerzDefault graph compiler.
    It applies its topology to the input token sequence to compile the supervision graph.
    
    Based on https://github.com/k2-fsa/snowfall/blob/master/snowfall/training/ctc_graph.py
    defaultTcpunum_classesblank	topo_typetopo_with_self_loopsdevicec                 C   sT   || _ || _ddlm} t||tt|||| j| _	t| j	
 | _d S )Nr   )
build_topo)r   r   (nemo.collections.asr.parts.k2.topologiesr   r   arc_sortlistrangeto
base_graphinvertctc_topo_inv)selfr   r   r   r   r   r    r   a/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/asr/parts/k2/graph_compilers.py__init__-   s   zCtcTopologyCompiler.__init__c                 C   s0   | j || _ | jd ur| j|| _|| _d S N)r   r   r   r   r   r   r   r   r   r   >   s   

zCtcTopologyCompiler.totargetstarget_lengthsreturnk2.Fsac                 C   s^   dd t ||D }t|| j}|j |_t| j	|}t
|| j}|d |S )Nc                 S   s    g | ]\}}|d |   qS r   )tolist).0tlr   r   r   
<listcomp>E   s     z/CtcTopologyCompiler.compile.<locals>.<listcomp>F)zipr   
linear_fsar   r   labelsclone
aux_labelsr   r   r   requires_grad_)r   r   r    token_ids_listlabel_graphsupervision_graphsr   r   r   compileD   s   
zCtcTopologyCompiler.compileN)__name__
__module____qualname____doc__torchr   intstrboolr   r   Tensorr1   r   r   r   r   r   &   s$    

r   c                       sz   e Zd ZdZddeddfdededed	ed
ejde	d f fddZ
	ddejdejde	d ddf fddZ  ZS )CtcNumGraphCompilerzGraph compiler with auxiliary graph to compose with the topology.
    The supervision graph contains the auxiliary graph information.
    r	   Tr
   Nr   r   r   r   r   	aux_graphr"   c                    sh   t  ||||| |d u rt| j g| j| _d S t	| j|
 | _t| j| j| _d S r   )superr   r   create_fsa_vecr   r   r   r   decoding_graphr   invert_r   r   r   r   r   r   r   r   r<   	__class__r   r   r   U   s
   	 zCtcNumGraphCompiler.__init__r   r    r!   c                    sf   |d u r| j d u rtd| d| j  |d ur,t| j| | _ t| j | j| _ t	 
||S )Nz;At least one of aux_graph and self.base_graph must be set: , )r   
ValueErrorr   r   r   r   r   r   r   r=   r1   )r   r   r    r<   rB   r   r   r1   e   s   zCtcNumGraphCompiler.compiler   )r2   r3   r4   r5   r6   r   r7   r8   r9   r   r   r:   r1   __classcell__r   r   rB   r   r;   P   s:    r;   c                       s   e Zd ZdZddeddfdededed	ed
ejde	d f fddZ
d
ejf fddZ	ddejdejde	d ded f fddZ  ZS )MmiGraphCompilerzGraph compiler for MMI loss.
    The decoding graph is a composition of the auxiliary graph and the topology.
    It is returned along with the supervision graph on every compile() call.
    r	   Tr
   Nr   r   r   r   r   r<   r"   c                    s^   t  |||||| |d u rt| j g| j| _d S t| j	
 g| j| _d S r   )r=   r   r   r>   r   r   r   r   r?   r   detachrA   rB   r   r   r   x   s   	  zMmiGraphCompiler.__init__c                    s(   | j d ur| j || _ t | d S r   )r?   r   r=   r   rB   r   r   r      s   
zMmiGraphCompiler.tor   r    r!   )r"   r"   c                    sf   t  |||}|d u r| jd u rtd| d| j |d ur.t| j g| j	| _|| jfS )Nz?At least one of aux_graph and self.decoding_graph must be set: rD   )
r=   r1   r?   rE   r   r>   r   rH   r   r   )r   r   r    r<   r0   rB   r   r   r1      s   
zMmiGraphCompiler.compiler   )r2   r3   r4   r5   r6   r   r7   r8   r9   r   r   r   r:   r   r1   rF   r   r   rB   r   rG   r   s<    	rG   c                       sj   e Zd ZdZddeddfdededed	ed
ejdef fddZ	dej
dej
ddf fddZ  ZS )RnntTopologyCompilera  Default graph compiler for RNNT loss.
    Each supervision graph is composed with the corresponding RNNT emission adapter.

    If max_adapter_length is provided, the maximum adapter length is limited.

    Note:
      The actual number of classes is `num_classes` + 1 with <eps> as the class 0.

    Warning:
      It is currently not recommended to use topologies other than "minimal".
    minimalTr
   r   r   r   r   r   r   max_adapter_lengthc                    sP   |dkrt dt ||||| ddlm} || _|tt|||| _d S )Ncompactz4This compiler does not support topo_type==`compact`.r   )RnntEmissionAdapterBuilder)	NotImplementedErrorr=   r   r   rM   rK   r   r   _builder)r   r   r   r   r   r   rK   rM   rB   r   r   r      s   	zRnntTopologyCompiler.__init__r   r    r!   r"   c                    sl   t t ||| jjd}| | jdkr&| j| k r&t|| jk| j|n|j	| j
d}tj||ddS )Ninputr   )r   F)treat_epsilons_specially)r   r=   r1   rO   eps_numrK   maxr6   wherer   r   r   	intersect)r   r   r    r0   adaptersrB   r   r   r1      s   zRnntTopologyCompiler.compile)r2   r3   r4   r5   r6   r   r7   r8   r9   r   r:   r1   rF   r   r   rB   r   rI      s(    &rI   )typingr   r   r6   #nemo.collections.asr.parts.k2.utilsr   r   r   nemo.core.utils.k2_guardr   objectr   r;   rG   rI   r   r   r   r   <module>   s   *"'