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 support for subclassing cupy.ndarray

See original GitHub issue

Hi!

Many projects (including mine) use subclasses of numpy.ndarray, like this:

class Field(np.ndarray):
    def __new__(cls, data, prefix=1):
        data = np.asarray(data)
        field = data.view(cls)
        field.prefix = prefix
        return field

    def __array_finalize__(self, field):
        if field is None:
            return
        self.prefix = getattr(field, 'prefix', 1)

If one tries to use cupy.ndarray as a numpy replacement, then at creation of the Field instance an error is raised:

AttributeError: 'cupy.core.core.ndarray' object has no attribute 'prefix'

Are there plans to support in cupy subclassing similar to numpy approach? Maybe some workarounds are possible currently?

Regards, Marek

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:15 (14 by maintainers)

github_iconTop GitHub Comments

3reactions
takagicommented, Jul 4, 2022

I’m trying to realize cupy.ndarray subclassing by workaround1 in the gist The following TODO is for managing its progress.

  • Direct constructor call #6716
  • View casting #6720
  • New from template
    • ufuncs
    • ndarray methods
    • routines (this part includes many things and is to be expanded)
  • documentation
2reactions
takagicommented, Jul 4, 2022

Yes.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to subclass a CuPy array? - python
According to the documentation of numpy.ndarray.view, it takes two input arguments: dtype and type . If the dtype argument is a subclass of ......
Read more >
cupy.asanyarray — CuPy 11.4.0 documentation
Converts an object to array. This is currently equivalent to cupy.asarray() , since there is no subclass of cupy.ndarray in CuPy. Note that...
Read more >
ndarray subclass not preserved by real/imag · Issue #6887
Hi, just a small bug. I was testing v11.0.0rc1 as I was interested in ndarray subclassing. I noticed that the real and imag...
Read more >
Standard array subclasses — NumPy v1.23 Manual
Subclassing a numpy.ndarray is possible but if your goal is to create an array with modified behavior, as do dask arrays for distributed...
Read more >
CuPy Documentation
Supports the semantics identical to numpy.ndarray, including basic ... This requirement is optional if you install CuPy from conda-forge.
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