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

Mixed-precision计算原理(FP32+FP16)

原文:

https://lightning.ai/pages/community/tutorial/accelerating-large-language-models-with-mixed-precision-techniques/

This approach allows for efficient training while maintaining the accuracy and stability of the neural network.

In more detail, the steps are as follows.

  1. Convert weights to FP16: In this step, the weights (or parameters) of the neural network, which are initially in FP32 format, are converted to lower-precision FP16 format. This reduces the memory footprint and allows for faster computation, as FP16 operations require less memory and can be processed more quickly by the hardware.
  2. Compute gradients: The forward and backward passes of the neural network are performed using the lower-precision FP16 weights. This step calculates the gradients (partial derivatives) of the loss function with respect to the network’s weights, which are used to update the weights during the optimization process.
  3. Convert gradients to FP32: After computing the gradients in FP16, they are converted back to the higher-precision FP32 format. This conversion is essential for maintaining numerical stability and avoiding issues such as vanishing or exploding gradients that can occur when using lower-precision arithmetic.
  4. Multiply by learning rate and update weights: Now in FP32 format, the gradients are multiplied by a learning rate (a scalar value that determines the step size during optimization).
  5. The product from step 4 is then used to update the original FP32 neural network weights. The learning rate helps control the convergence of the optimization process and is crucial for achieving good performance.

简而言之:

g * lr + w老 --> w新,这里的g、w老、w新,都是FP32的;

其余计算梯度中的w、activation、gradient等,全部都是FP16的;

训练效果:

耗时缩减为FP32的1/2 ~ 1/3

显存变化不大(因为,增加显存:weight多专一份FP16,减少显存:forward时保存的activation变成FP16了,二者基本抵消)

推理效果:

显存减少一半;耗时缩减为FP32的1/2;

使用FP16后的test accuracy反而上升,解释:(正则效应,带来噪音,帮助模型泛化得更好,减少过拟合)

A likely explanation is that this is due to regularizing effects of using a lower precision. Lower precision may introduce some level of noise in the training process, which can help the model generalize better and reduce overfitting, potentially leading to higher accuracy on the validation and test sets.

bf16,指数位增加,所以能覆盖更大的数值范围,所以能使训练过程更鲁棒,减少overflow和underflow的出现概率;

相关文章:

Mixed-precision计算原理(FP32+FP16)

原文: https://lightning.ai/pages/community/tutorial/accelerating-large-language-models-with-mixed-precision-techniques/ This approach allows for efficient training while maintaining the accuracy and stability of the neural network. In more det…...

Go 控制协程(goroutine)的并发数量

在使用协程并发处理某些任务时, 其并发数量往往因为各种因素的限制不能无限的增大. 例如网络请求、数据库查询等等。 从运行效率角度考虑,在相关服务可以负载的前提下(限制最大并发数),尽可能高的并发。 在Go语言中,…...

web安全渗透测试十大常规项(一):web渗透测试之CSRF跨站请求伪造

渗透测试之CSRF跨站请求伪造 CSRF跨站请求伪造 CSRF跨站请求伪造...

YOLOv10尝鲜测试五分钟极简配置

最近清华大学团队又推出YOLOv10,真是好家伙了。 安装: pip install supervision githttps://github.com/THU-MIG/yolov10.git下载权重:https://github.com/THU-MIG/yolov10/releases/download/v1.0/yolov10n.pt 预测: from ult…...

社交媒体数据恢复:聊天宝

请注意,本教程仅针对聊天宝应用程序,而非其他聊天软件。以下是详细的步骤: 首先,请确保您已经登录了聊天宝应用程序。如果您尚未登录,请使用您的账号登录。 在聊天宝主界面,找到您希望恢复聊天记录的对话框…...

备战秋招—模拟版图面试题来了

随着暑期的脚步逐渐临近,电子工程和集成电路设计领域的毕业生们,也将迎来了另一个求职的黄金期——秋招。我们总说机会是留给有准备的人。对于有志于投身于模拟版图设计的学子们来说,为了在众多求职者中脱颖而出,充分备战模拟版图…...

CAN总线简介

1. CAN总线概述 1.1 CAN定义与历史背景 CAN,全称为Controller Area Network,是一种基于消息广播的串行通信协议。它最初由德国Bosch公司在1983年为汽车行业开发,目的是实现汽车内部电子控制单元(ECUs)之间的可靠通信。…...

【HSQL001】HiveSQL内置函数手册总结(更新中)

1.熟悉、梳理、总结下Hive SQL相关知识体系。 2.日常研发过程中使用较少,随着时间的推移,很快就忘得一干二净,所以梳理总结下,以备日常使用参考 3.欢迎批评指正,跪谢一键三连! 文章目录 1.函数清单 1.函数清…...

Rust面试宝典第14题:旋转数组

题目 给定一个数组,将数组中的元素向右移动k个位置,其中k是非负数。要求如下: (1)尽可能想出更多的解决方案,至少有三种不同的方法可以解决这个问题。 (2)使用时间复杂度为O(n)和空间…...

解决SpringBoot中插入汉字变成?(一秒解决)

在这里url后面加一行配置即可&useUnicodetrue&characterEncodingUTF-8即可 解释 spring.datasource.url: 这里包含了数据库的URL,以及额外的参数如useUnicodetrue用于启用Unicode字符集支持,characterEncodingUTF-8用于指定字符编码为UTF-8&…...

5.26牛客循环结构

1002. 难点: 两层循环条件设置 思路 可以设置三个变量 代码 1003 思路: 与星号双塔差不多,在此基础上加大一点难度 每日练题5.23 (EOF用法)-CSDN博客 代码 1004 代码...

AIGC 004-T2I-adapter另外一种支持多条件组合控制的文生图方案!

AIGC 004-T2I-adapter另外一种支持多条件组合控制的文生图方案! 文章目录 0 论文工作1 论文方法2 效果 0 论文工作 T2I-Adapter 论文提出了一种名为 T2I-Adapter 的轻量级适配器模块,旨在增强文本到图像 (T2I) 扩散模型的语义理解和生成能力。 论文指出…...

详解 Cookies 和 WebStorage

Cookies 和 WebStorage Cookies 和 WebStorageCookies简要介绍操作 Cookies(document.cookie)不足之处 WebStorage简要介绍LocalStorage Vs. SessionStorage操作 WebStorage 三种数据存储方式的对比分析共性差异 REFERENCES Cookies 和 WebStorage Cook…...

BeanFactory、FactroyBean、ApplicationContext

BeanFactory Ioc容器、定义接口规范来管理spring bean的生命周期、依赖、注入,spring中有各种Ioc容器 FactroyBean 定制的工厂Bean,可以通过抽象工厂方式创建的bean,不纳入spring的生命周期、依赖、注入特性,相当于spring给第三…...

【计算机网络】HTTPS 协议原理

加密 1. 加密概念 加密就是把明文 (要传输的信息)进行一系列变换,生成密文。 解密就是把密文再进行一系列变换,还原成明文。 在这个加密和解密的过程中,往往需要⼀个或者多个中间的数据,辅助进行这个过程,这样的数…...

springboot + Vue前后端项目(第十二记)

项目实战第十二记 1.写在前面2. 整合Echarts2.1 vue安装Echarts2.2 使用Echarts2.3 EchartsController编写2.4 Home.vue编写 总结写在最后 1.写在前面 本篇主要讲解系统整合Echarts 2. 整合Echarts 2.1 vue安装Echarts npm i echarts -S2.2 使用Echarts vue中使用echarts的…...

linux 常用命令:find grep ps netstat sudo df du rm

rm 命令 删除 -r 是递归参数(recursive),用于删除目录及其内容。如果不加这个参数,rm 命令无法删除非空目录。-f 是强制参数(force),用于强制删除文件或目录,不会进行任何确认提示…...

SQLiteOpenHelper数据库帮助器

SQLiteOpenHelper数据库帮助器是Android提供的数据库辅助工具。 1、继承SQLiteOpenHelper类,需要重写onCreate和onUpgrade两个方法 案例:实现增删改查 package com.example.databases_text;import android.app.PictureInPictureParams; import androi…...

2024年5月26日 (周日) 叶子游戏新闻

资深开发者:3A游戏当前处于一种尴尬的中间地带游戏行业整体,尤其是3A游戏正处于艰难时期。尽管2023年3A游戏佳作频出,广受好评,但居高不下的游戏开发成本(传闻《漫威蜘蛛侠2》的制作成本高达3亿美元)正严重…...

STM32-10-定时器

STM32-01-认识单片机 STM32-02-基础知识 STM32-03-HAL库 STM32-04-时钟树 STM32-05-SYSTEM文件夹 STM32-06-GPIO STM32-07-外部中断 STM32-08-串口 STM32-09-IWDG和WWDG 文章目录 一、STM32 基础定时器1. 基本定时器简介2. 基本定时器框图3. 基本定时器相关寄存器4. 定时器溢出…...

IDEA运行Tomcat出现乱码问题解决汇总

最近正值期末周,有很多同学在写期末Java web作业时,运行tomcat出现乱码问题,经过多次解决与研究,我做了如下整理: 原因: IDEA本身编码与tomcat的编码与Windows编码不同导致,Windows 系统控制台…...

376. Wiggle Subsequence

376. Wiggle Subsequence 代码 class Solution { public:int wiggleMaxLength(vector<int>& nums) {int n nums.size();int res 1;int prediff 0;int curdiff 0;for(int i 0;i < n-1;i){curdiff nums[i1] - nums[i];if( (prediff > 0 && curdif…...

【Go】3、Go语言进阶与依赖管理

前言 本系列文章参考自稀土掘金上的 【字节内部课】公开课&#xff0c;做自我学习总结整理。 Go语言并发编程 Go语言原生支持并发编程&#xff0c;它的核心机制是 Goroutine 协程、Channel 通道&#xff0c;并基于CSP&#xff08;Communicating Sequential Processes&#xff0…...

IoT/HCIP实验-3/LiteOS操作系统内核实验(任务、内存、信号量、CMSIS..)

文章目录 概述HelloWorld 工程C/C配置编译器主配置Makefile脚本烧录器主配置运行结果程序调用栈 任务管理实验实验结果osal 系统适配层osal_task_create 其他实验实验源码内存管理实验互斥锁实验信号量实验 CMISIS接口实验还是得JlINKCMSIS 简介LiteOS->CMSIS任务间消息交互…...

Go 并发编程基础:通道(Channel)的使用

在 Go 中&#xff0c;Channel 是 Goroutine 之间通信的核心机制。它提供了一个线程安全的通信方式&#xff0c;用于在多个 Goroutine 之间传递数据&#xff0c;从而实现高效的并发编程。 本章将介绍 Channel 的基本概念、用法、缓冲、关闭机制以及 select 的使用。 一、Channel…...

NPOI Excel用OLE对象的形式插入文件附件以及插入图片

static void Main(string[] args) {XlsWithObjData();Console.WriteLine("输出完成"); }static void XlsWithObjData() {// 创建工作簿和单元格,只有HSSFWorkbook,XSSFWorkbook不可以HSSFWorkbook workbook new HSSFWorkbook();HSSFSheet sheet (HSSFSheet)workboo…...

深入理解Optional:处理空指针异常

1. 使用Optional处理可能为空的集合 在Java开发中&#xff0c;集合判空是一个常见但容易出错的场景。传统方式虽然可行&#xff0c;但存在一些潜在问题&#xff1a; // 传统判空方式 if (!CollectionUtils.isEmpty(userInfoList)) {for (UserInfo userInfo : userInfoList) {…...

Spring Security 认证流程——补充

一、认证流程概述 Spring Security 的认证流程基于 过滤器链&#xff08;Filter Chain&#xff09;&#xff0c;核心组件包括 UsernamePasswordAuthenticationFilter、AuthenticationManager、UserDetailsService 等。整个流程可分为以下步骤&#xff1a; 用户提交登录请求拦…...

git: early EOF

macOS报错&#xff1a; Initialized empty Git repository in /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/.git/ remote: Enumerating objects: 2691797, done. remote: Counting objects: 100% (1760/1760), done. remote: Compressing objects: 100% (636/636…...

c# 局部函数 定义、功能与示例

C# 局部函数&#xff1a;定义、功能与示例 1. 定义与功能 局部函数&#xff08;Local Function&#xff09;是嵌套在另一个方法内部的私有方法&#xff0c;仅在包含它的方法内可见。 • 作用&#xff1a;封装仅用于当前方法的逻辑&#xff0c;避免污染类作用域&#xff0c;提升…...