patrick-kidger / patrick-kidger/jaxtyping
How to specify per leaf shape of a pytree?
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.9k
- Forks
- 96
- PR merge metrics
- No merged PRs in 30d
Description
How to specify exact per leaf shape of a pytree, say a dict? And even further, the graph structure of a pytree.
For example, function f takes a dict as input:
@jaxtyped(typechecker=beartype)
def f(state: Dict{'x': Float[Array, 'b 10'], 'y': Float[Array, 'b 1']}):
...
# Example valid input
valid_state = {
'x': jnp.ones((3, 10)), # b=3
'y': jnp.zeros((3, 1)) # b=3, consistent
}
# Example invalid input (wrong shape for 'x')
invalid_state = {
'x': jnp.ones((3, 99)), # Shape is not 'b 10'
'y': jnp.zeros((3, 1))
}
f(valid_state)
try:
f(invalid_state)
except Exception as e:
print(f"\nError with invalid_state:\n{e}")
(The above snippet is not going to work)
Hope the type checker can check every leaf's shape and the graph structure.
PyTree[Float[Array, 'b ...']] is good but not fine-grained.
I think this feature is quite intuitive, e.g., in RL, jax env's step function takes a complex state. Maybe there is ways or workaround but I failed to find one. Sorry for possible ignorance.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No repository files, tests, or entry points are identified in the issue. Start by reviewing the existing PyTree and shape-annotation support, then determine how the shown dictionary example could express per-leaf shapes and shared dimensions. Done means validating each leaf and the requested pytree graph structure, with coverage for both valid and invalid examples.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- machine-learning
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100