|
ember.cpp 0.1.0
C++ inference engine for Llama-family and paperkiln models (CPU, CUDA, WebAssembly)
|
Represents an active TinyLlama session holding the loaded model and tokenizer. More...
#include <api.h>

Private Member Functions | |
| TinyLlamaSession (const TinyLlamaSession &)=delete | |
| TinyLlamaSession & | operator= (const TinyLlamaSession &)=delete |
| bool | batch_prefill_parallel (const std::vector< std::vector< int > > &all_tokens, const std::vector< int > &prompt_lengths, std::vector< std::vector< float > > &batch_final_logits) |
| bool | batch_generation_parallel (const std::vector< int > ¤t_tokens, const std::vector< int > &token_positions, const std::vector< int > &original_sequence_indices, std::vector< std::vector< float > > &batch_logits) |
Represents an active TinyLlama session holding the loaded model and tokenizer.
This class provides a high-level interface for text generation using TinyLlama models. It supports both GGUF and SafeTensors model formats, and handles model loading, tokenization, and text generation with various sampling strategies.
| tinyllama::TinyLlamaSession::TinyLlamaSession | ( | const std::string & | model_path, |
| const std::string & | tokenizer_path, | ||
| int | threads = 1, |
||
| int | num_gpu_layers_from_cli = 0, |
||
| bool | cli_use_mmap = true, |
||
| bool | use_kv_quant = false, |
||
| bool | use_batch_generation = false, |
||
| int | max_batch_size = 1 |
||
| ) |
Loads the model, config, and tokenizer from the specified directory or GGUF file.
| model_path | Path to the directory containing model files OR a .gguf file. |
| tokenizer_path | Path to the tokenizer file. |
| threads | Number of threads to use for model loading. |
| num_gpu_layers_from_cli | Number of GPU layers to use from command-line arguments. |
| cli_use_mmap | Whether to use mmap for loading the model. |
| use_kv_quant | Whether to use INT8 quantization for the KVCache on GPU. |
| use_batch_generation | Whether to enable single-token batch generation. |
| max_batch_size | Maximum number of sequences for multi-prompt batch processing (default: 1). |
| std::runtime_error | if loading fails. |
Definition at line 328 of file api.cpp.
References bpe_, chat_template_why_, ember::chat_vocab_from_gguf(), config_, ember_ir::device_supported(), ModelConfig::eos_token_id, eos_token_id_, ember_ir::find_ir(), ModelConfig::hidden_size, microtorch::chat::Template::id(), Logger::info(), KVCache::initialize(), ir_, ir_doc_, ModelConfig::is_gguf_file_loaded, k, kv_cache_, ModelConfig::LLAMA3_TIKTOKEN, ModelConfig::LLAMA_SENTENCEPIECE, SafeTensorsLoader::load_model_config_from_json(), ember::make_chat_setup(), max_batch_size_, ModelConfig::max_position_embeddings, model_, ModelConfig::num_attention_heads, ModelConfig::num_cpu_offload_layers, ModelConfig::num_hidden_layers, ModelConfig::num_key_value_heads, microtorch::bpe::read_gguf_tokenizer(), repetition_penalty_, ember::ChatSetup::resolved, state_k_, state_vocab_, ember::ChatSetup::tmpl, ember::ChatSetup::tok, tokenizer_, ModelConfig::tokenizer_family, ember_ir::tuned_llama_match(), ModelConfig::use_kvcache_quantization, ModelConfig::use_mmap_for_gguf, v, Logger::warning(), ember::ChatSetup::why, and word_template_.
| tinyllama::TinyLlamaSession::~TinyLlamaSession | ( | ) |
Destructor to ensure proper cleanup (e.g., KVCache CUDA memory).
Definition at line 702 of file api.cpp.
References Logger::info().
|
privatedelete |
|
private |
Definition at line 2063 of file api.cpp.
References config_, Logger::error(), ModelConfig::hidden_size, Logger::info(), j, kv_cache_, model_, ModelConfig::num_cpu_offload_layers, ModelConfig::num_hidden_layers, and Logger::warning().
Referenced by generate_batch().
|
private |
Definition at line 1784 of file api.cpp.
References config_, Logger::error(), ModelConfig::hidden_size, Logger::info(), j, kv_cache_, model_, ModelConfig::num_cpu_offload_layers, ModelConfig::num_hidden_layers, and ModelConfig::vocab_size.
Referenced by generate_batch().
|
inline |
Definition at line 213 of file api.h.
References bpe_, ember::insert_state_blocks(), j, state_k_, state_vocab_, and text.
Referenced by bpe_token_count(), and generate().
|
inline |
|
inline |
|
inline |
| std::string tinyllama::TinyLlamaSession::generate | ( | const std::string & | prompt, |
| int | steps = 128, |
||
| float | temperature = 0.1f, |
||
| int | top_k = 40, |
||
| float | top_p = 0.9f, |
||
| const std::string & | system_prompt = "", |
||
| bool | apply_q_a_format = false |
||
| ) |
Generates text based on a given prompt.
This method supports various text generation strategies through its sampling parameters. For temperatures close to 0, it approaches deterministic/greedy sampling. For higher temperatures with top-k and top-p, it produces more diverse outputs.
The method can also apply Q:A formatting to the prompt, which is recommended for both GGUF and SafeTensors models when used via the command-line interface.
| prompt | The input prompt string. |
| steps | The number of tokens to generate. |
| temperature | Sampling temperature. Lower values (e.g., 0.1) make the output more focused and deterministic. |
| top_k | Top-K sampling parameter. Limits sampling to K most likely tokens. Set to 0 or vocab_size to disable. |
| top_p | Nucleus sampling parameter. Limits sampling to tokens comprising top P probability mass (0.0 to 1.0). |
| system_prompt | Optional system prompt to guide the generation. |
| apply_q_a_format | Whether to apply Q:A format ("Q: [prompt]\nA:"). Recommended true for command-line use. |
| std::runtime_error | if generation fails. |
Definition at line 706 of file api.cpp.
References a, tinyllama::apply_contract_penalty(), tinyllama::apply_repetition_penalty(), tinyllama::TtftBreakdown::batch_prefill, bpe_, bpe_prompt_ids(), KVCache::clear_data(), config_, tinyllama::corpus_draft_table(), Logger::debug(), tinyllama::debug_dump_topk(), Tokenizer::DEFAULT, ember_draft::DraftTable::draft(), tinyllama::TtftBreakdown::embed_ms, eos_token_id_, Logger::error(), tinyllama::TtftBreakdown::first_token, generated_stream_, generated_text_for_api_return_, ModelConfig::hidden_size, id, Logger::info(), ir_, j, kv_cache_, tinyllama::TtftBreakdown::kv_reset_ms, kv_token_history_, last_generated_ids_, last_generated_tokens_, last_prompt_ids_, last_stop_id_, last_stop_reason_, last_ttft_, ModelConfig::LLAMA3_TIKTOKEN, ModelConfig::LLAMA_SENTENCEPIECE, tinyllama::log_vector_summary_detailed(), tinyllama::logits_debug_on(), ModelConfig::max_position_embeddings, model_, n, name, ModelConfig::num_cpu_offload_layers, ModelConfig::num_hidden_layers, tinyllama::TtftBreakdown::num_prompt_tokens, tinyllama::TtftBreakdown::op_ms, out, pos, tinyllama::TtftBreakdown::prefill_gpu_span_ms, tinyllama::TtftBreakdown::prefill_wall_ms, tinyllama::TtftBreakdown::prefix_hit_rate, tinyllama::TtftBreakdown::prefix_prev_cached_tokens, tinyllama::TtftBreakdown::prefix_reusable_tokens, tinyllama::TtftBreakdown::prefix_reused_tokens, tinyllama::prompt_lookup_draft(), repeat_last_n_, repeat_penalty_, repetition_penalty_, rng_, tinyllama::TtftBreakdown::sample_ms, tinyllama::sample_top_k_top_p_temperature(), scripted_replies_, KVCache::seq_len, tinyllama::TtftBreakdown::spec_accepted, tinyllama::TtftBreakdown::spec_draft_calls, tinyllama::TtftBreakdown::spec_draft_ms, tinyllama::TtftBreakdown::spec_drafted, tinyllama::TtftBreakdown::spec_passes, state_k_, state_vocab_, tinyllama::TtftBreakdown::t_entry, tinyllama::TtftBreakdown::t_first_token, text, token_callback_, tinyllama::TtftBreakdown::tokenize_ms, tokenizer_, ModelConfig::tokenizer_family, tinyllama::TtftBreakdown::ttft_ms, ember::utf8_valid(), v, ModelConfig::vocab_size, Logger::warning(), and ModelConfig::WORD_LEVEL.
Referenced by generate_batch().
| std::vector< std::string > tinyllama::TinyLlamaSession::generate_batch | ( | const std::vector< std::string > & | prompts, |
| int | steps = 128, |
||
| float | temperature = 0.1f, |
||
| int | top_k = 40, |
||
| float | top_p = 0.9f, |
||
| const std::string & | system_prompt = "", |
||
| bool | apply_q_a_format = false |
||
| ) |
Generates text for multiple prompts in a single batch (parallel processing).
This method processes multiple independent prompts simultaneously, providing significant efficiency gains compared to sequential generate() calls. Each prompt is processed independently with its own KV cache state.
| prompts | Vector of input prompt strings to process in batch. |
| steps | The number of tokens to generate for each prompt. |
| temperature | Sampling temperature. Lower values make output more focused and deterministic. |
| top_k | Top-K sampling parameter. Limits sampling to K most likely tokens. |
| top_p | Nucleus sampling parameter. Limits sampling to tokens comprising top P probability mass. |
| system_prompt | Optional system prompt applied to all prompts in the batch. |
| apply_q_a_format | Whether to apply Q:A format to all prompts in the batch. |
| std::runtime_error | if batch generation fails or if prompts vector is empty. |
Definition at line 1473 of file api.cpp.
References batch_generation_parallel(), batch_prefill_parallel(), KVCache::clear_data(), config_, Tokenizer::DEFAULT, eos_token_id_, Logger::error(), generate(), Logger::info(), KVCache::initialize_batch(), j, kv_cache_, kv_token_history_, ModelConfig::LLAMA3_TIKTOKEN, ModelConfig::LLAMA_SENTENCEPIECE, max_batch_size_, model_, rng_, tinyllama::sample_top_k_top_p_temperature(), KVCache::seq_len, tokenizer_, ModelConfig::tokenizer_family, use_batch_generation_, ModelConfig::vocab_size, and Logger::warning().
|
inline |
|
inline |
|
inline |
| const std::vector< std::string > & tinyllama::TinyLlamaSession::gguf_tokens | ( | ) | const |
Definition at line 38 of file score.cpp.
References j, model_, and GGUFData::tokenizer_tokens.
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
privatedelete |
| int tinyllama::TinyLlamaSession::sample_step | ( | std::vector< float > & | logits, |
| const std::vector< int > & | context, | ||
| float | temperature, | ||
| int | top_k, | ||
| float | top_p | ||
| ) |
Definition at line 1464 of file api.cpp.
References tinyllama::apply_contract_penalty(), bpe_, repeat_last_n_, repeat_penalty_, rng_, and tinyllama::sample_top_k_top_p_temperature().
| int tinyllama::TinyLlamaSession::score_context | ( | ) | const |
Definition at line 32 of file score.cpp.
References config_, ir_, and ModelConfig::max_position_embeddings.
Referenced by score_rows().
| bool tinyllama::TinyLlamaSession::score_reuse_ok | ( | ) | const |
Definition at line 44 of file score.cpp.
References config_, ir_, j, ModelConfig::num_cpu_offload_layers, ModelConfig::num_hidden_layers, off, and v.
Referenced by score_rows().
| std::vector< float > tinyllama::TinyLlamaSession::score_rows | ( | const std::vector< int > & | ids, |
| int | first_row, | ||
| bool | reuse_prefix, | ||
| int * | reused = nullptr |
||
| ) |
Definition at line 56 of file score.cpp.
References KVCache::clear_data(), config_, ModelConfig::hidden_size, ir_, j, kv_cache_, kv_token_history_, model_, n, ModelConfig::num_cpu_offload_layers, ModelConfig::num_hidden_layers, out, score_context(), score_ids_, score_reuse_ok(), score_vocab(), KVCache::seq_len, and TinyLlamaModel::set_gemm_fp32_only().
| int tinyllama::TinyLlamaSession::score_vocab | ( | ) | const |
Definition at line 30 of file score.cpp.
References config_, ir_, and ModelConfig::vocab_size.
Referenced by score_rows().
|
inline |
|
inline |
|
private |
Definition at line 307 of file api.h.
Referenced by bpe_decode(), bpe_encode(), bpe_prompt_ids(), bpe_token_count(), chat_format(), chat_template(), generate(), is_bpe(), is_chat_eos(), is_chat_stop(), sample_step(), and TinyLlamaSession().
|
private |
Definition at line 309 of file api.h.
Referenced by chat_template_why(), and TinyLlamaSession().
|
private |
Definition at line 294 of file api.h.
Referenced by batch_generation_parallel(), batch_prefill_parallel(), generate(), generate_batch(), get_config(), score_context(), score_reuse_ok(), score_rows(), score_vocab(), and TinyLlamaSession().
|
private |
Definition at line 296 of file api.h.
Referenced by generate(), generate_batch(), and TinyLlamaSession().
|
private |
Definition at line 303 of file api.h.
Referenced by generate().
|
private |
Definition at line 304 of file api.h.
Referenced by generate().
|
private |
Definition at line 291 of file api.h.
Referenced by generate(), score_context(), score_reuse_ok(), score_rows(), score_vocab(), and TinyLlamaSession().
|
private |
Definition at line 292 of file api.h.
Referenced by ir_document(), and TinyLlamaSession().
|
private |
Definition at line 295 of file api.h.
Referenced by batch_generation_parallel(), batch_prefill_parallel(), generate(), generate_batch(), get_kv_cache(), score_rows(), and TinyLlamaSession().
|
private |
Definition at line 306 of file api.h.
Referenced by generate(), generate_batch(), and score_rows().
|
private |
Definition at line 312 of file api.h.
Referenced by generate(), and last_generated_ids().
|
private |
Definition at line 311 of file api.h.
Referenced by generate(), and last_generated_tokens().
|
private |
Definition at line 312 of file api.h.
Referenced by generate(), and last_prompt_ids().
|
private |
Definition at line 313 of file api.h.
Referenced by generate(), and last_stop_id().
|
private |
Definition at line 310 of file api.h.
Referenced by generate(), and last_stop_reason().
|
private |
Definition at line 305 of file api.h.
Referenced by generate(), and last_ttft().
|
private |
Definition at line 320 of file api.h.
Referenced by generate_batch(), and TinyLlamaSession().
|
private |
Definition at line 288 of file api.h.
Referenced by batch_generation_parallel(), batch_prefill_parallel(), generate(), generate_batch(), gguf_tokens(), score_rows(), and TinyLlamaSession().
|
private |
Definition at line 300 of file api.h.
Referenced by generate(), sample_step(), and set_repeat_penalty().
|
private |
Definition at line 299 of file api.h.
Referenced by generate(), sample_step(), and set_repeat_penalty().
Definition at line 298 of file api.h.
Referenced by generate(), get_repetition_penalty(), set_repetition_penalty(), and TinyLlamaSession().
|
private |
Definition at line 297 of file api.h.
Referenced by generate(), generate_batch(), sample_step(), and set_seed().
|
private |
Definition at line 317 of file api.h.
Referenced by score_rows().
|
private |
Definition at line 315 of file api.h.
Referenced by generate(), and set_scripted_replies().
|
private |
Definition at line 316 of file api.h.
Referenced by bpe_prompt_ids(), generate(), state_k(), and TinyLlamaSession().
|
private |
Definition at line 316 of file api.h.
Referenced by bpe_prompt_ids(), generate(), and TinyLlamaSession().
Definition at line 314 of file api.h.
Referenced by generate(), and set_token_callback().
|
private |
Definition at line 293 of file api.h.
Referenced by generate(), generate_batch(), get_tokenizer(), and TinyLlamaSession().
|
private |
Definition at line 302 of file api.h.
Referenced by generate_batch().
|
private |
Definition at line 308 of file api.h.
Referenced by chat_template(), and TinyLlamaSession().