STRA-338 params e02: GR batch operations take GrBatchScope, micro attention block takes MicroBlockRequest
Files: crates/engine-qwen35-cuda/src/qwen_forward/executor_prefill_chunk/prepare_micro_ffn_rows_fn.rs crates/engine-qwen35-cuda/src/qwen_forward/executor_prefill_chunk/prepare_micro_ffn_rows_fn/gdn_native.rs crates/engine-qwen35-cuda/src/qwen_forward/executor_prefill_chunk/prepare_micro_ffn_rows_fn/plain_rows.rs crates/engine-qwen35-cuda/src/qwen_forward/executor_prefill_chunk/prepare_micro_ffn_rows_fn/qsa_native.rs
This commit is contained in:
+1
-1
@@ -7,7 +7,7 @@
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
use crate::qwen_forward::gdn::chunk_device::GdnChunkDevice;
|
||||
use crate::qwen_forward::gr::{DetachedGrRead, GrMicroAttentionBank, GrMicroAttentionBlock};
|
||||
use crate::qwen_forward::gr::{DetachedGrRead, GrMicroAttentionBank, GrMicroAttentionBlock, MicroBlockRequest};
|
||||
use crate::qwen_forward::moe_prefill_batch::MoeMicroProducts;
|
||||
use crate::qwen_forward::q6_attention_columns::ProjectionRow;
|
||||
use crate::qwen_forward::session::chunk::{ChunkAttentionRow, ChunkFfnRow, PreparedLayerRow};
|
||||
|
||||
+2
-1
@@ -60,6 +60,7 @@ fn gdn_micro_block(
|
||||
result.validate_origin(&env.binding.identity())?;
|
||||
result.identity().validate(txn.token(), txn.position(), env.layer)?;
|
||||
let (output, identity) = (result.output(), result.identity());
|
||||
let block = GrStage::prepare_micro_attention_block(env.binding, bank, local, read, output, identity, txn)?;
|
||||
let request = MicroBlockRequest { index: local, read, block: output, identity };
|
||||
let block = GrStage::prepare_micro_attention_block(env.binding, bank, request, txn)?;
|
||||
Ok((block, result.into_edge()))
|
||||
}
|
||||
|
||||
+6
-3
@@ -61,7 +61,8 @@ fn device_gdn_block<'ctx>(
|
||||
result.validate_origin(&binding.identity())?;
|
||||
result.identity().validate(token, position, layer)?;
|
||||
let (output, identity) = (result.output(), result.identity());
|
||||
let block = GrStage::prepare_micro_attention_block(binding, bank, local, read, output, identity, txn)?;
|
||||
let request = MicroBlockRequest { index: local, read, block: output, identity };
|
||||
let block = GrStage::prepare_micro_attention_block(binding, bank, request, txn)?;
|
||||
Ok((block, result.into_edge()))
|
||||
}
|
||||
|
||||
@@ -78,11 +79,13 @@ fn host_block<'ctx>(
|
||||
BoundLayerKind::Gdn => {
|
||||
let result =
|
||||
QwenForward::gdn_host_attention((binding, cache), layer, ids, (txn, read), projection.first_mut())?;
|
||||
GrStage::prepare_micro_attention_block(binding, bank, local, read, result.output(), result.identity(), txn)
|
||||
let request = MicroBlockRequest { index: local, read, block: result.output(), identity: result.identity() };
|
||||
GrStage::prepare_micro_attention_block(binding, bank, request, txn)
|
||||
}
|
||||
BoundLayerKind::Qsa => {
|
||||
let result = QwenForward::qsa_host_attention((binding, cache), layer, ids, (txn, read), projection)?;
|
||||
GrStage::prepare_micro_attention_block(binding, bank, local, read, result.output(), result.identity(), txn)
|
||||
let request = MicroBlockRequest { index: local, read, block: result.output(), identity: result.identity() };
|
||||
GrStage::prepare_micro_attention_block(binding, bank, request, txn)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+10
-16
@@ -3,12 +3,13 @@
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
use crate::qwen_forward::gr::ResidualReady;
|
||||
use crate::qwen_forward::prompt_device_rows::PromptWeights;
|
||||
use crate::qwen_forward::qsa_attention::QsaReady;
|
||||
use crate::qwen_forward::qsa_attention::{
|
||||
NativeAttended, NativePromptAttentionBatch, NativePromptAttentionBatch as Batch,
|
||||
};
|
||||
use crate::qwen_forward::qsa_native_resources::NativeQsaResources;
|
||||
use crate::qwen_forward::qsa_prefill_batch::NativeProjectionRow;
|
||||
use crate::qwen_forward::qsa_prefill_batch::{AttendedRows, NativeProjectionRow, NativeQsaDevices};
|
||||
use crate::qwen_forward::qsa_prompt_index;
|
||||
use leleka_cuda_driver::{QsaPromptAttention, QsaPromptIndexWorkspace, QsaPromptKv, QsaPromptStage};
|
||||
|
||||
@@ -51,14 +52,13 @@ fn qsa_native_project<'source, 'ctx>(
|
||||
let (binding, cache) = (env.binding, env.cache);
|
||||
let mut owner = cache.qsa_native_workspace(binding, current)?.ok_or(Error::IdentityMismatch)?;
|
||||
let mut workspace = cache.native_batch_workspace(binding, current)?.ok_or(Error::IdentityMismatch)?;
|
||||
let devices = NativeQsaDevices { owner: &mut owner, workspace: &mut workspace };
|
||||
crate::qwen_forward::qsa_prefill_batch::project(
|
||||
binding,
|
||||
cache,
|
||||
PromptWeights::new(binding, cache),
|
||||
chunk,
|
||||
env.layer,
|
||||
inputs,
|
||||
&mut owner,
|
||||
&mut workspace,
|
||||
devices,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -140,16 +140,9 @@ fn qsa_native_project_out<'ctx>(
|
||||
let mut owner = cache.qsa_native_workspace(binding, current)?.ok_or(Error::IdentityMismatch)?;
|
||||
let mut workspace = cache.native_batch_workspace(binding, current)?.ok_or(Error::IdentityMismatch)?;
|
||||
let layer = env.layer;
|
||||
crate::qwen_forward::qsa_prefill_batch::project_out(
|
||||
binding,
|
||||
cache,
|
||||
chunk,
|
||||
layer,
|
||||
attended,
|
||||
proofs,
|
||||
&mut owner,
|
||||
&mut workspace,
|
||||
)
|
||||
let devices = NativeQsaDevices { owner: &mut owner, workspace: &mut workspace };
|
||||
let rows = AttendedRows { rows: attended, proofs };
|
||||
crate::qwen_forward::qsa_prefill_batch::project_out(PromptWeights::new(binding, cache), chunk, layer, rows, devices)
|
||||
}
|
||||
|
||||
/// One attention block per row from the projected-out QSA results.
|
||||
@@ -169,7 +162,8 @@ fn qsa_native_output_blocks<'ctx>(
|
||||
result.validate_origin(&env.binding.identity(), env.layer)?;
|
||||
result.identity().validate(txn.token(), txn.position(), env.layer)?;
|
||||
let (output, identity) = (result.output(), result.identity());
|
||||
GrStage::prepare_micro_attention_block(env.binding, bank, local, read, output, identity, txn)
|
||||
let request = MicroBlockRequest { index: local, read, block: output, identity };
|
||||
GrStage::prepare_micro_attention_block(env.binding, bank, request, txn)
|
||||
})?;
|
||||
micro.blocks.push(block);
|
||||
}
|
||||
|
||||
@@ -97,7 +97,7 @@ mod batch_chunk;
|
||||
pub(in crate::qwen_forward) use batch_chunk::GrBatchRow;
|
||||
pub(in crate::qwen_forward) use batch_chunk::GrMicroAttentionBlock as GrMicroBlock;
|
||||
pub(in crate::qwen_forward) use batch_chunk::{
|
||||
GrMicroAttention, GrMicroAttentionBank, GrMicroAttentionBlock, GrMicroInput,
|
||||
GrMicroAttention, GrMicroAttentionBank, GrMicroAttentionBlock, GrMicroInput, MicroBlockRequest,
|
||||
};
|
||||
|
||||
#[path = "gr_attention_chunk.rs"]
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
//! Closed prepared producer for the actual chunk FFN injection.
|
||||
use super::*;
|
||||
#[path = "gr_batch_chunk/gr_batch_operands.rs"]
|
||||
mod gr_batch_operands;
|
||||
pub(in crate::qwen_forward) use gr_batch_operands::{GrBatchScope, MicroBlockRequest};
|
||||
|
||||
/// An immutable current-row input minted only by one of the three original
|
||||
/// producer paths, within the caller's authentic chunk phase fence.
|
||||
pub(in crate::qwen_forward) struct GrMicroInput {
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
//! The operand groups of the GR batch and micro-window operations.
|
||||
use super::*;
|
||||
|
||||
/// The model binding, the dense weight cache and the device context one GR batch operation is valid in; they
|
||||
/// always travel together.
|
||||
#[derive(Clone, Copy)]
|
||||
pub(in crate::qwen_forward) struct GrBatchScope<'a, 'ctx> {
|
||||
pub(in crate::qwen_forward) binding: &'a ModelBinding,
|
||||
pub(in crate::qwen_forward) cache: &'a DenseWeightCache<'ctx>,
|
||||
pub(in crate::qwen_forward) context: &'ctx Context<'ctx>,
|
||||
}
|
||||
|
||||
/// One micro-bank row whose attention block is prepared for publication: the row index, its detached read, the
|
||||
/// computed block and the token-layer identity the block must carry.
|
||||
#[derive(Clone, Copy)]
|
||||
pub(in crate::qwen_forward) struct MicroBlockRequest<'a> {
|
||||
pub(in crate::qwen_forward) index: usize,
|
||||
pub(in crate::qwen_forward) read: &'a DetachedGrRead,
|
||||
pub(in crate::qwen_forward) block: &'a [f32],
|
||||
pub(in crate::qwen_forward) identity: TokenLayer,
|
||||
}
|
||||
@@ -39,12 +39,11 @@ impl<'ctx> GrMicroDevice<'ctx> {
|
||||
}
|
||||
pub(in super::super) fn read_native(
|
||||
&mut self,
|
||||
binding: &ModelBinding,
|
||||
cache: &DenseWeightCache<'ctx>,
|
||||
context: &Context<'_>,
|
||||
scope: GrBatchScope<'_, 'ctx>,
|
||||
weights: [&DeviceBuffer<'_>; 4],
|
||||
epsilon: f32,
|
||||
) -> Result<leleka_cuda_driver::GrMicroValues> {
|
||||
let GrBatchScope { binding, cache, context } = scope;
|
||||
let mut workspace = cache.native_batch_workspace(binding, context)?.ok_or(Error::IdentityMismatch)?;
|
||||
match self {
|
||||
Self::Native(owner) => owner.read(weights, epsilon, &mut workspace).map_err(micro_driver_error),
|
||||
@@ -53,11 +52,10 @@ impl<'ctx> GrMicroDevice<'ctx> {
|
||||
}
|
||||
pub(in super::super) fn inject_native(
|
||||
&mut self,
|
||||
binding: &ModelBinding,
|
||||
cache: &DenseWeightCache<'ctx>,
|
||||
context: &Context<'_>,
|
||||
scope: GrBatchScope<'_, 'ctx>,
|
||||
blocks: &[f32],
|
||||
) -> Result<Box<[f32]>> {
|
||||
let GrBatchScope { binding, cache, context } = scope;
|
||||
let mut workspace = cache.native_batch_workspace(binding, context)?.ok_or(Error::IdentityMismatch)?;
|
||||
match self {
|
||||
Self::Native(owner) => owner.inject(blocks, &mut workspace).map_err(micro_driver_error),
|
||||
|
||||
@@ -94,7 +94,7 @@ impl GrStage {
|
||||
let mut device = GrMicroDevice::Legacy(
|
||||
context.gr_microbatch_read(inputs.len(), &batch.streams).map_err(micro_driver_error)?,
|
||||
);
|
||||
let env = MicroEnv { binding, cache, context };
|
||||
let env = GrBatchScope { binding, cache, context };
|
||||
let result = (|| {
|
||||
let weights = ready_micro_weights(weights)?;
|
||||
let values = device.read(weights, resident.weights.epsilon()).map_err(micro_driver_error)?;
|
||||
@@ -114,10 +114,10 @@ impl GrStage {
|
||||
let batch = collect_micro_batch(binding, &inputs)?;
|
||||
let (resident, weights) = micro_weights(cache, binding, batch.role)?;
|
||||
let mut device = open_native_micro_device(binding, cache, context, inputs.len(), &batch.streams)?;
|
||||
let env = MicroEnv { binding, cache, context };
|
||||
let env = GrBatchScope { binding, cache, context };
|
||||
let result = (|| {
|
||||
let weights = ready_micro_weights(weights)?;
|
||||
let values = device.read_native(binding, cache, context, weights, resident.weights.epsilon())?;
|
||||
let values = device.read_native(env, weights, resident.weights.epsilon())?;
|
||||
env.detach_rows(inputs, &values, &failed)
|
||||
})();
|
||||
env.seal(batch, device, failed, result)
|
||||
@@ -125,12 +125,10 @@ impl GrStage {
|
||||
pub(in crate::qwen_forward) fn prepare_micro_attention_block(
|
||||
binding: &ModelBinding,
|
||||
bank: &GrMicroAttentionBank<'_>,
|
||||
index: usize,
|
||||
read: &DetachedGrRead,
|
||||
block: &[f32],
|
||||
identity: TokenLayer,
|
||||
request: MicroBlockRequest<'_>,
|
||||
txn: &mut QwenStepTxn,
|
||||
) -> Result<GrMicroAttentionBlock> {
|
||||
let MicroBlockRequest { index, read, block, identity } = request;
|
||||
read.retained_handoff_available()?;
|
||||
txn.validate_binding(&binding.identity())?;
|
||||
check_micro_bank(bank, binding, txn)?;
|
||||
@@ -246,13 +244,7 @@ fn open_native_micro_device<'ctx>(
|
||||
.map_err(micro_driver_error)?,
|
||||
))
|
||||
}
|
||||
// What the micro read needs besides the device owner.
|
||||
struct MicroEnv<'a, 'ctx> {
|
||||
binding: &'a ModelBinding,
|
||||
cache: &'a DenseWeightCache<'ctx>,
|
||||
context: &'ctx Context<'ctx>,
|
||||
}
|
||||
impl<'a, 'ctx> MicroEnv<'a, 'ctx> {
|
||||
impl<'a, 'ctx> GrBatchScope<'a, 'ctx> {
|
||||
fn detach_rows(
|
||||
&self,
|
||||
inputs: Vec<GrMicroInput>,
|
||||
|
||||
@@ -68,7 +68,7 @@ impl GrStage {
|
||||
if let Some(values) = queued_inject(binding, context, cache, &failed, &operands)? {
|
||||
return finish_queued(binding, context, cache, rows, values);
|
||||
}
|
||||
device_inject(binding, context, cache, &failed, rows, &operands)
|
||||
device_inject(GrBatchScope { binding, cache, context }, &failed, rows, &operands)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -124,17 +124,15 @@ pub(super) fn finish_queued(
|
||||
|
||||
/// The generic device path: upload, inject, download and release every owner whatever the outcome.
|
||||
pub(super) fn device_inject<'c>(
|
||||
binding: &ModelBinding,
|
||||
context: &'c Context<'c>,
|
||||
cache: &DenseWeightCache<'_>,
|
||||
scope: GrBatchScope<'_, 'c>,
|
||||
failed: &Cell<bool>,
|
||||
rows: Vec<GrBatchRow>,
|
||||
operands: &BatchOperands,
|
||||
) -> Result<Vec<InjectedResidual>> {
|
||||
admit_budget(context, rows.len() * (2 * 40960 + 10240 + 16))?;
|
||||
admit_budget(scope.context, rows.len() * (2 * 40960 + 10240 + 16))?;
|
||||
let (mut residual, mut output, mut injection, mut candidate) = (None, None, None, None);
|
||||
let mut owners = [&mut residual, &mut output, &mut injection, &mut candidate];
|
||||
let result = inject_on_device(binding, context, cache, rows, operands, &mut owners);
|
||||
let result = inject_on_device(scope, rows, operands, &mut owners);
|
||||
// Attempt every owned release even after a CUDA or finite refusal.
|
||||
let mut cleanup = Ok(());
|
||||
for buffer in [candidate, injection, output, residual].into_iter().flatten() {
|
||||
@@ -152,13 +150,12 @@ pub(super) fn device_inject<'c>(
|
||||
type Owners<'a, 'c> = [&'a mut Option<DeviceBuffer<'c>>; 4];
|
||||
|
||||
fn inject_on_device<'c>(
|
||||
binding: &ModelBinding,
|
||||
context: &'c Context<'c>,
|
||||
cache: &DenseWeightCache<'_>,
|
||||
scope: GrBatchScope<'_, 'c>,
|
||||
rows: Vec<GrBatchRow>,
|
||||
operands: &BatchOperands,
|
||||
owners: &mut Owners<'_, 'c>,
|
||||
) -> Result<Vec<InjectedResidual>> {
|
||||
let GrBatchScope { binding, cache, context } = scope;
|
||||
let [residual, output, injection, candidate] = owners;
|
||||
**residual = Some(upload_f32(context, &operands.streams)?);
|
||||
**output = Some(upload_f32(context, &operands.blocks)?);
|
||||
|
||||
@@ -84,6 +84,10 @@ struct FfnInjection<'a, 'ctx> {
|
||||
failed: &'a Rc<Cell<bool>>,
|
||||
}
|
||||
impl<'a, 'ctx> FfnInjection<'a, 'ctx> {
|
||||
/// The binding, dense cache and context this injection runs in.
|
||||
fn scope(&self) -> batch_chunk::GrBatchScope<'a, 'ctx> {
|
||||
batch_chunk::GrBatchScope { binding: self.binding, cache: self.cache, context: self.context }
|
||||
}
|
||||
fn inject_and_read(
|
||||
&self,
|
||||
device: &mut batch_chunk::GrMicroDevice<'ctx>,
|
||||
@@ -101,7 +105,7 @@ impl<'a, 'ctx> FfnInjection<'a, 'ctx> {
|
||||
}
|
||||
fn inject_streams(&self, device: &mut batch_chunk::GrMicroDevice<'ctx>, values: &[f32]) -> Result<Box<[f32]>> {
|
||||
if matches!(&*device, batch_chunk::GrMicroDevice::Native(_)) {
|
||||
device.inject_native(self.binding, self.cache, self.context, values)
|
||||
device.inject_native(self.scope(), values)
|
||||
} else {
|
||||
device.inject(values).map_err(batch_chunk::micro_driver_error)
|
||||
}
|
||||
@@ -113,7 +117,7 @@ impl<'a, 'ctx> FfnInjection<'a, 'ctx> {
|
||||
epsilon: f32,
|
||||
) -> Result<leleka_cuda_driver::GrMicroValues> {
|
||||
if matches!(&*device, batch_chunk::GrMicroDevice::Native(_)) {
|
||||
device.read_native(self.binding, self.cache, self.context, [gamma?, down?, up?, inject?], epsilon)
|
||||
device.read_native(self.scope(), [gamma?, down?, up?, inject?], epsilon)
|
||||
} else {
|
||||
device.read([gamma?, down?, up?, inject?], epsilon).map_err(batch_chunk::micro_driver_error)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user