Skip to main content

token_bias/
token_bias.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3//! Applies one backend-neutral token-bias pipeline to an in-memory vocabulary.
4
5use logit_loom::{CandidateMode, Digest, Pipeline, Stage, TokenBias, TokenId, TransformSpec};
6
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let target = TokenId::new(42)?;
9    let transform = TokenBias::new([(target, 1.5)])?;
10    let specification = TransformSpec::new(
11        Digest::of_bytes("example-transform", b"token-42-bias-v1"),
12        CandidateMode::FullVocabulary,
13        16,
14    )?;
15    let mut pipeline = Pipeline::new(vec![Stage::new(specification, transform)?])?;
16
17    pipeline.begin(&[])?;
18    let mut logits = vec![0.0; 128];
19    pipeline.apply_to_vocabulary(0, &[], &mut logits)?;
20
21    let target_index = usize::try_from(target.get())?;
22    assert_eq!(logits[target_index].to_bits(), 1.5_f32.to_bits());
23    println!("pipeline receipt: {}", pipeline.receipt().digest()?);
24    Ok(())
25}