Skip to content

Instantly share code, notes, and snippets.

@GenevieveBuckley
Created June 15, 2022 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GenevieveBuckley/6adb2b134df3c9b17f814448a8acffc5 to your computer and use it in GitHub Desktop.
Save GenevieveBuckley/6adb2b134df3c9b17f814448a8acffc5 to your computer and use it in GitHub Desktop.
Cupy context manager
class CupyContextManager:
def __init__(self):
self.previous_np = None
def __enter__(self):
self.previous_np = globals().get("np")
import cupy as np
globals()["np"] = np
return np
def __exit__(self, exc_type, exc_value, exc_tb):
if self.previous_np is not None:
globals()["np"] = self.previous_np
else:
globals().pop('np', None)
with CupyContextManager() as np:
print(np)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment