patrick-kidger / patrick-kidger/jaxtyping
Splicing / variadic symbolic expressions
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.9k
- Forks
- 96
- PR merge metrics
- No merged PRs in 30d
Description
Would it be possible to make the following code snippet work?
import torch
from beartype import beartype
from jaxtyping import Float, jaxtyped
from torch import Tensor
class A:
def __init__(self, shape: tuple[int, ...]):
self.shape = shape
@jaxtyped(typechecker=beartype)
def forward(self, x: Float[Tensor, "... {self.shape}"]) -> Float[Tensor, "..."]:
return x.flatten(start_dim=-len(self.shape)).sum(dim=-1)
a = A((3, 10, 5))
x = torch.randn((7, 3, 4, 5))
print(a.forward(x))
At the moment it does not work as far as I can tell, because {self.shape} is only matched against a single dimension of x. Is there a way to evaluate the expression and splice in the tuple value into the type before the type gets matched against the dimensions? Maybe with something like a *{self.shape} syntax?
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
Start by running the provided Python example with jaxtyping, beartype, and torch to reproduce how {self.shape} is matched. Trace the shape-expression parsing and matching entry points; done means tuple-valued instance attributes can be spliced into symbolic expressions and the example validates with the intended output shape.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100