astral-sh / ruff

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

PyTorch-specific rules #12349

Open sbrugman opened 3 months ago

sbrugman commented 3 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 3 months ago

Also #9664.

njzjz commented 3 months ago

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

sbrugman commented 3 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 3 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 2 weeks 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.