Skip to content

Commit 3c27f80

Browse files
committed
fixed clippy errors
1 parent 0dfbbdc commit 3c27f80

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

llama-cpp-2/src/context/sample.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ impl LlamaContext<'_> {
184184
LlamaToken(token)
185185
}
186186

187-
/// Tail Free Sampling described in https://www.trentonbricken.com/Tail-Free-Sampling/.
187+
/// Tail Free Sampling described in [Tail-Free-Sampling](https://www.trentonbricken.com/Tail-Free-Sampling/).
188188
pub fn sample_tail_free(&self, token_data: &mut LlamaTokenDataArray, z: f32, min_keep: usize) {
189189
let ctx = self.context.as_ptr();
190190
unsafe {
@@ -194,7 +194,7 @@ impl LlamaContext<'_> {
194194
}
195195
}
196196

197-
/// Locally Typical Sampling implementation described in the paper https://arxiv.org/abs/2202.00666.
197+
/// Locally Typical Sampling implementation described in the [paper](https://arxiv.org/abs/2202.00666).
198198
pub fn sample_typical(&self, token_data: &mut LlamaTokenDataArray, p: f32, min_keep: usize) {
199199
let ctx = self.context.as_ptr();
200200
unsafe {
@@ -204,7 +204,7 @@ impl LlamaContext<'_> {
204204
}
205205
}
206206

207-
/// Nucleus sampling described in academic paper \"The Curious Case of Neural Text Degeneration\" https://arxiv.org/abs/1904.09751"
207+
/// Nucleus sampling described in academic paper [The Curious Case of Neural Text Degeneration](https://arxiv.org/abs/1904.09751)"
208208
pub fn sample_top_p(&self, token_data: &mut LlamaTokenDataArray, p: f32, min_keep: usize) {
209209
let ctx = self.context.as_ptr();
210210
unsafe {
@@ -214,7 +214,7 @@ impl LlamaContext<'_> {
214214
}
215215
}
216216

217-
/// Minimum P sampling as described in https://github.com/ggerganov/llama.cpp/pull/3841"
217+
/// Minimum P sampling as described in [#3841](https://github.com/ggerganov/llama.cpp/pull/3841)
218218
pub fn sample_min_p(
219219
&self,
220220
llama_token_data: &mut LlamaTokenDataArray,
@@ -229,7 +229,7 @@ impl LlamaContext<'_> {
229229
}
230230
}
231231

232-
/// Top-K sampling described in academic paper "The Curious Case of Neural Text Degeneration" https://arxiv.org/abs/1904.09751
232+
/// Top-K sampling described in academic paper [The Curious Case of Neural Text Degeneration](https://arxiv.org/abs/1904.09751)
233233
pub fn sample_top_k(&self, token_data: &mut LlamaTokenDataArray, k: i32, min_keep: usize) {
234234
let ctx = self.context.as_ptr();
235235
unsafe {

llama-cpp-2/src/context/session.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ pub enum LoadSessionError {
3535
/// failed to convert path to str
3636
#[error("failed to convert path {0} to str")]
3737
PathToStrError(PathBuf),
38+
39+
/// Insufficient max length
40+
#[error("max_length is not large enough to hold {n_out} (was {max_tokens})")]
41+
InsufficientMaxLength {
42+
/// The length of the session file
43+
n_out: usize,
44+
/// The maximum length
45+
max_tokens: usize,
46+
},
3847
}
3948

4049
impl LlamaContext<'_> {
@@ -44,9 +53,9 @@ impl LlamaContext<'_> {
4453
///
4554
/// * `path_session` - The file to save to.
4655
/// * `tokens` - The tokens to associate the session with. This should be a prefix of a sequence of tokens that the context has processed, so that the relevant KV caches are already filled.
47-
///
56+
///
4857
/// # Errors
49-
///
58+
///
5059
/// Fails if the path is not a valid utf8, is not a valid c string, or llama.cpp fails to save the session file.
5160
pub fn save_session_file(
5261
&self,
@@ -64,7 +73,7 @@ impl LlamaContext<'_> {
6473
llama_cpp_sys_2::llama_save_session_file(
6574
self.context.as_ptr(),
6675
cstr.as_ptr(),
67-
tokens.as_ptr() as *const i32,
76+
tokens.as_ptr().cast::<llama_cpp_sys_2::llama_token>(),
6877
tokens.len(),
6978
)
7079
} {
@@ -81,9 +90,9 @@ impl LlamaContext<'_> {
8190
///
8291
/// * `path_session` - The file to load from. It must be a session file from a compatible context, otherwise the function will error.
8392
/// * `max_tokens` - The maximum token length of the loaded session. If the session was saved with a longer length, the function will error.
84-
///
93+
///
8594
/// # Errors
86-
///
95+
///
8796
/// Fails if the path is not a valid utf8, is not a valid c string, or llama.cpp fails to load the session file. (e.g. the file does not exist, is not a session file, etc.)
8897
pub fn load_session_file(
8998
&mut self,
@@ -103,11 +112,17 @@ impl LlamaContext<'_> {
103112
if llama_cpp_sys_2::llama_load_session_file(
104113
self.context.as_ptr(),
105114
cstr.as_ptr(),
106-
tokens.as_mut_ptr().cast::<i32>(),
115+
// cast is valid as LlamaToken is repr(transparent)
116+
Vec::<LlamaToken>::as_mut_ptr(&mut tokens).cast::<llama_cpp_sys_2::llama_token>(),
107117
max_tokens,
108118
&mut n_out,
109119
) {
110-
assert!(n_out <= max_tokens, "n_out is greater than max_tokens");
120+
if n_out > max_tokens {
121+
return Err(LoadSessionError::InsufficientMaxLength {
122+
n_out,
123+
max_tokens,
124+
});
125+
}
111126
tokens.set_len(n_out);
112127
Ok(tokens)
113128
} else {

0 commit comments

Comments
 (0)