feat(logging): add swanlab backend for training runs

这个提交包含在:
MrBlue0632
2026-04-08 14:34:27 +08:00
父节点 bfdc28cd2a
当前提交 bcecabb6b9
共有 2 个文件被更改,包括 77 次插入 和 13 次删除

查看文件

@ -35,6 +35,9 @@ Validated in this repo / recent experiments:
- We currently recommend `dataset.image_transforms.enable=false` in the validated training commands because the image augmentation pipeline was not stable in the tested environment.
- Stage2 now defaults to `embedder_tensor_fastpath=true`, `enable_vlm_gradient_checkpointing=true`, and
`gradient_checkpointing_use_reentrant=false`.
- When stage2 is launched from `--policy.path=<stage1_ckpt>`, the stage1 checkpoint config is loaded first.
In practice, it is safer to explicitly pass the stage2 memory/speed flags on the CLI so they override any
stage1-era values saved in `config.json`.
## Optimization Summary
@ -44,6 +47,12 @@ The current Evo1 integration includes a small set of stage2-oriented training op
- Evo1 image collection during training no longer forces a `GPU -> CPU -> GPU` round trip before the VLM embedder.
- InternVL3 stage2 now enables a more memory-friendly checkpointing path and disables `use_cache` when checkpointing is active.
- The main `lerobot_train` entrypoint was kept intact; only lightweight Evo1 stage2 runtime logging was added.
- These Evo1-specific optimizations apply to `stage2` training and to the final `RL policy train` stage when it uses
`policy.type=evo1` with `training_stage=stage2`. They do not apply to `value-train` or `value-infer`, which use
the separate value-model pipeline.
- In the current local environment, the default `pistar06` language backbone `google/gemma-3-270m` may fail with
a gated-repo `403`. For runnable value/RL scripts, prefer explicitly setting
`--value.language_repo_id=Qwen/Qwen2.5-0.5B`.
## Stage 1 Command
@ -100,6 +109,10 @@ PYTHONPATH=src /opt/conda/bin/accelerate launch \
--resume_pretrain=true \
--policy.vlm_model_name=/root/private_data/models/InternVL3-1B \
--policy.device=cuda \
--policy.use_flash_attn=true \
--policy.embedder_tensor_fastpath=true \
--policy.enable_vlm_gradient_checkpointing=true \
--policy.gradient_checkpointing_use_reentrant=false \
--policy.push_to_hub=false \
--policy.dropout=0.2 \
--policy.optimizer_lr=1e-5 \

查看文件

@ -80,8 +80,40 @@ class WandBLogger:
self.job_name = cfg.job_name
self.env_fps = cfg.env.fps if cfg.env else None
self._group = cfg_to_group(cfg)
self._backend = "wandb"
self._wandb_custom_step_key: set[str] | None = None
swanlab_api_key = os.getenv("SWANLAB_API_KEY")
if swanlab_api_key:
import swanlab
self._backend = "swanlab"
swanlab.login(api_key=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
return
# Set up WandB.
os.environ["WANDB_SILENT"] = "True"
import wandb
@ -101,25 +133,21 @@ class WandBLogger:
tags=cfg_to_group(cfg, return_list=True, truncate_tags=True),
dir=self.log_dir,
config=cfg.to_dict(),
# TODO(rcadene): try set to True
save_code=False,
# TODO(rcadene): split train and eval, and run async eval with job_type="eval"
job_type="train_eval",
resume="must" if cfg.resume else None,
mode=self.cfg.mode if self.cfg.mode in ["online", "offline", "disabled"] else "online",
)
run_id = wandb.run.id
# NOTE: We will override the cfg.wandb.run_id with the wandb run id.
# This is because we want to be able to resume the run from the wandb run id.
cfg.wandb.run_id = run_id
# Handle custom step key for rl asynchronous training.
self._wandb_custom_step_key: set[str] | None = None
logging.info(colored("Logs will be synced with wandb.", "blue", attrs=["bold"]))
logging.info(f"Track this run --> {colored(wandb.run.get_url(), 'yellow', attrs=['bold'])}")
self._wandb = wandb
def log_policy(self, checkpoint_dir: Path):
"""Checkpoints the policy to wandb."""
if self._backend == "swanlab":
return
if self.cfg.disable_artifact:
return
@ -163,11 +191,26 @@ class WandBLogger:
if step is None and custom_step_key is None:
raise ValueError("Either step or custom_step_key must be provided.")
# NOTE: This is not simple. Wandb step must always monotonically increase and it
# increases with each wandb.log call, but in the case of asynchronous RL for example,
# multiple time steps is possible. For example, the interaction step with the environment,
# the training step, the evaluation step, etc. So we need to define a custom step key
# to log the correct step for each metric.
if self._backend == "swanlab":
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)
return
if custom_step_key is not None:
if self._wandb_custom_step_key is None:
self._wandb_custom_step_key = set()
@ -183,7 +226,6 @@ class WandBLogger:
)
continue
# Do not log the custom step key itself.
if self._wandb_custom_step_key is not None and k in self._wandb_custom_step_key:
continue
@ -199,5 +241,14 @@ class WandBLogger:
if mode not in {"train", "eval"}:
raise ValueError(mode)
if self._backend == "swanlab":
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)
return
wandb_video = self._wandb.Video(video_path, fps=self.env_fps, format="mp4")
self._wandb.log({f"{mode}/video": wandb_video}, step=step)