Skip to content

Weights & Biases

tensoris.lib.integrations.wandb

Weights & Biases (W&B) Experiment Tracking Integration.

Classes

WandbIntegration

Weights & Biases Logger for metric tracking, artifact logging, and hyperparameter sweeps.

References

Weights & Biases Python SDK Documentation: https://docs.wandb.ai/

Source code in src/tensoris/lib/integrations/wandb.py
class WandbIntegration:
    """Weights & Biases Logger for metric tracking, artifact logging, and hyperparameter sweeps.

    References:
        Weights & Biases Python SDK Documentation: https://docs.wandb.ai/
    """

    def __init__(
        self,
        project: str = "deep-learning-project",
        entity: str | None = None,
        config: dict[str, Any] | None = None,
        name: str | None = None,
        mode: str = "online",
    ) -> None:
        """Initialize W&B run.

        Args:
            project: W&B project name.
            entity: W&B username or team entity.
            config: Hyperparameter configuration dictionary.
            name: Display name for the run.
            mode: Run mode ('online', 'offline', or 'disabled').
        """
        self.project = project
        self.entity = entity
        self.config = config or {}
        self.name = name
        self.mode = mode
        self.run = None

    @property
    def is_available(self) -> bool:
        """Check if wandb library is installed."""
        return _WANDB_AVAILABLE

    def init(self) -> Any:
        """Initialize W&B run context."""
        if not self.is_available:
            raise RuntimeError(
                "wandb package is not installed. Install via `pip install wandb`."
            )
        self.run = wandb.init(
            project=self.project,
            entity=self.entity,
            config=self.config,
            name=self.name,
            mode=self.mode,
        )
        return self.run

    def log(self, metrics: dict[str, Any], step: int | None = None) -> None:
        """Log metric key-value dictionary to W&B dashboard.

        Args:
            metrics: Dictionary of numerical metrics or media logs.
            step: Optional global training step number.
        """
        if self.is_available and self.run is not None:
            wandb.log(metrics, step=step)

    def log_artifact(
        self, file_path: str, artifact_name: str, artifact_type: str = "model"
    ) -> None:
        """Upload checkpoint file or artifact to W&B.

        Args:
            file_path: Local file path to upload.
            artifact_name: W&B artifact name identifier.
            artifact_type: Artifact category ('model', 'dataset', 'checkpoint').
        """
        if self.is_available and self.run is not None:
            artifact = wandb.Artifact(artifact_name, type=artifact_type)
            artifact.add_file(file_path)
            self.run.log_artifact(artifact)

    def finish(self) -> None:
        """Finish W&B run."""
        if self.is_available and self.run is not None:
            wandb.finish()
Attributes
is_available property
is_available

Check if wandb library is installed.

Methods:
__init__
__init__(
    project="deep-learning-project",
    entity=None,
    config=None,
    name=None,
    mode="online",
)

Initialize W&B run.

Parameters:

Name Type Description Default
project str

W&B project name.

'deep-learning-project'
entity str | None

W&B username or team entity.

None
config dict[str, Any] | None

Hyperparameter configuration dictionary.

None
name str | None

Display name for the run.

None
mode str

Run mode ('online', 'offline', or 'disabled').

'online'
Source code in src/tensoris/lib/integrations/wandb.py
def __init__(
    self,
    project: str = "deep-learning-project",
    entity: str | None = None,
    config: dict[str, Any] | None = None,
    name: str | None = None,
    mode: str = "online",
) -> None:
    """Initialize W&B run.

    Args:
        project: W&B project name.
        entity: W&B username or team entity.
        config: Hyperparameter configuration dictionary.
        name: Display name for the run.
        mode: Run mode ('online', 'offline', or 'disabled').
    """
    self.project = project
    self.entity = entity
    self.config = config or {}
    self.name = name
    self.mode = mode
    self.run = None
finish
finish()

Finish W&B run.

Source code in src/tensoris/lib/integrations/wandb.py
def finish(self) -> None:
    """Finish W&B run."""
    if self.is_available and self.run is not None:
        wandb.finish()
init
init()

Initialize W&B run context.

Source code in src/tensoris/lib/integrations/wandb.py
def init(self) -> Any:
    """Initialize W&B run context."""
    if not self.is_available:
        raise RuntimeError(
            "wandb package is not installed. Install via `pip install wandb`."
        )
    self.run = wandb.init(
        project=self.project,
        entity=self.entity,
        config=self.config,
        name=self.name,
        mode=self.mode,
    )
    return self.run
log
log(metrics, step=None)

Log metric key-value dictionary to W&B dashboard.

Parameters:

Name Type Description Default
metrics dict[str, Any]

Dictionary of numerical metrics or media logs.

required
step int | None

Optional global training step number.

None
Source code in src/tensoris/lib/integrations/wandb.py
def log(self, metrics: dict[str, Any], step: int | None = None) -> None:
    """Log metric key-value dictionary to W&B dashboard.

    Args:
        metrics: Dictionary of numerical metrics or media logs.
        step: Optional global training step number.
    """
    if self.is_available and self.run is not None:
        wandb.log(metrics, step=step)
log_artifact
log_artifact(
    file_path, artifact_name, artifact_type="model"
)

Upload checkpoint file or artifact to W&B.

Parameters:

Name Type Description Default
file_path str

Local file path to upload.

required
artifact_name str

W&B artifact name identifier.

required
artifact_type str

Artifact category ('model', 'dataset', 'checkpoint').

'model'
Source code in src/tensoris/lib/integrations/wandb.py
def log_artifact(
    self, file_path: str, artifact_name: str, artifact_type: str = "model"
) -> None:
    """Upload checkpoint file or artifact to W&B.

    Args:
        file_path: Local file path to upload.
        artifact_name: W&B artifact name identifier.
        artifact_type: Artifact category ('model', 'dataset', 'checkpoint').
    """
    if self.is_available and self.run is not None:
        artifact = wandb.Artifact(artifact_name, type=artifact_type)
        artifact.add_file(file_path)
        self.run.log_artifact(artifact)