llama_cpp_2/model.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
//! A safe wrapper around `llama_model`.
use std::ffi::{c_char, CString};
use std::num::NonZeroU16;
use std::os::raw::c_int;
use std::path::Path;
use std::ptr::NonNull;
use crate::context::params::LlamaContextParams;
use crate::context::LlamaContext;
use crate::llama_backend::LlamaBackend;
use crate::model::params::LlamaModelParams;
use crate::token::LlamaToken;
use crate::token_type::{LlamaTokenAttr, LlamaTokenAttrs};
use crate::{
ApplyChatTemplateError, ChatTemplateError, LlamaContextLoadError, LlamaLoraAdapterInitError,
LlamaModelLoadError, NewLlamaChatMessageError, StringToTokenError, TokenToStringError,
};
pub mod params;
/// A safe wrapper around `llama_model`.
#[derive(Debug)]
#[repr(transparent)]
#[allow(clippy::module_name_repetitions)]
pub struct LlamaModel {
pub(crate) model: NonNull<llama_cpp_sys_2::llama_model>,
}
/// A safe wrapper around `llama_lora_adapter`.
#[derive(Debug)]
#[repr(transparent)]
#[allow(clippy::module_name_repetitions)]
pub struct LlamaLoraAdapter {
pub(crate) lora_adapter: NonNull<llama_cpp_sys_2::llama_adapter_lora>,
}
/// A Safe wrapper around `llama_chat_message`
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct LlamaChatMessage {
role: CString,
content: CString,
}
impl LlamaChatMessage {
/// Create a new `LlamaChatMessage`
///
/// # Errors
/// If either of ``role`` or ``content`` contain null bytes.
pub fn new(role: String, content: String) -> Result<Self, NewLlamaChatMessageError> {
Ok(Self {
role: CString::new(role)?,
content: CString::new(content)?,
})
}
}
/// How to determine if we should prepend a bos token to tokens
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AddBos {
/// Add the beginning of stream token to the start of the string.
Always,
/// Do not add the beginning of stream token to the start of the string.
Never,
}
/// How to determine if we should tokenize special tokens
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Special {
/// Allow tokenizing special and/or control tokens which otherwise are not exposed and treated as plaintext. Does not insert a leading space.
Tokenize,
/// Treat special and/or control tokens as plaintext.
Plaintext,
}
unsafe impl Send for LlamaModel {}
unsafe impl Sync for LlamaModel {}
impl LlamaModel {
pub(crate) fn vocab_ptr(&self) -> *const llama_cpp_sys_2::llama_vocab {
unsafe { llama_cpp_sys_2::llama_model_get_vocab(self.model.as_ptr()) }
}
/// get the number of tokens the model was trained on
///
/// # Panics
///
/// If the number of tokens the model was trained on does not fit into an `u32`. This should be impossible on most
/// platforms due to llama.cpp returning a `c_int` (i32 on most platforms) which is almost certainly positive.
#[must_use]
pub fn n_ctx_train(&self) -> u32 {
let n_ctx_train = unsafe { llama_cpp_sys_2::llama_n_ctx_train(self.model.as_ptr()) };
u32::try_from(n_ctx_train).expect("n_ctx_train fits into an u32")
}
/// Get all tokens in the model.
pub fn tokens(
&self,
special: Special,
) -> impl Iterator<Item = (LlamaToken, Result<String, TokenToStringError>)> + '_ {
(0..self.n_vocab())
.map(LlamaToken::new)
.map(move |llama_token| (llama_token, self.token_to_str(llama_token, special)))
}
/// Get the beginning of stream token.
#[must_use]
pub fn token_bos(&self) -> LlamaToken {
let token = unsafe { llama_cpp_sys_2::llama_token_bos(self.vocab_ptr()) };
LlamaToken(token)
}
/// Get the end of stream token.
#[must_use]
pub fn token_eos(&self) -> LlamaToken {
let token = unsafe { llama_cpp_sys_2::llama_token_eos(self.vocab_ptr()) };
LlamaToken(token)
}
/// Get the newline token.
#[must_use]
pub fn token_nl(&self) -> LlamaToken {
let token = unsafe { llama_cpp_sys_2::llama_token_nl(self.vocab_ptr()) };
LlamaToken(token)
}
/// Check if a token represents the end of generation (end of turn, end of sequence, etc.)
#[must_use]
pub fn is_eog_token(&self, token: LlamaToken) -> bool {
unsafe { llama_cpp_sys_2::llama_token_is_eog(self.vocab_ptr(), token.0) }
}
/// Get the decoder start token.
#[must_use]
pub fn decode_start_token(&self) -> LlamaToken {
let token =
unsafe { llama_cpp_sys_2::llama_model_decoder_start_token(self.model.as_ptr()) };
LlamaToken(token)
}
/// Convert single token to a string.
///
/// # Errors
///
/// See [`TokenToStringError`] for more information.
pub fn token_to_str(
&self,
token: LlamaToken,
special: Special,
) -> Result<String, TokenToStringError> {
let bytes = self.token_to_bytes(token, special)?;
Ok(String::from_utf8(bytes)?)
}
/// Convert single token to bytes.
///
/// # Errors
/// See [`TokenToStringError`] for more information.
///
/// # Panics
/// If a [`TokenToStringError::InsufficientBufferSpace`] error returned by
/// [`Self::token_to_bytes_with_size`] contains a positive nonzero value. This should never
/// happen.
pub fn token_to_bytes(
&self,
token: LlamaToken,
special: Special,
) -> Result<Vec<u8>, TokenToStringError> {
match self.token_to_bytes_with_size(token, 8, special, None) {
Err(TokenToStringError::InsufficientBufferSpace(i)) => self.token_to_bytes_with_size(
token,
(-i).try_into().expect("Error buffer size is positive"),
special,
None,
),
x => x,
}
}
/// Convert a vector of tokens to a single string.
///
/// # Errors
///
/// See [`TokenToStringError`] for more information.
pub fn tokens_to_str(
&self,
tokens: &[LlamaToken],
special: Special,
) -> Result<String, TokenToStringError> {
let mut builder: Vec<u8> = Vec::with_capacity(tokens.len() * 4);
for piece in tokens
.iter()
.copied()
.map(|t| self.token_to_bytes(t, special))
{
builder.extend_from_slice(&piece?);
}
Ok(String::from_utf8(builder)?)
}
/// Convert a string to a Vector of tokens.
///
/// # Errors
///
/// - if [`str`] contains a null byte.
///
/// # Panics
///
/// - if there is more than [`usize::MAX`] [`LlamaToken`]s in [`str`].
///
///
/// ```no_run
/// use llama_cpp_2::model::LlamaModel;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use std::path::Path;
/// use llama_cpp_2::model::AddBos;
/// let backend = llama_cpp_2::llama_backend::LlamaBackend::init()?;
/// let model = LlamaModel::load_from_file(&backend, Path::new("path/to/model"), &Default::default())?;
/// let tokens = model.str_to_token("Hello, World!", AddBos::Always)?;
/// # Ok(())
/// # }
pub fn str_to_token(
&self,
str: &str,
add_bos: AddBos,
) -> Result<Vec<LlamaToken>, StringToTokenError> {
let add_bos = match add_bos {
AddBos::Always => true,
AddBos::Never => false,
};
let tokens_estimation = std::cmp::max(8, (str.len() / 2) + usize::from(add_bos));
let mut buffer: Vec<LlamaToken> = Vec::with_capacity(tokens_estimation);
let c_string = CString::new(str)?;
let buffer_capacity =
c_int::try_from(buffer.capacity()).expect("buffer capacity should fit into a c_int");
let size = unsafe {
llama_cpp_sys_2::llama_tokenize(
self.vocab_ptr(),
c_string.as_ptr(),
c_int::try_from(c_string.as_bytes().len())?,
buffer.as_mut_ptr().cast::<llama_cpp_sys_2::llama_token>(),
buffer_capacity,
add_bos,
true,
)
};
// if we fail the first time we can resize the vector to the correct size and try again. This should never fail.
// as a result - size is guaranteed to be positive here.
let size = if size.is_negative() {
buffer.reserve_exact(usize::try_from(-size).expect("usize's are larger "));
unsafe {
llama_cpp_sys_2::llama_tokenize(
self.vocab_ptr(),
c_string.as_ptr(),
c_int::try_from(c_string.as_bytes().len())?,
buffer.as_mut_ptr().cast::<llama_cpp_sys_2::llama_token>(),
-size,
add_bos,
true,
)
}
} else {
size
};
let size = usize::try_from(size).expect("size is positive and usize ");
// Safety: `size` < `capacity` and llama-cpp has initialized elements up to `size`
unsafe { buffer.set_len(size) }
Ok(buffer)
}
/// Get the type of a token.
///
/// # Panics
///
/// If the token type is not known to this library.
#[must_use]
pub fn token_attr(&self, LlamaToken(id): LlamaToken) -> LlamaTokenAttrs {
let token_type = unsafe { llama_cpp_sys_2::llama_token_get_attr(self.vocab_ptr(), id) };
LlamaTokenAttrs::try_from(token_type).expect("token type is valid")
}
/// Convert a token to a string with a specified buffer size.
///
/// Generally you should use [`LlamaModel::token_to_str`] as it is able to decode tokens with
/// any length.
///
/// # Errors
///
/// - if the token type is unknown
/// - the resultant token is larger than `buffer_size`.
/// - the string returend by llama-cpp is not valid utf8.
///
/// # Panics
///
/// - if `buffer_size` does not fit into a [`c_int`].
/// - if the returned size from llama-cpp does not fit into a [`usize`]. (this should never happen)
pub fn token_to_str_with_size(
&self,
token: LlamaToken,
buffer_size: usize,
special: Special,
) -> Result<String, TokenToStringError> {
let bytes = self.token_to_bytes_with_size(token, buffer_size, special, None)?;
Ok(String::from_utf8(bytes)?)
}
/// Convert a token to bytes with a specified buffer size.
///
/// Generally you should use [`LlamaModel::token_to_bytes`] as it is able to handle tokens of
/// any length.
///
/// # Errors
///
/// - if the token type is unknown
/// - the resultant token is larger than `buffer_size`.
///
/// # Panics
///
/// - if `buffer_size` does not fit into a [`c_int`].
/// - if the returned size from llama-cpp does not fit into a [`usize`]. (this should never happen)
pub fn token_to_bytes_with_size(
&self,
token: LlamaToken,
buffer_size: usize,
special: Special,
lstrip: Option<NonZeroU16>,
) -> Result<Vec<u8>, TokenToStringError> {
if token == self.token_nl() {
return Ok(b"\n".to_vec());
}
// unsure what to do with this in the face of the 'special' arg + attr changes
let attrs = self.token_attr(token);
if attrs.is_empty()
|| attrs
.intersects(LlamaTokenAttr::Unknown | LlamaTokenAttr::Byte | LlamaTokenAttr::Unused)
|| attrs.contains(LlamaTokenAttr::Control)
&& (token == self.token_bos() || token == self.token_eos())
{
return Ok(Vec::new());
}
let special = match special {
Special::Tokenize => true,
Special::Plaintext => false,
};
let string = CString::new(vec![b'*'; buffer_size]).expect("no null");
let len = string.as_bytes().len();
let len = c_int::try_from(len).expect("length fits into c_int");
let buf = string.into_raw();
let lstrip = lstrip.map_or(0, |it| i32::from(it.get()));
let size = unsafe {
llama_cpp_sys_2::llama_token_to_piece(
self.vocab_ptr(),
token.0,
buf,
len,
lstrip,
special,
)
};
match size {
0 => Err(TokenToStringError::UnknownTokenType),
i if i.is_negative() => Err(TokenToStringError::InsufficientBufferSpace(i)),
size => {
let string = unsafe { CString::from_raw(buf) };
let mut bytes = string.into_bytes();
let len = usize::try_from(size).expect("size is positive and fits into usize");
bytes.truncate(len);
Ok(bytes)
}
}
}
/// The number of tokens the model was trained on.
///
/// This returns a `c_int` for maximum compatibility. Most of the time it can be cast to an i32
/// without issue.
#[must_use]
pub fn n_vocab(&self) -> i32 {
unsafe { llama_cpp_sys_2::llama_n_vocab(self.vocab_ptr()) }
}
/// The type of vocab the model was trained on.
///
/// # Panics
///
/// If llama-cpp emits a vocab type that is not known to this library.
#[must_use]
pub fn vocab_type(&self) -> VocabType {
// llama_cpp_sys_2::llama_model_get_vocab
let vocab_type = unsafe { llama_cpp_sys_2::llama_vocab_type(self.vocab_ptr()) };
VocabType::try_from(vocab_type).expect("invalid vocab type")
}
/// This returns a `c_int` for maximum compatibility. Most of the time it can be cast to an i32
/// without issue.
#[must_use]
pub fn n_embd(&self) -> c_int {
unsafe { llama_cpp_sys_2::llama_n_embd(self.model.as_ptr()) }
}
/// Get chat template from model.
///
/// # Errors
///
/// * If the model has no chat template
/// * If the chat template is not a valid [`CString`].
#[allow(clippy::missing_panics_doc)] // we statically know this will not panic as
pub fn get_chat_template(&self, buf_size: usize) -> Result<String, ChatTemplateError> {
// longest known template is about 1200 bytes from llama.cpp
let chat_temp = CString::new(vec![b'*'; buf_size]).expect("no null");
let chat_ptr = chat_temp.into_raw();
let chat_name = CString::new("tokenizer.chat_template").expect("no null bytes");
let ret = unsafe {
llama_cpp_sys_2::llama_model_meta_val_str(
self.model.as_ptr(),
chat_name.as_ptr(),
chat_ptr,
buf_size,
)
};
if ret < 0 {
return Err(ChatTemplateError::MissingTemplate(ret));
}
let template_c = unsafe { CString::from_raw(chat_ptr) };
let template = template_c.to_str()?;
let ret: usize = ret.try_into().unwrap();
if template.len() < ret {
return Err(ChatTemplateError::BuffSizeError(ret + 1));
}
Ok(template.to_owned())
}
/// Loads a model from a file.
///
/// # Errors
///
/// See [`LlamaModelLoadError`] for more information.
#[tracing::instrument(skip_all, fields(params))]
pub fn load_from_file(
_: &LlamaBackend,
path: impl AsRef<Path>,
params: &LlamaModelParams,
) -> Result<Self, LlamaModelLoadError> {
let path = path.as_ref();
debug_assert!(Path::new(path).exists(), "{path:?} does not exist");
let path = path
.to_str()
.ok_or(LlamaModelLoadError::PathToStrError(path.to_path_buf()))?;
let cstr = CString::new(path)?;
let llama_model =
unsafe { llama_cpp_sys_2::llama_load_model_from_file(cstr.as_ptr(), params.params) };
let model = NonNull::new(llama_model).ok_or(LlamaModelLoadError::NullResult)?;
tracing::debug!(?path, "Loaded model");
Ok(LlamaModel { model })
}
/// Initializes a lora adapter from a file.
///
/// # Errors
///
/// See [`LlamaLoraAdapterInitError`] for more information.
pub fn lora_adapter_init(
&self,
path: impl AsRef<Path>,
) -> Result<LlamaLoraAdapter, LlamaLoraAdapterInitError> {
let path = path.as_ref();
debug_assert!(Path::new(path).exists(), "{path:?} does not exist");
let path = path
.to_str()
.ok_or(LlamaLoraAdapterInitError::PathToStrError(
path.to_path_buf(),
))?;
let cstr = CString::new(path)?;
let adapter =
unsafe { llama_cpp_sys_2::llama_adapter_lora_init(self.model.as_ptr(), cstr.as_ptr()) };
let adapter = NonNull::new(adapter).ok_or(LlamaLoraAdapterInitError::NullResult)?;
tracing::debug!(?path, "Initialized lora adapter");
Ok(LlamaLoraAdapter {
lora_adapter: adapter,
})
}
/// Create a new context from this model.
///
/// # Errors
///
/// There is many ways this can fail. See [`LlamaContextLoadError`] for more information.
// we intentionally do not derive Copy on `LlamaContextParams` to allow llama.cpp to change the type to be non-trivially copyable.
#[allow(clippy::needless_pass_by_value)]
pub fn new_context(
&self,
_: &LlamaBackend,
params: LlamaContextParams,
) -> Result<LlamaContext, LlamaContextLoadError> {
let context_params = params.context_params;
let context = unsafe {
llama_cpp_sys_2::llama_new_context_with_model(self.model.as_ptr(), context_params)
};
let context = NonNull::new(context).ok_or(LlamaContextLoadError::NullReturn)?;
Ok(LlamaContext::new(self, context, params.embeddings()))
}
/// Apply the models chat template to some messages.
/// See https://github.com/ggerganov/llama.cpp/wiki/Templates-supported-by-llama_chat_apply_template
///
/// `tmpl` of None means to use the default template provided by llama.cpp for the model
///
/// # Errors
/// There are many ways this can fail. See [`ApplyChatTemplateError`] for more information.
#[tracing::instrument(skip_all)]
pub fn apply_chat_template(
&self,
tmpl: Option<String>,
chat: Vec<LlamaChatMessage>,
add_ass: bool,
) -> Result<String, ApplyChatTemplateError> {
// Buffer is twice the length of messages per their recommendation
let message_length = chat.iter().fold(0, |acc, c| {
acc + c.role.to_bytes().len() + c.content.to_bytes().len()
});
let mut buff: Vec<u8> = vec![0; message_length * 2];
// Build our llama_cpp_sys_2 chat messages
let chat: Vec<llama_cpp_sys_2::llama_chat_message> = chat
.iter()
.map(|c| llama_cpp_sys_2::llama_chat_message {
role: c.role.as_ptr(),
content: c.content.as_ptr(),
})
.collect();
// Set the tmpl pointer
let tmpl = tmpl.map(CString::new);
let tmpl_ptr = match &tmpl {
Some(str) => str.as_ref().map_err(Clone::clone)?.as_ptr(),
None => std::ptr::null(),
};
let res = unsafe {
llama_cpp_sys_2::llama_chat_apply_template(
tmpl_ptr,
chat.as_ptr(),
chat.len(),
add_ass,
buff.as_mut_ptr().cast::<c_char>(),
buff.len().try_into().expect("Buffer size exceeds i32::MAX"),
)
};
if res > buff.len().try_into().expect("Buffer size exceeds i32::MAX") {
buff.resize(res.try_into().expect("res is negative"), 0);
let res = unsafe {
llama_cpp_sys_2::llama_chat_apply_template(
tmpl_ptr,
chat.as_ptr(),
chat.len(),
add_ass,
buff.as_mut_ptr().cast::<c_char>(),
buff.len().try_into().expect("Buffer size exceeds i32::MAX"),
)
};
assert_eq!(Ok(res), buff.len().try_into());
}
buff.truncate(res.try_into().expect("res is negative"));
Ok(String::from_utf8(buff)?)
}
}
impl Drop for LlamaModel {
fn drop(&mut self) {
unsafe { llama_cpp_sys_2::llama_free_model(self.model.as_ptr()) }
}
}
/// a rusty equivalent of `llama_vocab_type`
#[repr(u32)]
#[derive(Debug, Eq, Copy, Clone, PartialEq)]
pub enum VocabType {
/// Byte Pair Encoding
BPE = llama_cpp_sys_2::LLAMA_VOCAB_TYPE_BPE as _,
/// Sentence Piece Tokenizer
SPM = llama_cpp_sys_2::LLAMA_VOCAB_TYPE_SPM as _,
}
/// There was an error converting a `llama_vocab_type` to a `VocabType`.
#[derive(thiserror::Error, Debug, Eq, PartialEq)]
pub enum LlamaTokenTypeFromIntError {
/// The value is not a valid `llama_token_type`. Contains the int value that was invalid.
#[error("Unknown Value {0}")]
UnknownValue(llama_cpp_sys_2::llama_vocab_type),
}
impl TryFrom<llama_cpp_sys_2::llama_vocab_type> for VocabType {
type Error = LlamaTokenTypeFromIntError;
fn try_from(value: llama_cpp_sys_2::llama_vocab_type) -> Result<Self, Self::Error> {
match value {
llama_cpp_sys_2::LLAMA_VOCAB_TYPE_BPE => Ok(VocabType::BPE),
llama_cpp_sys_2::LLAMA_VOCAB_TYPE_SPM => Ok(VocabType::SPM),
unknown => Err(LlamaTokenTypeFromIntError::UnknownValue(unknown)),
}
}
}