<h2 dir="auto"><g-emoji class="g-emoji" alias="rocket" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f680.png">🚀</g-emoji> Feature</h2>
<p dir="auto">Add support for PyPy interpreter</p>
<h2 dir="auto">Motivation</h2>
<p dir="auto">Increase speed of Python code that do not relate to PyTorch</p>
<h2 dir="auto"><g-emoji class="g-emoji" alias="rocket" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f680.png">🚀</g-emoji> Feature</h2>
<p dir="auto">Support pytorch from PyPy -- a fast, compliant alternative implementation of the Python language (<a href="http://pypy.org" rel="nofollow">http://pypy.org</a>)</p>
<h2 dir="auto">Motivation</h2>
<p dir="auto">While pytorch itself probably won't benefit much from PyPy JIT, but often pytorch is used as a part of larger application, where using PyPy can have speed benefits, e.g. for evaluation, data generation, or other activities.</p>
<h2 dir="auto">Pitch</h2>
<p dir="auto">(A clear and concise description of what you want to happen). I can imagine several stages:</p>
<ul dir="auto">
<li>PyPy support works by compiling from source</li>
<li>PyPy is integrated into CI</li>
<li>PyPy wheels are built and released</li>
</ul>
<h2 dir="auto">Additional context</h2>
<p dir="auto">Right now it seems that PyPy support, at least for commonly used stuff, is not far from reality, for example this branch <a href="https://github.com/lopuhin/pytorch/tree/pypy3.6">https://github.com/lopuhin/pytorch/tree/pypy3.6</a> works in PyPy 3.6 7.0.0 for a moderately complex application (didn't try running tests yet).</p>
<p dir="auto">Here is a summary of fixes in that branch:</p>
<ol dir="auto">
<li>working around a compilation issue in the JIT: <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/pytorch/pytorch/commit/0481da14052c09605b38940e08d8cae275c6a8cd/hovercard" href="https://github.com/pytorch/pytorch/commit/0481da14052c09605b38940e08d8cae275c6a8cd"><tt>0481da1</tt></a> - this is definitely a hack to get the compilation working, a proper implementation for PyPy needs to be in place.</li>
<li><strong>[merged]</strong> <code class="notranslate">PySlice_Unpack</code> is not yet available in PyPy 3.6 -<a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/pytorch/pytorch/commit/752e204c90a782432eed77018e4fe3ad5fa4dbf2/hovercard" href="https://github.com/pytorch/pytorch/commit/752e204c90a782432eed77018e4fe3ad5fa4dbf2"><tt>752e204</tt></a> - probably it's ok to merge this. With two above fixes, pytorch compiles on PyPy 3.6. PR <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="419048233" data-permission-text="Title is private" data-url="https://github.com/pytorch/pytorch/issues/17836" data-hovercard-type="pull_request" data-hovercard-url="/pytorch/pytorch/pull/17836/hovercard" href="https://github.com/pytorch/pytorch/pull/17836">#17836</a></li>
<li><strong>[merged in https://github.com/<a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="588071040" data-permission-text="Title is private" data-url="https://github.com/pybind/pybind11/issues/2146" data-hovercard-type="pull_request" data-hovercard-url="/pybind/pybind11/pull/2146/hovercard" href="https://github.com/pybind/pybind11/pull/2146">pybind/pybind11/pull/2146</a> ]</strong> Applying <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="351937050" data-permission-text="Title is private" data-url="https://github.com/pybind/pybind11/issues/1494" data-hovercard-type="pull_request" data-hovercard-url="/pybind/pybind11/pull/1494/hovercard" href="https://github.com/pybind/pybind11/pull/1494">pybind/pybind11#1494</a> to pybind11 - <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/pytorch/pytorch/commit/912b0b2a77b3ea0a5bac9fbe3ac5d577c86c6d7/hovercard" href="https://github.com/pytorch/pytorch/commit/912b0b2a77b3ea0a5bac9fbe3ac5d577c86c6d7"><tt>912b0b2</tt></a>. Unfortunately, while the fix seems correct, and pybind11 has partial support and CI for PyPy, more work needs to be done to properly integrate it into pybind11, because other pybind11 tests are failing on a more recent PyPy, see <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="351937050" data-permission-text="Title is private" data-url="https://github.com/pybind/pybind11/issues/1494" data-hovercard-type="pull_request" data-hovercard-url="/pybind/pybind11/pull/1494/hovercard" href="https://github.com/pybind/pybind11/pull/1494">pybind/pybind11#1494</a> and <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="418717719" data-permission-text="Title is private" data-url="https://github.com/pybind/pybind11/issues/1720" data-hovercard-type="pull_request" data-hovercard-url="/pybind/pybind11/pull/1720/hovercard" href="https://github.com/pybind/pybind11/pull/1720">pybind/pybind11#1720</a></li>
<li><strong>[merged]</strong> work around PyPy cpyext issue - <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/pytorch/pytorch/commit/00941d8ae4e28bd03245a04c5096297c35a3128d/hovercard" href="https://github.com/pytorch/pytorch/commit/00941d8ae4e28bd03245a04c5096297c35a3128d"><tt>00941d8</tt></a> - see <a href="https://bitbucket.org/pypy/pypy/issues/2968/segfault-calling-cpyext_tp_new_tuple" rel="nofollow">https://bitbucket.org/pypy/pypy/issues/2968/segfault-calling-cpyext_tp_new_tuple</a> (thanks <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/rlamy/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/rlamy">@rlamy</a> for the fix!), it seems that it is fine to merge this. PR <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="419048246" data-permission-text="Title is private" data-url="https://github.com/pytorch/pytorch/issues/17837" data-hovercard-type="pull_request" data-hovercard-url="/pytorch/pytorch/pull/17837/hovercard" href="https://github.com/pytorch/pytorch/pull/17837">#17837</a></li>
</ol>
<p dir="auto">Thanks to <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/xuhdev/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/xuhdev">@xuhdev</a> for an already merged PyPy compilation fix <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="361649752" data-permission-text="Title is private" data-url="https://github.com/pytorch/pytorch/issues/11857" data-hovercard-type="pull_request" data-hovercard-url="/pytorch/pytorch/pull/11857/hovercard" href="https://github.com/pytorch/pytorch/pull/11857">#11857</a></p>
<p dir="auto">I submitted PRs for 2 and 4. If anyone can pick other items, that would be great.</p>
<p dir="auto">While these are quite small fixes, still properly integrating them is not a small task, not speaking of setting up CI and supporting it.</p>
<p dir="auto">cc <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/ezyang/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/ezyang">@ezyang</a></p>