Compare commits

...
10 Commits
Author SHA1 Message Date
Michael J Clark 1456302503 link to why use type annotations 2024-05-19 09:16:10 +08:00
Michael J Clark 1c0186a1c9 tf, np, torch examples 2024-05-19 09:12:49 +08:00
Patrick Kidger 0d83ee77e6 Updated ecosystem again 2024-04-20 11:25:42 +02:00
Patrick Kidger 51ac630ef0 Updated ecosystem 2024-04-20 11:18:48 +02:00
Sergei Lebedev f83170e01d Define the IPython magic lazily
IPython is quite a chunky package, and importing it unconditionally makes
jaxtyping itself relatively slow to import.
2024-04-17 09:45:56 +02:00
Piotr Kaminski 07e58de0bd Replace ndim with len(shape) 2024-03-11 20:35:29 +01:00
Andy Rock 696cc5b90f also require jaxlib 2024-03-08 18:17:50 +01:00
Patrick Kidger 102e499d61 Fixes #188. 2024-03-07 18:28:08 +01:00
Patrick Kidger f708d1742b Quick fix for docs not generating correctly since the last jaxtyping release 2024-03-06 20:34:48 +01:00
Alex Ford 5e2518c591 Fix _check_shape str formatting for variadics 2024-03-06 20:21:03 +01:00
7 changed files with 72 additions and 54 deletions
+27 -23
View File
@@ -1,6 +1,6 @@
<h1 align="center">jaxtyping</h1> <h1 align="center">jaxtyping</h1>
Type annotations **and runtime type-checking** for: [Use type annotations **and runtime type-checking**](https://jax.readthedocs.io/en/latest/jep/12049-type-annotations.html) for:
1. shape and dtype of [JAX](https://github.com/google/jax) arrays; *(Now also supports PyTorch, NumPy, and TensorFlow!)* 1. shape and dtype of [JAX](https://github.com/google/jax) arrays; *(Now also supports PyTorch, NumPy, and TensorFlow!)*
2. [PyTrees](https://jax.readthedocs.io/en/latest/pytrees.html). 2. [PyTrees](https://jax.readthedocs.io/en/latest/pytrees.html).
@@ -8,7 +8,11 @@ Type annotations **and runtime type-checking** for:
**For example:** **For example:**
```python ```python
from jaxtyping import Array, Float, PyTree from jaxtyping import Array, Float, PyTree, , UInt, Int, Bool
import torch
impport numpy as np
import tensorflow as tf
# Accepts floating-point 2D arrays with matching axes # Accepts floating-point 2D arrays with matching axes
def matrix_multiply(x: Float[Array, "dim1 dim2"], def matrix_multiply(x: Float[Array, "dim1 dim2"],
@@ -21,6 +25,15 @@ def accepts_pytree_of_ints(x: PyTree[int]):
def accepts_pytree_of_arrays(x: PyTree[Float[Array, "batch c1 c2"]]): def accepts_pytree_of_arrays(x: PyTree[Float[Array, "batch c1 c2"]]):
... ...
def accepts_torch.Long(x: Int[torch.Tensor, "batch channel height width"]):
....
def accepts_numpy_float(x :Float[np.ndarray, "batch sequence features"]):
...
def accepts_tensorflow_uint(x: hint = UInt[tf.Tensor, "b c h w"]):
...
``` ```
## Installation ## Installation
@@ -39,32 +52,23 @@ The annotations provided by jaxtyping are compatible with runtime type-checking
Available at [https://docs.kidger.site/jaxtyping](https://docs.kidger.site/jaxtyping). Available at [https://docs.kidger.site/jaxtyping](https://docs.kidger.site/jaxtyping).
## Finally ## See also: other libraries in the JAX ecosystem
### See also: other libraries in the JAX ecosystem **Always useful**
[Equinox](https://github.com/patrick-kidger/equinox): neural networks and everything not already in core JAX!
[Equinox](https://github.com/patrick-kidger/equinox): neural networks.
**Deep learning**
[Optax](https://github.com/deepmind/optax): first-order gradient (SGD, Adam, ...) optimisers. [Optax](https://github.com/deepmind/optax): first-order gradient (SGD, Adam, ...) optimisers.
[Diffrax](https://github.com/patrick-kidger/diffrax): numerical differential equation solvers.
[Optimistix](https://github.com/patrick-kidger/optimistix): root finding, minimisation, fixed points, and least squares.
[Lineax](https://github.com/google/lineax): linear solvers.
[BlackJAX](https://github.com/blackjax-devs/blackjax): probabilistic+Bayesian sampling.
[Orbax](https://github.com/google/orbax): checkpointing (async/multi-host/multi-device). [Orbax](https://github.com/google/orbax): checkpointing (async/multi-host/multi-device).
[sympy2jax](https://github.com/google/sympy2jax): SymPy<->JAX conversion; train symbolic expressions via gradient descent.
[Eqxvision](https://github.com/paganpasta/eqxvision): computer vision models.
[Levanter](https://github.com/stanford-crfm/levanter): scalable+reliable training of foundation models (e.g. LLMs). [Levanter](https://github.com/stanford-crfm/levanter): scalable+reliable training of foundation models (e.g. LLMs).
**Scientific computing**
[Diffrax](https://github.com/patrick-kidger/diffrax): numerical differential equation solvers.
[Optimistix](https://github.com/patrick-kidger/optimistix): root finding, minimisation, fixed points, and least squares.
[Lineax](https://github.com/patrick-kidger/lineax): linear solvers.
[BlackJAX](https://github.com/blackjax-devs/blackjax): probabilistic+Bayesian sampling.
[sympy2jax](https://github.com/patrick-kidger/sympy2jax): SymPy<->JAX conversion; train symbolic expressions via gradient descent.
[PySR](https://github.com/milesCranmer/PySR): symbolic regression. (Non-JAX honourable mention!) [PySR](https://github.com/milesCranmer/PySR): symbolic regression. (Non-JAX honourable mention!)
### Disclaimer **Awesome JAX**
[Awesome JAX](https://github.com/n2cholas/awesome-jax): a longer list of other JAX projects.
This is not an official Google product.
+15 -10
View File
@@ -43,16 +43,21 @@ Have a read of the [Array annotations](./api/array.md) documentation on the left
## See also: other libraries in the JAX ecosystem ## See also: other libraries in the JAX ecosystem
[Equinox](https://github.com/patrick-kidger/equinox): neural networks. **Always useful**
[Equinox](https://github.com/patrick-kidger/equinox): neural networks and everything not already in core JAX!
**Deep learning**
[Optax](https://github.com/deepmind/optax): first-order gradient (SGD, Adam, ...) optimisers. [Optax](https://github.com/deepmind/optax): first-order gradient (SGD, Adam, ...) optimisers.
[Orbax](https://github.com/google/orbax): checkpointing (async/multi-host/multi-device).
[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). [Levanter](https://github.com/stanford-crfm/levanter): scalable+reliable training of foundation models (e.g. LLMs).
**Scientific computing**
[Diffrax](https://github.com/patrick-kidger/diffrax): numerical differential equation solvers.
[Optimistix](https://github.com/patrick-kidger/optimistix): root finding, minimisation, fixed points, and least squares.
[Lineax](https://github.com/patrick-kidger/lineax): linear solvers.
[BlackJAX](https://github.com/blackjax-devs/blackjax): probabilistic+Bayesian sampling.
[sympy2jax](https://github.com/patrick-kidger/sympy2jax): SymPy<->JAX conversion; train symbolic expressions via gradient descent.
[PySR](https://github.com/milesCranmer/PySR): symbolic regression. (Non-JAX honourable mention!)
**Awesome JAX**
[Awesome JAX](https://github.com/n2cholas/awesome-jax): a longer list of other JAX projects.
+4
View File
@@ -145,6 +145,7 @@ else:
pass pass
Array.__module__ = "builtins" Array.__module__ = "builtins"
Array.__qualname__ = "Array"
return Array return Array
else: else:
import jax import jax
@@ -157,6 +158,7 @@ else:
pass pass
ArrayLike.__module__ = "builtins" ArrayLike.__module__ = "builtins"
ArrayLike.__qualname__ = "ArrayLike"
return ArrayLike return ArrayLike
else: else:
import jax.typing import jax.typing
@@ -169,6 +171,7 @@ else:
pass pass
PRNGKeyArray.__module__ = "builtins" PRNGKeyArray.__module__ = "builtins"
PRNGKeyArray.__qualname__ = "PRNGKeyArray"
return PRNGKeyArray return PRNGKeyArray
else: else:
# New-style `jax.random.key` have scalar shape and dtype `key<foo>`. # New-style `jax.random.key` have scalar shape and dtype `key<foo>`.
@@ -216,6 +219,7 @@ else:
if typing.GENERATING_DOCUMENTATION: if typing.GENERATING_DOCUMENTATION:
# Equinox etc. docs get just `PyTreeDef`. # Equinox etc. docs get just `PyTreeDef`.
# jaxtyping docs get `jaxtyping.PyTreeDef`. # jaxtyping docs get `jaxtyping.PyTreeDef`.
PyTreeDef.__qualname__ = "PyTreeDef"
PyTreeDef.__module__ = "builtins" PyTreeDef.__module__ = "builtins"
return PyTreeDef return PyTreeDef
else: else:
+4 -4
View File
@@ -224,12 +224,12 @@ class _MetaAbstractArray(type):
arg_memo: dict[str, Any], arg_memo: dict[str, Any],
) -> str: ) -> str:
if cls.index_variadic is None: if cls.index_variadic is None:
if obj.ndim != len(cls.dims): if len(obj.shape) != len(cls.dims):
return f"this array has {obj.ndim} dimensions, not the {len(cls.dims)} expected by the type hint" # noqa: E501 return f"this array has {len(obj.shape)} dimensions, not the {len(cls.dims)} expected by the type hint" # noqa: E501
return _check_dims(cls.dims, obj.shape, single_memo, arg_memo) return _check_dims(cls.dims, obj.shape, single_memo, arg_memo)
else: else:
if obj.ndim < len(cls.dims) - 1: if len(obj.shape) < len(cls.dims) - 1:
return f"this array has {obj.ndim} dimensions, which is fewer than {len(cls.dims - 1)} that is the minimum expected by the type hint" # noqa: E501 return f"this array has {len(obj.shape)} dimensions, which is fewer than {len(cls.dims) - 1} that is the minimum expected by the type hint" # noqa: E501
i = cls.index_variadic i = cls.index_variadic
j = -(len(cls.dims) - i - 1) j = -(len(cls.dims) - i - 1)
if j == 0: if j == 0:
+6 -1
View File
@@ -189,7 +189,12 @@ def jaxtyped(fn=_sentinel, *, typechecker=_sentinel):
""" """
global _tb_flag global _tb_flag
if _tb_flag and importlib.util.find_spec("jax._src.traceback_util") is not None: if (
_tb_flag
and importlib.util.find_spec("jax") is not None
and importlib.util.find_spec("jaxlib") is not None
and importlib.util.find_spec("jax._src.traceback_util") is not None
):
import jax._src.traceback_util as traceback_util import jax._src.traceback_util as traceback_util
traceback_util.register_exclusion(__file__) traceback_util.register_exclusion(__file__)
+11 -11
View File
@@ -20,7 +20,9 @@
from ._import_hook import JaxtypingTransformer, Typechecker from ._import_hook import JaxtypingTransformer, Typechecker
try: def choose_typechecker_magics():
# The import is local to avoid degrading import times when the magic is
# not needed.
from IPython.core.magic import line_magic, Magics, magics_class from IPython.core.magic import line_magic, Magics, magics_class
@magics_class @magics_class
@@ -40,17 +42,15 @@ try:
JaxtypingTransformer(typechecker=Typechecker(typechecker)) JaxtypingTransformer(typechecker=Typechecker(typechecker))
) )
except Exception: return ChooseTypecheckerMagics
# Very broad exception-handling, as e.g. IPython will sometimes be
# present but fail to import for mysterious reasons.
pass
def load_ipython_extension(ipython): def load_ipython_extension(ipython):
try: try:
ipython.register_magics(ChooseTypecheckerMagics) ChooseTypecheckerMagics = choose_typechecker_magics()
except NameError: except Exception as e:
raise NameError( # Very broad exception-handling, as e.g. IPython will sometimes be
"ChooseTypecheckerMagics is not defined.\n\n" # present but fail to import for mysterious reasons.
+ "You may be trying to use IPython extension without IPython installed." raise RuntimeError("Failed to define jaxtyping.typechecker magic") from e
)
ipython.register_magics(ChooseTypecheckerMagics)
+1 -1
View File
@@ -1,6 +1,6 @@
[project] [project]
name = "jaxtyping" name = "jaxtyping"
version = "0.2.26" version = "0.2.28"
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"