工具 API¶
随机种子¶
种子策略的唯一来源。
Centralized random-seeding helpers — the single source of seeding policy.
Three deliberately distinct scopes:
- :func:
seed_basic— pythonrandom+numpy(+ optionaltorch/cuDNN). Used by non-HuggingFace code paths: the data distributor and the Word2Vec trainer. - :func:
seed_hf_global— the HuggingFace/transformersdeterministic block, run before theAcceleratoris constructed. - :func:
seed_accelerate—accelerateper-device seeding, run after theAcceleratoris constructed (device_specificneeds 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 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 |
True
|
deterministic
|
bool
|
When True (and |
True
|
seed_hf_global ¶
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 ¶
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 ¶
Welford's algorithm for iterative mean and variance calculation.
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
|
patience_counter
property
¶
Current patience counter (epochs without improvement).
update ¶
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. |
错误处理¶
log_exceptions_inclass ¶
Decorator factory returning a decorator that catches and logs exceptions.
The returned decorator:
- Logs the exception via the instance's logger attribute (
self.loggerby default). - 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'
|