o
    mi                     @   sb   d Z ddlZddlZddlmZmZ ddlmZ ddlZddl	m
Z eeZG dd deZdS )a  W&B callback for sb3.

Really simple callback to get logging for each tree

Example usage:

```python
import gym
from stable_baselines3 import PPO
from stable_baselines3.common.monitor import Monitor
from stable_baselines3.common.vec_env import DummyVecEnv, VecVideoRecorder
import wandb
from wandb.integration.sb3 import WandbCallback


config = {
    "policy_type": "MlpPolicy",
    "total_timesteps": 25000,
    "env_name": "CartPole-v1",
}
run = wandb.init(
    project="sb3",
    config=config,
    sync_tensorboard=True,  # auto-upload sb3's tensorboard metrics
    monitor_gym=True,  # auto-upload the videos of agents playing the game
    save_code=True,  # optional
)


def make_env():
    env = gym.make(config["env_name"])
    env = Monitor(env)  # record stats such as returns
    return env


env = DummyVecEnv([make_env])
env = VecVideoRecorder(
    env, "videos", record_video_trigger=lambda x: x % 2000 == 0, video_length=200
)
model = PPO(config["policy_type"], env, verbose=1, tensorboard_log=f"runs")
model.learn(
    total_timesteps=config["total_timesteps"],
    callback=WandbCallback(
        model_save_path=f"models/{run.id}",
        gradient_save_freq=100,
        log="all",
    ),
)
```
    N)LiteralOptional)BaseCallback)	telemetryc                       s~   e Zd ZdZ					ddedee deded	eed
  ddf fddZdddZ	de
fddZdddZdddZ  ZS )WandbCallbacka  Callback for logging experiments to Weights and Biases.

    Log SB3 experiments to Weights and Biases
        - Added model tracking and uploading
        - Added complete hyperparameters recording
        - Added gradient logging
        - Note that `wandb.init(...)` must be called before the WandbCallback can be used.

    Args:
        verbose: The verbosity of sb3 output
        model_save_path: Path to the folder where the model will be saved, The default value is `None` so the model is not logged
        model_save_freq: Frequency to save the model
        gradient_save_freq: Frequency to log gradient. The default value is 0 so the gradients are not logged
        log: What to log. One of "gradients", "parameters", or "all".
    r   Nallverbosemodel_save_pathmodel_save_freqgradient_save_freqlog)	gradients
parametersr   returnc                    s   t  | tjd u rtdt }d|j_W d    n1 s#w   Y  || _	|| _
|| _|dvr<td d}|| _| j
d urWtj| j
dd tj| j
d| _d S | j	dks`J d	d S )
Nz1You must call wandb.init() before WandbCallback()T)r   r   r   NzW`log` must be one of `None`, 'gradients', 'parameters', or 'all', falling back to 'all'r   )exist_okz	model.zipr   zLto use the `model_save_freq` you have to set the `model_save_path` parameter)super__init__wandbrunErrorwb_telemetrycontextfeaturesb3r
   r	   r   termwarnr   osmakedirspathjoin)selfr   r	   r
   r   r   tel	__class__ W/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/wandb/integration/sb3/sb3.pyr   Q   s*   




zWandbCallback.__init__c                 C   s   i }d|vrt | jj|d< | jjD ](}|tjv rqt | jj| tttfv r0| jj| ||< qt| jj| ||< q| j	dkrLtj
| jj| j	| jd tj| d S )Nalgor   )log_freqr   )typemodel__name____dict__r   configfloatintstrr   watchpolicyr   setdefaults)r   dkeyr#   r#   r$   _init_callbackq   s    

zWandbCallback._init_callbackc                 C   s0   | j dkr| jd ur| j| j  dkr|   dS )Nr   T)r
   r	   n_calls
save_modelr   r#   r#   r$   _on_step   s
   

zWandbCallback._on_stepc                 C   s   | j d ur|   d S d S )N)r	   r6   r7   r#   r#   r$   _on_training_end   s   
zWandbCallback._on_training_endc                 C   sD   | j | j tj| j| jd | jdkr td| j  d S d S )N)	base_path   zSaving model checkpoint to )r(   saver   r   r	   r   loggerinfor7   r#   r#   r$   r6      s
   
zWandbCallback.save_model)r   Nr   r   r   )r   N)r)   
__module____qualname____doc__r-   r   r.   r   r   r4   boolr8   r9   r6   __classcell__r#   r#   r!   r$   r   @   s0    

 
r   )rA   loggingr   typingr   r   "stable_baselines3.common.callbacksr   r   wandb.sdk.libr   r   	getLoggerr)   r=   r   r#   r#   r#   r$   <module>   s    3
