- config: yaml `_base_` 继承 + 深度合并 + 命令行 `--set a.b.c=value` 覆盖,检测循环引用 - registry: 组件按名注册与构建,模型/数据集共用 - device: require_cuda() 守卫,按 CLAUDE.md 约定禁止在无 Nvidia GPU 的设备上训练; 评估与导出走 resolve_device() 允许 CPU - labels: multilabel / multiclass 两种图像级标签形式的共用定义 - logging / seed: 控制台+文件日志、随机种子与 cudnn 确定性 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
34 lines
743 B
Python
34 lines
743 B
Python
"""随机种子与 cudnn 确定性设置。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import random
|
|
|
|
|
|
def seed_everything(seed: int, deterministic: bool = False) -> int:
|
|
random.seed(seed)
|
|
os.environ["PYTHONHASHSEED"] = str(seed)
|
|
|
|
try:
|
|
import numpy as np
|
|
|
|
np.random.seed(seed)
|
|
except ImportError: # pragma: no cover
|
|
pass
|
|
|
|
try:
|
|
import torch
|
|
|
|
torch.manual_seed(seed)
|
|
torch.cuda.manual_seed_all(seed)
|
|
if deterministic:
|
|
torch.backends.cudnn.deterministic = True
|
|
torch.backends.cudnn.benchmark = False
|
|
else:
|
|
torch.backends.cudnn.benchmark = True
|
|
except ImportError: # pragma: no cover
|
|
pass
|
|
|
|
return seed
|