Flexible and powerful tensor operations for readable and reliable code.
Supports numpy, pytorch, tensorflow, jax, and others.
Recent updates:
- 0.8.0: tinygrad backend added, small fixes
- 0.7.0: no-hassle torch.compile, support of array api standard and more
- 10'000🎉: github reports that more than 10k project use einops
- einops 0.6.1: paddle backend added
- einops 0.6 introduces packing and unpacking
- einops 0.5: einsum is now a part of einops
- Einops paper is accepted for oral presentation at ICLR 2022 (yes, it worth reading). Talk recordings are available
- flax and oneflow backend added
- torch.jit.script is supported for pytorch layers
- powerful EinMix added to einops. Einmix tutorial notebook
Tweets
In case you need convincing arguments for setting aside time to learn about einsum and einops... Tim Rocktäschel
Writing better code with PyTorch and einops 👌 Andrej Karpathy
Slowly but surely, einops is seeping in to every nook and cranny of my code. If you find yourself shuffling around bazillion dimensional tensors, this might change your life Nasim Rahaman
Contents
- Installation
- Documentation
- Tutorial
- API micro-reference
- Why use einops
- Supported frameworks
- Citing
- Repository and discussions
Installation
Plain and simple:
Tutorials
Tutorials are the most convenient way to see einops in action
- part 1: einops fundamentals
- part 2: einops for deep learning
- part 3: packing and unpacking
- part 4: improve pytorch code with einops
Kapil Sachdeva recorded a small intro to einops.
API
einops has a minimalistic yet powerful API.
Three core operations provided (einops tutorial shows those cover stacking, reshape, transposition, squeeze/unsqueeze, repeat, tile, concatenate, view and numerous reductions)
Later additions to the family are pack and unpack functions (better than stack/split/concatenate):
Finally, einops provides einsum with a support of multi-lettered names:
EinMix
EinMix is a generic linear layer, perfect for MLP Mixers and similar architectures.
Layers
Einops provides layers (einops keeps a separate version for each framework) that reflect corresponding functions
Example given for pytorch, but code in other frameworks is almost identical
No more flatten needed!
Additionally, torch layers as those are script-able and compile-able. Operations are torch.compile-able, but not script-able due to limitations of torch.jit.script.
Naming
einops stands for Einstein-Inspired Notation for operations (though "Einstein operations" is more attractive and easier to remember).
Notation was loosely inspired by Einstein summation (in particular by numpy.einsum operation).
Why use einops notation?!
Semantic information (being verbose in expectations)
While these two lines are doing the same job in some context, the second one provides information about the input and output. In other words, einops focuses on interface: what is the input and output, not how the output is computed.
The next operation looks similar:
but it gives the reader a hint: this is not an independent batch of images we are processing, but rather a sequence (video).
Semantic information makes the code easier to read and maintain.
Convenient checks
Reconsider the same example:
The second line checks that the input has four dimensions, but you can also specify particular dimensions. That's opposed to just writing comments about shapes since comments don't prevent mistakes, not tested, and without code review tend to be outdated
Result is strictly determined
Below we have at least two ways to define the depth-to-space operation
There are at least four more ways to do it. Which one is used by the framework?
These details are ignored, since usually it makes no difference, but it can make a big difference (e.g. if you use grouped convolutions in the next stage), and you'd like to specify this in your code.
Uniformity
These examples demonstrated that we don't use separate operations for 1d/2d/3d pooling, those are all defined in a uniform way.
Space-to-depth and depth-to space are defined in many frameworks but how about width-to-height? Here you go:
Framework independent behavior
Even simple functions are defined differently by different frameworks
Suppose x's shape was (3, 4, 5), then y has shape ...
- numpy, pytorch, cupy, chainer, jax: (60,)
- keras, tensorflow.layers, gluon: (3, 20)
einops works the same way in all frameworks.
Independence of framework terminology
Example: tile vs repeat causes lots of confusion. To copy image along width:
With einops you don't need to decipher which axis was repeated:
Testimonials provide users' perspective on the same question.
Supported frameworks
Einops works with ...
- numpy
- pytorch
- tensorflow
- jax
- cupy
- flax (community)
- paddle (community)
- oneflow (community)
- tinygrad (community)
- pytensor (community)
Additionally, einops can be used with any framework that supports Python array API standard, which includes
- numpy >= 2.0
- MLX # yes, einops works with apple's framework
- pydata/sparse >= 0.15 # and works with sparse tensors
- cubed # and with distributed tensors too
- quantco/ndonnx
- recent releases of jax and cupy.
- dask is supported via array-api-compat
Development
Devcontainer is provided, this environment can be used locally, or on your server, or within github codespaces. To start with devcontainers in vs code, clone repo, and click 'Reopen in Devcontainer'.
Starting from einops 0.8.1, einops distributes tests as a part of package.
numpy pytorch jax is an example, any subset of testable frameworks can be provided. Every framework is tested against numpy, so it is a requirement for tests.
Specifying --pip-install will install requirements in current virtualenv, and should be omitted if dependencies are installed locally.
To build/test docs:
Citing einops
Please use the following bibtex record
Supported python versions
einops works with python 3.9 or later.