
    ёiQ                       S SK Jr  S SKrS SKrS SKrS SKrS SKrS SKJr  S SK	J
r
JrJr  S SKJr  S SKrS SKJrJrJrJrJrJrJr  S SKJr  SS	KJrJrJrJr  SS
KJ r   SSKJ!r!J"r"  \
(       a  S SK#J$r$  S SK%J&r&  S SK'J(r(  S SKJ)r)   " S S\5      r* " S S\5      r+ " S S\5      r,S S S.           SS jjr-S S jr. S!     S"S jjr/ S!     S"S jjr0S#S jr1 " S S5      r2S r3g)$    )annotationsN)Enum)TYPE_CHECKINGCallableLiteralwarn)ProfilerOptionsTracerEventType	_Profilerdisable_memory_recorderdisable_op_info_recorderenable_memory_recorderenable_op_info_recorder)utils   )
SortedKeysStatisticData_build_tablegen_layer_flops)	benchmark)RecordEventwrap_optimizers)Iterable)TracebackType)Self)_ProfilerResultc                  <    \ rS rSrSrSrSrSrSrSr	Sr
S	rS
rSrSrg)SummaryView7   ae  
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 memory manipulation summary view.

- **SummaryView.UDFView** : The user defined summary view.
r   r                         N)__name__
__module____qualname____firstlineno____doc__
DeviceViewOverView	ModelViewDistributedView
KernelViewOperatorView
MemoryViewMemoryManipulationViewUDFView__static_attributes__r(       X/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/profiler/profiler.pyr   r   7   s7    , JHIOJLJGr8   r   c                  (    \ rS rSrSrSrSrSrSrSr	g)	ProfilerStateY   a  
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"   r(   N)
r)   r*   r+   r,   r-   CLOSEDREADYRECORDRECORD_AND_RETURNr7   r(   r8   r9   r;   r;   Y   s     FEFr8   r;   c                  (    \ rS rSrSrSrSrSrSrSr	g)	ProfilerTargetn   aa  
ProfilerTarget is used to specify target device for :ref:`Profiler <api_paddle_profiler_Profiler>` . Only CPU, GPU and XPU are supported currently.

The meaning of each ProfilerState is as following

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

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

- **ProfilerTarget.XPU** : Profile events on XPU.
r   r   r!   r"   r(   N)
r)   r*   r+   r,   r-   CPUGPUXPUCUSTOM_DEVICEr7   r(   r8   r9   rB   rB   n   s    
 C
C
CMr8   rB   )repeat
skip_firstc                   ^ ^^^^ SU UUUU4S jjnT S:  a  TS:  a  TS:  a  TS:  a  TS:  d   S5       eTS:X  a  [        S5        U$ )a  
Return a scheduler function, which scheduler the :ref:`ProfilerState <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: skipped, 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)
c                \  > U S:  d   eU T:  a  [         R                  $ U T-
  n TT-   T-   nX-  nTS:  a  UT:  a  [         R                  $ X-  nUT:  a  [         R                  $ UT:  a  UTT-   :  a  [         R                  $ X1S-
  :  a  [         R                  $ [         R                  $ )Nr   r   )r;   r=   r>   r?   r@   )	stepperiod_stepshas_repeatedmod_stepclosedreadyrecordrH   rI   s	       r9   getScheduleState(make_scheduler.<locals>.getScheduleState   s    qyy* '''j ~.+QJ<61 '''&f '''Hv~$= &&&**$+++$666r8   r   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.)rL   intreturnr;   r   )rP   rQ   rR   rH   rI   rS   s   ````` r9   make_schedulerrW      sf    l7 7. 	!QJQJaK!O. ..	 za	
 r8   c                "    [         R                  $ )z\
A default state scheduler, keep recording from the beginning of the profiler until ending.
)r;   r?   )rL   s    r9   _default_state_schedulerrY      s     r8   c                   ^ ^ [         R                  R                  T 5      (       d   [         R                  " T SS9  U U4S jnU$ ! [         a    [        ST  S35      ef = f)a  
Return a callable, used for outputting 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

        >>> # doctest: +REQUIRES(env:GPU)
        >>> import paddle.profiler as profiler
        >>> import paddle
        >>> paddle.device.set_device('gpu')
        >>> with profiler.Profiler(
        ...        targets=[profiler.ProfilerTarget.CPU, profiler.ProfilerTarget.GPU],
        ...        scheduler = (3, 10),
        ...        on_trace_ready=profiler.export_chrome_tracing('./log')) as p:
        ...     for iter in range(10):
        ...        #train()
        ...        p.step()
Texist_okCan not create directory '' for saving profiling results.c                N  > T(       d.  S[         R                  " 5        S[        R                  " 5        3m[        R                  R                  5       nSR                  TUR                  S5      5      nU R                  [        R                  R                  TU5      S5        g )Nhost_pid_z{}_time_{}.paddle_trace.json%Y_%m_%d_%H_%M_%S_%fjsonsocketgethostnameosgetpiddatetimenowformatstrftimeexportpathjoinprofrj   filenamedir_nameworker_names      r9   	handle_fn(export_chrome_tracing.<locals>.handle_fn  sz    !&"4"4"6!7tBIIK=IK##%188&<=
 	BGGLL84f=r8   rg   rn   existsmakedirs	ExceptionRuntimeErrorrs   rt   ru   s   `` r9   export_chrome_tracingr}      sd    @ 77>>(##	KK40>   	,XJ6UV 	   A A c                   ^ ^ [         R                  R                  T 5      (       d   [         R                  " T SS9  U U4S jnU$ ! [         a    [        ST  S35      ef = f)a  
Return a callable, used for outputting 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

        >>> # doctest: +REQUIRES(env:GPU)
        >>> import paddle.profiler as profiler
        >>> import paddle
        >>> paddle.device.set_device('gpu')
        >>> 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()
Tr[   r]   r^   c                N  > T(       d.  S[         R                  " 5        S[        R                  " 5        3m[        R                  R                  5       nSR                  TUR                  S5      5      nU R                  [        R                  R                  TU5      S5        g )Nr`   ra   z{}_time_{}.paddle_trace.pbrb   pbrd   rp   s      r9   ru   "export_protobuf.<locals>.handle_fnA  sz    !&"4"4"6!7tBIIK=IK##%/66&<=
 	BGGLL84d;r8   rw   r|   s   `` r9   export_protobufr     sd    B 77>>(##	KK40<   	,XJ6UV 	r~   c                    [         R                  " 5       (       a/  [        R                  [        R                  [        R
                  /$ [         R                  " 5       (       a   [        R                  [        R
                  /$ [         R                  " 5       (       a/  [        R                  [        R                  [        R
                  /$ [        R                  [        R
                  /$ )z:
Get the current supported profiler target in the system.
)	r   is_cupti_supportedrB   rD   rE   rG   is_cnpapi_supportedis_xpti_supportedrF   r(   r8   r9   _get_supported_targetsr   N  s     ##%%((
 	

 $$&&((
 	
 ""$$((
 	

  < <==r8   c            
         \ rS rSr% SrS\S'   S\S'   S\S'   S	\S
'   S\S'   S\S'   S\S'   S\S'   S\S'   S\S'   S\S'   S\S'   S\S'   S\S'   SSSSSSS/ SS.	                   S+S jjrS,S jr        S-S jrS.S  jr	S.S! jr
S/S0S" jjrS/S1S# jjrS$ rS2S3S% jjr\R                   S&SS'S4           S4S( jjrS5S) jrS*rg)6Profilerif  a  
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.XPU <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.
    custom_device_types (list, optional): If targets contain profiler.ProfilerTarget.CUSTOM_DEVICE, custom_device_types select the custom device type for profiling. The default value represents all custom devices will be selected.
    with_flops (bool, optional): If it is True, the flops of the op will be calculated. Default: False.

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

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

            >>> # doctest: +REQUIRES(env:GPU)
            >>> import paddle.profiler as profiler
            >>> import paddle
            >>> paddle.device.set_device('gpu')
            >>> 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

            >>> # doctest: +REQUIRES(env:GPU)
            >>> import paddle.profiler as profiler
            >>> import paddle
            >>> paddle.device.set_device('gpu')
            >>> 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

            >>> # doctest: +REQUIRES(env:GPU)
            >>> import paddle.profiler as profiler
            >>> import paddle
            >>> paddle.device.set_device('gpu')
            >>> 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): # type: ignore[type-arg]
            ...     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().__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    |
Iterable[ProfilerTarget]targetsr   profilerCallable[[int], ProfilerState]	schedulerCallable[[Profiler], None]on_trace_readyrU   step_numr;   previous_statecurrent_stater   record_eventr   profiler_resultbool
timer_onlyrecord_shapesprofile_memory
with_flops	emit_nvtxNF)	r   r   r   r   r   r   r   custom_device_typesr   c       	        V   [        5       n
U(       aK  [        U5      U l        U H4  nX;  d  M
  U R                  R                  U5        [	        SU S35        M6     OXl        [        5       n[        R                  U R                  ;   a  U=R                  S-  sl        [        R                  U R                  ;   a  U=R                  S-  sl        [        R                  U R                  ;   a  U=R                  S-  sl        [        R                  U R                  ;   a:  U=R                  S-  sl        U(       d  [        R                  R                  5       n[        5         [         R"                  " X5      U l        ['        U5      (       a  X l        O[+        U[,        [.        45      (       ah  [1        U5      S:X  a  US   US   :  d   eUu  p[3        US5      nUS:  a!  [5        [3        US-
  S5      SX-
  SS9U l        O[5        SSX-
  SS9U l        O[6        U l        Uc  [9        S	5      U l        OX0l        SU l        [>        R@                  U l!        U R)                  U R<                  5      U l"        S U l#        S U l$        X`l%        X@l&        XPl'        Xl(        Xpl)        g )
Nz
Profiling z% is not supported in current context.r   r!   r#   r'   r   )rP   rQ   rR   rH   z./profiler_log/)*r   setr   remover	   r
   rB   rD   trace_switchrE   rF   rG   paddledeviceget_all_custom_device_typer   r   creater   callabler   
isinstancetuplelistlenmaxrW   rY   r}   r   r   r;   r=   r   r   r   r   r   r   r   r   r   )selfr   r   r   r   r   r   r   r   r   supported_targetstargetprofileoptionstart_batch	end_batchs                  r9   __init__Profiler.__init__  s4    34w<DL!2LL''/$VH,QR " -L')-&&!+&-&&&0&-&&&0&''4<<7&&&0&&&,mm&N&N&P#!((LI&N	E4=11y>Q&9Q<)A,+FFF%."Kk1-Ka!/{Q2%3	" "0%3	" 6DN!"78I"JD"0+22!^^DMM: #$*,$"r8   c                &    U R                  5         U $ N)startr   s    r9   	__enter__Profiler.__enter__D  s    

r8   c                $    U R                  5         g r   )stop)r   exc_typeexc_valexc_tbs       r9   __exit__Profiler.__exit__H  s     			r8   c                   [        5       R                  5         U R                  (       a  U R                  (       a  S[        l        U R                  (       a  gU R                  (       d  U R                  (       a
  [        5         U R                  (       a
  [        5         U R                  [        R                  :X  a  U R                  R                  5         OU R                  [        R                   :X  a5  U R                  R                  5         U R                  R#                  5         ORU R                  [        R$                  :X  a4  U R                  R                  5         U R                  R#                  5         ['        SU R(                   3[*        R,                  S9U l        U R.                  R                  5         g)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

        >>> # doctest: +REQUIRES(env:GPU)
        >>> import paddle.profiler as profiler
        >>> import paddle
        >>> paddle.device.set_device('gpu')
        >>> 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#name
event_type)r   beginr   r   r   _is_profiler_usedr   r   r   r   r   r   r;   r>   r   preparer?   r   r@   r   r   r   ProfileStepr   r   s    r9   r   Profiler.startP  s   2 	$..&*E#??#%"$!4!44MM!!#=#7#77MM!!#MM!=#B#BBMM!!#MM!'/&22
 	!r8   c                   [        5       R                  5         U R                  (       a  gU R                  (       d  U R                  (       a
  [        5         U R                  (       a
  [        5         U R                  (       a!  U R                  R                  5         SU l        U R                  [        R                  :X  a?  [        S5        U R                  R                  5         U R                  R                  5         U R                  [        R                   :X  d  U R                  [        R"                  :X  aA  U R                  R                  5       U l        U R&                  (       a  U R'                  U 5        S[(        l        g)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

        >>> # doctest: +REQUIRES(env:GPU)
        >>> import paddle.profiler as profiler
        >>> import paddle
        >>> paddle.device.set_device('gpu')
        >>> 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()
NzbImproper Profiler state transform: READY->CLOSED, profiler will start and stop without saving dataF)r   endr   r   r   r   r   r   r   r   r;   r>   r	   r   r   r   r?   r@   r   r   r   r   r   s    r9   r   Profiler.stop  s    . 	??$&#% !!# $D!4!44t MM!MM -"6"66!!]%D%DD#'==#5#5#7D ""##D)"'r8   c                   [        5       R                  U5        U R                  (       a  gU R                  (       a!  U R                  R	                  5         SU l        U R
                  U l        U =R                  S-  sl        U R                  U R                  5      U l        U R                  5         [        SU R                   3[        R                  S9U l        U R                  R                  5         g)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

        >>> # doctest: +REQUIRES(env:GPU)
        >>> import paddle.profiler as profiler
        >>> import paddle
        >>> paddle.device.set_device('gpu')
        >>> 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   rL   r   r   r   r   r   r   r   _trigger_actionr   r   r   r   )r   num_sampless     r9   rL   Profiler.step  s    8 	%??!!# $D"00!^^DMM:'/&22
 	!r8   c                >    Uc  Sn[        5       R                  U5      $ )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   |
samples)r   	step_info)r   units     r9   r   Profiler.step_info  s"    V <D{$$T**r8   c                	   U R                   [        R                  :X  a  U R                  [        R                  :X  a  U R
                  R                  5         U R                  [        R                  :X  a4  U R
                  R                  5         U R
                  R                  5         U R                  [        R                  :X  a5  U R
                  R                  5         U R
                  R                  5         g g U R                   [        R                  :X  a  U R                  [        R                  :X  a?  [        S5        U R
                  R                  5         U R
                  R                  5         U R                  [        R                  :X  a  U R
                  R                  5         U R                  [        R                  :X  a  U R
                  R                  5         g g U R                   [        R                  :X  a  U R                  [        R                  :X  a%  [        S5        U R
                  R                  5         U R                  [        R                  :X  a?  [        S5        U R
                  R                  5         U R
                  R                  5         U R                  [        R                  :X  a  g g U R                   [        R                  :X  d   eU R                  [        R                  :X  a  U R
                  R                  5       U l        U R                  [        R                  :X  a9  U R
                  R                  5       U l        U R
                  R                  5         U R                  [        R                  :X  aS  U R
                  R                  5       U l        U R
                  R                  5         U R
                  R                  5         U R                  [        R                  :X  aS  U R
                  R                  5       U l        U R
                  R                  5         U R
                  R                  5         U R                  (       a  U R                  U 5        g g )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)r   r;   r=   r   r>   r   r   r?   r   r@   r	   r   r   r   r   s    r9   r   Profiler._trigger_action  s   -"6"66!!]%8%88%%'!!]%9%99%%'##%""m&E&EE%%'##% F
   M$7$77!!]%9%99h ##%""$!!]%9%99##%""m&E&EE##% F   M$8$88!!]%9%99V ""$!!]%8%88Y ""$%%'""m&E&EE F
 &&-*I*IIII""m&:&::'+}}'9'9';$""m&9&99'+}}'9'9';$%%'""m&:&::'+}}'9'9';$%%'##%""m&E&EE'+}}'9'9';$%%'##%""##D) #r8   c                ^    U R                   (       a  U R                   R                  X5        gg)a9  
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

        >>> # doctest: +REQUIRES(env:GPU)
        >>> import paddle
        >>> paddle.device.set_device('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   rn   rk   s      r9   rm   Profiler.exportU  s&    6   %%d3  r8   Tmsc                @   [        U[        5      (       a  U/nU R                  (       aS  [        U R                  R	                  5       U R                  R                  5       5      n[        [        UUUUUUS95        U R                  (       a  U R                  5         gg)ap  
Print the Summary table. Currently support overview, model, distributed, operator, memory manipulation and user-defined 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 from ['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

        >>> # doctest: +REQUIRES(env:GPU)
        >>> import paddle
        >>> paddle.device.set_device('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)
r   r   r   r   get_dataget_extra_infoprintr   r   _print_flops)r   r   r   r   r   r   statistic_datas          r9   summaryProfiler.summarys  s    H e[))GE*$$--/$$335N "'')'	 ?? r8   c                   U R                   (       d  [        S5        g [        SR                  SS5      5        [        [        U R                  R                  5       U5      5        [        SR                  SS5      5        g )NzERROR: with_flops disabled.z Flops Profiler Begin d   -z- Flops Profiler End -)r   r   centerr   r   r   )r   rH   s     r9   r   Profiler._print_flops  s_    /0&--c378od22;;=vFG&--c378r8   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   zIterable[ProfilerTarget] | Noner   z7Callable[[int], ProfilerState] | tuple[int, int] | Noner   z!Callable[[Profiler], None] | Noner   r   r   r   r   r   r   r   r   z	list[str]r   r   rV   None)rV   r   )r   ztype[BaseException] | Noner   zBaseException | Noner   zTracebackType | NonerV   r   )rV   r   r   )r   z
int | NonerV   r   )r   
str | NonerV   str) rc   )rn   r   rk   r   rV   r   )r   r   r   r   r   r   r   zLiteral['s', 'ms', 'us', 'ns']r   z&SummaryView | list[SummaryView] | NonerV   r   )r   )r)   r*   r+   r,   r-   __annotations__r   r   r   r   r   rL   r   r   rm   r   CPUTotalr   r   r7   r(   r8   r9   r   r   f  s   B &%--..M!!  $$O
 48 <@#$ )+ K# 1K#
 DK# :K# K# K# K# K# 'K# K# 
K#Z, & %	
 
/"b0(d*"X-+^E*N4@ !+ 3 3 488<8 8  8  	8 
 28  68  
8 t9r8   r   c                H    [        U S5       n[        R                  " U5      nS S S 5        0 nSW;   a   / US'   US    Hs  nUR                  5       S:X  a$  US   R                  [        R                  5        M;  UR                  5       S:X  d  MQ  US   R                  [        R                  5        Mu     SU;   a   [        US   [        5      (       af  US   R                  5        HN  u  pgUS	   nUS
   n	[        R                  " U5      n
[!        X5      nU	(       d  U" US   0 US   D6US'   MJ  XS'   MP     OUS   S   US   S   /US'    SU;   a~   [        US   [        5      (       ae  US   R                  5        HN  u  pgUS	   nUS
   n	[        R                  " U5      n
[!        X5      nU	(       d  U" US   0 US   D6US'   MJ  XS'   MP     SU;   a,  [        US   ["        5      (       a	  US   US'   O[	        S5        [        S0 UD6$ ! , (       d  f       GN= f! [         a-  n[	        SU 35        [	        S5        [        5       s S nA$ S nAff = f!   [	        S5        S US'    GN= f!   [	        S5        S US'    GN:= f!   [	        S5        S US'    N= f)Nrz%Load config file for profiler error: zUse default parameters instead.r   cpugpuz;Set targets parameter error, use default parameter instead.r   module
use_directargskwargsr   r   z=Set scheduler parameter error, use default parameter instead.r   zBSet on_trace_ready parameter error, use default parameter instead.r   z>Set timer_only parameter error, use default parameter instead.r(   )openrc   loadrz   r   r   lowerappendrB   rD   rE   r   dictitems	importlibimport_modulegetattrr   )config_path
filehandleconfig_dictetranslated_config_dictr   keyvaluemodule_pathr   r   methods               r9   get_profilerr    s   +s#z))J/K $  K		502"9-%i0<<>U**95<<^=O=OP\\^u,*95<<^=O=OP	 1 k!	7+k2D99"-k":"@"@"BJC"'/K!&|!4J&44[AF$V1F%>D"6]?.3Ho?.{; ?E{; #C  ,Q/,Q/7&{3 ;&	<+&67>>"-.>"?"E"E"GJC"'/K!&|!4J&44[AF$V1F%CI"6]D.3HoD./?@ DJ/?@ #H  {"k,/663>|3L"<0P -,--M $# 5aS9:/0z	5OP04"9-*	7O 37";/	<T 8<"#34sk   H# HH# AI &I >A=I5 <I5 A=J 
H H#  H# #
I-"IIII25J
J!)rP   rU   rQ   rU   rR   rU   rH   rU   rI   rU   rV   r   )rL   rU   r   )rs   r   rt   r   rV   r   )rV   r   )4
__future__r   ri   r  rc   rg   re   enumr   typingr   r   r   warningsr	   r   paddle.base.corer
   r   r   r   r   r   r   paddle.profilerr   profiler_statisticr   r   r   r   timerr   r   r   collections.abcr   typesr   typing_extensionsr   r   r   r;   rB   rW   rY   r}   r   r   r   r  r(   r8   r9   <module>r     s4   #    	   3 3     "   /(#&0$ DD *T 0 XX X 	X
 X X $Xv  .222 *22l .233 *33l>0N	9 N	9bH.r8   