Skip to content

Migrate random glue_lowering to overload where easy - #8061

Merged
sklam merged 9 commits into
numba:mainfrom
apmasell:unglue_easy
Aug 17, 2022
Merged

sklam merged 9 commits into
numba:mainfrom
apmasell:unglue_easy

Conversation

@apmasell

Copy link
Copy Markdown
Contributor

Convert all the glue_lowering to overload_method that is using
compile_internal to do basically exactly that.

@gmarkall

Copy link
Copy Markdown
Member

gpuci run tests

@apmasell

Copy link
Copy Markdown
Contributor Author

This is not ready for review.

Convert all the `glue_lowering` to `overload_method` that is using
`compile_internal` to do basically exactly that.
@apmasell apmasell changed the title Migrate glue_lowering to overload_method where easy Migrate random glue_lowering to overload where easy May 30, 2022
Comment thread numba/core/typing/randomdecl.py
Comment thread numba/cpython/randomimpl.py Outdated
@sklam

sklam commented May 31, 2022

Copy link
Copy Markdown
Member

/azp run

@azure-pipelines

Copy link
Copy Markdown
Contributor
Azure Pipelines successfully started running 1 pipeline(s).

Add missing functions for handling random distributions in arrays

@sklam sklam left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type checking is a bit too restrictive. See comment below.

Comment thread numba/cpython/randomimpl.py Outdated
Comment on lines +1156 to +1157
if (isinstance(ngood, types.Integer) and isinstance(nbad, types.Integer)
and isinstance(nsamples, types.Integer)):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is overly restrictive because numpy is casting ngood and nbad to integers. In the old-style, the compiler auto emits the cast. You can replicate that behavior by returning 2-tuple of (sig, impl). See example at https://github.com/numba/numba/blob/main/numba/typed/dictobject.py#L728.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

example:

import numpy as np
from numba import njit

args = [
(1, 2, 3),
(10., 30., 4),
]

@njit
def foo(good, bad, sample):
    return np.random.hypergeometric(good, bad, sample)



for good, bad, sample in args:
    print(foo(good, bad, sample))
    print(foo.py_func(good, bad, sample))

@sklam sklam added 4 - Waiting on author Waiting for author to respond to review and removed 3 - Ready for Review labels Jun 1, 2022
Allow floating point types in hypergeometric distribution
@apmasell apmasell added 3 - Ready for Review and removed 4 - Waiting on author Waiting for author to respond to review labels Jun 2, 2022
@sklam sklam self-assigned this Jul 12, 2022
@sklam sklam added this to the Numba 0.57 RC milestone Jul 12, 2022

@sklam sklam left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For most of these three argument version, the first two argument typing doesn't match the two argument version (usually requires just types.Number). I also think you can let the two-arg version do the check by not checking in the three-arg version.

Comment thread numba/cpython/randomimpl.py
Comment thread numba/cpython/randomimpl.py
Comment thread numba/cpython/randomimpl.py
Comment thread numba/cpython/randomimpl.py
Comment thread numba/cpython/randomimpl.py
for idx in range(out.size):
out_flat[idx] = np.random.vonmises(mu, kappa)
return out
return _impl

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not exercised by tests

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still need a test

Comment thread numba/cpython/randomimpl.py
Add checks for all random distributions array creation. Remove type checks for
arguments in array creation functions that will be checked by the scalar
versions. Also fixes bugs in several distributions.
Comment thread numba/cpython/randomimpl.py Outdated
return out

elif isinstance(size, (types.UniTuple)) and isinstance(size.dtype, types.Integer):
elif isinstance(size, (types.UniTuple)) and isinstance(size.dtype,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
elif isinstance(size, (types.UniTuple)) and isinstance(size.dtype,
elif isinstance(size, types.UniTuple) and isinstance(size.dtype,

Comment thread numba/cpython/randomimpl.py Outdated
@overload(np.random.triangular)
def triangular_impl(low, high, mode, size):
if isinstance(size, types.NoneType):
return lambda low, high, mode, _size: np.random.triangular(low, high,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return lambda low, high, mode, _size: np.random.triangular(low, high,
return lambda low, high, mode, size: np.random.triangular(low, high,

@overload requires the parameters to match exactly for the returned impl and the decorated func; otherwise you get error like:

 Rejected as the implementation raised a specific error:
     InternalError: Typing and implementation arguments differ in argument names.
   Typing signature:         (low, high, mode, size)
   Implementation signature: (low, high, mode, _size)
   Difference: {<Parameter "_size">, <Parameter "size">}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the func(..., size=None) path is not exercised by the tests.

Comment thread numba/cpython/randomimpl.py Outdated
@overload(np.random.gamma)
def gamma_impl(alpha, beta, size):
if isinstance(size, types.NoneType):
return lambda alpha, beta, _size: np.random.gamma(alpha, beta)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return lambda alpha, beta, _size: np.random.gamma(alpha, beta)
return lambda alpha, beta, size: np.random.gamma(alpha, beta)

Comment thread numba/cpython/randomimpl.py Outdated
@overload(np.random.standard_gamma)
def standard_gamma_impl(alpha, size):
if isinstance(size, types.NoneType):
return lambda alpha, _size: np.random.standard_gamma(alpha)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return lambda alpha, _size: np.random.standard_gamma(alpha)
return lambda alpha, size: np.random.standard_gamma(alpha)

Comment thread numba/cpython/randomimpl.py Outdated
@overload(np.random.beta)
def beta_impl(alpha, beta, size):
if isinstance(size, types.NoneType):
return lambda alpha, beta, _size: np.random.beta(alpha, beta)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return lambda alpha, beta, _size: np.random.beta(alpha, beta)
return lambda alpha, beta, size: np.random.beta(alpha, beta)

Comment thread numba/cpython/randomimpl.py Outdated
@overload(np.random.rayleigh)
def rayleigh_impl2(mode, size):
if isinstance(size, types.NoneType):
return lambda mode, _size: np.random.rayleigh(mode)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return lambda mode, _size: np.random.rayleigh(mode)
return lambda mode, size: np.random.rayleigh(mode)

Comment thread numba/cpython/randomimpl.py Outdated
@overload(np.random.standard_cauchy)
def standard_cauchy_impl(size):
if isinstance(size, types.NoneType):
return lambda p, _size: np.random.standard_cauchy(p)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return lambda p, _size: np.random.standard_cauchy(p)
return lambda p, size: np.random.standard_cauchy(p)

Comment thread numba/cpython/randomimpl.py Outdated
@overload(np.random.standard_t)
def standard_t_impl2(df, size):
if isinstance(size, types.NoneType):
return lambda p, _size: np.random.standard_t(p)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return lambda p, _size: np.random.standard_t(p)
return lambda p, size: np.random.standard_t(p)

Comment thread numba/cpython/randomimpl.py Outdated
@overload(np.random.wald)
def wald_impl2(mean, scale, size):
if isinstance(size, types.NoneType):
return lambda mean, scale, _size: np.random.wald(mean, scale)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return lambda mean, scale, _size: np.random.wald(mean, scale)
return lambda mean, scale, size: np.random.wald(mean, scale)

Comment thread numba/cpython/randomimpl.py Outdated
@overload(np.random.zipf)
def zipf_impl(a, size):
if isinstance(size, types.NoneType):
return lambda a, _size: np.random.zipf(a)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return lambda a, _size: np.random.zipf(a)
return lambda a, size: np.random.zipf(a)

apmasell added 2 commits July 20, 2022 13:31
Correct parameters names to match main function's parameter names. Add checks
for `size=None` to get a scalar value from random distributions (except for
unmigrated ones).
The gamma distribution matches the CPython implementation, not the NumPy one,
so create a special test that compares them appropriately when generating an
array.
Make the logseries result be 64-bit rather than pointersized

@sklam sklam left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these impl need to be exercised

Comment thread numba/cpython/randomimpl.py
Comment thread numba/cpython/randomimpl.py
Comment thread numba/cpython/randomimpl.py
Comment thread numba/cpython/randomimpl.py
Comment thread numba/cpython/randomimpl.py
@sklam sklam added 4 - Waiting on author Waiting for author to respond to review and removed 3 - Ready for Review labels Jul 26, 2022
This adds test for the remaining array implementations and also a new harness
for ones that use the gamma distribution that won't match the Python NumPy
version.
@apmasell apmasell added 3 - Ready for Review 4 - Waiting on reviewer Waiting for reviewer to respond to author and removed 4 - Waiting on author Waiting for author to respond to review 4 - Waiting on reviewer Waiting for reviewer to respond to author labels Jul 29, 2022
Comment thread numba/cpython/randomimpl.py Outdated
np_prod = n * p
bound = min(n, np_prod + 10.0 * math.sqrt(np_prod * q + 1))

finished = False

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this variable is dead even in the old code

@sklam sklam left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two last comments:

@sklam sklam added the 4 - Waiting on author Waiting for author to respond to review label Aug 3, 2022
Include test for vonmises random array. This is another one where the CPython
algorithm is used instead of the NumPy one.
@apmasell apmasell removed the 4 - Waiting on author Waiting for author to respond to review label Aug 4, 2022

@sklam sklam left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the patch!

@sklam sklam added 5 - Ready to merge Review and testing done, is ready to merge and removed 3 - Ready for Review labels Aug 17, 2022
@sklam
sklam merged commit cf1d297 into numba:main Aug 17, 2022
@apmasell
apmasell deleted the unglue_easy branch August 26, 2022 18:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

5 - Ready to merge Review and testing done, is ready to merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants