cupy in destructor is changed to None
See original GitHub issueDescription
Hi,
I found out cupy in destructor would be None
To Reproduce
Here are the scripts to reproduce
c.py
import cupy
import numpy as np
import ctypes as C
class ManagedMemory():
# https://stackoverflow.com/questions/23930671/how-to-create-n-dim-numpy-array-from-a-pointer
def __init__(self, shape, dtype):
size = 1
for s in shape:
size *= s
self.mem = cupy.cuda.runtime.mallocManaged(size * np.dtype(dtype).itemsize)
self.ptr =C.cast(self.mem, C.POINTER(C.c_float))
ctype = self.infer_from_dtype(dtype)
self.tensor = np.ctypeslib.as_array(self.ptr, shape=shape)
def infer_from_dtype(self, dtype):
if dtype == np.float32:
return C.c_float
elif dtype == np.int32:
return C.c_int
else:
raise NotImplementedError()
def __del__(self):
del self.tensor
cupy.cuda.runtime.free(self.mem)
d.py
from c import ManagedMemory
import numpy as np
mem = ManagedMemory((5,), np.float32)
python d.py
would give
Exception ignored in: <bound method ManagedMemory.__del__ of <c.ManagedMemory object at 0x7f9669da58>>
Traceback (most recent call last):
File "/home/acer/charmander/c.py", line 25, in __del__
AttributeError: 'NoneType' object has no attribute 'cuda'
Installation
Wheel (pip install cupy-***
)
Environment
# Paste the output here
addict==2.4.0
-e git+ssh://git@bitbucket.org/acer4smartanywhere/charmander.git@95646c34f2f333cdee8201caeae4b54197416f38#egg=charmander_v2
cupy @ file:///home/acer/cupy-8.2.0-cp36-cp36m-linux_aarch64.whl
cycler==0.11.0
Cython==0.29.26
dataclasses==0.8
decorator==4.4.2
easydict==1.9
fastrlock==0.8
imageio==2.13.5
joblib==1.1.0
kiwisolver==1.3.1
lmdb==1.3.0
matplotlib==3.3.4
-e git+https://github.com/open-mmlab/mmclassification.git@5232965b17b6c050f9b328b3740c631ed4034624#egg=mmcls
mmcv-full @ file:///home/acer/mmcv/dist/mmcv_full-1.4.3-cp36-cp36m-linux_aarch64.whl
-e git+https://github.com/open-mmlab/mmdetection.git@ff9bc39913cb3ff5dde79d3933add7dc2561bab7#egg=mmdet
networkx==2.5.1
ninja==1.10.2.3
numpy==1.19.4
onnx==1.10.2
opencv-python==4.5.5.62
packaging==21.3
Pillow==8.4.0
protobuf==3.19.3
pybind11==2.9.0
pycocotools==2.0.4
pyparsing==3.0.6
python-dateutil==2.8.2
PyWavelets==1.1.1
PyYAML==6.0
scikit-image==0.17.2
scikit-learn==0.24.2
scipy==1.5.4
setuptools-cythonize==1.0.6
six==1.16.0
terminaltables==3.1.10
threadpoolctl==3.0.0
tifffile==2020.9.3
torch==1.9.0
torchvision @ file:///home/acer/vision/dist/torchvision-0.10.0a0%2B300a8a4-cp36-cp36m-linux_aarch64.whl
tqdm==4.62.3
-e git+ssh://git@bitbucket.org/acer4smartanywhere/treecko.git@9eb62aa97f27774d104950664b6610dd4041d5a2#egg=treecko
typing_extensions==4.0.1
yapf==0.32.0
Additional Information
By the way,
There is an cupy.cuda.ManagedMemory, Do I need to manually free its internal memory? I found out it would call https://github.com/cupy/cupy/blob/v10.0.0/cupy/cuda/memory.pyx#L245
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
Intermittent OutOfMemoryError in Cupy - Stack Overflow
I'm limited by memory and keep loading data to the GPU using cupy.asarray() and then removing it with setting the variables to None....
Read more >Non Empty Destructors in C++ - pvigier's blog
The problem is not really that the destructor is non empty but that the destructor is nontrivial: there is a release of memory...
Read more >NVIDIA Deep Learning TensorRT Documentation
This NVIDIA TensorRT Developer Guide demonstrates how to use the C++ and Python APIs for implementing the most common deep learning layers.
Read more >CuPy Documentation - Read the Docs
For example, you can build CuPy using non-default CUDA directory by ... To switch to another GPU device, use the Device context manager:....
Read more >[Solved]-Can I call cuda code written in cupy code from c++?
I'm not familiar with CuPy but have a look at the Python C API (https://docs.python.org/3/c-api/). This allows you to run Python functions in...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
__del__
is called other Python modules and objects are still alive. We hit (a few variants of) this issue before, and realized that even after Python 3.4 that implemented PEP 442 this could still happen. For a fix, you can either check ifcupy.cuda.runtime.free
isNone
, or better use the finalizer instead of__del__
.ManagedMemory
.@leofang
yup. I am also referring
ManagedMemory
, which hascupy.cuda.ManagedMemory
as its member.when create
ManagedMemory
instance fromd.py
, it can’t destroycupy.cuda.ManagedMemory
automatically after program exits.like