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

河源哪里做网站搜索推广开户

河源哪里做网站,搜索推广开户,青海省建设厅官方网站建设云,2020年购物app排行开源仓库:TingsongYu/PyTorch-Tutorial-2nd: 《Pytorch实用教程》(第二版)无论是零基础入门,还是CV、NLP、LLM项目应用,或是进阶工程化部署落地,在这里都有。相信在本书的帮助下,读者将能够轻松…

开源仓库:TingsongYu/PyTorch-Tutorial-2nd: 《Pytorch实用教程》(第二版)无论是零基础入门,还是CV、NLP、LLM项目应用,或是进阶工程化部署落地,在这里都有。相信在本书的帮助下,读者将能够轻松掌握 PyTorch 的使用,成为一名优秀的深度学习工程师。 (github.com)https://github.com/TingsongYu/PyTorch-Tutorial-2nd

 在线阅读:简介 · PyTorch实用教程(第二版) (tingsongyu.github.io)https://tingsongyu.github.io/PyTorch-Tutorial-2nd/

 chapter-1

1. torch.compile 的基本原理

torch.compile 的工作原理可以概括为以下几个关键步骤:

  1. 图捕获(Graph Capture)

    • 将 Python 函数或模块转换为计算图表示

    • 通过追踪执行流或 AST 分析构建中间表示(IR)

  2. 图优化(Graph Optimization)

    • 算子融合(Operator Fusion):合并多个小操作

    • 内存优化:减少中间结果的内存分配

    • 自动并行化:识别并行计算机会

  3. 代码生成(Code Generation)

    • 生成优化的机器代码(CPU/GPU)

    • 针对不同后端(如 CUDA、ROCm)生成特定代码

  4. 缓存机制

    • 缓存编译结果避免重复编译

    • 根据输入张量特性自动选择最优实现

import time
import torch
import numpy as np
from torchvision import models
mode_list = "default reduce-overhead max-autotune".split()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(mode_list)
print(device)

2. 主要编译模式 

torch.compile 提供多种编译模式,通过 mode 参数指定:

2.1 default 模式

  • 平衡编译时间和运行性能

  • 进行基本优化但不尝试所有可能优化

  • 适合大多数常规场景

2.2 reduce-overhead 模式

  • 重点减少Python解释器开销

  • 适合小模型或频繁调用的函数

  • 编译时间较短

2.3 max-autotune 模式

  • 启用所有可能的优化

  • 进行彻底的自动调优

  • 编译时间最长但运行时性能最佳

  • 适合计算密集型任务

 实验一:sin函数

def sin_func(x):return torch.sin(x) + torch.cos(x)run_times = 100000
i_data = torch.tensor(1).to(device)
for mode in mode_list:torch.cuda.synchronize()time_0 = time.time()module_compiled = torch.compile(sin_func, mode=mode)torch.cuda.synchronize()time_1 = time.time()# warmupsin_func(i_data)module_compiled(i_data)torch.cuda.synchronize()time_2 = time.time()for i in range(run_times):sin_func(i_data)torch.cuda.synchronize()time_3 = time.time()for i in range(run_times):module_compiled(i_data)torch.cuda.synchronize()time_4 = time.time()compile_time = time_1 - time_0pre_time = time_3 - time_2post_time = time_4 - time_3speedup_ratio = (pre_time - post_time)/pre_timeprint(f"mode: {mode}, 编译耗时:{compile_time:.2f},编译前运行耗时:{pre_time:.2f}, 编译后运行耗时:{post_time:.2f},速度提升比例:{speedup_ratio:.2%}")

 

实验二:resnet18 

resnet18 = models.resnet18().to(device)
print(resnet18)
resnet18.eval()
fake_img = torch.randn(16, 3, 224, 224).to(device)

 

实验三:BERT 

from transformers import BertModel, BertTokenizer
import timebert = BertModel.from_pretrained('bert-base-uncased')
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')

# 准备一批输入数据
input_text = "Here is some text to encode"
inputs = tokenizer(input_text, return_tensors='pt', padding=True, truncation=True)
inputs = {k: v.to(device) for k, v in inputs.items()}
bert.to(device)
bert.eval()run_times = 100
with torch.no_grad():for mode in mode_list:# 编译torch.cuda.synchronize()time_0 = time.time()bert_compiled = torch.compile(bert, mode=mode)torch.cuda.synchronize()time_1 = time.time()# warmup 非常关键!bert(**inputs)bert_compiled(**inputs)torch.cuda.synchronize()time_2= time.time()for _ in range(run_times): _ = bert(**inputs)torch.cuda.synchronize()time_3= time.time()for _ in range(run_times):_ = bert_compiled(**inputs)torch.cuda.synchronize()time_4= time.time()compile_time = time_1 - time_0pre_time = time_3 - time_2post_time = time_4 - time_3speedup_ratio = (pre_time - post_time)/pre_timeprint(f"mode: {mode}, 编译耗时:{compile_time:.2f},编译前运行耗时:{pre_time:.2f}, 编译后运行耗时:{post_time:.2f},速度提升比例:{speedup_ratio:.2%}")

 实验四:numpy

run_times = 100def numpy_fn2(X: np.ndarray, Y: np.ndarray) -> np.ndarray:return np.sum(X[:, :, None] * Y[:, None, :], axis=(-2, -1))def numpy_fn(X: np.ndarray, Y: np.ndarray) -> np.ndarray:# Step 1: Normalize the input arrays to have zero mean and unit varianceX_mean, X_std = X.mean(axis=0), X.std(axis=0)Y_mean, Y_std = Y.mean(axis=0), Y.std(axis=0)# Avoid division by zero in case of zero standard deviationX_std[X_std == 0] = 1Y_std[Y_std == 0] = 1X_normalized = (X - X_mean) / X_stdY_normalized = (Y - Y_mean) / Y_std# Step 2: Perform the tensor product followed by sum over last two dimensionsintermediate_result = np.sum(X_normalized[:, :, None] * Y_normalized[:, None, :], axis=(-2, -1))# Step 3: Apply thresholding to clip values outside of [-1, 1]intermediate_result = np.clip(intermediate_result, -1, 1)# Step 4: Apply exponential function for non-linearityresult = np.exp(intermediate_result)# Step 5: Add a small regularization term to avoid overfittingregularization_term = 0.001 * np.sum(X_normalized ** 2 + Y_normalized ** 2, axis=1)result += regularization_termreturn resultx = np.random.randn(1024, 640)
y = np.random.randn(1024, 640)for mode in mode_list:torch.cuda.synchronize()time_0 = time.time()numpy_fn_compiled = torch.compile(numpy_fn, mode=mode)torch.cuda.synchronize()time_1 = time.time()# warmup 非常关键!numpy_fn(x, y)numpy_fn_compiled(x, y)#torch.cuda.synchronize()time_2 = time.time()for i in range(run_times):numpy_fn(x, y)torch.cuda.synchronize()time_3 = time.time()for i in range(run_times):numpy_fn_compiled(x, y)torch.cuda.synchronize()time_4 = time.time()compile_time = time_1 - time_0pre_time = time_3 - time_2post_time = time_4 - time_3speedup_ratio = (pre_time - post_time)/pre_timeprint(f"mode: {mode}, 编译耗时:{compile_time:.2f},编译前运行耗时:{pre_time:.2f}, 编译后运行耗时:{post_time:.2f},速度提升比例:{speedup_ratio:.2%}")

对于numpy计算,提升显著,能够提升90%速度。

 

http://www.dtcms.com/wzjs/67674.html

相关文章:

  • wordpress查看自己的评论优化培训方式
  • 一站式商家服务平台万网域名注册查询
  • 新乡做网站让手机变流畅的软件下载
  • wordpress彩色标签插件刷百度关键词排名优化
  • 重庆电子工程学院免费seo关键词优化方案
  • 松原做公司网站搜索引擎大全网站
  • 专业网站设计制作价格千锋教育培训机构就业率
  • 上海网站建设的价格低2022最新时事新闻及点评
  • 网上书城网站开发说明书网络推广网络营销和网站推广的区别
  • 电子商务网站商品怎么来线上宣传渠道和宣传方式
  • 怎样做p2p网站百度账号
  • 社区网站建设网络推广员工作好做吗
  • 外国设计师素材网站百度云网页版登录入口
  • 济南专业做网站的公司产品推广软文500字
  • 公司想做个自己的网站怎么做舆情分析网站免费
  • 一个公司设计网站怎么做今日头条新闻视频
  • 企业手机网站建设策划方案成都疫情最新消息
  • 杭州网站优化外包国内永久免费云服务器
  • 口碑好的郑州网站建设友情链接如何添加
  • 北京大兴住房和城乡建设委员会网站搜索引擎营销简称seo
  • 阴阳师网站怎么做百度一下百度一下你知道
  • 营销策划公司简介网站优化排名方法
  • 微信公众号如何做网站产品设计公司
  • qq刷赞网站推广软件国内做网站的公司
  • 做网站先做首页企业域名查询
  • 做生鲜的网站网络平台推广运营公司
  • 美国一特级a做爰片免费网站 视频金华关键词优化平台
  • 网站建设文献综述范文外链图片
  • 手机网站模板用什么做百度官网认证价格
  • 如何设计一个购物网站怎么找网站