mirror of
https://github.com/wassname/jaxtyping.git
synced 2026-09-10 12:14:04 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d246e21281 | ||
|
|
165065756f | ||
|
|
dcd73e3431 | ||
|
|
f175c7f315 | ||
|
|
da8300ec6c | ||
|
|
39439c2790 | ||
|
|
6202dcc639 |
+2
-2
@@ -1,2 +1,2 @@
|
|||||||
include LICENSE
|
include jaxtyping/py.typed
|
||||||
prune tests
|
prune test
|
||||||
|
|||||||
+16
-2
@@ -17,7 +17,21 @@
|
|||||||
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
from jax.numpy import ndarray as Array
|
import typing
|
||||||
|
|
||||||
|
|
||||||
|
if typing.TYPE_CHECKING:
|
||||||
|
# type checkers don't know which branch below will be executed
|
||||||
|
from jax.numpy import ndarray as Array
|
||||||
|
else:
|
||||||
|
if getattr(typing, "GENERATING_DOCUMENTATION", False):
|
||||||
|
|
||||||
|
class Array:
|
||||||
|
pass
|
||||||
|
|
||||||
|
Array.__module__ = "builtins"
|
||||||
|
else:
|
||||||
|
from jax.numpy import ndarray as Array
|
||||||
|
|
||||||
from .array_types import (
|
from .array_types import (
|
||||||
AbstractArray,
|
AbstractArray,
|
||||||
@@ -53,4 +67,4 @@ from .import_hook import install_import_hook
|
|||||||
from .pytree_type import PyTree
|
from .pytree_type import PyTree
|
||||||
|
|
||||||
|
|
||||||
__version__ = "0.2.2"
|
__version__ = "0.2.7"
|
||||||
|
|||||||
+26
-14
@@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
import enum
|
import enum
|
||||||
import functools as ft
|
import functools as ft
|
||||||
|
import typing
|
||||||
from typing import Any, Dict, List, NoReturn, Optional, Tuple, TYPE_CHECKING, Union
|
from typing import Any, Dict, List, NoReturn, Optional, Tuple, TYPE_CHECKING, Union
|
||||||
from typing_extensions import Literal
|
from typing_extensions import Literal
|
||||||
|
|
||||||
@@ -149,25 +150,25 @@ class _MetaAbstractArray(type):
|
|||||||
if cls.dtypes is not _any_dtype and dtype not in cls.dtypes:
|
if cls.dtypes is not _any_dtype and dtype not in cls.dtypes:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if len(storage.memo_stack) == 0:
|
no_temp_memo = hasattr(storage, "memo_stack") and len(storage.memo_stack) != 0
|
||||||
|
|
||||||
|
if no_temp_memo:
|
||||||
|
single_memo, variadic_memo, variadic_broadcast_memo = storage.memo_stack[-1]
|
||||||
|
# Make a copy so we don't mutate the original memo during the shape check.
|
||||||
|
single_memo = single_memo.copy()
|
||||||
|
variadic_memo = variadic_memo.copy()
|
||||||
|
variadic_broadcast_memo = variadic_broadcast_memo.copy()
|
||||||
|
else:
|
||||||
# `isinstance` happening outside any @jaxtyped decorators, e.g. at the
|
# `isinstance` happening outside any @jaxtyped decorators, e.g. at the
|
||||||
# global scope. In this case just create a temporary memo, since we're not
|
# global scope. In this case just create a temporary memo, since we're not
|
||||||
# going to be comparing against any stored values anyway.
|
# going to be comparing against any stored values anyway.
|
||||||
single_memo = {}
|
single_memo = {}
|
||||||
variadic_memo = {}
|
variadic_memo = {}
|
||||||
variadic_broadcast_memo = {}
|
variadic_broadcast_memo = {}
|
||||||
temp_memo = True
|
|
||||||
else:
|
|
||||||
single_memo, variadic_memo, variadic_broadcast_memo = storage.memo_stack[-1]
|
|
||||||
# Make a copy so we don't mutate the original memo during the shape check.
|
|
||||||
single_memo = single_memo.copy()
|
|
||||||
variadic_memo = variadic_memo.copy()
|
|
||||||
variadic_broadcast_memo = variadic_broadcast_memo.copy()
|
|
||||||
temp_memo = False
|
|
||||||
|
|
||||||
if cls._check_shape(obj, single_memo, variadic_memo, variadic_broadcast_memo):
|
if cls._check_shape(obj, single_memo, variadic_memo, variadic_broadcast_memo):
|
||||||
# We update the memo every time we successfully pass a shape check
|
# We update the memo every time we successfully pass a shape check
|
||||||
if not temp_memo:
|
if no_temp_memo:
|
||||||
storage.memo_stack[-1] = (
|
storage.memo_stack[-1] = (
|
||||||
single_memo,
|
single_memo,
|
||||||
variadic_memo,
|
variadic_memo,
|
||||||
@@ -383,9 +384,14 @@ class _MetaAbstractDtype(type):
|
|||||||
elem = _SymbolicDim(elem, broadcastable)
|
elem = _SymbolicDim(elem, broadcastable)
|
||||||
dims.append(elem)
|
dims.append(elem)
|
||||||
if _array_name_format == "dtype_and_shape":
|
if _array_name_format == "dtype_and_shape":
|
||||||
name = f"{cls.__name__}[{array_type.__name__}, '{dim_str}']"
|
# In python 3.8, e.g., typing.Union lacks `__name__`.
|
||||||
|
try:
|
||||||
|
type_str = array_type.__name__
|
||||||
|
except AttributeError:
|
||||||
|
type_str = repr(array_type)
|
||||||
|
name = f"{cls.__name__}[{type_str}, '{dim_str}']"
|
||||||
elif _array_name_format == "array":
|
elif _array_name_format == "array":
|
||||||
name = "Array"
|
name = array_type.__name__
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"array_name_format {_array_name_format} not recognised")
|
raise ValueError(f"array_name_format {_array_name_format} not recognised")
|
||||||
out = _MetaAbstractArray(
|
out = _MetaAbstractArray(
|
||||||
@@ -398,7 +404,10 @@ class _MetaAbstractDtype(type):
|
|||||||
index_variadic=index_variadic,
|
index_variadic=index_variadic,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
out.__module__ = "jaxtyping"
|
if getattr(typing, "GENERATING_DOCUMENTATION", False):
|
||||||
|
out.__module__ = "builtins"
|
||||||
|
else:
|
||||||
|
out.__module__ = "jaxtyping"
|
||||||
return out
|
return out
|
||||||
|
|
||||||
|
|
||||||
@@ -470,7 +479,10 @@ else:
|
|||||||
|
|
||||||
_Cls.__name__ = name
|
_Cls.__name__ = name
|
||||||
_Cls.__qualname__ = name
|
_Cls.__qualname__ = name
|
||||||
_Cls.__module__ = "jaxtyping"
|
if getattr(typing, "GENERATING_DOCUMENTATION", False):
|
||||||
|
_Cls.__module__ = "builtins"
|
||||||
|
else:
|
||||||
|
_Cls.__module__ = "jaxtyping"
|
||||||
return _Cls
|
return _Cls
|
||||||
|
|
||||||
UInt8 = _make_dtype(_uint8, "UInt8")
|
UInt8 = _make_dtype(_uint8, "UInt8")
|
||||||
|
|||||||
@@ -65,7 +65,9 @@ def _call_with_frames_removed(f, *args, **kwargs):
|
|||||||
|
|
||||||
|
|
||||||
def _optimized_cache_from_source(path, debug_override=None):
|
def _optimized_cache_from_source(path, debug_override=None):
|
||||||
return cache_from_source(path, debug_override, optimization="jaxtyping")
|
# Version 2: change the position of the `@jaxtyped` decorator, so need a
|
||||||
|
# different name to avoid hitting old __pycache__
|
||||||
|
return cache_from_source(path, debug_override, optimization="jaxtyping2")
|
||||||
|
|
||||||
|
|
||||||
class _JaxtypingTransformer(ast.NodeVisitor):
|
class _JaxtypingTransformer(ast.NodeVisitor):
|
||||||
@@ -99,10 +101,16 @@ class _JaxtypingTransformer(ast.NodeVisitor):
|
|||||||
has_annotated_args = any(arg for arg in node.args.args if arg.annotation)
|
has_annotated_args = any(arg for arg in node.args.args if arg.annotation)
|
||||||
has_annotated_return = bool(node.returns)
|
has_annotated_return = bool(node.returns)
|
||||||
if has_annotated_args or has_annotated_return:
|
if has_annotated_args or has_annotated_return:
|
||||||
# Place at the start of the decorator list, in case a typechecking
|
# Place at the end of the decorator list, as otherwise we wrap e.g.
|
||||||
# annotation has been manually applied; we need to be above that.
|
# `jax.custom_{jvp,vjp}` and lose the ability to `defjvp` etc.
|
||||||
node.decorator_list.insert(
|
#
|
||||||
0,
|
# Note that the counter-argument here is that we'd like to place this
|
||||||
|
# at the start of the decorator list, in case a typechecking annotation
|
||||||
|
# has been manually applied, and we'd need to be above that. In this
|
||||||
|
# case we're just going to have to need to ask the user to remove their
|
||||||
|
# typechecking annotation (and let this decorator do it instead).
|
||||||
|
# It's more important we be compatible with normal JAX code.
|
||||||
|
node.decorator_list.append(
|
||||||
ast.Attribute(
|
ast.Attribute(
|
||||||
ast.Name(id="jaxtyping", ctx=ast.Load()), "jaxtyped", ast.Load()
|
ast.Name(id="jaxtyping", ctx=ast.Load()), "jaxtyped", ast.Load()
|
||||||
),
|
),
|
||||||
@@ -230,8 +238,16 @@ def install_import_hook(
|
|||||||
- `typechecker`: the module and function of the typechecker you want to use, as a
|
- `typechecker`: the module and function of the typechecker you want to use, as a
|
||||||
2-tuple of strings. For example `typechecker=("typeguard", "typechecked")` or
|
2-tuple of strings. For example `typechecker=("typeguard", "typechecked")` or
|
||||||
`typechecker=("beartype", "beartype")`. You may pass `typechecker=None` if you
|
`typechecker=("beartype", "beartype")`. You may pass `typechecker=None` if you
|
||||||
do not want to automatically decorate with a typechecker as well; e.g. if you
|
do not want to automatically decorate with a typechecker as well.
|
||||||
have a codebase that already has these decorators.
|
|
||||||
|
If the function already has any decorators on it, then both the `@jaxtyped` and the
|
||||||
|
typechecker decorators will go at the bottom of the decorator list, e.g.
|
||||||
|
```python
|
||||||
|
@some_other_decorator
|
||||||
|
@jaxtyped
|
||||||
|
@beartype.beartype
|
||||||
|
def foo(...): ...
|
||||||
|
```
|
||||||
|
|
||||||
**Returns:**
|
**Returns:**
|
||||||
|
|
||||||
@@ -243,8 +259,8 @@ def install_import_hook(
|
|||||||
```python
|
```python
|
||||||
# entry_point.py
|
# entry_point.py
|
||||||
from jaxtyped import install_import_hook
|
from jaxtyped import install_import_hook
|
||||||
install_import_hook("main", ("beartype", "beartype"))
|
with install_import_hook("main", ("beartype", "beartype"))
|
||||||
import main
|
import main
|
||||||
... # do whatever you're doing
|
... # do whatever you're doing
|
||||||
|
|
||||||
# main.py
|
# main.py
|
||||||
|
|||||||
@@ -18,7 +18,9 @@
|
|||||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
import functools as ft
|
import functools as ft
|
||||||
from typing import Generic, TypeVar
|
import typing
|
||||||
|
from typing import Generic, TYPE_CHECKING, TypeVar
|
||||||
|
from typing_extensions import Protocol
|
||||||
|
|
||||||
import jax
|
import jax
|
||||||
import typeguard
|
import typeguard
|
||||||
@@ -51,7 +53,10 @@ class _MetaPyTree(type):
|
|||||||
def __getitem__(cls, item):
|
def __getitem__(cls, item):
|
||||||
name = str(_FakePyTree[item])
|
name = str(_FakePyTree[item])
|
||||||
out = _MetaSubscriptPyTree(name, (), {"leaftype": item})
|
out = _MetaSubscriptPyTree(name, (), {"leaftype": item})
|
||||||
out.__module__ = "jaxtyping"
|
if getattr(typing, "GENERATING_DOCUMENTATION", False):
|
||||||
|
out.__module__ = "builtins"
|
||||||
|
else:
|
||||||
|
out.__module__ = "jaxtyping"
|
||||||
return out
|
return out
|
||||||
|
|
||||||
|
|
||||||
@@ -82,8 +87,18 @@ class _MetaSubscriptPyTree(type):
|
|||||||
return all(map(is_leaftype, leaves))
|
return all(map(is_leaftype, leaves))
|
||||||
|
|
||||||
|
|
||||||
PyTree = _MetaPyTree("PyTree", (), {})
|
if TYPE_CHECKING:
|
||||||
PyTree.__module__ = "jaxtyping"
|
# Work around pytype bug #1288
|
||||||
|
# pytype: skip-file
|
||||||
|
class PyTree(Protocol[_T]):
|
||||||
|
pass
|
||||||
|
|
||||||
|
else:
|
||||||
|
PyTree = _MetaPyTree("PyTree", (), {})
|
||||||
|
if getattr(typing, "GENERATING_DOCUMENTATION", False):
|
||||||
|
PyTree.__module__ = "builtins"
|
||||||
|
else:
|
||||||
|
PyTree.__module__ = "jaxtyping"
|
||||||
# Can't do `class PyTree(Generic[_T]): ...` because we need to override the
|
# Can't do `class PyTree(Generic[_T]): ...` because we need to override the
|
||||||
# instancecheck for PyTree[foo], but we subclassing
|
# instancecheck for PyTree[foo], but subclassing
|
||||||
# `type(Generic[int])`, i.e. `typing._GenericAlias` is disallowed.
|
# `type(Generic[int])`, i.e. `typing._GenericAlias` is disallowed.
|
||||||
|
|||||||
@@ -94,4 +94,5 @@ setuptools.setup(
|
|||||||
install_requires=install_requires,
|
install_requires=install_requires,
|
||||||
entry_points=entry_points,
|
entry_points=entry_points,
|
||||||
packages=[name],
|
packages=[name],
|
||||||
|
include_package_data=True,
|
||||||
)
|
)
|
||||||
|
|||||||
+27
-2
@@ -25,7 +25,22 @@ from typeguard import typechecked
|
|||||||
from jaxtyping import Array, Float, jaxtyped
|
from jaxtyping import Array, Float, jaxtyped
|
||||||
|
|
||||||
|
|
||||||
def test_threading():
|
class _ErrorableThread(threading.Thread):
|
||||||
|
def run(self):
|
||||||
|
try:
|
||||||
|
super().run()
|
||||||
|
except Exception as e:
|
||||||
|
self.exc = e
|
||||||
|
finally:
|
||||||
|
del self._target, self._args, self._kwargs
|
||||||
|
|
||||||
|
def join(self, timeout=None):
|
||||||
|
super().join(timeout)
|
||||||
|
if hasattr(self, "exc"):
|
||||||
|
raise self.exc
|
||||||
|
|
||||||
|
|
||||||
|
def test_threading_jaxtyped():
|
||||||
@jaxtyped
|
@jaxtyped
|
||||||
@typechecked
|
@typechecked
|
||||||
def add(x: Float[Array, "a b"], y: Float[Array, "a b"]) -> Float[Array, "a b"]:
|
def add(x: Float[Array, "a b"], y: Float[Array, "a b"]) -> Float[Array, "a b"]:
|
||||||
@@ -36,6 +51,16 @@ def test_threading():
|
|||||||
b = jnp.array([[2.0, 3.0]])
|
b = jnp.array([[2.0, 3.0]])
|
||||||
add(a, b)
|
add(a, b)
|
||||||
|
|
||||||
thread = threading.Thread(target=run)
|
thread = _ErrorableThread(target=run)
|
||||||
|
thread.start()
|
||||||
|
thread.join()
|
||||||
|
|
||||||
|
|
||||||
|
def test_threading_nojaxtyped():
|
||||||
|
def run():
|
||||||
|
a = jnp.array([[1.0, 2.0]])
|
||||||
|
assert isinstance(a, Float[Array, "..."])
|
||||||
|
|
||||||
|
thread = _ErrorableThread(target=run)
|
||||||
thread.start()
|
thread.start()
|
||||||
thread.join()
|
thread.join()
|
||||||
|
|||||||
Reference in New Issue
Block a user