char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Daniel Beveniuscmake : add PCH and unity build to improve build times (#28091)3bcfeb700

char/aliceai-t5-moe
3.2 KiB122 linesraw
1#!/usr/bin/env bash
2# Compile-time profiling using clang -ftime-trace + ClangBuildAnalyzer.
3#
4# Usage:
5#   ./scripts/build-profile.sh [--full] [-jN]
6#
7# --full: include Server, Tools, and Tests (default: minimal build)
8# -jN   :  number of parallel jobs (default: all cores)
9#
10# Requires ClangBuildAnalyzer:
11#   macOS: brew install clang-build-analyzer
12#   Linux: https://github.com/aras-p/ClangBuildAnalyzer.git
13
14set -euo pipefail
15
16SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
17ROOT_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
18
19FULL=0
20JOBS="-j$(nproc 2>/dev/null || sysctl -n hw.ncpu)"
21
22for arg in "$@"; do
23    case "${arg}" in
24        --full) FULL=1 ;;
25        -j*)    JOBS="${arg}" ;;
26        *) echo "error: unknown argument: ${arg}" >&2; exit 1 ;;
27    esac
28done
29
30if [ "${FULL}" -eq 1 ]; then
31    BUILD_DIR="${ROOT_DIR}/build-profile-full"
32    REPORT="${BUILD_DIR}/profile-report-full.txt"
33else
34    BUILD_DIR="${ROOT_DIR}/build-profile-baseline"
35    REPORT="${BUILD_DIR}/profile-report.txt"
36fi
37
38OUTPUT_BIN="${BUILD_DIR}/clang_analysis.bin"
39
40if ! command -v clang++ &>/dev/null; then
41    echo "error: clang++ not found" >&2
42    exit 1
43fi
44
45if ! command -v ClangBuildAnalyzer &>/dev/null; then
46    echo "error: ClangBuildAnalyzer not found" >&2
47    echo "  brew install clangbuildanalyzer  (macOS)" >&2
48    echo "  or: https://github.com/aras-p/ClangBuildAnalyzer/releases" >&2
49    exit 1
50fi
51
52CLANG_VER=$(clang++ --version | head -1)
53echo "compiler : ${CLANG_VER}"
54echo "build dir: ${BUILD_DIR}"
55echo "output   : ${OUTPUT_BIN}"
56echo "jobs     : ${JOBS}"
57echo
58
59if command -v ccache &>/dev/null; then
60    echo "clearing ccache..."
61    ccache -C -z
62fi
63
64export CCACHE_DISABLE=1
65
66cmake --fresh \
67    -S "${ROOT_DIR}" \
68    -B "${BUILD_DIR}" \
69    -DCMAKE_BUILD_TYPE=Release \
70    -DCMAKE_C_COMPILER=clang \
71    -DCMAKE_CXX_COMPILER=clang++ \
72    -DCMAKE_C_FLAGS="-ftime-trace" \
73    -DCMAKE_CXX_FLAGS="-ftime-trace" \
74    -DGGML_CCACHE=OFF \
75    -DGGML_OPENMP=ON \
76    -DGGML_NATIVE=OFF \
77    -DLLAMA_BUILD_TESTS=$([ "${FULL}" -eq 1 ] && echo ON || echo OFF) \
78    -DLLAMA_BUILD_EXAMPLES=OFF \
79    -DLLAMA_BUILD_TOOLS=$([ "${FULL}" -eq 1 ] && echo ON || echo OFF) \
80    -DLLAMA_BUILD_SERVER=$([ "${FULL}" -eq 1 ] && echo ON || echo OFF) \
81    -DLLAMA_BUILD_APP=OFF
82
83echo
84
85echo "Initializing ClangBuildAnalyzer..."
86ClangBuildAnalyzer --start "${BUILD_DIR}"
87echo
88
89echo "building..."
90echo
91
92START=$(date +%s)
93
94cmake --build "${BUILD_DIR}" --clean-first "${JOBS}"
95
96END=$(date +%s)
97ELAPSED=$((END - START))
98
99echo
100printf "build time: %ds (%dm %ds)\n" "${ELAPSED}" "$((ELAPSED / 60))" "$((ELAPSED % 60))"
101echo
102
103echo "Aggregating profile metrics..."
104ClangBuildAnalyzer --stop "${BUILD_DIR}" "${OUTPUT_BIN}" > /dev/null
105
106echo
107echo "================================================================================"
108TUS=$(grep -oP "Compilation \(\K[0-9]+" "${REPORT}" 2>/dev/null || echo "?")
109ClangBuildAnalyzer --analyze "${OUTPUT_BIN}" | tee "${REPORT}"
110
111echo
112echo "translation units: ${TUS}"
113echo
114echo "largest trace files (top 20 by size):"
115find "${BUILD_DIR}" -name "*.json" ! -name "compile_commands.json" \
116    | xargs ls -l 2>/dev/null \
117    | awk 'NF>5 {print $5, $NF}' \
118    | sort -rn \
119    | awk 'NR<=20 {printf "%8.1f KB  %s\n", $1/1024, $2}'
120
121echo
122echo "ClangBuildAnalyzer report was generated: ${REPORT}"