Should be more robust to jax/numpy/tensorflow version changes

This commit is contained in:
Patrick Kidger
2023-09-25 11:07:36 -07:00
parent e05985df2b
commit 1e5229c20e
4 changed files with 29 additions and 5 deletions
+1
View File
@@ -26,3 +26,4 @@ repos:
rev: 'v0.0.255'
hooks:
- id: ruff
args: ["--fix"]
+13 -4
View File
@@ -144,10 +144,19 @@ def _check_dims(
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)
try:
is_dtype = issubclass(dtype, jax.numpy.generic)
except TypeError:
# `dtype` not a class
return False
else:
if is_dtype:
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)
else:
return False
class _MetaAbstractArray(type):
+2 -1
View File
@@ -1,7 +1,8 @@
beartype
cloudpickle
equinox
IPython
jaxlib
pytest
tensorflow
typeguard<3
IPython
+13
View File
@@ -0,0 +1,13 @@
# Tensorflow dependency kept in a separate file, so that we can optionally exclude it
# more easily.
import tensorflow as tf
from jaxtyping import UInt
def test_tf_dtype():
x = tf.constant(1, dtype=tf.uint8)
y = tf.constant(1, dtype=tf.float32)
hint = UInt[tf.Tensor, "..."]
assert isinstance(x, hint)
assert not isinstance(y, hint)