Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion kernels/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn main() -> Result<()> {
builder.build_lib(build_dir.join("libpagedattention.a"));

let kernel_dir = PathBuf::from("../kernels/");
let absolute_kernel_dir = std::fs::canonicalize(&kernel_dir).unwrap();
let absolute_kernel_dir = std::fs::canonicalize(&kernel_dir)?;

println!(
"cargo:rustc-link-search=native={}",
Expand Down
2 changes: 1 addition & 1 deletion src/backend/custom_ops/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl candle::CustomOp1 for ArgSort {
let dev = storage.device();
let elem_count = layout.shape().elem_count();
let ncols = self.last_dim as i32;
let nrows = (elem_count as i32 / ncols) as i32;
let nrows = elem_count as i32 / ncols;
let dst = unsafe { dev.alloc::<u32>(elem_count) }.w()?;

use std::ffi::c_void;
Expand Down
20 changes: 10 additions & 10 deletions src/backend/gptq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl GPTQMatMul {
let qzeros_ = qzeros_.slice(qzeros_l.start_offset()..);
*qzeros_.device_ptr() as *const c_void
} else {
std::ptr::null() as *const c_void
std::ptr::null()
};

let g_idx_ptr = if self.g_idx.is_some() {
Expand All @@ -91,7 +91,7 @@ impl GPTQMatMul {
let g_idx_ = g_idx_.slice(g_idx_l.start_offset()..);
*g_idx_.device_ptr() as *const c_void
} else {
std::ptr::null() as *const c_void
std::ptr::null()
};

unsafe {
Expand Down Expand Up @@ -123,7 +123,7 @@ impl GPTQMatMul {
size_k as i32,
size_n as i32,
workspace_ptr,
self.group_size as i32,
self.group_size,
*dev.cu_stream() as i64,
);
} else {
Expand All @@ -138,7 +138,7 @@ impl GPTQMatMul {
size_k as i32, //k
size_n as i32, //n
workspace_ptr,
self.group_size as i32,
self.group_size,
*dev.cu_stream() as i64,
);
}
Expand All @@ -155,7 +155,7 @@ impl GPTQMatMul {
size_k as i32,
size_n as i32,
workspace_ptr,
self.group_size as i32,
self.group_size,
*dev.cu_stream() as i64,
);
} else {
Expand All @@ -170,7 +170,7 @@ impl GPTQMatMul {
size_k as i32, //k
size_n as i32, //n
workspace_ptr,
self.group_size as i32,
self.group_size,
*dev.cu_stream() as i64,
);
}
Expand Down Expand Up @@ -282,14 +282,14 @@ impl MarlinRepack {
//in_dim 4096, out_dim 1024 (/pack_factor)
//ws shape [4096, 128]
//out_shape [256, 2048]
out_shape[0] = (q_shape[0] / pack_factor / 2) as usize;
out_shape[1] = (q_shape[1] * pack_factor * 2) as usize;
out_shape[0] = q_shape[0] / pack_factor / 2;
out_shape[1] = q_shape[1] * pack_factor * 2;
} else {
//in_dim 4096 (/pack_factor), out_dim 1024
//ws shape [512, 1024]
//out_shape [256, 2048]
out_shape[0] = (q_shape[0] / 2) as usize;
out_shape[1] = (q_shape[1] * 2) as usize;
out_shape[0] = q_shape[0] / 2;
out_shape[1] = q_shape[1] * 2;
}

let oshape: Shape = out_shape.into();
Expand Down
2 changes: 1 addition & 1 deletion src/backend/paged_attention.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl PagedAttention {
let kv_head_stride = kc_l.stride()[1];

let partition_size = 512;
let max_num_partitions = (self.max_context_len + partition_size - 1) / partition_size;
let max_num_partitions = self.max_context_len.div_ceil(partition_size);
let use_v1 = (max_num_partitions == 1 || num_seqs * num_heads > 512)
&& partition_size % block_size == 0;

Expand Down
14 changes: 7 additions & 7 deletions src/backend/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ impl Progress {
let pb = m.add(ProgressBar::new(size as u64));
pb.set_style(sty.clone());
if n > 1 {
pb.set_message(format!("On Rank {} Device", i));
pb.set_message(format!("On Rank {i} Device"));
}
bars.push(pb);
}

if n > 1 {
m.println(format!("Loading model in {} ranks!", n)).unwrap();
m.println(format!("Loading model in {n} ranks!")).unwrap();
}
Self { m, bars, size }
}
Expand All @@ -71,9 +71,9 @@ impl Progress {
self.bars[idx].inc(progress as u64 - pos);
if self.bars.len() > 1 {
if progress >= self.size {
self.bars[idx].set_message(format!("On Rank {} Device Finished", idx));
self.bars[idx].set_message(format!("On Rank {idx} Device Finished"));
} else {
self.bars[idx].set_message(format!("On Rank {} Device", idx));
self.bars[idx].set_message(format!("On Rank {idx} Device"));
}
}
}
Expand All @@ -84,7 +84,7 @@ impl Progress {
let pos = self.bars[idx].position();
self.bars[idx].inc(self.size as u64 - pos);
if self.bars.len() > 1 {
self.bars[idx].set_message(format!("On Rank {} Device Finished", idx));
self.bars[idx].set_message(format!("On Rank {idx} Device Finished"));
}
}
self.m.clear().unwrap();
Expand Down Expand Up @@ -134,7 +134,7 @@ pub async fn progress_worker(
#[cfg(not(feature = "nccl"))]
let progress_bar = Some(Progress::new(1, length));

let _ = thread::sleep(time::Duration::from_millis(1000 as u64));
let _ = thread::sleep(time::Duration::from_millis(1000_u64));

loop {
{
Expand Down Expand Up @@ -181,7 +181,7 @@ pub async fn progress_worker(
}
}

let _ = thread::sleep(time::Duration::from_millis(500 as u64));
let _ = thread::sleep(time::Duration::from_millis(500_u64));
}
});
handle
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ pub fn hub_load_local_safetensors(
pub fn new_device(ordinal: usize) -> Result<Device> {
if cuda_is_available() {
use candle_core::CudaDevice;
let device = Device::Cuda(CudaDevice::new_with_stream(ordinal).unwrap());
let device = Device::Cuda(CudaDevice::new_with_stream(ordinal)?);
Ok(device)
} else if metal_is_available() {
Ok(Device::new_metal(ordinal)?)
Expand Down
18 changes: 9 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ fn get_cache_config(
/ config.num_hidden_layers
/ 2;
CacheConfig {
block_size: block_size,
block_size,
num_gpu_blocks: Some(num_gpu_blocks),
num_cpu_blocks: Some(num_cpu_blocks),
fully_init: true,
Expand Down Expand Up @@ -152,8 +152,8 @@ fn config_log(
LevelFilter::Trace,
];
let level = level.to_uppercase();
for (i, name) in log_level_names.to_vec().into_iter().enumerate() {
if level.find(name).is_some() {
for (i, name) in log_level_names.iter().copied().enumerate() {
if level.contains(name) {
cfg_filter = log_levels[i]
}
}
Expand Down Expand Up @@ -207,9 +207,9 @@ async fn main() -> Result<(), APIError> {
filenames: {
let path = path.clone().unwrap_or("".to_string());
if Path::new(&path).join(file).exists() {
vec![Path::new(&path).join(file).into()]
vec![Path::new(&path).join(file)]
} else {
panic!("Model file not found {}", file);
panic!("Model file not found {file}");
}
},
},
Expand Down Expand Up @@ -350,7 +350,7 @@ async fn main() -> Result<(), APIError> {

#[cfg(not(feature = "nccl"))]
let (pipelines, global_rank) = {
let log_file = format!("candle-vllm.log");
let log_file = "candle-vllm.log".to_string();
let _ = config_log(logger, args.log, log_file);
(
loader
Expand All @@ -361,7 +361,7 @@ async fn main() -> Result<(), APIError> {
};

let (default_pipelines, pipeline_config) = match pipelines {
Err(e) => panic!("{:?}", e),
Err(e) => panic!("{e:?}"),
Ok((p, c)) => (p, c),
};
let mut config: Option<Config> = None;
Expand All @@ -382,7 +382,7 @@ async fn main() -> Result<(), APIError> {
&cfg,
&cache_cfg,
cache_cfg.dtype,
&pipeline.device(),
pipeline.device(),
num_shards,
)
.unwrap();
Expand Down Expand Up @@ -458,7 +458,7 @@ async fn main() -> Result<(), APIError> {
.route("/v1/chat/completions", post(chat_completions))
.with_state(Arc::new(server_data));

let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", port))
let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{port}"))
.await
.map_err(|e| APIError::new(e.to_string()))?;
axum::serve(listener, app)
Expand Down
14 changes: 7 additions & 7 deletions src/openai/conversation/default_conversation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ impl Conversation for DefaultConversation {
env.set_unknown_method_callback(minijinja_contrib::pycompat::unknown_method_callback);
let template = self.chat_template.as_ref().unwrap();
let mut template = template.replace("[::-1]", "|reverse");
if template.find("{{ meta }}").is_some() {
if template.contains("{{ meta }}") {
template = template.replace("{%- set meta = message.get(\"metadata\", \"\") %}", "");
template = template.replace("{{ meta }}", "");
}
Expand Down Expand Up @@ -198,11 +198,11 @@ impl Conversation for DefaultConversation {
tracing::warn!("apply chat template failed {:?}", e);
}
//no chat template exists? using the built-in template
let system_prompt = if self.system_message.is_some() {
format!("<|system|>\n {}", self.system_message.clone().unwrap())
} else {
"".to_string()
};
let system_prompt = self
.system_message
.as_ref()
.map_or("".to_string(), |msg| format!("<|system|>\n {msg}"));

match self.sep_style {
SeparatorStyle::AddColonSingle
| SeparatorStyle::AddColonSpaceSingle
Expand Down Expand Up @@ -441,7 +441,7 @@ impl Conversation for DefaultConversation {
SeparatorStyle::GLM => {
let mut accum = "[gMASK]<sop>".to_string();
accum += &system_prompt.clone();
for (_, message) in self.messages.iter().enumerate() {
for message in self.messages.iter() {
if message.role.clone() == self.roles.0 {
//user message
accum += &format!("<|user|>\n {}", message.content);
Expand Down
2 changes: 1 addition & 1 deletion src/openai/distributed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ impl ReplicatedLinear {
}

pub fn forward(&self, x: &Tensor) -> Result<Tensor> {
let mut xs = self.linear.forward(&x)?;
let mut xs = self.linear.forward(x)?;
if let Some(bias) = &self.bias {
xs = xs.broadcast_add(bias)?;
}
Expand Down
25 changes: 7 additions & 18 deletions src/openai/logits_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,9 @@ impl LogitsProcessor {
top_p: Option<f32>,
) -> Sampling {
let temperature = temperature.and_then(|v| if v < 1e-7 { None } else { Some(v) });
let top_k: Option<usize> = if top_k.is_some() && top_k.unwrap() > 0 {
Some(top_k.unwrap() as usize)
} else {
None
};
let top_k: Option<usize> = top_k.filter(|&k| k > 0).map(|k| k as usize);

let temperature: Option<f32> = if temperature.is_some() && temperature.unwrap() > 0. {
Some(temperature.unwrap())
} else {
None
};
let temperature: Option<f32> = temperature.filter(|&t| t > 0.0);

match (temperature, top_k, top_p) {
(None, _, _) => Sampling::ArgMax,
Expand Down Expand Up @@ -182,18 +174,16 @@ impl LogitsProcessor {
Ok(prs)
};

let sampling = if sampling_params.is_some() {
let param = sampling_params.as_ref().unwrap();
LogitsProcessor::get_strategy(param.temperature, param.top_k, param.top_p)
} else {
self.sampling.to_owned()
};
let sampling = sampling_params.as_ref().map_or_else(
|| self.sampling.to_owned(),
|param| LogitsProcessor::get_strategy(param.temperature, param.top_k, param.top_p),
);

let next_tokens = match &sampling {
Sampling::ArgMax => self.sample_argmax(&logits)?,
Sampling::All { temperature } => {
let prs = prs(*temperature as f64)?.to_vec2()?;
(0..batch)
.into_iter()
.map(|b| self.sample_multinomial(&prs[b]).unwrap())
.collect()
}
Expand All @@ -203,7 +193,6 @@ impl LogitsProcessor {
// simply sample from the predicted probability distribution
let prs = prs.to_vec2()?;
(0..batch)
.into_iter()
.map(|b| self.sample_multinomial(&prs[b]).unwrap())
.collect()
} else {
Expand Down
19 changes: 8 additions & 11 deletions src/openai/models/gemma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,26 +138,23 @@ impl RotaryEmbedding {
let sin = self.sin.narrow(0, seqlen_offset[0], seq_len)?;
let x_q = q.narrow(0, b, 1)?;
let x_k = k.narrow(0, b, 1)?;
let q_embed = candle_nn::rotary_emb::rope(&x_q, &cos, &sin).unwrap();
let k_embed = candle_nn::rotary_emb::rope(&x_k, &cos, &sin).unwrap();
let q_embed = candle_nn::rotary_emb::rope(&x_q, &cos, &sin)?;
let k_embed = candle_nn::rotary_emb::rope(&x_k, &cos, &sin)?;
q_embeds.push(q_embed);
k_embeds.push(k_embed);
}
Ok((
Tensor::cat(&q_embeds, 0).unwrap(),
Tensor::cat(&k_embeds, 0).unwrap(),
))
Ok((Tensor::cat(&q_embeds, 0)?, Tensor::cat(&k_embeds, 0)?))
}
}

struct MLP {
struct Mlp {
gate_proj: TensorParallelColumnLinear,
up_proj: TensorParallelColumnLinear,
down_proj: TensorParallelRowLinear,
act_fn: candle_nn::Activation,
}

impl MLP {
impl Mlp {
fn new(cfg: &Config, vb: VarBuilder, comm: Rc<Comm>) -> Result<Self> {
let hidden_sz = cfg.hidden_size;
let intermediate_sz = cfg.intermediate_size;
Expand Down Expand Up @@ -197,7 +194,7 @@ impl MLP {
}
}

impl Module for MLP {
impl Module for Mlp {
fn forward(&self, xs: &Tensor) -> Result<Tensor> {
let lhs = self.act_fn.forward(&self.gate_proj.forward(xs)?)?;
let rhs = self.up_proj.forward(xs)?;
Expand Down Expand Up @@ -359,7 +356,7 @@ impl Attention {

struct DecoderLayer {
self_attn: Attention,
mlp: MLP,
mlp: Mlp,
input_layernorm: RmsNorm,
post_feedforward_layernorm: Option<RmsNorm>,
pre_feedforward_layernorm: Option<RmsNorm>,
Expand All @@ -374,7 +371,7 @@ impl DecoderLayer {
comm: Rc<Comm>,
) -> Result<Self> {
let self_attn = Attention::new(rotary_emb, cfg, vb.pp("self_attn"), comm.clone())?;
let mlp = MLP::new(cfg, vb.pp("mlp"), comm.clone())?;
let mlp = Mlp::new(cfg, vb.pp("mlp"), comm.clone())?;
let input_layernorm =
rms_norm(cfg.hidden_size, cfg.rms_norm_eps, vb.pp("input_layernorm"))?;

Expand Down
Loading
Loading