跳转至

工具 API

随机种子

种子策略的唯一来源。

Centralized random-seeding helpers — the single source of seeding policy.

Three deliberately distinct scopes:

  • :func:seed_basic — python random + numpy (+ optional torch/cuDNN). Used by non-HuggingFace code paths: the data distributor and the Word2Vec trainer.
  • :func:seed_hf_global — the HuggingFace/transformers deterministic block, run before the Accelerator is constructed.
  • :func:seed_accelerateaccelerate per-device seeding, run after the Accelerator is constructed (device_specific needs the process index).

transformers/accelerate are imported lazily inside the functions so that importing this module stays cheap on CPU-only / no-HF environments (see C-H1).

seed_basic

seed_basic(seed: int, *, include_torch: bool = True, deterministic: bool = True) -> None

Seed python random, numpy and (optionally) torch + cuDNN.

Parameters:

Name Type Description Default
seed int

The seed value.

required
include_torch bool

When True, also seed torch (CPU + all CUDA devices). Set False for numpy/sklearn-only paths that never touch torch RNGs.

True
deterministic bool

When True (and include_torch), force cuDNN into deterministic, non-benchmarking mode.

True

seed_hf_global

seed_hf_global(seed: int) -> None

Deterministic global seeding for HuggingFace training, pre-Accelerator.

Seeds transformers (which covers python/numpy/torch), pins the hash and cuBLAS workspace, and forces cuDNN determinism. Call this before constructing the Accelerator; pair with :func:seed_accelerate after.

seed_accelerate

seed_accelerate(seed: int) -> None

Per-device accelerate seeding, run after the Accelerator exists.

device_specific=True offsets the seed by the process index, so the distributed state must already be initialized when this is called.

累加器

MeanVarianceTracker

MeanVarianceTracker()

Welford's algorithm for iterative mean and variance calculation.

MinMaxTracker

MinMaxTracker()

EarlyStopTracker

EarlyStopTracker(patience: int = 5, min_delta: float = 0.0, mode: str = 'min', relative: bool = True)

Early stopping tracker for training loops.

Monitors metric improvements and triggers stopping when patience is exhausted. Supports both absolute and relative improvement thresholds, and both minimization and maximization objectives.

Usage

tracker = EarlyStopTracker(patience=5, min_delta=0.001, mode='min', relative=True)

for epoch in range(max_epochs): loss = train_epoch()

should_stop = tracker.update(loss, epoch)

if tracker.improved:
    logger.info(f"Metric improved to {loss:.6f}")

if should_stop:
    logger.info(f"Early stopping at epoch {epoch}")
    break

Attributes:

Name Type Description
patience

Number of epochs without improvement before stopping.

min_delta

Minimum improvement threshold.

mode

'min' for loss (lower is better) or 'max' for accuracy (higher is better).

relative

If True, use relative improvement; if False, use absolute improvement.

Initialize early stopping tracker.

Parameters:

Name Type Description Default
patience int

Number of epochs without improvement before stopping.

5
min_delta float

Minimum improvement threshold (relative if relative=True, absolute otherwise).

0.0
mode str

'min' (lower is better, e.g., loss) or 'max' (higher is better, e.g., accuracy).

'min'
relative bool

If True, use relative improvement; if False, use absolute improvement.

True

best_metric property

best_metric: float

Best metric value observed so far.

best_epoch property

best_epoch: Optional[int]

Epoch index with best metric.

patience_counter property

patience_counter: int

Current patience counter (epochs without improvement).

improved property

improved: bool

Whether improvement was detected in last update.

improvement property

improvement: float

Improvement value from last update.

should_stop property

should_stop: bool

Whether early stopping should be triggered.

update

update(metric: float, epoch: int) -> bool

Update tracker with new metric value.

Parameters:

Name Type Description Default
metric float

Current metric value.

required
epoch int

Current epoch index.

required

Returns:

Type Description
bool

True if early stopping should be triggered (patience exhausted), False otherwise.

clear

clear() -> None

Reset tracker to initial state.

错误处理

log_exceptions_inclass

log_exceptions_inclass(logger_attr: str = 'logger')

Decorator factory returning a decorator that catches and logs exceptions.

The returned decorator:

  1. Logs the exception via the instance's logger attribute (self.logger by default).
  2. Also writes the exception to a timestamped file under logs/error/ using a dedicated ErrorLogger.

Parameters:

Name Type Description Default
logger_attr str

Name of the logger attribute on the instance. Defaults to "logger".

'logger'