<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>性能 on Echo的技术博客</title><link>https://cybersecurityerial.github.io/echo_blog/tags/%E6%80%A7%E8%83%BD/</link><description>Recent content in 性能 on Echo的技术博客</description><generator>Hugo</generator><language>zh-cn</language><lastBuildDate>Sat, 04 Jul 2026 18:23:55 +0800</lastBuildDate><atom:link href="https://cybersecurityerial.github.io/echo_blog/tags/%E6%80%A7%E8%83%BD/index.xml" rel="self" type="application/rss+xml"/><item><title>工程踩坑：torch.empty 和 torch.zeros 的性能区别</title><link>https://cybersecurityerial.github.io/echo_blog/posts/torch-empty-vs-zeros-performance/</link><pubDate>Sat, 04 Jul 2026 18:23:55 +0800</pubDate><guid>https://cybersecurityerial.github.io/echo_blog/posts/torch-empty-vs-zeros-performance/</guid><description>&lt;h2 id="torchempty-torchzeros语义区别"&gt;torch.empty() torch.zeros()语义区别&lt;/h2&gt;
&lt;p&gt;区别很简单，是否初始化。&lt;/p&gt;
&lt;p&gt;性能上，empty后的tensor分页操作是lazy的，lazy模式不总是好的。&lt;/p&gt;
&lt;h2 id="lazyempty严重劣化的caserdma或其他需要pinned-mem场景"&gt;lazy（empty）严重劣化的case&amp;ndash;RDMA（或其他需要pinned mem场景）&lt;/h2&gt;
&lt;p&gt;如果是empty，内核给torch操作所在进程分配page是lazy的。如果没有真的分配地址，相当于进程只持有虚拟地址。如果rdma恰好要reg这块地址。
rdma调用ibv-reg-mr会导致os先处理缺页，rdma缺页的处理比普通用户更慢，因为rdma一般要pin很大的buffer，普通写入时缺页一般只是一个page。另外rdma分配好page之后还要pinpage，做dma映射，最后才能建立mr。&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;其实rdma场景烂的本质原因就是lazy设计带来的外挂问题太多了，rdma的缺页操作后面挂了一串东西：build pte&amp;ndash;pin&amp;ndash;dma mapping&amp;ndash;create mr，lazy可以，但是如果lazy的东西太长，无法被overlap，就容易出问题，rdma正是有这样的问题。&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;所以如果你的tensor要被rdma取用和发送，就一定要先用zeros初始化一下，保证真的有物理page。&lt;/p&gt;</description></item></channel></rss>