57 Commits
Author SHA1 Message Date
Roman Knyazhitskiy 172b83b4fc Adding a test for generator support (#171)
* Add a test for generators

* Remove output annotations from decorators

Also guarded torch imports for better compatibility with
requirements.txt

* Add flag to the main meta class to skip the typecheck

* Return to the old solution

* Make async tests work

* Minor adjustments/fixing typos

* Correct Python path for new tests

* Remove some jax-dependent code

* Implement equality for MetaArrays

* Make all Dim variations frozen dataclasses

* Shorten AbstractArray methods

* Final touches

* Removing get_origin use

* Update tests with @jaxtyp
2024-02-25 12:07:01 +00:00
Patrick Kidger 17ea4b13eb No longer imports JAX at all! This is done dynamically when required. See #178 2024-02-25 12:07:01 +00:00
jianlijianli 9beb5f2d29 Add int4/uint4 support in jaxtyping. (#174)
* Add int4/uint4 support in jaxtyping.

* Fix a typo and update api docs.
2024-02-25 12:07:01 +00:00
Patrick Kidger d7fd59a34c Added print_bindings. 2024-02-25 12:07:01 +00:00
Patrick Kidger 1acc0d7153 Improved error messages a little bit, in particular to highlight individual problematic arguments. 2023-12-08 10:17:57 -08:00
Patrick Kidger 33cf4fcdac Simplified internals by removing jaxtyping_raise; jaxtyping_malformed. 2023-12-05 19:06:00 -08:00
Patrick Kidger 125bc89ee9 Added environment config flags.
These flags are `JAXTYPING_DISABLE` and `JAXTYPING_REMOVE_TYPECHECKER_STACK`.

In addition, have now added warnings when using old-style double-decorator syntax, which also serves to guard against the easy mistake of
```python
@jaxtyped(typechecker)
def foo(...)
```
which actually decorates the `typechecker`, not `foo`.
2023-12-05 19:06:00 -08:00
Patrick Kidger 0a76c9c70c Error message improvements 2023-11-27 09:50:02 -08:00
Patrick Kidger 5fbd6718ab Added support for 'self' in dataclass attribute annotations; switched from args and kwargs to just arguments. 2023-11-27 09:50:02 -08:00
Patrick Kidger 80a99568f7 Removed unused elements from unions, e.g. Float[ArrayLike, ...] will no longer include Float[np.bool, ...]. 2023-11-27 09:50:02 -08:00
Patrick Kidger 58600d3fe0 Fixed new-style PRNG keys. 2023-11-27 09:50:02 -08:00
Patrick Kidger 7925e278f4 Symbolic expressions now support delayed binding to arguments. Fixes #93. 2023-11-27 09:50:02 -08:00
Patrick Kidger 12d540794f Pretty error messages: fixes #6.
Phew, this ended up being a pretty complicated change!
The basic summary is that we now support the syntax
```
@jaxtyped(typechecker=typechecker)
def f(...): ...
```
and when using this, we now get pretty error messages about what went
wrong.

(
The old syntax, i.e.
```
@jaxtyped
@typechecker
def f(...): ...
```
is still supported, but doesn't give much information.
)

The internals of this do quite a lot of magic! In particular we
dynamically create quite a lot of functions and test the provided
arguments against their signatures. The overhead should still be
minimal under `jax.jit`, though.
(TODO: what's the overhead like in non-jit situations, e.g. PyTorch?
I've tried to minimise the overhead throughout just to be sure, but
perhaps PyTorch users should stick to the old syntax?)
2023-11-27 09:50:02 -08:00
Patrick Kidger d12291de7e Added support for treepath-dependent sizes. 2023-11-27 09:50:02 -08:00
Patrick Kidger 9e1ba8a77d Added support for declaring PyTree structures. 2023-11-27 09:50:02 -08:00
Patrick Kidger e4a93ee218 Added with jaxtyped("context"):, for now undocumented. 2023-11-07 11:34:40 -08:00
Patrick Kidger 260fb36876 Fixed mixing variadic+broadcast with variadic+nonbroadcast dimensions.
Previously, something like this would not raise an error, as
variadic+broadcast dimensions were stored in a separate namespace to
variadic+nonbroadcast dimensions:
```python
def f(x: Float[Array, "*foo"], y: Float[Array, "#*foo"]):
    pass

a, b = ...
assert a.shape == (3, 4)
assert b.shape == (5,)
f(a, b)
```
2023-11-07 11:34:40 -08:00
Patrick Kidger 9646eff7e1 Fixes for g3 2023-10-23 22:00:51 -07:00
Patrick Kidger 1a048b1f2f Fixed Float[ArrayLike, "#*foo"] leaving out bool/int/float/complex. 2023-10-21 12:01:21 -07:00
Patrick Kidger 91a36aaee4 dataclasses now have fields checked, not __init__.
Previously, using the import hook with dataclasses resulted in the `__init__` method of the dataclass being checked.
This was undesirable when using `eqx.field(converter=...)`, as the annotation didn't necessarily reflect the argument type.
A typical example was
```python
class Foo(eqx.Module):
    x: jax.Array = eqx.field(converter=jnp.ndarray)

Foo(1)  # 1 is not an array! But this code is valid.
```

After this change, we instead monkey-patch our checks to happen at the end of the `__init__` of the dataclass -- after conversion has run.

Note that this requires https://github.com/patrick-kidger/equinox/pull/524. Otherwise, Equinox does conversion too late (in `_ModuleMeta.__call__`, after `__init__` has been run).
2023-10-09 21:58:14 -07:00
Roma Knyaz 513a54b048 Better handling of user-defined typechecker 2023-10-09 10:21:07 -07:00
Roma Knyaz 4917c2e30f Fix flakiness of transitive import hook test 2023-10-02 09:03:06 -07:00
Patrick Kidger 17092ad8d8 Simplified the import hook tests 2023-09-27 15:37:56 -07:00
Patrick Kidger 1e5229c20e Should be more robust to jax/numpy/tensorflow version changes 2023-09-25 11:07:36 -07:00
Roma Knyaz f454cb797c Make jaxtyping an IPython extension 2023-09-20 10:34:32 -07:00
Patrick Kidger 6a64ef114e Now provides PyTreeDef, and can detect PyTrees via issubclass(x, PyTree) 2023-06-01 10:56:00 -07:00
Patrick Kidger 849b15db3b Added better docs; added PRNGKeys. Packaging with pyproject.toml.
Also added support for matching dtypes using regexes, and for nesting e.g. Shaped[Float[Array, "dim1 dim2"], "dim3"].
2023-05-10 15:27:25 -07:00
Patrick Kidger 46c7896c99 Transitive test now uses typeguard instead of beartype 2023-04-19 11:48:08 -07:00
Patrick Kidger c92b0d0ab1 Some improvements (#77)
* Various improvements.

- Added support for functions in symbolic dimensions, e.g. "min(foo,bar)", which were previously disallowed due to the presence of a comma. (#51)
- Added support for adding ignored names to dimensions, e.g. "cols=4". (#76)

* Now works with Python 3.10 A | B union types.
2023-04-13 19:18:55 +01:00
Patrick Kidger 158b8b8f0c Now works with torch.compile? (#72) 2023-04-13 18:53:14 +01:00
Patrick Kidger f0b240df5f Switched to ruff 2023-03-15 22:36:24 -07:00
Patrick Kidger a4d27c7cc1 Fixed _Jaxtyped.__get__, e.g. swallowing abstractmethod decorations 2023-03-15 22:27:36 -07:00
Patrick Kidger e03c1c329e We now have Float[np.ndarray, ...] <: np.ndarray. Added basic torch tests. (#68)
This required quite a lot of refactoring! JAX supports virtual subclass registration (its metaclass is ABCMeta) but NumPy does not, so we have to actually subclass `np.ndarray`.
Simple stuff like __base__ hacking fails due to deallocator conflicts.
2023-03-04 17:29:04 +00:00
Patrick Kidger fef81cf0a0 The import hook now supports BeartypeConf/BeartypeStrategy 2023-03-03 10:34:03 -08:00
Patrick Kidger 5600a1aac8 Fixed cloudpickle breaking, mark 2 2023-03-02 17:37:53 -08:00
Patrick Kidger 2b339715f9 Fixed cloudpickle breaking 2023-03-02 12:35:38 -08:00
Patrick Kidger ffc56bf782 Edge case fix 2023-02-25 17:38:45 -08:00
Patrick Kidger e2f004afd4 Added support for jax.typing.ArrayLike; now works with PyTorch's bool 2023-02-25 17:01:27 -08:00
Patrick Kidger 81c56052e5 Fixes for some new failures. (Where did they come from?) (#65)
* Fixes for some new failures. (Where did they come from?)

* Fixed isort?
2023-02-16 10:08:57 -08:00
Brent Yi 4b3f834e12 Fix vanilla dataclasses (#56) 2023-01-15 10:52:18 +01:00
Patrick Kidger 7dba3516c2 Fixed working with the new (unreleased) version of typeguard (#53) 2022-12-29 17:46:25 +00:00
Patrick Kidger a220df9964 The import hook now decorates dataclass __init__ methods (#48) 2022-11-16 13:38:04 -08:00
Peter Roelants d3651ca70e NamedTuple example (#36) 2022-10-03 07:40:51 -07:00
Patrick Kidger 39439c2790 More fixes (#27)
* Edge-case doc fixes for parameterising types with PyTrees or AbstractDtypes

* Fixes for threading

* version bump
2022-09-20 15:12:21 -07:00
Patrick Kidger c2e9d913d5 Fixed jaxtyped breaking descriptors. Fixed long module names. (#25) 2022-09-19 22:40:25 -07:00
Patrick Kidger 62ddcc25b5 Threading fix (#24) 2022-09-16 08:04:28 -07:00
Patrick Kidger 01f8f20bf5 Fixed a few names 2022-09-07 12:20:42 -07:00
Patrick Kidger a53fe6af57 Changes from feedback:
- Float32 introduced to replace f32 (etc.)
- Old f32 aliases were previously around for backward-compatibility, but
  the code is pretty hideous, and all stakeholders are now on board
  through RFC #13.
- Overall feedback was that Int{Sign,Unsign} was too long and not enough
  like numpy. Changed to Int, UInt, and Integer.
2022-09-07 11:09:48 -07:00
Patrick Kidger 48e8131247 Added jaxtyping.Array=jnp.ndarray 2022-08-30 12:56:47 -07:00
Patrick Kidger 903000f3d5 Rewrote syntax 2022-08-30 12:47:45 -07:00