question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add CuPy compress

See original GitHub issue

Would be useful to have an compress implementation for CuPy arrays.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
grlee77commented, Oct 3, 2019

Implementation can sometimes be tricky because you often need some familiarity with NumPy/C to determine what is going on there and then some familiarity with CUDA/C++ to be able to implement an equivalent function for CuPy.

In the case of compress, you could check whether maybe it can be implemented by adapting an existing function like _take as defined in cupy/core/_routines_indexing.pyx. (NumPy’s compress ultimately calls PyArray_Compress)

An easier, pure Python route is available in cases where a function can be defined in terms of other functions already implemented in CuPy. In the case of compress, it seems like the following the following might work:

def compress(condition, a, axis=None, out=None):
    # TODO: replace with more efficient kernel-based implementation
    return cupy.take(a, cupy.where(condition)[0], axis, out)

This is not computationally optimal as we have to call both cupy.where and cupy.take instead of just a single underlying CUDA kernel, but it is better than nothing. I think a PR of that sort could potentially be accepted if good tests are added to make sure the functionality matches what is provided in NumPy.

Tests would need to be added to the existing file tests/cupy_tests/core_tests/test_ndarray_indexing.py.

1reaction
jakirkhamcommented, Feb 20, 2020

Yep, it’s still open. Thanks for giving this a go Harshan 🙂

Read more comments on GitHub >

github_iconTop Results From Across the Web

cupy.savez_compressed — CuPy 11.4.0 documentation
Saves one or more arrays into a file in compressed .npz format. It is equivalent to cupy.savez() function except the output file is...
Read more >
cupy.append — CuPy 11.4.0 documentation
A copy of arr with values appended to axis . Note that append does not occur in-place: a new array is allocated and...
Read more >
cupy.ndarray — CuPy 11.4.0 documentation
Returns selected slices of this array along given axis. Warning. This function may synchronize the device. See also. cupy.compress() for full documentation, ...
Read more >
cupy.resize — CuPy 11.4.0 documentation
cupy.resize# ... Return a new array with the specified shape. If the new array is larger than the original array, then the new...
Read more >
cupy.save — CuPy 11.4.0 documentation
cupy.save#. cupy.save(file, arr, allow_pickle=None)[source]#. Saves an array to a binary file in .npy format. Parameters.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found