Files
mandy/runtime/test/vk_compile_gate_calibration_test.cpp
OreoFR 1d7c662b4b compile gate: auto default, calibrated at device creation
With LEONIDA_COMPILE_GATE unset or `auto`, the device measures the driver once
before the VulkanContext exists: a worker creates a synthetic compute pipeline
(SPIR-V written in code, new specialization seed each run, no pipeline cache)
while the queue gets empty submissions waited on a fence. Quick submissions with
the create still in the driver give the adaptive gate; a held one, a create too
fast to tell, an unexplained slow submission or any failure give classic. The
verdict is cached per vendor/device/driver/pipelineCacheUUID in
compile_gate_calibration.txt beside the pipeline cache;
LEONIDA_COMPILE_GATE_RECALIBRATE=1 measures again. Only the title process
calibrates; build hosts and tests keep classic. `classic`, `adaptive`, `1`, `0`
still force a mode.

Tests: compile_gate_policy_test (no GPU: settings, NVIDIA version decode,
verdicts, cache file, SPIR-V shape) and vk_compile_gate_calibration_test (GPU:
a verdict within 20 s, adaptive on NVIDIA 621.40).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 13:24:49 +01:00

72 lines
3.7 KiB
C++

// The LEONIDA_COMPILE_GATE=auto calibration (src/compile_gate_calibration.h) on this machine's GPU: it must finish
// within kBoundMs and, on NVIDIA, reach a verdict (adaptive or holds) rather than give up. On NVIDIA 621.40 (an RTX
// 4070 Laptop) submissions were measured never to wait for a create, so there the verdict must be adaptive;
// LEONIDA_EXPECT_COMPILE_GATE=adaptive|classic states the expected gate on any other GPU. 77 without a device.
#include "vk_test_device.h"
#include "../src/compile_gate_calibration.h"
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <cstring>
namespace {
constexpr double kBoundMs = 20000;
}
int main() {
namespace policy = leonida::compile_gate_policy;
vktest::Device device;
if (!device.create()) {
std::printf("vk_compile_gate_calibration_test: SKIP (no Vulkan device)\n");
return 77;
}
VkPhysicalDeviceProperties props{};
device.instance_fn<PFN_vkGetPhysicalDeviceProperties>("vkGetPhysicalDeviceProperties")(device.physical, &props);
const std::string driver = policy::describe_driver(props.vendorID, props.driverVersion);
std::printf("vk_compile_gate_calibration_test: %s, %s\n", props.deviceName, driver.c_str());
if (props.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
std::printf("vk_compile_gate_calibration_test: SKIP (a CPU device: the runtime does not calibrate those)\n");
return 77;
}
const auto seed = static_cast<std::uint32_t>(std::chrono::steady_clock::now().time_since_epoch().count()) | 1u;
const auto result = leonida::compile_gate_calibration::calibrate(device.device_proc, device.device, device.queue,
device.family, seed, "[test]");
std::printf("vk_compile_gate_calibration_test: %s after %u trial(s), %u statements, %.0f ms: %s\n",
result.ok ? policy::outcome_name(result.verdict.outcome) : "failed", result.trials,
unsigned(result.statements), result.total_ms,
result.ok ? policy::describe_verdict(result.verdict).c_str() : result.error.c_str());
int failures = 0;
if (!result.ok) {
std::fprintf(stderr, "FAIL: calibration failed: %s\n", result.error.c_str());
++failures;
}
if (result.total_ms > kBoundMs) {
std::fprintf(stderr, "FAIL: calibration took %.0f ms (bound %.0f ms)\n", result.total_ms, kBoundMs);
++failures;
}
const bool decided =
result.verdict.outcome == policy::Outcome::adaptive || result.verdict.outcome == policy::Outcome::holds;
if (result.ok && props.vendorID == policy::kNvidiaVendorId && !decided) {
std::fprintf(stderr, "FAIL: no verdict on NVIDIA (%s)\n", policy::outcome_name(result.verdict.outcome));
++failures;
}
const char* expect = std::getenv("LEONIDA_EXPECT_COMPILE_GATE");
const bool measured_621_40 = props.vendorID == policy::kNvidiaVendorId &&
policy::decode_nvidia(props.driverVersion).major == 621 &&
policy::decode_nvidia(props.driverVersion).minor == 40;
if (!expect && measured_621_40) expect = "adaptive";
if (result.ok && expect) {
const char* got = policy::mode_for(result.verdict.outcome) == leonida::CompileGateMode::adaptive ? "adaptive"
: "classic";
if (std::strcmp(expect, got)) {
std::fprintf(stderr, "FAIL: expected the %s gate, the calibration gives %s\n", expect, got);
++failures;
}
}
if (failures) return 1;
std::printf("vk_compile_gate_calibration_test: ok\n");
return 0;
}