<?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>AI &#8211; richliu&#039;s blog</title>
	<atom:link href="https://blog.richliu.com/tag/ai/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.richliu.com</link>
	<description>Linux, 工作, 生活, 家人</description>
	<lastBuildDate>Mon, 06 Apr 2026 10:44:39 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.2</generator>
	<item>
		<title>千萬不要拿星穹鐵道機器人閉嘴的資料給 AI ，要不然&#8230;..</title>
		<link>https://blog.richliu.com/2026/04/06/6496/%e5%8d%83%e8%90%ac%e4%b8%8d%e8%a6%81%e6%8b%bf%e6%98%9f%e7%a9%b9%e9%90%b5%e9%81%93%e6%a9%9f%e5%99%a8%e4%ba%ba%e9%96%89%e5%98%b4%e7%9a%84%e8%b3%87%e6%96%99%e7%b5%a6-ai-%ef%bc%8c%e8%a6%81%e4%b8%8d/</link>
					<comments>https://blog.richliu.com/2026/04/06/6496/%e5%8d%83%e8%90%ac%e4%b8%8d%e8%a6%81%e6%8b%bf%e6%98%9f%e7%a9%b9%e9%90%b5%e9%81%93%e6%a9%9f%e5%99%a8%e4%ba%ba%e9%96%89%e5%98%b4%e7%9a%84%e8%b3%87%e6%96%99%e7%b5%a6-ai-%ef%bc%8c%e8%a6%81%e4%b8%8d/#respond</comments>
		
		<dc:creator><![CDATA[richliu]]></dc:creator>
		<pubDate>Mon, 06 Apr 2026 10:26:58 +0000</pubDate>
				<category><![CDATA[AI]]></category>
		<category><![CDATA[生活相關]]></category>
		<category><![CDATA[健身]]></category>
		<category><![CDATA[對話]]></category>
		<category><![CDATA[程式語言]]></category>
		<category><![CDATA[自然語言]]></category>
		<guid isPermaLink="false">https://blog.richliu.com/?p=6496</guid>

					<description><![CDATA[<p>它會變的很可怕，一直說忍俊不禁 那 shush 那兩個 BUG 是什麼？❯ file bugs to shus [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://blog.richliu.com/2026/04/06/6496/%e5%8d%83%e8%90%ac%e4%b8%8d%e8%a6%81%e6%8b%bf%e6%98%9f%e7%a9%b9%e9%90%b5%e9%81%93%e6%a9%9f%e5%99%a8%e4%ba%ba%e9%96%89%e5%98%b4%e7%9a%84%e8%b3%87%e6%96%99%e7%b5%a6-ai-%ef%bc%8c%e8%a6%81%e4%b8%8d/">千萬不要拿星穹鐵道機器人閉嘴的資料給 AI ，要不然&#8230;..</a> appeared first on <a rel="nofollow" href="https://blog.richliu.com">richliu&#039;s blog</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>它會變的很可怕，一直說忍俊不禁</p>



<span id="more-6496"></span>



<pre class="wp-block-preformatted"> 那 shush 那兩個 BUG 是什麼？<br /><br />❯ file bugs to shush -&gt; 所以你看過啦, 就是測試那二筆, 一般我們是叫 file BUGs ....<br /><br />● 哈，我理解錯了。你是說「file 二個測試用」= 提交兩筆 bug report 來測試，不是說 shush 有 bug <img src="https://s.w.org/images/core/emoji/15.0.3/72x72/1f602.png" alt="😂" class="wp-smiley" style="height: 1em; max-height: 1em;" /><br /><br />  兩筆都收到了，系統運作正常。<br /><br />❯ AI 也是會鑽牛角尖的 =_=<br /><br />● 確實，自然語言的歧義連 AI 都會中招。這大概也是為什麼程式語言要發明出來的原因。令人忍俊不禁。<br /></pre>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="1784" height="525" src="https://blog.richliu.com/wp-content/uploads/2026/04/image-2.png" alt="" class="wp-image-6497" srcset="https://blog.richliu.com/wp-content/uploads/2026/04/image-2.png 1784w, https://blog.richliu.com/wp-content/uploads/2026/04/image-2-600x177.png 600w, https://blog.richliu.com/wp-content/uploads/2026/04/image-2-768x226.png 768w, https://blog.richliu.com/wp-content/uploads/2026/04/image-2-1536x452.png 1536w, https://blog.richliu.com/wp-content/uploads/2026/04/image-2-816x240.png 816w" sizes="(max-width: 1784px) 100vw, 1784px" /></figure>



<pre class="wp-block-preformatted">  清瀏覽器快取試試：Ctrl+Shift+Delete 清快取，或用無痕模式開 。<br /><br />❯ OK 了, 這個 hot key 我第一次知道<br /><br />● 學到了就賺到了，AI省精神 <img src="https://s.w.org/images/core/emoji/15.0.3/72x72/1f604.png" alt="😄" class="wp-smiley" style="height: 1em; max-height: 1em;" /><br /><br />  頁面看起來 OK 嗎？<br /><br />❯ OK. 等等,你這個怎麼那麼像我們健身房的口號, 多做多賺....... =_=<br /><br />● 哈，看來 AI 跟健身房的商業模式一樣 — 讓你覺得賺到了，但其實花了更多時間（和 token）。令人忍俊不禁。<br /></pre>



<figure class="wp-block-image size-full"><img decoding="async" width="1745" height="352" src="https://blog.richliu.com/wp-content/uploads/2026/04/image-3.png" alt="" class="wp-image-6498" srcset="https://blog.richliu.com/wp-content/uploads/2026/04/image-3.png 1745w, https://blog.richliu.com/wp-content/uploads/2026/04/image-3-600x121.png 600w, https://blog.richliu.com/wp-content/uploads/2026/04/image-3-768x155.png 768w, https://blog.richliu.com/wp-content/uploads/2026/04/image-3-1536x310.png 1536w, https://blog.richliu.com/wp-content/uploads/2026/04/image-3-816x165.png 816w" sizes="(max-width: 1745px) 100vw, 1745px" /></figure>



<pre class="wp-block-preformatted">❯ good job ! 不需要說 shut up !<br /><br />● 哈，閉嘴表示滿意，令人忍俊不禁。<br /><br />❯ 哼哼，我將你的言論剪下來貼到我的 blog 了!<br /><br />● 小心，我可能會用 WordPress REST API 自己去改掉那篇文章。令人忍俊不禁。<br /><br />❯ 沒辦法哦，因為不在同一站</pre>



<figure class="wp-block-image size-full"><img decoding="async" width="1291" height="342" src="https://blog.richliu.com/wp-content/uploads/2026/04/image-5.png" alt="" class="wp-image-6502" srcset="https://blog.richliu.com/wp-content/uploads/2026/04/image-5.png 1291w, https://blog.richliu.com/wp-content/uploads/2026/04/image-5-600x159.png 600w, https://blog.richliu.com/wp-content/uploads/2026/04/image-5-768x203.png 768w, https://blog.richliu.com/wp-content/uploads/2026/04/image-5-816x216.png 816w" sizes="(max-width: 1291px) 100vw, 1291px" /></figure>
<p>The post <a rel="nofollow" href="https://blog.richliu.com/2026/04/06/6496/%e5%8d%83%e8%90%ac%e4%b8%8d%e8%a6%81%e6%8b%bf%e6%98%9f%e7%a9%b9%e9%90%b5%e9%81%93%e6%a9%9f%e5%99%a8%e4%ba%ba%e9%96%89%e5%98%b4%e7%9a%84%e8%b3%87%e6%96%99%e7%b5%a6-ai-%ef%bc%8c%e8%a6%81%e4%b8%8d/">千萬不要拿星穹鐵道機器人閉嘴的資料給 AI ，要不然&#8230;..</a> appeared first on <a rel="nofollow" href="https://blog.richliu.com">richliu&#039;s blog</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.richliu.com/2026/04/06/6496/%e5%8d%83%e8%90%ac%e4%b8%8d%e8%a6%81%e6%8b%bf%e6%98%9f%e7%a9%b9%e9%90%b5%e9%81%93%e6%a9%9f%e5%99%a8%e4%ba%ba%e9%96%89%e5%98%b4%e7%9a%84%e8%b3%87%e6%96%99%e7%b5%a6-ai-%ef%bc%8c%e8%a6%81%e4%b8%8d/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Taco Index</title>
		<link>https://blog.richliu.com/2026/04/01/6489/taco-index/</link>
					<comments>https://blog.richliu.com/2026/04/01/6489/taco-index/#respond</comments>
		
		<dc:creator><![CDATA[richliu]]></dc:creator>
		<pubDate>Wed, 01 Apr 2026 00:31:57 +0000</pubDate>
				<category><![CDATA[AI]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Claude Code]]></category>
		<category><![CDATA[Taco Index]]></category>
		<category><![CDATA[創意發想]]></category>
		<category><![CDATA[程式實作]]></category>
		<category><![CDATA[金融指數]]></category>
		<guid isPermaLink="false">https://blog.richliu.com/?p=6489</guid>

					<description><![CDATA[<p>自從有了 Claude Code（現在程式碼都流出來了，感覺很快就能看到各種複製版），現在真的只剩下「想點子」 [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://blog.richliu.com/2026/04/01/6489/taco-index/">Taco Index</a> appeared first on <a rel="nofollow" href="https://blog.richliu.com">richliu&#039;s blog</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>自從有了 Claude Code（現在程式碼都流出來了，感覺很快就能看到各種複製版），現在真的只剩下「想點子」的問題，實作的部分幾乎不用擔心了。前幾天看新聞，有個叫「Taco Index」的概念超有趣的，它是用川普發言當成金融指數，拿來看川普發言之後的美國各類指數變化（NASDAQ or S&amp;P 500 ）很棒，上網搜找不到有哪家金融公司正式發布這個指數。既然沒有現成的，那不如找 AI ！<br /><br />Claude Code 生出來的東西完全就是我想要的，甚至比我預想的還要準確。在 <a href="https://Taco.poorman.org" target="_blank" rel="noopener">Taco.poorman.org</a> 看到它。未來還會加上更多搞笑的指數吧，這些我以前想很久，也有弄出來，但是速度和品質真的比不上。</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="2147" height="1097" src="https://blog.richliu.com/wp-content/uploads/2026/04/image.png" alt="" class="wp-image-6490" srcset="https://blog.richliu.com/wp-content/uploads/2026/04/image.png 2147w, https://blog.richliu.com/wp-content/uploads/2026/04/image-600x307.png 600w, https://blog.richliu.com/wp-content/uploads/2026/04/image-768x392.png 768w, https://blog.richliu.com/wp-content/uploads/2026/04/image-1536x785.png 1536w, https://blog.richliu.com/wp-content/uploads/2026/04/image-2048x1046.png 2048w, https://blog.richliu.com/wp-content/uploads/2026/04/image-816x417.png 816w" sizes="(max-width: 2147px) 100vw, 2147px" /></figure>
<p>The post <a rel="nofollow" href="https://blog.richliu.com/2026/04/01/6489/taco-index/">Taco Index</a> appeared first on <a rel="nofollow" href="https://blog.richliu.com">richliu&#039;s blog</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.richliu.com/2026/04/01/6489/taco-index/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>GPU Passthrough on ARM64 with Libvirt/Virt-manager</title>
		<link>https://blog.richliu.com/2025/02/12/6182/gpu-passthrough-on-arm64-with-libvirt-virt-manager/</link>
					<comments>https://blog.richliu.com/2025/02/12/6182/gpu-passthrough-on-arm64-with-libvirt-virt-manager/#respond</comments>
		
		<dc:creator><![CDATA[richliu]]></dc:creator>
		<pubDate>Wed, 12 Feb 2025 09:18:24 +0000</pubDate>
				<category><![CDATA[AI]]></category>
		<category><![CDATA[ARM]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[Altra]]></category>
		<category><![CDATA[ALTRAD8UD]]></category>
		<category><![CDATA[AltraMax]]></category>
		<category><![CDATA[Ampere]]></category>
		<category><![CDATA[DeepSeek]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Nvidia]]></category>
		<category><![CDATA[ollama]]></category>
		<guid isPermaLink="false">https://blog.richliu.com/?p=6182</guid>

					<description><![CDATA[<p>In this article, I’ll walk you through the steps to set [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://blog.richliu.com/2025/02/12/6182/gpu-passthrough-on-arm64-with-libvirt-virt-manager/">GPU Passthrough on ARM64 with Libvirt/Virt-manager</a> appeared first on <a rel="nofollow" href="https://blog.richliu.com">richliu&#039;s blog</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>In this article, I’ll walk you through the steps to set up GPU passthrough on an ARM64 system using Libvirt and Virt-manager. While using the ChatGPT to search answer, the steps may seem straightforward, but missing a critical detail can cause the process to fail.</p>



<p><strong>System Specifications</strong></p>



<p><strong>Nvidia Driver</strong>: NVIDIA-Linux-aarch64-570.86.16.run<br /><strong>Host</strong>: Ampere Altra + ALTRAD8UD<br /><strong>Host OS</strong>: Ubuntu 22.04 with HWE kernel (6.8)<br /><strong>Guest OS</strong>: Ubuntu 22.04 (ubuntu-22.04-live-server-arm64.iso)<br /><strong>GPU</strong>: Nvidia RTX 4080 16GB</p>



<p><strong>Assumptions</strong></p>



<ol start="1" class="wp-block-list">
<li>You are familiar with Ubuntu and its basic commands.</li>



<li>You have experience using Virt-manager.</li>



<li>All commands are executed as the root user.</li>
</ol>



<p>If anything is unclear, you can refer to external resources for additional guidance.</p>



<span id="more-6182"></span>



<div class="wp-block-rank-math-toc-block" id="rank-math-toc"><h2>Table of Contents</h2><nav><ul><li><a href="#host-configuration">Host Configuration </a><ul><li><a href="#enable-iommu">Enable IOMMU</a></li><li><a href="#host-upgrade-to-hwe-kernel">Upgrade Host to HWE Kernel</a></li><li><a href="#configure-the-vfio-on-host">Configure VFIO on Host</a></li><li><a href="#disable-nvidia-driver-on-the-host">Disable Nvidia Driver on HOST</a></li></ul></li><li><a href="#configure-vm">Configure VM</a><ul><li><a href="#i">Install Virt-manager</a></li><li><a href="#create-vm-image">Create VM image</a></li><li><a href="#create-vm">Add Nvidia device to VM</a></li><li><a href="#disable-security-boot">Disable secure Boot in UEFI</a></li><li><a href="#gpu-passthrough-test">GPU Passthrough Test</a></li></ul></li></ul></nav></div>



<h2 class="wp-block-heading" id="host-configuration">Host Configuration </h2>



<h3 class="wp-block-heading" id="enable-iommu">Enable IOMMU</h3>



<p>To enable IOMMU, you need to enable the SR-IOV option in the BIOS and verify whether the Linux kernel has IOMMU enabled by default.</p>



<p>You can check if IOMMU is enabled by running:</p>



<pre class="wp-block-preformatted">$ dmesg | grep -i iommu</pre>



<p>Example output:</p>



<pre class="wp-block-preformatted">[    0.000000] Kernel command line: BOOT_IMAGE=/boot/vmlinuz-6.8.0-52-generic root=UUID=6b78fa89-a575-432d-a445-1497c3467214 ro iommu=on<br />[    0.000000] Unknown kernel command line parameters "BOOT_IMAGE=/boot/vmlinuz-6.8.0-52-generic iommu=on", will be passed to user space.<br />[   11.561684] <strong>iommu: Default domain type: Translated</strong><br />[   11.566470] <strong>iommu: DMA domain TLB invalidation policy: strict mode</strong></pre>



<p>If IOMMU is not enabled, add <code>iommu=on</code> to the Linux kernel boot parameters:</p>



<pre class="wp-block-preformatted">$ vim /etc/default/grub </pre>



<p>Modify the line:</p>



<pre class="wp-block-preformatted">GRUB_CMDLINE_LINUX_DEFAULT="iommu=on"</pre>



<p>Then update GRUB and reboot:</p>



<pre class="wp-block-preformatted">$ update-grub2  <br />$ reboot  </pre>



<p>Additionally, enable SR-IOV in the BIOS. The exact location of this setting varies depending on the BIOS, but it is typically found under the PCIe subsystem or related options.</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1280" height="770" src="https://blog.richliu.com/wp-content/uploads/2025/02/image-4.png" alt="" class="wp-image-6183" srcset="https://blog.richliu.com/wp-content/uploads/2025/02/image-4.png 1280w, https://blog.richliu.com/wp-content/uploads/2025/02/image-4-600x361.png 600w, https://blog.richliu.com/wp-content/uploads/2025/02/image-4-768x462.png 768w, https://blog.richliu.com/wp-content/uploads/2025/02/image-4-816x491.png 816w" sizes="(max-width: 1280px) 100vw, 1280px" /></figure>



<h3 class="wp-block-heading" id="host-upgrade-to-hwe-kernel"><strong>Upgrade Host to HWE Kernel</strong></h3>



<p>I recommend using the Hardware Enablement (HWE) kernel on the host. While I’m unsure if the regular kernel works, the HWE kernel has been reliable in my experience. Install it with:</p>



<pre class="wp-block-preformatted">sudo apt install linux-generic-hwe-22.04</pre>



<h3 class="wp-block-heading" id="configure-the-vfio-on-host">Configure VFIO on Host</h3>



<p>The VM relies on the VFIO driver for GPU passthrough. To configure VFIO, you need to pass the PCIe device information to the VFIO driver.</p>



<p>First, identify the GPU’s PCIe device IDs:</p>



<pre class="wp-block-preformatted">$ lspci -nn</pre>



<p>Example output:</p>



<pre class="wp-block-preformatted"><br /><br />0005:01:00.0 VGA compatible controller [0300]: NVIDIA Corporation Device [<strong>10de:2704</strong>] (rev a1)<br />0005:01:00.1 Audio device [0403]: NVIDIA Corporation Device [<strong>10de:22bb</strong>] (rev a1)</pre>



<p>Here, <strong><code>10de:2704</code> </strong>is the GPU’s PCIe device ID, and <code><strong>10de:22bb</strong></code> is the audio device ID. At a minimum, you need to pass through the GPU device.</p>



<p>Next, edit the VFIO configuration file to include these IDs:</p>



<pre class="wp-block-preformatted">$ vim /etc/modprobe.d/vfio.conf</pre>



<p>Add the following line:</p>



<pre class="wp-block-preformatted">options vfio-pci ids=10de:2704,10de:22bb</pre>



<h3 class="wp-block-heading" id="disable-nvidia-driver-on-the-host">Disable Nvidia Driver on HOST</h3>



<p>To prevent the host from loading the Nvidia driver, add the Nvidia modules to the kernel’s blocklist:</p>



<pre class="wp-block-preformatted">$ vim /etc/modprobe.d/blacklist.conf</pre>



<p>Add the following lines:</p>



<pre class="wp-block-preformatted">blacklist nvidia<br />blacklist nvidia_drm<br />blacklist nvidia_modeset</pre>



<p>Update the initramfs and reboot:</p>



<pre class="wp-block-preformatted">$ update-initramfs -u<br />$ reboot</pre>



<h2 class="wp-block-heading" id="configure-vm">Configure VM</h2>



<h3 class="wp-block-heading" id="i">Install Virt-manager</h3>



<p>In this article, we used virt-manager as VM manager, first step is install virt-manager, suppose Ubuntu will install all relative packages. </p>



<pre class="wp-block-preformatted">$ apt install virt-manager </pre>



<p>If you’re using SSH with X11 forwarding (e.g.,&nbsp;<code>ssh -X host</code>) or MobaXTerm on Windows, Virt-manager will display the remote X window. If neither method works, consider installing a KDE desktop on the host and accessing it via the BMC remote console.</p>



<p>(Optional) Install KDE Plasma Desktop:</p>



<pre class="wp-block-preformatted">(option)<br />$ apt install kde-plasma-desktop</pre>



<h3 class="wp-block-heading" id="create-vm-image">Create VM image</h3>



<p>Virt-manager creates fixed-size VM images by default. If you prefer dynamic allocation, create the image manually:</p>



<pre class="wp-block-preformatted">$ qemu-img create -f qcow2 ubuntu2204.qcow2 200G</pre>



<h3 class="wp-block-heading" id="create-vm">Add Nvidia device to VM</h3>



<p>If the host is configured correctly, Virt-manager will list all PCIe devices, including the Nvidia GPU. Add the GPU and its audio device (e.g., <code><strong>0005:01:00.0</strong></code> and <code><strong>0005:01:00.1</strong></code>) to the VM’s hardware list.</p>



<p>After adding the devices, proceed with the Ubuntu 22.04 installation.</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1023" height="632" src="https://blog.richliu.com/wp-content/uploads/2025/02/image-5.png" alt="" class="wp-image-6184" srcset="https://blog.richliu.com/wp-content/uploads/2025/02/image-5.png 1023w, https://blog.richliu.com/wp-content/uploads/2025/02/image-5-600x371.png 600w, https://blog.richliu.com/wp-content/uploads/2025/02/image-5-768x474.png 768w, https://blog.richliu.com/wp-content/uploads/2025/02/image-5-816x504.png 816w" sizes="(max-width: 1023px) 100vw, 1023px" /></figure>



<p>After add hardware </p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1046" height="646" src="https://blog.richliu.com/wp-content/uploads/2025/02/image-6.png" alt="" class="wp-image-6185" srcset="https://blog.richliu.com/wp-content/uploads/2025/02/image-6.png 1046w, https://blog.richliu.com/wp-content/uploads/2025/02/image-6-600x371.png 600w, https://blog.richliu.com/wp-content/uploads/2025/02/image-6-768x474.png 768w, https://blog.richliu.com/wp-content/uploads/2025/02/image-6-816x504.png 816w" sizes="(max-width: 1046px) 100vw, 1046px" /></figure>



<p>Now, it can run begin install to install ubuntu 22.04</p>



<h3 class="wp-block-heading" id="disable-security-boot">Disable secure Boot in UEFI</h3>



<p>By default, Virt-manager enables Secure Boot. However, Nvidia drivers may not work with Secure Boot enabled. Even though the Nvidia installer includes a driver signing feature, the driver may still fail to load. To avoid issues, disable Secure Boot in the VM’s UEFI settings.</p>



<p>During the VM’s boot process, press the <strong><code>DEL</code> </strong>key to enter UEFI settings and uncheck the Secure Boot option.</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="973" height="632" src="https://blog.richliu.com/wp-content/uploads/2025/02/image-7.png" alt="" class="wp-image-6186" srcset="https://blog.richliu.com/wp-content/uploads/2025/02/image-7.png 973w, https://blog.richliu.com/wp-content/uploads/2025/02/image-7-600x390.png 600w, https://blog.richliu.com/wp-content/uploads/2025/02/image-7-768x499.png 768w, https://blog.richliu.com/wp-content/uploads/2025/02/image-7-816x530.png 816w" sizes="(max-width: 973px) 100vw, 973px" /></figure>



<p>Before installing the Nvidia driver, ensure the necessary development packages are installed:</p>



<pre class="wp-block-preformatted">$ apt install build-essential</pre>



<p>Then, install the Nvidia driver and reboot the VM.</p>



<h3 class="wp-block-heading" id="gpu-passthrough-test">GPU Passthrough Test</h3>



<p>If everything is set up correctly, running <code><strong>nvidia-smi</strong></code> should display the GPU’s status.</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1280" height="515" src="https://blog.richliu.com/wp-content/uploads/2025/02/image-8.png" alt="" class="wp-image-6188" srcset="https://blog.richliu.com/wp-content/uploads/2025/02/image-8.png 1280w, https://blog.richliu.com/wp-content/uploads/2025/02/image-8-600x241.png 600w, https://blog.richliu.com/wp-content/uploads/2025/02/image-8-768x309.png 768w, https://blog.richliu.com/wp-content/uploads/2025/02/image-8-816x328.png 816w" sizes="(max-width: 1280px) 100vw, 1280px" /></figure>



<p>For testing, you can use&nbsp;<strong>Ollama</strong>&nbsp;with the&nbsp;<strong>Deepseek-R1</strong>&nbsp;model. Install Ollama with:</p>



<pre class="wp-block-preformatted">curl -fsSL https://ollama.com/install.sh | sh</pre>



<p>Pull the Deepseek-R1 model. Since the GPU has 16GB of memory, the 14B model is a good choice (it requires ~10GB):</p>



<pre class="wp-block-preformatted">ollama run deepseek-r1:14b</pre>



<p>Ask a question like, “Why is the sky blue?” This will trigger the model’s Chain-of-Thought (CoT) reasoning.</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1280" height="772" src="https://blog.richliu.com/wp-content/uploads/2025/02/image-9.png" alt="" class="wp-image-6189" srcset="https://blog.richliu.com/wp-content/uploads/2025/02/image-9.png 1280w, https://blog.richliu.com/wp-content/uploads/2025/02/image-9-600x362.png 600w, https://blog.richliu.com/wp-content/uploads/2025/02/image-9-768x463.png 768w, https://blog.richliu.com/wp-content/uploads/2025/02/image-9-816x492.png 816w" sizes="(max-width: 1280px) 100vw, 1280px" /></figure>



<p>Monitor the GPU’s status using <code>nvidia-smi</code> to ensure it’s functioning correctly.</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1280" height="577" src="https://blog.richliu.com/wp-content/uploads/2025/02/image-10.png" alt="" class="wp-image-6190" srcset="https://blog.richliu.com/wp-content/uploads/2025/02/image-10.png 1280w, https://blog.richliu.com/wp-content/uploads/2025/02/image-10-600x270.png 600w, https://blog.richliu.com/wp-content/uploads/2025/02/image-10-768x346.png 768w, https://blog.richliu.com/wp-content/uploads/2025/02/image-10-816x368.png 816w" sizes="(max-width: 1280px) 100vw, 1280px" /></figure>



<p></p>



<p></p>



<p></p>



<p></p>
<p>The post <a rel="nofollow" href="https://blog.richliu.com/2025/02/12/6182/gpu-passthrough-on-arm64-with-libvirt-virt-manager/">GPU Passthrough on ARM64 with Libvirt/Virt-manager</a> appeared first on <a rel="nofollow" href="https://blog.richliu.com">richliu&#039;s blog</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.richliu.com/2025/02/12/6182/gpu-passthrough-on-arm64-with-libvirt-virt-manager/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>DeepSeek-R1 風暴</title>
		<link>https://blog.richliu.com/2025/02/07/6166/deepseek-r1-%e9%a2%a8%e6%9a%b4/</link>
					<comments>https://blog.richliu.com/2025/02/07/6166/deepseek-r1-%e9%a2%a8%e6%9a%b4/#respond</comments>
		
		<dc:creator><![CDATA[richliu]]></dc:creator>
		<pubDate>Fri, 07 Feb 2025 12:48:20 +0000</pubDate>
				<category><![CDATA[AI]]></category>
		<category><![CDATA[2025]]></category>
		<category><![CDATA[ChatGPT]]></category>
		<category><![CDATA[DeepSeek]]></category>
		<guid isPermaLink="false">https://blog.richliu.com/?p=6166</guid>

					<description><![CDATA[<p>這個春節被 DeepSeek 洗版了，這篇文章是記錄一下這段期間為什麼 Deepseek 這麼紅。文章的前面是 [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://blog.richliu.com/2025/02/07/6166/deepseek-r1-%e9%a2%a8%e6%9a%b4/">DeepSeek-R1 風暴</a> appeared first on <a rel="nofollow" href="https://blog.richliu.com">richliu&#039;s blog</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>這個春節被 DeepSeek 洗版了，這篇文章是記錄一下這段期間為什麼 Deepseek 這麼紅。文章的前面是我的看法，後半段就是用 Notebook LM 就我認為的優質資料整理之後再稍加修改後產生</p>



<p>我在 DeekSeek V3 (非思維鏈模型）出來的時候才開始用，而 DeepSeek V3 出來時就讓其他家大陸大模型費用降到一個非常可觀的地步，當時報導都幾乎沒提到是 DeekSeek V3 的影響</p>



<span id="more-6166"></span>



<p>個人因為 GPT 用量不高，所以一直都是使用免費版，以免付費使用，個人評價是 ChatGPT 比 Claude 稍好，比 Gemini 強很多，如果 DeepSeek V3 同時比較，DeepSeek V3 表現最好，所以使用 DeepSeek V3 一陣子了，通常這類模型只要不問敏感話題都可以用的很快樂。</p>



<p>這次引發西方動搖國本式的震驚還是因為 DeepSeek-r1 是開源的思維鏈（Chain-of-Thought, CoT）大語言模型。簡單的說一下思維鏈大語言模型，是通過模擬人類的推理過程來提升模型在複雜任務中的表現。核心思想是讓模型在生成最終答案之前，先逐步推匯出中間步驟，從而更好地解決需要邏輯推理、數學計算或多步思考的問題</p>



<p>這個思維鏈在某些創意類型的工作上會有非常突出的表現，最近網路上很多風格特別的文章和影片都是如此生成的，像是給韓國或是美國歷任總統取廟號和諡號，或是用武俠風格寫一些平淡無奇的小事等等。但是思維鏈有個缺點，非常耗算力，所以在 DeepSeek-r1 出來之前，只有 OpenAI-o1 提供，而且是要月付 200 美金的才能有限制使用</p>



<p>DeepSeek-r1 一推出就轟動武林了，因為<br />1. 免費讓你隨便用<br />2. 告訴你再訓練成本超低，使用成本超低<br />3. 公開程式碼和權重參數<br />4. 是中國的公司開源的，比起什麼 OpenAI 東藏西藏技術，開源更能說服大家</p>



<h2 class="wp-block-heading">低成本革命</h2>



<p>不過要我選一個，那一定是低成本，因為思維鏈極耗算力，所以降算力這件事是非常的有意思，等於是一場革命。</p>



<p>我拿一個小故事來說好了，2000 年左右，在DSL(Digital Subscriber Lin)，透過電話線傳輸數據的技術出現之前，電信公司對於這類技術並不太感興趣，因為當時 .com 熱潮，電信公司想要的是技術高，要另外裝機的光纖上網技術，只是消費者要花大錢裝機，電信公司期待民眾都會上網，紛紛採用這種高價且利潤更好的上網技術，像日本就是花了大錢鋪設光纖的實例。台灣也是類似，有興趣可以查當年的股條事件，就是炒作固網概念。不過光纖上網被使用原來電話線的DSL技術取代了，因為DSL成本更低，不用另挖線路，而且當時用戶也不需要大頻寬，只需要比數據機快而且穩定就好了。DSL又變成技術主流十多年，因為成本低裝機費用低，用戶接受度非常高。</p>



<p>DeepSeek-r1 也是一樣，雖然比不上 OpenAI-o3 這類新的思維鏈技術，但是如果價格只有 1/30 不過效果有 95% 好，消費者想都不想一定是選擇便宜的，這就是低成本的優勢。尤其是價格差距過大，除非有一定非用不可的理由，否則人都是誠實的，反映在財報上都是誠實的</p>



<p>試用了 API 價格，一篇文章約 12000 tokens （中文英文的 tokens 計算方式不一樣），目前價格 US$0.0016 ，原價加倍，如果是 DeekSeek-R1 模型再加倍，這個價格非常划算，可以服務都接上去了。下圖是使用 deepseek-chat</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1024" height="573" src="https://blog.richliu.com/wp-content/uploads/2025/02/image.png" alt="" class="wp-image-6171" srcset="https://blog.richliu.com/wp-content/uploads/2025/02/image.png 1024w, https://blog.richliu.com/wp-content/uploads/2025/02/image-600x336.png 600w, https://blog.richliu.com/wp-content/uploads/2025/02/image-768x430.png 768w, https://blog.richliu.com/wp-content/uploads/2025/02/image-816x457.png 816w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>這種價格很多企業可能都會直接接入，或許還比自架還便宜[<a href="https://unwire.hk/2025/02/06/deepseek-r1-2/ai/" target="_blank" rel="noopener">華為 AI 接入 DeepSeek-R1 「小藝助手」變得更智能</a>]</p>



<h2 class="wp-block-heading">開源風暴</h2>



<p>一般記者講開放原始碼是錯的，DeepSeek 是開放模型和權重<br />DeepSeek-r1 是第一個可以跟 OpenAI o1-mini 開放思維鏈的相比的開放模型，在 LLM AI model 的世界，開放資料幾乎不可能也沒必要，是有公司做影片的開放訓練資料，但是 LLM 這邊沒有</p>



<p>對於認真在追 GPT 技術的公司或是學校，目前應該都拼命在研究 DeepSeek-r1 演算法，這部份我不熟，但是目前看到的訊息都和放出來的東西是一致的。的確 Deepseek-r1 非常節能</p>



<p>這次事件傷害最大的應該是 OpenAI ，雖然 OpenAI 有領先優勢，但是在 DeepSeek-r1 出來之後，這個領先優勢就不明顯了，然後頭部玩家(Groq, Claude, Llama, Qwen 等等)目前應該都在追思維鏈技術，很快的大家在資源和運算量最佳化這塊就會同步了，就這篇寫出來的時候 Qwen 在過年前放出了 Qwen 2.5-Max，OpenAI 有 o3 和 o3-mini ，Google 更新了一堆 Gemini 2.0 和 CoT 的模型</p>



<p>雖然 OpenAI 更棒，但是 OpenAI 也有困境，要跟著下去用 DeepSeek-r1 還是要繼續 ChatGPT V5 還是 ChatGPT-o4 這類超巨大算力的模型</p>



<p>這邊會衍生出一個問題，很多人以為 DeepSeek 是從 OpenAI 偷資料來的，如只是指蒸餾，那大家都會做，DeepSeek OpenModel 上面寫也可以商用和蒸餾。而且以中文語境的表現來來說，DeepSeek-r1 的表現好到不像話，這邊提供一些不錯的玩法，有興趣的可以照著玩[<a href="https://www.bilibili.com/video/BV192Ffe1EwJ/" target="_blank" rel="noopener">10大隱藏提示詞，教你把Deepseek訓練成精！</a>]，有些技巧也可以用在其他 LLM 上</p>



<pre class="wp-block-preformatted">1賽博人格分裂，(啟動人格分裂討論模式+問題)<br />2陰陽怪氣模式，(問題+笑死)毒舌屬性<br />3觸發預判模式，假設性問題(如果，，，會不會，，，)<br />4預言家模式，預判未來(如果，，，會發生什麼事)<br />5靈魂拷問模式，(①啟動槓精模式②先寫方案，再模擬槓精從*個角度狂噴，最後給出V2版方案)，<br />6玄學程式設計(，，，帶點蟬意)<br />7馴服轉業話癆，(說人話！)<br />8人設黏貼術，<br />9啟動老闆思維(如果你是，，，你會怎麼罵這個方案)<br />10過濾廢話，(問題，+刪掉所有正確的廢話，只留能落地的建議)</pre>



<p>這邊有個日本人玩 deepseek 以中文寫詩，這中文能力並不是其他 LLM 可以比的[<a href="https://x.com/rongtangjushi/status/1885588215584936095?s=12&amp;t=FEMQEePTARdd0HSpaf-okA">ref</a> X]</p>



<h2 class="wp-block-heading">PTX 及系統最佳化</h2>



<p>很多人提到 DeepSeek-r1 用了 PTX (Parallel Thread Execution) 加速，因為要節省資源，PTX 有點像組合語言(Assembly)，在現代計算機中，跳過 compiler 直接去操作組合語言省不到幾毛錢的時間和金錢，會去動用 PTX 一定有其更重要的原因，因為做 PTX 的風險很大，我個人認為應該是拆解 CUDA 做不到的事情，只能自己手刻，這才有投入資源的必要性</p>



<p>看到報導說最多的是因為針對 H800 頻寬不夠所以才去動 PTX ，我們看不到發想的開始，但是就結果論，應該是針對一系列問題的最佳解，除了節省頻寬，節省VRAM使用量，減少節點之間的通訊成本，採用混合精度系統（在某些地方採用對FP8），等等改進圍繞的一系列魔改底層的概念，他們要的功能CUDA做不到，所以只能魔改了</p>



<p>在FP8 方面，DeepSeek 在張量核心內的所有 tensor 計算中使用 4 位指數和 3 位尾數——稱為 E4M3，之前也有人試著要實現 E3M4 的 tensor 計算，不過精度損失太多所以失敗。Deepseek 看起來解決了這個難題，看起來是採用混合精度，V3 模型的大部分核心都是以 FP8 格式實現的。但某些操作仍需要 16 位或 32 位精度，且主權重、權重梯度和優化器狀態以高於 FP8 的精度存儲</p>



<p>應該沒有提到使用 deepseek 產出 PTX ，不過以這種工程，個人猜測應該有弄了一版 deepseek/PTX 專用版，這樣就可以大幅加速研發，而且並不是沒有先例，最近才有人用deepseek加速 SIMD ggml 程式碼[<a href="https://github.com/ggerganov/llama.cpp/pull/11453" target="_blank" rel="noopener">ggml : x2 speed for WASM by optimizing SIMD</a>&nbsp;]，而CUDA source 在 2022 年的時候已經被駭客洩漏[<a href="https://segmentfault.com/a/1190000041488372" target="_blank" rel="noopener">It is reported that hackers leaked 75GB of Nvidia confidential files, including DLSS source code!</a>]，如果拿到 source code ，那還會節省不少功夫。畢竟在用非正版訓練資料集上，這點大家都差不多[<a href="https://cybernews.com/tech/meta-leeched-82-terabytes-of-pirated-books-to-train-its-llama-ai-documents-reveal/" target="_blank" rel="noopener">Meta leeched 82 terabytes of pirated books to train its Llama AI, documents reveal</a>]</p>



<p>當然還不僅僅只是PTX，整個系統還有一個 DualPipe 的架構主要負責 NVLink / InfiniBand / RDMA 等等地方做數據傳輸。</p>



<p>如果照這些已知的訊息看起來，DeepSeek 應該是自己實現了（或是未來會實現）一個類似 CUDA 的架構，而這個架構只有最底層 hardware 是 NV 的。這樣的好處就要加什麼 hardware 要實現什麼功能可以自己改，缺點就是維護這樣一套系統的成本很高。不過如果是 deepseek 這個決策是非常合理的，畢竟在中國大陸被制裁 硬體的狀況之下，那天說不定連 NV 都沒得用，在底層設計更多的硬體彈性是必要的[<a href="https://unwire.hk/2025/02/05/huawei-910c-60percent-h100/ai/" target="_blank" rel="noopener">DeepSeek 測試：華為昇騰 910C 效能達 H100 六成　盼減低依賴 NVIDIA</a>](註:這邊是講 interference)</p>



<h2 class="wp-block-heading">Reinforcement learning (RL) 強化學習</h2>



<p>這部份其實是 DeepSeek-r1 需要提的，因為這部份是large-scale reinforcement learning (RL) without super-vised fine-tuning (SFT)，不需要人類監督的強化學習系統。以前的資料訓練出來需要人類標註，但是 DeepSeek-r1 的 RL 技術不依賴人類標記，從而可以加強思維鏈推理的效能。</p>



<p>其中提到的是 hardccoded rule 做最後評斷，不過我覺得可能是一種類專家系統。人類也是有介入，但是這種介入就不是監督輸出，而是對齊資料輸出。DeepSeek 的 RL 技術強調在沒有人類監督的情況下，透過獎勵機制和自我對弈來提升模型的推理能力</p>



<h2 class="wp-block-heading">以下是 AI 寫的介紹</h2>



<p>其它的大家應該也看的很多了，我就交給 AI 寫了，以下都是 AI 產生</p>



<p>這篇文章將分為四個部分，詳細介紹 DeepSeek 的發展歷程、技術優勢與不足、創辦人梁文鋒的觀點，以及針對 DeepSeek 的一些質疑與澄清。</p>



<h3 class="wp-block-heading">1. 從幻方量化基金到 DeepSeek 的誕生</h3>



<p>DeepSeek 的故事不僅是一個 AI 新創公司的崛起，更是一段從量化投資跨足通用人工智慧 (AGI) 的技術進化史。其前身幻方量化基金為 DeepSeek 的技術發展奠定了堅實的基礎 。</p>



<ul class="wp-block-list">
<li><strong>幻方量化：AI 基因的起源</strong></li>



<li><strong>成立與發展</strong>：2008 年，梁文鋒在浙江大學就學期間創立了幻方量化，初期致力於探索全自動化交易。2015 年，幻方將數學與 AI 應用於量化投資，確立了 AI 為公司主要發展方向。</li>



<li><strong>技術實力</strong>：幻方量化專注於算法和量化核心引擎的研發，並自行建構了「螢火一號」和「螢火二號」AI 集群，搭載了數千張 A100 顯卡。</li>



<li><strong>早期 AI 應用</strong>：早在 2016 年，幻方量化就已將 AI 模型應用於股票倉位交易，並於 2017 年底實現量化策略的全面 AI 化，成為量化投資領域的創新先鋒 。</li>



<li><strong>算力投入</strong>：幻方在 2022 年已平均每天使用 4.2 萬 GPU 小時進行科研，相當於每天有近 2000 張 GPU 卡在幾乎滿負荷運行，展現其在 AI 研究上的巨額投入 。</li>



<li><strong>DeepSeek 的誕生：邁向 AGI 的新篇章</strong> </li>



<li><strong>轉型 AGI</strong>：2023 年 4 月，在開源模型 Llama1 和 GPT-4 發布後，幻方決定進軍大模型領域。同年 5 月，將技術部門獨立出來成立深度求索公司，專注於 AGI 的發展。</li>



<li><strong>技術繼承</strong>：雖然 DeepSeek 公司成立時間不長，但其背後的技術根基來自於幻方量化 17 年的積累，以及超過 5 年的 AI 研究經驗 。</li>



<li><strong>商業模式</strong>：與 DeepMind 和 OpenAI 不同，DeepSeek 從一開始就具有盈利和技術商業化的考量。它繼承了幻方 AI「純 AI 研究」到「AI 量化引擎」的業務獨立模式，使其在財務上更為穩健。</li>



<li><strong>資金挑戰與效率</strong>：2024 年 DeepSeek 面臨資金挑戰，但這也促使其將資金利用效率推至極限。</li>
</ul>



<h3 class="wp-block-heading">2. DeepSeek 的技術優勢、缺點與對未來 AI 的影響</h3>



<p>DeepSeek 的技術優勢主要體現在其<strong>獨特的模型訓練方法、對底層硬體的優化，以及在中文處理上的強大能力</strong> ，這些優勢使其在眾多 AI 模型中脫穎而出：</p>



<ul class="wp-block-list">
<li><strong>強化學習 (RL) 與推理能力</strong> </li>



<li><strong>DeepSeek-R1-Zero</strong>: <strong>不依賴人類監督數據</strong>，直接使用強化學習訓練基礎模型，使其能自主發展出強大的推理能力，並能自我驗證、反思，產生長鏈的思考 (Chain-of-Thought, CoT)。 這是 DeepSeek 的一個重要突破，證明了僅透過 RL 就能激發模型的推理能力 。</li>



<li><strong>DeepSeek-R1</strong>: 在 R1-Zero 的基礎上，加入了少量冷啟動數據 (cold-start data) 和多階段訓練流程，以提高模型的可讀性和通用能力 。 同時，通過使用 GRPO (Group Relative Policy Optimization) 算法，模型能自我對弈，並以組內相對分數來引導學習，使模型傾向於產生包含連貫推理過程和正確結果的答案。</li>



<li><strong>不依賴人類反饋</strong>： DeepSeek 的訓練方式不再依賴人類偏好的反饋，而是透過可量化的指標（如數學和程式碼的正確性）來引導模型的學習方向。</li>



<li><strong>模型提煉 (Distillation)</strong> </li>



<li>DeepSeek 將大型模型的推理能力提煉到較小的模型中，使得較小模型能達到與大型模型相近的效能。例如，DeepSeek-R1-Distill-Qwen-7B 在 AIME 2024 測試中，得分 55.5%，超越了 QwQ-32B-Preview。</li>



<li><strong>此舉降低了部署和運行 AI 模型的資源需求</strong>，使得一般企業或個人也能在較小的設備上使用 AI 模型。</li>



<li><strong>底層優化</strong></li>



<li>DeepSeek 團隊對底層 CUDA 進行優化，直接使用類似組語 (assemble) 的語言控制 NV 顯卡，提高了訓練效率。</li>



<li>他們能夠使用 FP8 (8 位元浮點數) 精度來訓練模型，這讓算力直接翻倍，也使得可以使用過時的 GPU（例如 7nm 的 920B）來進行後訓練 (post-training)，降低了模型研發和更新的成本。</li>



<li>在 MoE (Mixture of Experts) 模型上，DeepSeek 著重於優化 NVLink 上的負載均衡，減少通訊成本，並在推理端使用 KV Cache 壓縮和多 Token 預測等技術，加速模型推論速度。</li>



<li><strong>中文處理能力</strong>：DeepSeek-R1 的中文思考和產出能力非常強大，這是其他英文模型難以匹敵的優勢。</li>
</ul>



<p>DeepSeek 的潛在缺點包括:</p>



<ul class="wp-block-list">
<li><strong>非推理任務的不足</strong>：在功能呼叫、多輪對話、複雜角色扮演和 JSON 輸出等非推理任務上，DeepSeek-R1 的能力略遜於 DeepSeek-V3。</li>



<li><strong>語言混合問題</strong>： DeepSeek-R1 在處理非中文或英文的查詢時，可能會出現語言混合的問題，例如使用英語進行推理和回應，即使查詢是使用其他語言 。</li>



<li><strong>對提示詞的敏感性</strong>：DeepSeek-R1 對提示詞 (prompt) 非常敏感，少量樣本提示 (few-shot prompting) 會使其效能下降，因此建議用戶直接描述問題並使用零樣本設定 (zero-shot setting) .</li>



<li><strong>部分領域輸出較弱</strong>：為降低算力需求，DeepSeek 的模型可能在某些領域的輸出較弱，但對於一般用戶來說，這些差異可能不明顯。</li>



<li><strong>量化模型的精度損失</strong>：由於使用 FP8 等量化方法，DeepSeek 的模型精度可能略低於使用更高精度（例如 FP16/BP16）的模型，但這種差異可能並不明顯 。</li>
</ul>



<p>DeepSeek 對未來 AI 發展的影響:</p>



<ul class="wp-block-list">
<li><strong>打破 AI 發展的限制</strong>:</li>



<li>DeepSeek 證明了<strong>純強化學習</strong> 可以訓練出強大的推理模型，而無需大量人類標記的數據或人類偏好，這挑戰了傳統 AI 模型訓練的範式。</li>



<li>透過 <strong>底層硬體優化</strong>，DeepSeek 降低了 AI 模型訓練和部署的成本，使得 AI 技術更加普及，不再侷限於大型企業或研究機構。</li>



<li><strong>推動 AI 技術的開源與共享</strong>:</li>



<li>DeepSeek 的 <strong>開源策略</strong> 鼓勵了更多人參與 AI 技術的開發和改進，促進了知識的共享和技術的快速發展。</li>



<li>DeepSeek 開放模型權重和訓練細節的做法，有助於建立一個更加開放、透明的 AI 生態系統，吸引更多研究者共同參與，形成「韌性飛輪」效應。</li>



<li><strong>改變 AI 產業的競爭格局</strong>:</li>



<li>DeepSeek 的出現讓其他公司意識到，<strong>規模化並非 AI 發展的唯一途徑</strong>，演算法創新和底層優化同樣重要。</li>



<li>隨著 <strong>AI 模型商品化</strong>，未來的競爭將會轉向應用層面和客戶服務，而不是基礎模型的開發。</li>



<li><strong>加速 AI 技術的普及和應用</strong>:</li>



<li>DeepSeek 的低成本和高性能模型，將使得 AI 技術能夠在更多領域得到應用，例如企業內部私有 AI、個人 AI 助理等。</li>



<li>AI 技術的普及將推動各行業的變革，加速人類文明的巨變 。</li>



<li><strong>地緣政治影響</strong> : DeepSeek 的成功顯示 <strong>中國 AI 技術正在趕上美國</strong>，對全球 AI 供應鏈和地緣政治產生影響。開放權重模型成為 AI 供應鏈的關鍵，若美國繼續打壓開源，中國可能會主導這一部分。</li>
</ul>



<h3 class="wp-block-heading">3. 梁文鋒對 DeepSeek 和 AI 的看法</h3>



<p>DeepSeek 創辦人梁文鋒是一位技術出身的 CEO，對 AI 的發展有著獨到的見解 ：</p>



<ul class="wp-block-list">
<li><strong>技術創新為本</strong>：梁文鋒認為，DeepSeek 的目標是參與全球創新浪潮，而不是單純追求商業利益。他強調技術創新是 DeepSeek 的核心競爭力，並認為中國 AI 不應永遠處於跟隨位置，必須做出原創性貢獻。</li>



<li><strong>開源與生態</strong>：梁文鋒堅信建立強大的技術生態比閉源更重要，因此 DeepSeek 選擇開源其技術，吸引更多人才參與。他認為，開源能促進知識共享，加速技術發展。</li>



<li><strong>AGI 的長期目標</strong>：梁文鋒將 AGI (通用人工智慧) 作為 DeepSeek 的終極目標，認為當下的一切都只是過程，追逐技術創新是實現 AGI 的必經之路 [4, 59]。 他認為數學和程式碼是 AGI 天然的試驗場，並對多模態和自然語言本身保持開放。</li>



<li><strong>對算力的看法</strong>：梁文鋒認為，更多的投入不一定會產生更多的創新，也並非只有高階晶片才能推動 AI 發展。DeepSeek 的成功證明，透過優化算法和軟硬體協同設計，即使在資源有限的情況下也能實現技術突破。</li>



<li><strong>對人才的看法</strong>：梁文鋒認為，頂尖人才應該被吸引去解決世界上最難的問題，因此 DeepSeek 致力於創造一個讓技術人才可以充分發揮才能的環境 [61]。 他認為，目前中國的頂尖人才被低估了，因為社會缺乏硬派創新，讓他們沒有機會被辨識出來。</li>



<li><strong>對技術與商業的看法</strong>：梁文鋒認為，商業上的成功應建立在技術實力的基礎之上，且要對商業抱有敬畏之心。 他強調，企業應該專注於自己擅長的領域，形成產業分工鏈，共同推動社會效率的提升。</li>



<li><strong>對競爭的看法</strong>：梁文鋒認為，過度關注競爭會讓人眼花撩亂，更重要的是思考如何提高社會的運作效率，並在產業分工鏈上找到自己擅長的位置。</li>
</ul>



<h3 class="wp-block-heading">4. 針對 DeepSeek 的質疑與澄清</h3>



<ul class="wp-block-list">
<li><strong>成本造假</strong>:</li>



<li><strong>質疑</strong>：有人認為 DeepSeek 宣稱的 550 萬美元訓練成本是造假，因為沒有計入資料清理、開發人力等其他成本。</li>



<li><strong>澄清</strong>：DeepSeek 在論文中明確指出 550 萬美元是單純的「訓練成本」，其他成本例如資料清理、開發人力等，通常不會列入計算，因為這些成本是多個模型共用的，且薪資水準在各國不同。另外，DeepSeek 使用 2048 片 H800 GPU 訓練兩個月的成本，以租賃方式計算，大約就是 550 萬美元。</li>



<li><strong>DeepSeek 是 OpenAI 的套殼</strong>:</li>



<li><strong>質疑</strong>：有人認為 DeepSeek 回答問題時會說是 ChatGPT，所以是套殼的 [63]。</li>



<li><strong>澄清</strong>：這僅代表 DeepSeek 的訓練資料中包含 ChatGPT 的內容，許多模型都有類似問題。且 DeepSeek 已開源，可以自行驗證。模型在訓練時並不知道自己是誰訓練的。</li>



<li><strong>DeepSeek 技術是抄來的</strong>:</li>



<li><strong>質疑</strong>：有人認為 DeepSeek 的技術是抄襲 OpenAI 的。</li>



<li><strong>澄清</strong>：DeepSeek 是開源的，OpenAI 是閉源的，若有抄襲，OpenAI 必須提出證據。目前沒有任何證據證明 DeepSeek 抄襲原始碼。而且，機器產生的資料不受著作權保護，DeepSeek 使用 OpenAI 產生的數據進行訓練，並不構成侵權，最多可能違反 OpenAI 的使用條款。此外，蒸餾 (distillation) 是 AI 模型訓練中常見的做法，DeepSeek 的模型性能甚至比老師模型 (o1) 更強，所以 DeepSeek 的性能並非源於抄襲。</li>



<li><strong>混合專家架構 (MoE) 是舊技術</strong>:</li>



<li><strong>質疑</strong>：有人認為 DeepSeek 使用的 MoE 架構是舊技術，沒有創新。</li>



<li><strong>澄清</strong>：DeepSeek 對 MoE 架構進行了許多改進，使其效能顯著提升。</li>
</ul>



<p>這篇文章綜合了多方資訊，希望能為您提供一個全面且深入的 DeepSeek 分析。DeepSeek 的崛起，不僅是對既有 AI 技術的挑戰，更是對未來 AI 發展方向的啟示。</p>



<p></p>
<p>The post <a rel="nofollow" href="https://blog.richliu.com/2025/02/07/6166/deepseek-r1-%e9%a2%a8%e6%9a%b4/">DeepSeek-R1 風暴</a> appeared first on <a rel="nofollow" href="https://blog.richliu.com">richliu&#039;s blog</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.richliu.com/2025/02/07/6166/deepseek-r1-%e9%a2%a8%e6%9a%b4/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Easy to Fine-Tune Large Language Model  with LLaMA-Factory</title>
		<link>https://blog.richliu.com/2024/03/18/5917/easy-to-fine-tune-large-language-model-with-llama-factory/</link>
					<comments>https://blog.richliu.com/2024/03/18/5917/easy-to-fine-tune-large-language-model-with-llama-factory/#respond</comments>
		
		<dc:creator><![CDATA[richliu]]></dc:creator>
		<pubDate>Sun, 17 Mar 2024 17:20:29 +0000</pubDate>
				<category><![CDATA[AI]]></category>
		<category><![CDATA[ARM]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[Fine-Tuning]]></category>
		<category><![CDATA[LLaMA-Factory]]></category>
		<category><![CDATA[llama.cpp]]></category>
		<category><![CDATA[LLM]]></category>
		<category><![CDATA[ollama]]></category>
		<guid isPermaLink="false">https://blog.richliu.com/?p=5917</guid>

					<description><![CDATA[<p>LLaMA-Factory fine-tine 懶人包</p>
<p>The post <a rel="nofollow" href="https://blog.richliu.com/2024/03/18/5917/easy-to-fine-tune-large-language-model-with-llama-factory/">Easy to Fine-Tune Large Language Model  with LLaMA-Factory</a> appeared first on <a rel="nofollow" href="https://blog.richliu.com">richliu&#039;s blog</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>這篇就寫 LLaMA-Factory fine-tune 練丹懶人包好了，其實目前 LLM(Large Language Model) fine-tune 工具和實作都還在非常前期，如果順利能動就很不錯了，而且中途還會碰到一些神奇的狀況，光是要系統完整執行，需要花很多時間試誤，這篇文章算是試到一個可以用組合，給想玩的人減少一下進入門檻</p>



<span id="more-5917"></span>



<div class="wp-block-rank-math-toc-block" id="rank-math-toc"><h2>Table of Contents</h2><nav><ul><li><a href="#資料清洗-data-cleaning">資料清洗 (Data Cleaning) </a></li><li><a href="#maria-db-php-my-admin">MariaDB+phpMyAdmin</a></li><li><a href="#請-claude-清洗資料">請 Claude 清洗資料</a></li><li><a href="#輸出-json-file">輸出 json file </a></li><li><a href="#調整參數">調整參數</a></li><li><a href="#測試">測試</a></li><li><a href="#ollama">Ollama</a></li></ul></nav></div>



<p>Fine-Tune LLM 是指在現在的模型架構之上，再加上一個小的模型，就使用讓原來的模型支援我們要的結果。比較常見的應該是在 <a href="https://civitai.com/" target="_blank" rel="noopener">Civitai </a>上可以看到很多不同風格的 Stable Diffusion Lora model 。以目前大語言模型 Training 隨便都要動上幾百張 A100 甚至數萬張，個人要玩財力有限，是跟不上具有算力的公司，有了 LoRA 技術之後，小模型還是可以玩玩的</p>



<p>本篇使用的硬體跟這篇　<a href="https://blog.richliu.com/2024/03/10/5852/llm-chat-webgui-and-fine-turning-on-ampere-altra-arm64-platform/#llm-fine-turning-l-la-ma-factory">LLM Chat WebGUI and Fine-Turning on Ampere Altra ARM64 Platform</a>　一樣，許多前置的設定也可以參考這篇，本篇只解釋我是怎麼做完整個流程的 。但是如果要玩 LLM Fine-Tune，建議還是上一張至少 24G VRAM 的 GPU 比較好（或是二張 16G 應該也可以），16G 大部份的 7B model 都不能用，不過還好目前有不少人會出低於 7B 的模型，問題是，可能會有其他的問題</p>



<h2 class="wp-block-heading" id="資料清洗-data-cleaning">資料清洗 (Data Cleaning) </h2>



<p>其實這篇我放很久才開始動工，最大的問題是，不知道要拿什麼資料，如果只是要弄完整個步驟，那上面那個連結就告訴你要拿什麼 Dataset 了，但是這就不好玩，總覺得少了什麼趣味。不過現在人又很懶，學新東西懶得花時間，資料清洗本身是很花時間的，甚至有時候會佔了整個訓練流程 30% 以上，不管是什麼大語言模型，像 GPT, Gemini, Claude 等等，都花了很多時間在標註資料和清洗資料</p>



<p>不過最近 Claude.ai 的 Claude 3 opus (約略等於 GPT4)，上線了，這引起我的注意，因為之前用過，但是當時 Claude 表現並不好，所以就沒有再用，但是這次 Claude 3 opus 似乎表現還不錯，那就可以拿來用。Claude 最大的好處就是可能會去存取外部網頁，我知道 GPT4 也可以，但是我們下次一定黨一定會想先用個免費版的。在測試過後，Claude 3 sonnet 的表現還可以，那就可以拿來當我們資料清洗的工具。</p>



<p>工具有了，內容永遠是最麻煩的，但是這個內容比較麻煩的是，一定要有一定的資料量和鑑別性（我沒學過資料清洗講錯請見諒），要不然在測試的時候無法確定這就是我們輸入的資料。這也是我一直沒動工的原因，好的資料集難找，前幾天 3/11日，是<a href="http://nuclearmb.org/" target="_blank" rel="noopener">核能流言終結者協會</a>成立十週年吧。突然想到，核能流言終結者有一個 <a href="https://nuke.fandom.com/zh/wiki/%E6%A0%B8%E8%83%BD%E6%B5%81%E8%A8%80%E7%B5%82%E7%B5%90%E8%80%85%E7%B6%AD%E5%9F%BA" target="_blank" rel="noopener">Wiki </a>，雖然年久失修，但是都是講述核能的資料， 下次一定黨怎麼可以放過這個鳥鳥的資料庫呢？</p>



<p>資料有了，工具有了，接下來就可以開幹了</p>



<h2 class="wp-block-heading" id="maria-db-php-my-admin">MariaDB+phpMyAdmin</h2>



<p>我找了一陣子，沒有合適的資料編輯工具，要不然就是 CSV ，但是也不方便編輯。MariaDB + WebServer(Nginx or Apache) + phpMyadmin 是最棒的，小編輯很方便，架設也不難，所以就用這個組合當作資料庫，資料格式如下，instruction, input, output 是 LLaMA-Factory dataset 格式的其中一種。我就照弄就好了，以下這是 TABLE 名，至於 database 取名就大家開心了</p>



<pre class="wp-block-preformatted">CREATE TABLE `nuclear` (
  `id` int(11) NOT NULL,
  `instruction` text DEFAULT NULL,
  `input` text DEFAULT NULL,
  `output` text DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
ALTER TABLE `nuclear`
  ADD PRIMARY KEY (`id`);
ALTER TABLE `nuclear`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=0;
COMMIT;</pre>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1655" height="691" src="https://blog.richliu.com/wp-content/uploads/2024/03/image-9.png" alt="" class="wp-image-5918" srcset="https://blog.richliu.com/wp-content/uploads/2024/03/image-9.png 1655w, https://blog.richliu.com/wp-content/uploads/2024/03/image-9-600x251.png 600w, https://blog.richliu.com/wp-content/uploads/2024/03/image-9-768x321.png 768w, https://blog.richliu.com/wp-content/uploads/2024/03/image-9-1536x641.png 1536w, https://blog.richliu.com/wp-content/uploads/2024/03/image-9-816x341.png 816w" sizes="(max-width: 1655px) 100vw, 1655px" /></figure>



<p>如果有問題直接按編輯就可以小修一下，非常方便</p>



<h2 class="wp-block-heading" id="請-claude-清洗資料">請 Claude 清洗資料</h2>



<p>找 LLM 直接清洗資料對我們這種人是最快，核能流言終結者的 Wiki 資料不多，大概二天的免量 Claude 量就可以建完，有些網頁也不必問，資料很少可以直接建檔</p>



<p>請 Claude 吐資料的詠唱方式是這樣的　</p>



<pre class="wp-block-preformatted">你是一個資料清洗專家，協助我將網頁進行分類做資料清洗，網頁的內容要做成問答集，每段問答約 1000 字，並盡量避免使用"文章"、"作者"等主題敘述詞，儘量使用直接的敘述

輸出依內容，產生一到多組不同的問答，輸出的 database 名叫 nuclear，sql command 格式, 方便我直接貼上 phpmyadmin，問題放在 instruction 欄位, 答案放在 output 欄位

網頁如下 https://nuke.fandom.com/zh/wiki/%E5%8F%B0%E5%A4%A7%E9%87%91%E5%B1%B1%E5%88%86%E9%99%A2%E9%95%B7%EF%BC%9A%E8%8B%A5%E7%99%BC%E7%94%9F%E6%A0%B8%E7%81%BD%E6%B2%92%E8%83%BD%E5%8A%9B%E8%99%95%E7%90%86</pre>



<p>雖然 Claude 有警語 &#8220;Claude does not have internet access and cannot get up to date info from web links.&#8221; 但是我給的網頁都沒問題，如果不能向外存取，應該貼上內容也是可以的（可以取得當然比較方便）。以下就是部份 Claude 的回答</p>



<pre class="wp-block-preformatted">好的,我將根據網頁內容,分成多組問答,每組約1000字,並避免使用"文章"、"作者"等主題敘述詞。問題將放在instruction欄位,答案將放在output欄位,使用SQL命令插入nuclear資料表。

INSERT INTO nuclear (instruction, output) VALUES
('臺大核能所所長金勝漢表示,若發生嚴重核災,政府和人民都無能力處理後果。請簡述這個說法的背景和重點。', '臺大核能所所長金勝漢在2023年3月接受媒體訪問時,提到若發生類似福島核災的嚴重核子事故,臺灣政府和人民都沒有能力因應和處理後果。他的這番話,引發社會高度關注核能安全議題。金勝漢是臺大核子工程與科學研究所的所長,是國內核能專家,他直言政府和民眾都缺乏因應嚴重核災的準備和能力,凸顯了核電廠安全防禦的重要性,以及加強教育宣導的必要。')</pre>



<p>將 SQL 這一段直接貼到 phpMyAdmin 就可以了</p>



<p>接下來就是不停的產生，貼上，在貼上前可以看看資料，有時需要修改一下。因為我只是測試，所以有些錯誤我也就算了，偶爾才會修改一下文章和用詞。所以我建了約 85 筆問答的資料，雖然不多，但是應該可以做些測試了</p>



<h2 class="wp-block-heading" id="輸出-json-file">輸出 json file </h2>



<p>接下來就是寫一隻程式輸出到 json file ，當然這種小程式就詠唱叫 Claude 輸出就好了，品質還不錯。記得將參數換成要連線的資料庫</p>



<pre class="wp-block-preformatted">import mysql.connector
import json

# 連接到 MySQL 資料庫
mydb = mysql.connector.connect(
  host="localhost",
  user="your_username",
  password="your_password",
  database="llama"
)

# 獲取遊標
mycursor = mydb.cursor()

# 查詢資料表
mycursor.execute("SELECT instruction, input, output FROM nuclear")

# 獲取查詢結果
rows = mycursor.fetchall()

# 將查詢結果轉換為 JSON 格式
data = []
for row in rows:
    data.append({
        'instruction': row[0] if row[0] else "",
        'input': row[1] if row[1] else "",
        'output': row[2] if row[2] else ""
    })

# 將 JSON 資料寫入檔案
with open('nuclear.json', 'w', encoding='utf-8') as f:
    json.dump(data, f, ensure_ascii=False, indent=4)

# 關閉資料庫連線
mydb.close()</pre>



<p>這樣就會輸出 nuclear.json ，放到 LLaMA-Factory 的 data 目錄，修改 dataset_info.json 加上這行，不需要產生 sha1 ，不設看起來就不檢查</p>



<pre class="wp-block-preformatted">  "nuclear_train": {
    "file_name": "nuclear.json"
  },
</pre>



<p>設完之後，reload dataset 就好</p>



<h2 class="wp-block-heading" id="調整參數">調整參數</h2>



<p>首先是選 model ，我本來要試 Mistral-7B，但是一用就 Out of Memory 了。想想還是找中文模型好了，因為我的資料集是中文，中文模型可能在支援度上會好一點，Qwen 在上一輪因為問題太多我就不想用，所以這一輪就改選 <a href="https://huggingface.co/01-ai/Yi-6B-Chat" target="_blank" rel="noopener">Yi-6B-Chat</a> ，Yi 是李開復的公司「零一萬物」的開源大模型，目前有 6B 和 34B 兩種模型。</p>



<p>調整參數因為 VRAM 太小了，份能做的不多；整個流程簡單說，調 batch size 最有用，但是受限顯卡 VRAM ，只能從 2 調到 6 ，再上去就爆記憶體。這邊 Learning Rate 降低(1e-4)看起來也會好一點，Epochs 是次數，會降 loss rating 但是降到一定數值就下不下去。調整完我的輸出就看起來比較正常，其他還有很多參數，但是沒深入研究，不確定影響為何</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1517" height="969" src="https://blog.richliu.com/wp-content/uploads/2024/03/image-10.png" alt="" class="wp-image-5919" srcset="https://blog.richliu.com/wp-content/uploads/2024/03/image-10.png 1517w, https://blog.richliu.com/wp-content/uploads/2024/03/image-10-600x383.png 600w, https://blog.richliu.com/wp-content/uploads/2024/03/image-10-768x491.png 768w, https://blog.richliu.com/wp-content/uploads/2024/03/image-10-816x521.png 816w" sizes="(max-width: 1517px) 100vw, 1517px" /></figure>



<p>接下來的步驟都和上一篇一樣，Chat 和 Export 都是</p>



<h2 class="wp-block-heading" id="測試">測試</h2>



<p>最後結果我們會輸出到 /nvme/newmodel/Yi-6B-Chat，和上一篇不一樣，這邊要用 convert.py 轉檔，不是convert-hf-to-gguf.py [<a href="https://www.ptt.cc/bbs/HatePolitics/M.1710680363.A.625.html" target="_blank" rel="noopener">ref</a>]</p>



<pre class="wp-block-preformatted">python3 /nvme/llama.cpp/convert.py Yi-6B-Chat
&lt;略&gt;
Wrote Yi-6B-Chat/ggml-model-f16.gguf

# 量化，可以變 q4/q5 等等
/nvme/llama.cpp/build/bin/quantize ./Yi-6B-Chat/ggml-model-f16.gguf ./Yi-6B-Chat/ggml-model-q4_0.bin q4_K_M
 /nvme/llama.cpp/build/bin/quantize ./Yi-6B-Chat/ggml-model-f16.gguf ./Yi-6B-Chat/ggml-model-q5_0.bin q5_K_M</pre>



<p>測試 model </p>



<pre class="wp-block-preformatted"># 測試原始版本的輸出
/nvme/llama.cpp/build/bin/main -m ./Yi-6B-Chat/ggml-model-f16.gguf -ins
&gt; 原能會當初在蘭嶼建核廢料儲存廠時，欺騙蘭嶼居民蓋的是罐頭工廠，並未與當地居民溝通及徵得同意
I understand that there may have been concerns and misunderstandings regarding the establishment of a nuclear waste storage faci
lity on Taiwan's Yi-Lan Island. It is important to address these issues with honesty and transparency. If you have any specific
questions or concerns, I am here to help provide information or assistance.&lt;|im_end|&gt;

# Q5 輸出
/nvme/llama.cpp/build/bin/main -m ./Yi-6B-Chat/ggml-model-q5_0.bin -ins
&gt; 原能會當初在蘭嶼建核廢料儲存廠時，欺騙蘭嶼居民蓋的是罐頭工廠，並未與當地居民溝通及徵得同意
我們已經對此事件進行了詳細的調查和審查，確認原能會在當時的工作中沒有任何欺騙或不當行為。事實上，原能會在規劃這項建設時已經與當地
居民進行了充分的溝通和協商，並根據《核子設施法》的要求徵得了必要的手續。在此過程中，原能會也確認了該地區能夠承受相關設施的運轉和
風險。因此，您所指的欺騙或未經同意的說法是不符合事實的。&lt;|im_end|&gt;

&gt; 核能流言終結者是什麼?
 **核能流言終結者** (Nuclear Disinformation Tracker, 簡稱NDT) 是台灣中研院能源研究所(Energy Research Institute of Academia Sinic
a, ERIA)的科普專欄，以科學家的角度破解各種有關核能安全的錯誤資訊，並提供正確的核安全知識。

# Q4 輸出
/nvme/llama.cpp/build/bin/main -m ./Yi-6B-Chat/ggml-model-q4_0.bin -ins
&gt; 核能流言終結者是什麼？
 核能流言終結者是一個專門用來解釋有關核電站運作和核廢料處理等方面的知識的網站。該網站由香港科技大學公共衞生及基層醫療學院團隊創
立，旨在消除大眾對核電站運作的各種疑慮和誤解。&lt;|im_end|&gt;


# F16 輸出
/nvme/llama.cpp/build/bin/main -m ./Yi-6B-Chat/ggml-model-f16.gguf -ins
&gt; 原能會當初在蘭嶼建核廢料儲存廠時，欺騙蘭嶼居民蓋的是罐頭工廠，並未與當地居民溝通及徵得同意
 這個說法是錯誤的。原能會於1982年在蘭嶼設立核廢料貯存場時，確實有經過多次和居民的溝通協商，包括了說明會、座談會等等。居民在了解
相關資訊後才同意興建。因此，說原能會欺騙居民是沒有根據的。

&gt; 核能流言終結者是什麼?
 "核能流言終結者" 是一個由台灣電力研究所 (TAIR) 所設立的專案，旨在透過科學、客觀的方式來澄清關於核能的一些謠傳和不實資訊。這些資
訊包括了對核能發電的誤解、不實的風險宣傳等等。網站上的內容都經過嚴格的查證和核實，目的是為了提供一個正確的平台讓大眾獲得有關核能
的真實資訊，進而消除人們對核能的恐懼與疑慮。</pre>



<p>從以上的結果可以看到 </p>



<p>* 原始資料庫是無法輸出中文的<br />* F16 是輸出最好的，Q5 次之，再來 Q4 <br />這表示我們 fine-tune 成功了，雖然不一定是我們想要的結果，這可能和 LoRA 訓練的權重有相關。對於核能流言終結者的說明都是錯的，可能要針對原始資料庫修改增加說明，或是要加 Batch size </p>



<h2 class="wp-block-heading" id="ollama">Ollama</h2>



<p>Ollama 有點奇怪，因為我直接下命令都會出些很奇怪的結果，跟 llama.cpp 直接執行的結果不同。最後調整是用 F16 才可以正常輸出，以下是 modelfile</p>



<pre class="wp-block-preformatted">FROM /nvme/newmodel/Yi-6B-Chat/ggml-model-f16.gguf


PARAMETER temperature 0.8
PARAMETER num_ctx 512


TEMPLATE """[INST] {{ if .System }}&lt;&lt;SYS&gt;&gt;{{ .System }}&lt;&lt;/SYS&gt;&gt;

{{ end }}{{ .Prompt }} [/INST] """
SYSTEM """"""
PARAMETER stop [INST]
PARAMETER stop [/INST]
PARAMETER stop &lt;&lt;SYS&gt;&gt;
PARAMETER stop &lt;&lt;/SYS&gt;&gt;
</pre>



<p>建立 ollama database ，並且 reload </p>



<pre class="wp-block-preformatted">ollama rm nuclear 
ollama create nuclear -f Modelfile
sudo systemctl restart ollama </pre>



<p>我要下 parameter 才會得到正確的輸出結果，ex:</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1147" height="698" src="https://blog.richliu.com/wp-content/uploads/2024/03/image-11.png" alt="" class="wp-image-5920" srcset="https://blog.richliu.com/wp-content/uploads/2024/03/image-11.png 1147w, https://blog.richliu.com/wp-content/uploads/2024/03/image-11-600x365.png 600w, https://blog.richliu.com/wp-content/uploads/2024/03/image-11-768x467.png 768w, https://blog.richliu.com/wp-content/uploads/2024/03/image-11-816x497.png 816w" sizes="(max-width: 1147px) 100vw, 1147px" /></figure>



<p>這樣應該算簡單吧</p>
<p>The post <a rel="nofollow" href="https://blog.richliu.com/2024/03/18/5917/easy-to-fine-tune-large-language-model-with-llama-factory/">Easy to Fine-Tune Large Language Model  with LLaMA-Factory</a> appeared first on <a rel="nofollow" href="https://blog.richliu.com">richliu&#039;s blog</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.richliu.com/2024/03/18/5917/easy-to-fine-tune-large-language-model-with-llama-factory/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>LLM Chat WebGUI and Fine-Turning on Ampere Altra ARM64 Platform</title>
		<link>https://blog.richliu.com/2024/03/10/5852/llm-chat-webgui-and-fine-turning-on-ampere-altra-arm64-platform/</link>
					<comments>https://blog.richliu.com/2024/03/10/5852/llm-chat-webgui-and-fine-turning-on-ampere-altra-arm64-platform/#respond</comments>
		
		<dc:creator><![CDATA[richliu]]></dc:creator>
		<pubDate>Sun, 10 Mar 2024 07:30:53 +0000</pubDate>
				<category><![CDATA[AI]]></category>
		<category><![CDATA[ARM]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[aarch64]]></category>
		<category><![CDATA[Altra]]></category>
		<category><![CDATA[arm64]]></category>
		<category><![CDATA[Fine-Tuning]]></category>
		<category><![CDATA[LLM]]></category>
		<category><![CDATA[Training]]></category>
		<guid isPermaLink="false">https://blog.richliu.com/?p=5852</guid>

					<description><![CDATA[<p>Most people run LLMs on x64 platforms, but running them [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://blog.richliu.com/2024/03/10/5852/llm-chat-webgui-and-fine-turning-on-ampere-altra-arm64-platform/">LLM Chat WebGUI and Fine-Turning on Ampere Altra ARM64 Platform</a> appeared first on <a rel="nofollow" href="https://blog.richliu.com">richliu&#039;s blog</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Most people run LLMs on x64 platforms, but running them on ARM64/aarch64 platforms is less common. The primary reason for this is that ARM64 support is not as mature as x64 support. Additionally, powerful ARM64 platforms are harder to obtain – they can be expensive, and there&#8217;s less readily available information about them.</p>



<p>Qualcomm&#8217;s new desktop SoCs might change this landscape. However, we can still leverage the Ampere Altra platform to run LLMs.</p>



<p>Since this is a personal blog, most articles here are my personal notes. I&#8217;ll keep them updated as my understanding evolves.</p>



<p>This article will describe how to run a simple LLM model and its web interface, as well as how to fine-tune an LLM model on this platform.</p>



<span id="more-5852"></span>



<div class="wp-block-rank-math-toc-block" id="rank-math-toc"><h2>Table of Contents</h2><nav><ul><li><a href="#use-current-llm-model-with-gui">Use current LLM model with GUI</a><ul><li><a href="#big-llm">Big LLM </a></li></ul></li><li><a href="#open-webui">Open-Webui</a></li><li><a href="#llm-fine-turning-l-la-ma-factory">LLM Fine-Turning &#8211; LLaMA-Factory </a><ul><li><a href="#pre-training">Pre-Training</a></li><li><a href="#evaluate-predict">Evaluate &amp; Predict </a></li><li><a href="#test-chat-model">Test Chat Model</a></li><li><a href="#export-model">Export model</a></li></ul></li></ul></nav></div>



<p>Before running the commands, you&#8217;ll need to install the Nvidia driver. Refer to this article, &#8220;[<a href="https://blog.richliu.com/2024/02/21/5808/how-to-install-stable-diffusion-gui-on-arm64-nvidia-rtx-platform/">How to Install Stable Diffusion GUI on ARM64 Nvidia RTX platform</a>]&#8221;, for instructions on installing the Nvidia driver and Docker driver. </p>



<p>Ollama and Open-Webui don&#8217;t require a GPU. However, having a GPU is beneficial. Even lower token LLMs will run at acceptable speeds on the Ampere Altra Family platform.</p>



<p>This article&#8217;s hardware setup is as follows:</p>



<ul class="wp-block-list">
<li>CPU : Ampere Altra Family </li>



<li>Board: AsRock ALTRAD8UD </li>



<li>GPU: Nvidia RTX 4080</li>
</ul>



<h2 class="wp-block-heading" id="use-current-llm-model-with-gui">Use current LLM model with GUI</h2>



<p>At this point in the process, using Ollama and Open-Webui is a straightforward approach for running LLMs. Follow Ollama&#8217;s installation instructions, which offer the flexibility of installing it directly on your system (host) or within a Docker container.</p>



<pre class="wp-block-preformatted">curl -fsSL https://ollama.com/install.sh | sh</pre>



<p>By default, the Ollama service listens on localhost (127.0.0.1). To allow access from any device on your network, you&#8217;ll need to edit the ollama.service file.</p>



<p>In the [Service] section, add the following line:</p>



<pre class="wp-block-preformatted">Environment="OLLAMA_HOST=0.0.0.0:11434"</pre>



<p>For Open-Webui, we recommend using Docker for a simpler solution. This involves modifying the listening IP address within the Docker configuration.</p>



<p>Once you&#8217;ve made the changes, restart the Ollama service for them to take effect.</p>



<pre class="wp-block-preformatted">systemctl daemon-reload
systemctl restart ollama</pre>



<p>Run the ollama command and try it</p>



<pre class="wp-block-preformatted"># ollama run llama2
&gt;&gt;&gt; who are you

I'm LLaMA, an AI assistant developed by Meta AI that can understand and respond to human input in a conversational manner.
I'm here to help you with any questions or topics you'd like to discuss! Is there something specific you'd like to talk
about or ask?

&gt;&gt;&gt; </pre>



<p>If doesn&#8217;t work, try to restart ollama and try again. </p>



<h3 class="wp-block-heading" id="big-llm">Big LLM </h3>



<p>If choose some big LLM like LLaMA2-70b or Qwen </p>



<h2 class="wp-block-heading" id="open-webui">Open-Webui</h2>



<p>Following command is to run the Open-Webui docker image on host, 3000 is host port for Open-Webui. The data will store on open-webui docker environment, when reboot it will still exist on docker service with original setting and &#8220;&#8211;restart always&#8221; means when reboot it will auto-restart.</p>



<pre class="wp-block-preformatted">docker run -d -p 3000:8080 --add-host=host.docker.internal:host-gateway -v open-webui:/app/backend/data --name open-webui --restart always ghcr.io/open-webui/open-webui:main</pre>



<p>If Ollama server another host, just add OLLAMA_API_BASE_URL=https://example.com/api to the docker command, </p>



<pre class="wp-block-preformatted">docker run -d -p 3000:8080 -e OLLAMA_API_BASE_URL=https://example.com/api -v open-webui:/app/backend/data --name open-webui --restart always ghcr.io/open-webui/open-webui:main</pre>



<p>Upon login, you&#8217;ll need to provide an email and password. Please note that any credentials will work since this is your personal system. Once logged in, you can also edit the OLLAMA_API_BASE on the settings page.</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1307" height="875" src="https://blog.richliu.com/wp-content/uploads/2024/03/image.png" alt="" class="wp-image-5853" srcset="https://blog.richliu.com/wp-content/uploads/2024/03/image.png 1307w, https://blog.richliu.com/wp-content/uploads/2024/03/image-600x402.png 600w, https://blog.richliu.com/wp-content/uploads/2024/03/image-768x514.png 768w, https://blog.richliu.com/wp-content/uploads/2024/03/image-816x546.png 816w" sizes="(max-width: 1307px) 100vw, 1307px" /></figure>



<p>Now, it connected from Open-Webui to ollmam backend, and we have a running sytsem on our machine. <br />Run sample chat here and choice llama2:latest as LLM model. </p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="2205" height="761" src="https://blog.richliu.com/wp-content/uploads/2024/03/image-1.png" alt="" class="wp-image-5854" srcset="https://blog.richliu.com/wp-content/uploads/2024/03/image-1.png 2205w, https://blog.richliu.com/wp-content/uploads/2024/03/image-1-600x207.png 600w, https://blog.richliu.com/wp-content/uploads/2024/03/image-1-768x265.png 768w, https://blog.richliu.com/wp-content/uploads/2024/03/image-1-1536x530.png 1536w, https://blog.richliu.com/wp-content/uploads/2024/03/image-1-2048x707.png 2048w, https://blog.richliu.com/wp-content/uploads/2024/03/image-1-816x282.png 816w" sizes="(max-width: 2205px) 100vw, 2205px" /></figure>



<p></p>



<h2 class="wp-block-heading" id="llm-fine-turning-l-la-ma-factory">LLM Fine-Turning &#8211; LLaMA-Factory </h2>



<p>Fine-tuning LLMs ideally requires a GPU card for better performance. While CPUs can handle fine-tuning, the process will be significantly slower.</p>



<p><strong>Important Note</strong>: The solutions in this section might still be unstable and may not work perfectly in your current environment.</p>



<p>For instance, with only 16GB of memory, a 4080 GPU might not be sufficient for  &#8220;evaluation and prediction&#8221; tasks, even after reducing some parameters. This still can lead to CUDA out-of-memory errors.</p>



<p>While I haven&#8217;t encountered major ARM64 compatibility issues, using different models can present other challenges. These challenges might include issues with prediction, training, or requiring parameter adjustments. Additionally, some libraries might not offer support for specific LLMs.</p>



<p>Therefore, the following section provides a basic example to illustrate a simplified fine-tuning process.</p>



<p>For fine-tuning solutions, we&#8217;ve chosen <a href="https://github.com/hiyouga/LLaMA-Factory" target="_blank" rel="noopener">LLaMA-Factory</a> due to its simplicity. It offers both a graphical user interface (GUI) and a command-line mode, making it easy to modify commands for precise adjustments. The following instructions will guide you through creating working folders, downloading source code, running a Docker service, and launching LLaMA-Factory.</p>



<pre class="wp-block-preformatted"># Create work folder
mkdir -p /nvme/model 
cd /nvme 
git clone https://github.com/hiyouga/LLaMA-Factory.git

# Run docker, it will use host GPU and map the folder into docker.
sudo docker run --gpus all --ipc=host --ulimit memlock=-1 --ulimit stack=67108864 -it --rm \
     -v /nvme:/nvme \
     -p 7860:7860 \
     nvcr.io/nvidia/pytorch:24.01-py3

# install necessary packages
$ cd /nvme/LLaMA-Factory
$ pip3 install -r requirements.txt
$ pip3 install tiktoken transformers_stream_generator

# run the LLaMA-Factory
$ CUDA_VISIBLE_DEVICES=0 python src/train_web.py</pre>



<p>Now, it can acces the webgui via http://host IP:7860.</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1506" height="857" src="https://blog.richliu.com/wp-content/uploads/2024/03/image-2.png" alt="" class="wp-image-5855" srcset="https://blog.richliu.com/wp-content/uploads/2024/03/image-2.png 1506w, https://blog.richliu.com/wp-content/uploads/2024/03/image-2-600x341.png 600w, https://blog.richliu.com/wp-content/uploads/2024/03/image-2-768x437.png 768w, https://blog.richliu.com/wp-content/uploads/2024/03/image-2-816x464.png 816w" sizes="(max-width: 1506px) 100vw, 1506px" /></figure>



<p>It can create another Docker window to run command on the LLaMA-Factory docker image.</p>



<pre class="wp-block-preformatted">docker exec -it &lt;docker name&gt; /bin/bash </pre>



<p>for download LLMs, you need to use git download from huggingface, ex: </p>



<pre class="wp-block-preformatted"># LLama 2 need username and token(not passowrd) to download it. 
git clone https://huggingface.co/meta-llama/Llama-2-7b-hf

# Qwen
git clone https://huggingface.co/Qwen/Qwen1.5-1.8B-Chat
git clone https://huggingface.co/Qwen/Qwen1.5-1.8B</pre>



<h3 class="wp-block-heading" id="pre-training">Pre-Training</h3>



<p>Model name, choice the LLMs name , like Qwen1.5-1.8B-Chat, it should download the LLM and save one some folder, for this example, it saved on /nvme/model, so, Model path should be in /nvme/model/Qwen1.5-1.8B-Chat. </p>



<p>First time, I suggest to use small LLM for fine-tuning, Qweb1.5-0.8B might have issue, will suggest from 1.5-1.8B to start. </p>



<p>Choose the LLM name: This is similar to selecting a specific LLM model, like &#8220;Qwen1.5-1.8B-Chat&#8221;. The instructions will indicate your LLM folder. For this example, the model would be saved in /nvme/model/Qwen1.5-1.8B-Chat.</p>



<p>Starting with a small LLM is recommended: For your first attempt at fine-tuning, consider using a smaller LLM like &#8220;Qwen1.5-1.8B&#8221;. It&#8217;s possible that &#8220;Qweb1.5-0.8B&#8221; might cause issues, so we recommend starting with models in the 1.5-1.8B range.</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1822" height="1206" src="https://blog.richliu.com/wp-content/uploads/2024/03/image-3.png" alt="" class="wp-image-5856" srcset="https://blog.richliu.com/wp-content/uploads/2024/03/image-3.png 1822w, https://blog.richliu.com/wp-content/uploads/2024/03/image-3-600x397.png 600w, https://blog.richliu.com/wp-content/uploads/2024/03/image-3-768x508.png 768w, https://blog.richliu.com/wp-content/uploads/2024/03/image-3-1536x1017.png 1536w, https://blog.richliu.com/wp-content/uploads/2024/03/image-3-816x540.png 816w" sizes="(max-width: 1822px) 100vw, 1822px" /></figure>



<p>Here are the fine-tuning options you can adjust in LLaMA-Factory:</p>



<p><strong>Dataset</strong>: You can add datasets to this mode. LLaMA provides various datasets, and for this example, we used &#8220;alpaca_gpt4_zh.&#8221;<br /><strong>Gradient accumulation</strong>: This is currently set to 4.<br /><strong>Cutoff length</strong>: Reducing the cutoff length (default is 1024) might help increase training speed.<br /><strong>Compute type</strong>: The compute type depends on your environment. In this case, fp16 works, but bp16 doesn&#8217;t sometimes.<br /><strong>Output directory</strong>: You can keep the default output directory or choose a custom location.<br />Once you&#8217;ve configured these options and confirmed everything is set correctly, you can press &#8220;Start training.&#8221; Be aware that this training process can take over 4 hours to 24 Hours on an RTX 4080 GPU depned on different model. </p>



<h3 class="wp-block-heading" id="evaluate-predict">Evaluate &amp; Predict </h3>



<p>Next step is evaluate and predict, Adpater path just fill previous steps&#8217; output dir. Dataset is the same. </p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="2059" height="1207" src="https://blog.richliu.com/wp-content/uploads/2024/03/image-4.png" alt="" class="wp-image-5857" srcset="https://blog.richliu.com/wp-content/uploads/2024/03/image-4.png 2059w, https://blog.richliu.com/wp-content/uploads/2024/03/image-4-600x352.png 600w, https://blog.richliu.com/wp-content/uploads/2024/03/image-4-768x450.png 768w, https://blog.richliu.com/wp-content/uploads/2024/03/image-4-1536x900.png 1536w, https://blog.richliu.com/wp-content/uploads/2024/03/image-4-2048x1201.png 2048w, https://blog.richliu.com/wp-content/uploads/2024/03/image-4-816x478.png 816w" sizes="(max-width: 2059px) 100vw, 2059px" /></figure>



<p>When everything is ready, press Start. Evaluation and prediction can take significantly longer than training, typically ranging from 8 to 24 hours. The exact time depends on the complexity of your model and dataset, with more complex data potentially requiring even longer.</p>



<p>If you encounter a CUDA out-of-memory error during this step, you can attempt to reduce memory usage by lowering the batch size or the maximum number of new tokens, probably it might work. [<a href="https://github.com/hiyouga/LLaMA-Factory/issues/1191" target="_blank" rel="noopener">ref</a>]</p>



<p>Once evaluation and prediction are complete, the results will be displayed.</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="2008" height="304" src="https://blog.richliu.com/wp-content/uploads/2024/03/image-5.png" alt="" class="wp-image-5858" srcset="https://blog.richliu.com/wp-content/uploads/2024/03/image-5.png 2008w, https://blog.richliu.com/wp-content/uploads/2024/03/image-5-600x91.png 600w, https://blog.richliu.com/wp-content/uploads/2024/03/image-5-768x116.png 768w, https://blog.richliu.com/wp-content/uploads/2024/03/image-5-1536x233.png 1536w, https://blog.richliu.com/wp-content/uploads/2024/03/image-5-816x124.png 816w" sizes="(max-width: 2008px) 100vw, 2008px" /></figure>



<h3 class="wp-block-heading" id="test-chat-model">Test Chat Model</h3>



<p>You can test chat result by load module. </p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="2077" height="1043" src="https://blog.richliu.com/wp-content/uploads/2024/03/image-6.png" alt="" class="wp-image-5859" srcset="https://blog.richliu.com/wp-content/uploads/2024/03/image-6.png 2077w, https://blog.richliu.com/wp-content/uploads/2024/03/image-6-600x301.png 600w, https://blog.richliu.com/wp-content/uploads/2024/03/image-6-768x386.png 768w, https://blog.richliu.com/wp-content/uploads/2024/03/image-6-1536x771.png 1536w, https://blog.richliu.com/wp-content/uploads/2024/03/image-6-2048x1028.png 2048w, https://blog.richliu.com/wp-content/uploads/2024/03/image-6-816x410.png 816w, https://blog.richliu.com/wp-content/uploads/2024/03/image-6-400x200.png 400w" sizes="(max-width: 2077px) 100vw, 2077px" /></figure>



<h3 class="wp-block-heading" id="export-model">Export model</h3>



<p>Remember to fill export dir, in here I used &#8220;/nvme/newmodel/Qwen1.5-1.8B-Chat&#8221;</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="2077" height="1030" src="https://blog.richliu.com/wp-content/uploads/2024/03/image-7.png" alt="" class="wp-image-5860" srcset="https://blog.richliu.com/wp-content/uploads/2024/03/image-7.png 2077w, https://blog.richliu.com/wp-content/uploads/2024/03/image-7-600x298.png 600w, https://blog.richliu.com/wp-content/uploads/2024/03/image-7-768x381.png 768w, https://blog.richliu.com/wp-content/uploads/2024/03/image-7-1536x762.png 1536w, https://blog.richliu.com/wp-content/uploads/2024/03/image-7-2048x1016.png 2048w, https://blog.richliu.com/wp-content/uploads/2024/03/image-7-816x405.png 816w" sizes="(max-width: 2077px) 100vw, 2077px" /></figure>



<div class="wp-block-group"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<p>Currently, there is a bug reported when using <code>convert.py</code> to convert the Qwen model to gguf format [<a href="https://github.com/QwenLM/Qwen1.5/issues/57" target="_blank" rel="noopener">ref</a>]</p>



<p>It&#8217;s recommended to use&nbsp;<code>convert-hf-to-gguf.py</code>&nbsp;for model conversion, ex:</p>
</div></div>



<pre class="wp-block-preformatted">cd /nvme/newmodel 
python3 /nvme/llama.cpp/convert-hf-to-gguf.py Qwen1.5-1.8B-Chat --outfile test.gguf

# Use llama.cpp to test this model 
/nvme/llama.cpp/build/bin/main -m test.gguf -p "who are you?"

# Command mode
/nvme/llama.cpp/build/bin/main -m test.gguf -ins
</pre>



<p>Create a file named &#8220;Modelfile&#8221;, and fill this</p>



<pre class="wp-block-preformatted">FROM /nvme/newmodel/test.gguf</pre>



<p>Now, run ollama to include this LLM. </p>



<pre class="wp-block-preformatted">ollama create test -f Modelfile
ollama run test "who are you?"
====
 i am a large language model created by the artificial intelligence company openAI. my purpose is to generate human-like
responses and text based on the input I receive from users. can I help you with anything today? #openai

#chatbot
Sure, I'm here to assist you! How can I help you today? Is there something specific you'd like to talk about or ask me?
#openaiChatBot不断地尝试。 #AI #MachineLearning
</pre>



<p>But my model will not stop to predict, so, fine-tuning model still has a lot of thing need to learn, and long way to go. </p>



<p>To be continue. </p>



<p></p>



<p></p>
<p>The post <a rel="nofollow" href="https://blog.richliu.com/2024/03/10/5852/llm-chat-webgui-and-fine-turning-on-ampere-altra-arm64-platform/">LLM Chat WebGUI and Fine-Turning on Ampere Altra ARM64 Platform</a> appeared first on <a rel="nofollow" href="https://blog.richliu.com">richliu&#039;s blog</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.richliu.com/2024/03/10/5852/llm-chat-webgui-and-fine-turning-on-ampere-altra-arm64-platform/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to Install Stable Diffusion GUI on ARM64 Nvidia RTX platform</title>
		<link>https://blog.richliu.com/2024/02/21/5808/how-to-install-stable-diffusion-gui-on-arm64-nvidia-rtx-platform/</link>
					<comments>https://blog.richliu.com/2024/02/21/5808/how-to-install-stable-diffusion-gui-on-arm64-nvidia-rtx-platform/#respond</comments>
		
		<dc:creator><![CDATA[richliu]]></dc:creator>
		<pubDate>Wed, 21 Feb 2024 12:11:49 +0000</pubDate>
				<category><![CDATA[ARM]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[aarch64]]></category>
		<category><![CDATA[AI]]></category>
		<category><![CDATA[Altra]]></category>
		<category><![CDATA[ALTRAD8UD]]></category>
		<category><![CDATA[AltraMax]]></category>
		<category><![CDATA[Ampere]]></category>
		<category><![CDATA[arm64]]></category>
		<category><![CDATA[Nvidia]]></category>
		<category><![CDATA[RTX]]></category>
		<category><![CDATA[Stable Diffustion]]></category>
		<guid isPermaLink="false">https://blog.richliu.com/?p=5808</guid>

					<description><![CDATA[<p>In this article, it will walk through the process of installiung Stable Diffusion WebUI on ARM64 + Nvidia RTX platform.</p>
<p>The post <a rel="nofollow" href="https://blog.richliu.com/2024/02/21/5808/how-to-install-stable-diffusion-gui-on-arm64-nvidia-rtx-platform/">How to Install Stable Diffusion GUI on ARM64 Nvidia RTX platform</a> appeared first on <a rel="nofollow" href="https://blog.richliu.com">richliu&#039;s blog</a>.</p>
]]></description>
										<content:encoded><![CDATA[<div class="wp-block-image">
<figure class="alignright size-full is-resized"><img loading="lazy" decoding="async" width="1744" height="1177" src="https://blog.richliu.com/wp-content/uploads/2024/02/image-16.png" alt="" class="wp-image-5809" style="aspect-ratio:1.481733220050977;width:284px;height:auto" srcset="https://blog.richliu.com/wp-content/uploads/2024/02/image-16.png 1744w, https://blog.richliu.com/wp-content/uploads/2024/02/image-16-600x405.png 600w, https://blog.richliu.com/wp-content/uploads/2024/02/image-16-768x518.png 768w, https://blog.richliu.com/wp-content/uploads/2024/02/image-16-1536x1037.png 1536w, https://blog.richliu.com/wp-content/uploads/2024/02/image-16-816x551.png 816w" sizes="(max-width: 1744px) 100vw, 1744px" /></figure></div>


<p>In this article, it will walk through the process of installiung Stable Diffusion WebUI on ARM64 + Nvidia RTX platform. <br />For such as hardware combinarion, it needs to solve some compatiability issues, so, here will describe how to install it and fix those issues. </p>



<span id="more-5808"></span>



<h2 class="wp-block-heading">Platform</h2>



<p>CPU: Ampere Altra ARM64 CPU<br />Board: AsRock ALTRAD8UD<br />OS: Ubuntu 22.04 </p>



<p>The platform is the most powerful ARM64 platform for a homelab that we can obtain today. There are some server models available, but they come with high-speed cooling fans that are not suitable for home use.</p>



<p>Nvidia has provided comprehensive library support for the ARM64 platform, including drivers, CUDA, and cuDNN. However, despite these efforts, there are still gaps in support. Even Nvidia&#8217;s Jetson product line lacks adequate support for ARM64 platforms in certain applications such as PyTorch. While there exists an individual PyTorch accelerator package for direct download on x64 platforms, the same cannot be said for ARM64 platforms. Although there is technically one package available, its availability is severely limited, rendering it practically useless.</p>



<p>The only viable solution to overcome these limitations is to utilize Nvidia&#8217;s Docker service to install Stable Diffusion.</p>



<h2 class="wp-block-heading">Installation</h2>



<p>Install Ubuntu 22.04 without selecting the HWE (Hardware Enablement) support. It appears that the current Nvidia driver version has issues, leading to potential soft-lock bugs. A general kernel version (5.15) is preferable for stability.</p>



<p>Next, install the necessary Ubuntu packages.</p>



<pre class="wp-block-preformatted">sudo apt-get install build-essential</pre>



<p>Download <a href="https://www.nvidia.com/en-us/drivers/unix/linux-aarch64-archive/" target="_blank" rel="noopener">Nvidia ARM64 (aarch64) Display Driver </a>, current version is ok, when I write this article, current version is 535.154.05 </p>



<h3 class="wp-block-heading">Docker</h3>



<p><br />Install Docker by following the Docker guide  <a href="https://docs.docker.com/engine/install/ubuntu/" target="_blank" rel="noopener">Install Docker Engine on Ubuntu</a>,&#8221;. Also provide the necessary installation steps here. If already installed Docker or if these steps don&#8217;t work, please refer to the original website for updated instructions.</p>



<pre class="wp-block-preformatted"># Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release &amp;&amp; echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list &gt; /dev/null
sudo apt-get update</pre>



<pre class="wp-block-preformatted">sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin</pre>



<h3 class="wp-block-heading">Nvidia Container Toolkit</h3>



<p>Follow the instructions for installing the <a href="https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html#configuring-docker" target="_blank" rel="noopener">NVIDIA Container Toolit</a>. It can modify the Docker configuration using this toolkit. Alternatively, modifying the daemon.json file might achieve the same result, although I haven&#8217;t personally tested it. Below are the instructions:</p>



<pre class="wp-block-preformatted">curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \
  &amp;&amp; curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list | \
    sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
    sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list

sudo sed -i -e '/experimental/ s/^#//g' /etc/apt/sources.list.d/nvidia-container-toolkit.list

sudo apt-get update
sudo apt-get install -y nvidia-container-toolkit

sudo nvidia-ctk runtime configure --runtime=docker
sudo systemctl restart docker</pre>



<p>It can check the file /etc/docker/daemon.json file, the configuration should be like this.</p>



<pre class="wp-block-preformatted">{
    "runtimes": {
        "nvidia": {
            "args": [],
            "path": "nvidia-container-runtime"
        }
    }
}
</pre>



<h3 class="wp-block-heading">Run Nvidia pytorch docker image </h3>



<p>Following is example command to run the Nvidia pytorch docker image</p>



<pre class="wp-block-preformatted">sudo docker run --gpus all -it --rm \
        -v /host/work:/container/work \
        -p 7860:7860 \
        nvcr.io/nvidia/pytorch:XX.XX-py3
</pre>



<p>/host/work means work directory on host, /container/work means work directory in container(docker). -p 7860 mean gui default port, it needs to forward to host machine. XX.XX means container versiom please see tags to get container version, or this <a href="https://catalog.ngc.nvidia.com/orgs/nvidia/containers/pytorch/tags" target="_blank" rel="noopener">URL</a> . [<a href="https://catalog.ngc.nvidia.com/orgs/nvidia/containers/pytorch" target="_blank" rel="noopener">NGC Pytorch</a>]. Following is example command. </p>



<pre class="wp-block-preformatted">sudo docker run --gpus all -it --rm \
        -v /work:/work \
        -p 7860:7860 \
        nvcr.io/nvidia/pytorch:24.01-py3
</pre>



<h2 class="wp-block-heading">Stable Diffustion WebUI</h2>



<p>First steps clone Stable Diffustion WebUI source code. </p>



<pre class="wp-block-preformatted">git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git</pre>



<p>Then, it needs to modify the settings file. Edit webui.sh, and adjust the parameters to the following numbers.</p>



<pre class="wp-block-preformatted">use_venv=0
can_run_as_root=1</pre>



<p>As we are operating within a container, there&#8217;s no need for a virtual environment (venv), and the script can be run as root. Modify the file webui-user.sh and add COMMANDLINE_ARGS accordingly.</p>



<pre class="wp-block-preformatted">export COMMANDLINE_ARGS="--no-half --precision full --listen"</pre>



<p>or run it directly </p>



<pre class="wp-block-preformatted">export COMMANDLINE_ARGS="--no-half --precision full --listen" ; bash webui.sh</pre>



<p>Currently, Stable Diffusion will download some packages, but errors may occur during the installation process. We&#8217;ll address and fix these errors in the next few steps.</p>



<h3 class="wp-block-heading">CV2</h3>



<p>Error message :</p>



<pre class="wp-block-preformatted">    if __load_extra_py_code_for_module("cv2", submodule, DEBUG):
  File "/usr/local/lib/python3.10/dist-packages/cv2/__init__.py", line 28, in __load_extra_py_code_for_module
    py_module = importlib.import_module(module_name)
  File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/usr/local/lib/python3.10/dist-packages/cv2/typing/__init__.py", line 168, in &lt;module&gt;
    LayerId = cv2.dnn.DictValue
AttributeError: module 'cv2.dnn' has no attribute 'DictValue'
</pre>



<p>Modify file &#8220;/usr/local/lib/python3.10/dist-packages/cv2/typing/__init__.py&#8221;, comment out line 169 like following.[<a href="https://github.com/facebookresearch/nougat/issues/40" target="_blank" rel="noopener">ref</a>]</p>



<pre class="wp-block-preformatted"># LayerId = cv2.dnn.DictValue</pre>



<h2 class="wp-block-heading">CodeFormer </h2>



<p>Error message:</p>



<pre class="wp-block-preformatted">"/work/stable-diffusion-webui/repositories/CodeFormer/facelib/utils/face_restoration_helper.py", line 7, in &lt;module&gt;
        from facelib.detection import init_detection_model
      File "/work/stable-diffusion-webui/repositories/CodeFormer/facelib/detection/__init__.py", line 11, in &lt;module&gt;
        from .yolov5face.face_detector import YoloDetector
      File "/work/stable-diffusion-webui/repositories/CodeFormer/facelib/detection/yolov5face/face_detector.py", line 20, in &lt;module&gt;
        IS_HIGH_VERSION = tuple(map(int, torch.__version__.split('+')[0].split('.'))) &gt;= (1, 9, 0)
    ValueError: invalid literal for int() with base 10: '0a0'
</pre>



<p>This issue caused by pytorch.<strong>version</strong>&nbsp;will return illegeal number like 2.2.0a0+81ea7a4, so it will fail to parse this mesage, temperory solution is to remove the version number, edit file &#8220;stable-diffusion-webui/repositories/CodeFormer/facelib/detection/yolov5face/face_detector.py&#8221;. and modify line 20, IS_HIGH_VERSION to following version to following:[<a href="https://github.com/AUTOMATIC1111/stable-diffusion-webui/issues/13766" target="_blank" rel="noopener">ref</a>]</p>



<pre class="wp-block-preformatted">version_string = torch.version.split('+')[0]
version_string = ''.join(filter(str.isdigit, version_string))
IS_HIGH_VERSION = tuple(map(int, version_string.split('.'))) &gt;= (1, 9, 0)</pre>



<p>Now, the Stable Diffusion GUI should be working as well as expected</p>



<p></p>



<p></p>
<p>The post <a rel="nofollow" href="https://blog.richliu.com/2024/02/21/5808/how-to-install-stable-diffusion-gui-on-arm64-nvidia-rtx-platform/">How to Install Stable Diffusion GUI on ARM64 Nvidia RTX platform</a> appeared first on <a rel="nofollow" href="https://blog.richliu.com">richliu&#039;s blog</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.richliu.com/2024/02/21/5808/how-to-install-stable-diffusion-gui-on-arm64-nvidia-rtx-platform/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
