o
    -wiW	                     @   sD   d Z ddlZddlZddlmZmZ ddlmZ dZdZ	dd Z
dS )	z"Integration with pytorch profiler.    N)Error
UsageError)	telemetrytorchztorch.profilerc                  C   s   ddl m}  tjjtdd}tjjtdd}| |j| dk r'td|j zt	j
tjjd}t	| W n tyC   tdd	w t }d|j_W d	   n1 sWw   Y  ||S )
a   Create a trace handler for traces generated by the profiler.

     Provide as an argument to `torch.profiler.profile`:
     ```python
     torch.profiler.profile(..., on_trace_ready=wandb.profiler.torch_trace_handler())
     ```

    Calling this function ensures that profiler charts & tables can be viewed in
    your run dashboard on wandb.ai.

    Please note that `wandb.init()` must be called before this function is
    invoked, and the reinit setting must not be set to "create_new". The PyTorch
    (torch) version must also be at least 1.9, in order to ensure stability of
    their Profiler API.

    Args:
        None

    Returns:
        None

    Raises:
        UsageError if wandb.init() hasn't been called before profiling.
        Error if torch version is less than 1.9.0.

    Examples:
    ```python
    run = wandb.init()
    run.config.id = "profile_code"

    with torch.profiler.profile(
        schedule=torch.profiler.schedule(wait=1, warmup=1, active=3, repeat=1),
        on_trace_ready=wandb.profiler.torch_trace_handler(),
        record_shapes=True,
        with_stack=True,
    ) as prof:
        for i, batch in enumerate(dataloader):
            if step >= 5:
                break
            train(batch)
            prof.step()
    ```
    r   )parseT)requiredz1.9.0ztorch version must be at least 1.9 in order to use the PyTorch Profiler API.            
Version of torch currently installed: pytorch_traceszHPlease call `wandb.init()` before `wandb.profiler.torch_trace_handler()`N)packaging.versionr   wandbutil
get_modulePYTORCH_MODULEPYTORCH_PROFILER_MODULE__version__r   ospathjoinrundirmkdirAttributeErrorr   r   contextfeaturetorch_profiler_tracetensorboard_trace_handler)r   r   torch_profilerlogdirtel r   X/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/wandb/sdk/internal/profiler.pytorch_trace_handler   s.   ,


r    )__doc__r   r
   wandb.errorsr   r   wandb.sdk.libr   r   r   r    r   r   r   r   <module>   s    