Numba not recognizing numpy.random.Generator obj

Manual says Generator objects and their methods are supported since v0.56. I’m using numba 0.56.4 and numpy 1.23.5

ideas?

numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Failed in nopython mode pipeline (step: nopython frontend)
Unknown attribute 'uniform' of type NumPyRandomGeneratorType

Can you post a python code sample of what you’re trying to do?

Sure. Below is the module-level function that I want to give the decorator. All arguments are themselves class attributes (numpy.ndarray type) at higher levels, if that matters; they are explicitly provided individually as args:

# @nb.njit(parallel=False, cache=False, error_model='numpy', fastmath=True)
def _sim_reset(
        _RG,
        _true_duty_cycle,
        _r_mhz100_noise_phase,
        _r_khz500_noise_phase,
        _ro_period_expansion_factor,
        sim_length,
        _r_dj_wave,
        _r_ro_core_rj_seed,
        _r_ro_transit_rj_seed,
        _r_signal_rj_seed,
        ro_core_rj_sigma_pct,
        ro_transit_rj_sigma,
        signal_rj_sigma,
        ro_period_selection,
    ):

    _true_duty_cycle[:] = _RG.uniform()
    _r_mhz100_noise_phase[:] = _RG.uniform(0, 6.283185307179586)
    _r_khz500_noise_phase[:] = _RG.uniform(0, 6.283185307179586)

    _r_dj_wave[:] = _RG.uniform(-1, 1, size=(sim_length,))
    _r_ro_core_rj_seed[:] = _RG.normal(0, ro_core_rj_sigma_pct, size=(sim_length,))
    _r_ro_transit_rj_seed[:] = _RG.normal(0, ro_transit_rj_sigma, size=(sim_length,))
    _r_signal_rj_seed[:] = _RG.normal(0, signal_rj_sigma, size=(sim_length,))

    _ro_period_expansion_factor[:] = int(np.ceil(sim_length / ro_period_selection.shape[0]))

This works for me. If you can provide a complete minimal reproducer, that could be helpful.

import numpy as np
import numba

@numba.njit
def tester():
    return np.random.uniform(0.0, 1.0, size=12)

print(tester())

The issue is that as I understand it, I’m supposed to be able to pass a generator object, which you haven’t done in your code. See below, which fails when passing the Generator object:

import numpy as np
import numba

gengen =np.random.Generator(np.random.SFC64())
print(f'py context gengen: {gengen.uniform()}')

@numba.njit
def tester(gengen):
    return gengen.uniform(0.0, 1.0, size=12)
print(f'numba context gengen:{tester(gengen)}')

Thanks for the minimal reproducer, very helpful.

I poked at it a bit but didn’t figure out how to get that Generator class going.

hi, support for generator seems to expanding from a basic support in 0.56 to better support in 0.57, look at the list of PRs Pull requests · numba/numba · GitHub

your example is working for me using the latest main branch. I think this means that support for uniform is coming in 0.57

Luk

@luk-f-a 's comments/findings are correct.

Numba 0.56.x series contains support for passing np.random.Generator instances as arguments to numba.jit compiled functions, returning instances from the same, and support for the Generator().random() method (this is noted in the Numba documentation).

Numba 0.57.x series will contain support for a much larger range of methods found on Generator instances (almost all of them!), but will not have support for constructing Generator instances and will still not be thread-safe (both require a lot more work).

Sounds like I overlooked the the part explaining that only the .random() method of Generator objs was supported in 0.56…

Apologies!

However, I do appreciate the community’s activity and willingness to engage, even if the oversight was mine :slight_smile:

Best,

@bjmi no problem! An 0.57 release candidate will be out sometime soon, an announcement will be posted to this forum when it arrives, feedback RE the Generators (and anything else) is welcomed.