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
Structured dtypes with multi-dim fields: numba either throws or crashes #3158
Comments
Thanks for the report. I can reproduce the raised |
This bug occurs in much simpler cases:
Accessing A quick fix is to replace
P.S. I would increase this bug's priority - it seems like a fairly common situation. |
@kazhoyan thanks, this is an exact reproducer: from numba import njit
import numpy as np
Item = np.dtype([('some_field', np.int32)])
SomeList = np.dtype([('items', Item, 10)])
arr = np.zeros((2,), SomeList)
@njit
def foo(x):
x[0]
foo.py_func(arr)
foo(arr) Pull Requests to fix/promote discussion around such issues are welcomed. |
This still open? |
Yes, still reproduces on 0.54.
Not that I'm aware of I'm afraid. |
Possible starter patch for this and #1469: diff --git a/numba/core/types/npytypes.py b/numba/core/types/npytypes.py
index 25050f30c..e9da0249d 100644
--- a/numba/core/types/npytypes.py
+++ b/numba/core/types/npytypes.py
@@ -209,6 +209,10 @@ class Record(Type):
return as_struct_dtype(self)
+ @property
+ def bitwidth(self):
+ return self.dtype.itemsize * 8
+
def can_convert_to(self, typingctx, other):
"""
Convert this Record to the *other*.
@@ -427,6 +431,10 @@ class Array(Buffer):
if (not aligned or
(isinstance(dtype, Record) and not dtype.aligned)):
self.aligned = False
+ if isinstance(dtype, NestedArray):
+ tmp = Array(dtype.dtype, dtype.ndim, 'C')
+ ndim += tmp.ndim
+ dtype = tmp.dtype
if name is None:
type_name = "array"
if not self.mutable:
@@ -553,6 +561,10 @@ class NestedArray(Array):
"""
def __init__(self, dtype, shape):
+ if isinstance(dtype, NestedArray):
+ tmp = Array(dtype.dtype, dtype.ndim, 'C')
+ shape += dtype.shape
+ dtype = tmp.dtype
assert dtype.bitwidth % 8 == 0, \
"Dtype bitwidth must be a multiple of bytes"
self._shape = shape This works around the issue, but the approach might need to be more cleanly thought-out (and tested). With a modified version of @stuartarchibald's reproducer above (to print out return values): from numba import njit
import numpy as np
Item = np.dtype([('some_field', np.int32)])
SomeList = np.dtype([('items', Item, 10)])
arr = np.zeros((2,), SomeList)
@njit
def foo(x):
return x[0]
print(foo.py_func(arr))
print(foo(arr)) I get
with this patch. |
Fixes Issues: - numba#1469 (numba#1469) - numba#3158 (numba#3158)
* main: (2583 commits) Correct sequence in test_issue_3158_1 Fix flake8 checks since upgrade to flake8=6.x Use nonzero data in tests of Issue numba#3158 Apply suggestions from PR numba#8120 review Make Numba dependency check run ahead of Numba internal imports. Check for void return type in compile_ptx applying review suggestions Add -e to all Azure script steps CI: Use `set -e` in "Before Install" step and fix install Implement cleanups suggested in PR numba#8120 feedback Remove cpu NRT init guard, rtsys.initialize already has this. remove forced fail test moved file (2) Add test for docstring. Add update_wrapper to dufunc. Remove superfluous assertion. Fix failing warning checking tests. Fix failing tests. * now uses np.testing.assert_array_equal for more detailed error reporting Supply concrete timeline for objmode fallback deprecation. ...
Given this (quite trivial) example:
numba will most likely throw like so:
... or you could also get
The text was updated successfully, but these errors were encountered: