X Tutup
The Wayback Machine - https://web.archive.org/web/20240111222518/https://github.com/python/cpython/issues/106051
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embedded Python Crashes When Importing Torch With C API #106051

Closed
tempaccountforissue opened this issue Jun 23, 2023 · 8 comments
Closed

Embedded Python Crashes When Importing Torch With C API #106051

tempaccountforissue opened this issue Jun 23, 2023 · 8 comments
Labels
pending The issue will be closed if no feedback is provided type-bug An unexpected behavior, bug, or error

Comments

@tempaccountforissue
Copy link

Bug report

I'm trying to embed Python into a C++ based application using the Python/C API. If I don't include an embedded Python installation inside my projects folder, then the program uses my local Python install by default. Using the local install doesn't result in any errors and torch is able to import successfully. However, if I include a Windows embeddable package (64-bit) version of Python in my projects folder, then Python crashes after calling PyImport_ImportModule("importsFile") in my C++ file.

Inside of importsFile.pyI have the following code:

import numpy as np
import matplotlib
import torch

If I comment out import torch, then the other modules are able to import without error. However, if I leave import torch uncommented, then Python crashes.

I'm not sure if this is a bug on Python's side or PyTorch's side. Python only seems to crash when importing torch specifically, but it also only crashes when calling PyImport_ImportModule. So, it seems to me like it could be on either end.

Is this a known issue?
How can I fix it?

Your environment

  • CPython versions tested on:
    Python 3.9.7 - Windows embeddable package (64-bit)

  • Operating system and architecture:
    Windows 10 64-bit

@tempaccountforissue tempaccountforissue added the type-bug An unexpected behavior, bug, or error label Jun 23, 2023
@JelleZijlstra
Copy link
Member

Have you tried using a debugger to find the cause of the crash?

It's more likely that this is a bug in Torch than in CPython.

@tempaccountforissue
Copy link
Author

@JelleZijlstra I've never used a debugger with the Python/C API. Are there any resources you know of that I could use for this?

@JelleZijlstra
Copy link
Member

Any C++ debugger should do.

@ethanhs
Copy link
Contributor

ethanhs commented Jun 24, 2023

Also details about how you add torch to your import path/how you install it would help in replicating the crash.

@tempaccountforissue
Copy link
Author

@ethanhs It's an embedded version of Python, so I followed this Stack Overflow post to get pip installed. Then I followed the instructions on PyTorch's website to install Torch.

I used the following configuration for installing PyTorch:

  • PyTorch Build = Stable (2.0.1)
  • Your OS = Windows
  • Package = Pip
  • Language = Python
  • Compute Platform = CUDA 11.8
  • Run this Command: = pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118

Do you need any other information?

@iritkatriel
Copy link
Member

Have you contacted PyTorch about this? Core devs don't typically debug issues like this, which involve third party code, unless it's pretty clear that it's indeed a Python bug and there is no way to narrow it down - those cases are rare, and this doesn't seem to be one of them.

@iritkatriel iritkatriel added the pending The issue will be closed if no feedback is provided label Dec 1, 2023
@tempaccountforissue
Copy link
Author

@iritkatriel I created an issue on PyTorch's GitHub page around the same time I created this one. They have been (slowly) looking into it. I will update this issue if they find anything definitive.

@iritkatriel
Copy link
Member

Thanks. If they find reason to believe there is a bug in python then please create an issue with a clear bug report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending The issue will be closed if no feedback is provided type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants
X Tutup