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
//! Test-only override of the prepared-route switches on the calling thread.
//!
//! The switches (`HF2Q_REPACKED_GGUF_M32`, `_M32_PROJ`, `_M32_HEAD`,
//! `_ANY_WIDTH`, `_SHARED_HEAD`) are read once per process, so a test cannot
//! set them through the environment without changing every other test of
//! the binary. A test that needs a route holds an [`Override`] instead:
//! while it lives, the switch readers of this thread return its values and
//! never consult the environment. Joint steps are encoded on the calling
//! thread, so the override covers a whole step.
use std::cell::Cell;
/// One value per prepared-route switch.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub(crate) struct PreparedSwitches {
/// `HF2Q_REPACKED_GGUF_M32`: the dense FFN, and the gate of the others.
pub ffn: bool,
/// `HF2Q_REPACKED_GGUF_M32_PROJ`.
pub projections: bool,
/// `HF2Q_REPACKED_GGUF_M32_HEAD`.
pub head: bool,
/// `HF2Q_REPACKED_GGUF_ANY_WIDTH`.
pub any_width: bool,
/// `HF2Q_REPACKED_GGUF_SHARED_HEAD`.
pub shared_head: bool,
/// Oracle only: allocate BF16 projection inputs separately while keeping
/// the same attention kernel, precision and command schedule.
pub independent_fa_staging: bool,
}
impl PreparedSwitches {
/// Every switch off: the stock kernels.
pub(crate) const OFF: Self = Self {
ffn: false,
projections: false,
head: false,
any_width: false,
shared_head: false,
independent_fa_staging: false,
};
/// The prepared FFN, projections and head at 32 rows only.
pub(crate) const WIDTH_32: Self = Self {
ffn: true,
projections: true,
head: true,
any_width: false,
shared_head: false,
independent_fa_staging: false,
};
/// Every switch on.
pub(crate) const ALL: Self = Self {
ffn: true,
projections: true,
head: true,
any_width: true,
shared_head: true,
independent_fa_staging: false,
};
}
thread_local! {
static OVERRIDE: Cell<Option<PreparedSwitches>> = const { Cell::new(None) };
}
/// The override of this thread, if a test holds one.
pub(crate) fn current() -> Option<PreparedSwitches> {
OVERRIDE.with(Cell::get)
}
/// Holds the calling thread's switches at fixed values until dropped.
pub(crate) struct Override {
previous: Option<PreparedSwitches>,
}
impl Override {
pub(crate) fn set(switches: PreparedSwitches) -> Self {
Self {
previous: OVERRIDE.with(|cell| cell.replace(Some(switches))),
}
}
}
impl Drop for Override {
fn drop(&mut self) {
OVERRIDE.with(|cell| cell.set(self.previous));
}
}
#[test]
fn the_override_is_scoped_to_its_guard_and_nests() {
let before = current();
{
let _all = Override::set(PreparedSwitches::ALL);
assert_eq!(current(), Some(PreparedSwitches::ALL));
{
let _off = Override::set(PreparedSwitches::OFF);
assert_eq!(current(), Some(PreparedSwitches::OFF));
}
assert_eq!(current(), Some(PreparedSwitches::ALL));
}
assert_eq!(current(), before);
}