Skip to content

Commit 13f3fe4

Browse files
committed
Fix most non-pedantic clippy warnings
1 parent d92f87d commit 13f3fe4

File tree

7 files changed

+46
-54
lines changed

7 files changed

+46
-54
lines changed

examples/reranker/src/main.rs

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,11 @@ fn main() -> Result<()> {
9191
.with_n_threads_batch(std::thread::available_parallelism()?.get().try_into()?)
9292
.with_embeddings(true)
9393
.with_pooling_type(pooling_type);
94-
println!("ctx_params: {:?}", ctx_params);
94+
println!("ctx_params: {ctx_params:?}");
9595
let mut ctx = model
9696
.new_context(&backend, ctx_params)
9797
.with_context(|| "unable to create the llama_context")?;
9898

99-
let n_embd = model.n_embd();
100-
10199
let prompt_lines = {
102100
let mut lines = Vec::new();
103101
for doc in documents {
@@ -107,13 +105,13 @@ fn main() -> Result<()> {
107105
lines
108106
};
109107

110-
println!("prompt_lines: {:?}", prompt_lines);
108+
println!("prompt_lines: {prompt_lines:?}");
111109
// tokenize the prompt
112110
let tokens_lines_list = prompt_lines
113111
.iter()
114112
.map(|line| model.str_to_token(line, AddBos::Always))
115113
.collect::<Result<Vec<_>, _>>()
116-
.with_context(|| format!("failed to tokenize {:?}", prompt_lines))?;
114+
.with_context(|| format!("failed to tokenize {prompt_lines:?}"))?;
117115

118116
let n_ctx = ctx.n_ctx() as usize;
119117
let n_ctx_train = model.n_ctx_train();
@@ -169,7 +167,7 @@ fn main() -> Result<()> {
169167
max_seq_id_batch,
170168
&mut output,
171169
normalise,
172-
pooling.clone(),
170+
&pooling,
173171
)?;
174172
max_seq_id_batch = 0;
175173
batch.clear();
@@ -185,31 +183,21 @@ fn main() -> Result<()> {
185183
max_seq_id_batch,
186184
&mut output,
187185
normalise,
188-
pooling.clone(),
186+
&pooling,
189187
)?;
190188

191189
let t_main_end = ggml_time_us();
192190

193191
for (j, embeddings) in output.iter().enumerate() {
194-
if pooling == "none" {
195-
eprintln!("embedding {j}: ");
196-
for i in 0..n_embd as usize {
197-
if !normalise {
198-
eprint!("{:6.5} ", embeddings[i]);
199-
} else {
200-
eprint!("{:9.6} ", embeddings[i]);
201-
}
202-
}
203-
eprintln!();
204-
} else if pooling == "rank" {
192+
if pooling == "rank" {
205193
eprintln!("rerank score {j}: {:8.3}", embeddings[0]);
206194
} else {
207195
eprintln!("embedding {j}: ");
208-
for i in 0..n_embd as usize {
209-
if !normalise {
210-
eprint!("{:6.5} ", embeddings[i]);
196+
for embedding in embeddings {
197+
if normalise {
198+
eprint!("{embedding:9.6} ");
211199
} else {
212-
eprint!("{:9.6} ", embeddings[i]);
200+
eprint!("{embedding:6.5} ");
213201
}
214202
}
215203
eprintln!();
@@ -236,7 +224,7 @@ fn batch_decode(
236224
s_batch: i32,
237225
output: &mut Vec<Vec<f32>>,
238226
normalise: bool,
239-
pooling: String,
227+
pooling: &str,
240228
) -> Result<()> {
241229
eprintln!(
242230
"{}: n_tokens = {}, n_seq = {}",
@@ -256,9 +244,9 @@ fn batch_decode(
256244
.with_context(|| "Failed to get sequence embeddings")?;
257245
let normalized = if normalise {
258246
if pooling == "rank" {
259-
normalize_embeddings(&embeddings, -1)
247+
normalize_embeddings(embeddings, -1)
260248
} else {
261-
normalize_embeddings(&embeddings, 2)
249+
normalize_embeddings(embeddings, 2)
262250
}
263251
} else {
264252
embeddings.to_vec()
@@ -281,27 +269,30 @@ fn normalize_embeddings(input: &[f32], embd_norm: i32) -> Vec<f32> {
281269
0 => {
282270
// max absolute
283271
let max_abs = input.iter().map(|x| x.abs()).fold(0.0f32, f32::max) / 32760.0;
284-
max_abs as f64
272+
f64::from(max_abs)
285273
}
286274
2 => {
287275
// euclidean norm
288276
input
289277
.iter()
290-
.map(|x| (*x as f64).powi(2))
278+
.map(|x| f64::from(*x).powi(2))
291279
.sum::<f64>()
292280
.sqrt()
293281
}
294282
p => {
295283
// p-norm
296-
let sum = input.iter().map(|x| (x.abs() as f64).powi(p)).sum::<f64>();
297-
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))
298289
}
299290
};
300291

301292
let norm = if sum > 0.0 { 1.0 / sum } else { 0.0 };
302293

303294
for i in 0..n {
304-
output[i] = (input[i] as f64 * norm) as f32;
295+
output[i] = (f64::from(input[i]) * norm) as f32;
305296
}
306297

307298
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: 9 additions & 9 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
///
@@ -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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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/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.

0 commit comments

Comments
 (0)