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

Numba Wrapper Address Protocol (WAP) is broken #8282

Open
2 tasks done
guilhermeleobas opened this issue Jul 26, 2022 · 5 comments
Open
2 tasks done

Numba Wrapper Address Protocol (WAP) is broken #8282

guilhermeleobas opened this issue Jul 26, 2022 · 5 comments
Labels
bug - regression A regression against a previous version of Numba bug - typing Bugs: occuring at typing time

Comments

@guilhermeleobas
Copy link
Collaborator

guilhermeleobas commented Jul 26, 2022

Reporting a bug

  • I have tried using the latest released version of Numba (most recent is
    visible in the change log (https://github.com/numba/numba/blob/main/CHANGE_LOG).
  • I have included a self contained code sample to reproduce the problem.
    i.e. it's possible to run as 'python bug.py'.

Reproducer is from Numba docs:

import numba, ctypes, ctypes.util, math

libm = ctypes.cdll.LoadLibrary(ctypes.util.find_library('m'))

class LibMCos(numba.types.WrapperAddressProtocol):
    def __wrapper_address__(self):
        return ctypes.cast(libm.cos, ctypes.c_voidp).value
    def signature(self):
        return numba.float64(numba.float64)

cos = LibMCos()

@numba.njit
def foo(f, x):
    return f(x)

print(foo(cos, 0.0))
$ python repro.py
Traceback (most recent call last):
  File "/home/guilhermeleobas/git/numba/repro.py", line 17, in <module>
    print(foo(cos, 0.0))
  File "/home/guilhermeleobas/git/numba/numba/core/dispatcher.py", line 468, in _compile_for_args
    error_rewrite(e, 'typing')
  File "/home/guilhermeleobas/git/numba/numba/core/dispatcher.py", line 409, in error_rewrite
    raise e.with_traceback(None)
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
non-precise type pyobject
During: typing of argument at /home/guilhermeleobas/git/numba/repro.py (15)

File "repro.py", line 15:
def foo(f, x):
    return f(x)
@stuartarchibald
Copy link
Contributor

Thanks for the report. This stopped working sometime between 0.53 and 0.54.

@stuartarchibald
Copy link
Contributor

git bisect suggests: 98f733f is the first bad commit.

@stuartarchibald
Copy link
Contributor

Given the contents of 98f733f, I wonder if WAP relied on some import ordering/side effect.

@stuartarchibald
Copy link
Contributor

This "fixes" it:

diff --git a/numba/core/cpu.py b/numba/core/cpu.py
--- a/numba/core/cpu.py
+++ b/numba/core/cpu.py
@@ -58,6 +58,17 @@ class CPUContext(BaseContext):
         # Initialize NRT runtime
         rtsys.initialize(self)
 
+        from numba.core.types import FunctionType, WrapperAddressProtocol
+        from numba.extending import typeof_impl
+        @typeof_impl.register(WrapperAddressProtocol)
+        def typeof_function_type(val, c):
+            if isinstance(val, WrapperAddressProtocol):
+                sig = val.signature()
+            else:
+                raise NotImplementedError(
+                    f'function type from {type(val).__name__}')
+            return FunctionType(sig)
+
         # Add lower_extension attribute
         self.lower_extensions = {}
         from numba.parfors.parfor_lowering import _lower_parfor_parallel

the issue is essentially that the WrapperAddressProtocol is not registered early enough for typing to know what it is.

@stuartarchibald stuartarchibald added bug - regression A regression against a previous version of Numba bug - typing Bugs: occuring at typing time and removed needtriage labels Jul 26, 2022
@guilhermeleobas
Copy link
Collaborator Author

Thanks, Stuart. I think the tests for WAP are not failing on CI because test.SubTest is swallowing any exception thrown by the compiler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug - regression A regression against a previous version of Numba bug - typing Bugs: occuring at typing time
Projects
None yet
Development

No branches or pull requests

2 participants