o
    Qe̙                     @   s  d dl Z d dlZd dlZd dlmZ d dlmZmZmZm	Z	m
Z
 d dlmZ d dlZd dlZd dlZd dlmZmZmZmZmZmZmZmZ ddlmZmZ ddlmZmZmZ d d	l mZ dd
l!m"Z" G dd deZ#G dd deZ$G dd deZ%d d dde&de&de&de&de&defddZ'de&fddZ(	d)de)de	e) defdd Z*	d)de)de	e) defd!d"Z+dee% fd#d$Z,G d%d& d&Z-d'd( Z.dS )*    N)Enum)AnyCallableIterableOptionalUnion)warn)	_Profiler_ProfilerResultProfilerOptionsTracerEventTypeenable_memory_recorderenable_input_shape_recorderdisable_memory_recorderdisable_input_shape_recorder   )RecordEventwrap_optimizers)StatisticData_build_table
SortedKeys)utils)	benchmarkc                   @   s4   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdS )SummaryViewa  
    SummaryView define the summary view of different contents.

    - **SummaryView.DeviceView** : The device summary view.

    - **SummaryView.OverView** : The overview summary view.

    - **SummaryView.ModelView** : The model summary view.

    - **SummaryView.DistributedView** : The distributed summary view.

    - **SummaryView.KernelView** : The kernel summary view.

    - **SummaryView.OperatorView** : The operator summary view.

    - **SummaryView.MemoryView** : The memory summary view.

    - **SummaryView.MemoryManipulationView** : The meomory manipulation summary view.

    - **SummaryView.UDFView** : The user defined summary view.
    r   r                        N)__name__
__module____qualname____doc__Z
DeviceViewZOverViewZ	ModelViewZDistributedViewZ
KernelViewZOperatorViewZ
MemoryViewZMemoryManipulationViewZUDFView r%   r%   HD:\Projects\ConvertPro\env\Lib\site-packages\paddle/profiler/profiler.pyr   *   s    r   c                   @       e Zd ZdZdZdZdZdZdS )ProfilerStatea  
    ProfilerState is used to present the state of :ref:`Profiler <api_paddle_profiler_Profiler>` .

    The meaning of each ProfilerState is as following

    - **ProfilerState.CLOSED** : The profiler is closed, and no profiling data will be recorded.

    - **ProfilerState.READY** : The profiler is open, but the data will not be recorded. This state is used for reducing overhead influence when profiler starts.

    - **ProfilerState.RECORD** : The profiler is open, and the data will be recorded.

    - **ProfilerState.RECORD_AND_RETURN** : The profiler is open, and this state stands for the last batch of "RECORD" state in current profiling period. The collected data will be returned in this state.
    r   r   r   r   N)r!   r"   r#   r$   CLOSEDREADYRECORDRECORD_AND_RETURNr%   r%   r%   r&   r(   K   s    r(   c                   @   r'   )ProfilerTargetaz  
    ProfilerTarget is used to specify target device for :ref:`profiling <api_paddle_profiler_Profiler>` . Only CPU, GPU and MLU are supported currently.

    The meaning of each ProfilerState is as following

    - **ProfilerTarget.CPU** : Profile events on CPU.

    - **ProfilerTarget.GPU** : Profile events on GPU.

    - **ProfilerTarget.MLU** : Profile events on MLU.
    r   r   r   r   N)r!   r"   r#   r$   CPUGPUMLUCUSTOM_DEVICEr%   r%   r%   r&   r-   _   s    r-   )repeat
skip_firstclosedreadyrecordr2   r3   returnc                    sb   dt dtf fdd} dkr#dkr#dkr#dkr#dks'J ddkr/td |S )a\  
    Return a scheduler function, which scheduler the :ref:`state <api_paddle_profiler_ProfilerState>` according to the setting.
    The state transform confirms to:

    .. code-block:: text

        (CLOSED)  (CLOSED)    (CLOSED)  (READY)    (RECORD,last RETURN)      (CLOSED)
        START -> skip_first -> closed -> ready    ->    record       ->      END
                                |                        |
                                |                        | (if has_repeated < repeat)
                                - - - - - - - - - - - -
        Note that repeat <= 0 means the cycle will continue until the profiler exits.

    Args:
        closed(int): The number of steps in state ProfilerState.CLOSED.
        ready(int):  The number of steps in state ProfilerState.READY.
        record(int): The number of steps in state ProfilerState.RECORD, and the state in last step will be set as ProfilerState.RECORD_AND_RETURN.
        repeat(int, optional): The number of cycles to repeat above state transform. Default value is 0, which means it will repeat this cycle until profiler exits.
        skip_first(int, optional): The number of first steps to drop, not participate in the state transform, and at ProfilerState.CLOSED state. Default value is 0.

    Returns:
        A scheduler function, conforms to above state transform setting. The function will takes one parameter `step_num`, and returns corresponding ProfilerState.

    Examples:
        1. profiling range [2, 5].

        Assume batch 0: closed, batch 1: ready, batch [2, 5] record.

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

                import paddle.profiler as profiler
                profiler.make_scheduler(closed=1, ready=1, record=4, repeat=1)


        2. profiling range [3,6], [9,12], [15,18].

        Assume batch 0: skiped, batch 1: closed, batch 2: ready, batch [3,6]: record, repeat.

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

                import paddle.profiler as profiler
                profiler.make_scheduler(closed=1, ready=1, record=4, skip_first=1)
    stepr7   c                    s   | dksJ | k rt jS |  }    }| | }dkr&|kr&t jS | | }| k r1t jS | kr>|  k r>t jS ||d k rGt jS t jS )Nr   r   )r(   r)   r*   r+   r,   )r8   Zperiod_stepsZhas_repeatedZmod_stepr4   r5   r6   r2   r3   r%   r&   getScheduleState   s    z(make_scheduler.<locals>.getScheduleStater   z$Invalid profiler scheduler argumentszProfiler will record data after enabling profiler immediately,           some data collected at the beginning of profiling may be 'noisy' because of overhead.)intr(   r   )r4   r5   r6   r2   r3   r:   r%   r9   r&   make_schedulerq   s   6
r<   r8   c                 C   s   t jS )zd
    A default state scheduler, keep recording from the beginning of the profiler until ending.
    )r(   r+   )r8   r%   r%   r&   _default_state_scheduler   s   r=   dir_nameworker_namec                    N   t j sz	t j dd W n ty   td w  fdd}|S )a  
    Return a callable, used for outputing tracing data to chrome tracing format file.
    The output file will be saved in directory ``dir_name``, and file name will be set as `worker_name`.
    if `worker_name` is not set, the default name is `[hostname]_[pid]`.

    Args:
        dir_name(str): Directory to save profiling data.
        worker_name(str, optional): Prefix of the file name saved, default is `[hostname]_[pid]`.

    Returns:
        A callable, which takes a Profiler object as parameter and calls its export method to save data to chrome tracing format file.

    Examples:
        The return value can be used as parameter ``on_trace_ready`` in :ref:`Profiler <api_paddle_profiler_Profiler>` .

        .. code-block:: python

            # required: gpu
            import paddle.profiler as profiler
            with profiler.Profiler(
                    targets=[profiler.ProfilerTarget.CPU, profiler.ProfilerTarget.GPU],
                    scheduler = (3, 10),
                    on_trace_ready=profiler.export_protobuf('./log')) as p:
                for iter in range(10):
                    #train()
                    p.step()
    Texist_ok;Can not create directory '{}' for saving profiling results.c                    R   sd t tt tj }d |d}| 	tj
 |d d S )Nhost_{}pid_{}z{}_time_{}.paddle_trace.json%Y_%m_%d_%H_%M_%S_%fjsonformatsocketgethostnamestrosgetpiddatetimenowstrftimeexportpathjoinZprofrP   filenamer>   r?   r%   r&   	handle_fn      

z(export_chrome_tracing.<locals>.handle_fnrM   rS   existsmakedirs	ExceptionRuntimeErrorrI   r>   r?   rX   r%   rW   r&   export_chrome_tracing      r`   c                    r@   )az  
    Return a callable, used for outputing tracing data to protobuf file.
    The output file will be saved in directory ``dir_name``, and file name will be set as ``worker_name``.
    if ``worker_name`` is not set, the default name is `[hostname]_[pid]`.

    Args:
        dir_name(str): Directory to save profiling data.
        worker_name(str, optional): Prefix of the file name saved, default is `[hostname]_[pid]`.

    Returns:
        A callable, which takes a Profiler object as parameter and calls its export method to save data to protobuf file.

    Examples:
        The return value can be used as parameter ``on_trace_ready`` in :ref:`Profiler <api_paddle_profiler_Profiler>` .

        .. code-block:: python

            # required: gpu
            import paddle.profiler as profiler
            with profiler.Profiler(
                    targets=[profiler.ProfilerTarget.CPU, profiler.ProfilerTarget.GPU],
                    scheduler = (3, 10),
                    on_trace_ready = profiler.export_protobuf('./log')) as p:
                for iter in range(10):
                    #train()
                    p.step()
    TrA   rC   c                    rD   )NrE   z{}_time_{}.paddle_trace.pbrF   ZpbrH   rU   rW   r%   r&   rX   2  rY   z"export_protobuf.<locals>.handle_fnrZ   r_   r%   rW   r&   export_protobuf
  ra   rb   c                   C   s<   t  rtjtjtjgS t  rtjtjtjgS tjtjgS )zB
    Get the current supported profiler target in the system.
    )r	   Zis_cupti_supportedr-   r.   r/   r1   Zis_cnpapi_supportedr0   r%   r%   r%   r&   _get_supported_targetsA  s   rc   c                   @   s   e Zd ZdZdddddddg ddeee  deee	ge
f edf deedef  d	ee d
ee dee dee fddZdd Zdd Zdd Zdd Zd&dee	 fddZd&ddZdd Zd'd d!Zejd"dd#dfd$d%ZdS )(Profilera	  
    Profiler context manager, user interface to manage profiling process to start, stop, export profiling data and print summary table.

    Args:
        targets (list, optional): specify target devices to profile, and all existing and supported devices will be chosen by default. Currently supported values, :ref:`ProfilerTarget.CPU <api_paddle_profiler_ProfilerTarget>` , :ref:`ProfilerTarget.GPU <api_paddle_profiler_ProfilerTarget>` and :ref:`ProfilerTarget.MLU <api_paddle_profiler_ProfilerTarget>` .
        scheduler (Callable|tuple, optional): If it is a callable object, it takes a step number as parameter and return the corresponding :ref:`ProfilerState <api_paddle_profiler_ProfilerState>`. This callable object can be generated by :ref:`make_scheduler <api_paddle_profiler_make_scheduler>` function.
            If not provided (None), the default scheduler will keep tracing until the profiler exits. If it is a tuple, it has two values start_batch and end_batch,
            which means profiling range [start_batch, end_batch).
        on_trace_ready (Callable, optional): Callable object, serves as callback function, and takes the Profiler object as parameter, which provides a way for users to do post-processing.
            This callable object will be called when ``scheduler`` returns ``ProfilerState.RECORD_AND_RETURN``. The default value is :ref:`export_chrome_tracing <api_paddle_profiler_export_chrome_tracing>`.
        timer_only (bool, optional): If it is True, the cost of Dataloader and every step of the model will be count without profiling. Otherwise, the model will
            be timed and profiled. Default: False.
        record_shapes (bool, optional): If it is True, collect op's input shape information. Default: False.
        profile_memory (bool, optional): If it is True, collect tensor memory allocation and release information. Default: False.

    Examples:
        1. profiling range [2, 5).

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

                # required: gpu
                import paddle.profiler as profiler
                with profiler.Profiler(
                        targets=[profiler.ProfilerTarget.CPU, profiler.ProfilerTarget.GPU],
                        scheduler = (2, 5),
                        on_trace_ready = profiler.export_chrome_tracing('./log')) as p:
                    for iter in range(10):
                        #train()
                        p.step()

        2. profiling range [2,4], [7, 9], [11,13].

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

                # required: gpu
                import paddle.profiler as profiler
                with profiler.Profiler(
                        targets=[profiler.ProfilerTarget.CPU, profiler.ProfilerTarget.GPU],
                        scheduler = profiler.make_scheduler(closed=1, ready=1, record=3, repeat=3),
                        on_trace_ready = profiler.export_chrome_tracing('./log')) as p:
                    for iter in range(10):
                        #train()
                        p.step()

        3. Use profiler without context manager, and use default parameters.

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

                # required: gpu
                import paddle.profiler as profiler
                p = profiler.Profiler()
                p.start()
                for iter in range(10):
                    #train()
                    p.step()
                p.stop()
                p.summary()

        4. Use profiler to get throughput and cost of the model.

            .. code-block:: python
                :name: code-example-timer1

                import paddle
                import paddle.profiler as profiler

                class RandomDataset(paddle.io.Dataset):
                    def __init__(self, num_samples):
                        self.num_samples = num_samples

                    def __getitem__(self, idx):
                        image = paddle.rand(shape=[100], dtype='float32')
                        label = paddle.randint(0, 10, shape=[1], dtype='int64')
                        return image, label

                    def __len__(self):
                        return self.num_samples

                class SimpleNet(paddle.nn.Layer):
                    def __init__(self):
                        super(SimpleNet, self).__init__()
                        self.fc = paddle.nn.Linear(100, 10)

                    def forward(self, image, label=None):
                        return self.fc(image)

                dataset = RandomDataset(20 * 4)
                simple_net = SimpleNet()
                opt = paddle.optimizer.SGD(learning_rate=1e-3, parameters=simple_net.parameters())
                BATCH_SIZE = 4
                loader = paddle.io.DataLoader(
                    dataset,
                    batch_size=BATCH_SIZE)
                p = profiler.Profiler(timer_only=True)
                p.start()
                for i, (image, label) in enumerate(loader()):
                    out = simple_net(image)
                    loss = paddle.nn.functional.cross_entropy(out, label)
                    avg_loss = paddle.mean(loss)
                    avg_loss.backward()
                    opt.minimize(avg_loss)
                    simple_net.clear_gradients()
                    p.step(num_samples=BATCH_SIZE)
                    if i % 10 == 0:
                        step_info = p.step_info(unit='images')
                        print("Iter {}: {}".format(i, step_info))
                        # The average statistics for 10 steps between the last and this call will be
                        # printed when the "step_info" is called at 10 iteration intervals.
                        # The values you get may be different from the following.
                        # Iter 0:  reader_cost: 0.51946 s batch_cost: 0.66077 s ips: 6.054 images/s
                        # Iter 10:  reader_cost: 0.00014 s batch_cost: 0.00441 s ips: 907.009 images/s
                p.stop()
                # The performance summary will be automatically printed when the "stop" is called.
                # Reader Ratio: 2.658%
                # Time Unit: s, IPS Unit: images/s
                # |                 |       avg       |       max       |       min       |
                # |   reader_cost   |     0.00011     |     0.00013     |     0.00007     |
                # |    batch_cost   |     0.00405     |     0.00434     |     0.00326     |
                # |       ips       |    1086.42904   |    1227.30604   |    959.92796    |
    NF)targets	scheduleron_trace_readyrecord_shapesprofile_memory
timer_only	emit_nvtxcustom_device_typesre   rf   rg   .rh   rj   rk   rl   c                C   s  t  }	|r!t|| _|D ]}
|
|	vr| j|
 td|
 qn|	| _t }tj| jv r4| j	dO  _	tj
| jv rA| j	dO  _	tj| jv rN| j	dO  _	tj| jv rb| j	dO  _	|sbtj }t  t||| _t|rt|| _nDt|ttfrt|dkr|d |d ksJ |\}}t|d}|dkrtt|d dd|| dd| _ntdd|| dd| _nt| _|d krtd| _n|| _d| _t j!| _"| | j| _#d | _$d | _%|| _&|| _'|| _(|| _)d S )	Nz1Profiling {} is not supported in current context.r   r   r   r    r   )r4   r5   r6   r2   z./profiler_log/)*rc   setre   remover   rI   r   r-   r.   Ztrace_switchr/   r0   r1   paddleZdeviceZget_all_custom_device_typer   r	   createprofilercallablerf   
isinstancetuplelistlenmaxr<   r=   r`   rg   step_numr(   r)   previous_statecurrent_staterecord_eventprofiler_resultrj   rh   ri   rk   )selfre   rf   rg   rh   ri   rj   rk   rl   Zsupported_targetstargetZprofileoptionZstart_batchZ	end_batchr%   r%   r&   __init__  sv   
	
 



zProfiler.__init__c                 C   s   |    | S N)startr}   r%   r%   r&   	__enter__  s   zProfiler.__enter__c                 C   s   |    d S r   )stop)r}   exc_typeexc_valexc_tbr%   r%   r&   __exit__   s   zProfiler.__exit__c                 C   s   t    | jr| jrdt_| jrdS | jrt  | jrt	  | j
tjkr+| j  n!| j
tjkr<| j  | j  n| j
tjkrL| j  | j  td| jtjd| _| j  dS )a  
        Start profiler and enter the first profiler step(0).
        State transformed from CLOSED to self.current_state and trigger corresponding action.

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

                # required: gpu
                import paddle.profiler as profiler
                prof = profiler.Profiler(
                    targets=[profiler.ProfilerTarget.CPU, profiler.ProfilerTarget.GPU],
                    scheduler = (1, 9),
                    on_trace_ready = profiler.export_chrome_tracing('./log'))
                prof.start()
                for iter in range(10):
                    #train()
                    prof.step()
                prof.stop()

        TNProfileStep#{}nameZ
event_type)r   beginrj   rk   r   _is_profiler_usedrh   r   ri   r   rz   r(   r*   rq   preparer+   r   r,   r   rI   rx   r   ProfileStepr{   r   r%   r%   r&   r   #  s,   




zProfiler.startc                 C   s   t    | jr
dS | jrt  | jrt  | jr!| j  d| _| jt	j
kr5td | j  | j  | jt	jksA| jt	jkrO| j | _| jrO| |  dt_dS )a  
        Stop profiler and State transformed from self.current_state to CLOSED.
        Trigger corresponding action and post-process profiler result using self.on_trace_ready if result exists.

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

                # required: gpu
                import paddle.profiler as profiler
                prof = profiler.Profiler(
                    targets=[profiler.ProfilerTarget.CPU, profiler.ProfilerTarget.GPU],
                    scheduler = (1, 7),
                    on_trace_ready = profiler.export_chrome_tracing('./log'))
                prof.start()
                for iter in range(10):
                    #train()
                    prof.step()
                prof.stop()
        NzbInproper Profiler state transform: READY->CLOSED, profiler will start and stop without saving dataF)r   endrj   rh   r   ri   r   r{   rz   r(   r*   r   rq   r   r   r+   r,   r|   rg   r   r   r   r%   r%   r&   r   R  s,   





zProfiler.stopnum_samplesc                 C   s~   t  | | jrdS | jr| j  d| _| j| _|  jd7  _| | j| _| 	  t
d| jtjd| _| j  dS )a  
        Signals the profiler that the next profiling step has started.
        Get the new ProfilerState and trigger corresponding action.

        Args:
            num_samples (int|None, optional): Specifies the batch size of every step of the model
                that is used to compute throughput when `timer_only` is True. Default: None.

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

                # required: gpu
                import paddle.profiler as profiler
                prof = profiler.Profiler(
                    targets=[profiler.ProfilerTarget.CPU, profiler.ProfilerTarget.GPU],
                    scheduler = (3, 7),
                    on_trace_ready = profiler.export_chrome_tracing('./log'))

                prof.start()
                for iter in range(10):
                    #train()
                    prof.step()
                prof.stop()
        Nr   r   r   )r   r8   rj   r{   r   rz   ry   rx   rf   _trigger_actionr   rI   r   r   r   )r}   r   r%   r%   r&   r8     s   

zProfiler.stepc                 C   s   |du rd}t  |S )a  
        Get statistics for current step. If the function is called at certain iteration
        intervals, the result is the average of all steps between the previous call and
        this call. Statistics are as follows:

        1. reader_cost: the cost of loading data measured in seconds.

        2. batch_cost: the cost of step measured in seconds.

        3. ips(Instance Per Second): the throughput of the model measured in `samples/s`
        or others depends on the `unit`. When `num_samples` of `step()` is None, it is
        measured in `steps/s`.

        Args:
            unit (string, optional): The unit of input data is only used When `num_samples`
                of `step()` is specified as a number. For example, when it is `images`, the unit
                of throughput is `images/s`. Default: None, the unit of throughput is `samples/s`.

        Returns:
            string: A string representing the statistic.

        Examples:
            .. code-block:: python
                :name: code-example-timer2

                import paddle.profiler as profiler
                prof = profiler.Profiler(timer_only=True)
                prof.start()
                for iter in range(20):
                    #train()
                    prof.step()
                    if iter % 10 == 0:
                        print("Iter {}: {}".format(iter, prof.step_info()))
                        # The example does not call the DataLoader, so there is no "reader_cost".
                        # Iter 0:  batch_cost: 0.00001 s ips: 86216.623 steps/s
                        # Iter 10:  batch_cost: 0.00001 s ips: 103645.034 steps/s
                prof.stop()
                # Time Unit: s, IPS Unit: steps/s
                # |                 |       avg       |       max       |       min       |
                # |    batch_cost   |     0.00000     |     0.00002     |     0.00000     |
                # |       ips       |   267846.19437  |   712030.38727  |   45134.16662   |
        NZsamples)r   	step_info)r}   unitr%   r%   r&   r     s   +zProfiler.step_infoc                 C   s  | j tjkr5| jtjkr| j  | jtjkr!| j  | j  | jtj	kr3| j  | j  d S d S | j tjkri| jtjkrOt
d | j  | j  | jtjkrZ| j  | jtj	krg| j  d S d S | j tjkr| jtjkr~t
d | j  | jtjkrt
d | j  | j  | jtj	kr	 d S d S | j tj	ksJ | jtjkr| j | _| jtjkr| j | _| j  | jtjkr| j | _| j  | j  | jtj	kr| j | _| j  | j  | jr| |  d S d S )NzRImproper schedule: READY->CLOSED, profiler will start and stop without saving dataz@Improper schedule: RECORD->CLOSED, profiler will not saving datazCImproper schedule: RECORD->READY, profiler will stop and re-prepare)ry   r(   r)   rz   r*   rq   r   r+   r   r,   r   r   r|   rg   r   r%   r%   r&   r     sl   














zProfiler._trigger_action rG   c                 C   s   | j r| j || dS dS )aP  
        Exports the tracing data to file.

        Args:
            path(str): file path of the output.
            format(str, optional): output format, can be chosen from ['json', 'pb'], 'json' for chrome tracing and 'pb' for protobuf, default value is 'json'.


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

                # required: gpu
                import paddle.profiler as profiler
                prof = profiler.Profiler(
                    targets=[profiler.ProfilerTarget.CPU, profiler.ProfilerTarget.GPU],
                    scheduler = (3, 7))
                prof.start()
                for iter in range(10):
                    #train()
                    prof.step()
                prof.stop()
                prof.export(path="./profiler_data.json", format="json")
        N)r|   save)r}   rS   rI   r%   r%   r&   rR   "  s   zProfiler.exportTmsc              	   C   sL   t |tr|g}| jr$t| j | j }tt||||||d dS dS )a  
        Print the Summary table. Currently support overview, model, distributed, operator, memory manipulation and userdefined summary.

        Args:
            sorted_by( :ref:`SortedKeys <api_paddle_profiler_SortedKeys>` , optional): how to rank the op table items, default value is SortedKeys.CPUTotal.
            op_detail(bool, optional): expand each operator detail information, default value is True.
            thread_sep(bool, optional): print op table each thread, default value is False.
            time_unit(str, optional): time unit for display, can be chosen form ['s', 'ms', 'us', 'ns'], default value is 'ms'.
            views(SummaryView|list[SummaryView], optional): summary tables to print, default to None means all views to be printed.

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

                # required: gpu
                import paddle.profiler as profiler
                prof = profiler.Profiler(
                    targets=[profiler.ProfilerTarget.CPU, profiler.ProfilerTarget.GPU],
                    scheduler = (3, 7),
                    on_trace_ready = profiler.export_chrome_tracing('./log'))
                prof.start()
                for iter in range(10):
                    #train()
                    prof.step()
                prof.stop()
                prof.summary(sorted_by=profiler.SortedKeys.CPUTotal, op_detail=True, thread_sep=False, time_unit='ms')
        )	sorted_by	op_detail
thread_sep	time_unitviewsN)rs   r   r|   r   get_dataget_extra_infoprintr   )r}   r   r   r   r   r   Zstatistic_datar%   r%   r&   summary>  s$   
#zProfiler.summaryr   )r   rG   )r!   r"   r#   r$   r   r   r-   r   r   r;   r(   rt   r   boolru   r   r   r   r   r   r8   r   r   rR   r   ZCPUTotalr   r%   r%   r%   r&   rd   T  sN    
	

K/0
*/
Grd   c              
   C   s  zt | d}t|}W d    n1 sw   Y  W n  ty< } ztd| td t W  Y d }~S d }~ww i }d|v rzz*g |d< |d D ]}| dkr]|d t	j
 qL| dkrk|d t	j qLW n   td d |d< Y d|v rzIt|d tr|d  D ]*\}}|d	 }|d
 }	t|}
t|
|}|	s||d i |d |d< q||d< qn|d d |d d g|d< W n   td d |d< Y d|v r"z<t|d tr|d  D ]+\}}|d	 }|d
 }	t|}
t|
|}|	s||d i |d |d< q||d< qW n   td d |d< Y d|v r:t|d tr6|d |d< ntd tdi |S )Nrz'Load config file for profiler error: {}zUse default parameters instead.re   cpuZgpuz;Set targets parameter error, use default parameter instead.rf   module
use_directargskwargsr   r   z=Set scheduler parameter error, use default parameter instead.rg   zBSet on_trace_ready parameter error, use default parameter instead.rj   z>Set timer_only parameter error, use default parameter instead.r%   )openrG   loadr]   r   rI   rd   lowerappendr-   r.   r/   rs   dictitems	importlibimport_modulegetattrr   )Zconfig_pathZ
filehandleZconfig_dicteZtranslated_config_dictr~   keyvaluemodule_pathr   r   methodr%   r%   r&   get_profileru  s   












r   r   )/rM   rJ   rO   enumr   typingr   r   r   r   r   warningsr   r   rG   ro   Zpaddle.fluid.corer	   r
   r   r   r   r   r   r   r   r   r   Zprofiler_statisticr   r   r   Zpaddle.profilertimerr   r   r(   r-   r;   r<   r=   rL   r`   rb   rc   rd   r   r%   r%   r%   r&   <module>   sp   (!
[
8
7    %