o
    }oi	                     @   s0   d dl Z d dlmZmZmZ G dd deZdS )    N)CallbackLightningModuleTrainerc                       sp   e Zd ZdZ		ddededededdf
 fd	d
ZdedededdfddZ	dedededdfddZ
  ZS )NsightProfilinga  
    A simple Callback enabling Nsight Systems (nsys) profiling without a dependency on Megatron Core
    (unlike nemo.lightning.pytorch.callbacks.nsys.NsysCallback).

    It can be enabled from YAML configuration as follows:

    .. code-block:: yaml

        trainer:
          ...
          callbacks:
            - _target_: nemo.collections.speechlm2.parts.nsight.NsightProfiling
              start_step: 5
              end_step: 10
              gen_shape: true
              nvtx_ranges: true
    F
begin_stepend_step	gen_shapenvtx_rangesreturnNc                    s&   t    || _|| _|| _|| _d S )N)super__init__r   r   r   r	   )selfr   r   r   r	   	__class__ [/home/ubuntu/.local/lib/python3.10/site-packages/nemo/collections/speechlm2/parts/nsight.pyr   %   s
   

zNsightProfiling.__init__trainer	pl_module	batch_idxc                 C   sb   || j kr-td tjj   | jr/| jr#tj	jj
dd  d S tj	j
   d S d S d S )NzSTARTING NSIGHT PROFILINGT)record_shapes)r   printtorchcudaprofilercudartcudaProfilerStartr	   r   autograd	emit_nvtx	__enter__)r   r   r   batchr   r   r   r   on_train_batch_start2   s   
z$NsightProfiling.on_train_batch_startc                 C   sJ   || j kr!td tjj   | jr#tjj	 
d d d  d S d S d S )NzSTOPPING NSIGHT PROFILING)r   r   r   r   r   r   cudaProfilerStopr	   r   r   __exit__)r   r   r   outputsr   r   r   r   r   on_train_batch_end<   s   
z"NsightProfiling.on_train_batch_end)FF)__name__
__module____qualname____doc__intboolr   r   r   r    r$   __classcell__r   r   r   r   r      s"    "
r   )r   	lightningr   r   r   r   r   r   r   r   <module>   s   