<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://misakau.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://misakau.github.io/" rel="alternate" type="text/html" /><updated>2026-05-28T16:32:42+00:00</updated><id>https://misakau.github.io/feed.xml</id><title type="html">Xun Wang</title><subtitle>personal description</subtitle><author><name>Xun Wang</name><email>wang-x24@mails.tsinghua.edu.cn</email></author><entry><title type="html">peft和pytorch不兼容</title><link href="https://misakau.github.io/posts/2024/12/peft_and_pytorch/" rel="alternate" type="text/html" title="peft和pytorch不兼容" /><published>2024-12-18T00:00:00+00:00</published><updated>2024-12-18T00:00:00+00:00</updated><id>https://misakau.github.io/posts/2024/12/peft_and_pytorch</id><content type="html" xml:base="https://misakau.github.io/posts/2024/12/peft_and_pytorch/"><![CDATA[<p>最近在跑<a href="https://huggingface.co/blog/lora">LoRA微调Stable Diffusion</a>的时候，发现程序在最后加载LoRA权重时报错：</p>

<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>  File <span class="s2">"/anaconda3/envs/lora/lib/python3.10/site-packages/peft/utils/save_and_load.py"</span>, line 445, <span class="k">in </span>set_peft_model_state_dict
  load_result <span class="o">=</span> model.load_state_dict<span class="o">(</span>peft_model_state_dict, <span class="nv">strict</span><span class="o">=</span>False, <span class="nv">assign</span><span class="o">=</span>True<span class="o">)</span>
  TypeError: Module.load_state_dict<span class="o">()</span> got an unexpected keyword argument <span class="s1">'assign'</span>
</code></pre></div></div>

<p>搜了一大圈之后才搞清楚原来<code class="language-plaintext highlighter-rouge">assign</code>关键字是在 <a href="https://pytorch.org/docs/2.1/generated/torch.nn.Module.html#torch.nn.Module.load_state_dict">Pytorch 2.1.x</a> 新增的，而我自己的版本是 <a href="https://pytorch.org/docs/2.0/generated/torch.nn.Module.html?highlight=nn+module+load_state_dict#torch.nn.Module.load_state_dict">Pytorch 2.0.x</a>，所以并没有这个参数…</p>

<p>解决方法是把<code class="language-plaintext highlighter-rouge">peft</code>版本降低到<code class="language-plaintext highlighter-rouge">peft==0.12.0</code>，这个版本是支持<code class="language-plaintext highlighter-rouge">Pytorch 2.0.x</code>的。 而从<code class="language-plaintext highlighter-rouge">peft==0.13.0</code>开始就引入了上述不兼容的代码。</p>]]></content><author><name>Xun Wang</name><email>wang-x24@mails.tsinghua.edu.cn</email></author><category term="env" /><category term="pytorch" /><category term="peft" /><summary type="html"><![CDATA[最近在跑LoRA微调Stable Diffusion的时候，发现程序在最后加载LoRA权重时报错：]]></summary></entry><entry><title type="html">Install D4RL</title><link href="https://misakau.github.io/posts/2024/12/install_d4rl/" rel="alternate" type="text/html" title="Install D4RL" /><published>2024-12-16T00:00:00+00:00</published><updated>2024-12-16T00:00:00+00:00</updated><id>https://misakau.github.io/posts/2024/12/install_d4rl</id><content type="html" xml:base="https://misakau.github.io/posts/2024/12/install_d4rl/"><![CDATA[<p>每次安装D4RL都会踩一堆坑，然后上网搜一堆零散的攻略，所以我自己整理一下整个过程，以后直接来查就好了。</p>

<h2 id="基本环境">基本环境</h2>
<p>conda创建虚拟环境后，从<a href="https://pytorch.org/get-started/previous-versions/">Pytorch官网</a>下载需要的版本。
其他的包缺啥装啥就行。conda和pip记得换源，不然可能会很慢。</p>

<p><strong>conda 换源</strong></p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>conda config <span class="nt">--add</span> channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
conda config <span class="nt">--add</span> channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
conda config <span class="nt">--add</span> channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
</code></pre></div></div>
<p><strong>pip 换源</strong></p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>conda activate &lt;env_name&gt;
pip config <span class="nb">set </span>global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
</code></pre></div></div>
<p>安装MuJoCo210
—
一般安装的都是MuJoCo210版本，首先去<a href="https://github.com/google-deepmind/mujoco/releases?page=4">Github</a>上下载对应的压缩包（例如<a href="https://github.com/google-deepmind/mujoco/releases/download/2.1.0/mujoco210-linux-x86_64.tar.gz">mujoco210-linux-x86_64.tar.gz</a>），并且解压（或者移动）到<code class="language-plaintext highlighter-rouge">~/.mujoco/mujoco210</code>文件夹。</p>

<p><strong>解压命令</strong></p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">tar</span> <span class="nt">-zxvf</span> &lt;filename.tar.gz&gt;
</code></pre></div></div>
<p><strong>设置环境变量</strong></p>

<p>然后在<code class="language-plaintext highlighter-rouge">~.bashrc</code>中把它添加到<code class="language-plaintext highlighter-rouge">LD_LIBRARY_PATH</code>里，如果已有别的版本的mujoco记得屏蔽掉：</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">export </span><span class="nv">LD_LIBRARY_PATH</span><span class="o">=</span>/home/wangxun/.mujoco/mujoco210/bin:<span class="nv">$LD_LIBRARY_PATH</span>
</code></pre></div></div>
<p>最后安装2.1.x的<code class="language-plaintext highlighter-rouge">mujoco-py</code>即可，例如：<code class="language-plaintext highlighter-rouge">mujoco-py==2.1.2.14</code></p>

<h2 id="安装d4rl">安装D4RL</h2>
<p>首先把D4RL和mjrl都从Github上克隆下来</p>

<p><strong>克隆命令</strong></p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>git clone git@github.com:Farama-Foundation/D4RL.git
git clone git@github.com:aravindr93/mjrl.git
</code></pre></div></div>

<p>然后进入虚拟环境，进入<code class="language-plaintext highlighter-rouge">mjrl</code>文件夹，用<code class="language-plaintext highlighter-rouge">pip</code>安装mjrl</p>

<p><strong>安装命令</strong></p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">cd</span> /your_path_to_mjrl
pip <span class="nb">install</span> <span class="nt">-e</span> <span class="nb">.</span>
</code></pre></div></div>

<p>然后进入D4RL文件夹，注释掉<code class="language-plaintext highlighter-rouge">setup.py</code>中的mjrl链接</p>
<div class="language-diff highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">@@ -68,6 +68,6 @@</span>
         "termcolor",  # adept_envs dependency
         "click",  # adept_envs dependency
         "dm_control&gt;=1.0.3",
<span class="gd">-        "mjrl @ git+https://github.com/aravindr93/mjrl@master#egg=mjrl",
</span><span class="gi">+        #"mjrl @ git+https://github.com/aravindr93/mjrl@master#egg=mjrl",
</span>     ],
 )
</code></pre></div></div>
<p>然后用同样的方法安装D4RL</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">cd</span> /your_path_to_D4RL
pip <span class="nb">install</span> <span class="nt">-e</span> <span class="nb">.</span>
</code></pre></div></div>
<p>完成后测试是否能够成功导入<code class="language-plaintext highlighter-rouge">d4rl</code>包。</p>

<h3 id="可能会有一些报错需要注意下面几个确认点">可能会有一些报错，需要注意下面几个确认点：</h3>
<p><strong>确认点1</strong></p>

<p><code class="language-plaintext highlighter-rouge">/usr/lib/nvidia</code>是否添加到环境变量，如果没有，执行下面的命令：</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">export </span><span class="nv">LD_LIBRARY_PATH</span><span class="o">=</span>/usr/lib/nvidia:<span class="nv">$LD_LIBRARY_PATH</span>
</code></pre></div></div>
<p><strong>确认点2</strong></p>

<p><code class="language-plaintext highlighter-rouge">Cython</code>版本是否兼容，如果不兼容，安装以下版本：</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>pip <span class="nb">install </span><span class="nv">Cython</span><span class="o">==</span>3.0.0a10
</code></pre></div></div>
<p><strong>确认点3</strong></p>

<p>是否安装<code class="language-plaintext highlighter-rouge">X11</code>开发工具包，如果未安装，通过以下命令安装：</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">sudo </span>apt-get update
<span class="nb">sudo </span>apt-get <span class="nb">install </span>libx11-dev
</code></pre></div></div>
<p><strong>确认点4</strong></p>

<p>是否安装<code class="language-plaintext highlighter-rouge">GL/glew</code>开发工具包，如果未安装，通过以下命令安装：</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">sudo </span>apt-get update
<span class="nb">sudo </span>apt-get <span class="nb">install </span>libglew-dev
</code></pre></div></div>]]></content><author><name>Xun Wang</name><email>wang-x24@mails.tsinghua.edu.cn</email></author><category term="env" /><category term="offline RL" /><category term="personal notes" /><summary type="html"><![CDATA[每次安装D4RL都会踩一堆坑，然后上网搜一堆零散的攻略，所以我自己整理一下整个过程，以后直接来查就好了。]]></summary></entry></feed>