willow-ahrens / finch-tensor

Sparse Tensors in Python and more! Datastructure-Driven Array Programming Language
MIT License
8 stars 3 forks source link

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

Open hameerabbasi opened 3 months ago

hameerabbasi commented 3 months ago
          This is weird, all the others here are NumPy dtypes.

_Originally posted by @hameerabbasi in https://github.com/willow-ahrens/finch-tensor/pull/48#discussion_r1603510065_

willow-ahrens commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

Can we report this upstream (perhaps to JuliaCall)?

willow-ahrens commented 3 months ago

yeah, maybe we should report it.

willow-ahrens commented 3 months ago

@hameerabbasi @mtsokol They fixed it!