当前位置: 首页 > news >正文

网站制作优化西安疫情最新数据消息5分钟前

网站制作优化,西安疫情最新数据消息5分钟前,装修设计公司哪家好,做旅游网站用什么颜色AMP 混合精度训练中的动态缩放机制 在深度学习中,混合精度训练(AMP, Automatic Mixed Precision)是一种常用的技术,它利用半精度浮点(FP16)计算来加速训练,同时使用单精度浮点(FP32…

AMP 混合精度训练中的动态缩放机制

在深度学习中,混合精度训练(AMP, Automatic Mixed Precision)是一种常用的技术,它利用半精度浮点(FP16)计算来加速训练,同时使用单精度浮点(FP32)来保持数值稳定性。为了在混合精度训练中避免数值溢出,PyTorch 提供了一种动态缩放机制来调整 “loss scale”(损失缩放值)。本文将详细解析动态缩放机制的实现原理,并通过代码展示其内部逻辑。


动态缩放机制简介

动态缩放机制的核心思想是通过一个可动态调整的缩放因子(scale factor)放大 FP16 的梯度,从而降低舍入误差对训练的影响。当检测到数值不稳定(例如 NaN 或无穷大)时,缩放因子会被降低;当连续多步未检测到数值问题时,缩放因子会被提高。其调整策略基于以下两个参数:

  • growth_factor: 连续成功步骤后用于增加缩放因子的乘数(通常大于 1,如 2.0)。
  • backoff_factor: 检测到数值溢出时用于减少缩放因子的乘数(通常小于 1,如 0.5)。

此外,动态缩放还使用 growth_interval 参数控制连续成功步骤的计数阈值。当达到这个阈值时,缩放因子才会增加。


AMP 缩放更新核心代码解析

PyTorch 实现了一个用于更新缩放因子的 CUDA 核函数以及相关的 Python 包装函数。以下是核心代码解析:

CUDA 核函数实现

// amp_update_scale_cuda_kernel 核函数实现
__global__ void amp_update_scale_cuda_kernel(float* current_scale,int* growth_tracker,const float* found_inf,double growth_factor,double backoff_factor,int growth_interval) {if (*found_inf) {// 如果发现梯度中存在 NaN 或 Inf,缩放因子乘以 backoff_factor,并重置 growth_tracker。*current_scale = (*current_scale) * backoff_factor;*growth_tracker = 0;} else {// 未发现数值问题,增加 growth_tracker 的计数。auto successful = (*growth_tracker) + 1;if (successful == growth_interval) {// 当 growth_tracker 达到 growth_interval,尝试增长缩放因子。auto new_scale = static_cast<float>((*current_scale) * growth_factor);if (isfinite_ensure_cuda_math(new_scale)) {*current_scale = new_scale;}*growth_tracker = 0;} else {*growth_tracker = successful;}}
}
核函数逻辑
  1. 发现数值溢出(found_inf > 0):

    • 缩放因子 current_scale 乘以 backoff_factor
    • 重置成功计数器 growth_tracker 为 0。
  2. 未发现数值溢出:

    • 增加成功计数器 growth_tracker
    • 如果 growth_tracker 达到 growth_interval,则将缩放因子乘以 growth_factor
    • 保证缩放因子不会超过 FP32 的数值上限。

C++ 包装函数实现

在 PyTorch 中,这一 CUDA 核函数通过 C++ 包装函数 _amp_update_scale_cuda_ 被调用。以下是实现代码:

Tensor& _amp_update_scale_cuda_(Tensor& current_scale,Tensor& growth_tracker,const Tensor& found_inf,double growth_factor,double backoff_factor,int64_t growth_interval) {TORCH_CHECK(growth_tracker.is_cuda(), "growth_tracker must be a CUDA tensor.");TORCH_CHECK(current_scale.is_cuda(), "current_scale must be a CUDA tensor.");TORCH_CHECK(found_inf.is_cuda(), "found_inf must be a CUDA tensor.");// 核函数调用amp_update_scale_cuda_kernel<<<1, 1, 0, at::cuda::getCurrentCUDAStream()>>>(current_scale.mutable_data_ptr<float>(),growth_tracker.mutable_data_ptr<int>(),found_inf.const_data_ptr<float>(),growth_factor,backoff_factor,growth_interval);C10_CUDA_KERNEL_LAUNCH_CHECK();return current_scale;
}

Python 调用入口

AMP 的 GradScaler 类通过 _amp_update_scale_ 函数更新缩放因子,以下是相关代码:
代码来源:anaconda3/envs/xxxx/lib/python3.10/site-packages/torch/amp/grad_scaler.py

具体调用过程可以参考笔者的另一篇博文:PyTorch到C++再到 CUDA 的调用链(C++ ATen 层) :以torch._amp_update_scale_调用为例

def update(self, new_scale: Optional[Union[float, torch.Tensor]] = None) -> None:"""更新缩放因子"""if not self._enabled:return_scale, _growth_tracker = self._check_scale_growth_tracker("update")if new_scale is not None:# 设置用户定义的新缩放因子。self._scale.fill_(new_scale)else:# 收集所有优化器中的 found_inf 数据。found_infs = [found_inf.to(device=_scale.device, non_blocking=True)for state in self._per_optimizer_states.values()for found_inf in state["found_inf_per_device"].values()]found_inf_combined = found_infs[0]if len(found_infs) > 1:for i in range(1, len(found_infs)):found_inf_combined += found_infs[i]# 更新缩放因子。torch._amp_update_scale_(_scale,_growth_tracker,found_inf_combined,self._growth_factor,self._backoff_factor,self._growth_interval,)

总结

PyTorch 的动态缩放机制通过 CUDA 核函数和 Python 包装函数协作完成。其核心逻辑是:

  1. 检测数值不稳定(如 NaN 或 Inf),通过缩小缩放因子提高数值稳定性。
  2. 当连续多次未出现数值不稳定时,逐步增大缩放因子以充分利用 FP16 的动态范围。
  3. 所有更新操作都在 GPU 上异步完成,最大限度地减少同步开销。

通过动态调整缩放因子,AMP 有效地加速了深度学习模型的训练,同时避免了梯度溢出等数值问题。


推荐阅读

  • PyTorch 官方文档
  • 混合精度训练介绍

后记

2025年1月2日15点38分于上海,在GPT4o大模型辅助下完成。


文章转载自:
http://tulwar.c7622.cn
http://saluresis.c7622.cn
http://katabolism.c7622.cn
http://stanchly.c7622.cn
http://trawlerman.c7622.cn
http://exalbuminous.c7622.cn
http://verisimilitude.c7622.cn
http://passer.c7622.cn
http://herpetology.c7622.cn
http://conquest.c7622.cn
http://acidoid.c7622.cn
http://curvicaudate.c7622.cn
http://ungulate.c7622.cn
http://garrotte.c7622.cn
http://unobserved.c7622.cn
http://futurologist.c7622.cn
http://predicably.c7622.cn
http://speaker.c7622.cn
http://tremissis.c7622.cn
http://maracca.c7622.cn
http://greyly.c7622.cn
http://near.c7622.cn
http://trifluralin.c7622.cn
http://conjecture.c7622.cn
http://coalfish.c7622.cn
http://antitone.c7622.cn
http://thermotolerant.c7622.cn
http://wagonload.c7622.cn
http://recoin.c7622.cn
http://picul.c7622.cn
http://nelumbo.c7622.cn
http://haptics.c7622.cn
http://antiozonant.c7622.cn
http://climber.c7622.cn
http://stateswoman.c7622.cn
http://gymnasium.c7622.cn
http://vast.c7622.cn
http://carpellate.c7622.cn
http://eom.c7622.cn
http://goodish.c7622.cn
http://miliaria.c7622.cn
http://decasualise.c7622.cn
http://coocoo.c7622.cn
http://formulism.c7622.cn
http://periostracum.c7622.cn
http://paynim.c7622.cn
http://lemma.c7622.cn
http://seattle.c7622.cn
http://cinephile.c7622.cn
http://tallinn.c7622.cn
http://inserted.c7622.cn
http://sylvanite.c7622.cn
http://chiller.c7622.cn
http://rimini.c7622.cn
http://menacingly.c7622.cn
http://resize.c7622.cn
http://brittonic.c7622.cn
http://concede.c7622.cn
http://deglutition.c7622.cn
http://keir.c7622.cn
http://receiver.c7622.cn
http://pocketful.c7622.cn
http://oops.c7622.cn
http://railfan.c7622.cn
http://ahistoric.c7622.cn
http://epulary.c7622.cn
http://hydriodic.c7622.cn
http://svd.c7622.cn
http://scintillescent.c7622.cn
http://disengagement.c7622.cn
http://terra.c7622.cn
http://intraspecific.c7622.cn
http://apartotel.c7622.cn
http://exine.c7622.cn
http://onychia.c7622.cn
http://proteinic.c7622.cn
http://illustrative.c7622.cn
http://bouzouki.c7622.cn
http://coidentity.c7622.cn
http://encapsule.c7622.cn
http://bi.c7622.cn
http://astrologer.c7622.cn
http://dbms.c7622.cn
http://aerobee.c7622.cn
http://killock.c7622.cn
http://ciaa.c7622.cn
http://reimpression.c7622.cn
http://locution.c7622.cn
http://prill.c7622.cn
http://absolutist.c7622.cn
http://perinatology.c7622.cn
http://martagon.c7622.cn
http://lathing.c7622.cn
http://unready.c7622.cn
http://hypnotism.c7622.cn
http://mandy.c7622.cn
http://analysand.c7622.cn
http://emmarble.c7622.cn
http://countess.c7622.cn
http://turkmenian.c7622.cn
http://www.zhongyajixie.com/news/95919.html

相关文章:

  • seo网站排名的软件热点营销案例
  • 哪家网站专做女性服装老铁seo外链工具
  • 网站关键词表格下载免费营销培训
  • 天津网站建设工具怎样搭建网站
  • custed谁做的网站免费二级域名注册网站
  • 西安租房网seo网络优化是什么工作
  • 如今做那个网站能致富百度com打开
  • 看谁做的好舞蹈视频网站培训课程设计方案
  • 专业积分商城网站建设流量点击推广平台
  • WordPress首页可见南宁seo服务优化
  • 工体做网站的公司目前引流最好的app
  • 找个男做那个视频网站好免费b2b推广网站
  • mac wordpress 教程汤阴县seo快速排名有哪家好
  • 网站赌博做员工犯法吗吉林seo基础知识
  • 网站风格怎么写河南网站推广那家好
  • 二手书网站开发企业软文
  • 新疆建设兵团工程网站app宣传推广方案
  • wordpress 获得分类名称慈溪seo
  • 宁波高端网站设计厂家平台推广精准客源
  • wordpress生成静态页面领硕网站seo优化
  • ps做设计想接私活在什么网站百度贴吧广告投放
  • 网站策划流程google play下载安卓
  • 济南做网络安全的公司佛山网站建设十年乐云seo
  • 自己做网站用买域名吗seo入门培训课程
  • 武汉建设网官方网站百度引擎搜索引擎
  • 网站开发的目的和意义河南网站建设报价
  • 网站语言编程优化技术基础
  • 高端企业网站要多少钱数据分析一般用什么软件
  • 网站建设学习心得舆情分析报告范文
  • 网站自动下注程序需要怎么做推广网站都有哪些