Skip to main content

Linter for PyTorch Tensor operations

Project description


Pylint plugin to check for PyTorch tensor type annotations

Tested on Python 3.6+

Branch Status


pip install pylint pylint_tta

# Use pylint defaults with the plugin
pylint --load-plugins=pylint_tta <directory>

# Only use the plugin
pylint --load-plugins=pylint_tta --disable=all --enable=C9001,C9002,C9003,C9004,C9005,C9006 <directory>

What it checks for

Given file,

import astroid
import torch

def fail_func():
    a = torch.randn(5, 5, 5)

    b_fail = a.view(5, 25, -1)
    c_fail = a[:, : 1]
    c_fail: str = a[:, : 1]
    d_fail: str = a.unsqueeze(1)
    e_fail = a.unsqueeze_(1)

    return None

def pass_func():
    a = torch.randn(5, 5, 5)

    c_pass: ('w', 'h', 'b') = a[:, :, 1]
    d_pass: ('width', 'batch', 'height') = a.unsqueeze(1)

    return None

pylint-tta will yield:

************* Module example1 C9005: In-place operation detected (disallow-inplace-tensor-operations) C9001: Missing type annotations on tensor operation (missing-tensor-annotations) C9003: Missing type annotations on slicing (missing-slicing-annotations) C9004: Invalid type annotations on slicing (invalid-slicing-annotations) C9002: Non tuple type annotations on tensor operation (invalid-tensor-annotations) C9006: Assignment on in-place operation detected (assignment-on-inplace-operations)


Code Name
C9001 missing-tensor-annotations
C9002 invalid-tensor-annotations
C9003 missing-slicing-annotations
C9004 invalid-slicing-annotations
C9005 disallow-inplace-tensor-operations
C9006 assignment-on-inplace-operations


Run Example

export PYTHONPATH=`pwd`; pylint --load-plugins=pylint_tta --disable=all --enable=C9001,C9002,C9003,C9004


pytest -s

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pylint_tta-0.0.4a0.tar.gz (3.3 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page