@@ -6,6 +6,7 @@ use std::num::{NonZeroU8, TryFromIntError};
6
6
7
7
/// Errors that can occur when attempting to prepare values for the kv cache
8
8
#[ derive( Debug , Eq , PartialEq , thiserror:: Error ) ]
9
+ #[ allow( clippy:: module_name_repetitions) ]
9
10
pub enum KvCacheConversionError {
10
11
/// Sequence id conversion to i32 failed
11
12
#[ error( "Provided sequence id is too large for a i32" ) ]
@@ -33,15 +34,16 @@ impl LlamaContext<'_> {
33
34
/// Copy the cache from one sequence to another.
34
35
///
35
36
/// # Returns
36
- /// A `Result` indicating whether the operation was successful. If the either position exceeds
37
- /// the maximum i32 value, no copy is attempted and an `Err` is returned.
37
+ /// A `Result` indicating whether the operation was successful.
38
38
///
39
39
/// # Parameters
40
- ///
41
40
/// * `src` - The sequence id to copy the cache from.
42
41
/// * `dest` - The sequence id to copy the cache to.
43
42
/// * `p0` - The start position of the cache to clear. If `None`, the entire cache is copied up to `p1`.
44
43
/// * `p1` - The end position of the cache to clear. If `None`, the entire cache is copied starting from `p0`.
44
+ ///
45
+ /// # Errors
46
+ /// If either position exceeds [`i32::MAX`].
45
47
pub fn copy_kv_cache_seq (
46
48
& mut self ,
47
49
src : i32 ,
@@ -51,10 +53,10 @@ impl LlamaContext<'_> {
51
53
) -> Result < ( ) , KvCacheConversionError > {
52
54
let p0 = p0
53
55
. map_or ( Ok ( -1 ) , i32:: try_from)
54
- . map_err ( |e| KvCacheConversionError :: P0TooLarge ( e ) ) ?;
56
+ . map_err ( KvCacheConversionError :: P0TooLarge ) ?;
55
57
let p1 = p1
56
58
. map_or ( Ok ( -1 ) , i32:: try_from)
57
- . map_err ( |e| KvCacheConversionError :: P1TooLarge ( e ) ) ?;
59
+ . map_err ( KvCacheConversionError :: P1TooLarge ) ?;
58
60
unsafe {
59
61
llama_cpp_sys_2:: llama_kv_cache_seq_cp ( self . context . as_ptr ( ) , src, dest, p0, p1) ;
60
62
}
@@ -69,10 +71,12 @@ impl LlamaContext<'_> {
69
71
/// either position exceeds the maximum i32 value, no removal is attempted and an `Err` is returned.
70
72
///
71
73
/// # Parameters
72
- ///
73
74
/// * `src` - The sequence id to clear the cache for. If `None`, matches all sequences
74
75
/// * `p0` - The start position of the cache to clear. If `None`, the entire cache is cleared up to `p1`.
75
76
/// * `p1` - The end position of the cache to clear. If `None`, the entire cache is cleared from `p0`.
77
+ ///
78
+ /// # Errors
79
+ /// If the sequence id or either position exceeds [`i32::MAX`].
76
80
pub fn clear_kv_cache_seq (
77
81
& mut self ,
78
82
src : Option < u32 > ,
@@ -81,13 +85,13 @@ impl LlamaContext<'_> {
81
85
) -> Result < bool , KvCacheConversionError > {
82
86
let src = src
83
87
. map_or ( Ok ( -1 ) , i32:: try_from)
84
- . map_err ( |e| KvCacheConversionError :: SeqIdTooLarge ( e ) ) ?;
88
+ . map_err ( KvCacheConversionError :: SeqIdTooLarge ) ?;
85
89
let p0 = p0
86
90
. map_or ( Ok ( -1 ) , i32:: try_from)
87
- . map_err ( |e| KvCacheConversionError :: P0TooLarge ( e ) ) ?;
91
+ . map_err ( KvCacheConversionError :: P0TooLarge ) ?;
88
92
let p1 = p1
89
93
. map_or ( Ok ( -1 ) , i32:: try_from)
90
- . map_err ( |e| KvCacheConversionError :: P1TooLarge ( e ) ) ?;
94
+ . map_err ( KvCacheConversionError :: P1TooLarge ) ?;
91
95
Ok ( unsafe { llama_cpp_sys_2:: llama_kv_cache_seq_rm ( self . context . as_ptr ( ) , src, p0, p1) } )
92
96
}
93
97
@@ -118,15 +122,17 @@ impl LlamaContext<'_> {
118
122
/// - explicitly with [`Self::kv_cache_update`]
119
123
///
120
124
/// # Returns
121
- /// A `Result` indicating whether the operation was successful. If either position
122
- /// exceeds the maximum i32 value, no update is attempted and an `Err` is returned.
125
+ /// A `Result` indicating whether the operation was successful.
123
126
///
124
127
/// # Parameters
125
128
///
126
129
/// * `seq_id` - The sequence id to update
127
130
/// * `p0` - The start position of the cache to update. If `None`, the entire cache is updated up to `p1`.
128
131
/// * `p1` - The end position of the cache to update. If `None`, the entire cache is updated starting from `p0`.
129
132
/// * `delta` - The relative position to add to the tokens
133
+ ///
134
+ /// # Errors
135
+ /// If either position exceeds [`i32::MAX`].
130
136
pub fn kv_cache_seq_add (
131
137
& mut self ,
132
138
seq_id : i32 ,
@@ -136,10 +142,10 @@ impl LlamaContext<'_> {
136
142
) -> Result < ( ) , KvCacheConversionError > {
137
143
let p0 = p0
138
144
. map_or ( Ok ( -1 ) , i32:: try_from)
139
- . map_err ( |e| KvCacheConversionError :: P0TooLarge ( e ) ) ?;
145
+ . map_err ( KvCacheConversionError :: P0TooLarge ) ?;
140
146
let p1 = p1
141
147
. map_or ( Ok ( -1 ) , i32:: try_from)
142
- . map_err ( |e| KvCacheConversionError :: P1TooLarge ( e ) ) ?;
148
+ . map_err ( KvCacheConversionError :: P1TooLarge ) ?;
143
149
unsafe {
144
150
llama_cpp_sys_2:: llama_kv_cache_seq_add ( self . context . as_ptr ( ) , seq_id, p0, p1, delta) ;
145
151
}
@@ -152,15 +158,17 @@ impl LlamaContext<'_> {
152
158
/// - explicitly with [`Self::kv_cache_update`]
153
159
///
154
160
/// # Returns
155
- /// A `Result` indicating whether the operation was successful. If either position
156
- /// exceeds the maximum i32 value, no update is attempted and an `Err` is returned.
161
+ /// A `Result` indicating whether the operation was successful.
157
162
///
158
163
/// # Parameters
159
164
///
160
165
/// * `seq_id` - The sequence id to update
161
166
/// * `p0` - The start position of the cache to update. If `None`, the entire cache is updated up to `p1`.
162
167
/// * `p1` - The end position of the cache to update. If `None`, the entire cache is updated starting from `p0`.
163
168
/// * `d` - The factor to divide the positions by
169
+ ///
170
+ /// # Errors
171
+ /// If either position exceeds [`i32::MAX`].
164
172
pub fn kv_cache_seq_div (
165
173
& mut self ,
166
174
seq_id : i32 ,
@@ -170,10 +178,10 @@ impl LlamaContext<'_> {
170
178
) -> Result < ( ) , KvCacheConversionError > {
171
179
let p0 = p0
172
180
. map_or ( Ok ( -1 ) , i32:: try_from)
173
- . map_err ( |e| KvCacheConversionError :: P0TooLarge ( e ) ) ?;
181
+ . map_err ( KvCacheConversionError :: P0TooLarge ) ?;
174
182
let p1 = p1
175
183
. map_or ( Ok ( -1 ) , i32:: try_from)
176
- . map_err ( |e| KvCacheConversionError :: P1TooLarge ( e ) ) ?;
184
+ . map_err ( KvCacheConversionError :: P1TooLarge ) ?;
177
185
let d = c_int:: from ( d. get ( ) ) ;
178
186
unsafe { llama_cpp_sys_2:: llama_kv_cache_seq_div ( self . context . as_ptr ( ) , seq_id, p0, p1, d) }
179
187
Ok ( ( ) )
@@ -239,7 +247,7 @@ pub struct KVCacheView<'a> {
239
247
view : llama_cpp_sys_2:: llama_kv_cache_view ,
240
248
}
241
249
242
- impl < ' a > KVCacheView < ' a > {
250
+ impl KVCacheView < ' _ > {
243
251
/// Update the KV cache view structure with the current state of the KV cache. (use only for debugging purposes)
244
252
pub fn update ( & mut self ) {
245
253
unsafe {
@@ -314,7 +322,7 @@ impl<'a> KVCacheView<'a> {
314
322
}
315
323
}
316
324
317
- impl < ' a > Drop for KVCacheView < ' a > {
325
+ impl Drop for KVCacheView < ' _ > {
318
326
fn drop ( & mut self ) {
319
327
unsafe {
320
328
llama_cpp_sys_2:: llama_kv_cache_view_free ( & mut self . view ) ;
0 commit comments