|
| 1 | +# Copyright (C) 2024 Intel Corporation |
| 2 | +# Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. |
| 3 | +# See LICENSE.TXT |
| 4 | +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 5 | + |
| 6 | +import csv |
| 7 | +import io |
| 8 | +from pathlib import Path |
| 9 | +import re |
| 10 | +import shutil |
| 11 | +from utils.utils import download, git_clone |
| 12 | +from .base import Benchmark, Suite |
| 13 | +from .result import Result |
| 14 | +from utils.utils import run, create_build_path |
| 15 | +from .options import options |
| 16 | +import os |
| 17 | + |
| 18 | +class OneAPI: |
| 19 | + # random unique number for benchmark oneAPI installation |
| 20 | + ONEAPI_BENCHMARK_INSTANCE_ID = 98765 |
| 21 | + def __init__(self, directory): |
| 22 | + self.oneapi_dir = os.path.join(directory, 'oneapi') |
| 23 | + Path(self.oneapi_dir).mkdir(parents=True, exist_ok=True) |
| 24 | + # delete if some option is set? |
| 25 | + |
| 26 | + # can we just hardcode these links? |
| 27 | + self.install_package('dnnl', 'https://registrationcenter-download.intel.com/akdlm/IRC_NAS/87e117ab-039b-437d-9c80-dcd5c9e675d5/intel-onednn-2025.0.0.862_offline.sh') |
| 28 | + self.install_package('mkl', 'https://registrationcenter-download.intel.com/akdlm/IRC_NAS/79153e0f-74d7-45af-b8c2-258941adf58a/intel-onemkl-2025.0.0.940_offline.sh') |
| 29 | + return |
| 30 | + |
| 31 | + def install_package(self, name, url): |
| 32 | + package_path = os.path.join(self.oneapi_dir, name) |
| 33 | + if Path(package_path).exists(): |
| 34 | + print(f"{package_path} exists, skipping installing oneAPI package {name}...") |
| 35 | + return |
| 36 | + |
| 37 | + package = download(self.oneapi_dir, url, f'package_{name}.sh') |
| 38 | + try: |
| 39 | + print(f"installing f{name}") |
| 40 | + run(f"sh {package} -a -s --eula accept --install-dir {self.oneapi_dir} --instance f{self.ONEAPI_BENCHMARK_INSTANCE_ID}") |
| 41 | + except: |
| 42 | + print("oneAPI installation likely exists already") |
| 43 | + return |
| 44 | + print(f"f{name} installation complete") |
| 45 | + |
| 46 | + def package_dir(self, package, dir): |
| 47 | + return os.path.join(self.oneapi_dir, package, 'latest', dir) |
| 48 | + |
| 49 | + def package_cmake(self, package): |
| 50 | + package_lib = self.package_dir(package, 'lib') |
| 51 | + return os.path.join(package_lib, 'cmake', package) |
| 52 | + |
| 53 | + def mkl_lib(self): |
| 54 | + return self.package_dir('mkl', 'lib') |
| 55 | + |
| 56 | + def mkl_include(self): |
| 57 | + return self.package_dir('mkl', 'include') |
| 58 | + |
| 59 | + def mkl_cmake(self): |
| 60 | + return self.package_cmake('mkl') |
| 61 | + |
| 62 | + def dnn_lib(self): |
| 63 | + return self.package_dir('dnnl', 'lib') |
| 64 | + |
| 65 | + def dnn_include(self): |
| 66 | + return self.package_dir('dnnl', 'include') |
| 67 | + |
| 68 | + def dnn_cmake(self): |
| 69 | + return self.package_cmake('dnnl') |
| 70 | + |
| 71 | + def tbb_lib(self): |
| 72 | + return self.package_dir('tbb', 'lib') |
| 73 | + |
| 74 | + def tbb_cmake(self): |
| 75 | + return self.package_cmake('tbb') |
| 76 | + |
| 77 | + def compiler_lib(self): |
| 78 | + return self.package_dir('compiler', 'lib') |
| 79 | + |
| 80 | + def ld_libraries(self): |
| 81 | + return [ |
| 82 | + self.compiler_lib(), |
| 83 | + self.mkl_lib(), |
| 84 | + self.tbb_lib(), |
| 85 | + self.dnn_lib() |
| 86 | + ] |
| 87 | + |
| 88 | +class LlamaCppBench(Suite): |
| 89 | + def __init__(self, directory): |
| 90 | + if options.sycl is None: |
| 91 | + return |
| 92 | + |
| 93 | + self.directory = directory |
| 94 | + |
| 95 | + def setup(self): |
| 96 | + if options.sycl is None: |
| 97 | + return |
| 98 | + |
| 99 | + repo_path = git_clone(self.directory, "llamacpp-repo", "https://github.com/ggerganov/llama.cpp", "1ee9eea094fe5846c7d8d770aa7caa749d246b23") |
| 100 | + |
| 101 | + self.models_dir = os.path.join(self.directory, 'models') |
| 102 | + Path(self.models_dir).mkdir(parents=True, exist_ok=True) |
| 103 | + |
| 104 | + self.model = download(self.models_dir, "https://huggingface.co/microsoft/Phi-3-mini-4k-instruct-gguf/resolve/main/Phi-3-mini-4k-instruct-q4.gguf", "Phi-3-mini-4k-instruct-q4.gguf") |
| 105 | + |
| 106 | + self.oneapi = OneAPI(self.directory) |
| 107 | + |
| 108 | + self.build_path = create_build_path(self.directory, 'llamacpp-build') |
| 109 | + |
| 110 | + configure_command = [ |
| 111 | + "cmake", |
| 112 | + f"-B {self.build_path}", |
| 113 | + f"-S {repo_path}", |
| 114 | + f"-DCMAKE_BUILD_TYPE=Release", |
| 115 | + f"-DGGML_SYCL=ON", |
| 116 | + f"-DCMAKE_C_COMPILER=clang", |
| 117 | + f"-DCMAKE_CXX_COMPILER=clang++", |
| 118 | + f"-DDNNL_DIR={self.oneapi.dnn_cmake()}", |
| 119 | + f"-DTBB_DIR={self.oneapi.tbb_cmake()}", |
| 120 | + f'-DCMAKE_CXX_FLAGS=-I"{self.oneapi.mkl_include()}"', |
| 121 | + f'-DCMAKE_SHARED_LINKER_FLAGS=-L{self.oneapi.compiler_lib()} -L{self.oneapi.mkl_lib()}' |
| 122 | + ] |
| 123 | + print(f"{self.__class__.__name__}: Run {configure_command}") |
| 124 | + run(configure_command, add_sycl=True) |
| 125 | + print(f"{self.__class__.__name__}: Run cmake --build {self.build_path} -j") |
| 126 | + run(f"cmake --build {self.build_path} -j", add_sycl=True, ld_library=self.oneapi.ld_libraries()) |
| 127 | + |
| 128 | + def benchmarks(self) -> list[Benchmark]: |
| 129 | + if options.sycl is None: |
| 130 | + return [] |
| 131 | + |
| 132 | + return [ |
| 133 | + LlamaBench(self) |
| 134 | + ] |
| 135 | + |
| 136 | +class LlamaBench(Benchmark): |
| 137 | + def __init__(self, bench): |
| 138 | + self.bench = bench |
| 139 | + super().__init__(bench.directory) |
| 140 | + |
| 141 | + def unit(self): |
| 142 | + return "token/s" |
| 143 | + |
| 144 | + def setup(self): |
| 145 | + self.benchmark_bin = os.path.join(self.bench.build_path, 'bin', 'llama-bench') |
| 146 | + |
| 147 | + def name(self): |
| 148 | + return f"llama.cpp" |
| 149 | + |
| 150 | + def lower_is_better(self): |
| 151 | + return False |
| 152 | + |
| 153 | + def ignore_iterations(self): |
| 154 | + return True |
| 155 | + |
| 156 | + def run(self, env_vars) -> list[Result]: |
| 157 | + command = [ |
| 158 | + f"{self.benchmark_bin}", |
| 159 | + "--output", "csv", |
| 160 | + "-n", "128", |
| 161 | + "-p", "512", |
| 162 | + "-b", "128,256,512", |
| 163 | + "--numa", "isolate", |
| 164 | + "-t", "56", # TODO: use only as many threads as numa node 0 has cpus |
| 165 | + "--model", f"{self.bench.model}", |
| 166 | + ] |
| 167 | + |
| 168 | + result = self.run_bench(command, env_vars, ld_library=self.bench.oneapi.ld_libraries()) |
| 169 | + parsed = self.parse_output(result) |
| 170 | + results = [] |
| 171 | + for r in parsed: |
| 172 | + (extra_label, mean) = r |
| 173 | + label = f"{self.name()} {extra_label}" |
| 174 | + results.append(Result(label=label, value=mean, command=command, env=env_vars, stdout=result)) |
| 175 | + return results |
| 176 | + |
| 177 | + def parse_output(self, output): |
| 178 | + csv_file = io.StringIO(output) |
| 179 | + reader = csv.DictReader(csv_file) |
| 180 | + |
| 181 | + results = [] |
| 182 | + for row in reader: |
| 183 | + try: |
| 184 | + n_batch = row["n_batch"] |
| 185 | + avg_ts = float(row["avg_ts"]) |
| 186 | + n_prompt = int(row["n_prompt"]) |
| 187 | + label = "Prompt Processing" if n_prompt != 0 else "Text Generation" |
| 188 | + label += f" Batched {n_batch}" |
| 189 | + results.append((label, avg_ts)) |
| 190 | + except KeyError as e: |
| 191 | + raise ValueError(f"Error parsing output: {e}") |
| 192 | + |
| 193 | + return results |
| 194 | + |
| 195 | + def teardown(self): |
| 196 | + return |
0 commit comments