brainsteam.co.uk/new_files/2021/03/01/running-old-pytorch-docker/index.html

190 lines
13 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge"><title>Reproducing &#39;ancient&#39; experiments with Pytorch inside docker - Brainsteam</title><meta name="viewport" content="width=device-width, initial-scale=1">
<meta itemprop="name" content="Reproducing &#39;ancient&#39; experiments with Pytorch inside docker">
<meta itemprop="description" content="Using containers to run old &amp; deprecated PyTorch code with relative ease"><meta itemprop="datePublished" content="2021-03-01T20:21:11&#43;00:00" />
<meta itemprop="dateModified" content="2021-03-01T20:21:11&#43;00:00" />
<meta itemprop="wordCount" content="966"><meta itemprop="image" content="https://brainsteam.co.uk/2021/03/01/running-old-pytorch-docker/images/feature.jpg">
<meta itemprop="keywords" content="machine-learning,python,ai,devops,mlops," /><meta property="og:title" content="Reproducing &#39;ancient&#39; experiments with Pytorch inside docker" />
<meta property="og:description" content="Using containers to run old &amp; deprecated PyTorch code with relative ease" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://brainsteam.co.uk/2021/03/01/running-old-pytorch-docker/" /><meta property="og:image" content="https://brainsteam.co.uk/2021/03/01/running-old-pytorch-docker/images/feature.jpg"/><meta property="article:section" content="posts" />
<meta property="article:published_time" content="2021-03-01T20:21:11&#43;00:00" />
<meta property="article:modified_time" content="2021-03-01T20:21:11&#43;00:00" />
<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="https://brainsteam.co.uk/2021/03/01/running-old-pytorch-docker/images/feature.jpg"/>
<meta name="twitter:title" content="Reproducing &#39;ancient&#39; experiments with Pytorch inside docker"/>
<meta name="twitter:description" content="Using containers to run old &amp; deprecated PyTorch code with relative ease"/>
<link href='https://fonts.googleapis.com/css?family=Playfair+Display:700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" media="screen" href="https://brainsteam.co.uk/css/normalize.css" />
<link rel="stylesheet" type="text/css" media="screen" href="https://brainsteam.co.uk/css/main.css" />
<link id="dark-scheme" rel="stylesheet" type="text/css" href="https://brainsteam.co.uk/css/dark.css" />
<script src="https://brainsteam.co.uk/js/feather.min.js"></script>
<script src="https://brainsteam.co.uk/js/main.js"></script>
</head>
<body>
<div class="container wrapper">
<div class="header">
<div class="avatar">
<a href="https://brainsteam.co.uk/">
<img src="/images/avatar.png" alt="Brainsteam" />
</a>
</div>
<h1 class="site-title"><a href="https://brainsteam.co.uk/">Brainsteam</a></h1>
<div class="site-description"><p>The irregular mental expulsions of a PhD student and CTO of Filament, my views are my own and do not represent my employers in any way.</p><nav class="nav social">
<ul class="flat"><li><a href="https://twitter.com/jamesravey/" title="Twitter" rel="me"><i data-feather="twitter"></i></a></li><li><a href="https://github.com/ravenscroftj" title="Github" rel="me"><i data-feather="github"></i></a></li><li><a href="/index.xml" title="RSS" rel="me"><i data-feather="rss"></i></a></li></ul>
</nav></div>
<nav class="nav">
<ul class="flat">
<li>
<a href="/">Home</a>
</li>
<li>
<a href="/tags">Tags</a>
</li>
<li>
<a href="https://jamesravey.me">About Me</a>
</li>
</ul>
</nav>
</div>
<div class="post">
<div class="post-header">
<div class="meta">
<div class="date">
<span class="day">01</span>
<span class="rest">Mar 2021</span>
</div>
</div>
<div class="matter">
<h1 class="title">Reproducing &#39;ancient&#39; experiments with Pytorch inside docker</h1>
</div>
</div>
<div class="markdown">
<figure>
<img src="images/feature.jpg"
alt="A beige analog compass by Ylanite Koppens"/> <figcaption>
<p>A beige analog compass by <a href='https://www.pexels.com/photo/beige-analog-compass-697662/'>Ylanite Koppens</a></p>
</figcaption>
</figure>
<h2 id="introduction">Introduction</h2>
<p>Open machine learning research is undergoing something of a reproducibiltiy crisis. In fairness it&rsquo;s not usually the authors' fault - or at least not entirely. We&rsquo;re a fickle industry and the tools and frameworks were &lsquo;in vogue&rsquo; and state of the art a couple of years ago are now obsolete. Furthermore, academics and open source contributors are under no obligation to keep their code up to date. It is often left up to the reproducer to figure out how to breathe life back into older work.</p>
<p>This is a topic I&rsquo;ll write about in more detail some day but for now I&rsquo;ll focus on a case study around a fairly recent ML model that I wanted to run 2 years after it was written and the challenges I faced making that happen.</p>
<h2 id="the-model">The model</h2>
<p><a href="https://github.com/yala/text_nn/tree/master/rationale_net">The model</a> in question is a PyTorch implementation of <a href="https://arxiv.org/abs/1606.04155">Lei et al. 2016</a> paper on Rationalizing Neural Predictions. It&rsquo;s a relatively simple (relatively doing the heavy lifting here) neural text model that tries to extract short summaries or &lsquo;rationales&rsquo; from the text being classified that explain the class. Again a longer post about what exactly I&rsquo;m doing with this will be coming soon.</p>
<p>Tao Lei did originally provide a Theano implementation of his model <a href="https://github.com/taolei87/rcnn/tree/master/code">here</a> but running Theano (<a href="https://groups.google.com/forum/#!topic/theano-users/7Poq8BZutbY">RIP</a>]) in 2021 poses much more of a challenge than even running 2 year old PyTorch code (I know <strong>Exactly</strong> how ridiculous that sounds and I completely agree - not ideal at all! I might have a go at resurrecting this one later).</p>
<h2 id="first-steps--taking-stock">First steps- taking stock</h2>
<p>First we need to work out what is needed to run this model locally. Thankfully the author provides a relatively comprehensive README, and pip requirements files that outline which libraries are needed to run the model. A quick peek inside <a href="https://github.com/yala/text_nn/blob/master/requirements3.txt">requirements3.txt</a> is quite revealing - it shows a dependency on a binary PyTorch 0.3.0 pre-built for python 3.6. The versions of the remaining libraries are unconstrained which means pip can figure out which specific versions of those libs are compatible with PyTorch 0.3.0 and pull them for us.</p>
<p>We have enough information to have a first go at running this thing - we can do a quick git clone and then get started.</p>
<h2 id="blind-optimism">Blind Optimism</h2>
<p>I&rsquo;m a big fan of <a href="https://docs.conda.io/en/latest/miniconda.html">Miniconda</a> for managing my Python virtual environments - not least because it gives you &lsquo;free&rsquo; CUDA dependency management meaning I don&rsquo;t have to faff about installing different nvidia drivers on my development system if I&rsquo;m switching between projects that use different versions of TensorFlow or PyTorch built against different CUDA libraries.</p>
<p>I create a Python 3.6 environment and then try to install torch 0.3.0 via conda. No joy - this version of the library is compiled against CUDA 8.0 and the <a href="https://anaconda.org/anaconda/cudatoolkit/files">earliest version of cuda available in conda is 9.0</a>.</p>
<p>So our options are now:</p>
<ol>
<li>Try running with a newer version of cuda and see what happens (there might be unknown side effects)</li>
<li>Compile PyTorch 0.3.0 with a newer version of CUDA (Again possible unknown side effects/faff trying to compile things)</li>
<li>Try to find and run install CUDA 8.0 libraries on our host system and then install PyTorch 0.3.0 (faff&hellip;)</li>
</ol>
<p>I actually had a go at 1. but got an error message so we can probably make the assumption that there were several breaking API changes between PyTorch 0.3.0 running on CUDA V8 and PyTorch 1.7.0 running on CUDA V11.</p>
<p>Counter-intuitively, 3 is our next best choice.</p>
<h2 id="the-solution---containerise">The solution - containerise</h2>
<p>If you&rsquo;re not familiar with docker and containers and you&rsquo;re in software eng/ML eng then I&rsquo;d highly recommend looking into it. Containers allow us to borrow from the mid-late 90s Java <a href="https://www.computerweekly.com/feature/Write-once-run-anywhere">&ldquo;write once, run anywhere&rdquo;</a> paradigmn - they&rsquo;re like super light-weight virtual machines that contain all of the dependencies you need to run your application. They are super useful when you want to experiment with libraries and dependencies in a controlled way without messing up your host operating system.</p>
<p>NVidia provide their own version of docker that supports GPU passthrough - i.e. our &ldquo;tiny virtual machine&rdquo; can have access to the GPU in our host computer.</p>
<p>Firstly, I install <a href="https://nvidia.github.io/nvidia-docker/">nvidia-docker</a> on my host machine which runs Ubuntu. There are plenty of guides on <a href="https://nvidiasettlement.com/install-nvidia-docker">how to do this</a>.</p>
<p>Next we build a lightweight <a href="https://github.com/ravenscroftj/text_nn/blob/master/Dockerfile">Dockerfile</a> for building a container around the model implementation.</p>
<p>In summary, the above linked docker file:</p>
<ol>
<li>Starts from an Ubuntu base image with nvidia and cuda 8.0 installed already</li>
<li>Installs miniconda</li>
<li>Grabs the pytorch 0.3.0 library and other pythondependencies we need.</li>
</ol>
<p>Since we the cuda libs are installed separately (part of the docker base image), the fact that conda tries to install the wrong cuda toolkit isn&rsquo;t a problem inside our container.</p>
<p>After placing the docker file in the root of the git project, we can run <code>docker build -t yala_env .</code> to build the container and then <code>docker run -it yala_env /bin/bash</code> to start an interactive shell inside the virtualenv.</p>
<p>We run the train script and - as if by magic - it works! Hooray!</p>
<h2 id="learnings">Learnings</h2>
<p>So the main learnings from this experience were:</p>
<ul>
<li>code maintainence and reproducibility in machine learning are hard problems to which the community is yet to find the solution.</li>
<li>conda can sometimes help with managing nvidia/cuda mess so that we don&rsquo;t have to uninstall and reinstall system libraries</li>
<li>Docker and containers can provide a powerful and relatively simple way to isolate libraries and dependencies when even conda doesn&rsquo;t help us.</li>
</ul>
<p>I haven&rsquo;t tried it yet but I&rsquo;m fairly sure this method would work well with even older stuff like the original theano-based model I mentioned in this article - Docker seems like a really useful and helpful tool in the intrepid data scientist&rsquo;s ability to re-run and reproduce old experiments.</p>
</div>
<div class="tags">
<ul class="flat">
<li><a href="/tags/machine-learning">machine-learning</a></li>
<li><a href="/tags/python">python</a></li>
<li><a href="/tags/ai">ai</a></li>
<li><a href="/tags/devops">devops</a></li>
<li><a href="/tags/mlops">mlops</a></li>
</ul>
</div><div id="disqus_thread"></div>
<script type="text/javascript">
(function () {
if (window.location.hostname == "localhost")
return;
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
var disqus_shortname = 'brainsteam';
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the </a></noscript>
<a href="http://disqus.com/" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
</div>
</div>
<div class="footer wrapper">
<nav class="nav">
<div>2021 © James Ravenscroft 2020 | <a href="https://github.com/knadh/hugo-ink">Ink</a> theme on <a href="https://gohugo.io">Hugo</a></div>
</nav>
</div>
<script type="application/javascript">
var doNotTrack = false;
if (!doNotTrack) {
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
ga('create', 'UA-186263385-1', 'auto');
ga('send', 'pageview');
}
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>
<script>feather.replace()</script>
</body>
</html>