o
    QeV                     @   s   d dl mZ d dlmZ d dlZd dlmZ d dlmZ d dl	m
Z
mZ dZdaejejejejejejejgZG dd	 d	eZd
efddZdd Zdd ZdS )    )Any)warnN)ContextDecorator)core)_RecordEventTracerEventTypeFc                   @   sV   e Zd ZdZejfdedefddZdd Zde	d	e	d
e	fddZ
dd Zdd ZdS )RecordEventa  
    Interface for recording a time range by user defined.

    Args:
        name (str): Name of the record event.
        event_type (TracerEventType, optional): Optional, default value is
            `TracerEventType.PythonUserDefined`. It is reserved for internal
            purpose, and it is better not to specify this parameter.

    Examples:
        .. code-block:: python
            :name: code-example1

            import paddle
            import paddle.profiler as profiler
            # method1: using context manager
            with profiler.RecordEvent("record_add"):
                data1 = paddle.randn(shape=[3])
                data2 = paddle.randn(shape=[3])
                result = data1 + data2
            # method2: call begin() and end()
            record_event = profiler.RecordEvent("record_add")
            record_event.begin()
            data1 = paddle.randn(shape=[3])
            data2 = paddle.randn(shape=[3])
            result = data1 + data2
            record_event.end()

    **Note**:
        RecordEvent will take effect only when :ref:`Profiler <api_paddle_profiler_Profiler>` is on and at the state of `RECORD`.
    name
event_typec                 C   s   || _ || _d | _d S N)r	   r
   event)selfr	   r
    r   ED:\Projects\ConvertPro\env\Lib\site-packages\paddle/profiler/utils.py__init__F   s   
zRecordEvent.__init__c                 C   s   |    | S r   )beginr   r   r   r   	__enter__O   s   zRecordEvent.__enter__exc_type	exc_value	tracebackc                 C   s   |    d S r   )end)r   r   r   r   r   r   r   __exit__S   s   zRecordEvent.__exit__c                 C   s>   t sdS | jtvrtdjt  d| _dS t| j| j| _dS )a  
        Record the time of beginning.

        Examples:

            .. code-block:: python
                :name: code-example2

                import paddle
                import paddle.profiler as profiler
                record_event = profiler.RecordEvent("record_sub")
                record_event.begin()
                data1 = paddle.randn(shape=[3])
                data2 = paddle.randn(shape=[3])
                result = data1 - data2
                record_event.end()
        NzVOnly TracerEvent Type in [{}, {}, {}, {}, {}, {},{}]                  can be recorded.)_is_profiler_usedr
   _AllowedEventTypeListr   formatr   r   r	   r   r   r   r   r   V   s   

zRecordEvent.beginc                 C   s   | j r
| j   dS dS )a  
        Record the time of ending.

        Examples:

            .. code-block:: python
                :name: code-example3

                import paddle
                import paddle.profiler as profiler
                record_event = profiler.RecordEvent("record_mul")
                record_event.begin()
                data1 = paddle.randn(shape=[3])
                data2 = paddle.randn(shape=[3])
                result = data1 * data2
                record_event.end()
        N)r   r   r   r   r   r   r   u   s   zRecordEvent.endN)__name__
__module____qualname____doc__r   PythonUserDefinedstrr   r   r   r   r   r   r   r   r   r   r   %   s    #
	r   filenamec                 C   s
   t | S )a  
    Load dumped profiler data back to memory.

    Args:
        filename(str): Name of the exported protobuf file of profiler data.

    Returns:
        ``ProfilerResult`` object, which stores profiling data.

    Examples:
        .. code-block:: python

            # required: gpu
            import paddle.profiler as profiler
            with profiler.Profiler(
                    targets=[profiler.ProfilerTarget.CPU, profiler.ProfilerTarget.GPU],
                    scheduler = (3, 10)) as p:
                for iter in range(10):
                    #train()
                    p.step()
            p.export('test_export_protobuf.pb', format='pb')
            profiler_result = profiler.load_profiler_result('test_export_protobuf.pb')
    )r   load_profiler_result)r"   r   r   r   r#      s   
r#   c                   C   s   t dkS )NT)r   r   r   r   r   in_profiler_mode   s   r$   c                  C   sb   dd } t dkr
d S dd lm} |jD ]}|dkr,t||}t|dd d kr,| |j|_qda d S )Nc                    s   t   fdd}|S )Nc                     sT   t  r#tdtjd  | i |W  d    S 1 sw   Y  d S  | i |S )NzOptimization Step)r
   )r$   r   r   Optimization)argskwargsfuncr   r   warpper   s   $z;wrap_optimizers.<locals>.optimizer_warpper.<locals>.warpper)	functoolswraps)r)   r*   r   r(   r   optimizer_warpper   s   	z*wrap_optimizers.<locals>.optimizer_warpperTr   Z	Optimizerstep)_has_optimizer_wrappedZpaddle.optimizer	optimizer__all__getattrr.   )r-   r0   	classnameZclassobjectr   r   r   wrap_optimizers   s   

r4   )typingr   warningsr   r+   
contextlibr   Zpaddle.fluidr   Zpaddle.fluid.corer   r   r   r/   Z
DataloaderZProfileStepForwardZBackwardr%   ZPythonOpr    r   r   r!   r#   r$   r4   r   r   r   r   <module>   s(   f