2024-10-31 16:51:57 +00:00
|
|
|
use crate::ffi::{
|
|
|
|
create_single_worker_backend, GenerationParams, LlamaCppBackendImpl, SamplingParams,
|
|
|
|
};
|
2024-10-24 14:42:50 +00:00
|
|
|
use async_trait::async_trait;
|
2024-10-31 23:50:42 +00:00
|
|
|
use cxx::UniquePtr;
|
2024-10-24 14:42:50 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2024-10-31 23:50:42 +00:00
|
|
|
use std::sync::mpsc::{channel, Receiver, SendError, Sender};
|
2024-10-24 14:42:50 +00:00
|
|
|
use std::sync::Arc;
|
2024-10-31 23:50:42 +00:00
|
|
|
use std::thread::{spawn, JoinHandle};
|
2024-10-04 08:42:31 +00:00
|
|
|
use text_generation_router::infer::{Backend, InferError, InferStreamResponse};
|
2024-10-31 23:50:42 +00:00
|
|
|
use text_generation_router::validation::{
|
|
|
|
ValidGenerateRequest, ValidParameters, ValidStoppingParameters,
|
|
|
|
};
|
|
|
|
use text_generation_router::Token;
|
2024-10-24 14:42:50 +00:00
|
|
|
use thiserror::Error;
|
2024-10-31 23:50:42 +00:00
|
|
|
use tokio::sync::mpsc::{unbounded_channel, UnboundedSender};
|
|
|
|
use tokio::sync::TryAcquireError;
|
2024-10-04 08:42:31 +00:00
|
|
|
use tokio_stream::wrappers::UnboundedReceiverStream;
|
2024-10-31 23:50:42 +00:00
|
|
|
use tracing::{error, info};
|
2024-10-04 08:42:31 +00:00
|
|
|
|
2024-10-24 14:42:50 +00:00
|
|
|
unsafe impl Send for LlamaCppBackendImpl {}
|
|
|
|
|
2024-10-31 23:50:42 +00:00
|
|
|
impl From<&ValidParameters> for SamplingParams {
|
|
|
|
fn from(v: &ValidParameters) -> Self {
|
|
|
|
Self {
|
|
|
|
top_k: v.top_k,
|
|
|
|
top_p: v.top_p,
|
|
|
|
frequency_penalty: v.frequency_penalty,
|
|
|
|
repetition_penalty: v.repetition_penalty,
|
|
|
|
seed: v.seed,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&ValidStoppingParameters> for GenerationParams {
|
|
|
|
fn from(v: &ValidStoppingParameters) -> Self {
|
|
|
|
Self {
|
|
|
|
max_new_tokens: v.max_new_tokens,
|
|
|
|
ignore_eos_token: v.ignore_eos_token,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(debug_assertions, derive(Debug))]
|
|
|
|
struct InferContext {
|
|
|
|
pub(crate) stream: UnboundedSender<Result<InferStreamResponse, InferError>>,
|
|
|
|
pub(crate) input_tokens: Arc<Vec<u32>>,
|
|
|
|
pub(crate) generated_tokens: Vec<u32>,
|
|
|
|
pub(crate) generation_params: GenerationParams,
|
|
|
|
pub(crate) sampling_params: SamplingParams,
|
|
|
|
}
|
|
|
|
|
2024-10-24 14:42:50 +00:00
|
|
|
#[derive(Debug, Error)]
|
|
|
|
pub enum LlamaCppBackendError {
|
|
|
|
#[error("Provided GGUF model path {0} doesn't exist")]
|
|
|
|
ModelFileDoesntExist(String),
|
|
|
|
|
|
|
|
#[error("Failed to initialize model from GGUF file {0}: {1}")]
|
|
|
|
ModelInitializationFailed(PathBuf, String),
|
2024-10-24 07:56:40 +00:00
|
|
|
}
|
|
|
|
|
2024-10-31 23:50:42 +00:00
|
|
|
pub struct LlamaCppBackend {
|
|
|
|
backlog: Sender<InferContext>,
|
|
|
|
scheduler_handle: JoinHandle<()>,
|
|
|
|
}
|
2024-10-24 14:42:50 +00:00
|
|
|
|
|
|
|
impl LlamaCppBackend {
|
2024-10-31 16:51:57 +00:00
|
|
|
pub fn new<P: AsRef<Path> + Send>(model_path: P) -> Result<Self, LlamaCppBackendError> {
|
2024-10-24 14:42:50 +00:00
|
|
|
let path = Arc::new(model_path.as_ref());
|
|
|
|
if !path.exists() {
|
|
|
|
return Err(LlamaCppBackendError::ModelFileDoesntExist(
|
|
|
|
path.display().to_string(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2024-10-31 23:50:42 +00:00
|
|
|
let backend = create_single_worker_backend(path.to_str().unwrap()).map_err(|err| {
|
2024-10-31 16:51:57 +00:00
|
|
|
LlamaCppBackendError::ModelInitializationFailed(
|
|
|
|
path.to_path_buf(),
|
|
|
|
err.what().to_string(),
|
|
|
|
)
|
|
|
|
})?;
|
2024-10-24 14:42:50 +00:00
|
|
|
|
|
|
|
info!(
|
|
|
|
"Successfully initialized llama.cpp backend from {}",
|
|
|
|
path.display()
|
|
|
|
);
|
|
|
|
|
2024-10-31 23:50:42 +00:00
|
|
|
let (submitter, receiver) = channel();
|
|
|
|
let handle = spawn(|| scheduler_loop(backend, receiver));
|
|
|
|
Ok(Self {
|
|
|
|
backlog: submitter,
|
|
|
|
scheduler_handle: handle,
|
|
|
|
})
|
2024-10-24 07:56:40 +00:00
|
|
|
}
|
|
|
|
}
|
2024-10-04 08:42:31 +00:00
|
|
|
|
2024-10-31 23:50:42 +00:00
|
|
|
fn scheduler_loop(
|
|
|
|
mut backend: UniquePtr<LlamaCppBackendImpl>,
|
|
|
|
mut backlog: Receiver<InferContext>,
|
|
|
|
) {
|
|
|
|
loop {
|
|
|
|
println!("Looping");
|
|
|
|
if let Ok(mut ctx) = backlog.recv() {
|
|
|
|
println!("{ctx:?}, {}", &ctx.generated_tokens.capacity());
|
|
|
|
match backend.pin_mut().generate(
|
|
|
|
&ctx.input_tokens,
|
|
|
|
&mut ctx.generated_tokens,
|
|
|
|
&ctx.generation_params,
|
|
|
|
&ctx.sampling_params,
|
|
|
|
|new_token_id: u32, new_token_logit: f32, is_eos: bool| {
|
|
|
|
let response = InferStreamResponse::Intermediate {
|
|
|
|
token: Token {
|
|
|
|
id: new_token_id,
|
|
|
|
text: "".to_string(),
|
|
|
|
logprob: new_token_logit,
|
|
|
|
special: false,
|
|
|
|
},
|
|
|
|
top_tokens: vec![],
|
|
|
|
};
|
|
|
|
println!("Generated token: {response:?}");
|
|
|
|
// let _ = tokio::spawn(async {
|
|
|
|
// match ctx.stream.send(Ok(response)) {
|
|
|
|
// Ok(_) => {}
|
|
|
|
// Err(ref err) => {
|
|
|
|
// error!(
|
|
|
|
// "Failed to send back token to the client: {}",
|
|
|
|
// err.to_string()
|
|
|
|
// );
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
},
|
|
|
|
) {
|
|
|
|
Ok(n_tokens) => {
|
|
|
|
unsafe {
|
|
|
|
ctx.generated_tokens.set_len(n_tokens);
|
|
|
|
}
|
|
|
|
println!(
|
|
|
|
"Generated {} tokens -> {:?}",
|
|
|
|
n_tokens, &ctx.generated_tokens
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Err(err) => println!("Error: {}", err),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
info!("IPC channel is closed, exiting the scheduler loop");
|
|
|
|
break;
|
2024-10-28 21:44:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-10-24 14:42:50 +00:00
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl Backend for LlamaCppBackend {
|
2024-10-04 08:42:31 +00:00
|
|
|
fn schedule(
|
|
|
|
&self,
|
2024-10-31 23:50:42 +00:00
|
|
|
request: ValidGenerateRequest,
|
2024-10-04 08:42:31 +00:00
|
|
|
) -> Result<UnboundedReceiverStream<Result<InferStreamResponse, InferError>>, InferError> {
|
2024-10-31 23:50:42 +00:00
|
|
|
if let Some(input_ids) = request.input_ids {
|
|
|
|
let (sx, rx) = unbounded_channel();
|
|
|
|
let sampling_params = SamplingParams::from(&request.parameters);
|
|
|
|
let generation_params = GenerationParams::from(&request.stopping_parameters);
|
|
|
|
|
|
|
|
let ctx = InferContext {
|
|
|
|
stream: sx,
|
|
|
|
input_tokens: Arc::clone(&input_ids),
|
|
|
|
generated_tokens: Vec::with_capacity(generation_params.max_new_tokens as usize),
|
|
|
|
generation_params,
|
|
|
|
sampling_params,
|
|
|
|
};
|
|
|
|
|
|
|
|
match self.backlog.send(ctx) {
|
|
|
|
Ok(_) => Ok(UnboundedReceiverStream::new(rx)),
|
|
|
|
Err(_) => Err(InferError::GenerationError(
|
|
|
|
"Failed to sent the request".to_string(),
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(InferError::GenerationError(
|
|
|
|
"Unsupported modalities".to_string(),
|
|
|
|
))
|
|
|
|
}
|
2024-10-04 08:42:31 +00:00
|
|
|
}
|
|
|
|
|
2024-10-24 14:42:50 +00:00
|
|
|
async fn health(&self, _: bool) -> bool {
|
|
|
|
true
|
2024-10-04 08:42:31 +00:00
|
|
|
}
|
|
|
|
}
|