Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot patch __call__ on an object with vectorcall defined #101497

Open
davidsaxton opened this issue Feb 1, 2023 · 2 comments
Open

Cannot patch __call__ on an object with vectorcall defined #101497

davidsaxton opened this issue Feb 1, 2023 · 2 comments
Labels
topic-C-API type-bug An unexpected behavior, bug, or error

Comments

@davidsaxton
Copy link

davidsaxton commented Feb 1, 2023

It appears that when the type of an object has JAX_TPFLAGS_HAVE_VECTORCALL set on tp_flags, then overrides to call from within python are ignored.

I encountered this bug when using the latest version of JAX, which introduced vectorcalls in tensorflow/tensorflow@bf3eb11 :

import jax
g = jax.jit(lambda x: x + 1)
print(type(g))
# > <class 'google3.third_party.tensorflow.compiler.xla.python.xla_extension.CompiledFunction'>
print(g(5))
# > 6
type(g).__call__ = lambda *args: 0
print(g(5))
# > 6
@davidsaxton davidsaxton added the type-bug An unexpected behavior, bug, or error label Feb 1, 2023
@encukou
Copy link
Member

encukou commented Feb 3, 2023

The class is responsible for ensuring that that vectorcall matches tp_call. Unfortunately, that's hard to do for mutable classes, where tp_call can be overridden by users. Consider making the class immutable.
It'll be changed in 3.12, see the change note: https://docs.python.org/3.12/c-api/call.html#the-vectorcall-protocol

@encukou
Copy link
Member

encukou commented Feb 3, 2023

Also see this warning: https://docs.python.org/3/c-api/typeobj.html?highlight=vectorcall_offset#c.PyTypeObject.tp_vectorcall_offset
PRs to make the docs clearer would be welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-C-API type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants