fix(runtime): identify failed forward and GR stage precisely
This commit is contained in:
@@ -18,6 +18,35 @@ use super::{
|
||||
};
|
||||
use crate::model_binding::qwen4exp_input::{BoundLayerKind, ModelBinding};
|
||||
|
||||
/// Fixed operator labels only; diagnostics never contain request/model data.
|
||||
#[derive(Debug)]
|
||||
enum ForwardPhase {
|
||||
Input,
|
||||
ReadAttention,
|
||||
Ple,
|
||||
Gdn,
|
||||
QsaIndex,
|
||||
QsaAttention,
|
||||
GrAttentionInject,
|
||||
GrFfn,
|
||||
MoeRoute,
|
||||
MoeExpert,
|
||||
GrFfnInject,
|
||||
Output,
|
||||
}
|
||||
|
||||
/// Stderr failures cannot replace the original stage error or alter rollback.
|
||||
fn diagnose<T>(phase: ForwardPhase, layer: u32, pos: Position, result: Result<T>) -> Result<T> {
|
||||
result.inspect_err(|error| {
|
||||
use std::io::Write;
|
||||
let _ = writeln!(
|
||||
std::io::stderr().lock(),
|
||||
"leleka-native phase={phase:?} layer={layer} position={} error={error:?}",
|
||||
pos.get(),
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) struct QwenForward;
|
||||
|
||||
impl QwenForward {
|
||||
@@ -68,13 +97,20 @@ impl QwenForward {
|
||||
// touching any model rows or invoking a stage.
|
||||
let layer_bound = validate_layer_plan(binding)?;
|
||||
|
||||
let input = match (binding.kar_ple_binding(), kar_cache) {
|
||||
(None, None) => InputStage::consume(binding, token, pos, txn)?,
|
||||
(Some(_), Some(cache)) => InputStage::consume_kar(binding, cache, token, pos, txn)?,
|
||||
// Never interpret a missing or wrong loaded provider as a
|
||||
// request to switch modes or fall back to the native rows.
|
||||
_ => return Err(Error::IdentityMismatch),
|
||||
};
|
||||
let input = diagnose(
|
||||
ForwardPhase::Input,
|
||||
0,
|
||||
pos,
|
||||
match (binding.kar_ple_binding(), kar_cache) {
|
||||
(None, None) => InputStage::consume(binding, token, pos, txn),
|
||||
(Some(_), Some(cache)) => {
|
||||
InputStage::consume_kar(binding, cache, token, pos, txn)
|
||||
}
|
||||
// Never interpret a missing or wrong loaded provider as a
|
||||
// request to switch modes or fall back to the native rows.
|
||||
_ => Err(Error::IdentityMismatch),
|
||||
},
|
||||
)?;
|
||||
txn.stage_input(input)?;
|
||||
|
||||
let mut previous = None;
|
||||
@@ -82,56 +118,109 @@ impl QwenForward {
|
||||
#[cfg(test)]
|
||||
eprintln!("integration: starting layer {}", descriptor.index());
|
||||
let layer = LayerId::new(u64::from(descriptor.index()), layer_bound)?;
|
||||
let residual = read_layer_attention(binding, layer, previous.as_ref(), txn)?;
|
||||
let residual = diagnose(
|
||||
ForwardPhase::ReadAttention,
|
||||
layer.get(),
|
||||
pos,
|
||||
read_layer_attention(binding, layer, previous.as_ref(), txn),
|
||||
)?;
|
||||
let injected = match descriptor.kind() {
|
||||
BoundLayerKind::Gdn => {
|
||||
let prior = txn.gdn_prior(layer)?;
|
||||
let mut attention = GdnStage::step(binding, layer, &residual, &prior, txn)?;
|
||||
let mut attention = diagnose(
|
||||
ForwardPhase::Gdn,
|
||||
layer.get(),
|
||||
pos,
|
||||
GdnStage::step(binding, layer, &residual, &prior, txn),
|
||||
)?;
|
||||
attention.validate_origin(&binding.identity())?;
|
||||
attention.identity().validate(token, pos, layer)?;
|
||||
let pending = attention.take_pending()?;
|
||||
txn.stage_gdn(&prior, pending)?;
|
||||
GrStage::inject(
|
||||
binding,
|
||||
&residual,
|
||||
attention.output(),
|
||||
attention.identity(),
|
||||
txn,
|
||||
diagnose(
|
||||
ForwardPhase::GrAttentionInject,
|
||||
layer.get(),
|
||||
pos,
|
||||
GrStage::inject(
|
||||
binding,
|
||||
&residual,
|
||||
attention.output(),
|
||||
attention.identity(),
|
||||
txn,
|
||||
),
|
||||
)?
|
||||
}
|
||||
BoundLayerKind::Qsa => {
|
||||
let prior = txn.qsa_prior(layer)?;
|
||||
let mut prepared =
|
||||
QsaIndexStage::prepare(binding, layer, &residual, pos, &prior, txn)?;
|
||||
let mut prepared = diagnose(
|
||||
ForwardPhase::QsaIndex,
|
||||
layer.get(),
|
||||
pos,
|
||||
QsaIndexStage::prepare(binding, layer, &residual, pos, &prior, txn),
|
||||
)?;
|
||||
prepared.validate_origin(&binding.identity(), layer)?;
|
||||
prepared.identity().validate(token, pos, layer)?;
|
||||
let pending = prepared.take_pending()?;
|
||||
txn.stage_qsa(&prior, pending)?;
|
||||
let attention = QsaAttentionStage::decode(binding, layer, prepared, txn)?;
|
||||
let attention = diagnose(
|
||||
ForwardPhase::QsaAttention,
|
||||
layer.get(),
|
||||
pos,
|
||||
QsaAttentionStage::decode(binding, layer, prepared, txn),
|
||||
)?;
|
||||
attention.validate_origin(&binding.identity(), layer)?;
|
||||
attention.identity().validate(token, pos, layer)?;
|
||||
GrStage::inject(
|
||||
binding,
|
||||
&residual,
|
||||
attention.output(),
|
||||
attention.identity(),
|
||||
txn,
|
||||
diagnose(
|
||||
ForwardPhase::GrAttentionInject,
|
||||
layer.get(),
|
||||
pos,
|
||||
GrStage::inject(
|
||||
binding,
|
||||
&residual,
|
||||
attention.output(),
|
||||
attention.identity(),
|
||||
txn,
|
||||
),
|
||||
)?
|
||||
}
|
||||
};
|
||||
let ffn = GrStage::read_ffn(binding, layer, &injected, txn)?;
|
||||
let route = MoeRouteStage::route_from_ffn(binding, layer, &ffn, txn)?;
|
||||
let experts = MoeExpertStage::execute(binding, layer, route, &ffn, txn)?;
|
||||
let ffn = diagnose(
|
||||
ForwardPhase::GrFfn,
|
||||
layer.get(),
|
||||
pos,
|
||||
GrStage::read_ffn(binding, layer, &injected, txn),
|
||||
)?;
|
||||
let route = diagnose(
|
||||
ForwardPhase::MoeRoute,
|
||||
layer.get(),
|
||||
pos,
|
||||
MoeRouteStage::route_from_ffn(binding, layer, &ffn, txn),
|
||||
)?;
|
||||
let experts = diagnose(
|
||||
ForwardPhase::MoeExpert,
|
||||
layer.get(),
|
||||
pos,
|
||||
MoeExpertStage::execute(binding, layer, route, &ffn, txn),
|
||||
)?;
|
||||
experts.validate_origin(&binding.identity())?;
|
||||
experts.identity().validate(token, pos, layer)?;
|
||||
let completed =
|
||||
GrStage::inject(binding, &ffn, experts.output(), experts.identity(), txn)?;
|
||||
let completed = diagnose(
|
||||
ForwardPhase::GrFfnInject,
|
||||
layer.get(),
|
||||
pos,
|
||||
GrStage::inject(binding, &ffn, experts.output(), experts.identity(), txn),
|
||||
)?;
|
||||
txn.mark_layer_complete(&completed)?;
|
||||
previous = Some(completed);
|
||||
}
|
||||
let output = output.ok_or(Error::Unsupported(Stage::Output))?;
|
||||
let final_residual = previous.as_ref().ok_or(Error::InvalidDimension)?;
|
||||
let logits = output.logits(binding, final_residual, txn)?;
|
||||
let logits = diagnose(
|
||||
ForwardPhase::Output,
|
||||
binding.layer_count().saturating_sub(1),
|
||||
pos,
|
||||
output.logits(binding, final_residual, txn),
|
||||
)?;
|
||||
txn.finish_forward(logits)
|
||||
})
|
||||
}
|
||||
@@ -160,7 +249,12 @@ fn read_layer_attention(
|
||||
1 => {
|
||||
let previous = previous.ok_or(Error::IdentityMismatch)?;
|
||||
let prior = txn.ple_prior()?;
|
||||
let mut ple = InputStage::apply_ple(binding, previous, &prior, txn)?;
|
||||
let mut ple = diagnose(
|
||||
ForwardPhase::Ple,
|
||||
layer.get(),
|
||||
txn.position(),
|
||||
InputStage::apply_ple(binding, previous, &prior, txn),
|
||||
)?;
|
||||
ple.validate_origin(&binding.identity())?;
|
||||
ple.identity()
|
||||
.validate(txn.token(), txn.position(), layer)?;
|
||||
|
||||
@@ -15,6 +15,48 @@ use leleka_cuda_driver::{Context, DeviceBuffer, Error as DriverError};
|
||||
const READ_DEVICE_BYTES: usize =
|
||||
2 * 10240 * 320 * 2 + 5 * 10240 * 4 + 2 * 320 * 4 + 2560 * 4 + 4 * 10240 * 2 + 4 * 4;
|
||||
|
||||
// Closed diagnostic labels contain no tensor, request or artifact data.
|
||||
#[derive(Debug)]
|
||||
enum GrPhase {
|
||||
Inputs,
|
||||
Weights,
|
||||
Norm,
|
||||
Down,
|
||||
Silu,
|
||||
Up,
|
||||
GateProducts,
|
||||
GateMixed,
|
||||
InjectProjection,
|
||||
}
|
||||
|
||||
fn diagnose<T>(phase: GrPhase, result: Result<T>) -> Result<T> {
|
||||
result.inspect_err(|error| {
|
||||
use std::io::Write;
|
||||
// Preserve the real error even when stderr is unavailable.
|
||||
let _ = writeln!(
|
||||
std::io::stderr().lock(),
|
||||
"leleka-gr phase={phase:?} error={error:?}"
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
/// Only the three fixed failure sites below use existing finite readbacks.
|
||||
/// This emits two aggregate extrema, never elements or extra CUDA readbacks.
|
||||
fn diagnose_extrema<T>(phase: GrPhase, result: Result<T>, finite_input: &[f32]) -> Result<T> {
|
||||
result.inspect_err(|error| {
|
||||
use std::io::Write;
|
||||
let min = finite_input.iter().copied().fold(f32::INFINITY, f32::min);
|
||||
let max = finite_input
|
||||
.iter()
|
||||
.copied()
|
||||
.fold(f32::NEG_INFINITY, f32::max);
|
||||
let _ = writeln!(
|
||||
std::io::stderr().lock(),
|
||||
"leleka-gr phase={phase:?} error={error:?} input_min={min} input_max={max}"
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
struct ReadCandidate {
|
||||
mixed: Box<[f32]>,
|
||||
inject: Option<[f32; 4]>,
|
||||
@@ -196,10 +238,13 @@ impl GrStage {
|
||||
txn.validate_binding(&binding.identity())?;
|
||||
txn.validate_input(identity.token(), identity.position())?;
|
||||
validate_layer(identity.layer(), binding.layer_count())?;
|
||||
validate_streams(streams)?;
|
||||
diagnose(GrPhase::Inputs, validate_streams(streams))?;
|
||||
let context = txn.cuda_context()?;
|
||||
admit_budget(context, READ_DEVICE_BYTES)?;
|
||||
let weights = binding.gr_weights(role).map_err(binding_error)?;
|
||||
let weights = diagnose(
|
||||
GrPhase::Weights,
|
||||
binding.gr_weights(role).map_err(binding_error),
|
||||
)?;
|
||||
if !weights.matches(&binding.identity(), role) {
|
||||
return Err(Error::IdentityMismatch);
|
||||
}
|
||||
@@ -235,10 +280,13 @@ impl GrStage {
|
||||
residual
|
||||
.identity
|
||||
.validate(txn.token(), txn.position(), residual.identity.layer())?;
|
||||
validate_streams(residual.streams())?;
|
||||
diagnose(GrPhase::Inputs, validate_streams(residual.streams()))?;
|
||||
let context = txn.cuda_context()?;
|
||||
admit_budget(context, READ_DEVICE_BYTES)?;
|
||||
let weights = binding.gr_weights(GrRole::Final).map_err(binding_error)?;
|
||||
let weights = diagnose(
|
||||
GrPhase::Weights,
|
||||
binding.gr_weights(GrRole::Final).map_err(binding_error),
|
||||
)?;
|
||||
if !weights.matches(&binding.identity(), GrRole::Final) || weights.inject().is_some() {
|
||||
return Err(Error::IdentityMismatch);
|
||||
}
|
||||
@@ -303,43 +351,78 @@ fn read_cuda(
|
||||
let mut gates = context.allocate(40960).map_err(driver_error)?;
|
||||
let mut products = context.allocate(40960).map_err(driver_error)?;
|
||||
let mut mixed = context.allocate(10240).map_err(driver_error)?;
|
||||
context
|
||||
.gr_norm_f32(&residual, &gamma, weights.epsilon(), &mut xn)
|
||||
.map_err(driver_error)?;
|
||||
download_f32(&xn, 10240)?;
|
||||
context
|
||||
.gr_down_projection_bf16_f32(&down_weights, &xn, &mut down)
|
||||
.map_err(driver_error)?;
|
||||
download_f32(&down, 320)?;
|
||||
context
|
||||
.gr_down_silu_hc_f32(&down, &mut activated)
|
||||
.map_err(driver_error)?;
|
||||
download_f32(&activated, 320)?;
|
||||
context
|
||||
.gr_up_projection_bf16_f32(&up_weights, &activated, &mut gates)
|
||||
.map_err(driver_error)?;
|
||||
download_f32(&gates, 10240)?;
|
||||
diagnose(
|
||||
GrPhase::Norm,
|
||||
context
|
||||
.gr_norm_f32(&residual, &gamma, weights.epsilon(), &mut xn)
|
||||
.map_err(driver_error),
|
||||
)?;
|
||||
let xn_values = diagnose(GrPhase::Norm, download_f32(&xn, 10240))?;
|
||||
diagnose(
|
||||
GrPhase::Down,
|
||||
context
|
||||
.gr_down_projection_bf16_f32(&down_weights, &xn, &mut down)
|
||||
.map_err(driver_error),
|
||||
)?;
|
||||
let down_values = diagnose(GrPhase::Down, download_f32(&down, 320))?;
|
||||
diagnose_extrema(
|
||||
GrPhase::Silu,
|
||||
context
|
||||
.gr_down_silu_hc_f32(&down, &mut activated)
|
||||
.map_err(driver_error),
|
||||
&down_values,
|
||||
)?;
|
||||
diagnose_extrema(GrPhase::Silu, download_f32(&activated, 320), &down_values)?;
|
||||
drop(down_values);
|
||||
diagnose(
|
||||
GrPhase::Up,
|
||||
context
|
||||
.gr_up_projection_bf16_f32(&up_weights, &activated, &mut gates)
|
||||
.map_err(driver_error),
|
||||
)?;
|
||||
let gate_values = diagnose(GrPhase::Up, download_f32(&gates, 10240))?;
|
||||
if final_mix {
|
||||
context
|
||||
.gr_gate_mean_final_unfused_f32(&xn, &gates, &mut products, &mut mixed)
|
||||
.map_err(driver_error)?;
|
||||
diagnose_extrema(
|
||||
GrPhase::GateProducts,
|
||||
context
|
||||
.gr_gate_mean_final_unfused_f32(&xn, &gates, &mut products, &mut mixed)
|
||||
.map_err(driver_error),
|
||||
&gate_values,
|
||||
)?;
|
||||
} else {
|
||||
context
|
||||
.gr_gate_mean_layer_fused_f32(&xn, &gates, &mut products, &mut mixed)
|
||||
.map_err(driver_error)?;
|
||||
diagnose_extrema(
|
||||
GrPhase::GateProducts,
|
||||
context
|
||||
.gr_gate_mean_layer_fused_f32(&xn, &gates, &mut products, &mut mixed)
|
||||
.map_err(driver_error),
|
||||
&gate_values,
|
||||
)?;
|
||||
}
|
||||
download_f32(&products, 10240)?;
|
||||
let mixed_values = download_f32(&mixed, 2560)?;
|
||||
diagnose_extrema(
|
||||
GrPhase::GateProducts,
|
||||
download_f32(&products, 10240),
|
||||
&gate_values,
|
||||
)?;
|
||||
drop(gate_values);
|
||||
let mixed_values = diagnose(GrPhase::GateMixed, download_f32(&mixed, 2560))?;
|
||||
let inject_values = if let Some(values) = weights.inject() {
|
||||
if final_mix {
|
||||
return Err(Error::IdentityMismatch);
|
||||
}
|
||||
let matrix = upload_bf16(context, values)?;
|
||||
let mut output = context.allocate(16).map_err(driver_error)?;
|
||||
context
|
||||
.gr_inject_projection_bf16_f32(&matrix, &xn, &mut output)
|
||||
.map_err(driver_error)?;
|
||||
let values = download_f32(&output, 4)?;
|
||||
diagnose_extrema(
|
||||
GrPhase::InjectProjection,
|
||||
context
|
||||
.gr_inject_projection_bf16_f32(&matrix, &xn, &mut output)
|
||||
.map_err(driver_error),
|
||||
&xn_values,
|
||||
)?;
|
||||
let values = diagnose_extrema(
|
||||
GrPhase::InjectProjection,
|
||||
download_f32(&output, 4),
|
||||
&xn_values,
|
||||
)?;
|
||||
let result = Some(
|
||||
values
|
||||
.as_ref()
|
||||
@@ -352,6 +435,7 @@ fn read_cuda(
|
||||
} else {
|
||||
None
|
||||
};
|
||||
drop(xn_values);
|
||||
// Successful publication includes confirmed explicit resource teardown.
|
||||
for buffer in [
|
||||
mixed,
|
||||
|
||||
Reference in New Issue
Block a user