diff --git a/.github/Dockerfile.ci b/.github/Dockerfile.ci index 26f35f82..933af2a9 100644 --- a/.github/Dockerfile.ci +++ b/.github/Dockerfile.ci @@ -1,11 +1,9 @@ -ARG GIT_SHA -ARG FROM_TAG=${GIT_SHA:-latest} +ARG FROM_TAG=latest FROM ghcr.io/tenstorrent/tt-xla/tt-xla-base-ubuntu-22-04:${FROM_TAG} AS ci-build SHELL ["/bin/bash", "-c"] # Create a directory for the build and toolchain -ARG GIT_SHA ENV PROJECT_NAME=tt-xla ENV BUILD_DIR=/home/build ENV TTMLIR_TOOLCHAIN_DIR=/opt/ttmlir-toolchain @@ -18,8 +16,12 @@ RUN mkdir -p $BUILD_DIR && \ # Copy the project from host, cloned in build-image.yml COPY . $BUILD_DIR/$PROJECT_NAME -# Build the toolchain WORKDIR $BUILD_DIR/$PROJECT_NAME + +# Show last commit +RUN git log -1 + +# Build the toolchain RUN cmake -B toolchain -DTOOLCHAIN=ON third_party/ && \ cd third_party/tt-mlir/src/tt-mlir/ && \ source env/activate && \ diff --git a/.github/build-docker-images.sh b/.github/build-docker-images.sh new file mode 100755 index 00000000..d3b05cd0 --- /dev/null +++ b/.github/build-docker-images.sh @@ -0,0 +1,52 @@ +#!/bin/bash +# SPDX-FileCopyrightText: (c) 2024 Tenstorrent AI ULC +# +# SPDX-License-Identifier: Apache-2.0 + +set -e + +REPO=tenstorrent/tt-xla +BASE_IMAGE_NAME=ghcr.io/$REPO/tt-xla-base-ubuntu-22-04 +CI_IMAGE_NAME=ghcr.io/$REPO/tt-xla-ci-ubuntu-22-04 + +# Compute the hash of the Dockerfile +DOCKER_TAG=$(./.github/get-docker-tag.sh) +echo "Docker tag: $DOCKER_TAG" + +# Are we on main branch +ON_MAIN=$(git branch --show-current | grep -q main && echo "true" || echo "false") + +build_and_push() { + local image_name=$1 + local dockerfile=$2 + local on_main=$3 + + + if docker manifest inspect $image_name:$DOCKER_TAG > /dev/null; then + echo "Image $image_name:$DOCKER_TAG already exists" + else + echo "Building image $image_name:$DOCKER_TAG" + docker build \ + --progress=plain \ + --build-arg FROM_TAG=$DOCKER_TAG \ + -t $image_name:$DOCKER_TAG \ + -t $image_name:latest \ + -f $dockerfile . + + echo "Pushing image $image_name:$DOCKER_TAG" + docker push $image_name:$DOCKER_TAG + + # If we are on main branch also push the latest tag + if [ "$on_main" = "true" ]; then + echo "Pushing image $image_name:latest" + docker push $image_name:latest + fi + fi +} + +build_and_push $BASE_IMAGE_NAME .github/Dockerfile.base $ON_MAIN +build_and_push $CI_IMAGE_NAME .github/Dockerfile.ci $ON_MAIN + +echo "All images built and pushed successfully" +echo "CI_IMAGE_NAME:" +echo $CI_IMAGE_NAME:$DOCKER_TAG diff --git a/.github/get-docker-tag.sh b/.github/get-docker-tag.sh new file mode 100755 index 00000000..9ed9f0f7 --- /dev/null +++ b/.github/get-docker-tag.sh @@ -0,0 +1,33 @@ +#!/bin/bash +# SPDX-FileCopyrightText: (c) 2024 Tenstorrent AI ULC +# +# SPDX-License-Identifier: Apache-2.0 + +# Calculate hash for docker image tag. +# The hash is based on the MLIR docker tag and the hash of the Dockerfile(s). + +# Exit immediately if a command exits with a non-zero status +set -e + +# Execute this in a separate bash process +( + # Read tt-mlir version from third_party/CMakeLists.txt and clone third_party/tt-mlir + # Get the MLIR docker tag + TT_MLIR_VERSION=$(grep -oP 'set\(TT_MLIR_VERSION "\K[^"]+' third_party/CMakeLists.txt) + if [ ! -d "third_party/tt-mlir" ]; then + git clone https://github.com/tenstorrent/tt-mlir.git third_party/tt-mlir --quiet + fi + cd third_party/tt-mlir + git fetch --quiet + git checkout $TT_MLIR_VERSION --quiet + if [ -f ".github/get-docker-tag.sh" ]; then + MLIR_DOCKER_TAG=$(.github/get-docker-tag.sh) + else + MLIR_DOCKER_TAG="default-tag" + fi + cd ../.. +) + +DOCKERFILE_HASH_FILES=".github/Dockerfile.base .github/Dockerfile.ci" +DOCKERFILE_HASH=$( (echo $MLIR_DOCKER_TAG; sha256sum $DOCKERFILE_HASH_FILES) | sha256sum | cut -d ' ' -f 1) +echo dt-$DOCKERFILE_HASH diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index da0f9dd6..3767b4a5 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -81,11 +81,41 @@ jobs: # echo "version=$version" >> $GITHUB_ENV # echo $version + build-image: + runs-on: n300 + outputs: + docker-image: ${{ steps.build.outputs.docker-image }} + steps: + - name: Fix permissions + shell: bash + run: sudo chown ubuntu:ubuntu -R $(pwd) + + - name: Checkout repository + uses: actions/checkout@v4 + + - 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 Docker images and output the image name + id: build + shell: bash + run: | + # Output the image name + set pipefail + .github/build-docker-images.sh | tee docker.log + DOCKER_CI_IMAGE=$(tail -n 1 docker.log) + echo "DOCKER_CI_IMAGE $DOCKER_CI_IMAGE" + echo "docker-image=$DOCKER_CI_IMAGE" >> "$GITHUB_OUTPUT" # # Run tests on TT hardware build-and-run-tests: timeout-minutes: 120 + needs: build-image strategy: fail-fast: false matrix: @@ -99,8 +129,8 @@ jobs: - ${{ matrix.build.runs-on }} container: - image: ghcr.io/tenstorrent/tt-xla/tt-xla-ci-ubuntu-22-04:latest - options: --user root --device /dev/tenstorrent/0 + image: ${{ needs.build-image.outputs.docker-image }} + options: --device /dev/tenstorrent/0 volumes: - /dev/hugepages:/dev/hugepages - /dev/hugepages-1G:/dev/hugepages-1G diff --git a/.github/workflows/build-image.yml b/.github/workflows/build-image.yml deleted file mode 100644 index 2583eee3..00000000 --- a/.github/workflows/build-image.yml +++ /dev/null @@ -1,80 +0,0 @@ -name: Build and Publish Docker Image - -on: - workflow_dispatch: - workflow_call: - -jobs: - build: - - runs-on: - - n300 - - env: - BASE_IMAGE_NAME: ghcr.io/${{ github.repository }}/tt-xla-base-ubuntu-22-04 - CI_IMAGE_NAME: ghcr.io/${{ github.repository }}/tt-xla-ci-ubuntu-22-04 - - steps: - - - name: Fix permissions - run: sudo chmod 777 -R $GITHUB_WORKSPACE - - - name: Checkout repository - uses: actions/checkout@v4 - with: - submodules: recursive - lfs: true - - - 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 }} - - # Build images - - - name: Build and export base Docker image - uses: docker/build-push-action@v6 - with: - file: .github/Dockerfile.base - push: true - build-args: | - GIT_SHA=${{ github.sha }} - tags: | - ${{ env.BASE_IMAGE_NAME}}:${{ github.sha }} - - - name: Build and export CI Docker image - uses: docker/build-push-action@v6 - with: - file: .github/Dockerfile.ci - push: true - build-args: | - GIT_SHA=${{ github.sha }} - tags: | - ${{ env.CI_IMAGE_NAME}}:${{ github.sha }} - - # Tag images as latest - - - name: Build and push base Docker image - uses: docker/build-push-action@v6 - with: - file: .github/Dockerfile.base - push: true - build-args: | - GIT_SHA=${{ github.sha }} - tags: | - ${{ env.BASE_IMAGE_NAME}}:latest - - - name: Build and push CI Docker image - uses: docker/build-push-action@v6 - with: - file: .github/Dockerfile.ci - push: true - build-args: | - GIT_SHA=${{ github.sha }} - tags: | - ${{ env.CI_IMAGE_NAME}}:latest