
    h                         S SK r S SKrS SKrS SKJr  S SKJr  S SKJrJ	r	  \	" 5       (       a  S SK
r
\" 5       (       a  S SKr\ R                  S\S\S\S   4S	 j5       rS
\S\4S jrg)    N)	Generator)Trainer)is_mlflow_availableis_wandb_availabletrainernamereturn)NNNc              #   F  #    [         R                  " 5       nSv   [         R                  " 5       nX2-
  nSU R                  R                   SU 3U0nSU R                  R
                  ;   aB  [        R                  b1  U R                  R                  (       a  [        R                  " U5        SU R                  R
                  ;   aW  [        R                  bE  U R                  R                  (       a)  [        R                  " XPR                  R                  S9  gggg7f)a  
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         N/home/james-whalen/.local/lib/python3.13/site-packages/trl/extras/profiling.pyprofiling_contextr!      s     6 ""$J	  "H$H1'2C2C2L2L1MQtfUW_`',,(((UYY-BwGZGZGjGj		#$7<<)))fjj.DI\I\IlIl,==3L3LM Jm.D)s   DD!funcc                 F   ^  [         R                  " T 5      U 4S j5       nU$ )a  
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                 ~   > [        U TR                  5         T" U /UQ70 UD6sS S S 5        $ ! , (       d  f       g = f)N)r!   r   )selfr   kwargsr"   s      r    wrapper$profiling_decorator.<locals>.wrapper_   s/    tT]]3.t.v. 433s   .
<)	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>r5      s       %   M  $Nw $Nc $Ni@P6Q $N $NNh 8 r,   