当前位置:网站首页>pytorch with Automatic Mixed Precision(AMP)
pytorch with Automatic Mixed Precision(AMP)
2022-06-09 05:32:00 【kaims】
Automatic Mixed Precision examples — PyTorch 1.9.1 documentation
torch.cuda.amp A more convenient hybrid accuracy training mechanism is provided :
user There is no need to manually adjust the model parameters dtype transformation ,amp It will automatically select the appropriate numerical precision for the operator
For back propagation ,FP16 Gradient numerical overflow problem ,amp Provides a gradient scaling operation , And before the optimizer updates the parameters , Will automatically adjust the gradient unscaling, therefore , It has no effect on the superparameters used for model optimization
The above two points , By using amp.autocast and amp.GradScaler To achieve .
basic
# Creates model and optimizer in default precision
model = Net().cuda()
optimizer = optim.SGD(model.parameters(), ...)
# Creates a GradScaler once at the beginning of training.
scaler = GradScaler()
for epoch in epochs:
for input, target in data:
optimizer.zero_grad()
# Runs the forward pass with autocasting.
with autocast():
output = model(input)
loss = loss_fn(output, target)
# Scales loss. Calls backward() on scaled loss to create scaled gradients.
# Backward passes under autocast are not recommended.
# Backward ops run in the same dtype autocast chose for corresponding forward ops.
scaler.scale(loss).backward()
# scaler.step() first unscales the gradients of the optimizer's assigned params.
# If these gradients do not contain infs or NaNs, optimizer.step() is then called,
# otherwise, optimizer.step() is skipped.
scaler.step(optimizer)
# Updates the scale for next iteration.
scaler.update()gradient clipping
scaler = GradScaler()
for epoch in epochs:
for input, target in data:
optimizer.zero_grad()
with autocast():
output = model(input)
loss = loss_fn(output, target)
scaler.scale(loss).backward()
# Unscales the gradients of optimizer's assigned params in-place
scaler.unscale_(optimizer)
# Since the gradients of optimizer's assigned params are unscaled, clips as usual:
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm)
# optimizer's gradients are already unscaled, so scaler.step does not unscale them,
# although it still skips optimizer.step() if the gradients contain infs or NaNs.
scaler.step(optimizer)
# Updates the scale for next iteration.
scaler.update()gradient accumulation
scaler = GradScaler()
for epoch in epochs:
for i, (input, target) in enumerate(data):
with autocast():
output = model(input)
loss = loss_fn(output, target)
loss = loss / accumulate_steps
# Accumulates scaled gradients.
scaler.scale(loss).backward()
if i % accumulate_steps == 0:
# may unscale_ here if desired (e.g., to allow clipping unscaled gradients)
# unscale gradient , May not affect clip Of threshold
scaler.unscale_(optimizer)
# clip gradient
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm)
scaler.step(optimizer)
scaler.update()
optimizer.zero_grad()AMP in DDP
autocast Designed as “thread local” Of , So only in main thread Set up autocast The area is not work Of , therefore , Also need to model Of forward To embellish :
MyModel(nn.Module):
...
@autocast()
def forward(self, input):
...Or in forward Set in autocast Area :
MyModel(nn.Module):
...
def forward(self, input):
with autocast():
...The first one is in use DDP There was an error ( Show forward Some parameters of are not normally obtained , Unresolved ……)
边栏推荐
- 对pyqt5和SQL Server数据库进行连接
- Add failed when BigDecimal is 0.00
- Bubble sort, print diamond, print right triangle, print inverted triangle, print equilateral triangle, print 99 multiplication table
- Swagger basic use quick start
- Kube dns yaml
- MySQL one master multi slave configuration centos7 pro test
- 爬取html入mysql插入失败
- Alibaba cloud AI training camp -sql basics 5: window functions, etc
- 力扣今日题-1037. 有效的回旋镖
- MySQL add field or create table SQL statement
猜你喜欢

Marathon环境下fastdfs和vsftpd和miniIo文件服务器搭建的方式

Morsel-Driven Parallelism: 一种NUMA感知的并行Query Execution框架

Load research of Marathon LB

Good hazelnut comes from Liaoyang!

Sonarlint代码规范改造实践及一些想法

Mysql5.7 one master multi slave configuration

redis 缓存雪崩、穿透、击穿问题

YOLOv5的Tricks | 【Trick6】学习率调整策略(One Cycle Policy、余弦退火等)

STM32 FreeRTOS task Basics

1- enter the database
随机推荐
Alibaba cloud AI training camp -sql basics 6: test questions
LRU cache
MySQL add field or create table SQL statement
模式识别大作业——PCA&Fisher&KNN&Kmeans
内网渗透 - 哈希传递攻击
Design owlook network novel recommendation system
Missing digit JS in sword finger 0~n-1
Mysql5.7 dual master and dual slave configuration
Mining happiness with machine learning
Ecmascript6.0 Basics
Apache Devlake 代码库导览
AQS 之 CountdownLatch 源码分析
In latex, \cdots is followed by a sentence. What's wrong with the format of the following sentence.
Article title
Transaction code qc51 of SAP QM preliminary level creates quality certificate for purchase order
Failed to crawl HTML into MySQL insert
Ribbon vs feign - with simple examples
Palindrome linked list leetcode
数据血缘用例与扩展实践
Thinking about global exception capture - real global exception capture