GithubHelp home page GithubHelp logo

lgeiger / mypy-einsum Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 21 KB

MyPy Type Checking for NumPy/Jax/PyTorch Einsum Operations

License: MIT License

Python 100.00%
einsum jax numpy pytorch deep-learning mypy mypy-plugins

mypy-einsum's Introduction

MyPy Type Checking for NumPy/Jax/PyTorch Einsum Operations

mypy_einsum is a Mypy plugin for type checking np.einsum, jax.numpy.einsum, and torch.einsum operations.

The Einstein summation convention can be used to compute many multi-dimensional, linear algebraic array operations. einsum provides a succinct way of representing these.

However, since einsum equations are passed as a string, it is very easy to overlook typos or other bugs as linters are unable to help. mypy_einsum is a Mypy plugin that that is able to statically verify the correctness of einsum equations with needing to execute the code.

Installation

mypy_einsum can be installed with pip:

pip install mypy-einsum

Setup

To enable the plugin, add it to you projects Mypy configuration file. Usually mypy.ini:

[mypy]
plugins = mypy_einsum

or pyproject.toml:

[tool.mypy]
plugins = ["mypy_einsum"]

Example

Can you spot the ๐Ÿ› without running the code?

import numpy as np

a = np.arange(9).reshape(3, 3)

np.einsum("ik,kj->ij", a)

mypy_einsum will catch it for you:

โฏ mypy example.py --pretty
example.py:5: error: Number of einsum subscripts must be equal to the
number of operands.  [einsum]
    np.einsum("ik,kj->ij", a)
              ^~~~~~~~~~~
Found 1 error in 1 file (checked 1 source file)

After fixing it mypy will succeed ๐ŸŽ‰:

np.einsum("ik,kj->ij", a, a)
โฏ mypy example.py
Success: no issues found in 1 source file

Supported Operations

Reporting Issues and Contributing

mypy_einsum aims to never raise warnings for valid einsum operations. If you encounter a warning that you believe is incorrect, or think mypy_einsum is not reporting an error please let us know. Contributions are very welcome!

mypy-einsum's People

Contributors

lgeiger avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

mypy-einsum's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.