Skip to content

Conversation

@fduguet-nv
Copy link
Contributor

@fduguet-nv fduguet-nv commented Aug 24, 2022

Implementation of:

  • cunumeric.random.permutation
  • cunumeric.random.shuffle
  • cunumeric.random.choice

@fduguet-nv fduguet-nv requested a review from magnatelee August 25, 2022 12:01
"""

if isinstance(a, int) and a <= 0:
raise ValueError
Copy link
Contributor

Choose a reason for hiding this comment

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

Numpy includes a specific message for the exception

Input In [3], in <cell line: 1>()
----> 1 np.random.choice(a, -1)

File mtrand.pyx:962, in numpy.random.mtrand.RandomState.choice()

File mtrand.pyx:748, in numpy.random.mtrand.RandomState.randint()

File _bounded_integers.pyx:1256, in numpy.random._bounded_integers._rand_int64()

ValueError: negative dimensions are not allowed

if isinstance(a, int) and a <= 0:
raise ValueError
if not isinstance(size, int):
raise ValueError
Copy link
Contributor

@bryevdv bryevdv Aug 30, 2022

Choose a reason for hiding this comment

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

Numpy lets a TypeError go through in this situation

In [4]: np.random.choice(a, 2, p=3.4)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [4], in <cell line: 1>()
----> 1 np.random.choice(a, 2, p=3.4)

File mtrand.pyx:918, in numpy.random.mtrand.RandomState.choice()

TypeError: object of type 'float' has no len()

we could explicitly raise our own TypeError here if it doesn't arise naturally in our codepaths, but we should match exception type with Numpy in any case.

Comment on lines +243 to +246
p : 1-D array-like :
The probabilities associated with each entry in ``a``.
If not given the sample assumes a uniform distribution over all
entries in ``a``.
Copy link
Contributor

Choose a reason for hiding this comment

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

optional params need "optional" and also to state the default values. c.f. numpy docstring:

https://numpy.org/doc/stable/reference/random/generated/numpy.random.choice.html

return cunumeric.random.randint(0, a, size)
else:
if a < size:
raise ValueError
Copy link
Contributor

Choose a reason for hiding this comment

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

Numpy has a descriptive error message

In [9]: np.random.choice(5, size=8, replace=False)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [9], in <cell line: 1>()
----> 1 np.random.choice(5, size=8, replace=False)

File mtrand.pyx:965, in numpy.random.mtrand.RandomState.choice()

ValueError: Cannot take a larger sample than population when 'replace=False'

return a[indices]
else:
if len(a) < size:
raise ValueError
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above:

ValueError: Cannot take a larger sample than population when 'replace=False'

# check if p is a probability distribution
if abs(cump[-1] - 1.0) > len(p) * np.finfo(p.dtype).eps:
# does not sum up to 1
raise ValueError
Copy link
Contributor

Choose a reason for hiding this comment

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

From Numpy:

ValueError: probabilities do not sum to 1

return cunumeric.random.permutation(a)[:size]
else:
if not replace:
raise ValueError
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
raise ValueError
raise ValueError("cunumeric.random.choice does not currently support passing 'p' when 'replace=False'")

@bryevdv
Copy link
Contributor

bryevdv commented Aug 30, 2022

@fduguet-nv test changes look good, I did poke into comparing the exception conditions in more detail, and I think they just need tweaks to match Numpy more closely

@marcinz marcinz changed the base branch from branch-22.10 to branch-23.03 January 26, 2023 01:00
@marcinz marcinz changed the base branch from branch-23.03 to branch-23.05 March 6, 2023 20:47
@marcinz marcinz changed the base branch from branch-23.05 to branch-23.07 May 18, 2023 20:30
@marcinz marcinz changed the base branch from branch-23.07 to branch-23.09 July 18, 2023 15:44
@marcinz marcinz changed the base branch from branch-23.09 to branch-23.11 September 26, 2023 00:38
@marcinz marcinz changed the base branch from branch-23.11 to branch-24.01 November 9, 2023 17:15
@marcinz marcinz changed the base branch from branch-24.01 to branch-24.03 February 22, 2024 01:08
manopapad pushed a commit that referenced this pull request Feb 18, 2025
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

Successfully merging this pull request may close these issues.

2 participants