-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial Python Interface for cufile Async IO (#376)
Hi there, Thanks for this great repository! I want to use the cuFile async IO in my research project and noticed this kvikio repo. However, the initial support has been done in #259 and tracked in #204, but the Python interface hasn't been done yet. So I exported the write_async and read_async to the CuFile Python class and added test case. This will be very helpful for my project where I want to do the PyTorch training computation and simultaneously load tensors from the SSDs. I created this PR because hopefully, it could be helpful for your repository as well as keeping the Python interface current. Please let me know your thoughts. Thank you. Best Regards, Kun Authors: - Kun Wu (https://github.com/K-Wu) - Mads R. B. Kristensen (https://github.com/madsbk) Approvers: - Mads R. B. Kristensen (https://github.com/madsbk) URL: #376
- Loading branch information
Showing
4 changed files
with
227 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved. | ||
# See file LICENSE for terms. | ||
|
||
import os | ||
|
||
import cupy | ||
import pytest | ||
|
||
import kvikio | ||
import kvikio.defaults | ||
|
||
|
||
def check_bit_flags(x: int, y: int) -> bool: | ||
"""Check that the bits set in `y` is also set in `x`""" | ||
return x & y == y | ||
|
||
|
||
@pytest.mark.parametrize("size", [1, 10, 100, 1000, 1024, 4096, 4096 * 10]) | ||
def test_read_write(tmp_path, size): | ||
"""Test basic read/write""" | ||
filename = tmp_path / "test-file" | ||
|
||
stream = cupy.cuda.Stream() | ||
|
||
# Write file | ||
a = cupy.arange(size) | ||
f = kvikio.CuFile(filename, "w") | ||
assert not f.closed | ||
assert check_bit_flags(f.open_flags(), os.O_WRONLY) | ||
assert f.raw_write_async(a, stream.ptr).check_bytes_done() == a.nbytes | ||
|
||
# Try to read file opened in write-only mode | ||
with pytest.raises(RuntimeError, match="unsupported file open flags"): | ||
# The exception is raised when we call the raw_read_async API. | ||
future_stream = f.raw_read_async(a, stream.ptr) | ||
future_stream.check_bytes_done() | ||
|
||
# Close file | ||
f.close() | ||
assert f.closed | ||
|
||
# Read file into a new array and compare | ||
b = cupy.empty_like(a) | ||
c = cupy.empty_like(a) | ||
f = kvikio.CuFile(filename, "r+") | ||
assert check_bit_flags(f.open_flags(), os.O_RDWR) | ||
|
||
future_stream = f.raw_read_async(b, stream.ptr) | ||
future_stream2 = f.raw_write_async(b, stream.ptr) | ||
future_stream3 = f.raw_read_async(c, stream.ptr) | ||
assert ( | ||
future_stream.check_bytes_done() | ||
== future_stream2.check_bytes_done() | ||
== future_stream3.check_bytes_done() | ||
== b.nbytes | ||
) | ||
assert all(a == b) | ||
assert all(a == c) |