169 Commits

Author SHA1 Message Date
Roy Frostig
98d245ebb4 add a config setting to control the default PRNG implementation
Also add explicit seeding functions for each PRNG implementation.
2021-10-07 21:22:40 -07:00
jax authors
47b40e001d Merge pull request #7847 from google:prng-type
PiperOrigin-RevId: 396661434
2021-09-14 12:55:28 -07:00
Jake VanderPlas
245581411e Add PEP484-compatible export for jax and its subpackages 2021-09-13 14:08:48 -07:00
Roy Frostig
9134794ddf expose random key array type
This is meant to allow for downstream type annotations and checking
(e.g. via `isinstance`).
2021-09-07 16:30:13 -07:00
Roy Frostig
4eb437a568 alias prng.threefry_2x32 in random and warn of move
Some call this, apparently.
2021-08-19 20:43:11 -07:00
Roy Frostig
aa265cce95 introduce custom PRNG implementations and an array-like adapter for them
A PRNG implementation is determined by a key shape and a set of basic
functions on such a key: seed, split, random_bits, and fold_in.

A PRNG implementation can then by lifted to an array-of-keys-like
object. Namely, a new internal pytree class PRNGKeyArray wraps the
implementation and maintains an array of keys of the right shape. This
array-like object is the new "key" that gets passed around the various
functions in the public random API (e.g. `random.uniform`,
`random.normal`, ...). So the PRNGKeyArray class really serves two
purposes at once:

1. To adapt key implementations into "arrays" of such keys.
2. To carry a reference to the PRNG implementation around and delegate
   back to it from the functions in random.
2021-08-19 20:43:11 -07:00
Roy Frostig
b4ccecca88 factor PRNG routines from random module to prng 2021-08-17 19:27:31 -07:00
George Necula
6a48c60a72 Rename master to main in embedded links.
Tried to avoid the change on external links to repos that
have not yet renamed master.
2021-06-18 10:00:01 +03:00
Jake VanderPlas
8e789c7380 Run doctest on all source files except jax2tf 2021-04-05 10:39:59 -07:00
Jake VanderPlas
4ae393f8a8 DOC: fix jax.random documentation 2020-11-20 16:41:32 -08:00
Peter Hawkins
77827538b4 [JAX] Move contents of jax.random into jax._src.random.
PiperOrigin-RevId: 343305017
2020-11-19 09:22:59 -08:00
Cloud Han
a6acce58e0 Build on Windows
1. Build on Windows

2. Fix OverflowError

    When calling `key = random.PRNGKey(0)` OverflowError: Python int too
    large to convert to C long for casting value 4294967295 (0xFFFFFFFF)
    from python int to int32.

3. fix file path in regex of errors_test

4. handle ValueError of os.path.commonpath
2020-11-19 23:33:06 +08:00
Jake VanderPlas
18228f45cb PRNGSeed: ensure JIT invariance for valid inputs. 2020-11-12 09:06:24 -08:00
Matthew Johnson
9ba28d2634 Copybara import of the project:
--
ced333d1d4aec2825e9afd81c2ca9721b7e3cc67 by Matthew Johnson <mattjj@google.com>:

redo #4535 lazy simplification

PiperOrigin-RevId: 338670328
2020-10-23 07:35:01 -07:00
Matthew Johnson
fcaced32aa Copybara import of the project:
--
ced333d1d4aec2825e9afd81c2ca9721b7e3cc67 by Matthew Johnson <mattjj@google.com>:

redo #4535 lazy simplification

PiperOrigin-RevId: 338606348
2020-10-22 21:18:22 -07:00
Matthew Johnson
ced333d1d4 redo #4535 lazy simplification 2020-10-22 16:56:29 -07:00
Peter Hawkins
aa107cf1f4 Move jax.numpy internals into jax._src.numpy. 2020-10-16 20:35:19 -04:00
Matthew Johnson
f553ed24e1 Temporary rollback of #4535 pending a possible XLA bug it exposed in an internal test.
PiperOrigin-RevId: 337219426
2020-10-14 18:52:13 -07:00
jax authors
fb01f59020 Merge pull request #4535 from google:lazy-simplification
PiperOrigin-RevId: 337183224
2020-10-14 15:16:50 -07:00
Matthew Johnson
990dc57deb Merge remote-tracking branch 'origin/master' into lazy-simplification 2020-10-14 14:52:16 -07:00
Peter Hawkins
1d4c53cef9 Fix CUDA launch error when generating an empty PRNG array. 2020-10-14 14:33:56 -04:00
Peter Hawkins
0b8eb92d59 Add stop_gradients around lax.nextafter to fix TFP gradient errors for jax.random.truncated_normal. 2020-10-13 09:16:29 -04:00
Peter Hawkins
080007ab82 Ensure values returned by jax.random.truncated_normal() are in range.
A user observed -inf values being returned by truncated_normal(), which occur if the uniform random value passed to erfinv() is out of range, e.g., due to rounding. Do more of the computation using jax.random.uniform(), which promises correct behavior in the face of rounding.

As an added security measure, also clamp the outputs of the function to the open interval.
2020-10-12 16:27:54 -04:00
Matthew Johnson
4e65a6f0a9 don't generate lazy iota/eye/tri/delta omnistaging 2020-10-10 21:08:52 -07:00
Matthew Johnson
c42d736e34 remove limit on size of random arrays 2020-09-23 19:37:34 -07:00
George Necula
1e84cbe9cc
[jax2tf] Fix random.split when jax_exable_x64 (#4208)
Since we do the threefry with signed integers when converting to TF,
we run into the type promotion 'uint32 - int32 = int64', which
then results in lax.shift_right_logical(uint32, int64), which fails.
2020-09-07 14:41:50 +03:00
George Necula
5eac47726b
[jax2tf] Implementation of random_gamma (#4192)
* [jax2tf] implementation of random_gamma

The simplest implementation is by converting the JAX own impl_rule,
which rewrites gamma into other JAX primitives.

On TPU with use_vmap=True the performance is the same for JAX and TF, provided
we use tf.function(compile=True).
2020-09-03 14:18:35 +03:00
George Necula
417c9ff764
Fix pytype error (#4158) 2020-08-27 09:41:16 +03:00
Matthew Johnson
1d93991003
allow random.choice to accept ndarray input (#4145)
* allow random.choice to accept ndarray `a`

follow-up to #4137 to allow ndarray inputs to be passed

* add jax.random.choice tests to cover ndarray input

* don't use callables in test params

it can mess with pytest-xdist because of hashing by id
2020-08-26 10:21:56 -07:00
Jake Vanderplas
6d54eb563e
Do not call asarray() on inputs of jax.random.choice (#4137) 2020-08-25 05:47:43 -07:00
Matthew Johnson
56b3688db9 make random.choice error when shape isn't sequence
fixes #4124
2020-08-21 19:58:06 -07:00
Mihaela Rosca
1e8ac24863
Add rademacher, maxwell, double_sided_maxwell and weibull_min to jax.random. (#4104) 2020-08-20 07:46:55 -07:00
Jake Vanderplas
29aa9bfc8f
Cleanup: avoid jnp.prod & np.prod on array shapes (#4086) 2020-08-18 10:17:38 -07:00
Ethan Luo Yicheng
6e4ec7cb81
Fix broadcasting in random.uniform and randint. (#4035) 2020-08-12 11:52:42 -07:00
Scott Linderman
ea88c55f55
Fixes and tests for jax.random.multivariate_normal (#4002)
* Fix bug #3997, change `jax.random.multivariate_normal` to handle batches of covariance matrices.  It works as long as mean and covariance are broadcast-compatible, as specified in the docstring.

* Fix bug in multivariate_normal shape checking

Minor bug: should be checking for compatibility of `shape`, `mean`, and the the last two dimensions of the _covariance_ matrix.

* Add test for multivariate_normal shapes

This test checks that `jax.random.multivariate_normal` produces the expected output shape for various combinations of event dimension and `mean`, `covariance`, and `shape` shapes.

* Fix linter issues in tests/random_test.py

Trimming trialing whitespace and 80 char limit.

* Really trimming whitespace in tests/random_test.py

Arg. Have to fix my editor to do this automatically.
2020-08-09 11:32:45 -07:00
John Aslanides
038c85dad0
Improve type annotations for jit and vmap. (#3938) 2020-08-08 12:22:54 -04:00
Matthew Johnson
4236eb2b59
omnistaging, under a flag and disabled by default (#3370)
This change, when enabled, stages out all primitive calls in the dynamic
scope of a jitted, pmapped, or control flow function, rather than only
staging out based on data dependence. One improvement is that jitted
functions can consume less memory, by avoiding instantiating large
constants at trace time, and cause less memory fragmentation as well. It
also simplifies several internals.

See https://github.com/google/jax/pull/3370 fo more information.
2020-07-30 12:59:36 -07:00
Jake Vanderplas
ee7f035349
jax.random: use correct x32/x64 default dtypes. (#3841)
This is a no-op in the current package, but will make things cleaner during the x64 deprecation.
2020-07-26 08:58:37 -07:00
bion howard
74d363e552
fix extremely minor typo (#3815)
"ijnputs" -> "inputs"
2020-07-21 12:41:08 -07:00
Matthew Johnson
74ee2ef6eb
avoid value-based error check in random.choice (#3531) 2020-06-23 14:03:36 -07:00
Matthew Johnson
2f7108f78b
remove the lower_fun default multiple_results=True (#3524) 2020-06-22 17:50:33 -07:00
Jake Vanderplas
19f308b9ed
implement jax.random.choice (#3463) 2020-06-19 16:04:42 -07:00
Srinivas Vasudevan
927c209148
Add random_gamma_grad and use in jax.random.gamma (#3281) 2020-06-19 09:34:18 -04:00
fehiepsi
b680c994ae allow scalar input in poisson sampler 2020-06-12 01:42:25 -04:00
Adam Paszke
e36c72b983
Make ad_util.zero a class that carries avals (similar to UndefinedPrimal) (#3222) 2020-06-08 17:50:14 +02:00
Jake Vanderplas
2a10dbbf37
deflake remainder of jax (#3343) 2020-06-06 10:51:34 -07:00
Adam Paszke
adb442eb8a Make ad_util.zero a class that carries avals (similar to UndefinedPrimal)
This is useful for remat transpose rule submitted in #3162 and e.g.
allowed me to catch a slight overuse of defjvp2 for `random_gamma_p` (it
was unnecessarily declared as having multiple outputs).
2020-06-05 15:51:30 +00:00
Matthew Johnson
9c0a58a8e7
add float dtype checks to random.py (#3320)
fixes #3317
2020-06-04 10:13:15 -07:00
Matthew Johnson
c42a7f7890
remove some trailing whitespace (#3287) 2020-06-02 17:37:20 -07:00
Roy Frostig
e7e4cbce5d docstring fix 2020-05-29 16:00:20 -07:00