|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Copyright 2026 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +set -e |
| 18 | + |
| 19 | +# Sequence of checks: |
| 20 | +# - Non-PR / Diff error: Run all suites (fail-open) |
| 21 | +# - Docs only: Skip all tests and notebooks |
| 22 | +# - Notebooks only: Run notebooks only |
| 23 | +# - Core CI & dependencies: Run all suites |
| 24 | +# - GPU only: Run GPU tests only (no notebooks, as notebooks run on TPU) |
| 25 | +# - Pathways only: Run Pathways tests only |
| 26 | +# - Post-training only: Run post-training tests and notebooks only |
| 27 | +# - General inference only: Run pretrain TPU/CPU suites only |
| 28 | +# - Default fallback: Run all suites (fail-open for core/shared code) |
| 29 | + |
| 30 | +# Helper to output a key-value flag to GITHUB_OUTPUT (if set) and stdout |
| 31 | +emit_flag() { |
| 32 | + local key="$1" |
| 33 | + local val="$2" |
| 34 | + echo "$key=$val" |
| 35 | + if [[ -n "$GITHUB_OUTPUT" ]]; then |
| 36 | + echo "$key=$val" >> "$GITHUB_OUTPUT" |
| 37 | + fi |
| 38 | +} |
| 39 | + |
| 40 | +# Helper to set all flags to 'true' or 'false' |
| 41 | +set_all_flags() { |
| 42 | + local val="$1" |
| 43 | + for flag in run_tests run_notebooks run_pretrain_tests run_posttrain_tests run_pathways_tests run_gpu_tests; do |
| 44 | + emit_flag "$flag" "$val" |
| 45 | + done |
| 46 | +} |
| 47 | + |
| 48 | +# Helper to enable specific flags and set all others to 'false' |
| 49 | +enable_flags() { |
| 50 | + local enabled=" $* " |
| 51 | + for flag in run_tests run_notebooks run_pretrain_tests run_posttrain_tests run_pathways_tests run_gpu_tests; do |
| 52 | + if [[ "$enabled" =~ " $flag " ]]; then |
| 53 | + emit_flag "$flag" "true" |
| 54 | + else |
| 55 | + emit_flag "$flag" "false" |
| 56 | + fi |
| 57 | + done |
| 58 | +} |
| 59 | + |
| 60 | +# Helper to check if changed files exclusively match a specific domain pattern |
| 61 | +matches_only_domain() { |
| 62 | + local pattern="$1" |
| 63 | + echo "$CHANGED_FILES" | grep -E "$pattern" > /dev/null && \ |
| 64 | + ! echo "$CHANGED_FILES" | grep -v -E "($pattern|\.md$)" > /dev/null |
| 65 | +} |
| 66 | + |
| 67 | +EVENT_NAME="${EVENT_NAME:-${GITHUB_EVENT_NAME:-pull_request}}" |
| 68 | +BASE_REF="${1:-${GITHUB_BASE_REF:-main}}" |
| 69 | + |
| 70 | +if [ "$EVENT_NAME" != "pull_request" ]; then |
| 71 | + echo "Not a pull request (event: $EVENT_NAME), running all tests" |
| 72 | + set_all_flags "true" |
| 73 | + exit 0 |
| 74 | +fi |
| 75 | + |
| 76 | +if ! git rev-parse --verify "origin/$BASE_REF" > /dev/null 2>&1; then |
| 77 | + git fetch origin "$BASE_REF" 2>/dev/null || true |
| 78 | +fi |
| 79 | + |
| 80 | +CHANGED_FILES=$(git diff --name-only "origin/${BASE_REF}...HEAD" 2>/dev/null || true) |
| 81 | + |
| 82 | +echo "Changed files against origin/${BASE_REF}:" |
| 83 | +echo "$CHANGED_FILES" |
| 84 | + |
| 85 | +if [ -z "$CHANGED_FILES" ]; then |
| 86 | + echo "No files detected or diff failed. Running everything as a fail-safe." |
| 87 | + set_all_flags "true" |
| 88 | + exit 0 |
| 89 | +fi |
| 90 | + |
| 91 | +# Documentation only changes |
| 92 | +if ! echo "$CHANGED_FILES" | grep -v -E '\.md$' > /dev/null; then |
| 93 | + echo "Documentation-only files changed, skipping all tests and notebooks." |
| 94 | + set_all_flags "false" |
| 95 | + exit 0 |
| 96 | +fi |
| 97 | + |
| 98 | +# Notebook only changes |
| 99 | +if ! echo "$CHANGED_FILES" | grep -v -E '\.(ipynb|md)$' > /dev/null; then |
| 100 | + echo "Only notebook/doc files changed, running notebooks only." |
| 101 | + enable_flags run_notebooks |
| 102 | + exit 0 |
| 103 | +fi |
| 104 | + |
| 105 | +# Core CI workflows or dependencies changes |
| 106 | +if echo "$CHANGED_FILES" | grep -E '(^|/)(src/dependencies/|\.github/workflows/)' | grep -v -E '(\.github/workflows/run_pathways_tests\.yml)' > /dev/null; then |
| 107 | + echo "Core files (dependencies, workflows) changed, enabling all tests and notebooks." |
| 108 | + set_all_flags "true" |
| 109 | + exit 0 |
| 110 | +fi |
| 111 | + |
| 112 | +# GPU only changes (note: notebooks run on TPU, so GPU changes skip notebooks) |
| 113 | +if matches_only_domain 'src/maxtext/configs/gpu/|src/maxtext/inference/gpu/|tests/end_to_end/gpu/'; then |
| 114 | + echo "Only GPU files changed, enabling GPU tests only." |
| 115 | + enable_flags run_tests run_gpu_tests |
| 116 | + exit 0 |
| 117 | +fi |
| 118 | + |
| 119 | +# Pathways only changes |
| 120 | +if matches_only_domain 'src/maxtext/inference/jetstream_pathways/|\.github/workflows/run_pathways_tests\.yml'; then |
| 121 | + echo "Only Pathways files changed, enabling Pathways tests only." |
| 122 | + enable_flags run_tests run_pathways_tests |
| 123 | + exit 0 |
| 124 | +fi |
| 125 | + |
| 126 | +# Post-training only changes |
| 127 | +if matches_only_domain 'src/maxtext/trainers/post_train/|tests/post_training/'; then |
| 128 | + echo "Only post-training files changed, enabling post-training tests only." |
| 129 | + enable_flags run_tests run_notebooks run_posttrain_tests |
| 130 | + exit 0 |
| 131 | +fi |
| 132 | + |
| 133 | +# General inference only changes |
| 134 | +if matches_only_domain 'src/maxtext/inference/|tests/inference/'; then |
| 135 | + echo "Only inference files changed, enabling pretrain unit/integration tests only." |
| 136 | + enable_flags run_tests run_pretrain_tests |
| 137 | + exit 0 |
| 138 | +fi |
| 139 | + |
| 140 | +# Default fallback: run all domain suites |
| 141 | +echo "General source changes detected, enabling all domain suites." |
| 142 | +set_all_flags "true" |
0 commit comments