question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Type checking for pytrees

See original GitHub issue

Glad to hear JAX will eventually have annotations! I was following this issue for updates. I actually started trying to add annotations myself before I realized how much work it was going to be.

One thing it would be nice to expose as soon as possible though are base types for tensors and pytrees:

Tensor = Union[np.ndarray, jnp.ndarray]  # probably needs more things in the union like tracers and DeviceArray?
PyTree = Union[Tensor,
               'PyTreeLike',
               Tuple['PyTree', ...],
               List['PyTree'],
               Dict[Hashable, 'PyTree'],
               None]

_Originally posted by @NeilGirdhar in https://github.com/google/jax/issues/1555#issuecomment-639554300_

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:5
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
XuehaiPancommented, Oct 21, 2022

Yes, in the sense that I think we’re fairly confident it cannot be done without a Python type checker that supports recursive types, which mypy and pytype (our current type checkers) do not. So, you should write Any.

mypy now supports recursive types since v0.981, and will be enabled by default since v0.990. E.g.:

JSON = Union[Dict[str, 'JSON'], List['JSON'], str, int, float, bool, None]
1reaction
shoyercommented, Jun 12, 2020

I can imagine two ways in which it might make sense to type check pytrees:

  • Types checking over the types of leaves, e.g., a pytree of JAX arrays
  • Type checking over the structure of trees, e.g., to verify that two pytrees are both dictionaries with the same keys

In theory, the first type could be checked with Generic and the second type could be checked (at least partially) with TypeVar.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pytrees - JAX documentation - Read the Docs
Classes are considered container-like if they are in the pytree registry, which by default includes lists, tuples, and dicts. That is: any object...
Read more >
py_trees Documentation - Read the Docs
A parallel checking for a valid policy configuration after ... The py-trees-demo-action-behaviour program demonstrates a more complicated ...
Read more >
Patrick Kidger (fosstodon.org/@PatrickKidger) on Twitter ...
Type annotations and runtime checking for shape and dtype of JAX arrays, and PyTrees. - GitHub - google/jaxtyping: Type annotations and ...
Read more >
TF_JAX_tutorials - Part 10 (Pytrees in JAX) - Kaggle
JAX also lets you register custom types as pytrees (we will take a few ... Check if we can make a pytree from...
Read more >
Python Type Checking (Guide) - Real Python
Type annotations and type hints; Adding static types to code, both your code and the code of others; Running a static type checker;...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found