Skip to content

Commit

Permalink
[CI] Add clang-format (mlc-ai#1103)
Browse files Browse the repository at this point in the history
  • Loading branch information
junrushao authored Oct 22, 2023
1 parent 46d11e6 commit 6159cc4
Show file tree
Hide file tree
Showing 13 changed files with 262 additions and 60 deletions.
42 changes: 32 additions & 10 deletions .github/workflows/python_lint.yml → .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
name: Python Lint
name: Lint
on: [push, pull_request]
env:
IMAGE: 'mlcaidev/ci-cpu:2c03e7f'
IMAGE: 'mlcaidev/ci-cpu:caab922'

jobs:
isort:
name: Python / isort
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
submodules: 'recursive'
submodules: ''
- name: Version
run: |
wget https://raw.githubusercontent.com/mlc-ai/package/main/docker/bash.sh -O ./ci/bash.sh
Expand All @@ -20,11 +21,12 @@ jobs:
./ci/bash.sh $IMAGE bash ./ci/task/isort.sh
black:
name: Python / black
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
submodules: 'recursive'
submodules: ''
- name: Version
run: |
wget https://raw.githubusercontent.com/mlc-ai/package/main/docker/bash.sh -O ./ci/bash.sh
Expand All @@ -35,11 +37,12 @@ jobs:
./ci/bash.sh $IMAGE bash ./ci/task/black.sh
mypy:
name: Python / mypy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
submodules: 'recursive'
submodules: ''
- name: Version
run: |
wget https://raw.githubusercontent.com/mlc-ai/package/main/docker/bash.sh -O ./ci/bash.sh
Expand All @@ -50,11 +53,12 @@ jobs:
./ci/bash.sh $IMAGE bash ./ci/task/mypy.sh
pylint:
name: Python / pylint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
submodules: 'recursive'
submodules: ''
- name: Version
run: |
wget https://raw.githubusercontent.com/mlc-ai/package/main/docker/bash.sh -O ./ci/bash.sh
Expand All @@ -63,3 +67,21 @@ jobs:
- name: Lint
run: |
./ci/bash.sh $IMAGE bash ./ci/task/pylint.sh
clang-format:
name: C++ / clang-format
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: ''
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 0
- name: Version
run: |
wget https://raw.githubusercontent.com/mlc-ai/package/main/docker/bash.sh -O ./ci/bash.sh
chmod u+x ./ci/bash.sh
./ci/bash.sh $IMAGE "conda env export --name ci-lint"
- name: Lint
run: |
./ci/bash.sh $IMAGE bash ./ci/task/clang-format.sh
3 changes: 1 addition & 2 deletions android/src/cpp/tvm_runtime.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#define DMLC_USE_LOGGING_LIBRARY <tvm/runtime/logging.h>
#define TVM_USE_LIBBACKTRACE 0

#include <android/log.h>
#include <dlfcn.h>
#include <dmlc/logging.h>
#include <dmlc/thread_local.h>
#include <tvm/runtime/c_runtime_api.h>

#include <android/log.h>

static_assert(TVM_LOG_CUSTOMIZE == 1, "TVM_LOG_CUSTOMIZE must be 1");

namespace tvm {
Expand Down
91 changes: 91 additions & 0 deletions ci/bash.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env bash

#
# Start a bash, mount /workspace to be current directory.
#
# Usage: docker/bash.sh <CONTAINER_NAME>
# Starts an interactive session
#
# Usage2: docker/bash.sh <CONTAINER_NAME> [COMMAND]
# Execute command in the docker image, non-interactive
#
if [ "$#" -lt 1 ]; then
echo "Usage: docker/bash.sh <CONTAINER_NAME> [--no-gpu] [COMMAND]"
exit -1
fi

if [ "$1" == "--no-gpu" ]; then
ENABLE_NV_DOCKER=0
shift
else
ENABLE_NV_DOCKER=1
fi

DOCKER_IMAGE_NAME=("$1")


if [ "$#" -eq 1 ]; then
COMMAND="bash"
if [[ $(uname) == "Darwin" ]]; then
# Docker's host networking driver isn't supported on macOS.
# Use default bridge network and expose port for jupyter notebook.
DOCKER_EXTRA_PARAMS=("-it -p 8888:8888")
else
DOCKER_EXTRA_PARAMS=("-it --net=host")
fi
else
shift 1
COMMAND=("$@")
fi

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
WORKSPACE="$(pwd)"

# Use nvidia-docker if the container is GPU.
if [[ ! -z $CUDA_VISIBLE_DEVICES ]]; then
CUDA_ENV="-e CUDA_VISIBLE_DEVICES=${CUDA_VISIBLE_DEVICES}"
else
CUDA_ENV=""
fi

# If this is an wheel test command then pass the env var to docker.
if [[ ! -z $WHEEL_TEST ]]; then
WHEEL_TEST="-e WHEEL_TEST=${WHEEL_TEST}"
fi

if [[ "${DOCKER_IMAGE_NAME}" == *"cu"* ]]; then
if [ "$ENABLE_NV_DOCKER" -eq 1 ]; then
if ! type "nvidia-docker" 1> /dev/null 2> /dev/null
then
DOCKER_BINARY="docker"
CUDA_ENV=" --gpus all "${CUDA_ENV}
else
DOCKER_BINARY="nvidia-docker"
fi
else
DOCKER_BINARY="docker"
fi
else
DOCKER_BINARY="docker"
fi

# Print arguments.
echo "WORKSPACE: ${WORKSPACE}"
echo "DOCKER CONTAINER NAME: ${DOCKER_IMAGE_NAME}"
echo ""

echo "Running '${COMMAND[@]}' inside ${DOCKER_IMAGE_NAME}..."

# By default we cleanup - remove the container once it finish running (--rm)
# and share the PID namespace (--pid=host) so the process inside does not have
# pid 1 and SIGKILL is propagated to the process inside (jenkins can kill it).

${DOCKER_BINARY} run --rm --pid=host\
-v ${WORKSPACE}:/workspace \
-v ${SCRIPT_DIR}:/docker \
-w /workspace \
${CUDA_ENV} \
${WHEEL_TEST} \
${DOCKER_EXTRA_PARAMS[@]} \
${DOCKER_IMAGE_NAME} \
${COMMAND[@]}
7 changes: 5 additions & 2 deletions ci/task/black.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ micromamba activate ci-lint
export NUM_THREADS=$(nproc)
export PYTHONPATH="./python:$PYTHONPATH"

black --check --workers $NUM_THREADS ./python/
black --check --workers $NUM_THREADS ./tests/python
set -x

black --check --workers $NUM_THREADS \
./python/ \
./tests/python
67 changes: 67 additions & 0 deletions ci/task/clang-format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/bin/bash
set -eo pipefail

source ~/.bashrc
micromamba activate ci-lint
export NUM_THREADS=$(nproc)
export PYTHONPATH="./python:$PYTHONPATH"

set -x
git config --global --add safe.directory '*'

INPLACE_FORMAT=${INPLACE_FORMAT:=false}
LINT_ALL_FILES=true
REVISION=$(git rev-list --max-parents=0 HEAD)

while (($#)); do
case "$1" in
-i)
INPLACE_FORMAT=true
shift 1
;;
--rev)
LINT_ALL_FILES=false
REVISION=$2
shift 2
;;
*)
echo "Usage: clang-format.sh [-i] [--rev <commit>]"
echo ""
echo "Run clang-format on files that changed since <commit> or on all files in the repo"
echo "Examples:"
echo "- Compare last one commit: clang-format.sh --rev HEAD~1"
echo "- Compare against upstream/main: clang-format.sh --rev upstream/main"
echo "The -i will format files in-place instead of checking them."
exit 1
;;
esac
done

cleanup() {
if [ -f /tmp/$$.clang-format.txt ]; then
echo ""
echo "---------clang-format log----------"
cat /tmp/$$.clang-format.txt
fi
rm -rf /tmp/$$.clang-format.txt
}
trap cleanup 0

if [[ "$INPLACE_FORMAT" == "true" ]]; then
echo "Running inplace git-clang-format against $REVISION"
git-clang-format --extensions h,hh,hpp,c,cc,cpp,mm "$REVISION"
exit 0
fi

if [[ "$LINT_ALL_FILES" == "true" ]]; then
echo "Running git-clang-format against all C++ files"
git-clang-format --diff --extensions h,hh,hpp,c,cc,cpp,mm "$REVISION" 1>/tmp/$$.clang-format.txt
else
echo "Running git-clang-format against $REVISION"
git-clang-format --diff --extensions h,hh,hpp,c,cc,cpp,mm "$REVISION" 1>/tmp/$$.clang-format.txt
fi

if grep --quiet -E "diff" </tmp/$$.clang-format.txt; then
echo "clang-format lint error found. Consider running clang-format on these files to fix them."
exit 1
fi
7 changes: 5 additions & 2 deletions ci/task/isort.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ micromamba activate ci-lint
export NUM_THREADS=$(nproc)
export PYTHONPATH="./python:$PYTHONPATH"

isort --check-only -j $NUM_THREADS --profile black ./python/
isort --check-only -j $NUM_THREADS --profile black ./tests/python/
set -x

isort --check-only -j $NUM_THREADS --profile black \
./python/ \
./tests/python/
8 changes: 6 additions & 2 deletions ci/task/mypy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ micromamba activate ci-lint
export NUM_THREADS=$(nproc)
export PYTHONPATH="./python:$PYTHONPATH"

mypy ./python/mlc_chat/compiler ./python/mlc_chat/support
mypy ./tests/python/model ./tests/python/parameter
set -x

mypy ./python/mlc_chat/compiler \
./python/mlc_chat/support \
./tests/python/model \
./tests/python/parameter
2 changes: 2 additions & 0 deletions ci/task/pylint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ micromamba activate ci-lint
export NUM_THREADS=$(nproc)
export PYTHONPATH="./python:$PYTHONPATH"

set -x

# TVM Unity is a dependency to this testing
pip install --quiet --pre -U -f https://mlc.ai/wheels mlc-ai-nightly

Expand Down
Loading

0 comments on commit 6159cc4

Please sign in to comment.