← all repositories
arogozhnikov/einops

Your `.view` calls are lying about shapes

It replaces cryptic tensor reshaping with self-documenting notation that runs identically across NumPy, PyTorch, JAX, and others.

9.6k stars Python ML FrameworksData Tooling
einops
Not currently ranked — collecting fresh signals.
star history

What it does

einops is a Python library that re-implements tensor reshaping, reduction, repetition, and einsum through a single mini-language. Instead of chaining view, permute, and unsqueeze, you write a pattern string like 'b c h w -> b (c h w)' that names every axis and explicitly states the desired output shape. The library parses that string and generates the correct operations for NumPy, PyTorch, JAX, TensorFlow, MLX, and any framework supporting the Python Array API standard.

The interesting bit

The notation doubles as runtime documentation and a contract: because axes are named, einops can verify dimensions at execution time, catching shape mismatches that a bare .view would silently propagate. It also eliminates framework-specific ambiguities—flatten means different things in Keras and PyTorch, but rearrange means the same thing everywhere.

Key highlights

  • Self-documenting shapes: Pattern strings like time c h w -> time (c h w) tell the reader whether the first axis is a batch or a video sequence, something a comment cannot enforce.
  • Runtime validation: You can pin exact sizes (e.g., c=256, h=19, w=19) so the code fails immediately if the tensor does not match your assumptions.
  • Framework polyglot: The same pattern runs on NumPy, PyTorch, JAX, TensorFlow, CuPy, MLX, TinyGrad, and even sparse or distributed tensors via the Array API standard.
  • Pack and unpack: Heterogeneous tensors—like a class token plus image patches plus text tokens—can be packed into a single batch for a transformer and then unpacked without manual cat/split bookkeeping.
  • Deep-learning layers: Rearrange and Reduce layers drop directly into PyTorch Sequential models and are compatible with torch.compile.

Caveats

  • torch.jit.script compatibility is limited: the functional operations do not script, though the layer wrappers do, owing to constraints in TorchScript itself.
  • Several backends—Flax, Paddle, OneFlow, TinyGrad, and PyTensor—are marked as community-supported, so mileage may vary.
  • The browser-based playground currently runs only two of the four tutorial notebooks.

Verdict

Anyone juggling multi-dimensional tensors in research or production should keep this in their toolkit. If you rarely leave NumPy and only ever reshape 2-D matrices, the learning curve is probably steeper than the payoff.

Frequently asked

What is arogozhnikov/einops?
It replaces cryptic tensor reshaping with self-documenting notation that runs identically across NumPy, PyTorch, JAX, and others.
Is einops open source?
Yes — arogozhnikov/einops is open source, released under the MIT license.
What language is einops written in?
arogozhnikov/einops is primarily written in Python.
How popular is einops?
arogozhnikov/einops has 9.6k stars on GitHub.
Where can I find einops?
arogozhnikov/einops is on GitHub at https://github.com/arogozhnikov/einops.

heatdrop uses Google Analytics to see which pages get read — nothing else. Your call. How we handle data.