char-fork/llama.cpp

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

Hongqiang Wangopencl: cache compiled cl_program binaries on disk (#26050)ed7adbfef

char/aliceai-t5-moe
3.3 KiB75 linesraw
1// On-disk cache for OpenCL cl_program binaries. Lets a fresh process skip the
2// expensive clBuildProgram-from-source step when a binary for the exact same
3// (source, compile options, device, driver, platform) was previously saved.
4//
5// Activation: default on via GGML_OPENCL_KERNEL_CACHE_DIR:
6//   unset / empty / "1" / "default"      : platform default cache dir
7//                                          (%LOCALAPPDATA%\llama.cpp\cl-cache,
8//                                          ~/Library/Caches/llama.cpp/cl-cache,
9//                                          <temp dir>/llama.cpp/cl-cache elsewhere)
10//   "0" / "off" / "none" / "disable(d)"  : disabled (all functions no-op)
11//   any other value                      : used verbatim as the cache path
12// If the chosen directory cannot be created/used, the cache silently disables
13// itself for the process and falls back to source compile.
14// GGML_OPENCL_KERNEL_CACHE_DEBUG=1 prints a HIT/MISS/SAVE trace (with a running
15// tally) straight to stderr — visible even in tools that filter INFO/WARN logs;
16// redirect stderr to record it.
17//
18// Cache key (SHA-256 hex):
19//   sha256(source_bytes || '\x00' ||
20//          compile_opts  || '\x00' ||
21//          CL_DEVICE_NAME || '\x00' ||
22//          CL_DRIVER_VERSION || '\x00' ||
23//          CL_PLATFORM_VERSION || '\x00' ||
24//          CL_PROGRAM_CACHE_FORMAT_VERSION)
25//
26// The key fully captures everything that can affect the produced binary,
27// without needing the host source revision (a kernel source change shows up
28// in source_bytes; a compile-option change shows up in compile_opts).
29//
30// File layout per cache entry: <cache_dir>/<sha256-hex>.clbin
31//   bytes [0..7]   : magic "GGMLCLBC"
32//   bytes [8..11]  : uint32_t format version (CL_PROGRAM_CACHE_FORMAT_VERSION)
33//   bytes [12..15] : uint32_t reserved (0)
34//   bytes [16..]   : raw cl_program binary as returned by
35//                    clGetProgramInfo(CL_PROGRAM_BINARIES)
36//
37// Concurrency: writes go to <name>.tmp.<pid> then atomic rename. On race,
38// last-writer-wins. No locks.
39
40#pragma once
41
42#include <CL/cl.h>
43#include <string>
44
45// Bumped manually if host-side OpenCL API usage changes in a way that
46// affects compile semantics but does not show up in source_bytes /
47// compile_opts (e.g. switching from clCreateProgramWithSource to
48// clCompileProgram + clLinkProgram, or changing how multiple sources
49// are concatenated). Most commits — including kernel changes — do NOT
50// require bumping this; the source bytes already capture those.
51#define CL_PROGRAM_CACHE_FORMAT_VERSION 1u
52
53struct cl_program_cache_state {
54    // Empty string means cache is disabled.
55    std::string dir;
56    // Concatenated device/driver/platform identity + cache format version,
57    // computed once at init and folded into every key.
58    std::string key_suffix;
59};
60
61cl_program_cache_state cl_program_cache_init(cl_device_id device);
62
63cl_program cl_program_cache_try_load(
64    const cl_program_cache_state & state,
65    cl_context                     context,
66    cl_device_id                   device,
67    const char *                   source,
68    const std::string &            compile_opts);
69
70void cl_program_cache_try_save(
71    const cl_program_cache_state & state,
72    cl_program                     program,
73    cl_device_id                   device,
74    const char *                   source,
75    const std::string &            compile_opts);