|
| 1 | +#!/bin/bash |
| 2 | +############################################################################### |
| 3 | +# Copyright (c) 2025, Advanced Micro Devices, Inc. All rights reserved. |
| 4 | +# |
| 5 | +# See LICENSE for license information. |
| 6 | +############################################################################### |
| 7 | +# |
| 8 | +# Execute hooks based on command group and name |
| 9 | +# Usage: execute_hooks <hook_group> <hook_name> [args...] |
| 10 | +# |
| 11 | + |
| 12 | +# Requires common.sh to be sourced |
| 13 | +if [[ -z "${__PRIMUS_COMMON_SOURCED:-}" ]]; then |
| 14 | + # Fallback logging if common.sh not loaded |
| 15 | + LOG_INFO_RANK0() { |
| 16 | + if [ "${NODE_RANK:-0}" -eq 0 ]; then |
| 17 | + echo "[INFO] $*" |
| 18 | + fi |
| 19 | + } |
| 20 | + LOG_ERROR_RANK0() { |
| 21 | + if [ "${NODE_RANK:-0}" -eq 0 ]; then |
| 22 | + echo "[ERROR] $*" >&2 |
| 23 | + fi |
| 24 | + } |
| 25 | + LOG_WARN() { |
| 26 | + if [ "${NODE_RANK:-0}" -eq 0 ]; then |
| 27 | + echo "[WARN] $*" >&2 |
| 28 | + fi |
| 29 | + } |
| 30 | +fi |
| 31 | + |
| 32 | +# Execute hooks for a given command |
| 33 | +# Args: |
| 34 | +# $1: hook_group (e.g., "train", "benchmark") |
| 35 | +# $2: hook_name (e.g., "pretrain", "gemm") |
| 36 | +# $@: Additional arguments to pass to hooks |
| 37 | +execute_hooks() { |
| 38 | + if [[ $# -lt 2 ]]; then |
| 39 | + LOG_INFO_RANK0 "[Hooks] No hook target specified (need group and name)" |
| 40 | + return 0 |
| 41 | + fi |
| 42 | + |
| 43 | + local hook_group="$1" |
| 44 | + local hook_name="$2" |
| 45 | + shift 2 |
| 46 | + local hook_args=("$@") |
| 47 | + |
| 48 | + # Determine script directory |
| 49 | + local script_dir |
| 50 | + script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 51 | + |
| 52 | + local hook_dir="${script_dir}/hooks/${hook_group}/${hook_name}" |
| 53 | + |
| 54 | + if [[ ! -d "$hook_dir" ]]; then |
| 55 | + LOG_INFO_RANK0 "[Hooks] No hook directory for [$hook_group/$hook_name]" |
| 56 | + return 0 |
| 57 | + fi |
| 58 | + |
| 59 | + LOG_INFO_RANK0 "[Hooks] Detected hooks directory: $hook_dir" |
| 60 | + |
| 61 | + # Find all hook files (*.sh and *.py) |
| 62 | + local hook_files=() |
| 63 | + mapfile -t hook_files < <(find "$hook_dir" -maxdepth 1 -type f \( -name "*.sh" -o -name "*.py" \) | sort) |
| 64 | + |
| 65 | + if [[ ${#hook_files[@]} -eq 0 ]]; then |
| 66 | + LOG_INFO_RANK0 "[Hooks] No hook files found in $hook_dir" |
| 67 | + return 0 |
| 68 | + fi |
| 69 | + |
| 70 | + # Execute each hook file |
| 71 | + for hook_file in "${hook_files[@]}"; do |
| 72 | + LOG_INFO_RANK0 "[Hooks] Executing hook: $hook_file ${hook_args[*]}" |
| 73 | + |
| 74 | + start_time=$(date +%s) |
| 75 | + |
| 76 | + if [[ "$hook_file" == *.sh ]]; then |
| 77 | + if ! bash "$hook_file" "${hook_args[@]}"; then |
| 78 | + LOG_ERROR_RANK0 "[Hooks] Hook failed: $hook_file (exit code: $?)" |
| 79 | + return 1 |
| 80 | + fi |
| 81 | + elif [[ "$hook_file" == *.py ]]; then |
| 82 | + if ! python3 "$hook_file" "${hook_args[@]}"; then |
| 83 | + LOG_ERROR_RANK0 "[Hooks] Hook failed: $hook_file (exit code: $?)" |
| 84 | + return 1 |
| 85 | + fi |
| 86 | + else |
| 87 | + LOG_WARN "[Hooks] Skipping unknown hook type: $hook_file" |
| 88 | + fi |
| 89 | + |
| 90 | + duration=$(( $(date +%s) - start_time )) |
| 91 | + LOG_INFO_RANK0 "[Hooks] Hook $hook_file finished in ${duration}s" |
| 92 | + done |
| 93 | + |
| 94 | + LOG_INFO_RANK0 "[Hooks] All hooks executed successfully" |
| 95 | + return 0 |
| 96 | +} |
| 97 | + |
| 98 | +# If called directly (not sourced), execute the function |
| 99 | +if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then |
| 100 | + # Always source common.sh when called directly, as functions are not inherited by subshells |
| 101 | + # Unset the guard variable to force re-sourcing in this new shell instance |
| 102 | + unset __PRIMUS_COMMON_SOURCED |
| 103 | + SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 104 | + # shellcheck disable=SC1091 |
| 105 | + source "$SCRIPT_DIR/../lib/common.sh" || { |
| 106 | + echo "[ERROR] Failed to load common library" >&2 |
| 107 | + exit 1 |
| 108 | + } |
| 109 | + execute_hooks "$@" |
| 110 | +fi |
0 commit comments