o
    }oi                     @   sB   d dl Z d dlmZ d dlmZ d dlmZ G dd de jZdS )    N)Optional)loggingc                   @   sh   e Zd ZdZeddejdee fddZ	eddejdee fddZ
ejd	d
 Zejdd ZdS )WithOptionalCudaGraphszo
    Abstract interface for modules with CUDA graphs.
    Allows to enable/disable CUDA graphs on the fly.
    Nmoduleattribute_pathc              	   C      |r| dng }| D ]7\}}|}z|D ]}t||}qW n	 ty(   Y qw t|| rD|  tdt| d	|g|   qdS )av  
        Disable CUDA graphs Enable CUDA graphs, finding submodule recursively.

        Args:
            module: instance of nn.Module
            attribute_path: field containing instance of WithOptionalCudaGraphs
                   E.g., "decoding.decoding" means that "<module>.decoding.decoding" are checked.
                   If None, "<module>" is checked.
        .z Disabled CUDA graphs for module N)
splitnamed_modulesgetattrAttributeError
isinstancedisable_cuda_graphsr   infotypejoinclsr   r   
attributesname	submoduleobject_to_check	attribute r   f/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/common/parts/optional_cuda_graphs.pydisable_cuda_graphs_recursive      
$z4WithOptionalCudaGraphs.disable_cuda_graphs_recursivec              	   C   r   )aa  
        Enable CUDA graphs, finding submodule recursively

        Args:
            module: instance of nn.Module
            attribute_path: field containing instance of WithOptionalCudaGraphs
                   E.g., "decoding.decoding" means that "<module>.decoding.decoding" are checked.
                   If None, "<module>" is checked.
        r   zEnabled CUDA graphs for module N)
r	   r
   r   r   r   maybe_enable_cuda_graphsr   r   r   r   r   r   r   r   enable_cuda_graphs_recursive7   r   z3WithOptionalCudaGraphs.enable_cuda_graphs_recursivec                 C      t )z%Disable (maybe temporary) CUDA graphsNotImplementedErrorselfr   r   r   r   Q      z*WithOptionalCudaGraphs.disable_cuda_graphsc                 C   r   )z(Enable CUDA graphs if all conditions metr    r"   r   r   r   r   V   r$   z/WithOptionalCudaGraphs.maybe_enable_cuda_graphs)N)__name__
__module____qualname____doc__classmethodnnModuler   strr   r   abcabstractmethodr   r   r   r   r   r   r      s    
r   )	r-   typingr   torch.nnr*   
nemo.utilsr   ABCr   r   r   r   r   <module>   s
   