TRL documentation

Other

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Other

profiling_decorator

trl.extras.profiling.profiling_decorator

< >

( func: Callable ) Callable

Parameters

  • func (Callable) — Function to be profiled.

Returns

Callable

Wrapped function that profiles execution time.

Decorator to profile a function and log execution time using extras.profiling.profiling_context().

This decorator works with methods that have access to a trainer instance (typically as self).

Example:

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

profiling_context

trl.extras.profiling.profiling_context

< >

( trainer: Trainer name: str ) ProfilingContext

Parameters

  • trainer (~transformers.Trainer) — Trainer object containing configuration for logging.
  • name (str) — Name of the block to be profiled. Will be prefixed with the trainer class name.

Returns

ProfilingContext

A configured profiling context manager.

Factory function to create a ProfilingContext from a Trainer instance.

This function maintains backwards compatibility with existing code while using the decoupled ProfilingContext class internally.

Example:

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
Update on GitHub