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

[BUG] random.poisson doesn't accept arrays of lambda parameters #1152

Open
syamajala opened this issue Aug 29, 2024 · 1 comment
Open

[BUG] random.poisson doesn't accept arrays of lambda parameters #1152

syamajala opened this issue Aug 29, 2024 · 1 comment

Comments

@syamajala
Copy link

syamajala commented Aug 29, 2024

Software versions

cunumeric 24.06.00

Jupyter notebook / Jupyter Lab version

No response

Expected behavior

The example below works with numpy and cupy, but seems to crash with cunumeric.

Observed behavior

The code crashes. See below for a stack trace.

Example code or instructions

import cunumeric as np

def generate_gauss_2d(p, xi, yi):
    arg = -(np.square(xi - p[1]) + np.square(yi - p[2])) / (2 * p[3] * p[3])
    y = p[0] * np.exp(arg) + p[4]

    return y


if __name__ == '__main__':

    # number of fits and fit points
    number_fits = 10000
    size_x = 12
    number_points = size_x * size_x
    number_parameters = 5

    # set input arguments

    # true parameters
    true_parameters = np.array((10, 5.5, 5.5, 3, 10), dtype=np.float32)

    # initialize random number generator
    np.random.seed(0)

    # initial parameters (relative randomized, positions relative to width)
    initial_parameters = np.tile(true_parameters, (number_fits, 1))
    initial_parameters[:, (1, 2)] += true_parameters[3] * (-0.2 + 0.4 * np.random.rand(number_fits, 2))
    initial_parameters[:, (0, 3, 4)] *= 0.8 + 0.4 * np.random.rand(number_fits, 3)

    # generate x and y values
    g = np.arange(size_x)
    yi, xi = np.meshgrid(g, g, indexing='ij')
    xi = xi.astype(np.float32)
    yi = yi.astype(np.float32)

    # generate data
    data = generate_gauss_2d(true_parameters, xi, yi)
    data = np.reshape(data, (1, number_points))
    data = np.tile(data, (number_fits, 1))

    # add Poisson noise
    data = np.random.poisson(data)
    data = data.astype(np.float32)

Stack traceback or browser console output

/sdf/group/lcls/ds/tools/conda_envs/cunumeric-mec/lib/python3.12/site-packages/legion_top.py:302: RuntimeWarning: cuNu$
eric has not implemented meshgrid and is falling back to canonical NumPy. You may notice significantly decreased perfo$
mance for this function call.
  exec(code, module.__dict__, module.__dict__)
TypeError: only length-1 arrays can be converted to Python scalars

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/sdf/group/lcls/ds/tools/conda_envs/cunumeric-mec/lib/python3.12/site-packages/legion_top.py", line 482, in le$
ion_python_main
[0 - 7f8aa5be1000]   28.334343 {6}{python}: python exception occurred within task:
    run_path(args[start], '__main__')
  File "/sdf/group/lcls/ds/tools/conda_envs/cunumeric-mec/lib/python3.12/site-packages/legion_top.py", line 302, in ru$
_path
    exec(code, module.__dict__, module.__dict__)
  File "example.py", line 54, in <module>
    data = np.random.poisson(data)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "runtime.pyx", line 633, in legate.core._lib.runtime.runtime.track_provenance.decorator.wrapper
  File "runtime.pyx", line 634, in legate.core._lib.runtime.runtime.track_provenance.decorator.wrapper
  File "/sdf/group/lcls/ds/tools/conda_envs/cunumeric-mec/lib/python3.12/site-packages/cunumeric/_utils/coverage.py", $ine 111, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/sdf/group/lcls/ds/tools/conda_envs/cunumeric-mec/lib/python3.12/site-packages/cunumeric/random/_random.py", l$ne 956, in poisson
    return get_static_generator().poisson(lam, size)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/sdf/group/lcls/ds/tools/conda_envs/cunumeric-mec/lib/python3.12/site-packages/cunumeric/random/_generator.py"$ line 278, in poisson
    return self.bit_generator.poisson(lam, size)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/sdf/group/lcls/ds/tools/conda_envs/cunumeric-mec/lib/python3.12/site-packages/cunumeric/random/_bitgenerator.$y", line 187, in poisson
    res._thunk.bitgenerator_poisson(
  File "/sdf/group/lcls/ds/tools/conda_envs/cunumeric-mec/lib/python3.12/site-packages/cunumeric/_thunk/eager.py", lin$ 857, in bitgenerator_poisson
    self.array.fill(np.random.poisson(lam))
ValueError: setting an array element with a sequence.
@manopapad
Copy link
Contributor

I can reproduce the same issue with just

import numpy as np
import cunumeric as cn
data = cn.ones((3,4))
cn.random.poisson(data)

The underlying issue here is that cunumeric.random.poisson (and other similar random distribution APIs) only accepts a single "lambda" parameter, whereas numpy.random.poisson also accepts arrays of parameters.

@manopapad manopapad changed the title [BUG] TypeError: only length-1 arrays can be converted to Python scalars [BUG] random.poisson doesn't accept arrays of lambda parameters Aug 30, 2024
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