patrick-kidger / patrick-kidger/jaxtyping

Splicing / variadic symbolic expressions

Open
#265 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.