镜像自地址
https://github.com/MINT-SJTU/Evo-RL.git
已同步 2026-11-20 18:17:06 +00:00
feat(logging): add swanlab backend for training runs (#17)
* feat(logging): add swanlab backend + evo1 stage2 optimizations - Add SwanLab logging support (auto-selects when SWANLAB_API_KEY is set) - Add stage2 training optimizations: tensor fastpath, gradient checkpointing - Document stage2 config override behavior and runnable value-model alternative Cherry-picked from:bfdc28c,bcecabb* refactor(logging): separate SwanLabLogger from WandBLogger - Restore WandBLogger to its original clean form with all comments - Add SwanLabLogger as a separate class with the same interface - Add make_logger() factory function to select backend based on SWANLAB_API_KEY environment variable - Move evo1 stage2 runtime logging from lerobot_train.py into EVO1Policy.__init__ where it belongs - Add ImportError handling for swanlab dependency * fix(logging): gracefully fall back to WandB when swanlab is not installed If SWANLAB_API_KEY is set but the swanlab package is not installed, make_logger() now logs a warning and falls back to WandBLogger instead of crashing with an ImportError at training startup. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Elvinyk <223015152@link.cuhk.edu.cn> Co-authored-by: MrBlue0632 <2028207642@qq.com> Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
这个提交包含在:
@ -150,13 +150,11 @@ def train(cfg: TrainRLServerPipelineConfig, job_name: str | None = None):
|
||||
logging.info(f"Learner logging initialized, writing to {log_file}")
|
||||
logging.info(pformat(cfg.to_dict()))
|
||||
|
||||
# Setup WandB logging if enabled
|
||||
if cfg.wandb.enable and cfg.wandb.project:
|
||||
from lerobot.rl.wandb_utils import WandBLogger
|
||||
# Setup logging
|
||||
from lerobot.rl.wandb_utils import make_logger
|
||||
|
||||
wandb_logger = WandBLogger(cfg)
|
||||
else:
|
||||
wandb_logger = None
|
||||
wandb_logger = make_logger(cfg)
|
||||
if wandb_logger is None:
|
||||
logging.info(colored("Logs will be saved locally.", "yellow", attrs=["bold"]))
|
||||
|
||||
# Handle resume logic
|
||||
|
||||
@ -201,3 +201,106 @@ class WandBLogger:
|
||||
|
||||
wandb_video = self._wandb.Video(video_path, fps=self.env_fps, format="mp4")
|
||||
self._wandb.log({f"{mode}/video": wandb_video}, step=step)
|
||||
|
||||
|
||||
class SwanLabLogger:
|
||||
"""A helper class to log training metrics using SwanLab."""
|
||||
|
||||
def __init__(self, cfg: TrainPipelineConfig):
|
||||
try:
|
||||
import swanlab
|
||||
except ImportError:
|
||||
raise ImportError(
|
||||
"SwanLab is required when SWANLAB_API_KEY is set. "
|
||||
"Install it with: pip install swanlab"
|
||||
)
|
||||
|
||||
self.cfg = cfg.wandb
|
||||
self.log_dir = cfg.output_dir
|
||||
self.job_name = cfg.job_name
|
||||
self.env_fps = cfg.env.fps if cfg.env else None
|
||||
|
||||
swanlab.login(api_key=os.environ["SWANLAB_API_KEY"])
|
||||
self._run = swanlab.init(
|
||||
project=self.cfg.project,
|
||||
workspace=self.cfg.entity,
|
||||
experiment_name=self.job_name,
|
||||
description=self.cfg.notes,
|
||||
tags=cfg_to_group(cfg, return_list=True, truncate_tags=True),
|
||||
logdir=str(self.log_dir),
|
||||
config=cfg.to_dict(),
|
||||
mode=self.cfg.mode if self.cfg.mode in ["offline", "disabled"] else "cloud",
|
||||
id=cfg.wandb.run_id,
|
||||
resume="must" if cfg.resume else None,
|
||||
)
|
||||
run_id = getattr(self._run, "id", None)
|
||||
cfg.wandb.run_id = run_id
|
||||
logging.info(colored("Logs will be synced with swanlab.", "blue", attrs=["bold"]))
|
||||
try:
|
||||
import swanlab.data as swanlab_data
|
||||
|
||||
run_url = swanlab_data.get_url()
|
||||
if run_url:
|
||||
logging.info(f"Track this run --> {colored(run_url, 'yellow', attrs=['bold'])}")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def log_policy(self, checkpoint_dir: Path):
|
||||
return
|
||||
|
||||
def log_dict(
|
||||
self, d: dict, step: int | None = None, mode: str = "train", custom_step_key: str | None = None
|
||||
):
|
||||
if mode not in {"train", "eval"}:
|
||||
raise ValueError(mode)
|
||||
if step is None and custom_step_key is None:
|
||||
raise ValueError("Either step or custom_step_key must be provided.")
|
||||
|
||||
data = {}
|
||||
if custom_step_key is not None and custom_step_key in d:
|
||||
value_custom_step = d[custom_step_key]
|
||||
data[f"{mode}/{custom_step_key}"] = value_custom_step
|
||||
if step is None:
|
||||
step = int(value_custom_step)
|
||||
for k, v in d.items():
|
||||
if not isinstance(v, (int | float | str)):
|
||||
logging.warning(
|
||||
f'SwanLab logging of key "{k}" was ignored as its type "{type(v)}" is not handled by this wrapper.'
|
||||
)
|
||||
continue
|
||||
if custom_step_key is not None and k == custom_step_key:
|
||||
continue
|
||||
data[f"{mode}/{k}"] = v
|
||||
if data:
|
||||
self._run.log(data, step=step)
|
||||
|
||||
def log_video(self, video_path: str, step: int, mode: str = "train"):
|
||||
if mode not in {"train", "eval"}:
|
||||
raise ValueError(mode)
|
||||
|
||||
try:
|
||||
import swanlab
|
||||
|
||||
self._run.log({f"{mode}/video": swanlab.Video(video_path)}, step=step)
|
||||
except Exception as exc:
|
||||
logging.warning("Failed to log video to SwanLab: %s", exc)
|
||||
|
||||
|
||||
def make_logger(cfg: TrainPipelineConfig) -> WandBLogger | SwanLabLogger | None:
|
||||
"""Create a training logger based on config and environment.
|
||||
|
||||
Returns SwanLabLogger if SWANLAB_API_KEY is set, WandBLogger otherwise.
|
||||
Returns None if logging is disabled.
|
||||
"""
|
||||
if not (cfg.wandb.enable and cfg.wandb.project):
|
||||
return None
|
||||
|
||||
if os.getenv("SWANLAB_API_KEY"):
|
||||
try:
|
||||
return SwanLabLogger(cfg)
|
||||
except ImportError:
|
||||
logging.warning(
|
||||
"swanlab package is not installed but SWANLAB_API_KEY is set. "
|
||||
"Falling back to WandB. Install swanlab with: pip install swanlab"
|
||||
)
|
||||
return WandBLogger(cfg)
|
||||
|
||||
在新工单中引用
屏蔽一个用户