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.

[Documentation] Push `jaxtyping` over `torchtyping` for type-checking PyTorch tensors

See original GitHub issue

Hi. I was trying to use beartype as a drop-in replacement for typeguard with torchtyping.

from torch import rand
from torchtyping import TensorType, patch_typeguard
from typeguard import typechecked

patch_typeguard() 

@typechecked
def func(x: TensorType["batch"],
         y: TensorType["batch"]) -> TensorType["batch"]:
    return x + y

func(rand(3), rand(1))

This raises the expected error.

TypeError: Dimension 'batch' of inconsistent size. Got both 1 and 3.

but it doesn’t work. Should we manually write patch_beartype like patch_typeguard?

from torch import rand
from torchtyping import TensorType
from beartype import beartype

@beartype
def func(x: TensorType["batch"],
         y: TensorType["batch"]) -> TensorType["batch"]:
    return x + y

func(rand(3), rand(1))

Issue Analytics

  • State:open
  • Created 10 months ago
  • Reactions:2
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
patrick-kidgercommented, Dec 9, 2022

Update: as a quick hack to make things easier, the latest version (0.2.9) of jaxtyping should now no longer have JAX as a mandatory dependency.

If JAX is installed then the jaxtyping.{Array, PyTree} types will exist. If it isn’t, then they won’t. Either way the Float, Int etc. types will exist and should work with PyTorch tensors.

Choice of name aside, this should mean that jaxtyping is now backend-independent.

2reactions
patrick-kidgercommented, Dec 7, 2022

Yup, I’ve been thinking about how best to handle backend agnosticism. I’ve got some plans on that front, and when/if I get more time then I’d like to revisit this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

patrick-kidger/torchtyping - GitHub
Type annotations and dynamic checking for a tensor's shape, dtype, names, etc. ... If you use JAX instead of PyTorch, then see jaxtyping....
Read more >
[P] torchtyping -- documentation + runtime type checking of ...
I'm excited to announce torchtyping, as a way to document -- and check -- that PyTorch tensors have the correct shape (dtype, names,...
Read more >
torch.Tensor — PyTorch 1.13 documentation
Torch defines 10 tensor types with CPU and GPU variants which are as follows: ... T() on tensors of dimension other than 2...
Read more >
Patrick Kidger (fosstodon.org/@PatrickKidger) on Twitter: "So I ...
So I've made a library - "torchtyping" - for annotating a PyTorch Tensor's shape (dtype, names, layout, ...) And at runtime it checks...
Read more >
Torchtyping: Type Annotations for A Tensor's Shape, Dtype ...
EXAMPLE from torch import rand from torchtyping import TensorType, patch_typeguard from typeguard import typechecked patch_typeguard() # use before ...
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