STRA-338 size2 cd7: qwen_ffi layout, asset and sha256 functions cut into helpers
This commit is contained in:
@@ -3,7 +3,21 @@
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
|
||||
/// The sealed asset of an operation, found in the group that holds it.
|
||||
pub(in super::super) fn asset(operation: Operation) -> Option<&'static Asset> {
|
||||
const GROUPS: [fn(Operation) -> Option<&'static Asset>; 8] = [
|
||||
asset_group_0,
|
||||
asset_group_1,
|
||||
asset_group_2,
|
||||
asset_group_3,
|
||||
asset_group_4,
|
||||
asset_group_5,
|
||||
asset_group_6,
|
||||
asset_group_7,
|
||||
];
|
||||
GROUPS.iter().find_map(|group| group(operation))
|
||||
}
|
||||
fn asset_group_0(operation: Operation) -> Option<&'static Asset> {
|
||||
match operation {
|
||||
Operation::MtpS2GateUpFull => Some(&MTP_S2_FULL_ASSETS[0]),
|
||||
Operation::MtpS2DownFull => Some(&MTP_S2_FULL_ASSETS[1]),
|
||||
@@ -13,6 +27,11 @@ pub(in super::super) fn asset(operation: Operation) -> Option<&'static Asset> {
|
||||
Operation::MtpGrMlpUp => Some(&MTP_EXTENDED_ASSETS[3]),
|
||||
Operation::MtpQuant2560 => Some(&MTP_EXTENDED_ASSETS[4]),
|
||||
Operation::MtpQuant640 => Some(&MTP_EXTENDED_ASSETS[5]),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn asset_group_1(operation: Operation) -> Option<&'static Asset> {
|
||||
match operation {
|
||||
Operation::MtpS2GateUp => Some(&MTP_EXTENDED_ASSETS[6]),
|
||||
Operation::MtpS2Down => Some(&MTP_EXTENDED_ASSETS[7]),
|
||||
Operation::MtpS2Swiglu => Some(&MTP_EXTENDED_ASSETS[8]),
|
||||
@@ -21,6 +40,11 @@ pub(in super::super) fn asset(operation: Operation) -> Option<&'static Asset> {
|
||||
Operation::MtpBroadcast => Some(&MTP_INPUT_ASSETS[2]),
|
||||
Operation::MtpDenseChunk => Some(&MTP_DENSE_ASSETS[0]),
|
||||
Operation::MtpDenseMerge => Some(&MTP_DENSE_ASSETS[1]),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn asset_group_2(operation: Operation) -> Option<&'static Asset> {
|
||||
match operation {
|
||||
Operation::KarPleScatter => Some(&KAR_PLE_ASSETS[0]),
|
||||
Operation::KarPleDecode => Some(&KAR_PLE_ASSETS[1]),
|
||||
Operation::Down => Some(&BF16_ASSETS[0]),
|
||||
@@ -29,6 +53,11 @@ pub(in super::super) fn asset(operation: Operation) -> Option<&'static Asset> {
|
||||
Operation::Bf16 => Some(&BF16_ASSETS[3]),
|
||||
Operation::PleValueFull => Some(&PLE_VALUE_FULL_ASSET),
|
||||
Operation::PleValue => Some(&PLE_ASSETS[0]),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn asset_group_3(operation: Operation) -> Option<&'static Asset> {
|
||||
match operation {
|
||||
Operation::PleNorm => Some(&PLE_ASSETS[1]),
|
||||
Operation::PleGate => Some(&PLE_ASSETS[2]),
|
||||
Operation::PleBroadcast => Some(&PLE_ASSETS[3]),
|
||||
@@ -37,6 +66,11 @@ pub(in super::super) fn asset(operation: Operation) -> Option<&'static Asset> {
|
||||
Operation::Norm => Some(&GR_ASSETS[0]),
|
||||
Operation::DownSilu => Some(&GR_ASSETS[1]),
|
||||
Operation::LayerGate => Some(&GR_ASSETS[2]),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn asset_group_4(operation: Operation) -> Option<&'static Asset> {
|
||||
match operation {
|
||||
Operation::FinalGate => Some(&GR_ASSETS[3]),
|
||||
Operation::InjectResidual => Some(&GR_ASSETS[4]),
|
||||
Operation::GdnConv => Some(&GDN_ASSETS[0]),
|
||||
@@ -45,6 +79,11 @@ pub(in super::super) fn asset(operation: Operation) -> Option<&'static Asset> {
|
||||
Operation::GdnGate => Some(&GDN_ASSETS[3]),
|
||||
Operation::GdnStep => Some(&GDN_ASSETS[4]),
|
||||
Operation::GdnOutNorm => Some(&GDN_ASSETS[5]),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn asset_group_5(operation: Operation) -> Option<&'static Asset> {
|
||||
match operation {
|
||||
Operation::RouteTop10 => Some(&ROUTE_ASSET),
|
||||
Operation::MoeSwiglu => Some(&MOE_ASSETS[0]),
|
||||
Operation::MoeSharedScalar => Some(&MOE_ASSETS[1]),
|
||||
@@ -53,6 +92,11 @@ pub(in super::super) fn asset(operation: Operation) -> Option<&'static Asset> {
|
||||
Operation::QsaSplit => Some(&QSA_ASSETS[0]),
|
||||
Operation::QsaNormQ => Some(&QSA_ASSETS[1]),
|
||||
Operation::QsaNormK => Some(&QSA_ASSETS[2]),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn asset_group_6(operation: Operation) -> Option<&'static Asset> {
|
||||
match operation {
|
||||
Operation::QsaNormIndexQ => Some(&QSA_ASSETS[3]),
|
||||
Operation::QsaRopeQ => Some(&QSA_ASSETS[4]),
|
||||
Operation::QsaRopeK => Some(&QSA_ASSETS[5]),
|
||||
@@ -61,8 +105,37 @@ pub(in super::super) fn asset(operation: Operation) -> Option<&'static Asset> {
|
||||
Operation::QsaPackKv => Some(&QSA_ASSETS[8]),
|
||||
Operation::QsaBlockScores => Some(&QSA_ASSETS[9]),
|
||||
Operation::QsaBlockTopk => Some(&QSA_ASSETS[10]),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn asset_group_7(operation: Operation) -> Option<&'static Asset> {
|
||||
match operation {
|
||||
Operation::QsaAttend => Some(&QSA_ASSETS[11]),
|
||||
Operation::QsaGate => Some(&QSA_ASSETS[12]),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
/// Point the launch parameter array of `storage` at its own fields.
|
||||
fn wire_parameters(storage: *mut Storage, operation: Operation, layout: &Layout) {
|
||||
// SAFETY: uniquely raw-owned stable allocation. No Box/reference
|
||||
// retagging occurs after these self-referential field addresses form.
|
||||
unsafe {
|
||||
for i in 0..layout.count {
|
||||
let parameter = i + usize::from(layout.integer.is_some_and(|(_, index)| index <= i));
|
||||
(*storage).parameters[parameter] = (&raw mut (*storage).addresses[i]).cast();
|
||||
}
|
||||
if let Some((_, index)) = layout.integer {
|
||||
(*storage).parameters[index] = (&raw mut (*storage).integer).cast();
|
||||
}
|
||||
if operation == Operation::MtpDenseChunk {
|
||||
(*storage).parameters[6] = (&raw mut (*storage).rows).cast();
|
||||
(*storage).parameters[7] = (&raw mut (*storage).columns).cast();
|
||||
} else if layout.projection {
|
||||
(*storage).parameters[3] = (&raw mut (*storage).rows).cast();
|
||||
(*storage).parameters[4] = (&raw mut (*storage).columns).cast();
|
||||
} else if operation.has_epsilon() {
|
||||
(*storage).parameters[layout.count] = (&raw mut (*storage).epsilon).cast();
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Arguments {
|
||||
@@ -87,26 +160,7 @@ impl Arguments {
|
||||
integer: layout.integer.map_or(0, |(value, _)| value),
|
||||
parameters: [std::ptr::null_mut(); 10],
|
||||
}));
|
||||
// SAFETY: uniquely raw-owned stable allocation. No Box/reference
|
||||
// retagging occurs after these self-referential field addresses form.
|
||||
unsafe {
|
||||
for i in 0..layout.count {
|
||||
let parameter = i + usize::from(layout.integer.is_some_and(|(_, index)| index <= i));
|
||||
(*storage).parameters[parameter] = (&raw mut (*storage).addresses[i]).cast();
|
||||
}
|
||||
if let Some((_, index)) = layout.integer {
|
||||
(*storage).parameters[index] = (&raw mut (*storage).integer).cast();
|
||||
}
|
||||
if operation == Operation::MtpDenseChunk {
|
||||
(*storage).parameters[6] = (&raw mut (*storage).rows).cast();
|
||||
(*storage).parameters[7] = (&raw mut (*storage).columns).cast();
|
||||
} else if layout.projection {
|
||||
(*storage).parameters[3] = (&raw mut (*storage).rows).cast();
|
||||
(*storage).parameters[4] = (&raw mut (*storage).columns).cast();
|
||||
} else if operation.has_epsilon() {
|
||||
(*storage).parameters[layout.count] = (&raw mut (*storage).epsilon).cast();
|
||||
}
|
||||
}
|
||||
wire_parameters(storage, operation, &layout);
|
||||
Self {
|
||||
storage: NonNull::new(storage).expect("Box allocation is non-null"),
|
||||
layout,
|
||||
@@ -146,68 +200,24 @@ impl Drop for Arguments {
|
||||
}
|
||||
}
|
||||
|
||||
const SHA256_K: [u32; 64] = [
|
||||
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98,
|
||||
0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
|
||||
0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8,
|
||||
0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
|
||||
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819,
|
||||
0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
|
||||
0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7,
|
||||
0xc67178f2,
|
||||
];
|
||||
pub(crate) fn sha256(bytes: &[u8]) -> [u8; 32] {
|
||||
const K: [u32; 64] = [
|
||||
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98,
|
||||
0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
|
||||
0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8,
|
||||
0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
|
||||
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819,
|
||||
0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
|
||||
0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7,
|
||||
0xc67178f2,
|
||||
];
|
||||
let mut state: [u32; 8] =
|
||||
[0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19];
|
||||
let bit_length = (bytes.len() as u64).wrapping_mul(8).to_be_bytes();
|
||||
let blocks = bytes.len() / 64 + if bytes.len() % 64 < 56 { 1 } else { 2 };
|
||||
for block in 0..blocks {
|
||||
let mut data = [0u8; 64];
|
||||
for (i, b) in data.iter_mut().enumerate() {
|
||||
let position = block * 64 + i;
|
||||
*b = if position < bytes.len() {
|
||||
bytes[position]
|
||||
} else if position == bytes.len() {
|
||||
0x80
|
||||
} else if position >= blocks * 64 - 8 {
|
||||
bit_length[position - (blocks * 64 - 8)]
|
||||
} else {
|
||||
0
|
||||
};
|
||||
}
|
||||
let mut w = [0u32; 64];
|
||||
for (i, chunk) in data.as_chunks::<4>().0.iter().enumerate() {
|
||||
w[i] = u32::from_be_bytes(*chunk);
|
||||
}
|
||||
for i in 16..64 {
|
||||
let x = w[i - 15];
|
||||
let y = w[i - 2];
|
||||
w[i] = w[i - 16]
|
||||
.wrapping_add(x.rotate_right(7) ^ x.rotate_right(18) ^ (x >> 3))
|
||||
.wrapping_add(w[i - 7])
|
||||
.wrapping_add(y.rotate_right(17) ^ y.rotate_right(19) ^ (y >> 10));
|
||||
}
|
||||
let [mut a, mut b, mut c, mut d, mut e, mut f, mut g, mut h] = state;
|
||||
for i in 0..64 {
|
||||
let t1 = h
|
||||
.wrapping_add(e.rotate_right(6) ^ e.rotate_right(11) ^ e.rotate_right(25))
|
||||
.wrapping_add((e & f) ^ (!e & g))
|
||||
.wrapping_add(K[i])
|
||||
.wrapping_add(w[i]);
|
||||
let t2 =
|
||||
(a.rotate_right(2) ^ a.rotate_right(13) ^ a.rotate_right(22)).wrapping_add((a & b) ^ (a & c) ^ (b & c));
|
||||
h = g;
|
||||
g = f;
|
||||
f = e;
|
||||
e = d.wrapping_add(t1);
|
||||
d = c;
|
||||
c = b;
|
||||
b = a;
|
||||
a = t1.wrapping_add(t2);
|
||||
}
|
||||
for (old, new) in state.iter_mut().zip([a, b, c, d, e, f, g, h, 0]) {
|
||||
*old = old.wrapping_add(new);
|
||||
}
|
||||
let w = sha256_schedule(&sha256_block(bytes, (block, blocks), bit_length));
|
||||
sha256_compress(&mut state, &w);
|
||||
}
|
||||
let mut out = [0u8; 32];
|
||||
for (chunk, value) in out.as_chunks_mut::<4>().0.iter_mut().zip(state) {
|
||||
@@ -215,3 +225,60 @@ pub(crate) fn sha256(bytes: &[u8]) -> [u8; 32] {
|
||||
}
|
||||
out
|
||||
}
|
||||
/// The padded 64-byte block `block` of `blocks`: the message, a 0x80 byte, zeros and the bit length.
|
||||
fn sha256_block(bytes: &[u8], (block, blocks): (usize, usize), bit_length: [u8; 8]) -> [u8; 64] {
|
||||
let mut data = [0u8; 64];
|
||||
for (i, b) in data.iter_mut().enumerate() {
|
||||
let position = block * 64 + i;
|
||||
*b = if position < bytes.len() {
|
||||
bytes[position]
|
||||
} else if position == bytes.len() {
|
||||
0x80
|
||||
} else if position >= blocks * 64 - 8 {
|
||||
bit_length[position - (blocks * 64 - 8)]
|
||||
} else {
|
||||
0
|
||||
};
|
||||
}
|
||||
data
|
||||
}
|
||||
/// The 64-word message schedule of a block.
|
||||
fn sha256_schedule(data: &[u8; 64]) -> [u32; 64] {
|
||||
let mut w = [0u32; 64];
|
||||
for (i, chunk) in data.as_chunks::<4>().0.iter().enumerate() {
|
||||
w[i] = u32::from_be_bytes(*chunk);
|
||||
}
|
||||
for i in 16..64 {
|
||||
let x = w[i - 15];
|
||||
let y = w[i - 2];
|
||||
w[i] = w[i - 16]
|
||||
.wrapping_add(x.rotate_right(7) ^ x.rotate_right(18) ^ (x >> 3))
|
||||
.wrapping_add(w[i - 7])
|
||||
.wrapping_add(y.rotate_right(17) ^ y.rotate_right(19) ^ (y >> 10));
|
||||
}
|
||||
w
|
||||
}
|
||||
/// The 64 rounds over one scheduled block, added into `state`.
|
||||
fn sha256_compress(state: &mut [u32; 8], w: &[u32; 64]) {
|
||||
let [mut a, mut b, mut c, mut d, mut e, mut f, mut g, mut h] = *state;
|
||||
for i in 0..64 {
|
||||
let t1 = h
|
||||
.wrapping_add(e.rotate_right(6) ^ e.rotate_right(11) ^ e.rotate_right(25))
|
||||
.wrapping_add((e & f) ^ (!e & g))
|
||||
.wrapping_add(SHA256_K[i])
|
||||
.wrapping_add(w[i]);
|
||||
let t2 =
|
||||
(a.rotate_right(2) ^ a.rotate_right(13) ^ a.rotate_right(22)).wrapping_add((a & b) ^ (a & c) ^ (b & c));
|
||||
h = g;
|
||||
g = f;
|
||||
f = e;
|
||||
e = d.wrapping_add(t1);
|
||||
d = c;
|
||||
c = b;
|
||||
b = a;
|
||||
a = t1.wrapping_add(t2);
|
||||
}
|
||||
for (old, new) in state.iter_mut().zip([a, b, c, d, e, f, g, h, 0]) {
|
||||
*old = old.wrapping_add(new);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,205 +4,183 @@
|
||||
use super::*;
|
||||
|
||||
impl Operation {
|
||||
/// The kernel entry point of an operation, found in the group that holds it.
|
||||
pub(in super::super) fn entry(self) -> &'static CStr {
|
||||
const GROUPS: [fn(Operation) -> Option<&'static CStr>; 7] = [
|
||||
Operation::entry_group_0,
|
||||
Operation::entry_group_1,
|
||||
Operation::entry_group_2,
|
||||
Operation::entry_group_3,
|
||||
Operation::entry_group_4,
|
||||
Operation::entry_group_5,
|
||||
Operation::entry_group_6,
|
||||
];
|
||||
GROUPS.iter().find_map(|group| group(self)).expect("every operation has an entry point")
|
||||
}
|
||||
fn entry_group_0(self) -> Option<&'static CStr> {
|
||||
match self {
|
||||
Self::Down | Self::Up | Self::InjectProjection | Self::Bf16 => c"leleka_bf16_f32_mmvf",
|
||||
Self::Norm => c"leleka_gr_norm_f32",
|
||||
Self::DownSilu => c"leleka_gr_down_silu_hc_f32",
|
||||
Self::LayerGate => c"leleka_gr_gate_mean_layer_fused_f32",
|
||||
Self::FinalGate => c"leleka_gr_gate_mean_final_unfused_f32",
|
||||
Self::InjectResidual => c"leleka_gr_inject_residual_f32",
|
||||
Self::RouteTop10 => c"leleka_moe_route_top10",
|
||||
Self::GdnConv => c"leleka_gdn_conv_silu_f32",
|
||||
Self::GdnQk => c"leleka_gdn_qk_l2_f32",
|
||||
Self::GdnBeta => c"leleka_gdn_beta_sigmoid_f32",
|
||||
Self::GdnGate => c"leleka_gdn_gate_softplus_f32",
|
||||
Self::GdnStep => c"leleka_gdn_step_f32",
|
||||
Self::GdnOutNorm => c"leleka_gdn_out_norm_f32",
|
||||
Self::PleValue => c"leleka_ple_value_bf16_f32",
|
||||
Self::PleValueFull => c"leleka_ple_value_full_bf16_f32",
|
||||
Self::PleNorm => c"leleka_ple_grouped_norm_f32",
|
||||
Self::PleGate => c"leleka_ple_signed_gate_f32",
|
||||
Self::PleBroadcast => c"leleka_ple_broadcast_f32",
|
||||
Self::PleConv => c"leleka_ple_conv_history_f32",
|
||||
Self::PleResidual => c"leleka_ple_residual_f32",
|
||||
Self::MoeSwiglu => c"leleka_moe_swiglu_f32",
|
||||
Self::MoeSharedScalar => c"leleka_moe_shared_gate_f32_bf16",
|
||||
Self::MoeSharedScale => c"leleka_moe_scale_shared_f32",
|
||||
Self::MoeCombine => c"leleka_moe_combine_f32",
|
||||
Self::QsaSplit => c"leleka_qsa_split_q_gate",
|
||||
Self::QsaNormQ => c"leleka_qsa_norm_q",
|
||||
Self::QsaNormK => c"leleka_qsa_norm_k",
|
||||
Self::QsaNormIndexQ => c"leleka_qsa_norm_index_q",
|
||||
Self::QsaRopeQ => c"leleka_qsa_rope_q",
|
||||
Self::QsaRopeK => c"leleka_qsa_rope_k",
|
||||
Self::QsaRopeIndexQ => c"leleka_qsa_rope_index_q",
|
||||
Self::QsaIndexAppend => c"leleka_qsa_index_append",
|
||||
Self::QsaPackKv => c"leleka_qsa_pack_kv",
|
||||
Self::QsaBlockScores => c"leleka_qsa_block_scores",
|
||||
Self::QsaBlockTopk => c"leleka_qsa_block_topk",
|
||||
Self::QsaAttend => c"leleka_qsa_attend",
|
||||
Self::QsaGate => c"leleka_qsa_gate_f32",
|
||||
Self::KarPleScatter => c"leleka_kar_ple_scatter",
|
||||
Self::KarPleDecode => c"leleka_kar_ple_decode",
|
||||
Self::MtpEmbeddingNorm => c"leleka_mtp_embedding_norm_f32",
|
||||
Self::MtpHiddenNorm => c"leleka_mtp_hidden_norm_f32",
|
||||
Self::MtpBroadcast => c"leleka_mtp_broadcast_f32",
|
||||
Self::MtpDenseChunk => c"leleka_mtp_attn_chunk_f16",
|
||||
Self::MtpDenseMerge => c"leleka_mtp_attn_merge_f32",
|
||||
Self::MtpGrAttentionDown => c"leleka_mtp_gr_attention_down_f32_bf16",
|
||||
Self::MtpGrAttentionUp => c"leleka_mtp_gr_attention_up_f32_bf16",
|
||||
Self::MtpGrMlpDown => c"leleka_mtp_gr_mlp_down_f32_bf16",
|
||||
Self::MtpGrMlpUp => c"leleka_mtp_gr_mlp_up_f32_bf16",
|
||||
Self::MtpQuant2560 => c"leleka_mtp_quantize_scaled_2560",
|
||||
Self::MtpQuant640 => c"leleka_mtp_quantize_scaled_640",
|
||||
Self::MtpS2GateUp => c"leleka_mtp_s2_gate_up_tile",
|
||||
Self::MtpS2Down => c"leleka_mtp_s2_down_tile",
|
||||
Self::MtpS2Swiglu => c"leleka_mtp_swiglu_640",
|
||||
Self::MtpS2GateUpFull => c"leleka_mtp_s2_gate_up_full",
|
||||
Self::MtpS2DownFull => c"leleka_mtp_s2_down_full",
|
||||
Self::Down | Self::Up | Self::InjectProjection | Self::Bf16 => Some(c"leleka_bf16_f32_mmvf"),
|
||||
Self::Norm => Some(c"leleka_gr_norm_f32"),
|
||||
Self::DownSilu => Some(c"leleka_gr_down_silu_hc_f32"),
|
||||
Self::LayerGate => Some(c"leleka_gr_gate_mean_layer_fused_f32"),
|
||||
Self::FinalGate => Some(c"leleka_gr_gate_mean_final_unfused_f32"),
|
||||
Self::InjectResidual => Some(c"leleka_gr_inject_residual_f32"),
|
||||
Self::RouteTop10 => Some(c"leleka_moe_route_top10"),
|
||||
Self::GdnConv => Some(c"leleka_gdn_conv_silu_f32"),
|
||||
Self::GdnQk => Some(c"leleka_gdn_qk_l2_f32"),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn entry_group_1(self) -> Option<&'static CStr> {
|
||||
match self {
|
||||
Self::GdnBeta => Some(c"leleka_gdn_beta_sigmoid_f32"),
|
||||
Self::GdnGate => Some(c"leleka_gdn_gate_softplus_f32"),
|
||||
Self::GdnStep => Some(c"leleka_gdn_step_f32"),
|
||||
Self::GdnOutNorm => Some(c"leleka_gdn_out_norm_f32"),
|
||||
Self::PleValue => Some(c"leleka_ple_value_bf16_f32"),
|
||||
Self::PleValueFull => Some(c"leleka_ple_value_full_bf16_f32"),
|
||||
Self::PleNorm => Some(c"leleka_ple_grouped_norm_f32"),
|
||||
Self::PleGate => Some(c"leleka_ple_signed_gate_f32"),
|
||||
Self::PleBroadcast => Some(c"leleka_ple_broadcast_f32"),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn entry_group_2(self) -> Option<&'static CStr> {
|
||||
match self {
|
||||
Self::PleConv => Some(c"leleka_ple_conv_history_f32"),
|
||||
Self::PleResidual => Some(c"leleka_ple_residual_f32"),
|
||||
Self::MoeSwiglu => Some(c"leleka_moe_swiglu_f32"),
|
||||
Self::MoeSharedScalar => Some(c"leleka_moe_shared_gate_f32_bf16"),
|
||||
Self::MoeSharedScale => Some(c"leleka_moe_scale_shared_f32"),
|
||||
Self::MoeCombine => Some(c"leleka_moe_combine_f32"),
|
||||
Self::QsaSplit => Some(c"leleka_qsa_split_q_gate"),
|
||||
Self::QsaNormQ => Some(c"leleka_qsa_norm_q"),
|
||||
Self::QsaNormK => Some(c"leleka_qsa_norm_k"),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn entry_group_3(self) -> Option<&'static CStr> {
|
||||
match self {
|
||||
Self::QsaNormIndexQ => Some(c"leleka_qsa_norm_index_q"),
|
||||
Self::QsaRopeQ => Some(c"leleka_qsa_rope_q"),
|
||||
Self::QsaRopeK => Some(c"leleka_qsa_rope_k"),
|
||||
Self::QsaRopeIndexQ => Some(c"leleka_qsa_rope_index_q"),
|
||||
Self::QsaIndexAppend => Some(c"leleka_qsa_index_append"),
|
||||
Self::QsaPackKv => Some(c"leleka_qsa_pack_kv"),
|
||||
Self::QsaBlockScores => Some(c"leleka_qsa_block_scores"),
|
||||
Self::QsaBlockTopk => Some(c"leleka_qsa_block_topk"),
|
||||
Self::QsaAttend => Some(c"leleka_qsa_attend"),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn entry_group_4(self) -> Option<&'static CStr> {
|
||||
match self {
|
||||
Self::QsaGate => Some(c"leleka_qsa_gate_f32"),
|
||||
Self::KarPleScatter => Some(c"leleka_kar_ple_scatter"),
|
||||
Self::KarPleDecode => Some(c"leleka_kar_ple_decode"),
|
||||
Self::MtpEmbeddingNorm => Some(c"leleka_mtp_embedding_norm_f32"),
|
||||
Self::MtpHiddenNorm => Some(c"leleka_mtp_hidden_norm_f32"),
|
||||
Self::MtpBroadcast => Some(c"leleka_mtp_broadcast_f32"),
|
||||
Self::MtpDenseChunk => Some(c"leleka_mtp_attn_chunk_f16"),
|
||||
Self::MtpDenseMerge => Some(c"leleka_mtp_attn_merge_f32"),
|
||||
Self::MtpGrAttentionDown => Some(c"leleka_mtp_gr_attention_down_f32_bf16"),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn entry_group_5(self) -> Option<&'static CStr> {
|
||||
match self {
|
||||
Self::MtpGrAttentionUp => Some(c"leleka_mtp_gr_attention_up_f32_bf16"),
|
||||
Self::MtpGrMlpDown => Some(c"leleka_mtp_gr_mlp_down_f32_bf16"),
|
||||
Self::MtpGrMlpUp => Some(c"leleka_mtp_gr_mlp_up_f32_bf16"),
|
||||
Self::MtpQuant2560 => Some(c"leleka_mtp_quantize_scaled_2560"),
|
||||
Self::MtpQuant640 => Some(c"leleka_mtp_quantize_scaled_640"),
|
||||
Self::MtpS2GateUp => Some(c"leleka_mtp_s2_gate_up_tile"),
|
||||
Self::MtpS2Down => Some(c"leleka_mtp_s2_down_tile"),
|
||||
Self::MtpS2Swiglu => Some(c"leleka_mtp_swiglu_640"),
|
||||
Self::MtpS2GateUpFull => Some(c"leleka_mtp_s2_gate_up_full"),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
fn entry_group_6(self) -> Option<&'static CStr> {
|
||||
match self {
|
||||
Self::MtpS2DownFull => Some(c"leleka_mtp_s2_down_full"),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
pub(in super::super) fn layout(self) -> Layout {
|
||||
match self.mtp_family_layout().or_else(|| self.other_family_layout()) {
|
||||
Some(layout) => layout,
|
||||
None => self.elementwise_layout(),
|
||||
}
|
||||
}
|
||||
/// The MTP, dense-attention and KAR PLE operations have fixed geometries of their own.
|
||||
fn mtp_family_layout(self) -> Option<Layout> {
|
||||
if matches!(self, Self::MtpS2GateUpFull | Self::MtpS2DownFull) {
|
||||
return mtp_s2_full_layout(self, 0).expect("fixed first expert geometry");
|
||||
}
|
||||
if let Some(layout) = mtp_extended_layout(self) {
|
||||
return layout;
|
||||
}
|
||||
if let Some(layout) = mtp_input_layout(self) {
|
||||
return layout;
|
||||
return Some(mtp_s2_full_layout(self, 0).expect("fixed first expert geometry"));
|
||||
}
|
||||
mtp_extended_layout(self).or_else(|| mtp_input_layout(self)).or_else(|| self.dense_kar_layout())
|
||||
}
|
||||
fn dense_kar_layout(self) -> Option<Layout> {
|
||||
if matches!(self, Self::MtpDenseChunk | Self::MtpDenseMerge) {
|
||||
return mtp_dense_layout(self, 0).expect("fixed minimum MTP dense geometry");
|
||||
return Some(mtp_dense_layout(self, 0).expect("fixed minimum MTP dense geometry"));
|
||||
}
|
||||
if matches!(self, Self::KarPleScatter | Self::KarPleDecode) {
|
||||
return kar_ple_layout(self, 1).expect("fixed minimum KAR PLE geometry");
|
||||
return Some(kar_ple_layout(self, 1).expect("fixed minimum KAR PLE geometry"));
|
||||
}
|
||||
None
|
||||
}
|
||||
/// The QSA, MoE, PLE, GDN, router and BF16 projection operations.
|
||||
fn other_family_layout(self) -> Option<Layout> {
|
||||
if QSA_OPERATIONS.contains(&self) {
|
||||
return qsa_layout(self, u32::from(self == Self::QsaAttend)).expect("fixed minimum QSA geometry");
|
||||
return Some(qsa_layout(self, u32::from(self == Self::QsaAttend)).expect("fixed minimum QSA geometry"));
|
||||
}
|
||||
if let Some(layout) = moe_layout(self) {
|
||||
return layout;
|
||||
}
|
||||
if let Some(layout) = ple_layout(self) {
|
||||
return layout;
|
||||
}
|
||||
if let Some(layout) = gdn_layout(self) {
|
||||
return layout;
|
||||
}
|
||||
if self == Self::RouteTop10 {
|
||||
return Layout {
|
||||
bytes: [2048, 40, 40, 0, 0, 0, 0, 0, 0],
|
||||
align: [4; 9],
|
||||
count: 3,
|
||||
outputs: 6,
|
||||
rows: 512,
|
||||
columns: 1,
|
||||
grid: 1,
|
||||
grid_y: 1,
|
||||
shared_bytes: 0,
|
||||
integer: None,
|
||||
grid_z: 1,
|
||||
block: 32,
|
||||
block_y: 8,
|
||||
projection: false,
|
||||
};
|
||||
}
|
||||
match self {
|
||||
Self::Down => return bf16_layout(H, HC * N).expect("fixed down geometry"),
|
||||
Self::Up => return bf16_layout(HC * N, H).expect("fixed up geometry"),
|
||||
Self::InjectProjection => {
|
||||
return bf16_layout(HC, HC * N).expect("fixed inject geometry");
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
if self == Self::Bf16 {
|
||||
moe_layout(self)
|
||||
.or_else(|| ple_layout(self))
|
||||
.or_else(|| gdn_layout(self))
|
||||
.or_else(|| self.router_projection_layout())
|
||||
}
|
||||
fn router_projection_layout(self) -> Option<Layout> {
|
||||
Some(match self {
|
||||
Self::RouteTop10 => router_layout(),
|
||||
Self::Down => bf16_layout(H, HC * N).expect("fixed down geometry"),
|
||||
Self::Up => bf16_layout(HC * N, H).expect("fixed up geometry"),
|
||||
Self::InjectProjection => bf16_layout(HC, HC * N).expect("fixed inject geometry"),
|
||||
// Only a private CPU fixture uses this minimal layout. Public
|
||||
// BF16 calls always supply the fully checked actual dimensions.
|
||||
return bf16_layout(1, 2).expect("fixed minimal BF16 geometry");
|
||||
}
|
||||
// Native indices and geometry are bounded constants, never supplied by
|
||||
// a caller. Each projection uses W:u64,X:u64,Y:u64,M:u32,N:u32.
|
||||
// Other entries use their ordered buffer pointers, then epsilon only
|
||||
// for Norm; fixed N/HC/H live in the qualified executable contract.
|
||||
Self::Bf16 => bf16_layout(1, 2).expect("fixed minimal BF16 geometry"),
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
/// Native indices and geometry are bounded constants, never supplied by
|
||||
/// a caller. Each projection uses W:u64,X:u64,Y:u64,M:u32,N:u32.
|
||||
/// Other entries use their ordered buffer pointers, then epsilon only
|
||||
/// for Norm; fixed N/HC/H live in the qualified executable contract.
|
||||
fn elementwise_layout(self) -> Layout {
|
||||
let (bytes, align, count, outputs, rows, columns, block) = match self {
|
||||
Self::MtpEmbeddingNorm
|
||||
| Self::MtpGrAttentionDown
|
||||
| Self::MtpGrAttentionUp
|
||||
| Self::MtpGrMlpDown
|
||||
| Self::MtpGrMlpUp
|
||||
| Self::MtpQuant2560
|
||||
| Self::MtpQuant640
|
||||
| Self::MtpS2GateUp
|
||||
| Self::MtpS2Down
|
||||
| Self::MtpS2Swiglu
|
||||
| Self::MtpS2GateUpFull
|
||||
| Self::MtpS2DownFull
|
||||
| Self::MtpHiddenNorm
|
||||
| Self::MtpBroadcast
|
||||
| Self::MtpDenseChunk
|
||||
| Self::MtpDenseMerge => unreachable!("fixed MTP geometry above"),
|
||||
Self::KarPleScatter | Self::KarPleDecode => unreachable!("KAR PLE geometry above"),
|
||||
Self::QsaSplit
|
||||
| Self::QsaNormQ
|
||||
| Self::QsaNormK
|
||||
| Self::QsaNormIndexQ
|
||||
| Self::QsaRopeQ
|
||||
| Self::QsaRopeK
|
||||
| Self::QsaRopeIndexQ
|
||||
| Self::QsaIndexAppend
|
||||
| Self::QsaPackKv
|
||||
| Self::QsaBlockScores
|
||||
| Self::QsaBlockTopk
|
||||
| Self::QsaAttend
|
||||
| Self::QsaGate => unreachable!("fixed QSA geometry above"),
|
||||
Self::Down | Self::Up | Self::InjectProjection => {
|
||||
unreachable!("checked fixed MMVF geometry above")
|
||||
}
|
||||
Self::Norm => ([4 * HC * N, 4 * HC * N, 4 * HC * N, 0], [4; 4], 3, 4, HC, N, 1024),
|
||||
Self::DownSilu => ([4 * H, 4 * H, 0, 0], [4; 4], 2, 2, H, 1, 256),
|
||||
Self::LayerGate | Self::FinalGate => {
|
||||
([4 * HC * N, 4 * HC * N, 4 * HC * N, 4 * N], [4; 4], 4, 12, N, HC, 256)
|
||||
}
|
||||
Self::InjectResidual => ([4 * HC * N, 4 * N, 4 * HC, 4 * HC * N], [4; 4], 4, 8, HC * N, 1, 256),
|
||||
Self::Bf16 => unreachable!("minimal checked geometry above"),
|
||||
Self::RouteTop10 => unreachable!("fixed router geometry above"),
|
||||
Self::GdnConv | Self::GdnQk | Self::GdnBeta | Self::GdnGate | Self::GdnStep | Self::GdnOutNorm => {
|
||||
unreachable!("fixed GDN geometry above")
|
||||
}
|
||||
Self::PleValue
|
||||
| Self::PleValueFull
|
||||
| Self::PleNorm
|
||||
| Self::PleGate
|
||||
| Self::PleBroadcast
|
||||
| Self::PleConv
|
||||
| Self::PleResidual => unreachable!("fixed PLE geometry above"),
|
||||
Self::MoeSwiglu | Self::MoeSharedScalar | Self::MoeSharedScale | Self::MoeCombine => {
|
||||
unreachable!("fixed MoE geometry above")
|
||||
}
|
||||
_ => unreachable!("every other operation has a fixed geometry above"),
|
||||
};
|
||||
let projection = matches!(self, Self::Down | Self::Up | Self::InjectProjection);
|
||||
let grid = if projection || self == Self::Norm { rows } else { rows.div_ceil(block) };
|
||||
let grid = if self == Self::Norm { rows } else { rows.div_ceil(block) };
|
||||
let mut full_bytes = [0; 9];
|
||||
full_bytes[..4].copy_from_slice(&bytes);
|
||||
let mut full_align = [1; 9];
|
||||
full_align[..4].copy_from_slice(&align);
|
||||
Layout {
|
||||
bytes: full_bytes,
|
||||
align: full_align,
|
||||
count,
|
||||
outputs,
|
||||
rows: rows as u32,
|
||||
columns: columns as u32,
|
||||
grid: grid as u32,
|
||||
grid_y: 1,
|
||||
shared_bytes: 0,
|
||||
integer: None,
|
||||
grid_z: 1,
|
||||
block: block as u32,
|
||||
block_y: 1,
|
||||
projection,
|
||||
}
|
||||
Layout::flat(
|
||||
full_bytes,
|
||||
full_align,
|
||||
(count, outputs),
|
||||
(rows as u32, columns as u32),
|
||||
(grid as u32, block as u32),
|
||||
)
|
||||
}
|
||||
}
|
||||
/// The fixed router: logits, ids and weights.
|
||||
fn router_layout() -> Layout {
|
||||
let flat = Layout::flat([2048, 40, 40, 0, 0, 0, 0, 0, 0], [4; 9], (3, 6), (512, 1), (1, 32));
|
||||
Layout { block_y: 8, ..flat }
|
||||
}
|
||||
|
||||
@@ -47,12 +47,29 @@ pub(in super::super) fn bf16_layout(rows: usize, columns: usize) -> Result<Layou
|
||||
{
|
||||
return Err(invalid);
|
||||
}
|
||||
let [weights, activation, output] = bf16_bytes(rows, columns, &invalid)?;
|
||||
let block = bf16_block(columns);
|
||||
let flat = Layout::flat(
|
||||
[weights, activation, output, 0, 0, 0, 0, 0, 0],
|
||||
[4, 8, 4, 1, 1, 1, 1, 1, 0],
|
||||
(3, 4),
|
||||
(rows as u32, columns as u32),
|
||||
(rows as u32, block as u32),
|
||||
);
|
||||
Ok(Layout { projection: true, ..flat })
|
||||
}
|
||||
/// The byte sizes of the weights, the activation and the output of a BF16 projection.
|
||||
fn bf16_bytes(rows: usize, columns: usize, invalid: &Error) -> Result<[usize; 3]> {
|
||||
let weights = rows.checked_mul(columns).and_then(|v| v.checked_mul(2)).ok_or_else(|| invalid.clone())?;
|
||||
let activation = columns.checked_mul(4).ok_or_else(|| invalid.clone())?;
|
||||
let output = rows.checked_mul(4).ok_or_else(|| invalid.clone())?;
|
||||
for bytes in [weights, activation, output] {
|
||||
crate::valid_size(bytes).map_err(|_| invalid.clone())?;
|
||||
}
|
||||
Ok([weights, activation, output])
|
||||
}
|
||||
/// The block size, a multiple of 32 up to 256, with the fewest iterations over the columns.
|
||||
fn bf16_block(columns: usize) -> usize {
|
||||
let mut block = 32;
|
||||
let mut best = columns.div_ceil(64);
|
||||
for candidate in (64..=256).step_by(32) {
|
||||
@@ -62,22 +79,7 @@ pub(in super::super) fn bf16_layout(rows: usize, columns: usize) -> Result<Layou
|
||||
block = candidate;
|
||||
}
|
||||
}
|
||||
Ok(Layout {
|
||||
bytes: [weights, activation, output, 0, 0, 0, 0, 0, 0],
|
||||
align: [4, 8, 4, 1, 1, 1, 1, 1, 0],
|
||||
count: 3,
|
||||
outputs: 4,
|
||||
rows: rows as u32,
|
||||
columns: columns as u32,
|
||||
grid: rows as u32,
|
||||
grid_y: 1,
|
||||
shared_bytes: 0,
|
||||
integer: None,
|
||||
grid_z: 1,
|
||||
block: block as u32,
|
||||
block_y: 1,
|
||||
projection: true,
|
||||
})
|
||||
block
|
||||
}
|
||||
|
||||
pub(super) fn inspect_ptx(ptx: &'static [u8]) -> Result<[u8; 32]> {
|
||||
@@ -90,25 +92,31 @@ impl Asset {
|
||||
pub(in super::super) fn validate(&self, operation: Operation) -> Result<()> {
|
||||
self.validate_with_memo(operation, &PTX_PROOFS[operation as usize], inspect_ptx)
|
||||
}
|
||||
/// The asset is for `operation`, its receipt is its pin and carries the fixed topology.
|
||||
fn identity_admitted(&self, operation: Operation) -> bool {
|
||||
let topology = if operation == Operation::RouteTop10 { [512, 10, 1] } else { [N as u32, HC as u32, H as u32] };
|
||||
self.operation == operation
|
||||
&& self.receipt == self.pin
|
||||
&& self.receipt.version == 1
|
||||
&& self.receipt.abi_version == 1
|
||||
&& self.receipt.topology == topology
|
||||
}
|
||||
/// Every provenance field of the receipt is filled in.
|
||||
fn receipt_complete(&self) -> bool {
|
||||
self.receipt.source_sha256 != [0; 32]
|
||||
&& self.receipt.oracle_sha256 != [0; 32]
|
||||
&& !self.receipt.compiler.is_empty()
|
||||
&& !self.receipt.compiler_flags.is_empty()
|
||||
&& !self.receipt.qualification_receipt.is_empty()
|
||||
&& !self.receipt.comparison.is_empty()
|
||||
}
|
||||
pub(super) fn validate_with_memo(
|
||||
&self,
|
||||
operation: Operation,
|
||||
memo: &OnceLock<PtxProof>,
|
||||
inspect: impl FnOnce(&'static [u8]) -> Result<[u8; 32]>,
|
||||
) -> Result<()> {
|
||||
if self.operation != operation
|
||||
|| self.receipt != self.pin
|
||||
|| self.receipt.version != 1
|
||||
|| self.receipt.abi_version != 1
|
||||
|| self.receipt.topology
|
||||
!= if operation == Operation::RouteTop10 { [512, 10, 1] } else { [N as u32, HC as u32, H as u32] }
|
||||
|| self.receipt.source_sha256 == [0; 32]
|
||||
|| self.receipt.oracle_sha256 == [0; 32]
|
||||
|| self.receipt.compiler.is_empty()
|
||||
|| self.receipt.compiler_flags.is_empty()
|
||||
|| self.receipt.qualification_receipt.is_empty()
|
||||
|| self.receipt.comparison.is_empty()
|
||||
{
|
||||
if !self.identity_admitted(operation) || !self.receipt_complete() {
|
||||
return Err(Error::UnsupportedQwenOperation);
|
||||
}
|
||||
// Slice pointer equality includes length. Equal bytes at another
|
||||
|
||||
@@ -3,6 +3,35 @@
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
|
||||
/// bytes, align, count, outputs, grid, block of a fixed MTP launch.
|
||||
type Geometry = ([usize; 9], [u64; 9], usize, u16, u32, u32);
|
||||
impl Layout {
|
||||
/// A launch on a single block row: `grid_y = grid_z = block_y = 1`, no shared memory and no integer scalar.
|
||||
pub(super) fn flat(
|
||||
bytes: [usize; 9],
|
||||
align: [u64; 9],
|
||||
(count, outputs): (usize, u16),
|
||||
(rows, columns): (u32, u32),
|
||||
(grid, block): (u32, u32),
|
||||
) -> Self {
|
||||
Self {
|
||||
bytes,
|
||||
align,
|
||||
count,
|
||||
outputs,
|
||||
rows,
|
||||
columns,
|
||||
grid,
|
||||
grid_y: 1,
|
||||
grid_z: 1,
|
||||
shared_bytes: 0,
|
||||
integer: None,
|
||||
block,
|
||||
block_y: 1,
|
||||
projection: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
pub(in super::super) fn mtp_s2_full_layout(operation: Operation, expert: u32) -> Result<Layout> {
|
||||
if expert >= 512 {
|
||||
return Err(Error::InvalidQwenShape);
|
||||
@@ -20,26 +49,23 @@ pub(in super::super) fn mtp_s2_full_layout(operation: Operation, expert: u32) ->
|
||||
Operation::MtpS2DownFull => (680, 80, 10240),
|
||||
_ => return Err(Error::InvalidQwenShape),
|
||||
};
|
||||
Ok(Layout {
|
||||
bytes: [707_788_800, q, scales, out, 0, 0, 0, 0, 0],
|
||||
align: [8, 4, 4, 4, 1, 1, 1, 1, 1],
|
||||
count: 4,
|
||||
outputs: 8,
|
||||
rows: 2560,
|
||||
columns: 1,
|
||||
grid: 40,
|
||||
grid_y: 1,
|
||||
grid_z: 1,
|
||||
shared_bytes: 0,
|
||||
integer: Some((expert, 1)),
|
||||
block: 256,
|
||||
block_y: 1,
|
||||
projection: false,
|
||||
})
|
||||
let flat = Layout::flat(
|
||||
[707_788_800, q, scales, out, 0, 0, 0, 0, 0],
|
||||
[8, 4, 4, 4, 1, 1, 1, 1, 1],
|
||||
(4, 8),
|
||||
(2560, 1),
|
||||
(40, 256),
|
||||
);
|
||||
Ok(Layout { integer: Some((expert, 1)), ..flat })
|
||||
}
|
||||
|
||||
pub(super) fn mtp_extended_layout(operation: Operation) -> Option<Layout> {
|
||||
let (bytes, align, count, outputs, grid, block) = match operation {
|
||||
let (bytes, align, count, outputs, grid, block) =
|
||||
mtp_gr_geometry(operation).or_else(|| mtp_tile_geometry(operation))?;
|
||||
Some(Layout::flat(bytes, align, (count, outputs), (2560, 4), (grid, block)))
|
||||
}
|
||||
fn mtp_gr_geometry(operation: Operation) -> Option<Geometry> {
|
||||
Some(match operation {
|
||||
Operation::MtpGrAttentionDown => {
|
||||
([40960, 40960, 6553600, 81920, 1280, 16, 16, 0, 0], [16, 16, 16, 16, 16, 4, 4, 1, 1], 7, 112, 41, 256)
|
||||
}
|
||||
@@ -62,6 +88,11 @@ pub(super) fn mtp_extended_layout(operation: Operation) -> Option<Layout> {
|
||||
80,
|
||||
256,
|
||||
),
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
fn mtp_tile_geometry(operation: Operation) -> Option<Geometry> {
|
||||
Some(match operation {
|
||||
Operation::MtpQuant2560 => ([10240, 2720, 320, 0, 0, 0, 0, 0, 0], [4, 4, 4, 1, 1, 1, 1, 1, 1], 3, 6, 1, 128),
|
||||
Operation::MtpQuant640 => ([2560, 680, 80, 0, 0, 0, 0, 0, 0], [4, 4, 4, 1, 1, 1, 1, 1, 1], 3, 6, 1, 128),
|
||||
Operation::MtpS2GateUp => {
|
||||
@@ -70,22 +101,6 @@ pub(super) fn mtp_extended_layout(operation: Operation) -> Option<Layout> {
|
||||
Operation::MtpS2Down => ([10240, 1280, 680, 80, 256, 0, 0, 0, 0], [8, 2, 4, 4, 4, 1, 1, 1, 1], 5, 16, 1, 256),
|
||||
Operation::MtpS2Swiglu => ([5120, 2560, 0, 0, 0, 0, 0, 0, 0], [4, 4, 1, 1, 1, 1, 1, 1, 1], 2, 2, 3, 256),
|
||||
_ => return None,
|
||||
};
|
||||
Some(Layout {
|
||||
bytes,
|
||||
align,
|
||||
count,
|
||||
outputs,
|
||||
rows: 2560,
|
||||
columns: 4,
|
||||
grid,
|
||||
grid_y: 1,
|
||||
grid_z: 1,
|
||||
shared_bytes: 0,
|
||||
integer: None,
|
||||
block,
|
||||
block_y: 1,
|
||||
projection: false,
|
||||
})
|
||||
}
|
||||
pub(super) fn mtp_input_layout(operation: Operation) -> Option<Layout> {
|
||||
@@ -95,22 +110,7 @@ pub(super) fn mtp_input_layout(operation: Operation) -> Option<Layout> {
|
||||
Operation::MtpBroadcast => ([40960, 10240, 40960, 0, 0, 0, 0, 0, 0], 3, 10240, 2560, 40, 256),
|
||||
_ => return None,
|
||||
};
|
||||
Some(Layout {
|
||||
bytes,
|
||||
align: [4; 9],
|
||||
count,
|
||||
outputs: 4,
|
||||
rows,
|
||||
columns,
|
||||
grid,
|
||||
grid_y: 1,
|
||||
grid_z: 1,
|
||||
shared_bytes: 0,
|
||||
integer: None,
|
||||
block,
|
||||
block_y: 1,
|
||||
projection: false,
|
||||
})
|
||||
Some(Layout::flat(bytes, [4; 9], (count, 4), (rows, columns), (grid, block)))
|
||||
}
|
||||
|
||||
/// Position derives the entire causal window and launch geometry. The caller
|
||||
@@ -138,20 +138,6 @@ pub(in super::super) fn mtp_dense_layout(operation: Operation, position: u32) ->
|
||||
}
|
||||
_ => return Err(Error::InvalidQwenShape),
|
||||
};
|
||||
Ok(Layout {
|
||||
bytes,
|
||||
align,
|
||||
count,
|
||||
outputs,
|
||||
rows: width,
|
||||
columns: chunks,
|
||||
grid,
|
||||
grid_y,
|
||||
grid_z: 1,
|
||||
shared_bytes: 0,
|
||||
integer,
|
||||
block: 256,
|
||||
block_y: 1,
|
||||
projection: false,
|
||||
})
|
||||
let flat = Layout::flat(bytes, align, (count, outputs), (width, chunks), (grid, 256));
|
||||
Ok(Layout { grid_y, integer, ..flat })
|
||||
}
|
||||
|
||||
@@ -3,9 +3,18 @@
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
|
||||
/// bytes, count, outputs, grid, grid_y and the integer scalar of a QSA launch.
|
||||
type QsaGeometry = ([usize; 9], usize, u16, u32, u32, Option<(u32, usize)>);
|
||||
/// Closed source-defined QSA extents. Position and selected width are the only
|
||||
/// semantic scalars; native index/launch calculations never come from callers.
|
||||
pub(in super::super) fn qsa_layout(operation: Operation, value: u32) -> Result<Layout> {
|
||||
check_qsa_value(operation, value)?;
|
||||
let (bytes, count, outputs, grid, grid_y, integer) = qsa_geometry(operation, value)?;
|
||||
let flat = Layout::flat(bytes, qsa_align(operation), (count, outputs), (24, 256), (grid, 256));
|
||||
let shared_bytes = if operation == Operation::QsaAttend { 8332 } else { 0 };
|
||||
Ok(Layout { grid_y, integer, shared_bytes, ..flat })
|
||||
}
|
||||
fn check_qsa_value(operation: Operation, value: u32) -> Result<()> {
|
||||
if !QSA_OPERATIONS.contains(&operation) {
|
||||
return Err(Error::InvalidQwenShape);
|
||||
}
|
||||
@@ -16,11 +25,17 @@ pub(in super::super) fn qsa_layout(operation: Operation, value: u32) -> Result<L
|
||||
} else if value >= crate::MAX_CONTEXT_CELLS as u32 {
|
||||
return Err(Error::InvalidQwenShape);
|
||||
}
|
||||
let rows = value.checked_add(1).ok_or(Error::InvalidQwenShape)? / 4 + 1;
|
||||
let pooled = usize::try_from(rows).ok().and_then(|n| n.checked_mul(512)).ok_or(Error::InvalidQwenShape)?;
|
||||
let scores = usize::try_from(rows).ok().and_then(|n| n.checked_mul(4)).ok_or(Error::InvalidQwenShape)?;
|
||||
let packed = usize::try_from(value).ok().and_then(|n| n.checked_mul(1024)).ok_or(Error::InvalidQwenShape)?;
|
||||
let (bytes, count, outputs, grid, grid_y, integer) = match operation {
|
||||
Ok(())
|
||||
}
|
||||
fn qsa_geometry(operation: Operation, value: u32) -> Result<QsaGeometry> {
|
||||
match qsa_plain_geometry(operation, value) {
|
||||
Some(geometry) => Ok(geometry),
|
||||
None => qsa_sized_geometry(operation, value),
|
||||
}
|
||||
}
|
||||
/// The QSA launches whose extents are fixed.
|
||||
fn qsa_plain_geometry(operation: Operation, value: u32) -> Option<QsaGeometry> {
|
||||
Some(match operation {
|
||||
Operation::QsaSplit => ([49152, 24576, 24576, 0, 0, 0, 0, 0, 0], 3, 6, 24, 1, None),
|
||||
Operation::QsaNormQ => ([24576, 1024, 24576, 0, 0, 0, 0, 0, 0], 3, 4, 24, 1, None),
|
||||
Operation::QsaNormK => ([2048, 1024, 2048, 0, 0, 0, 0, 0, 0], 3, 4, 2, 1, None),
|
||||
@@ -28,14 +43,26 @@ pub(in super::super) fn qsa_layout(operation: Operation, value: u32) -> Result<L
|
||||
Operation::QsaRopeQ => ([24576, 24576, 0, 0, 0, 0, 0, 0, 0], 2, 2, 1, 24, Some((value, 1))),
|
||||
Operation::QsaRopeK => ([2048, 2048, 0, 0, 0, 0, 0, 0, 0], 2, 2, 1, 2, Some((value, 1))),
|
||||
Operation::QsaRopeIndexQ => ([2048, 2048, 0, 0, 0, 0, 0, 0, 0], 2, 2, 1, 4, Some((value, 1))),
|
||||
Operation::QsaIndexAppend => ([512, 512, 2048, 2560, 0, 0, 0, 0, 0], 4, 8, 1, 1, Some((value, 4))),
|
||||
Operation::QsaPackKv => ([2048, 2048, 1024, 1024, 0, 0, 0, 0, 0], 4, 12, 2, 1, None),
|
||||
Operation::QsaGate => ([24576, 24576, 24576, 0, 0, 0, 0, 0, 0], 3, 4, 24, 1, None),
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
/// The QSA launches whose extents follow the position or the selected width.
|
||||
fn qsa_sized_geometry(operation: Operation, value: u32) -> Result<QsaGeometry> {
|
||||
let rows = value.checked_add(1).ok_or(Error::InvalidQwenShape)? / 4 + 1;
|
||||
let pooled = usize::try_from(rows).ok().and_then(|n| n.checked_mul(512)).ok_or(Error::InvalidQwenShape)?;
|
||||
let scores = usize::try_from(rows).ok().and_then(|n| n.checked_mul(4)).ok_or(Error::InvalidQwenShape)?;
|
||||
let packed = usize::try_from(value).ok().and_then(|n| n.checked_mul(1024)).ok_or(Error::InvalidQwenShape)?;
|
||||
Ok(match operation {
|
||||
Operation::QsaIndexAppend => ([512, 512, 2048, 2560, 0, 0, 0, 0, 0], 4, 8, 1, 1, Some((value, 4))),
|
||||
Operation::QsaBlockScores => ([pooled, 512, 2048, scores, 0, 0, 0, 0, 0], 4, 8, 256, 1, Some((value, 4))),
|
||||
Operation::QsaBlockTopk => ([scores, 8204, 0, 0, 0, 0, 0, 0, 0], 2, 2, 1, 1, Some((value, 2))),
|
||||
Operation::QsaAttend => ([24576, packed, packed, 24576, 0, 0, 0, 0, 0], 4, 8, 24, 1, Some((value, 4))),
|
||||
Operation::QsaGate => ([24576, 24576, 24576, 0, 0, 0, 0, 0, 0], 3, 4, 24, 1, None),
|
||||
_ => return Err(Error::InvalidQwenShape),
|
||||
};
|
||||
})
|
||||
}
|
||||
fn qsa_align(operation: Operation) -> [u64; 9] {
|
||||
let mut align = [4; 9];
|
||||
match operation {
|
||||
Operation::QsaPackKv => {
|
||||
@@ -48,22 +75,7 @@ pub(in super::super) fn qsa_layout(operation: Operation, value: u32) -> Result<L
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
Ok(Layout {
|
||||
bytes,
|
||||
align,
|
||||
count,
|
||||
outputs,
|
||||
rows: 24,
|
||||
columns: 256,
|
||||
grid,
|
||||
grid_y,
|
||||
grid_z: 1,
|
||||
block: 256,
|
||||
block_y: 1,
|
||||
projection: false,
|
||||
integer,
|
||||
shared_bytes: if operation == Operation::QsaAttend { 8332 } else { 0 },
|
||||
})
|
||||
align
|
||||
}
|
||||
|
||||
pub(super) fn ple_layout(operation: Operation) -> Option<Layout> {
|
||||
@@ -83,22 +95,7 @@ pub(super) fn ple_layout(operation: Operation) -> Option<Layout> {
|
||||
Operation::PleConv => align[2] = 2,
|
||||
_ => (),
|
||||
}
|
||||
Some(Layout {
|
||||
bytes,
|
||||
align,
|
||||
count,
|
||||
outputs,
|
||||
rows: 4,
|
||||
columns: 2560,
|
||||
grid,
|
||||
grid_y: 1,
|
||||
shared_bytes: 0,
|
||||
integer: None,
|
||||
grid_z: 1,
|
||||
block: 256,
|
||||
block_y: 1,
|
||||
projection: false,
|
||||
})
|
||||
Some(Layout::flat(bytes, align, (count, outputs), (4, 2560), (grid, 256)))
|
||||
}
|
||||
|
||||
pub(super) fn moe_layout(operation: Operation) -> Option<Layout> {
|
||||
|
||||
Reference in New Issue
Block a user