astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.
https://docs.astral.sh/ruff
MIT License
33.29k stars 1.11k forks source link

PyTorch-specific rules #12349

Open sbrugman opened 4 months ago

sbrugman commented 4 months ago

Introducing lints for pytorch expands ruff utility in the AI/machine learning space, in addition to Numpy and Spark. Detecting potential errors during linting has the potential to save costly GPU compute.

randolf-scholz commented 4 months ago

Also #9664.

njzjz commented 4 months ago

There is a flake8 plugin called TorchFix developed by the PyTorch team: https://github.com/pytorch-labs/torchfix

sbrugman commented 4 months ago

There is a flake8 plugin called TorchFix developed by the PyTorch team: https://github.com/pytorch-labs/torchfix

Thanks for sharing! This would be a great base to start from!

NeilGirdhar commented 4 months ago

TOR008 -> Use array agnostic syntax (see also https://github.com/astral-sh/ruff/issues/8615), e.g. use x.mean() rather than torch.mean(x)

I think it would be better to move towards the Array API, which is the most "array agnostic". This eliminates any peculiarities of PyTorch versus NumPy as well. So:

xp = get_namespace(x)
xp.mean(x)

This works with NumPy, PyTorch, Jax, TensorFlow, CuPy, etc.

neosr-project commented 1 month ago

+1 for TorchFix. It integrates with flake8, so it should be easy to port the rules. It has been officially released now with the update to pytorch 2.5.