Visitar URL original
[feature request] PyTorch support for sub-interpreters with PEP 684 accepted and release in Python 3.12 路 Issue #102517 路 pytorch/pytorch 路 GitHub
Skip to content

[feature request] PyTorch support for sub-interpreters with PEP 684 accepted and release in Python 3.12聽#102517

Open

Description

@vadimkantorov

馃殌 The feature, motivation and pitch

It appears that sub-interpretor support for Python in PEP 684 https://discuss.python.org/t/pep-684-a-per-interpreter-gil/ is advancing and will be merged soon (https://github.com/python/cpython/blob/main/Misc/NEWS.d/3.12.0b1.rst, python/cpython#104210, https://github.com/ericsnowcurrently/multi-core-python/wiki/0-The-Plan)

Any updates on eventual support of sub-interpreters in PyTorch?

Also, a question would be of zero-copy in-RAM IPC data exchange: maybe via named pipes? Some mmaps? shm?

I found older issues asking the same:

This might be important for working in hosted multi-threaded environments where every thread runs its own Python / PyTorch code. This is also related to the current torch.compile / export / mini-interpreters story: https://discuss.pytorch.org/t/torch-compiles-deployment-story-to-non-python-host-processes/180943. This scenario was explicitly supported with TorchScript / TorchJit, but seems not yet developed for torch.compile, but it's an important usecase too (similar to API's OnnxRuntime is currently offering)

cc @albanD @colesbury

Alternatives

No response

Additional context

No response

Activity

  1. added
    triagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module
    module: python frontendFor issues relating to PyTorch's Python frontend
    on May 31, 2023
  2. albanD commented on May 31, 2023

    @albanD
    Collaborator

    Would https://github.com/pytorch/multipy work for you? Any details on why it would or not?

    Also I owuld be curious if there are more information on the questions in the other thread. In particular if our dependencies (like numpy) do support sub-interpreters?

  3. vadimkantorov commented on Jun 1, 2023

    @vadimkantorov
    ContributorAuthor

    @albanD I guess multipy would work! It would be great to see some tutorial of using multipy together with torch.compile / triton-compiled AOT packages

    But maybe in the future, it's still important to track state-of-subinterpreters support for PyTorch/torch.compile and have a simple test or have a multipy/torch.package-like system leveraging subinterpreters but still allowing packaging only relevant code/modules in self-contained archives

  4. vadimkantorov commented on Jun 2, 2023

    @vadimkantorov
    ContributorAuthor

    @albanD I would say this issue is not about python frontend, but more about "python backend" :)

  5. albanD commented on Jun 2, 2023

    @albanD
    Collaborator

    It would be great to see some tutorial of using multipy together with torch.compile / triton-compiled AOT packages

    They are independent so should "just work" in theory :)

  6. vadimkantorov commented on Jun 2, 2023

    @vadimkantorov
    ContributorAuthor

    Would be good to have a sample showcasing the two :) I think multipy is relatively unknown. And torch.compile currently misses communication about inference time (which was different for torch::jit::load etc)

  7. vadimkantorov commented on Oct 8, 2023

    @vadimkantorov
    ContributorAuthor

    I guess Python 3.12 is released now. So at least it would be nice to have some note in the docs on the support / interaction with sub-interpreters (even if it's not supported yet, it would be better to state it explicitly to save time for the users)

  8. albanD commented on Oct 9, 2023

    @albanD
    Collaborator

    Note that we don't support 3.12 yet. But if you have a good grasp on sub-interpreters, feel free to write a blurb on it!

  9. vadimkantorov commented on Oct 29, 2023

    @vadimkantorov
    ContributorAuthor

    Regarding "better off using multiprocessing", this scenario is mainly about embedding python in some other inference process which might need multiple sub-interpreters (and then maybe spawn is less convenient?). So some basic tests of having torch/domain libraries/numpy operating okay (or not okay) is important, as people are going to try this.

    Also, for this kinds of inference, being able to host the readonly model weights in memory (I guess important for both CPU and CUDA - for RAM my understanding is that it used to have shm, but I don't know if CUDA memory can be shared in some similar way) only once and have them accessible between multiple threads / subinterpreters may need to be discussed...

  10. albanD commented on Oct 31, 2023

    @albanD
    Collaborator

    I don't know if CUDA memory can be shared in some similar way

    Yes you can and that will be done by default if you send a cuda Tensor over multiprocessing objects.

    only once and have them accessible between multiple threads / subinterpreters

    This is already possible for threads or processes based system today. Not sure what subinterpreter adds to the picture (beyond making PyObject ownership more complex since no object should be shared between interpreters)?

  11. vadimkantorov commented on Nov 11, 2023

    @vadimkantorov
    ContributorAuthor

    E.g. maybe some bugs like this? #113520 When they were concealed, they might get hit if these multi-threaded interpreters will be used...

  12. albanD commented on Nov 16, 2023

    @albanD
    Collaborator

    maybe some bugs like this?

    Nope, these registration are happening on the C++ lib which would be shared and would fail the same way.
    I would argue this problem would actually be worse because you would loose the ability to lock between these concurrent calls since they can't interact with each other.

  13. vadimkantorov commented on Jul 16, 2025

    @vadimkantorov
    ContributorAuthor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    module: python frontendFor issues relating to PyTorch's Python frontendtriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions