Skip to content

carrascomj/shapels

Repository files navigation

shapels

jaxtyping + LSP.

shapels provides shape inference for torch tensors inside your editor.

showcase of shapels inside helix

But what does that mean? In the example above, shapels tracks the tensor dimensions and is able to report the wrong matrix multiplication that would have caused a runtime error.

Not only that: when hovering over a tensor, it reports the shape of that tensor through static analysis, even if no annotation was provided. It's effectively replacing

print(f"x -> {x.shape}")

but before running any code, in your editor of choice.

Installation

If you are not using VSCode, go to the latest release page, download the binary for your platform, unzip it and put it in your path.

shapels's VScode extension comes with the binaries prebundled so this step is not needed.

Installing from source

The first step is to install Rust:

# Unix-like OS
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

After cloning this repository, it can be installed with cargo:

git clone https://github.com/carrascomj/shapels.git
cd shapels
cargo install --path .

Editor support

Just install it from the extensions marketplace, it comes with the shapels binary prebundled for your platform.

But why don't you try neovim?

Add it to your config (you must have shapels in you path):

vim.lsp.config('shapels', {cmd={'shapels'}, root_markers={'pyproject.toml', 'setup.py', 'setup.cfg'}, filetypes={'python'}})
vim.lsp.enable('shapels')

But why don't you try helix?

Add it to your languages.toml (you must have shapels in you path):

[language-server]
shapels = {command = "shapels"}

[[language]]
name = "python"
scope = "source.python"
injection-regex = "py(thon)?"
file-types = ["py", "pyi", "py3", "pyw", "ptl", "rpy", "cpy", "ipy", "pyt", { glob = ".python_history" }, { glob = ".pythonstartup" }, { glob = ".pythonrc" }, { glob = "*SConstruct" }, { glob = "*SConscript" }, { glob = "*sconstruct" }]
shebangs = ["python", "uv"]
roots = ["pyproject.toml", "setup.py", "poetry.lock", "pyrightconfig.json"]
comment-token = "#"
# WARN: probably you have other LS already here, just add shapels to the list
language-servers = ["shapels"]
indent = { tab-width = 4, unit = "    " }  

But why don't you try Emacs?

It's a language server: it takes input from stdin and sends output to stdout; you probably know how to handle that.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

About

shape inference for torch in your editor

Topics

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Packages

No packages published