Skip to content
This repository was archived by the owner on Oct 31, 2023. It is now read-only.

fix silly numpy import/fork bug#24

Merged
tscmoo merged 1 commit into
mainfrom
fix-silly-numpy-import-fork-bug
Feb 5, 2022
Merged

fix silly numpy import/fork bug#24
tscmoo merged 1 commit into
mainfrom
fix-silly-numpy-import-fork-bug

Conversation

@tscmoo

@tscmoo tscmoo commented Feb 5, 2022

Copy link
Copy Markdown
Contributor

This is pretty silly, but during serialization we check if the input is a numpy array (despite #17). For this check, pybind needs to import numpy. During import, my local version of numpy calls fork. From googling, this is seen as a no-no, but it does it regardless. I've not investigated why it does the fork.
Forking during serialization breaks moolib with the usual dont-fork fatal error message (and setting the MOOLIB_ALLOW_FORK env var results in a deadlock).
There is no issue if the user already imported numpy (or pytorch, as that also pulls in numpy). However, if they didn't, even the most basic usage of moolib results in a fatal error.

This fix calls the pybind code that in turn imports numpy during Moolib module initialization, thus numpy is already imported during serialization.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Feb 5, 2022

@heiner heiner left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment thread src/moolib.cc
// numpy forks during import, and pybind can trigger a numpy import
// during serialization (just for checking if the input is a numpy array).
// forks generally break moolib, so do it here where it's safe
py::detail::npy_api::get();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One alternative would be to do that in Python, namely in __init__.py

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this is neater, as it runs the code that actually triggers the issue during serialization. If we imported in init.py, we'd just be hoping that it imports the same numpy and resolves the issue (which it probably would), but this is safer.

@heiner

heiner commented Feb 5, 2022

Copy link
Copy Markdown
Contributor

If I understand this correctly, the issue is likely in the linear algebra library numpy uses, specifically in MKL?

So not strictly speaking a numpy issue even.

@tscmoo tscmoo merged commit 81a693f into main Feb 5, 2022
@tscmoo tscmoo deleted the fix-silly-numpy-import-fork-bug branch February 5, 2022 21:25
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants