Skip to content

Commit 813e4be

Browse files
authored
Merge pull request #878 from ysimonson/fix-warnings
Fix a bunch of `cargo check` and clippy warnings
2 parents ff7884b + 13f3fe4 commit 813e4be

File tree

9 files changed

+52
-69
lines changed

9 files changed

+52
-69
lines changed

examples/reranker/src/main.rs

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use std::time::Duration;
1212

1313
use anyhow::{bail, Context, Result};
1414
use clap::Parser;
15-
use hf_hub::api::sync::ApiBuilder;
1615

1716
use llama_cpp_2::context::params::{LlamaContextParams, LlamaPoolingType};
1817
use llama_cpp_2::context::LlamaContext;
@@ -92,13 +91,11 @@ fn main() -> Result<()> {
9291
.with_n_threads_batch(std::thread::available_parallelism()?.get().try_into()?)
9392
.with_embeddings(true)
9493
.with_pooling_type(pooling_type);
95-
println!("ctx_params: {:?}", ctx_params);
94+
println!("ctx_params: {ctx_params:?}");
9695
let mut ctx = model
9796
.new_context(&backend, ctx_params)
9897
.with_context(|| "unable to create the llama_context")?;
9998

100-
let n_embd = model.n_embd();
101-
10299
let prompt_lines = {
103100
let mut lines = Vec::new();
104101
for doc in documents {
@@ -108,13 +105,13 @@ fn main() -> Result<()> {
108105
lines
109106
};
110107

111-
println!("prompt_lines: {:?}", prompt_lines);
108+
println!("prompt_lines: {prompt_lines:?}");
112109
// tokenize the prompt
113110
let tokens_lines_list = prompt_lines
114111
.iter()
115112
.map(|line| model.str_to_token(line, AddBos::Always))
116113
.collect::<Result<Vec<_>, _>>()
117-
.with_context(|| format!("failed to tokenize {:?}", prompt_lines))?;
114+
.with_context(|| format!("failed to tokenize {prompt_lines:?}"))?;
118115

119116
let n_ctx = ctx.n_ctx() as usize;
120117
let n_ctx_train = model.n_ctx_train();
@@ -156,7 +153,6 @@ fn main() -> Result<()> {
156153
// } else {
157154
// tokens_lines_list.len()
158155
// };
159-
let mut embeddings_stored = 0;
160156
let mut max_seq_id_batch = 0;
161157
let mut output = Vec::with_capacity(tokens_lines_list.len());
162158

@@ -169,16 +165,10 @@ fn main() -> Result<()> {
169165
&mut ctx,
170166
&mut batch,
171167
max_seq_id_batch,
172-
n_embd,
173168
&mut output,
174169
normalise,
175-
pooling.clone(),
170+
&pooling,
176171
)?;
177-
embeddings_stored += if pooling == "none" {
178-
batch.n_tokens()
179-
} else {
180-
max_seq_id_batch
181-
};
182172
max_seq_id_batch = 0;
183173
batch.clear();
184174
}
@@ -191,34 +181,23 @@ fn main() -> Result<()> {
191181
&mut ctx,
192182
&mut batch,
193183
max_seq_id_batch,
194-
n_embd,
195184
&mut output,
196185
normalise,
197-
pooling.clone(),
186+
&pooling,
198187
)?;
199188

200189
let t_main_end = ggml_time_us();
201190

202191
for (j, embeddings) in output.iter().enumerate() {
203-
if pooling == "none" {
204-
eprintln!("embedding {j}: ");
205-
for i in 0..n_embd as usize {
206-
if !normalise {
207-
eprint!("{:6.5} ", embeddings[i]);
208-
} else {
209-
eprint!("{:9.6} ", embeddings[i]);
210-
}
211-
}
212-
eprintln!();
213-
} else if pooling == "rank" {
192+
if pooling == "rank" {
214193
eprintln!("rerank score {j}: {:8.3}", embeddings[0]);
215194
} else {
216195
eprintln!("embedding {j}: ");
217-
for i in 0..n_embd as usize {
218-
if !normalise {
219-
eprint!("{:6.5} ", embeddings[i]);
196+
for embedding in embeddings {
197+
if normalise {
198+
eprint!("{embedding:9.6} ");
220199
} else {
221-
eprint!("{:9.6} ", embeddings[i]);
200+
eprint!("{embedding:6.5} ");
222201
}
223202
}
224203
eprintln!();
@@ -243,10 +222,9 @@ fn batch_decode(
243222
ctx: &mut LlamaContext,
244223
batch: &mut LlamaBatch,
245224
s_batch: i32,
246-
n_embd: i32,
247225
output: &mut Vec<Vec<f32>>,
248226
normalise: bool,
249-
pooling: String,
227+
pooling: &str,
250228
) -> Result<()> {
251229
eprintln!(
252230
"{}: n_tokens = {}, n_seq = {}",
@@ -266,9 +244,9 @@ fn batch_decode(
266244
.with_context(|| "Failed to get sequence embeddings")?;
267245
let normalized = if normalise {
268246
if pooling == "rank" {
269-
normalize_embeddings(&embeddings, -1)
247+
normalize_embeddings(embeddings, -1)
270248
} else {
271-
normalize_embeddings(&embeddings, 2)
249+
normalize_embeddings(embeddings, 2)
272250
}
273251
} else {
274252
embeddings.to_vec()
@@ -291,27 +269,30 @@ fn normalize_embeddings(input: &[f32], embd_norm: i32) -> Vec<f32> {
291269
0 => {
292270
// max absolute
293271
let max_abs = input.iter().map(|x| x.abs()).fold(0.0f32, f32::max) / 32760.0;
294-
max_abs as f64
272+
f64::from(max_abs)
295273
}
296274
2 => {
297275
// euclidean norm
298276
input
299277
.iter()
300-
.map(|x| (*x as f64).powi(2))
278+
.map(|x| f64::from(*x).powi(2))
301279
.sum::<f64>()
302280
.sqrt()
303281
}
304282
p => {
305283
// p-norm
306-
let sum = input.iter().map(|x| (x.abs() as f64).powi(p)).sum::<f64>();
307-
sum.powf(1.0 / p as f64)
284+
let sum = input
285+
.iter()
286+
.map(|x| f64::from(x.abs()).powi(p))
287+
.sum::<f64>();
288+
sum.powf(1.0 / f64::from(p))
308289
}
309290
};
310291

311292
let norm = if sum > 0.0 { 1.0 / sum } else { 0.0 };
312293

313294
for i in 0..n {
314-
output[i] = (input[i] as f64 * norm) as f32;
295+
output[i] = (f64::from(input[i]) * norm) as f32;
315296
}
316297

317298
output

examples/simple/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ struct Args {
9797
fn parse_key_val(s: &str) -> Result<(String, ParamOverrideValue)> {
9898
let pos = s
9999
.find('=')
100-
.ok_or_else(|| anyhow!("invalid KEY=value: no `=` found in `{}`", s))?;
100+
.ok_or_else(|| anyhow!("invalid KEY=value: no `=` found in `{s}`"))?;
101101
let key = s[..pos].parse()?;
102102
let value: String = s[pos + 1..].parse()?;
103103
let value = i64::from_str(&value)

llama-cpp-2/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ pub struct LogOptions {
449449
impl LogOptions {
450450
/// If enabled, logs are sent to tracing. If disabled, all logs are suppressed. Default is for
451451
/// logs to be sent to tracing.
452+
#[must_use]
452453
pub fn with_logs_enabled(mut self, enabled: bool) -> Self {
453454
self.disabled = !enabled;
454455
self

llama-cpp-2/src/model.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ pub struct LlamaLoraAdapter {
3636
pub(crate) lora_adapter: NonNull<llama_cpp_sys_2::llama_adapter_lora>,
3737
}
3838

39-
/// A performance-friendly wrapper around [LlamaModel::chat_template] which is then
40-
/// fed into [LlamaModel::apply_chat_template] to convert a list of messages into an LLM
41-
/// prompt. Internally the template is stored as a CString to avoid round-trip conversions
39+
/// A performance-friendly wrapper around [`LlamaModel::chat_template`] which is then
40+
/// fed into [`LlamaModel::apply_chat_template`] to convert a list of messages into an LLM
41+
/// prompt. Internally the template is stored as a `CString` to avoid round-trip conversions
4242
/// within the FFI.
4343
#[derive(Eq, PartialEq, Clone, PartialOrd, Ord, Hash)]
4444
pub struct LlamaChatTemplate(CString);
@@ -55,7 +55,7 @@ impl LlamaChatTemplate {
5555
&self.0
5656
}
5757

58-
/// Attempts to convert the CString into a Rust str reference.
58+
/// Attempts to convert the `CString` into a Rust str reference.
5959
pub fn to_str(&self) -> Result<&str, Utf8Error> {
6060
self.0.to_str()
6161
}
@@ -569,7 +569,7 @@ impl LlamaModel {
569569

570570
/// Get chat template from model by name. If the name parameter is None, the default chat template will be returned.
571571
///
572-
/// You supply this into [Self::apply_chat_template] to get back a string with the appropriate template
572+
/// You supply this into [`Self::apply_chat_template`] to get back a string with the appropriate template
573573
/// substitution applied to convert a list of messages into a prompt the LLM can use to complete
574574
/// the chat.
575575
///
@@ -666,11 +666,11 @@ impl LlamaModel {
666666
/// There is many ways this can fail. See [`LlamaContextLoadError`] for more information.
667667
// we intentionally do not derive Copy on `LlamaContextParams` to allow llama.cpp to change the type to be non-trivially copyable.
668668
#[allow(clippy::needless_pass_by_value)]
669-
pub fn new_context(
670-
&self,
669+
pub fn new_context<'a>(
670+
&'a self,
671671
_: &LlamaBackend,
672672
params: LlamaContextParams,
673-
) -> Result<LlamaContext, LlamaContextLoadError> {
673+
) -> Result<LlamaContext<'a>, LlamaContextLoadError> {
674674
let context_params = params.context_params;
675675
let context = unsafe {
676676
llama_cpp_sys_2::llama_new_context_with_model(self.model.as_ptr(), context_params)
@@ -681,14 +681,14 @@ impl LlamaModel {
681681
}
682682

683683
/// Apply the models chat template to some messages.
684-
/// See https://github.com/ggerganov/llama.cpp/wiki/Templates-supported-by-llama_chat_apply_template
684+
/// See <https://github.com/ggerganov/llama.cpp/wiki/Templates-supported-by-llama_chat_apply_template>
685685
///
686-
/// Unlike the llama.cpp apply_chat_template which just randomly uses the ChatML template when given
686+
/// Unlike the llama.cpp `apply_chat_template` which just randomly uses the ChatML template when given
687687
/// a null pointer for the template, this requires an explicit template to be specified. If you want to
688688
/// use "chatml", then just do `LlamaChatTemplate::new("chatml")` or any other model name or template
689689
/// string.
690690
///
691-
/// Use [Self::chat_template] to retrieve the template baked into the model (this is the preferred
691+
/// Use [`Self::chat_template`] to retrieve the template baked into the model (this is the preferred
692692
/// mechanism as using the wrong chat template can result in really unexpected responses from the LLM).
693693
///
694694
/// You probably want to set `add_ass` to true so that the generated template string ends with a the
@@ -764,7 +764,7 @@ where
764764
let mut buffer = vec![0u8; capacity];
765765

766766
// call the foreign function
767-
let result = c_function(buffer.as_mut_ptr() as *mut c_char, buffer.len());
767+
let result = c_function(buffer.as_mut_ptr().cast::<c_char>(), buffer.len());
768768
if result < 0 {
769769
return Err(MetaValError::NegativeReturn(result));
770770
}

llama-cpp-2/src/model/params.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl LlamaModelParams {
149149
/// assert_eq!(count, 0);
150150
/// ```
151151
#[must_use]
152-
pub fn kv_overrides(&self) -> KvOverrides {
152+
pub fn kv_overrides<'a>(&'a self) -> KvOverrides<'a> {
153153
KvOverrides::new(self)
154154
}
155155

@@ -235,7 +235,7 @@ impl LlamaModelParams {
235235
);
236236

237237
// There should be some way to do this without iterating over everything.
238-
for (_i, &c) in key.to_bytes_with_nul().iter().enumerate() {
238+
for &c in key.to_bytes_with_nul().iter() {
239239
c_char::try_from(c).expect("invalid character in key");
240240
}
241241

llama-cpp-2/src/model/params/kv_overrides.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub struct KvOverrides<'a> {
7878
}
7979

8080
impl KvOverrides<'_> {
81-
pub(super) fn new(model_params: &LlamaModelParams) -> KvOverrides {
81+
pub(super) fn new<'a>(model_params: &'a LlamaModelParams) -> KvOverrides<'a> {
8282
KvOverrides { model_params }
8383
}
8484
}

llama-cpp-2/src/sampling.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ impl LlamaSampler {
385385

386386
/// Penalizes tokens for being present in the context.
387387
///
388-
/// Parameters:
388+
/// Parameters:
389389
/// - ``penalty_last_n``: last n tokens to penalize (0 = disable penalty, -1 = context size)
390390
/// - ``penalty_repeat``: 1.0 = disabled
391391
/// - ``penalty_freq``: 0.0 = disabled
@@ -415,15 +415,15 @@ impl LlamaSampler {
415415
/// - ``n_vocab``: [`LlamaModel::n_vocab`]
416416
/// - ``seed``: Seed to initialize random generation with.
417417
/// - ``tau``: The target cross-entropy (or surprise) value you want to achieve for the
418-
/// generated text. A higher value corresponds to more surprising or less predictable text,
419-
/// while a lower value corresponds to less surprising or more predictable text.
418+
/// generated text. A higher value corresponds to more surprising or less predictable text,
419+
/// while a lower value corresponds to less surprising or more predictable text.
420420
/// - ``eta``: The learning rate used to update `mu` based on the error between the target and
421-
/// observed surprisal of the sampled word. A larger learning rate will cause `mu` to be
422-
/// updated more quickly, while a smaller learning rate will result in slower updates.
421+
/// observed surprisal of the sampled word. A larger learning rate will cause `mu` to be
422+
/// updated more quickly, while a smaller learning rate will result in slower updates.
423423
/// - ``m``: The number of tokens considered in the estimation of `s_hat`. This is an arbitrary
424-
/// value that is used to calculate `s_hat`, which in turn helps to calculate the value of `k`.
425-
/// In the paper, they use `m = 100`, but you can experiment with different values to see how
426-
/// it affects the performance of the algorithm.
424+
/// value that is used to calculate `s_hat`, which in turn helps to calculate the value of `k`.
425+
/// In the paper, they use `m = 100`, but you can experiment with different values to see how
426+
/// it affects the performance of the algorithm.
427427
#[must_use]
428428
pub fn mirostat(n_vocab: i32, seed: u32, tau: f32, eta: f32, m: i32) -> Self {
429429
let sampler =
@@ -436,11 +436,11 @@ impl LlamaSampler {
436436
/// # Parameters:
437437
/// - ``seed``: Seed to initialize random generation with.
438438
/// - ``tau``: The target cross-entropy (or surprise) value you want to achieve for the
439-
/// generated text. A higher value corresponds to more surprising or less predictable text,
440-
/// while a lower value corresponds to less surprising or more predictable text.
439+
/// generated text. A higher value corresponds to more surprising or less predictable text,
440+
/// while a lower value corresponds to less surprising or more predictable text.
441441
/// - ``eta``: The learning rate used to update `mu` based on the error between the target and
442-
/// observed surprisal of the sampled word. A larger learning rate will cause `mu` to be
443-
/// updated more quickly, while a smaller learning rate will result in slower updates.
442+
/// observed surprisal of the sampled word. A larger learning rate will cause `mu` to be
443+
/// updated more quickly, while a smaller learning rate will result in slower updates.
444444
#[must_use]
445445
pub fn mirostat_v2(seed: u32, tau: f32, eta: f32) -> Self {
446446
let sampler = unsafe { llama_cpp_sys_2::llama_sampler_init_mirostat_v2(seed, tau, eta) };

llama-cpp-sys-2/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ fn main() {
627627

628628
if matches!(target_os, TargetOs::Linux)
629629
&& target_triple.contains("aarch64")
630-
&& !env::var(format!("CARGO_FEATURE_{}", "native".to_uppercase())).is_ok()
630+
&& env::var(format!("CARGO_FEATURE_{}", "native".to_uppercase())).is_err()
631631
{
632632
// If the native feature is not enabled, we take off the native ARM64 support.
633633
// It is useful in docker environments where the native feature is not enabled.

llama-cpp-sys-2/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
#![allow(non_upper_case_globals)]
44
#![allow(non_camel_case_types)]
55
#![allow(non_snake_case)]
6+
#![allow(unpredictable_function_pointer_comparisons)]
67

78
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

0 commit comments

Comments
 (0)