Skip to main content

QTensorEngine

Struct QTensorEngine 

Source
pub struct QTensorEngine {
    pub pipeline: ComputePipeline,
    pub embedding_pipeline: ComputePipeline,
    pub is_initialized: bool,
    pub gguf_mmap: Option<Arc<Mmap>>,
    pub p64_index: Option<P64TensorIndex>,
    pub tensor_index_cache: Option<GgufTensorIndex>,
    pub tensor_data_offset: u64,
    pub hyperparams: GgufHyperparams,
    pub max_tensor_bytes: usize,
    /* private fields */
}

Fields§

§pipeline: ComputePipeline§embedding_pipeline: ComputePipeline

GPU-side Q6_K embedding dequant + matmul (zero CPU dequant).

§is_initialized: bool§gguf_mmap: Option<Arc<Mmap>>

Memory-mapped GGUF file (set after load_gguf).

§p64_index: Option<P64TensorIndex>

Cached P64 index after adopt_resident_p64_* — decode must not re-CRC the container.

§tensor_index_cache: Option<GgufTensorIndex>

Cached synthetic GGUF index built from p64_index (or from GGUF parse).

§tensor_data_offset: u64

Byte offset into the mmap where tensor data begins.

§hyperparams: GgufHyperparams§max_tensor_bytes: usize

Implementations§

Source§

impl QTensorEngine

Source

pub fn dispatch_output_logits_into( &self, index: &GgufTensorIndex, hidden: &[f32], emb_dim: usize, logits_out: &mut [f32], ) -> usize

Final logits via chunked projection into logits_out (fills min(vocab, buf) rows).

Source

pub fn decode_lexicon_bound( &self, _logits: &[f32], valid_lexicon_ids: &[u64], ) -> u64

Source§

impl QTensorEngine

Source

pub fn cuda_prepared_telemetry(&self) -> Option<CudaPreparedTelemetry>

Source§

impl QTensorEngine

Source

pub fn dispatch_quantized_token_embedding( &self, raw_embd: &[u8], ggml_type: u32, n_embd: u32, weight_tensor: &QTensor, ) -> Option<Vec<f32>>

Upload raw quantized embedding bytes to the GPU and matmul without CPU dequant. Returns None when the GGML type has no WGSL kernel (caller uses CPU fallback).

Source

pub fn dispatch_fused_transformer_block( &self, tensor: &QTensor, input_activations: &[f32], ) -> Vec<f32>

Source§

impl QTensorEngine

Source

pub fn dispatch_prefill_chunk( &mut self, index: &GgufTensorIndex, batch_hidden: &mut [f32], emb_dim: usize, n_tokens: u32, batch_start_token_idx: u32, scratch_a: &mut [f32], scratch_b: &mut [f32], max_layers: u32, ) -> bool

Chunked prefill: populate KV arena for n_tokens prompt positions starting at batch_start.

Source

pub fn dispatch_transformer_layer( &mut self, index: &GgufTensorIndex, layer: u32, token_idx: u32, hidden: &mut [f32], emb_dim: usize, scratch_a: &mut [f32], scratch_b: &mut [f32], ) -> bool

One transformer block using real mmap tensor offsets (stack buffers only).

Source

pub fn try_cuda_mega_pass_decode( &mut self, index: &GgufTensorIndex, hidden: &mut [f32], emb_dim: usize, token_idx: u32, ) -> Option<u32>

Attempt the CUDA mega-pass: all layers in one fenced CUDA stream. Returns Some(token_id) on success, None to fall back to per-layer path.

Source

pub fn try_cuda_mega_pass_decode_token( &mut self, index: &GgufTensorIndex, token_id: u32, hidden: &mut [f32], emb_dim: usize, token_idx: u32, ) -> Option<u32>

Decode directly from a token id using the resident Q8 embedding table.

This path avoids CPU embedding dequantization and the full hidden-state H2D upload.

Source

pub fn dispatch_transformer_forward( &mut self, index: &GgufTensorIndex, hidden: &mut [f32], emb_dim: usize, scratch_a: &mut [f32], scratch_b: &mut [f32], token_idx: u32, max_layers: u32, ) -> u32

Sequential layer-by-layer forward (one tensor payload in VRAM at a time). max_layers: 0 runs all blocks; otherwise caps how many layers execute.

Source

pub fn verify_topology_draft_batch( &mut self, index: &GgufTensorIndex, ctx: &mut Vec<u32>, draft: &TopologyDraftBatch, emb_dim: usize, emb_buf: &mut [f32], scratch_a: &mut [f32], scratch_b: &mut [f32], max_layers: u32, max_vocab_chunks: u32, ) -> u32

Topological speculative verify — accept longest draft prefix (B3.1d).

Source§

impl QTensorEngine

Source

pub fn dispatch_gemm_into( &self, index: &GgufTensorIndex, info: &GgufTensorInfo, input: &[f32], out: &mut [f32], n_in: usize, n_out: usize, ) -> bool

Quantized GEMM into caller out using reused GPU buffers (Q6_K) or CPU dequant fallback.

Source§

impl QTensorEngine

Source

pub fn device(&self) -> &Device

Shared process-wide wgpu device (LLM + render coexistence).

Source

pub fn queue(&self) -> &Queue

Shared process-wide wgpu queue.

Source

pub async fn try_new() -> Result<Self, String>

Source

pub fn new() -> Self

Source

pub fn reset_kv_cache(&mut self)

Zero the static KV arena at the start of a new decode context (zero heap in decode).

Source

pub fn get_kv_cache_cpu(&self) -> Option<&[f32]>

Source

pub fn set_kv_cache_cpu(&mut self, data: &[f32])

Source

pub fn read_kv_cache_gpu(&self) -> Option<Vec<f32>>

Read the entire GPU KV-cache arena back to host as f32 (native, cold path — forge KV capture, not the decode hot path). The returned flat buffer is interpretable via KvCacheLayout:: k_index/v_index only when the layout is f32 (int8 KV disabled at load); with an int8 layout the bytes are packed i8 lanes + scales and this returns None.

Source

pub fn capture_kv_f32( &self, n_tokens: u32, max_per_layer: usize, ) -> Option<KvCapture>

Decode the current f32 KV cache into per-layer K and V vectors for token positions 0..n_tokens (capped at max_per_layer per layer per stream). The GPU-readback capture route for the sparse-KV-dictionary go/no-go — reads the real decode-path K/V straight from VRAM, no CPU reference forward. Returns None on an int8 layout or if readback fails.

Source§

impl QTensorEngine

Source

pub fn kv_cache_bytes(&self) -> u64

Source

pub fn load_gguf_checked( &mut self, path: &str, ) -> Result<GgufLoadReport, String>

Source

pub fn load_gguf(&mut self, path: &str)

Memory-map a GGUF file so tensor bytes are accessible without heap allocation. Call this once after new(), before the first dispatch_fused_transformer_block.

Source

pub fn load_model_checked( &mut self, path: &str, ) -> Result<GgufLoadReport, String>

Memory-map and auto-detect a supported local model container.

Canonical P64 is detected by its exact p64\0 magic and adopted through the P64 validation path. All other inputs are passed to the GGUF parser, which rejects malformed or unsupported data.

Source

pub fn load_model(&mut self, path: &str)

Fail-soft wrapper retained for the agent decode path.

Source

pub fn adopt_resident_p64_mmap( &mut self, mmap: Arc<Mmap>, ) -> Result<GgufLoadReport, String>

Boot from an already-mapped P64 weight container (native). Mirrors the GGUF adopt_resident_mmap but for the P64 format: validates + builds a synthetic GGUF index from the manifest, points the byte source at the P64 bytes (tensor_data_start = 0, absolute blob offsets), reserves the GEMM/KV arenas, makes the (verbatim) output projection resident, and builds the resident 2-bit ternary-FFN dispatcher from the FFN blobs. The attention/norm/embed tensors stay at source precision and run the standard GGUF hot path.

Source

pub fn adopt_resident_q42_mmap( &mut self, mmap: Arc<Mmap>, ) -> Result<GgufLoadReport, String>

👎Deprecated:

use adopt_resident_p64_mmap

Compatibility alias for the historical pre-P64 API name.

Source

pub fn ternary_ffn_resident_len(&self) -> usize

A1b: number of resident ternary FFN tensors (0 unless a ternary P64 was adopted). Lets a test confirm the GPU resident path is actually populated (not a silent CPU-only fallback).

Source

pub fn adopt_resident_mmap( &mut self, mmap: Arc<Mmap>, ) -> Result<GgufLoadReport, String>

Attach an already-mapped resident GGUF (shared with orchestrator slot).

Source

pub fn bench_empty_submit_roundtrip(&self, n: u32) -> u64

Decode-profiler: wall-clock for n EMPTY submit → poll(Maintain::Wait) round-trips (no compute dispatched). Isolates the fixed CPU↔GPU fence latency: if a token’s forward time ≈ (its round-trip count × this per-round-trip cost), the bottleneck is synchronization, not math; if forward ≫ that, the kernels themselves are slow. Does NOT touch GPU_WAIT_COUNT.

Source§

impl QTensorEngine

Source

pub fn dispatch_output_argmax_chunked( &self, index: &GgufTensorIndex, hidden: &[f32], emb_dim: usize, chunk_logits: &mut [f32], max_chunks: u32, sieve_mask: Option<&SieveStateMask>, ) -> Option<StreamingArgmaxResult>

Chunked vocabulary projection with streaming argmax (zero heap, stack chunk buffer only). max_chunks: 0 sweeps the full vocabulary; otherwise caps chunk iterations (tests).

Source

pub fn dispatch_output_top1_chunked( &self, index: &GgufTensorIndex, hidden: &[f32], emb_dim: usize, ) -> Option<StreamingArgmaxResult>

Native decode fast path: output projection plus GPU block argmax (k=1) with one tiny candidate readback after all vocab chunks have been submitted. This avoids the full chunk-logit readback in Self::dispatch_output_argmax_chunked and avoids heap allocation in the decode loop.

Source

pub fn dispatch_output_topk_chunked( &self, index: &GgufTensorIndex, hidden: &[f32], emb_dim: usize, k: usize, ) -> Option<Vec<TopKItem>>

A1a: GPU top-k over the output projection — the logits stay on-GPU (gemm_output_buf), the top-k kernel reduces them per chunk, and only K (id, logit) candidates are read back (vs the 196 KB/token full-logit readback + CPU argmax in dispatch_output_argmax_chunked). Returns the merged global top-K, or None to signal the caller to fall back to the argmax path. v1: no sieve coupling (caller routes here only when no sieve mask is active).

Source

pub fn apply_output_norm_inplace( &self, index: &GgufTensorIndex, hidden: &mut [f32], emb_dim: usize, ) -> bool

Final output_norm RMSNorm in-place before vocabulary projection (Pre-Norm LLM tail). REQUIRED on all targets — native previously skipped it → logits from an un-normed hidden.

Source§

impl QTensorEngine

Source

pub fn resident_dispatches_per_token(&self) -> Option<u32>

Exact steady-state compute dispatch count for the prepared resident plan.

Source

pub fn resident_readback_bytes_per_token(&self) -> Option<u32>

Exact candidate staging copy size for one resident greedy token.

Source

pub fn dispatch_token_forward_resident( &mut self, index: &GgufTensorIndex, emb: &[f32], token_idx: u32, ) -> Option<StreamingArgmaxResult>

Single-fence resident-token decode (greedy top-1). Returns the argmax token, or None on any ineligibility (caller falls back to the legacy per-layer path).

Source

pub fn dispatch_token_forward_resident_hidden( &mut self, index: &GgufTensorIndex, emb: &[f32], token_idx: u32, out_hidden: &mut [f32], ) -> bool

Sampler-compatible resident forward: same single-fence layer stack + output RMSNorm, then read back the normed hidden into out_hidden so the caller can project full logits + sample on CPU without the legacy ~107-fence path.

emb is the token embedding input; out_hidden receives post-output-norm state (may be the same logical buffer only if the caller copies input first — they must not alias while the upload of emb is live; pass distinct slices).

Source§

impl QTensorEngine

Source

pub fn gemm_parity_probe( &mut self, info: &GgufTensorInfo, raw: &[u8], input: &[f32], gpu_out: &mut [f32], cpu_out: &mut [f32], n_in: usize, n_out: usize, ) -> bool

W3 kernel-parity probe (test/diagnostic): run the GPU GEMM (dispatch_gemm_raw_into) and the CPU reference (stack_gemm_quant) on the SAME quantized weights + input, writing each into a caller-provided buffer. Ensures the GEMM buffers exist first, so a fresh engine (no model loaded) can be probed directly. Returns true only if both ran; the caller compares the two outputs with crate::llm_kernel_parity. Enable crate::llm_gpu_profiler around the call to witness that the GPU path actually executed rather than silently falling back to the CPU.

Auto Trait Implementations§

Blanket Implementations§

§

impl<S, A> Aggregate<Result<S, Error>> for A
where A: Aggregate<S>,

§

fn from_shares<T>(iter: T) -> Result<A, Error>
where T: IntoIterator<Item = Result<S, Error>>,

Aggregate shares in an MPC protocol.
Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> Downcast<T> for T

§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> Upcast<T> for T

§

fn upcast(&self) -> Option<&T>

§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

§

impl<T> WasmNotSend for T
where T: Send,

§

impl<T> WasmNotSendSync for T
where T: WasmNotSend + WasmNotSync,

§

impl<T> WasmNotSync for T
where T: Sync,