ripvec-core 0.13.24

Semantic code search engine — GPU-accelerated ModernBERT embeddings, tree-sitter chunking, hybrid BM25+vector ranking
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! `ClassicBert` architecture (BGE-small-en-v1.5).
//!
//! 12-layer BERT with learned position embeddings, GELU activation, fused QKV
//! projections, and CLS pooling. This is the original BERT architecture used
//! by BGE-small.
//!
//! Weight structures are generic over the tensor type `T`, which is
//! [`Driver::Tensor`] when wired to a
//! backend. The [`ModelArch`] implementation composes
//! [`Driver`] primitives into the full forward
//! pass.

use super::super::Encoding;
use super::super::driver::{BatchInputs, Driver};
use super::ModelArch;

/// Weights for one `ClassicBert` encoder layer.
///
/// All projections include bias (unlike `ModernBERT`). The QKV weight is a fused
/// `[3*hidden, hidden]` matrix that produces Q, K, V in a single GEMM.
pub struct ClassicBertLayerWeights<T> {
    /// Fused Q+K+V projection weight `[3*hidden, hidden]`.
    pub qkv_weight: T,
    /// Fused Q+K+V projection bias `[3*hidden]`.
    pub qkv_bias: T,
    /// Attention output projection weight `[hidden, hidden]`.
    pub output_weight: T,
    /// Attention output projection bias `[hidden]`.
    pub output_bias: T,
    /// Post-attention `LayerNorm` weight `[hidden]`.
    pub output_ln_weight: T,
    /// Post-attention `LayerNorm` bias `[hidden]`.
    pub output_ln_bias: T,
    /// FFN intermediate projection weight `[intermediate, hidden]`.
    pub ffn_inter_weight: T,
    /// FFN intermediate projection bias `[intermediate]`.
    pub ffn_inter_bias: T,
    /// FFN output projection weight `[hidden, intermediate]`.
    pub ffn_out_weight: T,
    /// FFN output projection bias `[hidden]`.
    pub ffn_out_bias: T,
    /// Post-FFN `LayerNorm` weight `[hidden]`.
    pub ffn_ln_weight: T,
    /// Post-FFN `LayerNorm` bias `[hidden]`.
    pub ffn_ln_bias: T,
}

/// Full `ClassicBert` model weights, generic over tensor type.
///
/// Includes embedding tables, per-layer encoder weights, and model geometry.
/// The tensor type `T` becomes [`Driver::Tensor`]
/// when loaded onto a specific backend.
pub struct ClassicBertWeights<T> {
    /// Word embedding table `[vocab_size, hidden]`.
    pub word_embeddings: T,
    /// Learned position embedding table `[max_position, hidden]`.
    pub position_embeddings: T,
    /// Token type embedding table `[2, hidden]`.
    pub token_type_embeddings: T,
    /// Post-embedding `LayerNorm` weight `[hidden]`.
    pub emb_ln_weight: T,
    /// Post-embedding `LayerNorm` bias `[hidden]`.
    pub emb_ln_bias: T,
    /// Per-layer encoder weights.
    pub layers: Vec<ClassicBertLayerWeights<T>>,
    /// Number of attention heads (e.g., 12 for BGE-small).
    pub num_heads: usize,
    /// Dimension per attention head (`hidden / num_heads`).
    pub head_dim: usize,
    /// Hidden dimension (e.g., 384 for BGE-small).
    pub hidden_dim: usize,
    /// FFN intermediate dimension (e.g., 1536 for BGE-small).
    pub intermediate_dim: usize,
    /// Layer normalization epsilon (typically 1e-12).
    pub layer_norm_eps: f32,
}

/// `ClassicBert` architecture: BGE-small-en-v1.5.
///
/// 12 layers, learned position embeddings, GELU activation, CLS pooling.
/// Composes [`Driver`] primitives into the full forward pass.
pub struct ClassicBertArch<T> {
    /// Model weights on device.
    pub weights: ClassicBertWeights<T>,
}

/// Encoder geometry passed to sublayer helpers to avoid repeating fields.
struct EncoderGeometry {
    batch: usize,
    max_seq: usize,
    /// Actual tokens across all sequences (no padding). Used for linear ops.
    total_tokens: usize,
    /// Padded total: `batch * max_seq`. Used for attention layout.
    padded_tokens: usize,
    /// Per-sequence lengths for pad/unpad.
    seq_lengths: Vec<usize>,
    hidden: usize,
    num_heads: usize,
    head_dim: usize,
    intermediate: usize,
    scale: f32,
    eps: f32,
}

/// QKV projection (unpadded) + bias + pad + split heads.
///
/// Returns `(q, k, v)` each `[batch*num_heads, seq, head_dim]` in padded layout.
fn attn_qkv<D: Driver>(
    driver: &D,
    hidden_states: &D::Tensor,
    layer: &ClassicBertLayerWeights<D::Tensor>,
    g: &EncoderGeometry,
) -> crate::Result<(D::Tensor, D::Tensor, D::Tensor)> {
    // QKV projection: [total_tokens, hidden] @ [3*hidden, hidden]^T
    // Uses total_tokens (unpadded) — no wasted compute on padding.
    let mut qkv = driver.alloc_zeros(g.total_tokens * 3 * g.hidden)?;
    driver.gemm(
        hidden_states,
        &layer.qkv_weight,
        &mut qkv,
        g.total_tokens,
        3 * g.hidden,
        g.hidden,
        true,
    )?;
    driver.add_bias(&mut qkv, &layer.qkv_bias, g.total_tokens, 3 * g.hidden)?;

    // Pad QKV from [total_tokens, 3H] to [batch*max_seq, 3H] for attention.
    // qkv_split needs the padded batch×seq layout to reshape into per-head tensors.
    let mut qkv_padded = driver.alloc_zeros(g.padded_tokens * 3 * g.hidden)?;
    driver.pad_to_batch(
        &qkv,
        &mut qkv_padded,
        &g.seq_lengths,
        g.max_seq,
        3 * g.hidden,
    )?;

    // Split into Q, K, V each [batch * num_heads, seq, head_dim].
    let padded = g.padded_tokens;
    let mut q = driver.alloc_zeros(padded * g.hidden)?;
    let mut k = driver.alloc_zeros(padded * g.hidden)?;
    let mut v = driver.alloc_zeros(padded * g.hidden)?;
    driver.qkv_split(
        &mut q,
        &mut k,
        &mut v,
        &qkv_padded,
        g.batch,
        g.max_seq,
        g.hidden,
        g.num_heads,
        g.head_dim,
    )?;

    Ok((q, k, v))
}

/// Attention scores + output projection (padded) + unpad + bias + residual + `LayerNorm`.
#[expect(clippy::too_many_arguments, reason = "Q/K/V must be separate tensors")]
fn attn_scores_residual<D: Driver>(
    driver: &D,
    q: &D::Tensor,
    k: &D::Tensor,
    v: &D::Tensor,
    hidden_states: &D::Tensor,
    layer: &ClassicBertLayerWeights<D::Tensor>,
    inputs: &BatchInputs<D::Tensor>,
    g: &EncoderGeometry,
) -> crate::Result<D::Tensor> {
    let padded = g.padded_tokens;

    // Attention scores: Q @ K^T => [batch * num_heads, seq, seq]
    let mut scores = driver.alloc_zeros(g.batch * g.num_heads * g.max_seq * g.max_seq)?;
    driver.gemm_batched(
        q,
        k,
        &mut scores,
        g.max_seq,
        g.max_seq,
        g.head_dim,
        true,
        g.max_seq * g.head_dim,
        g.max_seq * g.head_dim,
        g.max_seq * g.max_seq,
        g.batch * g.num_heads,
    )?;
    driver.fused_scale_mask_softmax(
        &mut scores,
        &inputs.float_mask,
        g.batch,
        g.num_heads,
        g.max_seq,
        g.scale,
    )?;

    // Weighted sum: scores @ V => [batch * num_heads, seq, head_dim]
    let mut attn_out = driver.alloc_zeros(padded * g.hidden)?;
    driver.gemm_batched(
        &scores,
        v,
        &mut attn_out,
        g.max_seq,
        g.head_dim,
        g.max_seq,
        false,
        g.max_seq * g.max_seq,
        g.max_seq * g.head_dim,
        g.max_seq * g.head_dim,
        g.batch * g.num_heads,
    )?;

    // Reshape heads back to [padded_tokens, hidden] (still padded).
    let mut context = driver.alloc_zeros(padded * g.hidden)?;
    driver.attn_reshape(
        &mut context,
        &attn_out,
        g.batch,
        g.max_seq,
        g.num_heads,
        g.head_dim,
    )?;

    // Output projection on padded layout, then unpad.
    let mut projected_padded = driver.alloc_zeros(padded * g.hidden)?;
    driver.gemm(
        &context,
        &layer.output_weight,
        &mut projected_padded,
        padded,
        g.hidden,
        g.hidden,
        true,
    )?;

    // Unpad: [padded_tokens, H] → [total_tokens, H]
    let mut projected = driver.alloc_zeros(g.total_tokens * g.hidden)?;
    driver.unpad_from_batch(
        &projected_padded,
        &mut projected,
        &g.seq_lengths,
        g.max_seq,
        g.hidden,
    )?;

    driver.add_bias(&mut projected, &layer.output_bias, g.total_tokens, g.hidden)?;

    let mut output = driver.alloc_zeros(g.total_tokens * g.hidden)?;
    driver.fused_residual_layernorm(
        &mut output,
        &projected,
        hidden_states,
        &layer.output_ln_weight,
        &layer.output_ln_bias,
        g.total_tokens,
        g.hidden,
        g.eps,
    )?;
    Ok(output)
}

/// Run the feed-forward sublayer for one encoder layer.
///
/// Intermediate GEMM -> bias + GELU -> output GEMM -> bias + residual + `LayerNorm`.
fn ffn_sublayer<D: Driver>(
    driver: &D,
    attn_output: &D::Tensor,
    layer: &ClassicBertLayerWeights<D::Tensor>,
    g: &EncoderGeometry,
) -> crate::Result<D::Tensor> {
    // Intermediate: [total_tokens, hidden] @ [inter, hidden]^T => [total_tokens, inter]
    let mut intermediate = driver.alloc_zeros(g.total_tokens * g.intermediate)?;
    driver.gemm(
        attn_output,
        &layer.ffn_inter_weight,
        &mut intermediate,
        g.total_tokens,
        g.intermediate,
        g.hidden,
        true,
    )?;
    driver.fused_bias_gelu(
        &mut intermediate,
        &layer.ffn_inter_bias,
        g.total_tokens,
        g.intermediate,
    )?;

    // Output: [total_tokens, inter] @ [hidden, inter]^T => [total_tokens, hidden]
    let mut ffn_out = driver.alloc_zeros(g.total_tokens * g.hidden)?;
    driver.gemm(
        &intermediate,
        &layer.ffn_out_weight,
        &mut ffn_out,
        g.total_tokens,
        g.hidden,
        g.intermediate,
        true,
    )?;
    driver.add_bias(&mut ffn_out, &layer.ffn_out_bias, g.total_tokens, g.hidden)?;

    let mut output = driver.alloc_zeros(g.total_tokens * g.hidden)?;
    driver.fused_residual_layernorm(
        &mut output,
        &ffn_out,
        attn_output,
        &layer.ffn_ln_weight,
        &layer.ffn_ln_bias,
        g.total_tokens,
        g.hidden,
        g.eps,
    )?;
    Ok(output)
}

impl<D: Driver> ModelArch<D> for ClassicBertArch<D::Tensor> {
    #[expect(
        clippy::cast_precision_loss,
        reason = "head_dim is small (32-64); sqrt is exact at these sizes"
    )]
    fn forward(&self, driver: &D, encodings: &[Encoding]) -> crate::Result<Vec<Vec<f32>>> {
        let w = &self.weights;
        let batch = encodings.len();
        let hidden = w.hidden_dim;

        // Unpadded mode: tokens concatenated without padding.
        // Linear layers (GEMM, LN, GELU) process total_tokens rows — no wasted compute.
        // Attention pads/unpads around per-head operations via pad_to_batch/unpad_from_batch.
        let inputs = driver.prepare_batch_unpadded(encodings)?;
        let total_tokens = inputs.total_tokens;
        let max_seq = inputs.max_seq;

        // Enter batched mode: all GPU ops encode into ONE command buffer.
        driver.begin_batch()?;

        // Embedding: word + position + token_type + LayerNorm.
        let mut hidden_states =
            driver.embedding_lookup(&inputs.input_ids, &w.word_embeddings, total_tokens, hidden)?;
        driver.add_embeddings(
            &mut hidden_states,
            &w.position_embeddings,
            &inputs.position_ids,
            total_tokens,
            hidden,
        )?;
        driver.add_embeddings(
            &mut hidden_states,
            &w.token_type_embeddings,
            &inputs.token_type_ids,
            total_tokens,
            hidden,
        )?;
        let emb_input = driver.clone_tensor(&hidden_states, total_tokens * hidden)?;
        driver.layer_norm(
            &mut hidden_states,
            &emb_input,
            &w.emb_ln_weight,
            &w.emb_ln_bias,
            total_tokens,
            hidden,
            w.layer_norm_eps,
        )?;

        let g = EncoderGeometry {
            batch,
            max_seq,
            total_tokens,
            padded_tokens: batch * max_seq,
            seq_lengths: inputs.seq_lengths.clone(),
            hidden,
            num_heads: w.num_heads,
            head_dim: w.head_dim,
            intermediate: w.intermediate_dim,
            scale: 1.0 / (w.head_dim as f32).sqrt(),
            eps: w.layer_norm_eps,
        };

        // Encoder layers.
        for layer in &w.layers {
            let saved = driver.save_pool_cursor();
            let (q, k, v) = attn_qkv(driver, &hidden_states, layer, &g)?;
            let attn_output =
                attn_scores_residual(driver, &q, &k, &v, &hidden_states, layer, &inputs, &g)?;
            hidden_states = ffn_sublayer(driver, &attn_output, layer, &g)?;
            // All transient tensors (q, k, v, attn_output) dropped here.
            // Only hidden_states survives. Restore cursor so next layer reuses slots.
            driver.restore_pool_cursor(saved);
        }

        // Pad back to [batch, max_seq, hidden] for cls_pool kernel.
        let mut padded_for_pool = driver.alloc_zeros(batch * max_seq * hidden)?;
        driver.pad_to_batch(
            &hidden_states,
            &mut padded_for_pool,
            &inputs.seq_lengths,
            max_seq,
            hidden,
        )?;

        // CLS pooling + L2 normalize.
        let mut pooled = driver.alloc_zeros(batch * hidden)?;
        driver.cls_pool(&mut pooled, &padded_for_pool, batch, max_seq, hidden)?;
        driver.l2_normalize(&mut pooled, batch, hidden)?;

        // End batched mode -- commit all GPU work, wait for completion.
        driver.end_batch()?;

        driver.to_host(&pooled, batch, hidden)
    }
}