<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>GPU &#8211; 编码无悔 /  Intent &amp; Focused</title>
	<atom:link href="https://www.codelast.com/tag/gpu/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codelast.com</link>
	<description>最优化之路</description>
	<lastBuildDate>Mon, 04 May 2020 14:25:51 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>[原创] PyTorch做inference/prediction的时候如何使用GPU</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-pytorch%e5%81%9ainference-prediction%e7%9a%84%e6%97%b6%e5%80%99%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8gpu/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-pytorch%e5%81%9ainference-prediction%e7%9a%84%e6%97%b6%e5%80%99%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8gpu/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Mon, 27 Apr 2020 11:50:56 +0000</pubDate>
				<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[GPU]]></category>
		<category><![CDATA[inference]]></category>
		<category><![CDATA[prediction]]></category>
		<category><![CDATA[PyTorch]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=12047</guid>

					<description><![CDATA[<p>话不多说，直接进入主题。</p>
<p><span style="color:#0000ff;">✔</span> 判断能不能使用GPU<br />
可能有多种原因会导致不能使用GPU，比如PyTorch安装的是CPU版的，显卡驱动没有正确安装等。下面的 if 语句在正常的情况下会返回 True：</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'JetBrains Mono';font-size:13.5pt;">
<span style="color:#cc7832;">if </span>torch.cuda.is_available():
    <span style="color:#8888c6;">print</span>(<span style="color:#6a8759;">&#39;PyTorch can use GPU on current machine!&#39;</span>)</pre>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(0, 0, 255);">✔</span>&#160;设置模型使用GPU</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'JetBrains Mono';font-size:13.5pt;">
model = MyModel(*args<span style="color:#cc7832;">, </span>**kwargs)
model.load_state_dict(torch.load(your_model_file_path))
model.eval()  <span style="color:#808080;"># </span><span style="color:#808080;font-family:'DejaVu Sans Mono';">设置成</span><span style="color:#808080;">evaluation</span><span style="color:#808080;font-family:'DejaVu Sans Mono';">模式
</span><span style="color:#cc7832;">if </span>torch.cuda.is_available():
    <span style="color:#8888c6;">print</span>(<span style="color:#6a8759;">&#39;PyTorch can use GPU on current machine!&#39;</span></pre>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-pytorch%e5%81%9ainference-prediction%e7%9a%84%e6%97%b6%e5%80%99%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8gpu/" class="read-more">Read More </a>]]></description>
										<content:encoded><![CDATA[<p>话不多说，直接进入主题。</p>
<p><span style="color:#0000ff;"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2714.png" alt="✔" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span> 判断能不能使用GPU<br />
可能有多种原因会导致不能使用GPU，比如PyTorch安装的是CPU版的，显卡驱动没有正确安装等。下面的 if 语句在正常的情况下会返回 True：</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'JetBrains Mono';font-size:13.5pt;">
<span style="color:#cc7832;">if </span>torch.cuda.is_available():
    <span style="color:#8888c6;">print</span>(<span style="color:#6a8759;">&#39;PyTorch can use GPU on current machine!&#39;</span>)</pre>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(0, 0, 255);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2714.png" alt="✔" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span>&nbsp;设置模型使用GPU</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'JetBrains Mono';font-size:13.5pt;">
model = MyModel(*args<span style="color:#cc7832;">, </span>**kwargs)
model.load_state_dict(torch.load(your_model_file_path))
model.eval()  <span style="color:#808080;"># </span><span style="color:#808080;font-family:'DejaVu Sans Mono';">设置成</span><span style="color:#808080;">evaluation</span><span style="color:#808080;font-family:'DejaVu Sans Mono';">模式
</span><span style="color:#cc7832;">if </span>torch.cuda.is_available():
    <span style="color:#8888c6;">print</span>(<span style="color:#6a8759;">&#39;PyTorch can use GPU on current machine!&#39;</span>)
    device = torch.device(<span style="color:#6a8759;">&quot;cuda&quot;</span>)
    model.to(device)</pre>
<p>your_model_file_path 是模型文件的路径。<br />
<span id="more-12047"></span><br />
<span style="color: rgb(0, 0, 255);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2714.png" alt="✔" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span>&nbsp;inference/predict的时候使用GPU<br />
对一次 inference 来说，假设模型的输入数据为&nbsp;model_input_tensor（torch.Tensor类型），那么计算模型输出的方法是：</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'JetBrains Mono';font-size:13.5pt;">
<span style="color:#cc7832;">if </span>torch.cuda.is_available():  <span style="color:#808080;"># GPU available
</span><span style="color:#808080;">    </span>model_input_tensor = model_input_tensor.to(torch.device(<span style="color:#6a8759;">&#39;cuda&#39;</span>))
model_output = model(model_input_tensor)  <span style="color:#808080;"># inference</span></pre>
<p>
<span style="color: rgb(0, 0, 255);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2714.png" alt="✔" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span>&nbsp;检查程序跑起来之后是不是真的用了GPU<br />
用 nvidia-smi 命令来查看。</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	&nbsp;</p>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-pytorch%e5%81%9ainference-prediction%e7%9a%84%e6%97%b6%e5%80%99%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8gpu/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[原创] 强化学习框架 rlpyt 使用GPU训练模型</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%bc%ba%e5%8c%96%e5%ad%a6%e4%b9%a0%e6%a1%86%e6%9e%b6-rlpyt-%e4%bd%bf%e7%94%a8gpu%e8%ae%ad%e7%bb%83%e6%a8%a1%e5%9e%8b/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%bc%ba%e5%8c%96%e5%ad%a6%e4%b9%a0%e6%a1%86%e6%9e%b6-rlpyt-%e4%bd%bf%e7%94%a8gpu%e8%ae%ad%e7%bb%83%e6%a8%a1%e5%9e%8b/#comments</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Thu, 16 Apr 2020 18:08:09 +0000</pubDate>
				<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[GPU]]></category>
		<category><![CDATA[rlpyt]]></category>
		<category><![CDATA[强化学习]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=11914</guid>

					<description><![CDATA[<p></p>
<section class="RankEditor" data-opacity="1" data-rotate="0" data-width="100%" style="width: 100%; margin: 0px auto; opacity: 1;transform: rotateZ(0deg);-webkit-transform: rotateZ(0deg);-moz-transform: rotateZ(0deg);-ms-transform: rotateZ(0deg);-o-transform: rotateZ(0deg);">
<section style="margin-right: auto; margin-left: auto; display: flex; justify-content: center; align-items: center;">
<section style="width: 100%; border-radius: 5px; border: 1px solid rgb(105, 105, 105); padding: 6px; box-sizing: border-box;">
<section style="width: 100%; border-radius: 5px; border: 1px solid rgb(255, 185, 15); padding: 20px; text-align: justify; box-sizing: border-box;">
<p class="brush active" style=" font-size: 13px; color: rgb(51, 51, 51); letter-spacing: 1.5px; line-height: 1.75;min-width:1px;">
	<a href="https://github.com/astooke/rlpyt" rel="noopener noreferrer" style="font-size: medium;" target="_blank"><span style="background-color: rgb(255, 160, 122);">rlpyt</span></a><span style="font-size: medium;">&#160;是</span><span style="font-size: medium; color: rgb(0, 0, 255);">BAIR</span><span style="font-size: medium;">(Berkeley Artificial Intelligence Research，伯克利人工智能研究所)开源的一个强化学习(</span><span style="font-size: medium; color: rgb(255, 0, 0);">RL</span><span style="font-size: medium;">)框架。我之前写了一篇它的</span><a href="https://www.codelast.com/?p=10643" rel="noopener noreferrer" style="font-size: medium;" target="_blank"><span style="background-color: rgb(255, 160, 122);">简介</span></a><span style="font-size: medium;">。&#160;</span><br style="font-size: medium;" /><br />
	<span style="font-size: medium;">rlpyt 可利用GPU来训练模型，理论上速度会比CPU快。</span></p>
</section>
</section>
</section>
</section>
<p><span id="more-11914"></span></p>
<p><span style="color:#ff0000;">✔</span> 安装GPU版的PyTorch<br />
在Anaconda下，利用rlpyt自带的&#160;<span style="color:#b22222;">linux_cuda10.yml</span> 或&#160;<span style="color:#b22222;">linux_cuda9.yml</span> 来安装 rlpyt 依赖的Anaconda env（取决于你使用的CUDA Version是多少）。<br />
使用 <span style="color:#0000ff;">nvidia-smi</span> 命令查看 CUDA Version：</p>
<blockquote>
<div>
		NVIDIA-SMI 410.72&#160; &#160; &#160; &#160;Driver Version: 410.72&#160; &#160; &#160; &#160;CUDA Version: 10.0</div>
</blockquote>
<div>
	我的是 10.0，因此修改&#160;<span style="color: rgb(178, 34, 34);">linux_cuda10.yml</span></div>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%bc%ba%e5%8c%96%e5%ad%a6%e4%b9%a0%e6%a1%86%e6%9e%b6-rlpyt-%e4%bd%bf%e7%94%a8gpu%e8%ae%ad%e7%bb%83%e6%a8%a1%e5%9e%8b/" class="read-more">Read More </a>]]></description>
										<content:encoded><![CDATA[<p></p>
<section class="RankEditor" data-opacity="1" data-rotate="0" data-width="100%" style="width: 100%; margin: 0px auto; opacity: 1;transform: rotateZ(0deg);-webkit-transform: rotateZ(0deg);-moz-transform: rotateZ(0deg);-ms-transform: rotateZ(0deg);-o-transform: rotateZ(0deg);">
<section style="margin-right: auto; margin-left: auto; display: flex; justify-content: center; align-items: center;">
<section style="width: 100%; border-radius: 5px; border: 1px solid rgb(105, 105, 105); padding: 6px; box-sizing: border-box;">
<section style="width: 100%; border-radius: 5px; border: 1px solid rgb(255, 185, 15); padding: 20px; text-align: justify; box-sizing: border-box;">
<p class="brush active" style=" font-size: 13px; color: rgb(51, 51, 51); letter-spacing: 1.5px; line-height: 1.75;min-width:1px;">
	<a href="https://github.com/astooke/rlpyt" rel="noopener noreferrer" style="font-size: medium;" target="_blank"><span style="background-color: rgb(255, 160, 122);">rlpyt</span></a><span style="font-size: medium;">&nbsp;是</span><span style="font-size: medium; color: rgb(0, 0, 255);">BAIR</span><span style="font-size: medium;">(Berkeley Artificial Intelligence Research，伯克利人工智能研究所)开源的一个强化学习(</span><span style="font-size: medium; color: rgb(255, 0, 0);">RL</span><span style="font-size: medium;">)框架。我之前写了一篇它的</span><a href="https://www.codelast.com/?p=10643" rel="noopener noreferrer" style="font-size: medium;" target="_blank"><span style="background-color: rgb(255, 160, 122);">简介</span></a><span style="font-size: medium;">。&nbsp;</span><br style="font-size: medium;" /><br />
	<span style="font-size: medium;">rlpyt 可利用GPU来训练模型，理论上速度会比CPU快。</span></p>
</section>
</section>
</section>
</section>
<p><span id="more-11914"></span></p>
<p><span style="color:#ff0000;"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2714.png" alt="✔" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span> 安装GPU版的PyTorch<br />
在Anaconda下，利用rlpyt自带的&nbsp;<span style="color:#b22222;">linux_cuda10.yml</span> 或&nbsp;<span style="color:#b22222;">linux_cuda9.yml</span> 来安装 rlpyt 依赖的Anaconda env（取决于你使用的CUDA Version是多少）。<br />
使用 <span style="color:#0000ff;">nvidia-smi</span> 命令查看 CUDA Version：</p>
<blockquote>
<div>
		NVIDIA-SMI 410.72&nbsp; &nbsp; &nbsp; &nbsp;Driver Version: 410.72&nbsp; &nbsp; &nbsp; &nbsp;CUDA Version: 10.0</div>
</blockquote>
<div>
	我的是 10.0，因此修改&nbsp;<span style="color: rgb(178, 34, 34);">linux_cuda10.yml</span>，把下面这行：</div>
<blockquote>
<div>
		cudatoolkit=10.</div>
</blockquote>
<div>
	改成：</div>
<blockquote>
<div>
		cudatoolkit=10.0</div>
</blockquote>
<div>
	我测试下来，如果不改，rlpyt env也可以安装成功，但是装上的是CPU版的PyTorch；不仅如此，如果你服务器上的CUDA Version是10.1的，你也要把 yml 配置文件里改成 10.0，否则装上的也是CPU版的PyTorch：</p>
<blockquote>
<div>
			~$ conda list | grep pytorch</div>
<div>
			_pytorch_select&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0.1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cpu_0&nbsp; &nbsp; defaults</div>
<div>
			pytorch&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1.2.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cpu_py37h00be3c6_0&nbsp; &nbsp; defaults</div>
</blockquote>
<p>	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
	改了之后装上的就是GPU版的PyTorch了：</p>
<blockquote>
<div>
			~$ conda list | grep pytorch</div>
<div>
			_pytorch_select&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0.2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;gpu_0&nbsp; &nbsp; defaults</div>
<div>
			pytorch&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1.2.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cuda100py37h938c94c_0&nbsp; &nbsp; defaults</div>
</blockquote>
<div>
		<br />
		<span style="color: rgb(255, 0, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2714.png" alt="✔" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span>&nbsp;在模型训练程序调度GPU<br />
		具体应该怎么写GPU相关的程序，取决于你要实现什么样的功能。例如，使用同步/异步模式，它们对应的Sampler是不同的，等等。<br />
		涉及到具体的代码，这里难以给出一个详细的说明，可以参考 rlpyt 的examples代码。<br />
		<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
		<span style="color: rgb(255, 0, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2714.png" alt="✔" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span>&nbsp;确认真的在使用GPU训练<br />
		依然使用&nbsp;<span style="color: rgb(0, 0, 255);">nvidia-smi</span>&nbsp;命令来检测，正常情况下，在模型开始训练后，会看到 Processes 列表中有你的模型训练进程：</div>
<blockquote>
<div>
			3&nbsp; &nbsp; &nbsp;17754&nbsp; &nbsp; &nbsp; C&nbsp; &nbsp;.../.anaconda/envs/rlpyt/bin/python&nbsp; &nbsp;459MiB</div>
</blockquote>
<div>
		此外，在模型训练的过程中，查看GPU使用率应该不为0：</div>
<blockquote>
<div>
			dstat --nvidia-gpu -af</div>
</blockquote>
<div>
		这样就OK了。<br />
		据我实测，在我的应用场景中，同样功能的强化学习程序，GPU版比CPU版的模型训练速度快50%左右，GPU单卡使用率最高约为14%（只使用了一张卡，Nvidia P40显卡）。<br />
		<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
		<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
		转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
		感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
			<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>
</p></div>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%bc%ba%e5%8c%96%e5%ad%a6%e4%b9%a0%e6%a1%86%e6%9e%b6-rlpyt-%e4%bd%bf%e7%94%a8gpu%e8%ae%ad%e7%bb%83%e6%a8%a1%e5%9e%8b/feed/</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
			</item>
	</channel>
</rss>
