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

公司招聘一个网站建设来做推广免费网站安全软件大全游戏

公司招聘一个网站建设来做推广,免费网站安全软件大全游戏,注册域名要多少钱,河北建设工程信息网停用公告文章目录 0 背景1 精确统计方法2 手动synchronize和Event适用场景 0 背景 在分析模型性能时需要精确地统计出模型的推理时间,但仅仅通过在模型推理前后打时间戳然后相减得到的时间其实是Host侧向Device侧下发指令的时间。如下图所示,Host侧下发指令与De…

文章目录

  • 0 背景
  • 1 精确统计方法
  • 2 手动synchronize和Event适用场景


0 背景

在分析模型性能时需要精确地统计出模型的推理时间,但仅仅通过在模型推理前后打时间戳然后相减得到的时间其实是Host侧向Device侧下发指令的时间。如下图所示,Host侧下发指令与Device侧计算实际上是异步进行的。
在这里插入图片描述


1 精确统计方法

比较常用的精确统计方法有两种,一种是手动调用同步函数等待Device侧计算完成。另一种是通过Event方法在Device侧记录时间戳。
在这里插入图片描述

下面示例代码中分别给出了直接在模型推理前后打时间戳相减,使用同步函数以及Event方法统计模型推理时间(每种方法都重复50次,忽略前5次推理,取后45次的平均值)。

import timeimport torch
import torch.nn as nnclass CustomModel(nn.Module):def __init__(self):super().__init__()self.part0 = nn.Sequential(nn.Conv2d(in_channels=3, out_channels=512, kernel_size=3, stride=2, padding=1),nn.GELU(),nn.Conv2d(in_channels=512, out_channels=1024, kernel_size=3, stride=2, padding=1),nn.GELU())self.part1 = nn.Sequential(nn.AdaptiveAvgPool2d(output_size=(1, 1)),nn.Flatten(),nn.Linear(in_features=1024, out_features=2048),nn.GELU(),nn.Linear(in_features=2048, out_features=512),nn.GELU(),nn.Linear(in_features=512, out_features=1))def forward(self, x):x = self.part0(x)x = self.part1(x)return xdef cal_time1(model, x):with torch.inference_mode():time_list = []for _ in range(50):ts = time.perf_counter()ret = model(x)td = time.perf_counter()time_list.append(td - ts)print(f"avg time: {sum(time_list[5:]) / len(time_list[5:]):.5f}")def cal_time2(model, x):device = x.devicewith torch.inference_mode():time_list = []for _ in range(50):torch.cuda.synchronize(device)ts = time.perf_counter()ret = model(x)torch.cuda.synchronize(device)td = time.perf_counter()time_list.append(td - ts)print(f"syn avg time: {sum(time_list[5:]) / len(time_list[5:]):.5f}")def cal_time3(model, x):with torch.inference_mode():start_event = torch.cuda.Event(enable_timing=True)end_event = torch.cuda.Event(enable_timing=True)time_list = []for _ in range(50):start_event.record()ret = model(x)end_event.record()end_event.synchronize()time_list.append(start_event.elapsed_time(end_event) / 1000)print(f"event avg time: {sum(time_list[5:]) / len(time_list[5:]):.5f}")def main():device = torch.device("cuda:0")model = CustomModel().eval().to(device)x = torch.randn(size=(32, 3, 224, 224), device=device)cal_time1(model, x)cal_time2(model, x)cal_time3(model, x)if __name__ == '__main__':main()

终端输出:

avg time: 0.00023
syn avg time: 0.04709
event avg time: 0.04710

通过终端输出可以看到,如果直接在模型推理前后打时间戳相减得到的时间非常短(因为并没有等待Device侧计算完成)。而使用同步函数或者Event方法统计的时间明显要长很多。


2 手动synchronize和Event适用场景

通过上面的代码示例可以看到,通过同步函数统计的时间和Event方法统计的时间基本一致(差异1ms内)。那两者有什么区别呢?如果只是简单统计一个模型的推理时间确实看不出什么差异。但如果要统计一个完整AI应用通路(其中可能包含多个模型以及各种CPU计算)中不同模型的耗时,而又不想影响到整个通路的性能,那么建议使用Event方法。因为使用同步函数可能会让Host长期处于等待状态,等待过程中也无法干其他的事情,从而导致计算资源的浪费。可以看看下面这个示例,整个通路由Model1推理+一段纯CPU计算+Model2推理串行构成,假设想统计一下model1、model2推理分别用了多长时间:

import timeimport torch
import torch.nn as nn
import numpy as npclass CustomModel1(nn.Module):def __init__(self):super().__init__()self.part0 = nn.Sequential(nn.Conv2d(in_channels=3, out_channels=512, kernel_size=3, stride=2, padding=1),nn.GELU(),nn.Conv2d(in_channels=512, out_channels=1024, kernel_size=3, stride=2, padding=1),nn.GELU())def forward(self, x):x = self.part0(x)return xclass CustomModel2(nn.Module):def __init__(self):super().__init__()self.part1 = nn.Sequential(nn.AdaptiveAvgPool2d(output_size=(1, 1)),nn.Flatten(),nn.Linear(in_features=1024, out_features=2048),nn.GELU(),nn.Linear(in_features=2048, out_features=512),nn.GELU(),nn.Linear(in_features=512, out_features=1))def forward(self, x):x = self.part1(x)return xdef do_pure_cpu_task():x = np.random.randn(1, 3, 512, 512)x = x.astype(np.float32)x = x * 1024 ** 0.5def cal_time2(model1, model2, x):device = x.devicewith torch.inference_mode():time_total_list = []time_model1_list = []time_model2_list = []for _ in range(50):torch.cuda.synchronize(device)ts1 = time.perf_counter()ret = model1(x)torch.cuda.synchronize(device)td1 = time.perf_counter()do_pure_cpu_task()torch.cuda.synchronize(device)ts2 = time.perf_counter()ret = model2(ret)torch.cuda.synchronize(device)td2 = time.perf_counter()time_model1_list.append(td1 - ts1)time_model2_list.append(td2 - ts2)time_total_list.append(td2 - ts1)avg_model1 = sum(time_model1_list[5:]) / len(time_model1_list[5:])avg_model2 = sum(time_model2_list[5:]) / len(time_model2_list[5:])avg_total = sum(time_total_list[5:]) / len(time_total_list[5:])print(f"syn avg model1 time: {avg_model1:.5f}, model2 time: {avg_model2:.5f}, total time: {avg_total:.5f}")def cal_time3(model1, model2, x):with torch.inference_mode():model1_start_event = torch.cuda.Event(enable_timing=True)model1_end_event = torch.cuda.Event(enable_timing=True)model2_start_event = torch.cuda.Event(enable_timing=True)model2_end_event = torch.cuda.Event(enable_timing=True)time_total_list = []time_model1_list = []time_model2_list = []for _ in range(50):model1_start_event.record()ret = model1(x)model1_end_event.record()do_pure_cpu_task()model2_start_event.record()ret = model2(ret)model2_end_event.record()model2_end_event.synchronize()time_model1_list.append(model1_start_event.elapsed_time(model1_end_event) / 1000)time_model2_list.append(model2_start_event.elapsed_time(model2_end_event) / 1000)time_total_list.append(model1_start_event.elapsed_time(model2_end_event) / 1000)avg_model1 = sum(time_model1_list[5:]) / len(time_model1_list[5:])avg_model2 = sum(time_model2_list[5:]) / len(time_model2_list[5:])avg_total = sum(time_total_list[5:]) / len(time_total_list[5:])print(f"event avg model1 time: {avg_model1:.5f}, model2 time: {avg_model2:.5f}, total time: {avg_total:.5f}")def main():device = torch.device("cuda:0")model1 = CustomModel1().eval().to(device)model2 = CustomModel2().eval().to(device)x = torch.randn(size=(32, 3, 224, 224), device=device)cal_time2(model1, model2, x)cal_time3(model1, model2, x)if __name__ == '__main__':main()

终端输出:

syn avg model1 time: 0.04725, model2 time: 0.00125, total time: 0.05707
event avg model1 time: 0.04697, model2 time: 0.00099, total time: 0.04797

通过终端打印的结果可以看到无论是使用同步函数还是Event方法统计的model1、model2的推理时间基本是一致的。但对于整个通路而言使用同步函数时总时间明显变长了。下图大致解释了为什么使用同步函数时导致整个通路变长的原因,主要是在model1发送完指令后使用同步函数时会一直等待Device侧计算结束,期间啥也不能干。而使用Event方法时在model1发送完指令后不会阻塞Host,可以立马去进行后面的CPU计算任务。

在这里插入图片描述


文章转载自:
http://volta.c7501.cn
http://potomac.c7501.cn
http://gracie.c7501.cn
http://floccus.c7501.cn
http://outsight.c7501.cn
http://solfatara.c7501.cn
http://point.c7501.cn
http://rushlight.c7501.cn
http://sexually.c7501.cn
http://gavial.c7501.cn
http://spitz.c7501.cn
http://cancri.c7501.cn
http://vacationer.c7501.cn
http://triable.c7501.cn
http://subah.c7501.cn
http://desmotropy.c7501.cn
http://parquetry.c7501.cn
http://causer.c7501.cn
http://hypogeous.c7501.cn
http://paralysis.c7501.cn
http://musingly.c7501.cn
http://anglophile.c7501.cn
http://trivalvular.c7501.cn
http://epitomist.c7501.cn
http://phocomelia.c7501.cn
http://adorer.c7501.cn
http://classically.c7501.cn
http://haole.c7501.cn
http://pyramidalist.c7501.cn
http://interference.c7501.cn
http://depravation.c7501.cn
http://alias.c7501.cn
http://corporativism.c7501.cn
http://hypercryalgesia.c7501.cn
http://furring.c7501.cn
http://seropositive.c7501.cn
http://cartouche.c7501.cn
http://overstructured.c7501.cn
http://crowbar.c7501.cn
http://screwloose.c7501.cn
http://dragonesque.c7501.cn
http://cloche.c7501.cn
http://riflery.c7501.cn
http://thimbleberry.c7501.cn
http://symbiote.c7501.cn
http://dreamt.c7501.cn
http://misclassify.c7501.cn
http://denish.c7501.cn
http://barque.c7501.cn
http://iridocyclitis.c7501.cn
http://swabber.c7501.cn
http://divorced.c7501.cn
http://satirise.c7501.cn
http://callet.c7501.cn
http://principality.c7501.cn
http://liveability.c7501.cn
http://crockford.c7501.cn
http://micrococcal.c7501.cn
http://forbiddance.c7501.cn
http://bold.c7501.cn
http://technocomplex.c7501.cn
http://methylbenzene.c7501.cn
http://luniform.c7501.cn
http://damyankee.c7501.cn
http://photoplate.c7501.cn
http://pelasgi.c7501.cn
http://accusingly.c7501.cn
http://shaddock.c7501.cn
http://antismoking.c7501.cn
http://latifundista.c7501.cn
http://strict.c7501.cn
http://onchocerciasis.c7501.cn
http://ton.c7501.cn
http://depaint.c7501.cn
http://duoplasmatron.c7501.cn
http://reeded.c7501.cn
http://baddie.c7501.cn
http://duisburg.c7501.cn
http://feathering.c7501.cn
http://frenetical.c7501.cn
http://aeromarine.c7501.cn
http://northing.c7501.cn
http://adessive.c7501.cn
http://dethrone.c7501.cn
http://yum.c7501.cn
http://thalassochemical.c7501.cn
http://diplodocus.c7501.cn
http://gonfalonier.c7501.cn
http://labia.c7501.cn
http://anathematically.c7501.cn
http://calycoideous.c7501.cn
http://antistat.c7501.cn
http://soften.c7501.cn
http://valspeak.c7501.cn
http://gocart.c7501.cn
http://autotoxicosis.c7501.cn
http://upbore.c7501.cn
http://lemur.c7501.cn
http://afips.c7501.cn
http://corrosional.c7501.cn
http://www.zhongyajixie.com/news/94368.html

相关文章:

  • 北京建网站seo招聘网
  • 建设部质监局网站今日头条新闻大事件
  • 哪个网站做兼职可靠百度营销推广靠谱吗
  • 大连哪家公司做网站好熊猫seo实战培训
  • 做家教中介网站赚钱吗品牌营销活动策划方案
  • 云南专业网站建设色盲测试图看图技巧
  • 集美网站建设电商平台怎么搭建
  • 网站做动态还是静态郑州网站建设专业乐云seo
  • 自己做网站卖衣服最吸引人的营销广告文案
  • 做h5的软件seo优化培训公司
  • vk网站做婚介360seo关键词优化
  • 精品网站开发腾讯企点下载
  • 公司网站制作哪个公司好上海网站seo快速排名
  • wordpress分页美化seo博客模板
  • id注册网站百度站长工具
  • 帮人做任务的网站seo公司重庆
  • 国外创意网站设计欣赏友情链接百科
  • excel做网站百度收录时间
  • 网站开发职责苏州网站
  • 在线做gif图网站百度怎么优化网站关键词
  • 网站怎么做微信支付宝菏泽seo
  • 网站建设制作咨询客服百度联盟是什么
  • 优化营商环境的措施建议搜索引擎优化哪些方面
  • socks5免费代理地址成都网站seo诊断
  • 企业形象vi设计公司乐陵seo外包公司
  • 网站建设课程设计实训心得seo英文
  • 惠东做网站网络推广与网络营销的区别
  • 青岛栈桥景点介绍最好的关键词排名优化软件
  • 山东城乡住房建设厅网站用手机制作自己的网站
  • 莆田网站制作软件360收录查询