Compare commits

...
10 Commits
7 changed files with 77 additions and 9 deletions
+11 -5
View File
@@ -41,15 +41,21 @@ Available at [https://docs.kidger.site/jaxtyping](https://docs.kidger.site/jaxty
## Finally ## Finally
### See also: other tools in the JAX ecosystem ### See also: other libraries in the JAX ecosystem
Neural networks: [Equinox](https://github.com/patrick-kidger/equinox). [Equinox](https://github.com/patrick-kidger/equinox): neural networks.
Numerical differential equation solvers: [Diffrax](https://github.com/patrick-kidger/diffrax). [Optax](https://github.com/deepmind/optax): first-order gradient (SGD, Adam, ...) optimisers.
Computer vision models: [Eqxvision](https://github.com/paganpasta/eqxvision). [Diffrax](https://github.com/patrick-kidger/diffrax): numerical differential equation solvers.
SymPy<->JAX conversion; train symbolic expressions via gradient descent: [sympy2jax](https://github.com/google/sympy2jax). [Lineax](https://github.com/google/lineax): linear solvers and linear least squares.
[Eqxvision](https://github.com/paganpasta/eqxvision): computer vision models.
[sympy2jax](https://github.com/google/sympy2jax): SymPy<->JAX conversion; train symbolic expressions via gradient descent.
[Levanter](https://github.com/stanford-crfm/levanter): scalable+reliable training of foundation models (e.g. LLMs).
### Disclaimer ### Disclaimer
+16
View File
@@ -40,3 +40,19 @@ def accepts_pytree_of_arrays(x: PyTree[Float[Array, "batch c1 c2"]]):
## Next steps ## Next steps
Have a read of the [Array annotations](./api/array.md) documentation on the left-hand bar! Have a read of the [Array annotations](./api/array.md) documentation on the left-hand bar!
## See also: other libraries in the JAX ecosystem
[Equinox](https://github.com/patrick-kidger/equinox): neural networks.
[Optax](https://github.com/deepmind/optax): first-order gradient (SGD, Adam, ...) optimisers.
[Diffrax](https://github.com/patrick-kidger/diffrax): numerical differential equation solvers.
[Lineax](https://github.com/google/lineax): linear solvers and linear least squares.
[Eqxvision](https://github.com/paganpasta/eqxvision): computer vision models.
[sympy2jax](https://github.com/google/sympy2jax): SymPy<->JAX conversion; train symbolic expressions via gradient descent.
[Levanter](https://github.com/stanford-crfm/levanter): scalable+reliable training of foundation models (e.g. LLMs).
+27 -1
View File
@@ -154,12 +154,28 @@ if typing.TYPE_CHECKING:
# annotation, which static type checkers aren't smart enough to resolve. # annotation, which static type checkers aren't smart enough to resolve.
elif has_jax: elif has_jax:
if hasattr(typing, "GENERATING_DOCUMENTATION"): if hasattr(typing, "GENERATING_DOCUMENTATION"):
# Most parts of the Equinox ecosystem have
# `typing.GENERATING_DOCUMENTATION = True` when generating documentation, to
# add whatever shims are necessary to get pretty docs. E.g. to have type
# annotations appear as just `PyTree`, not `jaxtyping.PyTree`.
#
# As jaxtyping actually wants things to appear as e.g. `jaxtyping.PyTree`,
# rather than just `PyTree`, then it sets
# `typing.GENERATING_DOCUMENTATION = False`, to disable these shims.
#
# Here we do only a `hasattr` check, as we want to get this version of
# `PyTreeDef` in both the jaxtyping and the Equinox(/etc.) docs.
class PyTreeDef: class PyTreeDef:
"""Alias for `jax.tree_util.PyTreeDef`, which is the type of the return """Alias for `jax.tree_util.PyTreeDef`, which is the type of the return
from `jax.tree_util.tree_structure(...)`. from `jax.tree_util.tree_structure(...)`.
""" """
if typing.GENERATING_DOCUMENTATION:
# Equinox etc. docs get just `PyTreeDef`.
# jaxtyping docs get `jaxtyping.PyTreeDef`.
PyTreeDef.__module__ = "builtins"
else: else:
from jax.tree_util import PyTreeDef as PyTreeDef from jax.tree_util import PyTreeDef as PyTreeDef
@@ -172,7 +188,17 @@ if typing.TYPE_CHECKING:
from ._indirection import Scalar as Scalar, ScalarLike as ScalarLike from ._indirection import Scalar as Scalar, ScalarLike as ScalarLike
elif has_jax: elif has_jax:
from ._array_types import PRNGKeyArray, Scalar, ScalarLike # noqa: F401 from ._array_types import Scalar, ScalarLike # noqa: F401
if getattr(typing, "GENERATING_DOCUMENTATION", False):
# That is, we're generating some downstream documentation, not the jaxtyping
# documentation itself.
class PRNGKeyArray:
pass
PRNGKeyArray.__module__ = "builtins"
else:
from ._array_types import PRNGKeyArray
del has_jax del has_jax
+10 -1
View File
@@ -137,12 +137,21 @@ def _check_dims(
return True return True
def _is_jax_extended_dtype(dtype: Any) -> bool:
if not has_jax:
return False
if hasattr(jax.dtypes, "extended"): # jax>=0.4.14
return jax.numpy.issubdtype(dtype, jax.dtypes.extended)
else: # jax<=0.4.13
return jax.core.is_opaque_dtype(dtype)
class _MetaAbstractArray(type): class _MetaAbstractArray(type):
def __instancecheck__(cls, obj): def __instancecheck__(cls, obj):
if not isinstance(obj, cls.array_type): if not isinstance(obj, cls.array_type):
return False return False
if has_jax and jax.core.is_opaque_dtype(obj.dtype): if _is_jax_extended_dtype(obj.dtype):
dtype = str(obj.dtype) dtype = str(obj.dtype)
elif hasattr(obj.dtype, "type") and hasattr(obj.dtype.type, "__name__"): elif hasattr(obj.dtype, "type") and hasattr(obj.dtype.type, "__name__"):
# JAX, numpy # JAX, numpy
+8
View File
@@ -25,6 +25,14 @@ import types
import weakref import weakref
try:
import jax._src.traceback_util as traceback_util
except ImportError:
pass
else:
traceback_util.register_exclusion(__file__)
storage = threading.local() storage = threading.local()
+4 -1
View File
@@ -51,6 +51,7 @@
import ast import ast
import functools as ft import functools as ft
import hashlib
import sys import sys
from collections.abc import Sequence from collections.abc import Sequence
from importlib.abc import MetaPathFinder from importlib.abc import MetaPathFinder
@@ -165,7 +166,9 @@ class _JaxtypingLoader(SourceFileLoader):
def __init__(self, *args, typechecker, **kwargs): def __init__(self, *args, typechecker, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self._typechecker = typechecker self._typechecker = typechecker
self._typechecker_hash = str(abs(hash(self._typechecker))) self._typechecker_hash = hashlib.md5(
self._typechecker.encode("utf-8")
).hexdigest()
def source_to_code(self, data, path, *, _optimize=-1): def source_to_code(self, data, path, *, _optimize=-1):
source = decode_source(data) source = decode_source(data)
+1 -1
View File
@@ -1,6 +1,6 @@
[project] [project]
name = "jaxtyping" name = "jaxtyping"
version = "0.2.20" version = "0.2.21"
description = "Type annotations and runtime checking for shape and dtype of JAX arrays, and PyTrees." description = "Type annotations and runtime checking for shape and dtype of JAX arrays, and PyTrees."
readme = "README.md" readme = "README.md"
requires-python ="~=3.9" requires-python ="~=3.9"