-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add experimental static key reuse checking
- Loading branch information
Showing
25 changed files
with
1,617 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
``jax.experimental.key_reuse`` module | ||
===================================== | ||
|
||
.. automodule:: jax.experimental.key_reuse | ||
|
||
API | ||
--- | ||
|
||
.. autosummary:: | ||
:toctree: _autosummary | ||
|
||
unconsumed_copy | ||
KeyReuseError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Copyright 2023 The JAX Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
""" | ||
Experimental Key Reuse Checking | ||
------------------------------- | ||
This module contains **experimental** functionality for detecting re-use of random | ||
keys within JAX programs. It is under active development and the APIs here are likely | ||
to change. | ||
Key reuse checking can be enabled on `jit`-compiled functions using the | ||
:func:`jax.enable_key_reuse_checks` configuration:: | ||
>>> import jax | ||
>>> @jax.jit | ||
... def f(key): | ||
... return jax.random.uniform(key) + jax.random.normal(key) | ||
... | ||
>>> key = jax.random.key(0) | ||
>>> with jax.enable_key_reuse_checks(): | ||
... f(key) # doctest: +IGNORE_EXCEPTION_DETAIL | ||
Traceback (most recent call last): | ||
... | ||
KeyReuseError: In random_bits, key values a are already consumed. | ||
This flag can also be set globally if you wish to enagle key reuse checks in | ||
every JIT-compiled function. | ||
""" | ||
|
||
from jax.experimental.key_reuse._common import ( | ||
unconsumed_copy as unconsumed_copy, | ||
KeyReuseError as KeyReuseError, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Copyright 2023 The JAX Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from typing import NamedTuple, Union | ||
from jax import core | ||
from jax.interpreters import batching, mlir | ||
import numpy as np | ||
|
||
|
||
class Sink(NamedTuple): | ||
idx: int | ||
mask: Union[bool, np.ndarray] = True | ||
|
||
|
||
class Source(NamedTuple): | ||
idx: int | ||
mask: Union[bool, np.ndarray] = True | ||
|
||
|
||
class KeyReuseSignature(NamedTuple): | ||
sinks: list[Sink] | ||
sources: list[Source] | ||
|
||
|
||
class KeyReuseError(RuntimeError): | ||
pass | ||
|
||
consume_p = core.Primitive("consume") | ||
consume_p.def_impl(lambda x: x) | ||
consume_p.def_abstract_eval(lambda x: x) | ||
batching.defvectorized(consume_p) | ||
mlir.register_lowering( | ||
consume_p, | ||
mlir.lower_fun(lambda x: x, multiple_results=False)) | ||
|
||
def consume(key): | ||
"""Consume the key and return a consumed copy.""" | ||
return consume_p.bind(key) | ||
|
||
unconsumed_copy_p = core.Primitive("unconsumed_copy") | ||
unconsumed_copy_p.def_impl(lambda x: x) | ||
unconsumed_copy_p.def_abstract_eval(lambda x: x) | ||
batching.defvectorized(unconsumed_copy_p) | ||
mlir.register_lowering( | ||
unconsumed_copy_p, | ||
mlir.lower_fun(lambda x: x, multiple_results=False)) | ||
|
||
def unconsumed_copy(key): | ||
"""Return a copy of key marked as unconsumed.""" | ||
return unconsumed_copy_p.bind(key) | ||
|
||
assert_consumed_value_p = core.Primitive("assert_consumed_value") | ||
assert_consumed_value_p.def_impl(lambda x, *, value: x) | ||
assert_consumed_value_p.def_abstract_eval(lambda x, *, value: x) | ||
batching.defvectorized(assert_consumed_value_p) | ||
mlir.register_lowering( | ||
assert_consumed_value_p, | ||
mlir.lower_fun(lambda x, *, value: x, multiple_results=False)) | ||
|
||
def assert_unconsumed(key): | ||
"""Assert that a key is unconsumed""" | ||
assert_consumed_value_p.bind(key, value=False) | ||
|
||
def assert_consumed(key, value=True): | ||
"""Assert that a key is consumed""" | ||
assert_consumed_value_p.bind(key, value=value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Copyright 2023 The JAX Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from typing import Any, Callable, Optional, Union | ||
|
||
from jax import core | ||
from jax.experimental.key_reuse import _forwarding | ||
from jax.experimental.key_reuse import _simple | ||
import numpy as np | ||
|
||
# TODO(jakevdp) fix this | ||
KeyReuseSignature = Any | ||
|
||
|
||
def check_key_reuse(fun: Callable[..., Any], /, *args: Any, | ||
use_forwarding: bool = True) -> KeyReuseSignature: | ||
"""Function to statically check key reuse.""" | ||
if use_forwarding: | ||
return _forwarding.check_key_reuse(fun, *args) | ||
else: | ||
return _simple.check_key_reuse(fun, *args) | ||
|
||
|
||
def check_key_reuse_jaxpr(jaxpr: core.Jaxpr, *, use_forwarding: bool = True): | ||
"""Check the jaxpr for key reuse.""" | ||
get_jaxpr_type_signature(jaxpr, use_forwarding=use_forwarding) | ||
|
||
|
||
def get_jaxpr_type_signature( | ||
jaxpr: core.Jaxpr, *, | ||
consumed_inputs: Optional[list[Union[bool, np.ndarray]]] = None, | ||
use_forwarding: bool = True, | ||
) -> KeyReuseSignature: | ||
"""Parse the jaxpr to determine key reuse signature""" | ||
if use_forwarding: | ||
return _forwarding.get_jaxpr_type_signature(jaxpr, consumed_inputs) | ||
else: | ||
return _simple.get_jaxpr_type_signature(jaxpr, consumed_inputs) |
Oops, something went wrong.