From 2b75a037d29ee67570372fa85b9d6edfd7b0efab Mon Sep 17 00:00:00 2001 From: Pavlo Ratushnyi Date: Sat, 14 Nov 2026 22:28:15 +0100 Subject: [PATCH] hot path (not for main before the identity run): the prefill GEMM loan and the profile drain are generics, not trait objects (engine dyn 9 -> 3) PrefillLoans<'a, 'ctx, G: GdnGemm> and the GDN and MoE prefill stage functions take the loaned GEMM by type; prefill_profile::span takes the Context itself (it was the only implementer of Drain). The same calls in the same order; static instead of dynamic dispatch. Not run on a GPU: needs the laptop two-binary identity gate. Co-Authored-By: Claude Sonnet 5.5 --- architecture-baseline.json | 2 +- .../chunk_run/prompt_chunk_forward.rs | 2 +- .../prompt_chunk_forward/forward_draft.rs | 2 +- .../prompt_chunk_forward/forward_final.rs | 2 +- .../prompt_chunk_forward/forward_input.rs | 2 +- .../prompt_chunk_forward/forward_layers.rs | 10 +++++----- .../src/qwen_forward/gdn/prefill_batch.rs | 2 -- .../qwen_forward/gdn/prefill_batch/gdn_stage.rs | 6 +++--- .../gdn/prefill_batch/step_batch.rs | 10 +++++----- .../src/qwen_forward/moe_prefill_batch.rs | 7 ++----- .../moe_prefill_batch/moe_prefill_stage.rs | 14 +++++++------- .../moe_prefill_batch/route_micro.rs | 14 +++++++------- .../cache_original_prompt/prompt_layer_fns.rs | 2 +- .../src/qwen_forward/prefill_profile.rs | 17 ++++------------- .../prompt_device_read/produce_original_gdn.rs | 8 ++++---- docs/architecture/tech-debt.md | 4 ++-- 16 files changed, 45 insertions(+), 59 deletions(-) diff --git a/architecture-baseline.json b/architecture-baseline.json index 7d75e9a4..ea9ed5a8 100644 --- a/architecture-baseline.json +++ b/architecture-baseline.json @@ -4,7 +4,7 @@ "oversize": {}, "source": { "dyn-trait:leleka-cuda-driver": 45, - "dyn-trait:leleka-engine-qwen35-cuda": 9, + "dyn-trait:leleka-engine-qwen35-cuda": 3, "env-read:strata-control": 3, "glob-super-import:leleka-cuda-driver": 2, "instant-now:leleka-cuda-driver": 25, diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/executor/original_prompt/original_prompt_executor/chunk_run/prompt_chunk_forward.rs b/crates/engine-qwen35-cuda/src/qwen_forward/executor/original_prompt/original_prompt_executor/chunk_run/prompt_chunk_forward.rs index 5065a867..82db4d39 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/executor/original_prompt/original_prompt_executor/chunk_run/prompt_chunk_forward.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/executor/original_prompt/original_prompt_executor/chunk_run/prompt_chunk_forward.rs @@ -210,7 +210,7 @@ impl<'ctx> OriginalPromptExecutor<'ctx> { if cancelled() { return Err(Error::Unsupported(Stage::Session)); } - let _span = prof::span(PStage::FinishPreview, Some(self.context as &dyn prof::Drain)); + let _span = prof::span(PStage::FinishPreview, Some(self.context)); rows.finish_after_preview(work.weights.binding, work.weights.dense, work.txn, &final_row) .map_err(|error| trace_error("finish_after_preview", error))?; Ok((final_row, sample, draft_kv)) diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/executor/original_prompt/original_prompt_executor/chunk_run/prompt_chunk_forward/forward_draft.rs b/crates/engine-qwen35-cuda/src/qwen_forward/executor/original_prompt/original_prompt_executor/chunk_run/prompt_chunk_forward/forward_draft.rs index 8f588029..6b37c197 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/executor/original_prompt/original_prompt_executor/chunk_run/prompt_chunk_forward/forward_draft.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/executor/original_prompt/original_prompt_executor/chunk_run/prompt_chunk_forward/forward_draft.rs @@ -102,7 +102,7 @@ impl<'ctx> OriginalPromptExecutor<'ctx> { draft: ChunkDraft<'_, 'ctx>, cancelled: &mut impl FnMut() -> bool, ) -> Result { - let draft_span = prof::span(PStage::DraftKv, Some(self.context as &dyn prof::Drain)); + let draft_span = prof::span(PStage::DraftKv, Some(self.context)); let draft_kv = self.draft_kv_in_ring(inputs, rows, draft, cancelled)?; trace_step("forward: draft kv ok"); drop(draft_span); diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/executor/original_prompt/original_prompt_executor/chunk_run/prompt_chunk_forward/forward_final.rs b/crates/engine-qwen35-cuda/src/qwen_forward/executor/original_prompt/original_prompt_executor/chunk_run/prompt_chunk_forward/forward_final.rs index 4e5a75af..4bbca513 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/executor/original_prompt/original_prompt_executor/chunk_run/prompt_chunk_forward/forward_final.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/executor/original_prompt/original_prompt_executor/chunk_run/prompt_chunk_forward/forward_final.rs @@ -50,7 +50,7 @@ impl<'ctx> OriginalPromptExecutor<'ctx> { }; // the chunk's expert stream (if any) ended with the last MoE layer; never carried past the layers work.moe.end_prompt_stream()?; - let final_span = prof::span(PStage::Final, Some(self.context as &dyn prof::Drain)); + let final_span = prof::span(PStage::Final, Some(self.context)); trace_step("forward: all layers ok, final row"); let preview = rows.preview_conditioning(binding, dense, txn); let final_row = preview.map_err(|error| trace_error("preview_conditioning", error))?; diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/executor/original_prompt/original_prompt_executor/chunk_run/prompt_chunk_forward/forward_input.rs b/crates/engine-qwen35-cuda/src/qwen_forward/executor/original_prompt/original_prompt_executor/chunk_run/prompt_chunk_forward/forward_input.rs index d1dfe7d2..9d311ab3 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/executor/original_prompt/original_prompt_executor/chunk_run/prompt_chunk_forward/forward_input.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/executor/original_prompt/original_prompt_executor/chunk_run/prompt_chunk_forward/forward_input.rs @@ -107,7 +107,7 @@ impl<'ctx> OriginalPromptExecutor<'ctx> { ) -> Result> { let weights = chunk.weights; let (binding, tokens, txn) = (weights.binding, chunk.window, &*chunk.txn); - let drain = Some(self.context as &dyn prof::Drain); + let drain = Some(self.context); let init_span = prof::span(PStage::Initialize, drain); let sources = Self::prompt_sources(binding, self.context, txn, tokens.len())?; let upload_span = prof::span(PStage::InitUpload, drain); diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/executor/original_prompt/original_prompt_executor/chunk_run/prompt_chunk_forward/forward_layers.rs b/crates/engine-qwen35-cuda/src/qwen_forward/executor/original_prompt/original_prompt_executor/chunk_run/prompt_chunk_forward/forward_layers.rs index 0f802178..f96e319d 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/executor/original_prompt/original_prompt_executor/chunk_run/prompt_chunk_forward/forward_layers.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/executor/original_prompt/original_prompt_executor/chunk_run/prompt_chunk_forward/forward_layers.rs @@ -64,7 +64,7 @@ impl<'ctx> OriginalPromptExecutor<'ctx> { txn: &OriginalPromptTxn<'_, 'ctx>, carrier: Carrier<'w, 'arena, 'loan, 'ctx>, ) -> Result> { - let drain = Some(self.context as &dyn prof::Drain); + let drain = Some(self.context); let Carrier::Plain(mut rows) = carrier else { return Err(Error::IdentityMismatch); }; @@ -87,7 +87,7 @@ impl<'ctx> OriginalPromptExecutor<'ctx> { carrier: Carrier<'_, 'arena, 'loan, 'ctx>, index: u32, ) -> Result> { - let drain = Some(self.context as &dyn prof::Drain); + let drain = Some(self.context); let prompt = self.prompt.as_mut().ok_or(Error::IdentityMismatch)?; match carrier { Carrier::Plain(mut rows) => { @@ -111,7 +111,7 @@ impl<'ctx> OriginalPromptExecutor<'ctx> { read: PromptDeviceRead<'static, 'arena, 'loan, 'ctx>, layer: &BoundLayer, ) -> Result> { - let drain = Some(self.context as &dyn prof::Drain); + let drain = Some(self.context); let mixer_span = prof::span( match layer.kind() { BoundLayerKind::Gdn => PStage::Gdn, @@ -190,7 +190,7 @@ impl<'ctx> OriginalPromptExecutor<'ctx> { mut ffn: Carrier<'_, 'arena, 'loan, 'ctx>, index: u32, ) -> Result> { - let drain = Some(self.context as &dyn prof::Drain); + let drain = Some(self.context); validated(|| match &mut ffn { Carrier::Plain(rows) => rows.validate_original_rows(binding, dense), Carrier::Normed(rows) => rows.validate_original_rows(binding, dense), @@ -218,7 +218,7 @@ impl<'ctx> OriginalPromptExecutor<'ctx> { index: u32, ) -> Result> { let PromptWeights { binding, dense } = weights; - let drain = Some(self.context as &dyn prof::Drain); + let drain = Some(self.context); let prompt = self.prompt.as_mut().ok_or(Error::IdentityMismatch)?; let moe_span = prof::span(PStage::Moe, drain); // Literal fused GR norm is prohibited across PLE and final head. diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/gdn/prefill_batch.rs b/crates/engine-qwen35-cuda/src/qwen_forward/gdn/prefill_batch.rs index 0a986ef5..5f05e012 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/gdn/prefill_batch.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/gdn/prefill_batch.rs @@ -25,8 +25,6 @@ const ALPHA_BETA: usize = 96; const BATCH_RESERVE: usize = 128 * 1024 * 1024; const EAGER_TOKENS: usize = 64; /// Largest dequantized GDN matrix (attn_qkv, 10240 x 2560) in FP16 bytes. -/// The shared exclusively loaned GEMM adapter every batched step receives. -type GemmRef<'a> = &'a dyn GdnGemm; pub(in crate::qwen_forward) const BATCH_SCRATCH_BYTES: usize = CHANNELS * MIXED * 2; diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/gdn/prefill_batch/gdn_stage.rs b/crates/engine-qwen35-cuda/src/qwen_forward/gdn/prefill_batch/gdn_stage.rs index 18c0c3cd..a63fab20 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/gdn/prefill_batch/gdn_stage.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/gdn/prefill_batch/gdn_stage.rs @@ -5,7 +5,7 @@ use super::step_batch::{ check_gdn_window, gather_gdn_rows, gdn_operands, latch_cuda_failure, prepare_or_refuse, run_gdn_program, settle_gdn, window_bytes, }; -use super::{ChunkReady, GdnBatchResources, GdnChunkDevice, GdnChunkMicro, GdnStage, GemmRef, PromptLayer, Result}; +use super::{ChunkReady, GdnBatchResources, GdnChunkDevice, GdnChunkMicro, GdnGemm, GdnStage, PromptLayer, Result}; impl GdnStage { /// The batched Strata-prefill GDN step over one genuine current causal @@ -18,12 +18,12 @@ impl GdnStage { /// of the Out projection and its genuine journal edge. All edges are minted /// only after the whole batch drained finite and the chunk device adopted /// the final state. - pub(in crate::qwen_forward) fn step_chunk_batch<'ctx>( + pub(in crate::qwen_forward) fn step_chunk_batch<'ctx, G: GdnGemm>( prompt: PromptLayer<'_, '_, 'ctx>, micro: &GdnChunkMicro, owner: &mut GdnChunkDevice<'ctx>, resources: &mut GdnBatchResources<'ctx>, - gemm: GemmRef<'_>, + gemm: &G, ) -> Result> { let PromptLayer { binding, layer, cache, current } = prompt; #[cfg(all(test, any(target_os = "linux", target_os = "windows"), target_arch = "x86_64"))] diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/gdn/prefill_batch/step_batch.rs b/crates/engine-qwen35-cuda/src/qwen_forward/gdn/prefill_batch/step_batch.rs index ca08d80e..ea10467e 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/gdn/prefill_batch/step_batch.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/gdn/prefill_batch/step_batch.rs @@ -5,9 +5,9 @@ use super::{ BoundLayerKind, Buffers, CHANNELS, ChunkReady, Context, DenseWeightCache, DeviceBuffer, Dimension, Error, - GDN_BATCH_EPSILON, GdnBatchResources, GdnChunkDevice, GdnChunkMicro, GdnMatrixKind, GdnMatrixRole, GdnNativeFormat, - GemmRef, LayerId, MIXED, ModelBinding, Result, Stage, TokenLayer, VALUE_HEADS, VALUES, download, driver_error, - f32_bytes, native_format, validate_values, + GDN_BATCH_EPSILON, GdnBatchResources, GdnChunkDevice, GdnChunkMicro, GdnGemm, GdnMatrixKind, GdnMatrixRole, + GdnNativeFormat, LayerId, MIXED, ModelBinding, Result, Stage, TokenLayer, VALUE_HEADS, VALUES, download, + driver_error, f32_bytes, native_format, validate_values, }; use crate::qwen_forward::dense_weight_cache::GdnResident; use leleka_cuda_driver::GdnBatchPrefix; @@ -160,9 +160,9 @@ pub(super) fn window_bytes<'a, 'ctx>( } /// Upload, run the closed GDN program and read back the next history and the Out projection. -pub(super) fn run_gdn_program<'ctx>( +pub(super) fn run_gdn_program<'ctx, G: GdnGemm>( resources: &mut GdnBatchResources<'ctx>, - gemm: GemmRef<'_>, + gemm: &G, tokens: usize, ops: &GdnOperands<'_, 'ctx>, bytes: &WindowBytes<'_, 'ctx>, diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/moe_prefill_batch.rs b/crates/engine-qwen35-cuda/src/qwen_forward/moe_prefill_batch.rs index 4efe8cab..d5039907 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/moe_prefill_batch.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/moe_prefill_batch.rs @@ -21,9 +21,6 @@ use leleka_cuda_driver::{ MoeRouteBatch, }; -/// The shared exclusively loaned GEMM adapter every batched step receives. -type GemmRef<'a> = &'a dyn GdnGemm; - const MIXED: usize = 2560; const EXPERTS: usize = 512; const SELECTED: usize = 10; @@ -110,10 +107,10 @@ pub(in crate::qwen_forward) struct CombineWindow<'a> { /// What the genuine caller lends a window: the actual device context, the persistent prefill resources and the /// exclusively loaned shared GEMM workspace adapter. -pub(in crate::qwen_forward) struct PrefillLoans<'a, 'ctx> { +pub(in crate::qwen_forward) struct PrefillLoans<'a, 'ctx, G: GdnGemm> { pub(in crate::qwen_forward) current: &'ctx Context<'ctx>, pub(in crate::qwen_forward) resources: &'a mut MoePrefillResources<'ctx>, - pub(in crate::qwen_forward) gemm: GemmRef<'a>, + pub(in crate::qwen_forward) gemm: &'a G, } /// Per-call device owners, released together unless handed to the caller. diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/moe_prefill_batch/moe_prefill_stage.rs b/crates/engine-qwen35-cuda/src/qwen_forward/moe_prefill_batch/moe_prefill_stage.rs index be8d3520..9d133c62 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/moe_prefill_batch/moe_prefill_stage.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/moe_prefill_batch/moe_prefill_stage.rs @@ -3,9 +3,9 @@ //! recomputes no shared expert; it is single-use per window and fenced against the actual chunk rows and binding. use super::{ - Buffers, CombineInput, CombineOperands, CombineWindow, Error, MIXED, MicroInputs, MoeBf16Profile, MoeMicroProducts, - MoePrefillStage, PrefillLoans, Result, WindowOperands, WindowReads, admit, assemble_products, check_combine_window, - combine_on_device, device_bytes, driver_error, f32_bytes, route_on_device, validate_micro, + Buffers, CombineInput, CombineOperands, CombineWindow, Error, GdnGemm, MIXED, MicroInputs, MoeBf16Profile, + MoeMicroProducts, MoePrefillStage, PrefillLoans, Result, WindowOperands, WindowReads, admit, assemble_products, + check_combine_window, combine_on_device, device_bytes, driver_error, f32_bytes, route_on_device, validate_micro, }; impl MoePrefillStage { @@ -23,10 +23,10 @@ impl MoePrefillStage { /// `operands.weights` the layer's shared-expert owners, `loans.gemm` the exclusively loaned shared workspace /// adapter (retained through the final drain of this call). With `operands.combine`, the Original /// `moe_combine` runs too and its output is part of the products. - pub(in crate::qwen_forward) fn route_shared_micro<'ctx>( + pub(in crate::qwen_forward) fn route_shared_micro<'ctx, G: GdnGemm>( window: WindowReads<'_>, operands: WindowOperands<'_, 'ctx>, - loans: PrefillLoans<'_, 'ctx>, + loans: PrefillLoans<'_, 'ctx, G>, ) -> Result> { let WindowReads { binding, layer, reads } = window; let PrefillLoans { current, resources, gemm } = loans; @@ -51,11 +51,11 @@ impl MoePrefillStage { /// whole `bo` output is scanned (which also covers every `dm` element) and is stored in `products` only after a /// clean complete drain. Single-use: a window whose `route_shared_micro` ran the combine, or that already /// combined, is refused. - pub(in crate::qwen_forward) fn combine_micro<'ctx>( + pub(in crate::qwen_forward) fn combine_micro<'ctx, G: GdnGemm>( window: CombineWindow<'_>, products: &mut MoeMicroProducts<'ctx>, combine: CombineInput<'_, 'ctx>, - loans: PrefillLoans<'_, 'ctx>, + loans: PrefillLoans<'_, 'ctx, G>, ) -> Result<()> { let CombineWindow { binding, layer, expected } = window; let PrefillLoans { current, resources, gemm } = loans; diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/moe_prefill_batch/route_micro.rs b/crates/engine-qwen35-cuda/src/qwen_forward/moe_prefill_batch/route_micro.rs index 8024786a..49c937fa 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/moe_prefill_batch/route_micro.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/moe_prefill_batch/route_micro.rs @@ -1,7 +1,7 @@ //! Validation and device phases of `MoePrefillStage::route_shared_micro`. use super::{ - Buffers, CombineInput, DeviceBuffer, Dimension, EXPERTS, Error, GdnNativeFormat, GemmRef, GrRole, LayerId, MIXED, + Buffers, CombineInput, DeviceBuffer, Dimension, EXPERTS, Error, GdnGemm, GdnNativeFormat, GrRole, LayerId, MIXED, MOE_BATCH_MAX_TOKENS, ModelBinding, MoeMicroProducts, MoePrefillResources, MoeRouteBatch, ResidualReady, Result, RouteRow, SELECTED, SharedWeights, TokenLayer, down_format, driver_error, gate_up_format, validate_descriptors, validate_route_row, @@ -137,10 +137,10 @@ fn check_shared(weights: &SharedWeights<'_, '_>, layer: LayerId) -> Result<[GdnN /// Upload the mixed rows and run the closed route program; `finish` drains what was admitted and itself returns the /// latched admission error only when the drain showed no numerical refusal, so an earlier non-finite stage outranks a /// later admission error. -pub(super) fn route_on_device<'ctx>( +pub(super) fn route_on_device<'ctx, G: GdnGemm>( mut buffers: Buffers<'ctx>, resources: &mut MoePrefillResources<'ctx>, - gemm: GemmRef<'_>, + gemm: &G, inputs: &MicroInputs<'_, 'ctx>, mixed_bytes: &[u8], ) -> Result<(Buffers<'ctx>, MoeRouteBatch)> { @@ -153,10 +153,10 @@ pub(super) fn route_on_device<'ctx>( } } -fn launch_route<'ctx>( +fn launch_route<'ctx, G: GdnGemm>( buffers: &mut Buffers<'ctx>, resources: &mut MoePrefillResources<'ctx>, - gemm: GemmRef<'_>, + gemm: &G, inputs: &MicroInputs<'_, 'ctx>, mixed_bytes: &[u8], ) -> Result { @@ -280,11 +280,11 @@ fn check_window_size(binding: &ModelBinding, layer: LayerId, tokens: usize) -> R /// Run the closed Original-combine program over the window's own drained products. As in `route_shared_micro`, /// `finish` alone decides, so an admission error here is reported only when the drain showed no numerical refusal. -pub(super) fn combine_on_device<'ctx>( +pub(super) fn combine_on_device<'ctx, G: GdnGemm>( resources: &mut MoePrefillResources<'ctx>, products: &MoeMicroProducts<'ctx>, tokens: usize, - gemm: GemmRef<'_>, + gemm: &G, operands: CombineOperands<'_, 'ctx>, ) -> Result<()> { let CombineOperands { combine, bo } = operands; diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/moe_weight_cache/cache_original_prompt/prompt_layer_fns.rs b/crates/engine-qwen35-cuda/src/qwen_forward/moe_weight_cache/cache_original_prompt/prompt_layer_fns.rs index 7d13c550..93099d1a 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/moe_weight_cache/cache_original_prompt/prompt_layer_fns.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/moe_weight_cache/cache_original_prompt/prompt_layer_fns.rs @@ -70,7 +70,7 @@ pub(super) fn run_prompt_layer<'arena, 'loan, 'ctx>( ) -> Result> { let PromptParts { io, scan, weights } = parts; let weights = *weights; - let drain = Some(io.context as &dyn prof::Drain); + let drain = Some(io.context); begin_stream(io.schedule, scan.bank, io.layer, &read)?; // High-only is the selected literal Original mixed BF16 policy. let prepare_span = prof::span(PStage::MoePrepare, drain); diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/prefill_profile.rs b/crates/engine-qwen35-cuda/src/qwen_forward/prefill_profile.rs index e5fd266a..10375a82 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/prefill_profile.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/prefill_profile.rs @@ -6,6 +6,7 @@ //! `LELEKA_PREFILL_PROFILE_SYNC` also set, the device is drained at the end of every span, so each stage //! carries its own GPU time (the drains themselves cost a little; use it for attribution, not for totals). //! Disabled, a span is one cached flag test and no clock read. +use leleka_cuda_driver::Context; use std::cell::RefCell; use std::time::{Duration, Instant}; @@ -141,24 +142,14 @@ fn sync_enabled() -> bool { enabled() && crate::settings::process().diagnostics.prefill_profile_sync } -/// Something the span can drain at its end in the synchronized attribution mode. -pub(crate) trait Drain { - fn drain(&self); -} -impl Drain for leleka_cuda_driver::Context<'_> { - fn drain(&self) { - let _ = self.synchronize(); - } -} - /// One timed stage; the elapsed time is added when it drops. pub(crate) struct Span<'a> { stage: Stage, start: Option, - drain: Option<&'a dyn Drain>, + drain: Option<&'a Context<'a>>, } /// A span over `stage`; `drain` is synchronized at its end when `LELEKA_PREFILL_PROFILE_SYNC` is set. -pub(crate) fn span<'a>(stage: Stage, drain: Option<&'a dyn Drain>) -> Span<'a> { +pub(crate) fn span<'a>(stage: Stage, drain: Option<&'a Context<'a>>) -> Span<'a> { if !enabled() { return Span { stage, start: None, drain: None }; } @@ -168,7 +159,7 @@ impl Drop for Span<'_> { fn drop(&mut self) { let Some(start) = self.start else { return }; if let Some(drain) = self.drain { - drain.drain(); + let _ = drain.synchronize(); } let elapsed = start.elapsed(); let index = self.stage.index(); diff --git a/crates/engine-qwen35-cuda/src/qwen_forward/prompt_device_rows/prompt_device_read/produce_original_gdn.rs b/crates/engine-qwen35-cuda/src/qwen_forward/prompt_device_rows/prompt_device_read/produce_original_gdn.rs index 9948fc63..9c29caf9 100644 --- a/crates/engine-qwen35-cuda/src/qwen_forward/prompt_device_rows/prompt_device_read/produce_original_gdn.rs +++ b/crates/engine-qwen35-cuda/src/qwen_forward/prompt_device_rows/prompt_device_read/produce_original_gdn.rs @@ -111,10 +111,10 @@ pub(in super::super) fn produce_original_gdn<'arena, 'loan, 'ctx>( read: OriginalDeviceGrRead<'arena, 'loan, 'ctx>, gdn: &mut leleka_cuda_driver::OriginalGdnPromptDevice<'ctx>, ) -> Result> { - use super::super::super::prefill_profile::{Drain, Stage as P, span}; + use super::super::super::prefill_profile::{Stage as P, span}; let PromptWeights { binding, dense } = weights; let context = site.context; - let drain = Some(context as &dyn Drain); + let drain = Some(context); let mut lap = Lap::new("gdn-host"); let state_in = span(P::GdnStateIn, drain); let (mut state, rows) = GdnLayer::enter(weights, txn, site, &mut lap)?; @@ -382,6 +382,6 @@ pub(super) fn produce_block<'arena, 'loan, 'ctx>( /// The profiling span of the GR injection, synchronized with the context when `LELEKA_PREFILL_PROFILE_SYNC` is set. pub(super) fn inject_span<'a>(context: &'a Context<'_>) -> super::super::super::prefill_profile::Span<'a> { - use super::super::super::prefill_profile::{Drain, Stage, span}; - span(Stage::Inject, Some(context as &dyn Drain)) + use super::super::super::prefill_profile::{Stage, span}; + span(Stage::Inject, Some(context)) } diff --git a/docs/architecture/tech-debt.md b/docs/architecture/tech-debt.md index 47a89252..78d0cd28 100644 --- a/docs/architecture/tech-debt.md +++ b/docs/architecture/tech-debt.md @@ -269,7 +269,7 @@ The order: the debt of the engine is brought to zero in the code, or listed as a | `path-include` | 100 | 0 (420 files moved to the place their module name gives) | none; five `#[path]` remain in test-only modules: two files of the crate's `tests/` directory, the file two proofs share (`native_down_pair_io.rs`, one copy of its state each) | | `env-read` | 1 | 0 | `settings/source.rs`: the one reader of the process environment | | `instant-now` | 16 | 0 | 8 files of measurement timers in hot loops (round, miss, kernel; each one a log line or counter); load laps and the resident-RAM log line read `SystemClock` | -| `dyn-trait` | engine-core 18, engine-fake 1, engine 53 | engine-core 0, engine-fake 0, engine 9 | `Box` of the five factory files, `EngineFamily::spawn` and the fake family (process boundary); two stored host callbacks (measurement sink, GPU probe). The 9 left are on the hot branch, see below | +| `dyn-trait` | engine-core 18, engine-fake 1, engine 53 | engine-core 0, engine-fake 0, engine 9 (3 on the hot branch, see below) | `Box` of the five factory files, `EngineFamily::spawn` and the fake family (process boundary); two stored host callbacks (measurement sink, GPU probe). Of the 9 left, the `GdnGemm` loan (2) and the profile `Drain` (4) are generics on `claude/debt-hot-engine` (not merged), `OriginalServePool` (3) is open | | `process-global` | 59 | 0 | 37 files, in three groups: reads of the serving context that is fixed at start-up (`serving_cells()`), `Governor::global()` (the S13/S14 injection of a governor handle is not done), and the settings capture, one tuning cache, one trace epoch, four per-instance `OnceCell` fields | What was done, with the tools that did it (all under `scripts/dev/`, offline aids): @@ -304,7 +304,7 @@ Left, with the reason: |---|---|---| | `Governor::global()` (22 sites in 12 files) | the handle has to travel through `StackConfig`, the request environment and `PrefixServe`; each site is cold, but the plumbing touches the load path | one `MemoryHandle` built from what `memory_governor::startup` returns and carried in `StackConfig`; then `memory_governor`'s free functions become its methods | | `serving_cells()` (33 sites in 27 files) | the bound is an immutable process constant after start-up; the call sites are position and prompt-length checks on the decode and prefill paths | a `ServingBound` handle on the session and the request, with the identity gate | -| 9 `dyn` of the hot path: `OriginalServePool` (host), `GdnGemm` (prefill batch), `Drain` (prefill profile) | per-round and per-layer calls; the change is semantics-neutral but touches the decode loop and the prefill ring | branch `claude/debt-hot-engine`, not merged before the laptop identity run | +| 9 `dyn` of the hot path: `GdnGemm` (prefill batch, 2) and `Drain` (prefill profile, 4) are generic on the branch `claude/debt-hot-engine`, awaiting the laptop identity run; `OriginalServePool` (host and decode steps, 3) | per-layer and per-round calls; the two pool adapters (CPU pool, GPU tier) need an enum that carries four lifetimes through the host | merge the branch after the identity run; the pool enum with the host refactor | | `settings::process()` | leaf code that cannot take a parameter | S13/S14 | ### (d) Other open items of this file (unchanged by the pass; the owner's order for them)