Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/transformers/integrations/integration_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2278,6 +2278,18 @@ def setup(self, args, state, model, **kwargs):
- **SWANLAB_API_HOST** (`str`, *optional*, defaults to `None`):
API address for the SwanLab cloud environment for private version (its free)

- **SWANLAB_RUN_ID** (`str`, *optional*, defaults to `None`):
The SwanLab run ID (21-character string) to resume. When set together with `SWANLAB_RESUME`, enables
resuming a previous run so that `trainer.train(resume_from_checkpoint=...)` continues the same
experiment instead of creating a new one. The run ID can be found in the experiment's Environment tab
or in the URL on the SwanLab dashboard.
Comment on lines +2281 to +2285

Copilot AI Feb 4, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation mentions "SWANLAB_ID" as an alternative to "SWANLAB_RUN_ID", but this is only documented in the implementation code (line 2317), not in the docstring. For consistency and clarity, the docstring should document both environment variable names.

Consider updating the docstring at line 2281 to mention both names, for example:
"SWANLAB_RUN_ID (or SWANLAB_ID) (str, optional, defaults to None):"

This would make it clear to users that either environment variable name can be used.

Copilot uses AI. Check for mistakes.

- **SWANLAB_RESUME** (`str` or `bool`, *optional*, defaults to `None`):
Resume mode for SwanLab. Use with `SWANLAB_RUN_ID` when resuming training. Accepted values: `"allow"`
(resume if run exists, else create new; same as `True`), `"must"` (resume only, error if run missing),
`"never"` (always create new run; same as `False`). Set to `"allow"` or `True` when using
`trainer.train(resume_from_checkpoint=...)` to continue the same experiment.

"""
self._initialized = True

Expand All @@ -2301,6 +2313,21 @@ def setup(self, args, state, model, **kwargs):
init_args["experiment_name"] = trial_name
init_args["project"] = os.getenv("SWANLAB_PROJECT", None)

# Support resuming a previous run (e.g. when using trainer.train(resume_from_checkpoint=...))
swanlab_run_id = os.getenv("SWANLAB_RUN_ID", None) or os.getenv("SWANLAB_ID", None)
swanlab_resume = os.getenv("SWANLAB_RESUME", None)
if swanlab_run_id is not None:
init_args["id"] = swanlab_run_id
if swanlab_resume is not None:
if swanlab_resume.lower() in ("true", "1"):
init_args["resume"] = True
elif swanlab_resume.lower() in ("false", "0"):
init_args["resume"] = False
elif swanlab_resume.lower() in ("allow", "must", "never"):
init_args["resume"] = swanlab_resume.lower()
else:
init_args["resume"] = swanlab_resume
Comment on lines +2321 to +2329

Copilot AI Feb 4, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The environment variable parsing for boolean values doesn't follow the established pattern in this codebase. Other integrations in this file use either ENV_VARS_TRUE_VALUES (e.g., MLflowCallback at lines 1300-1301) or utility functions like is_env_variable_true() for consistent parsing.

For consistency with the codebase, consider using the standard approach:

swanlab_resume_env = os.getenv("SWANLAB_RESUME", None)
if swanlab_resume_env is not None:
    resume_upper = swanlab_resume_env.upper()
    if resume_upper in ENV_VARS_TRUE_VALUES or resume_upper == "ALLOW":
        init_args["resume"] = "allow"  # or True, depending on SwanLab's API
    elif resume_upper in ("FALSE", "0", "NEVER"):
        init_args["resume"] = "never"  # or False
    elif resume_upper == "MUST":
        init_args["resume"] = "must"
    else:
        init_args["resume"] = swanlab_resume_env

This approach:

  1. Follows the established pattern (see src/transformers/integrations/integration_utils.py:1300-1301)
  2. Handles case-insensitivity consistently
  3. Avoids potential AttributeError if the value is not a string
  4. Uses the same values that other integrations recognize ("TRUE", "1", "FALSE", "0")

Copilot uses AI. Check for mistakes.

if self._swanlab.get_run() is None:
self._swanlab.init(
**init_args,
Expand Down
Loading