From fb8748bdfad9a2c8bb2a2e3092055eba13421200 Mon Sep 17 00:00:00 2001 From: Stella Laurenzo Date: Sat, 27 Apr 2024 13:29:51 -0700 Subject: [PATCH] Switch to pre-commit for lint checks. (#3200) Users can run via `pre-commit run` or set up a hook as described in the instructions: https://pre-commit.com/ The CI is set to only run pre-commit on files changed in the patch. We will run with `--all-files` in a separate patch. --- .github/workflows/lint.yml | 26 -------------------------- .github/workflows/pre-commit.yml | 19 +++++++++++++++++++ .pre-commit-config.yaml | 21 +++++++++++++++++++++ .style.yapf | 2 -- README.md | 8 ++++---- docs/development.md | 13 +++++++++++++ pyproject.toml | 7 +++++++ 7 files changed, 64 insertions(+), 32 deletions(-) delete mode 100644 .github/workflows/lint.yml create mode 100644 .github/workflows/pre-commit.yml create mode 100644 .pre-commit-config.yaml delete mode 100644 .style.yapf create mode 100644 pyproject.toml diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml deleted file mode 100644 index 364e9fa9d..000000000 --- a/.github/workflows/lint.yml +++ /dev/null @@ -1,26 +0,0 @@ -# yamllint disable rule:line-length -name: Lint Checks - -on: - push: - branches: [main] - pull_request: - branches: [main] - -jobs: - static_lint_checks: - name: Static Lint Checks - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - with: - # `git-clang-format` needs access to the commit before the - # current merge commit to know what changes to format. - fetch-depth: 2 - - name: Validate GitHub Actions yaml files - run: | - yamllint ./.github/workflows/ ./.github/actions/ - - name: Check clang-format - run: | - wget -q https://raw.githubusercontent.com/llvm/llvm-project/main/clang/tools/clang-format/git-clang-format - python3 git-clang-format --diff HEAD~1 diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml new file mode 100644 index 000000000..3aad792ab --- /dev/null +++ b/.github/workflows/pre-commit.yml @@ -0,0 +1,19 @@ +name: pre-commit + +on: + pull_request: + push: + branches: [main] + +jobs: + pre-commit: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + # requites to grab the history of the PR + fetch-depth: 0 + - uses: actions/setup-python@v3 + - uses: pre-commit/action@v3.0.1 + with: + extra_args: --color=always --from-ref ${{ github.event.pull_request.base.sha }} --to-ref ${{ github.event.pull_request.head.sha }} diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 000000000..b7d58d956 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,21 @@ +# See https://pre-commit.com for more information +# See https://pre-commit.com/hooks.html for more hooks +exclude: "GeneratedTorchOps\\.td|abstract_interp_lib_gen\\.py|\\.excalidraw" +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v3.2.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-ast + - id: check-yaml + - id: check-added-large-files +- repo: https://github.com/psf/black + rev: 22.10.0 + hooks: + - id: black + +- repo: https://github.com/pre-commit/mirrors-clang-format + rev: 'v18.1.4' + hooks: + - id: clang-format diff --git a/.style.yapf b/.style.yapf deleted file mode 100644 index c2ecb9b1a..000000000 --- a/.style.yapf +++ /dev/null @@ -1,2 +0,0 @@ -[style] - based_on_style = pep8 diff --git a/README.md b/README.md index 772962b5f..70268ba72 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# The Torch-MLIR Project +# The Torch-MLIR Project The Torch-MLIR project aims to provide first class compiler support from the [PyTorch](https://pytorch.org) ecosystem to the MLIR ecosystem. @@ -8,7 +8,7 @@ necessarily a reflection of the completeness or stability of the code, it does indicate that the project is not yet endorsed as a component of LLVM. [PyTorch](https://pytorch.org) -PyTorch is an open source machine learning framework that facilitates the seamless transition from research and prototyping to production-level deployment. +PyTorch is an open source machine learning framework that facilitates the seamless transition from research and prototyping to production-level deployment. [MLIR](https://mlir.llvm.org) The MLIR project offers a novel approach for building extensible and reusable compiler architectures, which address the issue of software fragmentation, reduce the cost of developing domain-specific compilers, improve compilation for heterogeneous hardware, and promote compatibility between existing compilers. @@ -16,7 +16,7 @@ The MLIR project offers a novel approach for building extensible and reusable co [Torch-MLIR](https://github.com/llvm/torch-mlir) Several vendors have adopted MLIR as the middle layer in their systems, enabling them to map frameworks such as PyTorch, JAX, and TensorFlow into MLIR and subsequently lower them to their target hardware. We have observed half a dozen custom lowerings from PyTorch to MLIR, making it easier for hardware vendors to focus on their unique value, rather than needing to implement yet another PyTorch frontend for MLIR. The ultimate aim is to be similar to the current hardware vendors adding LLVM target support, rather than each one implementing Clang or a C++ frontend. -[![Release Build](https://github.com/llvm/torch-mlir/actions/workflows/buildRelease.yml/badge.svg)](https://github.com/llvm/torch-mlir/actions/workflows/buildRelease.yml) +[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit)](https://github.com/pre-commit/pre-commit) ## All the roads from PyTorch to Torch MLIR Dialect @@ -76,7 +76,7 @@ pip install torch-mlir -f https://github.com/llvm/torch-mlir-release/releases/ex ## Demos -### TorchScript ResNet18 +### TorchScript ResNet18 Standalone script to Convert a PyTorch ResNet18 model to MLIR and run it on the CPU Backend: diff --git a/docs/development.md b/docs/development.md index 5e9a3f6b8..e1b575b54 100644 --- a/docs/development.md +++ b/docs/development.md @@ -26,6 +26,19 @@ python -m pip install -r requirements.txt python -m pip install -r torchvision-requirements.txt ``` +## (Optional) Set up pre-commit + +This project uses [pre-commit](https://pre-commit.com/) in its CI. You can +install it locally too in order to lint and fix your code prior to the CI +complaining about it. + +```shell +pip install pre-commit +# You can run interactively with `pre-commit run` +# or install hooks so it runs automatically: +pre-commit install +``` + ## CMake Build Two setups are possible to build: in-tree and out-of-tree. The in-tree setup is the most straightforward, as it will build LLVM dependencies as well. diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 000000000..9be81bffa --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,7 @@ +[build-system] +requires = ["setuptools", "wheel"] +build-backend = "setuptools.build_meta" + +[tool.black] +line-length = 88 +target-version = ['py38']