<?xml version="1.0" encoding="utf-8"?> 
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-us">
    <generator uri="https://gohugo.io/" version="0.152.2">Hugo</generator><title type="html"><![CDATA[Uarray on Blog]]></title>
    
    
    
            <link href="https://blog.scientific-python.org/tags/uarray/" rel="alternate" type="text/html" title="html" />
            <link href="https://blog.scientific-python.org/tags/uarray/atom.xml" rel="self" type="application/atom" title="atom" />
    <updated>2026-04-04T04:32:36+00:00</updated>
    
    
    
    
        <id>https://blog.scientific-python.org/tags/uarray/</id>
    
        
        <entry>
            <title type="html"><![CDATA[SciPy Internship: 2021-2022]]></title>
            <link href="https://blog.scientific-python.org/scipy/internships/smit/?utm_source=atom_feed" rel="alternate" type="text/html" />
            
            
                <id>https://blog.scientific-python.org/scipy/internships/smit/</id>
            
            
            <published>2022-06-04T00:00:00+00:00</published>
            <updated>2022-06-04T00:00:00+00:00</updated>
            
            
            <content type="html"><![CDATA[<blockquote>Internship Experience</blockquote><p>I was <a href="https://mail.python.org/archives/list/scipy-dev@python.org/message/4S43BYHDQIPQENNJ6EMQY5QZDZK3ZT5I/">selected as an intern</a> to work on SciPy build system. In this blog post, I will be describing my journey of this 10-months long internship at SciPy. I worked on a variety of topics starting from migrating the SciPy build system to <a href="https://mesonbuild.com/index.html">Meson</a>, cleaning up the public API namespaces and adding <a href="https://uarray.org/en/latest/">Uarray</a> support to SciPy submodules.</p>
<h1 id="experience">Experience<a class="headerlink" href="#experience" title="Link to this heading">#</a></h1>
<h2 id="meson-build-system">Meson Build System<a class="headerlink" href="#meson-build-system" title="Link to this heading">#</a></h2>
<p>The main reasons for switching to Meson include (in addition to <code>distutils</code> being deprecated):</p>
<ol>
<li><em>Much faster builds</em></li>
<li><em>Reliability</em></li>
<li><em>Support for cross-compiling</em></li>
<li><em>Better build logs</em></li>
<li><em>Easier to debug build issues</em></li>
</ol>
<p><em>For more details on the initial proposal to switch to Meson, see <a href="https://github.com/scipy/scipy/issues/13615">scipy-13615</a></em></p>
<p>I was initially selected to work on the migrating the SciPy build system to <a href="https://mesonbuild.com/index.html">meson</a>. I started by adding Meson build support
for <a href="https://github.com/rgommers/scipy/pull/35">scipy.misc</a> and <a href="https://github.com/rgommers/scipy/pull/37">scipy.signal</a>. While working on this, we came across many <a href="https://github.com/rgommers/scipy/issues/42">build warnings</a> which we wanted to fix, since they unnecessarily increased the build log and might point to some hidden bugs. I fixed these warnings, the majority of which came from <a href="https://github.com/rgommers/scipy/issues/30">deprecated NumPy C API calls</a>.</p>
<ul>
<li>I also started <a href="https://github.com/rgommers/scipy/issues/58">benchmarking</a> the Meson build with various optimization levels, during which I ended up finding some <a href="https://github.com/scipy/scipy/issues/14667">failing benchmark tests</a> and tried to fix them.</li>
<li>I implemented the <a href="https://github.com/rgommers/scipy/pull/94">dev.py</a> interface that works in a similar way to <code>runtests.py</code>, but using Meson for building SciPy.</li>
<li>I extended my work on the Meson build by writing Python scripts for checking the installation of all <a href="https://github.com/rgommers/scipy/issues/69">test files</a> and <a href="https://github.com/scipy/scipy/pull/16010">.pyi files</a>.</li>
<li>I documented how to use <a href="https://github.com/rgommers/scipy/pull/96">dev.py</a>, and use <a href="https://github.com/scipy/scipy/pull/15953">parallel builds and optimization levels</a> with Meson.</li>
<li>I added <a href="https://github.com/rgommers/scipy/pull/130">meson option</a> to switch between BLAS/LAPACK libraries.</li>
</ul>
<p>Meson build support including all the above work was merged into SciPy&rsquo;s <code>main</code> branch around Christmas 2021. Meson will now become the default build in the upcoming 1.9.0 release.</p>
<h2 id="making-cleaner-public-namespaces">Making cleaner public namespaces<a class="headerlink" href="#making-cleaner-public-namespaces" title="Link to this heading">#</a></h2>
<h4 id="whats-the-issue">What&rsquo;s the issue?<a class="headerlink" href="#whats-the-issue" title="Link to this heading">#</a></h4>
<p><em>&ldquo;A basic API design principle is: a public object should only be available from one namespace. Having any function in two or more places is just extra technical debt, and with things like dispatching on an API or another library implementing a mirror API, the cost goes up.&rdquo;</em></p>

<div class="highlight">
  <pre>&gt;&gt;&gt; from scipy import ndimage
&gt;&gt;&gt; ndimage.filters.gaussian_filter is ndimage.gaussian_filter  # :(
True</pre>
</div>

<p>The <a href="http://scipy.github.io/devdocs/reference/index.html#api-definition">API reference docs</a> of SciPy define the public API. However, SciPy still had some submodules that were <em>accidentally</em> somewhat public by missing an underscore at the start of their name.
I worked on <a href="https://github.com/scipy/scipy/issues/14360">cleaning the pubic namespaces</a> for about a couple of months by carefully adding underscores to the <code>.py</code> files that were not meant to be public and added depecrated warnings if anyone tries to access them.</p>
<h4 id="the-solution">The solution:<a class="headerlink" href="#the-solution" title="Link to this heading">#</a></h4>

<div class="highlight">
  <pre>&gt;&gt;&gt; from scipy import ndimage
&gt;&gt;&gt; ndimage.filters.gaussian_filter is ndimage.gaussian_filter
&lt;stdin&gt;:1: DeprecationWarning: Please use `gaussian_filter` from the `scipy.ndimage` namespace, the `scipy.ndimage.filters` namespace is deprecated.
True</pre>
</div>

<h2 id="adding-uarray-support">Adding Uarray support<a class="headerlink" href="#adding-uarray-support" title="Link to this heading">#</a></h2>
<p><em>&ldquo;SciPy adopted uarray to support a multi-dispatch mechanism with the goal being: allow writing backends for public APIs that execute in parallel, distributed or on GPU.&rdquo;</em></p>
<p>For about the last four months, I worked on adding <a href="https://github.com/scipy/scipy/issues/14353">Uarray support</a> to SciPy submobules. I do recommend reading <a href="https://labs.quansight.org/blog/2021/10/array-libraries-interoperability/">this blog post</a> by Anirudh Dagar covering the motivation and actual usage of <code>uarray</code>. I picked up the following submodules for adding <code>uarray</code> compatibility:</p>
<ul>
<li><a href="https://github.com/rgommers/scipy/pull/101">signal</a></li>
<li><a href="https://github.com/scipy/scipy/pull/15610">linalg</a></li>
<li><a href="https://github.com/scipy/scipy/pull/15665">special</a></li>
</ul>
<p>At the same time, in order to show a working prototype, I also added <code>uarray</code> backends in CuPy to the following submodules:</p>
<ul>
<li><code>cupyx.scipy.ndimage</code> (<a href="https://github.com/cupy/cupy/pull/6403">PR #6403</a>)</li>
<li><code>cupyx.scipy.linalg</code> (<a href="https://github.com/cupy/cupy/pull/6460">PR #6460</a>)</li>
<li><code>cupyx.scipy.special</code> (<a href="https://github.com/cupy/cupy/pull/6564">PR #6564</a>)</li>
</ul>
<p>The pull requests contain links to Colab notebooks which show these features in action.</p>
<h4 id="what-does-usage-of-such-a-backend-look-like">What does usage of such a backend look like?<a class="headerlink" href="#what-does-usage-of-such-a-backend-look-like" title="Link to this heading">#</a></h4>


<div class="highlight">
  <pre class="chroma"><code><span class="line"><span class="cl"><span class="kn">import</span> <span class="nn">scipy</span>
</span></span><span class="line"><span class="cl"><span class="kn">import</span> <span class="nn">cupy</span> <span class="k">as</span> <span class="nn">cp</span>
</span></span><span class="line"><span class="cl"><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
</span></span><span class="line"><span class="cl"><span class="kn">from</span> <span class="nn">scipy.linalg</span> <span class="kn">import</span> <span class="n">inv</span><span class="p">,</span> <span class="n">set_backend</span>
</span></span><span class="line"><span class="cl"><span class="kn">import</span> <span class="nn">cupyx.scipy.linalg</span> <span class="k">as</span> <span class="nn">_cupy_backend</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="n">x_cu</span><span class="p">,</span> <span class="n">x_nu</span> <span class="o">=</span> <span class="n">cp</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mf">1.0</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">],</span> <span class="p">[</span><span class="mf">3.0</span><span class="p">,</span> <span class="mf">4.0</span><span class="p">]]),</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mf">1.0</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">],</span> <span class="p">[</span><span class="mf">3.0</span><span class="p">,</span> <span class="mf">4.0</span><span class="p">]])</span>
</span></span><span class="line"><span class="cl"><span class="n">y_scipy</span> <span class="o">=</span> <span class="n">inv</span><span class="p">(</span><span class="n">x_nu</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="k">with</span> <span class="n">set_backend</span><span class="p">(</span><span class="n">_cupy_backend</span><span class="p">):</span>
</span></span><span class="line"><span class="cl">    <span class="n">y_cupy</span> <span class="o">=</span> <span class="n">inv</span><span class="p">(</span><span class="n">x_cu</span><span class="p">)</span></span></span></code></pre>
</div>
<h2 id="miscelleanous-work">Miscelleanous Work<a class="headerlink" href="#miscelleanous-work" title="Link to this heading">#</a></h2>
<ul>
<li><a href="https://github.com/scipy/scipy/pull/15440">Fix path issues in runtests.py</a></li>
<li><a href="https://github.com/scipy/scipy/pull/15250">Array inputs for stats.kappa4</a></li>
<li><a href="https://github.com/rgommers/scipy/pull/115">Fixes mac CI conda env cache</a></li>
</ul>
<h2 id="future-work">Future Work<a class="headerlink" href="#future-work" title="Link to this heading">#</a></h2>
<ul>
<li>The &ldquo;switch to Meson&rdquo; project is nearing its completion. One of the final issues was to allow <a href="https://github.com/scipy/scipy/pull/15476">building wheels</a> with the <code>meson-python</code> backend.</li>
<li>The PRs opened for adding <code>uarray</code> support are still under heavy discussion, and the main aim will be get them merged as soon as possible once we have reached a concrete decision.</li>
</ul>
<h2 id="things-to-remember">Things to remember<a class="headerlink" href="#things-to-remember" title="Link to this heading">#</a></h2>
<ol>
<li><em>Patience</em>: Setting up new project always takes some time. We might need to update/fix the system libraries and try to resolve the errors gradually.</li>
<li><em>Learning</em>: Learning new things was one of the main key during the internship. I was completely new to build systems and GPU libraries.</li>
</ol>
<h2 id="thank-you">Thank You!!<a class="headerlink" href="#thank-you" title="Link to this heading">#</a></h2>
<p>I am very grateful to <a href="https://github.com/rgommers">Ralf Gommers</a> for providing me with this opportunity and believing in me. His guidance, support and patience played a major role during the entire course of internship.
I am also thankful to whole SciPy community for helping me with the PR reviews and providing essential feedback. Also, huge thanks to <a href="https://github.com/czgdp1807">Gagandeep Singh</a> for always being a part of this wonderful journey.</p>
<p><em>In a nutshell, I will remember this experience as: <a href="https://github.com/rgommers">Ralf Gommers</a> has boosted my career by millions!</em></p>
]]></content>
            
                 
                    
                 
                    
                         
                        
                            
                             
                                <category scheme="taxonomy:Tags" term="scipy" label="SciPy" />
                             
                                <category scheme="taxonomy:Tags" term="internship" label="internship" />
                             
                                <category scheme="taxonomy:Tags" term="meson-build" label="meson-build" />
                             
                                <category scheme="taxonomy:Tags" term="uarray" label="uarray" />
                            
                        
                    
                
            
        </entry>
    
</feed>
