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

Use weakref.finalize instead of __del__ for RandomState._generator destruction #8315

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

mroeschke
Copy link

Sometimes upon interpreter exit and using RandomState, the following exception can occur

Exception ignored in: <function RandomState.__del__ at 0x7f0cc41dadd0>
Traceback (most recent call last):
  File "/home/coder/.conda/envs/rapids/lib/python3.10/site-packages/cupy/random/_generator.py", line 65, in __del__
ModuleNotFoundError: import of cupy_backends.cuda.libs halted; None in sys.modules

In order to more robustly destroy RandomState._generator upon exit, a weakref.finalize should be used https://docs.python.org/3/library/weakref.html#comparing-finalizers-with-del-methods

@asi1024 asi1024 added cat:enhancement Improvements to existing features prio:medium labels May 1, 2024
@bdice
Copy link

bdice commented May 2, 2024

Thanks for this @mroeschke! We see this a lot in RAPIDS CI. A fix would be really helpful.

@leofang
Copy link
Member

leofang commented May 11, 2024

@mroeschke have you found a reliable way to reproduce the shundown warning? This treatment has a long history (the earliest I could find is #2806) but it seems wearkref had never been considered for some reason. If we can reproduce the behavior locally and then check with your patch, it'd be great!

@mroeschke
Copy link
Author

Sorry for the delay here @leofang. Here's a reproducer

Before:

>>> import cupy
>>> import sys
>>> cupy.__version__
'13.0.0rc1'
>>> rng = cupy.random.RandomState(1)
>>> sys.exit(1)
Exception ignored in: <function RandomState.__del__ at 0x7fc40cd675b0>
Traceback (most recent call last):
  File "/home/nfs/mroeschke/miniforge3/lib/python3.10/site-packages/cupy/random/_generator.py", line 65, in __del__
ImportError: sys.meta_path is None, Python is likely shutting down

After (with the following local modification)

diff --git a/cupy/random/_generator.py b/cupy/random/_generator.py
index 04e6c67b0..f2ddb5145 100644
--- a/cupy/random/_generator.py
+++ b/cupy/random/_generator.py
@@ -59,7 +59,11 @@ class RandomState(object):
         if method is None:
             method = curand.CURAND_RNG_PSEUDO_DEFAULT
         self._generator = curand.createGenerator(method)
-        self._finalizer = weakref.finalize(self, curand.destroyGenerator, self._generator)
+        
+        def destroyer(*args, **kwargs):
+            print("destroying generator")
+            curand.destroyGenerator(*args)
+        self._finalizer = weakref.finalize(self, destroyer, self._generator)
         self.method = method
         self.seed(seed)
>>> import cupy
>>> import sys
>>> rng = cupy.random.RandomState(1)
>>> sys.exit(1)
destroying generator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cat:enhancement Improvements to existing features prio:medium
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants