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

Conversion from np.bool_ does not produce julia Bool. #54

Open
hameerabbasi opened this issue May 17, 2024 · 7 comments
Open

Conversion from np.bool_ does not produce julia Bool. #54

hameerabbasi opened this issue May 17, 2024 · 7 comments

Comments

@hameerabbasi
Copy link
Collaborator

          This is weird, all the others here are NumPy dtypes.

Originally posted by @hameerabbasi in #48 (comment)

@willow-ahrens
Copy link
Collaborator

willow-ahrens commented May 17, 2024

The same change already happens in tensor.py::638:

if dtype == np.bool_:  # Fails with: Finch currently only supports isbits defaults
    dtype = bool

np.bool_ isn't accepted by Finch, only builtin bool is fine.

Bool is certainly supported by Finch, it's fairly easy to confirm. If you're getting an error like Finch currently only supports isbits defaults, that means that you are trying to construct a finch tensor full of heap-allocated objects. This points to a conversion from np.bool_ into something Julia or Finch isn't recognizing as a julia Bool.

@willow-ahrens
Copy link
Collaborator

One observation is that julia does seem to recognize a numpy array of bools.

>>> jl.println(jl.typeof(numpy.full((1,), numpy.True_)))
PyArray{Bool, 1, true, true, Bool}

@willow-ahrens
Copy link
Collaborator

some further investigation:

>>> jl.println(builtins.bool)
<class 'bool'>
>>> jl.println(numpy.bool_)
<class 'numpy.bool_'>
>>> jl.println(builtins.int)
<class 'int'>
>>> jl.println(numpy.int_)
<class 'numpy.int64'>

I don't spot the difference. I just think this is probably an artifact of PythonCall or a discrepancy between Python and Julia handling of Bool. Somewhere, the wrapper layer might not be converting a bool properly, or might not be able to.

@willow-ahrens willow-ahrens changed the title Finch.jl accepts all dtypes except bool. Conversion from np.bool_ does not produce julia Bool. May 17, 2024
@willow-ahrens
Copy link
Collaborator

Ah, here it is:

>>> jl.println(np.int_(1))
1
>>> jl.println(int(1))
1
>>> jl.println(np.True_)
fill(true)
>>> jl.println(True)
true

I am not sure why, but np.True_ converts to a scalar julia array containing true, but True converts to a julia Boolean true. This appears to be the only case where this occurs.

>>> jl.println(jl.typeof(np.True_))
PyArray{Bool, 0, true, true, Bool}

I don't know why this conversion occurs, but it's related to the python/julia wrapper. Finch is just complaining because it doesn't like heap-allocated objects.

@hameerabbasi
Copy link
Collaborator Author

Can we report this upstream (perhaps to JuliaCall)?

@willow-ahrens
Copy link
Collaborator

yeah, maybe we should report it.

@willow-ahrens
Copy link
Collaborator

@hameerabbasi @mtsokol They fixed it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants