Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Building inside docker container #293

Merged
merged 28 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions .github/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Use the official Ubuntu 22.04 as a parent image
FROM ubuntu:22.04
SHELL ["/bin/bash", "-c"]

# Set environment variables
ENV DEBIAN_FRONTEND=noninteractive
ENV TTMLIR_TOOLCHAIN_DIR=/opt/ttmlir-toolchain

# Install dependencies
RUN apt-get update && apt-get install -y \
software-properties-common \
build-essential \
python3-dev \
python3-venv \
python3-pip \
git \
git-lfs \
libhwloc-dev \
pandoc \
libtbb-dev \
libcapstone-dev \
pkg-config \
linux-tools-generic \
ninja-build \
wget \
libgtest-dev \
cmake \
ccache \
doxygen \
graphviz \
patchelf \
libyaml-cpp-dev \
libboost-all-dev

# Install clang 17
RUN wget https://apt.llvm.org/llvm.sh && \
chmod u+x llvm.sh && \
./llvm.sh 17 && \
apt install -y libc++-17-dev libc++abi-17-dev && \
ln -s /usr/bin/clang-17 /usr/bin/clang && \
ln -s /usr/bin/clang++-17 /usr/bin/clang++

# Install python packages
RUN pip install cmake

# Googletest latest version (which isn't in apt for 20.04)
vmilosevic marked this conversation as resolved.
Show resolved Hide resolved
RUN git clone https://github.com/google/googletest.git -b release-1.12.1 && cd googletest && mkdir build && cd build && cmake .. -DBUILD_GMOCK=OFF && make && make install && cd ../.. && rm -rf googletest

# Set the user for RUN commands
ARG BUILD_DIR=builder
RUN mkdir -p $BUILD_DIR

# Create and change ownership of toolchain dir
RUN mkdir -p $TTMLIR_TOOLCHAIN_DIR

# Clone the project and update submodules
RUN git clone https://github.com/tenstorrent/tt-mlir.git $BUILD_DIR/tt-mlir && \
cd $BUILD_DIR/tt-mlir && \
git submodule update --init --recursive -f

# Build the toolchain
WORKDIR $BUILD_DIR/tt-mlir
RUN cmake -B env/build env && \
cmake --build env/build

# Build project to test the container
RUN source env/activate && \
cmake -G Ninja \
-B build \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_C_COMPILER=clang \
-DCMAKE_CXX_COMPILER=clang++ \
-DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
-DTTMLIR_ENABLE_RUNTIME=ON \
-DTTMLIR_ENABLE_RUNTIME_TESTS=ON && \
cmake --build build --config Release

# Run clang-tidy
RUN cmake --build build -- clang-tidy || true

# Run the tests
RUN cmake --build build -- check-ttmlir || true

# Clean up the build directory
RUN rm -rf $BUILD_DIR/tt-mlir
37 changes: 37 additions & 0 deletions .github/workflows/build-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Build and Publish Docker Image

on:
workflow_dispatch:
workflow_call:

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
sparse-checkout: |
.github/Dockerfile
sparse-checkout-cone-mode: false

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: .github
file: .github/Dockerfile
push: true
tags: |
ghcr.io/${{ github.repository }}/tt-mlir-ubuntu-22-04:${{ github.sha }}
ghcr.io/${{ github.repository }}/tt-mlir-ubuntu-22-04:latest
84 changes: 84 additions & 0 deletions .github/workflows/docker-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
name: Build in Docker

on:
workflow_dispatch:
workflow_call:

jobs:

build-and-test:

strategy:
fail-fast: false
matrix:
image: ["ubuntu-22-04"]
build: [
{runs-on: ubuntu-latest, build_type: Release, enable_runtime: OFF},
{runs-on: self-hosted, build_type: Release, enable_runtime: ON},
]

runs-on: ${{ matrix.build.runs-on }}

container:
image: ghcr.io/${{ github.repository }}/tt-mlir-${{ matrix.image }}:latest
options: --user root

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history and tags

- name: Set reusable strings
id: strings
shell: bash
run: |
echo "work-dir=$(pwd)" >> "$GITHUB_OUTPUT"
echo "build-output-dir=$(pwd)/build" >> "$GITHUB_OUTPUT"

- name: Git safe dir
run: git config --global --add safe.directory ${{ steps.strings.outputs.work-dir }}

- name: Configure CMake
shell: bash
run: |
source env/activate
cmake -G Ninja \
-B ${{ steps.strings.outputs.build-output-dir }} \
-DCMAKE_CXX_COMPILER=clang++ \
-DCMAKE_C_COMPILER=clang \
-DCMAKE_BUILD_TYPE=${{ matrix.build.build_type }} \
-DTTMLIR_ENABLE_RUNTIME=${{ matrix.build.enable_runtime }} \
-DTTMLIR_ENABLE_RUNTIME_TESTS=${{ matrix.build.enable_runtime }} \
-S ${{ steps.strings.outputs.work-dir }}

- name: Build
shell: bash
run: |
source env/activate
cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build.build_type }}

- name: Lint
shell: bash
run: |
source env/activate
cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build.build_type }} -- clang-tidy
continue-on-error: true

- name: Run Test
shell: bash
run: |
source env/activate
cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build.build_type }} -- check-ttmlir

- name: Upload Test Report
uses: actions/upload-artifact@v4
with:
name: test-reports-${{ matrix.build.runs-on }}
path: build/test/report.xml

- name: Show Test Report
uses: mikepenz/action-junit-report@v4
if: success() || failure()
with:
report_paths: build/test/report.xml
check_name: MLIR Tests
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build
name: Build on macos-latest

on:
workflow_dispatch:
Expand All @@ -13,7 +13,6 @@ jobs:
fail-fast: false
matrix:
build: [
{runs-on: ubuntu-20.04, c_compiler: clang, cpp_compiler: clang++, build_type: Release, enable_runtime: ON},
{runs-on: macos-latest, c_compiler: clang, cpp_compiler: clang++, build_type: Release, enable_runtime: OFF}
]
runs-on: ${{ matrix.build.runs-on }}
Expand Down
7 changes: 5 additions & 2 deletions .github/workflows/on-pr-and-push-to-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ jobs:
spdx:
uses: ./.github/workflows/spdx.yml
secrets: inherit
build:
uses: ./.github/workflows/build.yml
macos-build:
uses: ./.github/workflows/macos-build.yml
secrets: inherit
docker-build:
uses: ./.github/workflows/docker-build.yml
secrets: inherit
63 changes: 60 additions & 3 deletions docs/src/internal-build.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,66 @@
# Internal Build Notes / IRD

- When building the runtime we must use Ubuntu 20.04 docker image
- When building the runtime we must use Ubuntu 22.04 docker image
- When making an IRD reservation use `--docker-image
yyz-gitlab.local.tenstorrent.com:5005/tenstorrent/infra/ird-ubuntu-20-04-amd64:latest`
- You'll have to manaully install a newer version of cmake, at least 3.20, the easiest way to do this is to `pip install cmake` and make sure this one is in your path
yyz-gitlab.local.tenstorrent.com:5005/tenstorrent/infra/ird-ubuntu-22-04-amd64:latest`
- You'll have to manaully install a newer version of cmake, at least 3.22, the easiest way to do this is to `pip install cmake` and make sure this one is in your path
- You'll want LLVM installation to persist IRD reservations, you can achieve this by:
- mkdir /localdev/$USER/ttmlir-toolchain
- When requesting an IRD use `--volumes /localdev/$USER/ttmlir-toolchain:/opt/ttmlir-toolchain`

## Working with Docker Images

Components:
- Dockerfile
- Workflow for building Docker image
- Project build using Docker image

Overview:

The [Dockerfile](.github/Dockerfile) describes how to create an image for building the tt-mlir project file. It starts with a supported base image (Ubuntu 22.04) and installs the necessary packages. The purpose of the Docker build is to:

- Set up build dependencies
- Prepare the tt-mlir toolchain

During the Docker build, the project is built and tests are run to ensure that everything is set up correctly. If any dependencies are missing, the Docker build will fail.

This process also prepopulates caches for Python packages and the ccache cache in the image, which should make subsequent builds faster.

### Building the Docker Image using GitHub Actions

The GitHub Actions workflow [Build and Publish Docker Image](.github/workflows/build-image.yml) builds the Docker image and uploads it to GitHub Packages at https://github.com/orgs/tenstorrent/packages?repo_name=tt-mlir. The image name is tt-mlir-ubuntu-22-04, and we use the git SHA we build from as the tag.

### Building the Docker Image Locally

To test the changes and build the image locally, use the following command:
```bash
docker build -f .github/Dockerfile -t tt-mlir-ubuntu-22-04:latest .
```

### Pushing the Docker Image to GitHub

Images built locally can be pushed to GitHub. First, we need to generate a PAT token with the "write:packages" access enabled. Go to GitHub -> Settings -> Developer settings -> Personal access tokens -> Generate new token.

Authenticate with GitHub Container Registry:
```bash
echo "<my-github-pat>" | docker login ghcr.io -u <username> --password-stdin
```

Add a tag to the built image:
```bash
docker tag tt-mlir-ubuntu-22-04:latest ghcr.io/tenstorrent/tt-mlir/tt-mlir-ubuntu-22-04:latest
```

Push the image:
```bash
docker push ghcr.io/tenstorrent/tt-mlir/tt-mlir-ubuntu-22-04:latest
```

### Using the Image in GitHub Actions Jobs

The GitHub Actions workflow [Build in Docker](.github/workflows/docker-build.yml) uses a Docker container for building:
```yaml
container:
image: ghcr.io/${{ github.repository }}/tt-mlir-ubuntu-22-04:latest
options: --user root
```
Loading