The runtime API module is the user-facing layer for offline generation. It hides tokenizer/model/KV wiring and exposes a small vLLM-style C++ interface.
include/tiny_llm/runtime/llm.hsrc/runtime/llm.cppinclude/tiny_llm/runtime/engine.hsrc/runtime/engine.cppinclude/tiny_llm/runtime/engine_args.hinclude/tiny_llm/runtime/scheduler_config.h
Class-level details for the high-level facade are maintained in LLM, and the text-level engine frontend is documented in LLMEngine.
- Construct a complete runtime from a HuggingFace model directory.
- Own deployment resources such as tokenizer, workspace allocator, KV memory pool, and engine object.
- Accept string prompts and user sampling parameters.
- Return full completion outputs or invoke a per-step streaming callback.
- Aggregate profiling statistics across a generation call.
LLMOptions configures the convenience facade.
Key attributes:
model: HuggingFace model directory.parallel_config: CPU or CUDA device configuration.weight_file: requested safetensors weight file, defaulting tomodel.safetensors.max_num_seqs: maximum logical active sequence count used to fill scheduler defaults.max_tokens: default maximum generated tokens when user params do not override it.block_size_tokens: tokens per KV cache block.kv_num_blocks: physical KV block count.workspace_pool_size: bytes reserved for per-step workspace.scheduler_config: low-level scheduler options, including the prefill token budget.
LLM is a move-only offline facade.
Important attributes:
options_: normalized construction options.tokenizer_: ownedHFLlamaTokenizer.workspace_: ownedStackAllocator.engine_: ownedLLMEngine.kv_pool_: raw CPU or CUDA KV memory pool.last_generation_profile_: accumulated runtime profile for the most recent generation call.
Main interfaces:
LLM(std::string model): constructs a CPU runtime from a model path.LLM(std::string model, ParallelConfig parallel_config): constructs a runtime for the selected device.LLM(LLMOptions options): constructs a runtime with explicit resource settings.generate(...): blocking batch or single-prompt generation. Pass an optionalCompletionStreamCallbackto receive incremental output events.last_generation_profile(): returns accumulated timings and token counters.
LLMSamplingParams is an alias for UserSamplingParams. A max_tokens value of 0 uses the runtime default from LLMOptions.max_tokens; positive values override it for the request.
Construction behavior:
- Expands
~in the model path. - Validates option values.
- Loads model config.
- Computes KV block size and total KV pool size.
- Constructs tokenizer, workspace, KV pool, and
LLMEngine. - Fills
EngineArgswith HF model construction fields and scheduler configuration.
LLMEngine is the string/token bridge over EngineCore. Class-level details are maintained in LLMEngine.
Important attributes:
core_: ownedEngineCore.last_step_profile_: profile copied from the latest user-visible core step.input_preprocessor_: prompt tokenization, request ID assignment, and sampling normalization.output_preprocessor_: incremental decoding, output state, and finish detection.
Main interfaces:
add_request(prompt, user_params): tokenizes and validates a prompt, adds it to core runtime, and registers output state.has_unfinished_requests(): checks frontend output state.step(): advances the runtime once and returns user-facing outputs.last_step_profile(): returns the most recent profile fromEngineCore.
Finished scheduler/KV state is released by scheduler update paths and a final empty cleanup core step after frontend output state is complete. The cleanup step is validated to produce no user-visible outputs and no scheduled tokens.
EngineArgs is the low-level construction object shared by LLMEngine, EngineCore, Scheduler, and ModelRunner.
Important fields:
- Prebuilt handles:
model,ctx,kv,tokenizer. - Device:
parallel_config. - Model construction:
model_type,hf_model_dir,hf_weight_file,max_batch_size. - Execution construction:
execution_stream,workspace. - KV construction:
kv_num_layers,kv_block_size_tokens,kv_num_blocks,kv_block_size_bytes,kv_memory_pool. - Generation/scheduling:
max_generated_tokens,scheduler_config.
If model is null and model_type is kHFLlamaSafeTensor, ModelRunner constructs the model from the HF directory. If ctx is null, the runtime path creates an ExecutionContext over the provided non-owning workspace; if kv is null, the scheduler constructs/binds KV cache resources from the KV fields.