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

The np.size() Function Fails #9464

Closed
RoyiAvital opened this issue Feb 24, 2024 · 3 comments · Fixed by #9504
Closed

The np.size() Function Fails #9464

RoyiAvital opened this issue Feb 24, 2024 · 3 comments · Fixed by #9504
Labels
easy feature_request good first issue A good issue for a first time contributor

Comments

@RoyiAvital
Copy link

The Bug

It seems Numba support the ndarray property of an array (See numpy.ndarray.size) yet fails with the function np.size().

Code for Producing the Bug

The following works:

import numpy as np
from numba import njit

@njit
def SumArrayWorks( vX : np.ndarray ) -> float:
  arrSum = 0.0
  for ii in range(vX.size):
    arrSum += vX[ii]

  return arrSum

vX = np.array([1.0, 2.0, 3.0, 4.0])
SumArrayWorks(vX) #<! Works!

While this will fail:

import numpy as np
from numba import njit

@njit
def SumArrayFails( vX : np.ndarray ) -> float:
  arrSum = 0.0
  for ii in range(np.size(vX)):
    arrSum += vX[ii]

  return arrSum

vX = np.array([1.0, 2.0, 3.0, 4.0])
SumArrayFails(vX) #<! Fails!

The error is:

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Use of unsupported NumPy function 'numpy.size' or unsupported use of the function.

System Information

  1. Numba: 0.59.0.
  2. Python: 3.12.
  3. NumPy: 1.26.4.
  4. OS: Windows 11 Pro 23H2.
@sklam
Copy link
Member

sklam commented Feb 26, 2024

np.size() is so rarely used that we completely missed it for all these years.

@sklam sklam added easy good first issue A good issue for a first time contributor labels Feb 26, 2024
@SDanielDev
Copy link

Hi, would a simple solution be to just call np.size() outside of the njit function? I am new to this, but here is my attempt to use np.size() over .size:

import numpy as np
from numba import njit

@njit
def SumArrayWorks_NpSize( vX : np.ndarray ) -> float:
  arrSum = 0.0
  for ii in range(vX_size):
    arrSum += vX[ii]

  return arrSum

vX = np.array([1.0, 2.0, 3.0, 4.0])
vX_size = np.size(vX)

SumArrayWorks_NpSize(vX) #<! Works!```

@RoyiAvital
Copy link
Author

@SDanielDev , The concept here is about having a native Numba support, not bypassing it.
Specifically, it is better to avoid using global variables. A simpler and better solution would be using vX.size instead of np.size() which works as shown above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
easy feature_request good first issue A good issue for a first time contributor
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants