-
Notifications
You must be signed in to change notification settings - Fork 65
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 cupy to measure memory leak #777
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
# limitations under the License. | ||
# | ||
|
||
import cuda.cudart | ||
import pytest | ||
|
||
from ...util.io import open_image_cucim | ||
|
@@ -22,23 +23,25 @@ | |
|
||
|
||
def test_read_region_cuda_memleak(testimg_tiff_stripe_4096x4096_256_jpeg): | ||
import GPUtil | ||
|
||
gpus = GPUtil.getGPUs() | ||
|
||
if len(gpus) == 0: | ||
def get_used_gpu_memory_mib(): | ||
"""Get the used GPU memory in MiB.""" | ||
status, free, total = cuda.cudart.cudaMemGetInfo() | ||
if status != cuda.cudart.cudaError_t.cudaSuccess: | ||
raise RuntimeError("Failed to get GPU memory info.") | ||
memory_used = (total - free) / (2**20) | ||
return memory_used | ||
|
||
status, num_gpus = cuda.cudart.cudaGetDeviceCount() | ||
if status != cuda.cudart.cudaError_t.cudaSuccess or num_gpus == 0: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can also potentially just use CuPy for this and avoid having to add the cuda-python dependency: add up top: import cupy as cp then can use here: def get_used_gpu_memory_mib():
"""Get the used GPU memory in MiB."""
dev = cp.cuda.Device()
free, total = dev.mem_info
memory_used = (total - free) / (2**20)
return memory_used
num_gpus = cp.cuda.runtime.getDeviceCount()
if num_gpus == 0: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great. I'll implement that and remove the cuda-python dependency. |
||
pytest.skip("No gpu available") | ||
|
||
img = open_image_cucim(testimg_tiff_stripe_4096x4096_256_jpeg) | ||
|
||
gpu = gpus[0] | ||
mem_usage_history = [gpu.memoryUsed] | ||
mem_usage_history = [get_used_gpu_memory_mib()] | ||
|
||
for i in range(10): | ||
_ = img.read_region(device="cuda") | ||
gpus = GPUtil.getGPUs() | ||
gpu = gpus[0] | ||
mem_usage_history.append(gpu.memoryUsed) | ||
mem_usage_history.append(get_used_gpu_memory_mib()) | ||
|
||
print(mem_usage_history) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
out of curiosity, what does this
&test_cuda_python_cu11
notation do?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a YAML anchor. We use this to define a list that we re-use in
*test_cuda_python_cu11
. We need a "fallback" list when thecuda
selector is not defined.