我是靠谱客的博主 要减肥棉花糖,这篇文章主要介绍C语言fp32转为fp16的代码,FP32转FP16能否加速libtorch调用,现在分享给大家,希望可以做个参考。

FP32转FP16能否加速libtorch调用

pytorchlibtorchFP16

###1. PYTORCH 采用FP16后的速度提升问题

pytorch可以使用half()函数将模型由FP32迅速简洁的转换成FP16.但FP16速度是否提升还依赖于GPU。以下面的代码为例,

import time

import torch

from torch.autograd import Variable

import torchvision.models as models

import torch.backends.cudnn as cudnn

cudnn.benchmark = True

net = models.resnet18().cuda()

inp = torch.randn(64, 3, 224, 224).cuda()

for i in range(5):

net.zero_grad()

out = net.forward(Variable(inp, requires_grad=True))

loss = out.sum()

loss.backward()

torch.cuda.synchronize()

start=time.time()

for i in range(100):

net.zero_grad()

out = net.forward(Variable(inp, requires_grad=True))

loss = out.sum()

loss.backward()

torch.cuda.synchronize()

end=t

最后

以上就是要减肥棉花糖最近收集整理的关于C语言fp32转为fp16的代码,FP32转FP16能否加速libtorch调用的全部内容,更多相关C语言fp32转为fp16内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部