Skip to content

Commit 9c8b962

Browse files
Dockerize and Cache Bazel {Local, CI} Builds (#1240)
This PR adds: - A minimal docker wrapper to the bazel GHA workflow to make it reproducible locally - Bazel cache to speed up GHA workflows (down to ~5 minutes from ~40+minutes) This is a no-op for non-bazel workflows and an incremental improvement.
1 parent 9be8997 commit 9c8b962

File tree

6 files changed

+83
-17
lines changed

6 files changed

+83
-17
lines changed

.github/workflows/bazelBuildAndTest.yml

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,38 @@ jobs:
2020
runs-on: ubuntu-22.04
2121

2222
steps:
23-
- name: Set up Python
24-
uses: actions/setup-python@v2
25-
with:
26-
python-version: 3.9
27-
2823
- name: Checkout torch-mlir
29-
uses: actions/checkout@v2
24+
uses: actions/checkout@v3
3025
with:
3126
submodules: 'true'
3227

33-
- name: Build with bazel
28+
- name: Setup cache for bazel
29+
uses: actions/cache@v3
30+
with:
31+
path: ~/.cache/bazel
32+
key: ubuntu_x86_64_torch_mlir_bazel_build_cache
33+
34+
- name: Set bazel cache permissions
35+
run: |
36+
sudo chown -R root:root "${HOME}/.cache/bazel"
37+
38+
- name: Build docker image
39+
run: |
40+
docker build -f utils/bazel/docker/Dockerfile \
41+
-t torch-mlir:ci \
42+
.
43+
44+
- name: Bazel build torch-mlir
45+
run: |
46+
docker run --rm \
47+
-v "$(pwd)":"/opt/src/torch-mlir" \
48+
-v "${HOME}/.cache/bazel":"/root/.cache/bazel" \
49+
torch-mlir:ci \
50+
./utils/bazel/docker/run_bazel_build.sh
51+
52+
- name: Switch bazel cache permissions
3453
run: |
35-
cd $GITHUB_WORKSPACE/utils/bazel
36-
bazel build @torch-mlir//...
54+
sudo chown -R "$USER":"$USER" "${HOME}/.cache/bazel"
3755
3856
- name: Send mail
3957
if: failure()

development.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,15 @@ manually `source`'d in a shell.
175175
176176
Torch-MLIR can also be built using Bazel (apart from the official CMake build) for users that depend on Bazel in their workflows. To build `torch-mlir-opt` using Bazel, follow these steps:
177177

178-
1. Install [Bazel](https://docs.bazel.build/versions/main/install.html) if you don't already have it
179-
2. Install a relatively new release of [Clang](https://releases.llvm.org/download.html)
180-
3. Build:
178+
1. Launch an interactive docker container with the required deps installed:
181179
```shell
182-
cd utils/bazel
183-
bazel build @torch-mlir//...
180+
./utils/bazel/docker/run_docker.sh
184181
```
185-
4. Find the built binary at `bazel-bin/external/torch-mlir/torch-mlir-opt`.
182+
2. Build torch-mlir using bazel (from container):
183+
```shell
184+
./utils/bazel/docker/run_bazel_build.sh
185+
```
186+
3. Find the built binary at `utils/bazel/bazel-bin/external/torch-mlir/torch-mlir-opt`.
186187

187188

188189
# Testing

utils/bazel/.bazelrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
build --action_env=CC=clang
66
build --action_env=CXX=clang++
7-
build --cxxopt=-std=c++17
8-
build --host_cxxopt=-std=c++17
7+
build --cxxopt=-std=c++17
8+
build --host_cxxopt=-std=c++17
99
build --cxxopt=-D_GLIBCXX_USE_CXX11_ABI=0
1010
build --cxxopt=-U__GXX_ABI_VERSION
1111
build --cxxopt=-D__GXX_ABI_VERSION=1011

utils/bazel/docker/Dockerfile

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
ARG BASE_IMG=ubuntu:18.04
2+
FROM ${BASE_IMG} as dev-base
3+
4+
ARG ARCH="x86_64"
5+
ARG BAZEL_VERSION=5.2.0
6+
7+
# Install basic packages
8+
RUN apt-get update && \
9+
apt-get install -y \
10+
clang-10 \
11+
curl \
12+
git \
13+
python3-pip \
14+
python3.8 \
15+
python3.8-dev \
16+
wget \
17+
unzip
18+
19+
RUN update-alternatives --install /usr/bin/python python /usr/bin/python3.8 10
20+
RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 10
21+
22+
RUN update-alternatives --install /usr/bin/clang clang /usr/bin/clang-10 10
23+
RUN update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-10 10
24+
25+
# Install bazel
26+
RUN wget -q https://github.com/bazelbuild/bazel/releases/download/${BAZEL_VERSION}/bazel-${BAZEL_VERSION}-linux-${ARCH} -O /usr/bin/bazel \
27+
&& chmod a+x /usr/bin/bazel
28+
29+
COPY requirements.txt /opt/app/requirements.txt
30+
WORKDIR /opt/app
31+
RUN python -m pip install --upgrade pip
32+
RUN python -m pip install --ignore-installed -r requirements.txt
33+
34+
WORKDIR /opt/src/torch-mlir

utils/bazel/docker/run_bazel_build.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
3+
cd "$(pwd)/utils/bazel" && bazel build @torch-mlir//...

utils/bazel/docker/run_docker.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
3+
docker build -f utils/bazel/docker/Dockerfile \
4+
-t torch-mlir:dev \
5+
.
6+
7+
docker run -it \
8+
-v "$(pwd)":"/opt/src/torch-mlir" \
9+
-v "${HOME}/.cache/bazel":"/root/.cache/bazel" \
10+
torch-mlir:dev

0 commit comments

Comments
 (0)