Skip to main content

qualia_core_db/gguf_bridge/
embedding.rs

1//! Quantized token embedding + fused transformer block dispatch
2//! Split from gguf_bridge/mod.rs (structural refactor; no behaviour change).
3use super::*;
4
5impl QTensorEngine {
6    /// Upload raw quantized embedding bytes to the GPU and matmul without CPU dequant.
7    /// Returns `None` when the GGML type has no WGSL kernel (caller uses CPU fallback).
8    #[cfg(all(not(target_arch = "wasm32"), feature = "gpu-runtime"))]
9    pub fn dispatch_quantized_token_embedding(
10        &self,
11        raw_embd: &[u8],
12        ggml_type: u32,
13        n_embd: u32,
14        weight_tensor: &QTensor,
15    ) -> Option<Vec<f32>> {
16        let descriptor = etalon_inference::projections::ProjectionTensor {
17            shape: &weight_tensor.shape,
18            byte_offset: weight_tensor.byte_offset,
19            is_quantized_q4_k: weight_tensor.is_quantized_q4_k,
20        };
21        etalon_inference::projections::ProjectionGpuEngine::new(self)
22            .dispatch_quantized_token_embedding(raw_embd, ggml_type, n_embd, &descriptor)
23    }
24
25    #[cfg(all(not(target_arch = "wasm32"), feature = "gpu-runtime"))]
26    /// Async dispatch: fused-transformer-block.
27    pub fn dispatch_fused_transformer_block(
28        &self,
29        tensor: &QTensor,
30        input_activations: &[f32],
31    ) -> Vec<f32> {
32        let descriptor = etalon_inference::projections::ProjectionTensor {
33            shape: &tensor.shape,
34            byte_offset: tensor.byte_offset,
35            is_quantized_q4_k: tensor.is_quantized_q4_k,
36        };
37        etalon_inference::projections::ProjectionGpuEngine::new(self)
38            .dispatch_fused_transformer_block(&descriptor, input_activations)
39    }
40
41    /// Browser inference must await WebGPU buffer mapping. The synchronous entry
42    /// point therefore reports the documented CPU-fallback sentinel.
43    #[cfg(target_arch = "wasm32")]
44    pub fn dispatch_quantized_token_embedding(
45        &self,
46        raw_embd: &[u8],
47        ggml_type: u32,
48        n_embd: u32,
49        weight_tensor: &QTensor,
50    ) -> Option<Vec<f32>> {
51        wlog(&format!(
52            "[embedding] synchronous browser dispatch unavailable (bytes={}, type={}, dim={}, tensor_offset={})",
53            raw_embd.len(),
54            ggml_type,
55            n_embd,
56            weight_tensor.byte_offset
57        ));
58        None
59    }
60
61    /// A synchronous GPU readback cannot make progress on the browser event
62    /// loop. Callers must use the async inference surface instead.
63    #[cfg(target_arch = "wasm32")]
64    pub fn dispatch_fused_transformer_block(
65        &self,
66        tensor: &QTensor,
67        input_activations: &[f32],
68    ) -> Vec<f32> {
69        panic!(
70            "synchronous browser transformer dispatch is unsupported for tensor at byte offset {} ({} activations); use inferWasmAsync/inferWasmStreaming",
71            tensor.byte_offset,
72            input_activations.len()
73        );
74    }
75}