Skip to content

Conversation

viantirreau
Copy link
Contributor

@viantirreau viantirreau commented Mar 17, 2021

Close #4898

@leofang
Copy link
Member

leofang commented Mar 17, 2021

Apparently we missed this because there's no test covering any copy_*_async() functions that use streams, at least they are not directly tested in test_memory.py.

@viantirreau is it possible for you to follow #4562 and add async tests? It should be sufficient to duplicate existing tests with streams. (Even better: parametrize the tests with and without streams.) Let me know if you need any help or me taking over the test part (since it was me who messed up 😅), thanks!

The default uses CUDA stream of the current context.
"""
ptr = mem if isinstance(mem, int) else mem.value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just checked. Could you move this line to the if size > 0: block below? This would align with the other function copy_to_host_async in the same file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem!
I will work on this tonight :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets delete it in +473, now is dupped :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooops 😆

@viantirreau
Copy link
Contributor Author

viantirreau commented Mar 18, 2021

Just a heads-up. I'm not very acquainted with Python tests, especially regarding GPUs 😆
Nevertheless, I think I can pull this off by looking at your PR #4562 and the other tests.

Even better: parametrize the tests with and without streams

By parametrized you mean using the decorator? If that's the case, I was thinking about creating a class especially for the async methods, something in the lines of

@testing.parameterize(*testing.product({
    'use_streams': [True, False],
}))
@testing.gpu
class TestMemoryPointerAsync(unittest.TestCase):
    
    def setUp(self):
        self.stream = stream_module.Stream() if self.use_streams else None

    def test_copy_to_and_from_host_async_using_raw_ptr(self):
        a_gpu = memory.alloc(4)
        a_cpu = ctypes.c_int(100)
        a_cpu_ptr = ctypes.cast(ctypes.byref(a_cpu), ctypes.c_void_p)
        a_gpu.copy_from_async(a_cpu_ptr.value, 4, stream=self.stream)

        b_cpu = ctypes.c_int()
        b_cpu_ptr = ctypes.cast(ctypes.byref(b_cpu), ctypes.c_void_p)
        a_gpu.copy_to_host_async(b_cpu_ptr.value, 4, stream=self.stream)
        assert b_cpu.value == a_cpu.value

    def test_copy_from_device_async_using_raw_ptr(self):
        a_gpu = memory.alloc(4)
        a_cpu = ctypes.c_int(100)
        a_cpu_ptr = ctypes.cast(ctypes.byref(a_cpu), ctypes.c_void_p)
        a_gpu.copy_from_async(a_cpu_ptr.value, 4, stream=self.stream)

        b_gpu = memory.alloc(4)
        b_gpu.copy_from_async(a_gpu, 4, stream=self.stream)
        b_cpu = ctypes.c_int()
        b_cpu_ptr = ctypes.cast(ctypes.byref(b_cpu), ctypes.c_void_p)
        b_gpu.copy_to_host_async(b_cpu_ptr.value, 4, stream=self.stream)
        assert b_cpu.value == a_cpu.value

When defining self.stream, I'm hesitant about whether it's correct to use stream_module.Stream() or cupy.cuda.Stream(), as I saw both being used in stream-like variables in the cuda memory tests.

PS: I'm now thinking that I could just parametrize the existing class TestMemoryPointer, and add these 3 methods. Appreciate your view on this.

@viantirreau
Copy link
Contributor Author

Ended up copying other two synchronous tests and I confirmed that all of them are passing in copy_*_async(), yay 🥳
Now I think that it's better to have this TestMemoryPointerAsync class alone, so as to not interfere with (and clutter) the non-stream versions of the tests. What do you think?

Copy link
Member

@leofang leofang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot, @viantirreau! I think it makes sense to isolate async tests to a separate class. Just left a few comments, mainly to ensure no race condition happens.

@leofang
Copy link
Member

leofang commented Mar 19, 2021

FYI, in the future to commit a bunch of suggestions at once, you can go to the "Files changed" tab, and select "add suggestion to batch". It'd appear as one single commit, reducing the need of multiple clicks 🙂

截圖 2021-03-19 下午1 26 39

Copy link
Member

@leofang leofang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks again for reporting the bug and for contributing the fix.

@leofang
Copy link
Member

leofang commented Mar 19, 2021

Jenkins, test this please

@viantirreau
Copy link
Contributor Author

Wooow! That's a great tip hahaha.

Thanks for your help, I enjoyed the challenge :)

@leofang
Copy link
Member

leofang commented Mar 19, 2021

Another tip: @viantirreau you can edit your PR description to include this clause "Close #4898". By the time your PR is merged, the issue will be automatically closed. A GitHub convenience feature.

@leofang
Copy link
Member

leofang commented Mar 19, 2021

Note: I confirm locally that the added tests in this PR would fail if the bug were not fixed.

@chainer-ci
Copy link
Member

Jenkins CI test (for commit 2e4c2b3, target branch master) succeeded!

@emcastillo emcastillo added this to the v9.0.0rc1 milestone Mar 20, 2021
@emcastillo emcastillo added the st:test-and-merge (deprecated) Ready to merge after test pass. label Mar 20, 2021
@leofang
Copy link
Member

leofang commented Mar 20, 2021

@emcastillo Automerge seems to be glitchy?

@emcastillo emcastillo merged commit 33b8565 into cupy:master Mar 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cat:bug Bugs prio:high st:test-and-merge (deprecated) Ready to merge after test pass.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

UnboundLocalError on cuda/memory.pyx
5 participants