The same lexer-based splitter as S12b, now with explicit re-exports: every moved free fn comes back to its module under its old name and old visibility (cfg attributes carried), so a name can never clash with another glob import. 162 module files of leleka-runtime (68), cuda-driver (84), deploy (10), cpu-sys, igpu and leleka-memory are split into groups of about 250 lines; types, constants, macros, nested modules and tests stay. Not touched: the files kar-off-ab edits, crate roots (lib.rs, main.rs, bin/*), and giant single impl blocks (next). This changes codegen-unit placement of engine code, so it is covered by the bitwise replay and the paired A/B on the 5090, not by a binary comparison.
198 lines
6.4 KiB
Rust
198 lines
6.4 KiB
Rust
//! Moved out of the parent module by STRA-338: impl Program, fn admit_ptx, fn admit, fn s_gemv_entry_index, impl Slot, fn conforms ....
|
|
|
|
#[allow(unused_imports)]
|
|
use super::*;
|
|
|
|
|
|
impl Program {
|
|
pub const ALL: [Program; 3] = [
|
|
Program::NativeFlashAttn,
|
|
Program::SGemvLegacy,
|
|
Program::DevicePoison,
|
|
];
|
|
pub const fn name(self) -> &'static str {
|
|
match self {
|
|
Self::NativeFlashAttn => "native_flash_attn",
|
|
Self::SGemvLegacy => "s_gemv_legacy",
|
|
Self::DevicePoison => "device_poison",
|
|
}
|
|
}
|
|
pub fn ptx(self) -> &'static [u8] {
|
|
match self {
|
|
Self::NativeFlashAttn => NATIVE_FLASH_ATTN_PTX,
|
|
Self::SGemvLegacy => S_GEMV_LEGACY_PTX,
|
|
Self::DevicePoison => DEVICE_POISON_PTX,
|
|
}
|
|
}
|
|
pub const fn pin(self) -> &'static PtxPin {
|
|
&PINS[self as usize]
|
|
}
|
|
/// The checked-in CUDA C++ source (a literal port of the named Original files).
|
|
pub const fn source(self) -> &'static str {
|
|
match self {
|
|
Self::NativeFlashAttn => include_str!("../native_flash_attn.cu"),
|
|
Self::SGemvLegacy => include_str!("../s_gemv_legacy.cu"),
|
|
Self::DevicePoison => include_str!("../device_poison.cu"),
|
|
}
|
|
}
|
|
pub fn entries(self) -> &'static [Entry] {
|
|
match self {
|
|
Self::NativeFlashAttn => &FLASH_ENTRIES,
|
|
Self::SGemvLegacy => &S_GEMV_ENTRIES,
|
|
Self::DevicePoison => &POISON_ENTRIES,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Fail closed: zero pins, malformed bytes or a digest mismatch all refuse.
|
|
pub fn admit_ptx(program: Program, ptx: &'static [u8], pin: &PtxPin) -> Result<()> {
|
|
let refuse = ExperimentalError::Unpinned(program.name());
|
|
if pin.source_sha256 == [0; 32] || pin.ptx_sha256 == [0; 32] {
|
|
return Err(refuse);
|
|
}
|
|
if ptx.len() < 2
|
|
|| ptx.last() != Some(&0)
|
|
|| ptx[..ptx.len() - 1].contains(&0)
|
|
|| !ptx.is_ascii()
|
|
{
|
|
return Err(refuse);
|
|
}
|
|
if qwen_ffi::sha256(&ptx[..ptx.len() - 1]) != pin.ptx_sha256 {
|
|
return Err(refuse);
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// The gate: no driver call happens before it passes.
|
|
pub fn admit(program: Program) -> Result<()> {
|
|
admit_ptx(program, program.ptx(), program.pin())
|
|
}
|
|
|
|
/// Index of a planned kernel's entry in [`S_GEMV_ENTRIES`].
|
|
pub fn s_gemv_entry_index(kernel: Kernel) -> usize {
|
|
Kernel::ALL_ENTRIES
|
|
.iter()
|
|
.position(|name| *name == kernel.entry())
|
|
.expect("every planned kernel is an exported entry")
|
|
}
|
|
|
|
impl Slot {
|
|
pub fn kind(self) -> Arg {
|
|
match self {
|
|
Self::Ptr(_) => Arg::Ptr,
|
|
Self::Null => Arg::PtrOpt,
|
|
Self::I32(_) => Arg::I32,
|
|
Self::I64(_) => Arg::I64,
|
|
Self::F32(_) => Arg::F32,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The slots agree with the entry's fixed signature (an optional pointer may be given or null).
|
|
pub fn conforms(entry: &Entry, slots: &[Slot]) -> bool {
|
|
slots.len() <= MAX_ENTRY_ARGUMENTS
|
|
&& entry.args.len() == slots.len()
|
|
&& entry
|
|
.args
|
|
.iter()
|
|
.zip(slots)
|
|
.all(|(want, slot)| match (want, slot.kind()) {
|
|
(Arg::PtrOpt, Arg::Ptr | Arg::PtrOpt) => true,
|
|
(a, b) => *a == b,
|
|
})
|
|
}
|
|
|
|
/// `leleka_native_flash_attn_short(q, k, v, step, max_context, padded_length, scale, out, status, mask)`.
|
|
pub fn flash_slots(max_context: i32, with_mask: bool) -> Vec<Slot> {
|
|
vec![
|
|
Slot::Ptr(Role::Q),
|
|
Slot::Ptr(Role::K),
|
|
Slot::Ptr(Role::V),
|
|
Slot::Ptr(Role::Step),
|
|
Slot::I32(max_context),
|
|
Slot::I32(flash::PADDED_LENGTH),
|
|
Slot::F32(flash::SCALE),
|
|
Slot::Ptr(Role::Out),
|
|
Slot::Ptr(Role::Status),
|
|
if with_mask {
|
|
Slot::Ptr(Role::Mask)
|
|
} else {
|
|
Slot::Null
|
|
},
|
|
]
|
|
}
|
|
pub fn flash_bytes(capacity: i64) -> Result<FlashBytes> {
|
|
if capacity < flash::MIN_CAPACITY || capacity as u64 > (usize::MAX / 1024) as u64 {
|
|
return invalid("native FlashAttention needs capacity >= 256 cells");
|
|
}
|
|
Ok(FlashBytes {
|
|
q: (flash::HEADS * flash::HEAD_DIM * 4) as u64,
|
|
kv: capacity as u64 * 1024,
|
|
step: (flash::STEP_COUNT * 4) as u64,
|
|
out: (flash::HEADS * flash::HEAD_DIM * 4) as u64,
|
|
status: 4,
|
|
mask: (flash::MAX_CONTEXT as u64) * 2,
|
|
})
|
|
}
|
|
|
|
/// `leleka_poison_f32(p, n_floats)` for one slice of the constructor's loop.
|
|
pub fn poison_slots(n_floats: u64) -> Result<Vec<Slot>> {
|
|
Ok(vec![
|
|
Slot::Ptr(Role::Poison),
|
|
Slot::I64(i64::try_from(n_floats).or_else(|_| invalid("poison slice exceeds i64"))?),
|
|
])
|
|
}
|
|
|
|
pub(super) fn s_activation_bytes(kernel: Kernel, n_in: u64) -> u64 {
|
|
use crate::s_gemv_legacy::{
|
|
Q8_0_BLOCK_BYTES, Q8_0_BLOCK_ELEMS, Q8K_BLOCK_BYTES, Q8K_BLOCK_ELEMS,
|
|
};
|
|
match kernel {
|
|
Kernel::SGemvQ8k(_) | Kernel::SGemvQ8kSplit(_) => {
|
|
n_in / Q8K_BLOCK_ELEMS as u64 * Q8K_BLOCK_BYTES as u64
|
|
}
|
|
Kernel::SGemvQ80Split(_) | Kernel::S2GemvQ8 => {
|
|
n_in / Q8_0_BLOCK_ELEMS as u64 * Q8_0_BLOCK_BYTES as u64
|
|
}
|
|
_ => n_in * 2,
|
|
}
|
|
}
|
|
|
|
/// Bytes the operands must hold: fp16 / `block_q8_K` / `block_q8_0` activation, `n_in / (8 / bits)` code bytes and
|
|
/// `n_in / group` scales (and offsets) per row, `n_out` floats out. The S2 kernels are the 2-bit, group-64 form
|
|
/// without offsets.
|
|
pub fn s_operand_bytes(kernel: Kernel, form: &SForm, n_in: i64, n_out: i64) -> Result<SBytes> {
|
|
if n_in <= 0 || n_out <= 0 {
|
|
return invalid("S-form GEMV needs positive n_in and n_out");
|
|
}
|
|
let (n_in, n_out) = (n_in as u64, n_out as u64);
|
|
let s2 = matches!(
|
|
kernel,
|
|
Kernel::S2Gemv
|
|
| Kernel::S2GemvQuads
|
|
| Kernel::S2GemvQ8
|
|
| Kernel::S2GemvFastStaged
|
|
| Kernel::S2GemvFastUnstaged
|
|
);
|
|
let (bits, group, offset) = if s2 {
|
|
(2u64, crate::s_gemv_legacy::S2_QK as u64, false)
|
|
} else {
|
|
(
|
|
form.code_bits as u64,
|
|
form.group_elems as u64,
|
|
form.has_offset,
|
|
)
|
|
};
|
|
if bits == 0 || group == 0 || n_in % group != 0 || n_in * bits % 8 != 0 {
|
|
return invalid("S-form GEMV rows must hold whole groups and whole code bytes");
|
|
}
|
|
let scales = n_out * (n_in / group) * 4;
|
|
Ok(SBytes {
|
|
x: s_activation_bytes(kernel, n_in),
|
|
codes: n_out * (n_in * bits / 8),
|
|
scales,
|
|
offset: offset.then_some(scales),
|
|
y: n_out * 4,
|
|
})
|
|
}
|