人工智能深度学习机器学习预训练分布式训练微调【免费下载链接】pytorch-lightningPretrain, finetune ANY AI model of ANY size on 1 or 10,000 GPUs with zero code changes.项目地址https://gitcode.com/gh_mirrors/py/pytorch-lightning点击查看免费下载本指南基于官方升级文档 docs/source-pytorch/upgrade/sections/1_9_regular.rst系统梳理从 Lightning 1.8 及更早版本升级到 1.9 时面向普通用户regular user的全部破坏性变更breaking changes覆盖 Python/PyTorch 最低版本要求、设备相关 Trainer 参数的统一收敛、断点续训入口的迁移、梯度累积调度器化、profiler 导入路径修正以及 Tuner 机制的全面重构。读完本文你将能对照自己的训练脚本逐项完成 1.9 迁移并在当前仓库源码src/lightning/pytorch中找到每一项变更的底层实现作为验证依据。1.9 版本升级总览为什么需要这份清单Lightning 1.9 是一次以“统一设备抽象”和“回调化内置能力”为主题的版本。它将散落在Trainer构造函数中的多个设备相关标志gpus、tpu_cores、ipus、num_processes收敛为单一的devices参数同时把auto_lr_find、auto_scale_batch_size、accumulate_grad_batches字典调度形式等内置于 Trainer 的功能迁移为显式的回调Callback与独立对象为后续 2.0 的清理工作铺路。迁移路径可分为四类运行环境升级Python 与 PyTorch 的最低版本要求提高参数重命名/收敛设备相关 Trainer 标志统一为devices入口迁移断点续训从 Trainer 构造参数改为Trainer.fit(ckpt_path...)能力外置调参Tuner与梯度累积调度从 Trainer 内置逻辑迁往独立回调。以下各节将逐条给出“原写法 → 新写法”的对照并附带源码级佐证。运行环境要求Python 3.8 与 PyTorch 1.11升级文档首先明确了 1.9 的最低运行环境原条件升级要求使用 Python 3.7升级到 Python 3.8 或更高版本使用 PyTorch 1.10升级到 PyTorch 1.11 或更高版本这意味着 1.9 起项目正式放弃对 Python 3.7 与 PyTorch 1.10 的支持。如果你的训练环境仍停留在这两个版本需要先升级解释器与深度学习框架再执行本指南后续的代码迁移。当前仓库的版本信息可在 src/lightning/version.py 与 src/lightning/about.py 中核对依赖约束可参考 requirements/pytorch/base.txt。设备参数统一gpus/tpu_cores/ipus/num_processes→devices变更内容这是 1.9 中最核心的破坏性变更以下四个 Trainer 标志被整体移除统一由devices承担旧写法已废弃新写法语义Trainer(gpus2)Trainer(devices2)使用 2 个 GPUTrainer(tpu_cores8)Trainer(devices8)使用 8 个 TPU 核心Trainer(ipus4)Trainer(devices4)使用 4 个 IPUTrainer(num_processes2)Trainer(devices2)使用 2 个进程devices同时支持整数、列表与字符串例如devices[0, 1]指定 GPU 索引、devices2指定数量并可与accelerator组合出完整的硬件/策略配置。源码验证在当前仓库中Trainer.__init__的参数列表只保留了accelerator、strategy、devices、num_nodes等不再存在gpus、tpu_cores、ipus、num_processes参数见 src/lightning/pytorch/trainer/trainer.py。设备配置的解析逻辑集中在连接器 src/lightning/pytorch/trainer/connectors/accelerator_connector.pydevices支持list[int]、str、int三种类型默认值为auto_check_device_config_and_set_final_flags负责校验devices取值[]、0、0等会被视为无效输入并抛出MisconfigurationException见 accelerator_connector.py_set_devices_flag_if_auto_passed处理devicesauto在交互式/笔记本环境中自动回退到 1 个设备否则按accelerator.auto_device_count()自动探测可用设备数量见 accelerator_connector.py。auto_select_gpus与pick_single_gpu/pick_multiple_gpus统一为devicesauto变更内容旧写法新写法Trainer(auto_select_gpusTrue)Trainer(devicesauto)pl.tuner.auto_gpu_select.pick_single_gpu(...)Trainer(devicesauto)pl.tuner.auto_gpu_select.pick_multiple_gpus(...)Trainer(devicesauto)在 1.9 中自动 GPU 选择不再作为 Trainer 的独立开关或独立工具函数存在而是成为devicesauto的默认行为Trainer 会自动探测并选择可用的设备。上面_set_devices_flag_if_auto_passed的实现正好印证了这一点——auto会触发accelerator.auto_device_count()自动探测逻辑交互式环境则安全回退到单设备。断点续训resume_from_checkpoint→Trainer.fit(ckpt_path...)变更内容旧写法trainer Trainer(resume_from_checkpointpath/to/checkpoint.ckpt) trainer.fit(model)新写法trainer Trainer() trainer.fit(model, ckpt_pathpath/to/checkpoint.ckpt)resume_from_checkpoint从 Trainer 构造参数中移除改为fit方法的ckpt_path参数从而支持在同一次运行中对不同 checkpoint 进行多次fit调用。源码验证当前Trainer.fit的签名中包含了ckpt_path: Optional[_PATH] None参数见 trainer.py其 docstring 说明ckpt_path接受路径/URL还支持三个特殊关键字best加载上一次fit的最佳模型 checkpoint、last加载最近一次 checkpoint以及registry从 Lightning Model Registry 下载模型当路径上不存在 checkpoint 文件时会抛出异常见 trainer.py。迁移后的典型用法# 续训最近一次 checkpoint trainer.fit(model, ckpt_pathlast) # 续训指定路径的 checkpoint trainer.fit(model, ckpt_pathpath/to/epoch5-step1000.ckpt)梯度累积调度accumulate_grad_batches字典→GradientAccumulationScheduler回调变更内容旧写法允许把accumulate_grad_batches设为调度字典按 epoch 切换累积步数trainer Trainer(accumulate_grad_batches{5: 2, 10: 4})1.9 起这种字典调度形式从 Trainer 中移除必须显式使用GradientAccumulationScheduler回调from lightning.pytorch import Trainer from lightning.pytorch.callbacks import GradientAccumulationScheduler accumulator GradientAccumulationScheduler(scheduling{4: 2, 9: 4}) # epoch 键从 0 开始计数 trainer Trainer(callbacks[accumulator])源码验证GradientAccumulationScheduler的实现位于 src/lightning/pytorch/callbacks/gradient_accumulation_scheduler.pyscheduling为{epoch: accumulation_factor}格式epoch 键是零索引的——若想在 4 个 epoch 之后切换累积因子需写{4: factor}构造时校验空字典抛TypeError键必须是非负整数因子必须是大于 0 的整数若用户未定义第 0 个 epoch 的因子会自动补上{0: 1}见 gradient_accumulation_scheduler.pyon_train_epoch_start钩子在每个 epoch 开始时根据trainer.current_epoch查询get_accumulate_grad_batches更新trainer.accumulate_grad_batches见 gradient_accumulation_scheduler.pyon_train_start中会做兼容性检查手动优化automatic_optimizationFalse、DeepSpeed 策略、以及同时设置Trainer(accumulate_grad_batches...)与回调都会触发报错或警告见 gradient_accumulation_scheduler.py。需要说明的是Trainer(accumulate_grad_batchesN)的整数值固定累积写法在 1.9 中依然保留见 trainer.py仅字典调度形式迁移到了回调。profiler 导入路径pl.profiler→pl.profilers变更内容旧写法已废弃新写法from lightning.pytorch.profiler import PyTorchProfilerfrom lightning.pytorch.profilers import PyTorchProfiler模块名从单数profiler统一为复数profilers。当前仓库中 profiler 的公共导出集中在 src/lightning/pytorch/profilers/init.py其中包含Profiler基类PassThroughProfiler空实现SimpleProfiler简单计时AdvancedProfiler基于 PyTorch autograd 的深度分析PyTorchProfiler基于torch.profiler的分析器XLAProfiler面向 XLA/TPU 的分析器这些类的实现分别位于 src/lightning/pytorch/profilers/ 目录下的simple.py、advanced.py、pytorch.py、xla.py、base.py。迁移时只需同步修改 import 语句即可。Tuner 机制重构从 Trainer 内置到独立对象与回调变更内容1.9 中 Tuner 经历了最大幅度的重构旧写法已废弃新写法以任何形式将Tuner作为Trainer的一部分使用使用独立的Tuner对象或直接使用LearningRateFinder、BatchSizeFinder回调Trainer(auto_scale_batch_sizeTrue)使用BatchSizeFinder回调Trainer.tune()方法已移除Trainer(auto_lr_findTrue)使用LearningRateFinder回调Trainer.tune()方法已移除新写法一独立 Tuner 对象Tuner现在是独立类构造时接收 Trainer并提供scale_batch_size()与lr_find()两个方法from lightning.pytorch.tuner import Tuner trainer Trainer() tuner Tuner(trainer) # 自动寻找合适的 batch size new_batch_size tuner.scale_batch_size(model, train_dataloaderstrain_dataloaders) # 自动寻找合适的初始学习率 lr_finder tuner.lr_find(model, train_dataloaderstrain_dataloaders) print(lr_finder.results) # 学习率-损失曲线 print(lr_finder.suggestion()) # 建议的初始学习率其实现位于 src/lightning/pytorch/tuner/tuning.pyscale_batch_size支持modepower每次将 batch size 乘 2直到出现 OOM与modebinsearch倍增遇到 OOM 后二分搜索另有steps_per_trial3、init_val2、max_trials25、max_val8192等参数见 tuning.pylr_find支持min_lr1e-8、max_lr1、num_training100、modeexponential/linear、early_stop_threshold4.0等参数method仅允许fit见 tuning.py内部实现上两个方法分别实例化BatchSizeFinder与LearningRateFinder回调并注入 trainer.callbacks设置_early_exit True提前退出运行结束后返回最优结果见 tuning.py 与 tuning.py。新写法二直接使用回调更符合 Lightning 回调哲学的用法是直接把两个 Finder 回调加入 Trainerfrom lightning.pytorch import Trainer from lightning.pytorch.callbacks import BatchSizeFinder, LearningRateFinder trainer Trainer( callbacks[ BatchSizeFinder(modepower), # 每个 epoch 开头尝试增大 batch size LearningRateFinder(min_lr1e-6, max_lr1e-2), # 训练开始前运行 LR 范围测试 ], ) trainer.fit(model)两个回调的参数与独立 Tuner 完全对齐BatchSizeFinder(modepower, steps_per_trial3, init_val2, max_trials25, batch_arg_namebatch_size, margin0.05, max_val8192)构造时校验mode与margin取值并检查batch_arg_name指定的属性必须存在于模型、datamodule 或其hparams中见 src/lightning/pytorch/callbacks/batch_size_finder.pyLearningRateFinder(min_lr1e-8, max_lr1, num_training_steps100, modeexponential, early_stop_threshold4.0, update_attrTrue, attr_name)搜索过程在isolate_rng()中运行以保证随机数种子隔离结果保存在self.optimal_lr见 src/lightning/pytorch/callbacks/lr_finder.py。注意事项Tuner的lr_find在 Trainer 已配置LearningRateFinder回调时会报错见 tuning.pyscale_batch_size同理且不支持分布式策略见 tuning.pyBatchSizeFinder回调同样不支持分布式策略且不能与直接传给.fit()的 dataloader 配合使用见 batch_size_finder.py。完整迁移对照表将本指南全部变更汇总如下供升级时逐项核对#旧写法1.8 及以前新写法1.9 起迁移要点1Python 3.7Python 3.8升级解释器2PyTorch 1.10PyTorch 1.11升级框架3Trainer(gpus2)Trainer(devices2)统一设备参数4Trainer(tpu_cores8)Trainer(devices8)统一设备参数5Trainer(ipus4)Trainer(devices4)统一设备参数6Trainer(num_processes2)Trainer(devices2)统一设备参数7Trainer(resume_from_checkpoint...)Trainer.fit(ckpt_path...)断点续训入口迁移8Trainer(auto_select_gpusTrue)Trainer(devicesauto)自动选卡内建9pl.tuner.auto_gpu_select.pick_single_gpuTrainer(devicesauto)函数移除10pl.tuner.auto_gpu_select.pick_multiple_gpusTrainer(devicesauto)函数移除11Trainer(accumulate_grad_batches{epoch: factor})GradientAccumulationScheduler(scheduling{...})字典调度回调化12from pl.profiler import ...from pl.profilers import ...模块名复数化13Trainer(auto_scale_batch_sizeTrue)BatchSizeFinder回调 / 独立TunerTrainer.tune()移除14Trainer(auto_lr_findTrue)LearningRateFinder回调 / 独立TunerTrainer.tune()移除迁移后的自检清单升级完成后建议按以下步骤验证迁移正确性环境检查确认 Python ≥ 3.8、PyTorch ≥ 1.11可通过python -c import sys, torch; print(sys.version, torch.__version__)快速核对搜索残留旧 API在代码库中检索gpus、tpu_cores、ipus、num_processes、resume_from_checkpoint、auto_select_gpus、auto_scale_batch_size、auto_lr_find、profiler单数 import等字符串逐一替换为对应新写法运行测试仓库在 tests/tests_pytorch/ 目录下提供了大量覆盖上述机制的测试用例例如 tuner 相关测试见 tests/tests_pytorch/tuner/test_lr_finder.py 与 tests/tests_pytorch/tuner/test_scale_batch_size.py迁移后可参照这些用例验证行为一致性。本文所有变更项均可在当前仓库源码中找到对应实现升级文档原文位于 docs/source-pytorch/upgrade/sections/1_9_regular.rst进一步的版本迁移说明可参考 docs/source-pytorch/upgrade/migration_guide.rst 与 docs/source-pytorch/upgrade/from_1_9.rst。赞分享人工智能深度学习机器学习预训练分布式训练微调【免费下载链接】pytorch-lightningPretrain, finetune ANY AI model of ANY size on 1 or 10,000 GPUs with zero code changes.项目地址https://gitcode.com/gh_mirrors/py/pytorch-lightning点击查看免费下载相关推荐PyTorch Lightning 1.6 高级用户 API 迁移指南从 Trainer 参数到回调与策略的全面重构PyTorch Lightning 1.6 高级用户 API 迁移指南从 Trainer 参数到回调与策略的全面重构 本文基于 docs/source pyt人工智能深度学习机器学习预训练分布式训练微调MongoDB Memory Server 版本迁移终极指南从旧版到v11.0.0的完整升级路径MongoDB Memory Server 版本迁移终极指南从旧版到v11.0.0的完整升级路径 MongoDB Memory Server 是一款轻量级的内google-cloud-go版本升级指南从旧版本迁移到最新API的最佳路径google cloud go版本升级指南从旧版本迁移到最新API的最佳路径 还在为Google Cloud Go客户端库的版本升级而头疼吗每次升级都担心破后端云原生创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考