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

Added is identity comparison for numpy records and added tests as is_comparison_tests.py #9384

Open
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

Kamakshi8104
Copy link

Hello!

I have added the workaround to numba/np/arrayobj.py and have created test cases for the same.
Please let me know if any more changes are required
regards,
Kamakshi

@gmarkall gmarkall linked an issue Jan 9, 2024 that may be closed by this pull request
Copy link
Member

@gmarkall gmarkall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many thanks for the PR. I've posted some initial feedback to address - once that's addressed, we may need an additional round of review on the tests (I haven't gone further commenting on the test cases as there already some issues there).

Comment on lines 53 to 65
def test_kernel(a, b) -> None:
print(int(a is a))
print(int(a is b))
print(int(b is a))
print(int(b is b))


a = np.zeros(1, dtype=[("a", "i4"), ("b", "i4")])[0]
b = a.copy()
njit(test_kernel)(a, b)
cuda.jit(test_kernel)[1, 1](a, b)
cuda.synchronize()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The workaround is the lowering implementation record_is, not all the code that went with it to demonstrate that it worked correctly.

Suggested change
def test_kernel(a, b) -> None:
print(int(a is a))
print(int(a is b))
print(int(b is a))
print(int(b is b))
a = np.zeros(1, dtype=[("a", "i4"), ("b", "i4")])[0]
b = a.copy()
njit(test_kernel)(a, b)
cuda.jit(test_kernel)[1, 1](a, b)
cuda.synchronize()

@@ -41,6 +41,28 @@
_choose_concatenation_layout)


@lower_builtin(operator.is_, types.Record, types.Record)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than inserting this lowering right at the top of the file, the natural place for it would be in the Comparisons section, next to the implementation of array_is.

@@ -29,7 +29,7 @@
from numba.core.typing import signature
from numba.core.types import StringLiteral
from numba.core.extending import (register_jitable, overload, overload_method,
intrinsic, overload_attribute)
intrinsic, overload_attribute, lower_builtin)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lower_builtin was already imported.

Suggested change
intrinsic, overload_attribute, lower_builtin)
intrinsic, overload_attribute)

@@ -12,7 +12,7 @@

import numpy as np

from numba import pndindex, literal_unroll
from numba import pndindex, literal_unroll, cuda, njit, types #type : ignore
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

types was already imported. cuda cannot be imported in Numba outside of the numba.cuda module in general.

Suggested change
from numba import pndindex, literal_unroll, cuda, njit, types #type : ignore
from numba import pndindex, literal_unroll

@@ -0,0 +1,75 @@
import numpy as np
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whilst it's good to have added tests, they should be in the numba.tests module, and should fit in with the existing unit test framework.

a = np.zeros(1, dtype=[("a", "i4"), ("b", "i4")])[0]
b = a.copy()

result = cuda.jit(test_kernel)[1, 1](a, b)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CUDA kernels cannot return values.

a = np.zeros(1, dtype=[("a", "i4"), ("b", "i4")])[0]
b = a.copy()

result = test_kernel(a, b)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_kernel returns None in its present implementation (which needs removing from numba.np.arrayobj also).

@gmarkall gmarkall added the 4 - Waiting on author Waiting for author to respond to review label Jan 9, 2024
Copy link
Author

@Kamakshi8104 Kamakshi8104 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey ...thank you for the feedback! I have incorporated changes mentioned by you . If anything else still needs to be done please let me know 🙂

@gmarkall
Copy link
Member

Thanks for making changes. For future changes - please could you avoid force-pushing as this tends to confuse the Github review interface - many thanks in advance!

@gmarkall gmarkall added 4 - Waiting on reviewer Waiting for reviewer to respond to author and removed 4 - Waiting on author Waiting for author to respond to review labels Jan 10, 2024
@gmarkall
Copy link
Member

Having skimmed over the latest revision, I'm not going to be able to spend more time reviewing this PR in its current form - I think it would be helpful for you to receive additional mentorship to fill in some of the conceptual gaps here, and that's not something I have enough time to do.

If you'd like to proceed with the PR, I would suggest making a post on the Numba Discourse (https://numba.discourse.group) describing what you're trying to achieve with the PR, and requesting if there's anyone with sufficient time and experience to help shepherd the PR closer to completion and being ready for another round of review.

@gmarkall gmarkall added 4 - Waiting on author Waiting for author to respond to review PR Help needed Help is needed to complete a PR. and removed 4 - Waiting on reviewer Waiting for reviewer to respond to author labels Jan 10, 2024
@Kamakshi8104
Copy link
Author

Hey,
I want to proceed with the PR and will connect with someone to help me complete it. Thanks!

numba/np/arrayobj.py Outdated Show resolved Hide resolved
numba/tests/is_comparison_tests.py Outdated Show resolved Hide resolved
numba/np/arrayobj.py Outdated Show resolved Hide resolved
@Kamakshi8104
Copy link
Author

I have made the corrections as pointed out by @guilhermeleobas . Sorry for the delay 🙂

numba/tests/test_array_methods.py Outdated Show resolved Hide resolved
numba/tests/test_array_methods.py Outdated Show resolved Hide resolved
numba/tests/test_array_methods.py Outdated Show resolved Hide resolved
numba/np/arrayobj.py Show resolved Hide resolved
@Kamakshi8104
Copy link
Author

Kamakshi8104 commented Jan 24, 2024

Thanks for the help @guilhermeleobas .
I have made changes please let me know if there are any more problems.
I had to use from np.arrayobj import record_is in test_array_methods.py since all the checks were failing without it

Copy link
Contributor

@guilhermeleobas guilhermeleobas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contributions, @Kamakshi8104. Code still needs some work to be done.

numba/tests/test_array_methods.py Outdated Show resolved Hide resolved
numba/tests/test_array_methods.py Outdated Show resolved Hide resolved
numba/tests/test_array_methods.py Outdated Show resolved Hide resolved
numba/np/arrayobj.py Outdated Show resolved Hide resolved
@Kamakshi8104
Copy link
Author

Thanks @guilhermeleobas ! I have tried a different approach for record_is_impl function. I hope this works!! Please update me on any needed changes🙂

Copy link
Contributor

@guilhermeleobas guilhermeleobas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Kamakshi8104, I made a mistake while reviewing your PR. The code you had originally should work.

Thanks, @gmarkall, for pointing that out.

numba/np/arrayobj.py Outdated Show resolved Hide resolved
@Kamakshi8104
Copy link
Author

Kamakshi8104 commented Jan 26, 2024

@guilhermeleobas I have changed it back as requested. Please take a look....let me know If anything else is required

Copy link
Contributor

@guilhermeleobas guilhermeleobas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the effort @Kamakshi8104. Have a few more comments, but we're getting there.

@@ -11,6 +11,7 @@
from numba.core.errors import TypingError, NumbaValueError
from numba.np.numpy_support import as_dtype, numpy_version
from numba.tests.support import TestCase, MemoryLeakMixin, needs_blas
#from np.arrayobj import record_is
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete this import as it is not needed

Comment on lines 1783 to 1806
def test_record_is(self):
def check(a, b, expected):
pyfunc = identity_usecase
cfunc=njit(pyfunc)


result = cfunc(a, b)
self.assertEqual(result, expected)

record_type = np.dtype([("a", "i4"), ("b", "i4")])

# Test case: Same records
a = np.zeros(1, dtype=record_type)[0]
b = a.copy()
check(a, b, True)

# Test case: Different records
c = np.ones(1, dtype=record_type)[0]
check(a, c, False)

# Test case: Same records with different values
d = np.zeros(1, dtype=record_type)[0]
d['a'] = 42
check(a, d, False)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than give the expected result as an argument, compute it by calling pyfunc(a, b). This should fix the CI issue.

Suggested change
def test_record_is(self):
def check(a, b, expected):
pyfunc = identity_usecase
cfunc=njit(pyfunc)
result = cfunc(a, b)
self.assertEqual(result, expected)
record_type = np.dtype([("a", "i4"), ("b", "i4")])
# Test case: Same records
a = np.zeros(1, dtype=record_type)[0]
b = a.copy()
check(a, b, True)
# Test case: Different records
c = np.ones(1, dtype=record_type)[0]
check(a, c, False)
# Test case: Same records with different values
d = np.zeros(1, dtype=record_type)[0]
d['a'] = 42
check(a, d, False)
def test_record_is(self):
def check(a, b):
pyfunc = identity_usecase
cfunc = njit(pyfunc)
result = cfunc(a, b)
expected = pyfunc(a, b)
self.assertEqual(result, expected)
record_type = np.dtype([("a", "i4"), ("b", "i4")])
# Test case: Same records
a = np.zeros(1, dtype=record_type)[0]
b = a.copy()
check(a, b)
# Test case: Different records
c = np.ones(1, dtype=record_type)[0]
check(a, c)
# Test case: Same records with different values
d = np.zeros(1, dtype=record_type)[0]
d['a'] = 42
check(a, d)

Comment on lines 1807 to 1811



# tests for record comparison

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you plan to add any other tests? If not, remove the empty lines.

@Kamakshi8104
Copy link
Author

Thank you @guilhermeleobas and @gmarkall for all your help!! All the tests have successfully passed. I hope this fixes the issue🙂

@guilhermeleobas guilhermeleobas added 4 - Waiting on reviewer Waiting for reviewer to respond to author and removed 4 - Waiting on author Waiting for author to respond to review labels Feb 9, 2024
Copy link
Contributor

@guilhermeleobas guilhermeleobas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, you need to also write a release notes document. Please, see:
https://github.com/numba/numba/blob/main/docs/upcoming_changes/README.rst

@Kamakshi8104
Copy link
Author

@gmarkall and @guilhermeleobas I have made all changes as well as added documentation. Please let me know if anything else I required further since the PR hasn't been merged yet. 🙂

Co-authored-by: Guilherme Leobas <guilhermeleobas@gmail.com>
Co-authored-by: Guilherme Leobas <guilhermeleobas@gmail.com>
@guilhermeleobas guilhermeleobas added 5 - Ready to merge Review and testing done, is ready to merge and removed 4 - Waiting on reviewer Waiting for reviewer to respond to author labels Apr 29, 2024
Copy link
Contributor

@guilhermeleobas guilhermeleobas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If @gmarkall does not have any other comments, we can merge this one for 0.61

@guilhermeleobas guilhermeleobas added 4 - Waiting on reviewer Waiting for reviewer to respond to author and removed 5 - Ready to merge Review and testing done, is ready to merge labels Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4 - Waiting on reviewer Waiting for reviewer to respond to author PR Help needed Help is needed to complete a PR.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

numpy records do not support "is" identity comparison.
3 participants