<?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://taweiyeh.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://taweiyeh.github.io/" rel="alternate" type="text/html" /><updated>2026-07-06T21:28:37+00:00</updated><id>https://taweiyeh.github.io/feed.xml</id><title type="html">Ta-Wei Yeh</title><subtitle>Portfolio of Ta-Wei Yeh — Ph.D. student in Electrical and Computer Engineering, robotics and embedded systems engineer.</subtitle><author><name>Ta-Wei Yeh</name></author><entry><title type="html">L4CasADi makes torch model no grad_fn</title><link href="https://taweiyeh.github.io/blog/l4casadi-makes-torch-model-no-grad-fn/" rel="alternate" type="text/html" title="L4CasADi makes torch model no grad_fn" /><published>2025-08-11T00:00:00+00:00</published><updated>2025-08-11T00:00:00+00:00</updated><id>https://taweiyeh.github.io/blog/l4casadi-makes-torch-model-no-grad-fn</id><content type="html" xml:base="https://taweiyeh.github.io/blog/l4casadi-makes-torch-model-no-grad-fn/"><![CDATA[<p><em>Originally published on <a href="https://medium.com/@twy359/l4casadi-makes-torch-model-no-grad-fn-1582be2fe8dd">Medium</a>.</em></p>

<p>When combining an optimal control problem (OCP) with a PyTorch model, <a href="https://github.com/Tim-Salzmann/l4casadi">L4CasADi</a> offers a useful solution. However, compiling the OCP can trigger an error:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn
</code></pre></div></div>

<p>This occurs after converting and compiling a PyTorch model in an OCP using L4CasADi. The problematic code pattern is:</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">import</span> <span class="nn">l4casadi</span> <span class="k">as</span> <span class="n">l4c</span>

<span class="n">l4casadi_model</span> <span class="o">=</span> <span class="n">l4c</span><span class="p">.</span><span class="n">L4CasADi</span><span class="p">(</span><span class="n">pytorch_model</span><span class="p">,</span> <span class="n">device</span><span class="o">=</span><span class="n">device</span><span class="p">)</span>
</code></pre></div></div>

<h2 id="the-solution">The solution</h2>

<p>Create a separate PyTorch model for the OCP instead:</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">import</span> <span class="nn">l4casadi</span> <span class="k">as</span> <span class="n">l4c</span>

<span class="c1"># copy model weight and bias from the original model
</span><span class="n">pytorch_model</span><span class="p">.</span><span class="n">load_state_dict</span><span class="p">(</span><span class="n">original_model</span><span class="p">.</span><span class="n">state_dict</span><span class="p">())</span>
<span class="n">l4casadi_model</span> <span class="o">=</span> <span class="n">l4c</span><span class="p">.</span><span class="n">L4CasADi</span><span class="p">(</span><span class="n">pytorch_model</span><span class="p">,</span> <span class="n">device</span><span class="o">=</span><span class="n">device</span><span class="p">)</span>
</code></pre></div></div>

<p>This approach preserves gradient tracking by keeping distinct model instances — one for optimization and one for training.</p>]]></content><author><name>Ta-Wei Yeh</name></author><summary type="html"><![CDATA[Originally published on Medium.]]></summary></entry><entry><title type="html">Build PyTorch Wheel on Jetson Xavier</title><link href="https://taweiyeh.github.io/blog/build-pytorch-wheel-on-jetson-xavier/" rel="alternate" type="text/html" title="Build PyTorch Wheel on Jetson Xavier" /><published>2025-02-04T00:00:00+00:00</published><updated>2025-02-04T00:00:00+00:00</updated><id>https://taweiyeh.github.io/blog/build-pytorch-wheel-on-jetson-xavier</id><content type="html" xml:base="https://taweiyeh.github.io/blog/build-pytorch-wheel-on-jetson-xavier/"><![CDATA[<p><em>Originally published on <a href="https://medium.com/@twy359/build-pytorch-wheel-on-jetson-xavier-cb0ff685f294">Medium</a>.</em></p>

<p>Building PyTorch from source becomes necessary when using Python 3.8 on JetPack 4.6.3, since Nvidia doesn’t provide pre-built wheels for this configuration. Downgrading to Python 3.6 was possible, but ROS Foxy requires Python 3.8, which made building from source the only viable option.</p>

<h2 id="setup-specifications">Setup specifications</h2>

<ul>
  <li>Python 3.8</li>
  <li>PyTorch 1.10.2</li>
  <li>Jetson Xavier with JetPack 4.6.3 and L4T 32.7.3</li>
  <li>CUDA 10.2</li>
</ul>

<h2 id="build-steps">Build steps</h2>

<p><strong>Clone and checkout:</strong></p>

<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>git clone <span class="nt">--recursive</span> https://github.com/pytorch/pytorch
<span class="nb">cd </span>pytorch
git checkout tags/v1.10.2
git submodule <span class="nb">sync
</span>git submodule update <span class="nt">--init</span> <span class="nt">--recursive</span> <span class="nt">--jobs</span> 0
</code></pre></div></div>

<p><strong>Maximize performance:</strong></p>

<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">sudo </span>nvpmodel <span class="nt">-m</span> 0
<span class="nb">sudo </span>jetson_clocks
</code></pre></div></div>

<p><strong>Configure the build environment:</strong></p>

<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">export </span><span class="nv">USE_NCCL</span><span class="o">=</span>1
<span class="nb">export </span><span class="nv">USE_DISTRIBUTED</span><span class="o">=</span>1
<span class="nb">export </span><span class="nv">USE_QNNPACK</span><span class="o">=</span>0
<span class="nb">export </span><span class="nv">USE_PYTORCH_QNNPACK</span><span class="o">=</span>0
<span class="nb">export </span><span class="nv">TORCH_CUDA_ARCH_LIST</span><span class="o">=</span><span class="s2">"7.2"</span>
<span class="nb">export </span><span class="nv">PYTORCH_BUILD_VERSION</span><span class="o">=</span>1.10.2
<span class="nb">export </span><span class="nv">PYTORCH_BUILD_NUMBER</span><span class="o">=</span>1
</code></pre></div></div>

<p><strong>Install dependencies:</strong></p>

<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">sudo </span>apt-get <span class="nb">install </span>cmake libopenblas-dev libopenmpi-dev
python3 <span class="nt">-m</span> pip <span class="nb">install</span> <span class="nt">-r</span> requirements.txt
python3 <span class="nt">-m</span> pip <span class="nb">install </span>scikit-build ninja
python3 <span class="nt">-m</span> pip <span class="nb">install </span><span class="nv">setuptools</span><span class="o">==</span>59.5.0
</code></pre></div></div>

<p><strong>Set the compiler version:</strong></p>

<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">sudo </span>update-alternatives <span class="nt">--install</span> /usr/bin/gcc gcc /usr/bin/gcc-8 1
<span class="nb">sudo </span>update-alternatives <span class="nt">--install</span> /usr/bin/g++ g++ /usr/bin/g++-8 1
</code></pre></div></div>

<p><strong>Build and install:</strong></p>

<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>python3 setup.py bdist_wheel
<span class="nb">cd </span>dist
python3 <span class="nt">-m</span> pip <span class="nb">install</span> <span class="nt">--no-cache</span> &lt;wheel_name&gt;
python3 <span class="nt">-c</span> <span class="s2">"import torch; torch.cuda.is_available()"</span>
</code></pre></div></div>

<h2 id="checking-the-cuda-architecture">Checking the CUDA architecture</h2>

<p>To verify CUDA capability using the <code class="language-plaintext highlighter-rouge">deviceQuery</code> utility:</p>

<ol>
  <li>Navigate to <code class="language-plaintext highlighter-rouge">/usr/local/cuda/samples/1_Utilities/deviceQuery</code></li>
  <li>Run <code class="language-plaintext highlighter-rouge">sudo make</code> to build</li>
  <li>Run <code class="language-plaintext highlighter-rouge">./deviceQuery</code> to display the specifications</li>
</ol>

<p>Jetson Xavier should report compute capability 7.2.</p>]]></content><author><name>Ta-Wei Yeh</name></author><summary type="html"><![CDATA[Originally published on Medium.]]></summary></entry><entry><title type="html">Install PyTorch and CUDA on Windows</title><link href="https://taweiyeh.github.io/blog/install-pytorch-and-cuda-on-windows/" rel="alternate" type="text/html" title="Install PyTorch and CUDA on Windows" /><published>2024-11-07T00:00:00+00:00</published><updated>2024-11-07T00:00:00+00:00</updated><id>https://taweiyeh.github.io/blog/install-pytorch-and-cuda-on-windows</id><content type="html" xml:base="https://taweiyeh.github.io/blog/install-pytorch-and-cuda-on-windows/"><![CDATA[<p><em>Originally published on <a href="https://medium.com/@twy359/install-pytorch-and-cuda-on-windows-9319deb4ce49">Medium</a>.</em></p>

<h2 id="my-environment">My environment</h2>

<ul>
  <li>Windows 11</li>
  <li>PyTorch 2.5.1</li>
  <li>CUDA 12.4</li>
</ul>

<h2 id="download-cuda">Download CUDA</h2>

<p>Download CUDA 12.4 from NVIDIA. There are two installer types: the local installer downloads the entire CUDA package (about 3.0 GB), while the network installer is a lightweight version that downloads CUDA as you install.</p>

<p>Note: attempting to install CUDA via Conda produced errors and was unsuccessful.</p>

<h2 id="install-pytorch">Install PyTorch</h2>

<p>Using Conda is the easiest route on Windows. For CUDA 12.4 compatibility:</p>

<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>conda <span class="nb">install </span>pytorch torchvision torchaudio pytorch-cuda<span class="o">=</span>12.4 <span class="nt">-c</span> pytorch <span class="nt">-c</span> nvidia
</code></pre></div></div>

<h2 id="testing">Testing</h2>

<p>Verify the installation succeeded:</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">import</span> <span class="nn">torch</span>
<span class="n">torch</span><span class="p">.</span><span class="n">cuda</span><span class="p">.</span><span class="n">is_available</span><span class="p">()</span>
<span class="c1"># True
</span></code></pre></div></div>

<h2 id="conclusion">Conclusion</h2>

<p>Make sure the version matches between <code class="language-plaintext highlighter-rouge">pytorch-cuda</code> and your CUDA version. If you need a different CUDA version, make sure your <code class="language-plaintext highlighter-rouge">pytorch-cuda</code> version changes accordingly.</p>]]></content><author><name>Ta-Wei Yeh</name></author><summary type="html"><![CDATA[Originally published on Medium.]]></summary></entry></feed>