a
    ha	                     @   sd   d dl Z d dlZd dlZddlmZmZ g dZg dZdddZd	d
 Z	dd Z
e jdd ZdS )    N   )check_errorcudart)initstartstopprofile)ZgpustarttimestampZgpuendtimestampZ
gridsize3dZthreadblocksizeZstreamidzenableonstart 0Zconckerneltrace	key_valuec                 C   s   t  }t|dstdttjdrRtjjd urRttjjdd dkrRtd|d u r^tn|}|dkrt|j	j
}n|d	kr|j	j}ntd
tjddF}|ddd |D  |  t||j| | W d    n1 s0    Y  d S )NcudaOutputModez-HIP does not support profiler initialization!cuda.r      z.CUDA12+ does not need profiler initialization!r	   csvz;supported CUDA profiler output modes are: key_value and csvT)delete   
c                 s   s   | ]}| d V  qdS )asciiN)encode).0f r   A/var/www/auris/lib/python3.9/site-packages/torch/cuda/profiler.py	<genexpr>-       zinit.<locals>.<genexpr>)r   hasattrAssertionErrortorchversionr   intsplitDEFAULT_FLAGSr
   ZKeyValuePairZCSVRuntimeErrortempfileNamedTemporaryFilewritejoinflushr   ZcudaProfilerInitializename)Zoutput_fileflagsZoutput_modertZoutput_mode_enumr   r   r   r   r      s,    




r   c                   C   s   t t   dS )zStarts cuda profiler data collection.

    .. warning::
        Raises CudaError in case of it is unable to start the profiler.
    N)r   r   ZcudaProfilerStartr   r   r   r   r   2   s    r   c                   C   s   t t   dS )zStops cuda profiler data collection.

    .. warning::
        Raises CudaError in case of it is unable to stop the profiler.
    N)r   r   ZcudaProfilerStopr   r   r   r   r   ;   s    r   c                   c   s$   zt   dV  W t  nt  0 dS )a  
    Enable profiling.

    Context Manager to enabling profile collection by the active profiling tool from CUDA backend.
    Example:
        >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_CUDA)
        >>> import torch
        >>> model = torch.nn.Linear(20, 30).cuda()
        >>> inputs = torch.randn(128, 20).cuda()
        >>> with torch.cuda.profiler.profile() as prof:
        ...     model(inputs)
    N)r   r   r   r   r   r   r   D   s    r   )Nr	   )
contextlibr!   r    r   r   __all__r   r   r   r   contextmanagerr   r   r   r   r   <module>   s   
		