How to run PyTorch on Pypy

username: elcolie
cooked: <p>I have read this post, but it was old topic and wiki page is broken by now.<br>
Any update on pypy running pytorch?</p>
<p>Ref:</p><aside class="quote" data-post="1" data-topic="5817">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/f/9fc29f/48.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/installing-pytorch-in-pypy-implementation-rather-than-cpython/5817">Installing Pytorch in PyPy Implementation rather than CPython</a> 
  </div>
  <blockquote>
    I am assuming that Pytorch is not supported in Pypy environment. I had an existing system which was slow in CPython so we moved to Pypy.  Now I need to integrate that system with some Pytorch model.  Turns out the default wheel is built for CPython. Is there any way I can install the Pytorch (any version) in Pypy implementation?
  </blockquote>
</aside>
<aside class="onebox githubissue" data-onebox-src="https://github.com/pytorch/pytorch/issues/17835">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/issues/17835" target="_blank" rel="noopener nofollow ugc">github.com/pytorch/pytorch</a>
  </header>

  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue">
	  <svg width="60" height="60" class="github-icon" viewBox="0 0 14 16" aria-hidden="true"><path fill-rule="evenodd" d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/pytorch/issues/17835" target="_blank" rel="noopener nofollow ugc">PyPy support</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2019-03-09" data-time="08:30:14" data-timezone="UTC">08:30AM - 09 Mar 19 UTC</span>
      </div>


      <div class="user">
        <a href="https://github.com/lopuhin" target="_blank" rel="noopener nofollow ugc">
          <img alt="lopuhin" src="https://avatars.githubusercontent.com/u/424613?v=4" class="onebox-avatar-inline" width="20" height="20">
          lopuhin
        </a>
      </div>
    </div>

    <div class="labels">
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          module: binaries
        </span>
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          feature
        </span>
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          triaged
        </span>
    </div>
  </div>
</div>

  <div class="github-row">
    <p class="github-body-container">## 🚀 Feature
Support pytorch from PyPy -- a fast, compliant alternative impleme<span class="show-more-container"><a href="" rel="noopener" class="show-more">…</a></span><span class="excerpt hidden">ntation of the Python language (http://pypy.org)

## Motivation

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.

## Pitch

(A clear and concise description of what you want to happen). I can imagine several stages:

- PyPy support works by compiling from source
- PyPy is integrated into CI
- PyPy wheels are built and released

## Additional context

Right now it seems that PyPy support, at least for commonly used stuff, is not far from reality, for example this branch https://github.com/lopuhin/pytorch/tree/pypy3.6 works in PyPy 3.6 7.0.0 for a moderately complex application (didn't try running tests yet).

Here is a summary of fixes in that branch:

1. working around a compilation issue in the JIT: https://github.com/pytorch/pytorch/commit/0481da14052c09605b38940e08d8cae275c6a8cd - this is definitely a hack to get the compilation working, a proper implementation for PyPy needs to be in place.
2. **[merged]** `PySlice_Unpack` is not yet available in PyPy 3.6 -https://github.com/pytorch/pytorch/commit/752e204c90a782432eed77018e4fe3ad5fa4dbf2 - probably it's ok to merge this. With two above fixes, pytorch compiles on PyPy 3.6. PR https://github.com/pytorch/pytorch/pull/17836
3. **[merged in https://github.com/pybind/pybind11/pull/2146 ]** Applying https://github.com/pybind/pybind11/pull/1494 to pybind11 - https://github.com/pytorch/pytorch/commit/912b0b2a77b3ea0a5bac9fbe3ac5d577c86c6d7. 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 https://github.com/pybind/pybind11/pull/1494 and https://github.com/pybind/pybind11/pull/1720
4. **[merged]** work around PyPy cpyext issue - https://github.com/pytorch/pytorch/commit/00941d8ae4e28bd03245a04c5096297c35a3128d - see https://bitbucket.org/pypy/pypy/issues/2968/segfault-calling-cpyext_tp_new_tuple (thanks @rlamy for the fix!), it seems that it is fine to merge this. PR https://github.com/pytorch/pytorch/pull/17837

Thanks to @xuhdev for an already merged PyPy compilation fix https://github.com/pytorch/pytorch/pull/11857

I submitted PRs for 2 and 4. If anyone can pick other items, that would be great.

While these are quite small fixes, still properly integrating them is not a small task, not speaking of setting up CI and supporting it.




cc @ezyang</span></p>
  </div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>


username: dinhanhx
cooked: <p>It’s kinda sad that when I google, this and 2 referenced links come up</p>