
    bi                         d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZm	Z	  e	       rd dl
Z
 e       rd dlZe j                  dededed   fd	       Zd
edefdZy)    N)	Generator)Trainer)is_mlflow_availableis_wandb_availabletrainernamereturn)NNNc              #   (  K   t        j                         }d t        j                         }||z
  }d| j                  j                   d| |i}d| j                  j
                  v r;t        j                  +| j                  j                  rt        j                  |       d| j                  j
                  v rTt        j                  C| j                  j                  r,t        j                  || j                  j                         yyyyw)aB  
    A context manager function for profiling a block of code. Results are logged to Weights & Biases or MLflow
    depending on the trainer's configuration.

    Args:
        trainer (`~transformers.Trainer`):
            Trainer object.
        name (`str`):
            Name of the block to be profiled. Used as a key in the logged dictionary.

    Example:
    ```python
    from transformers import Trainer
    from trl.extras.profiling import profiling_context


    class MyTrainer(Trainer):
        def some_method(self):
            A = np.random.rand(1000, 1000)
            B = np.random.rand(1000, 1000)
            with profiling_context(self, "matrix_multiplication"):
                # Code to profile: simulate a computationally expensive operation
                result = A @ B  # Matrix multiplication
    ```
    Nzprofiling/Time taken: .wandbmlflow)step)timeperf_counter	__class____name__args	report_tor   runacceleratoris_main_processlogr   log_metricsstateglobal_step)r   r   
start_timeend_timedurationprofiling_metricss         O/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/trl/extras/profiling.pyprofiling_contextr!      s     6 ""$J	  "H*$H1'2C2C2L2L1MQtfUW_`',,(((UYY-BwGZGZGjGj		#$7<<)))fjj.DI\I\IlIl,7==3L3LM Jm.D)s   DDfuncc                 B     t        j                          fd       }|S )aV  
    Decorator to profile a function and log execution time using [`extras.profiling.profiling_context`].

    Args:
        func (`callable`):
            Function to be profiled.

    Example:
    ```python
    from transformers import Trainer
    from trl.extras.profiling import profiling_decorator


    class MyTrainer(Trainer):
        @profiling_decorator
        def some_method(self):
            A = np.random.rand(1000, 1000)
            B = np.random.rand(1000, 1000)
            # Code to profile: simulate a computationally expensive operation
            result = A @ B
    ```
    c                 t    t        | j                        5   | g|i |cd d d        S # 1 sw Y   y xY w)N)r!   r   )selfr   kwargsr"   s      r    wrapperz$profiling_decorator.<locals>.wrapper_   s8    tT]]3 	/.t.v.	/ 	/ 	/s   .7)	functoolswraps)r"   r'   s   ` r    profiling_decoratorr*   G   s'    0 __T/ / N    )
contextlibr(   r   collections.abcr   transformersr   transformers.integrationsr   r   r   r   contextmanagerstrr!   callabler*    r+   r    <module>r4      s{       %   M  $Nw $Nc $Ni@P6Q $N $NNh 8 r+   