Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Identity() operator dtype?#780

Unanswered
nschloe asked this question inQ&A
Discussion options

In some of my numerical codes, I have a classIndentity() that represents cheap identity matrices. To make it work with things likeresult_type, I assign it the "lowest possible" dtype,"u1":

importnumpyasnpfromarray_api_compatimportarray_namespaceclassIdentity:dtype=np.dtype("u1")def__matmul__(self,x):returnxI=Identity()x=np.array([1,2,3])xp=array_namespace(x)print(xp.result_type(x,I))
int64

I would now like to make this work with generalxp arrays. Assigning a concrete implementation for dtype though isn't what I want. UsingNone as a dtype sounds plausible, but with NumPy gives a warning and then a wrongresult_type:

DeprecationWarning: in the future the `.dtype` attribute of a given datatype object must be a valid dtype instance. `data_type.dtype` may need to be coerced using `np.dtype(data_type.dtype)`. (Deprecated NumPy 1.20)  print(xp.result_type(x, I))
float64
You must be logged in to vote

Replies: 5 comments

Comment options

Hmm, based on just this example that may be a bit tricky to make work portably. Is thedtype attribute actually needed (I assumematmul andresult_type isn't the actual extent of API usage)? And if so, is it actually necessary that there is a singleIdentity object? If not, changing the example to this should work:

classIdentity:def__init__(self,dtype):self.dtype=dtypex=np.array([1,2,3])I=Identity(x.dtype)
You must be logged in to vote
0 replies
Comment options

I'm not sure that this kind of duck typing can be made to work, at least in a guaranteed way, in the current spec. There's nothing inresult_type that says it should support anything other thanarray anddtype inputs, wherearray is the array API library array type (or union of array types). It doesn't say anywhere that it should get the dtype from array instances in a duck typable way using.dtype.

With that being said, ignoring those issues, one issue with your "lowest possible" dtype is that integer and float dtypesdon't promote to each other. You could have two objects, one for integers with uint8 and one for floats with float32. Although also note that uint8 and int8 are both equally low in the dtype promotion graph, so choosing one will always cause the other to promote to int16.

TBH, what you likely really want is to use a library like JAX that lets you just writeeye() and optimizes the computation graph without explicitly creating the full array.

You must be logged in to vote
0 replies
Comment options

Is the dtype attribute actually needed

A typical use is, e.g.,

defcool_linear_solver(A,b,M=None):m,n=A.shapeassertm==nM=MorIdentity(n)dt=xp.result_dtype(A,b,M)x=xp.array(n,dtype)# ...# x += M @ (b - A @ x)# ...

IfM is given, it might "elevate"x's dtype requirements, but if not, it's just down toA andb.

Perhaps the easiest thing to do is to write

dt=xp.result_dtype(A,b,M.dtype)

and make explicit the fact that I expectM to have adtype attribute.@asmeurer's remark aboutint8 vsuint8 is correct of course, but so far hasn't given me any trouble (arrays are mostly float, something complex). Not sure what a "proper" solution would look like.

You must be logged in to vote
0 replies
Comment options

This would work based on the snippet you have given, right?

if M is None:    dt = xp.result_dtype(A, b)    M = Identity(n)else:    dt = xp.result_dtype(A, b, M)
You must be logged in to vote
0 replies
Comment options

@lucascolley Yeah, but there are several otherM-like entities that would need to factored in. Icould build up a list like

dtype_determining_objects= [A,b]ifMisnotNone:dtype_determining_objects.append(M)ifNisnotNone:dtype_determining_objects.append(N)ifQisnotNone:dtype_determining_objects.append(Q)dt=xp.result_dtype(*dtype_determining_objects)

but this feels clumsy.

You must be logged in to vote
0 replies
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
QuestionGeneral question.
4 participants
@nschloe@asmeurer@rgommers@lucascolley
Converted from issue

This discussion was converted from issue #739 on April 04, 2024 04:48.


[8]ページ先頭

©2009-2025 Movatter.jp