o
    پim+                     @   s2   d Z ddlZddlm  mZ G dd deZdS )z+PyTorch hooks for layerwise NVTX profiling.    Nc                       sP   e Zd ZdZ fddZedddZdd Zd	d
 Zdd Z	dddZ
  ZS )PytHooksa@  This module contains all the code needed to enable forward hooks in a pytorch network.

    To register the hooks for a given network, the user needs to instantiate a PytHook object.
    Then call the register_hooks method.

    Example:

        my_hook = PytHook()
        my_hook.register_hooks(my_network_model)
    c                    s   t    i | _dS )zkInitialize module variables

        Returns:
            None:

        Raises:
            None:
        N)super__init__module_to_name_map)self	__class__ W/home/ubuntu/.local/lib/python3.10/site-packages/sglang/srt/utils/nvtx_pytorch_hooks.pyr       s   
	
zPytHooks.__init__Nc                 C   sb   |du rg }t | tst | tr| D ]	}t|||}q|S t | tjr/t|  }|| |S )a  Descends iterators that contains Tensors and prints the Tensor

        Recursive function that descends iterator type arguments until
        it finds a Tensor object.

        Args:
            tensor_obj: Could be a Tensor or an iterator type that contains Tensors
            prefix: String name to assign to the Tensor
            tensor_list: List to accumulate tensor dimensions

        Returns:
            List of tensor dimensions

        Raises:
            None:
        N)	
isinstancelisttupler   print_tensortorchTensorsizeappend)
tensor_objprefixtensor_listtentensor_dimsr	   r	   r
   r   ,   s   
zPytHooks.print_tensorc                 C   s  i }t |tjjst |tjjst |tjjrOi }|j|d< |j|d< |j|d< |j	|d< |j
|d< |j|d< |j|d< |j|d< |j|d	< |j|d
< |}|S t |tjjsdt |tjjsdt |tjjri }|j|d< |j|d< |j|d< |j	|d< |j
|d< |j|d< |j|d< |j|d< |j|d	< |j|d
< |}|S t |tjjst |tjjst |tjjrdd }i }||j|d< ||j	|d< ||j
|d< ||j|d< |}|S t |tjjst |tjjst |tjjri }|j|jg|d< |j	|j	g|d< |j
|j
g|d< |j|d< |j|d< |}|S t |tjjs-t |tjjs-t |tjjr;i }|j|jg|d< |}|S t |tjjrO|j |d< |j!|d< |S t |tjj"sgt |tjj#sgt |tjj$rx|j%|d< |j&|d< |j'|d< |S t |tjj(r|j)|d< |S t |tjj*r|j+|d< |j)|d< |S t |tjj,r|j-|d< |j.|d< |S t |tjj/tjj0tjj1fr|j2|d< |S )a  Extract the static parameters from LLM and VLM relevant layer types

        Args:
            module_obj(class): Module state data structure.

        Returns:
            param_info(dict): Parameter meta_data for the given op.

        Raises:
            None

        in_chanout_chan
filter_dimstridepaddingdilation
transposedoutput_paddinggroupspadding_modec                 S   s(   t | tr	t| S t | tr| | gS d S N)r   r   r   int)	parameterr	   r	   r
   _handle_int_or_tuple   s
   

z;PytHooks.process_layer_params.<locals>._handle_int_or_tuple	ceil_modecount_include_padoutput_sizein_featuresout_featuresnum_featuresepsilonmomentumin_placepnum_embeddingsembedding_dimscale_factor)3r   r   nnConv1dConv2dConv3din_channelsout_channelskernel_sizer   r   r   r   r   r    r!   ConvTranspose1dConvTranspose2dConvTranspose3d	MaxPool1d	MaxPool2d	MaxPool3d	AvgPool1d	AvgPool2d	AvgPool3dr&   r'   AdaptiveAvgPool1dAdaptiveAvgPool2dAdaptiveAvgPool3dr(   Linearr)   r*   BatchNorm1dBatchNorm2dBatchNorm3dr+   epsr-   ReLUinplaceDropoutr/   	Embeddingr0   r1   UpsampleUpsamplingNearest2dUpsamplingBilinear2dr2   )r   
module_obj
param_infoconv_paramsconvtranspose_paramsr%   pooling_paramsr	   r	   r
   process_layer_paramsI   s   









[









J8

)










zPytHooks.process_layer_paramsc                 C   s   t   dS )a  Callback function that ends the NVTX marker

        Records the module name and tensor information
        Called after the module executes the forward method.

        Args:
            module_obj: Pointer to the module object
            in_tensor: Input tensor or list of tensors
            out_tensor: Output tensor of the resulting forward operator

        Returns:
            None:

        Raises:
            None:
        N)nvtx	range_pop)r   rR   	in_tensor
out_tensorr	   r	   r
   module_fwd_hook   s   zPytHooks.module_fwd_hookc                 C   s   i }| j |d}||d< |jdd}t|D ]\}\}}|dkr%i |d< t| |d |< qt|d}	|	r<|	|d< | |}
|
rG|
|d	< t	
d
| dS )aT  Creates an NVTX marker with the module name in it.

        This function is called before the module executes

        Args:
            module_obj: Module object data structure - used to get unique module name
            in_tensor: Input tensor data structure

        Returns:
            None

        Raises:
            None
        unknownModuleF)recurser   TrainableParamsInputInputsStaticParamsz{}N)r   getnamed_parameters	enumerater   r   r   r   rW   rX   
range_pushformat)r   rR   rZ   marker_dictmodule_namemodule_paramsidx
param_name	param_objin_tensor_listrS   r	   r	   r
   module_fwd_pre_hook   s    
zPytHooks.module_fwd_pre_hooktopc                 C   s   t jjt jjt jjt jjt jjf}|j|dD ]'\}}t||r!q|	| j
 || j || jvr8|| j|< qtd|dS )a  User level function that activates all the hooks

        The user needs to call this method from the network source code
        The code descends all the modules in the network and registers their
        respective hooks.

        Args:
            network_model: Model object for the network
            module_prefix: (default: top)

        Returns:
            None

        Raises:
            Exception if a module instance is reused
        )r   z!Module instance {} is not unique N)r   r3   IdentityrM   	Dropout1d	Dropout2d	Dropout3dnamed_modulesr   register_forward_pre_hookrp   register_forward_hookr\   r   
ValueErrorrh   )r   network_modelmodule_prefix
skip_typesnamemoduler	   r	   r
   register_hooks   s   

zPytHooks.register_hooksr"   )rq   )__name__
__module____qualname____doc__r   staticmethodr   rW   r\   rp   r   __classcell__r	   r	   r   r
   r      s    |&r   )r   r   torch.cuda.nvtxcudarX   objectr   r	   r	   r	   r
   <module>   s   