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

Ascend的aclgraph(八)AclConcreteGraph:capture_end

1 回顾

在上一章Ascend的aclgraph(七)AclConcreteGraph:capture_begin中提到了对capture_begin做了了解,现在继续看下capture_end

torchair->torch_npu流程图如下:
在这里插入图片描述

def __enter__(self):# Free as much memory as we can for the graphtorch.npu.synchronize()gc.collect()torch.npu.empty_cache()# Stackoverflow seems comfortable with this patternself.stream_ctx.__enter__()self.npu_graph.capture_begin(*self.pool, capture_error_mode=self.capture_error_mode)def __exit__(self, exc_type, exc_value, traceback):self.npu_graph.capture_end()self.stream_ctx.__exit__(exc_type, exc_value, traceback)

2 capture_end

.def("capture_end",torch::wrap_pybind_function_no_gil(&c10_npu::NPUGraph::capture_end))

如上述代码所示,在做时候,并没有其他额外逻辑,直接是调用到了c++侧的capture_end
wrap_pybind_function_no_gil是先去掉python的gil锁,以防死锁。
代码如下:

void NPUGraph::capture_end()
{auto stream = c10_npu::getCurrentNPUStream();TORCH_CHECK(stream == capture_stream_,"Capture must end on the same stream it began on.");aclmdlRI model_ri;NPU_CHECK_ERROR(c10_npu::acl::AclmdlRICaptureEnd(capture_stream_, &model_ri));c10_npu::NPUCachingAllocator::endAllocateToPool(capture_dev_, mempool_id_);TORCH_CHECK(model_ri == model_ri_, "Invalid end capture model id: ", model_ri);// In typical graph usage some tensors (e.g. the tensors used for graph IO) are not freed// between replays.// If Pytorch compiles and runs with a CUDA 11.4+ toolkit, there's a chance the allocator backend// is cudaMallocAsync.// cudaMallocAsync is generally graph-safe, but if some tensors are not freed between replays,// the graph's internal bookkeeping requires that we instantiate with// cudaGraphInstantiateFlagAutoFreeOnLaunch. See// cudaGraphLaunch// cudaGraphInstantiateWithFlagshas_graph_exec_ = true;uint32_t num_graph_nodes = 0;
}

流程图如下:
在这里插入图片描述
AclmdlRICaptureEndAclmdlRICaptureBegin是相对的,调用的是libascendcl.so中的AclmdlRICaptureEnd方法。

aclError AclmdlRICaptureEnd(aclrtStream stream, aclmdlRI *modelRI)
{typedef aclError (*AclmdlRICaptureEnd)(aclrtStream, aclmdlRI *);static AclmdlRICaptureEnd func = nullptr;if (func == nullptr) {func = (AclmdlRICaptureEnd) GET_FUNC(aclmdlRICaptureEnd);}TORCH_CHECK(func, "Failed to find function aclmdlRICaptureEnd", PTA_ERROR(ErrCode::NOT_FOUND));return func(stream, modelRI);
}

在昇腾官网上搜索到如下信息:
https://www.hiascend.com/document/detail/zh/canncommercial/81RC1/apiref/appdevgapi/aclcppdevg_03_1784.html
函数原型
在这里插入图片描述
接口解释
结束Stream的捕获动作,并获取模型的运行实例,该模型用于暂存所捕获的任务。

本接口需与其它接口配合使用,以便捕获Stream上下发的任务,暂存在内部创建的模型中,用于后续的任务执行,以此减少Host侧的任务下发开销,配合使用流程请参见aclmdlRICaptureBegin接口处的说明。

还有个一个函数是endAllocateToPool,给出函数定义

// Called by NPUGraph::capture_end
void endAllocateToPool(MempoolId_t mempool_id)
{std::lock_guard<std::recursive_mutex> lock(mutex);for (auto it = captures_underway.begin(); it != captures_underway.end(); ++it) {if (it->first == mempool_id) {captures_underway.erase(it);return;}}TORCH_CHECK(false, "endAllocatePool: not currently recording to mempool_id");
}

这里就是从captures_underway删除此次使用的mempool_id。这个操作与beginAllocateToPool中的emplace操作是相对应的。

3 总结

capture_end的逻辑相对简单。
下一步,梳理清楚torch.compile中的FX graph和AclmdlRICaptureBegin与AclmdlRICaptureEnd抓的图是什么关系。

http://www.xdnf.cn/news/6192.html

相关文章:

  • 网络编程超时检测,unix域套接字,粘包
  • WPF Datagrid 数据加载和性能
  • Spring的 @Validate注解详细分析
  • 【springcloud学习(dalston.sr1)】Ribbon负载均衡(七)
  • 【行为型之模板方法模式】游戏开发实战——Unity标准化流程与可扩展架构的核心实现
  • 数据库MySQL学习——day10()
  • FFMPEG 与 mp4
  • elpis-core: 基于 Koa 实现 web 服务引擎架构设计解析
  • LeetCode 热题 100_颜色分类(98_75_中等_C++)(技巧)(计数;双指针)
  • git push 报错:send-pack: unexpected disconnect while reading sideband packet
  • 鸿蒙OSUniApp 开发的下拉刷新与上拉加载列表#三方框架 #Uniapp
  • “堆”和“栈”
  • matlab插值方法(简短)
  • 4G物联网模块实现废气处理全流程数据可视化监控配置
  • Android多媒体——媒体解码流程分析(十四)
  • Cursor 0.5版本发布,新功能介绍
  • 从零实现一个高并发内存池 - 2
  • WebGL知识框架
  • 网络协议分析 实验五 UDP-IPv6-DNS
  • openfeign与dubbo调用下载excel实践
  • Python知识框架
  • Idea 设置编码UTF-8 Idea中 .properties 配置文件中文乱码
  • 【大模型】OpenManus 项目深度解析:构建通用 AI Agent的开源框架
  • Ubuntu——执行echo $USE什么都不显示
  • Turborepo + Vite + Next.js + Shadcn Monorepo 项目构建
  • 【JVS更新日志】企业文档AI助手上线、低代码、智能BI、智能APS、AI助手5.14更新说明!
  • Python如何解决中文乱码
  • 驾驭数据洪流:大数据治理的全面解析与实战方案
  • git使用的DLL错误
  • 线性规划求解及演示