1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//! VRAM-adaptive GPU batch-input sizing.
//!
//! This module owns the live GPU region-presence byte-budget selector used for
//! routing and cache-key stability.
use ;
// ---------------------------------------------------------------------------
// VRAM sizing table: ONE owner for every threshold and byte budget.
//
// The adaptive `gpu_batch_input_limit_for_vram_mb` match arms below are the SOLE
// readers of these; nothing is a bare magic number inline. The `_UNKNOWN` floor
// (128 MiB) doubles as the lower clamp bound and the `_HIGH` budget (1 GiB) as
// the upper clamp bound for any Tier-A override, so the operator can never drive
// the buffer outside the range the table itself honors.
// ---------------------------------------------------------------------------
/// `>= 24 GiB` VRAM (RTX 4090 / 5090, A100 / H100) -> 1 GiB input.
pub const VRAM_MB_TIER_HIGH: u64 = 24 * 1024;
pub const GPU_BATCH_INPUT_LIMIT_HIGH: usize = 1024 * 1024 * 1024;
/// `12 - 23 GiB` VRAM (RTX 3090, RTX 4080, M-Max) -> 512 MiB input.
pub const VRAM_MB_TIER_MID: u64 = 12 * 1024;
pub const GPU_BATCH_INPUT_LIMIT_MID: usize = 512 * 1024 * 1024;
/// `8 - 11 GiB` VRAM (RTX 3080, RTX 4070, M-Pro) -> 256 MiB input.
pub const VRAM_MB_TIER_LOW: u64 = 8 * 1024;
pub const GPU_BATCH_INPUT_LIMIT_LOW: usize = 256 * 1024 * 1024;
/// Conservative floor for hosts with low or unknown VRAM. Unknown must not
/// inherit the 8-11 GiB tier: absence of adapter memory evidence is the same
/// safety class as low-memory/iGPU/software adapters. Also the lower clamp bound
/// for a Tier-A override (see [`set_gpu_batch_input_limit`]).
pub const GPU_BATCH_INPUT_LIMIT_UNKNOWN: usize = 128 * 1024 * 1024;
/// Process-wide GPU batch-input override in bytes. `0` = unset (use the
/// VRAM-adaptive table). Set ONCE at scan startup, before the first
/// [`gpu_batch_input_limit`] call caches the value, from resolved config (Tier-A:
/// compiled default -> `.keyhog.toml` -> `--gpu-batch-input-limit`). Mirrors the
/// `REGEX_DFA_LIMIT_OVERRIDE` process-global pattern so the routing/cache-key
/// path needs no per-call plumbing.
static GPU_BATCH_INPUT_LIMIT_OVERRIDE: AtomicUsize = new;
/// The `[floor, cap]` the resolved GPU batch input limit is clamped into: the
/// 128 MiB unknown-host floor and the 1 GiB pre-compile-time ceiling that bound
/// the VRAM table. A Tier-A override is clamped into this range so no config/CLI
/// value can request a buffer the sizing contract forbids.
/// Override the GPU batch input limit for this process. Call before scanning.
/// `0` resets to the VRAM-adaptive default; any other value is clamped into
/// [`gpu_batch_input_limit_bounds`] at read time. Tier-A config knob
/// (compiled default -> TOML -> CLI), the sizing analogue of
/// [`crate::types::set_regex_dfa_limit`].
/// Clamp a raw Tier-A override into [`gpu_batch_input_limit_bounds`]. Pure, testable
/// without the process-global, so the clamp contract is proven deterministically.
pub
/// Resolve the Tier-A override into an effective byte budget, or `None` when
/// unset (`0`). Split out so the cached entry point stays thin. Reads the
/// process-global; the clamp itself lives in [`clamp_gpu_batch_input_limit`].
pub
/// VRAM-adaptive GPU batch-input limit. Bigger buffers mean fewer
/// device dispatches per multi-TB scan; each kernel launch is a fixed
/// ~50-300 µs cost regardless of payload, so doubling the input
/// halves dispatch overhead. Capped by host VRAM (input + transition
/// tables + match output must fit) and by a 1 GiB upper bound so the
/// pre-compile time stays bounded.
///
/// | VRAM detected | Input length | Adapter examples |
/// |-------------------|--------------|----------------------------------|
/// | >= 24 GiB | 1 GiB | RTX 4090 / 5090, A100 / H100 |
/// | 12 - 23 GiB | 512 MiB | RTX 3090, RTX 4080, M-Max |
/// | 8 - 11 GiB | 256 MiB | RTX 3080, RTX 4070, M-Pro |
/// | < 8 GiB / Unknown| 128 MiB | iGPU, software, no-GPU CI runner |
///
/// Cached on first call; the result is stable for the process
/// lifetime so routing and cache identities stay consistent across
/// every batch.
pub