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
// scoring.rs
//
// Copyright (c) 2023-2025 Junpei Kawamoto
//
// This software is released under the MIT License.
//
// http://opensource.org/licenses/mit-license.php
//! Structures for scoring.
use super::BatchType;
pub use ffi::{ScoringOptions, ScoringResult};
#[cxx::bridge]
pub(crate) mod ffi {
/// `ScoringOptions` specifies configuration options for the scoring process.
///
/// # Examples
///
/// Example of creating a default `ScoringOptions`:
///
/// ```
/// use ct2rs::sys::ScoringOptions;
///
/// let opts = ScoringOptions::default();
/// # assert_eq!(opts.max_input_length, 1024);
/// # assert_eq!(opts.offset, 0);
/// # assert_eq!(opts.max_batch_size, 0);
/// # assert_eq!(opts.batch_type, Default::default());
/// ```
#[derive(Clone, Debug)]
pub struct ScoringOptions {
/// Truncate the inputs after this many tokens (set 0 to disable truncation).
/// (default: 1024)
pub max_input_length: usize,
/// Offset. (default: 0)
pub offset: i64,
/// The maximum batch size.
/// If the number of inputs is greater than `max_batch_size`,
/// the inputs are sorted by length and split by chunks of `max_batch_size` examples
/// so that the number of padding positions is minimized.
/// (default: 0)
max_batch_size: usize,
/// Whether `max_batch_size` is the number of `examples` or `tokens`.
batch_type: BatchType,
}
/// `ScoringResult` represents the result of a scoring process,
/// containing tokens and their respective scores.
#[derive(Clone, Debug)]
pub struct ScoringResult {
/// The scored tokens.
pub tokens: Vec<String>,
/// Log probability of each token.
pub tokens_score: Vec<f32>,
}
struct _dummy {
_vec_scoring_result: Vec<ScoringResult>,
}
unsafe extern "C++" {
include!("ct2rs/include/config.h");
type BatchType = super::BatchType;
}
}
impl Default for ScoringOptions {
fn default() -> Self {
Self {
max_input_length: 1024,
offset: 0,
max_batch_size: 0,
batch_type: Default::default(),
}
}
}
impl ScoringResult {
/// Calculates and returns the total sum of all token scores.
pub fn cumulated_score(&self) -> f32 {
self.tokens_score.iter().sum()
}
/// Computes the average score per token, returning 0.0 if there are no tokens.
pub fn normalized_score(&self) -> f32 {
let num_tokens = self.tokens_score.len();
if num_tokens == 0 {
return 0.0;
}
self.cumulated_score() / num_tokens as f32
}
}
#[cfg(test)]
mod tests {
use crate::sys::scoring::ScoringResult;
const EPSILON: f32 = 1e-6;
#[test]
fn test_scoring_result() {
let res = ScoringResult {
tokens: vec!["a".to_string(), "b".to_string(), "c".to_string()],
tokens_score: vec![1.0, 2.0, 3.0],
};
assert!((res.cumulated_score() - 6.0).abs() < EPSILON);
assert!((res.normalized_score() - 2.0).abs() < EPSILON);
}
#[test]
fn test_empty_scoring_result() {
let res = ScoringResult {
tokens: vec![],
tokens_score: vec![],
};
assert_eq!(res.cumulated_score(), 0.0);
assert_eq!(res.normalized_score(), 0.0);
}
}