12 {
14 TinyLlama.cpp Python Bindings
15 =============================
16
17 Python bindings for the tinyllama.cpp inference engine supporting both GGUF and SafeTensors formats.
18
19 Key Classes:
20 - TinyLlamaSession: Main interface for model loading and text generation
21 - ModelConfig: Configuration object containing model parameters and metadata
22
23 Example Usage:
24 import tinyllama_cpp
25
26 # Basic CPU usage
27 session = tinyllama_cpp.TinyLlamaSession(
28 model_path="path/to/model.gguf",
29 tokenizer_path="path/to/tokenizer.json",
30 threads=4
31 )
32
33 # GPU usage with quantized KV cache
34 session = tinyllama_cpp.TinyLlamaSession(
35 model_path="path/to/model.gguf",
36 tokenizer_path="path/to/tokenizer.json",
37 threads=4,
38 n_gpu_layers=-1,
39 use_kv_quant=True
40 )
41
42 # Generate text
43 response = session.generate("What is AI?", steps=64, temperature=0.7)
44 print(response)
45
46 # Batch generation
47 prompts = ["What is AI?", "Explain quantum computing", "Tell me a joke"]
48 responses = session.generate_batch(prompts, steps=64)
49 for prompt, response in zip(prompts, responses):
50 print(f"Q: {prompt}\nA: {response}\n")
51 )pbdoc";
52
53 py::add_ostream_redirect(m, "ostream_redirect");
54
56 Model configuration containing architecture parameters and metadata.
57
58 This class holds all the configuration parameters for a loaded model,
59 including architecture details, tokenizer information, and model metadata.
60 Most fields are automatically populated when loading a model.
61 )pbdoc");
62
64 .def(py::init<>(), "Create an empty ModelConfig object")
85 .def("__repr__",
87 std::string
tf_str =
"UNKNOWN";
90 return "<ModelConfig: vocab_size=" + std::to_string(
cfg.vocab_size) +
91 ", hidden_size=" + std::to_string(
cfg.hidden_size) +
92 ", tokenizer_family=" +
tf_str +
93 ">";
94 }
95 );
96
97 py::enum_<ModelConfig::TokenizerFamily>(
model_config_class,
"TokenizerFamily",
"Enumeration of supported tokenizer families")
101 .export_values();
102
103 py::class_<tinyllama::TinyLlamaSession>(
m,
"TinyLlamaSession", R
"pbdoc(
104 Main interface for TinyLlama model inference.
105
106 This class provides a high-level interface for loading models and generating text.
107 It supports both GGUF and SafeTensors formats, CPU and GPU inference, and various
108 sampling strategies for text generation.
109
110 The session manages the model, tokenizer, and KV cache, providing both single
111 prompt generation and efficient batch processing capabilities.
112 )pbdoc")
114
118 }),
119 py::arg("model_path"),
120 py::arg("tokenizer_path"),
121 py::arg("threads") = 1,
122 py::arg("n_gpu_layers") = 0,
123 py::arg("use_mmap") = true,
124 py::arg("use_kv_quant") = false,
125 py::arg("use_batch_generation") = false,
126 py::arg("max_batch_size") = 1,
127 R"pbdoc(
128 Initialize a TinyLlama inference session.
129
130 Args:
131 model_path (str or Path): Path to model directory (SafeTensors) or .gguf file
132 tokenizer_path (str or Path): Path to tokenizer.json file. For GGUF models with
133 embedded tokenizer, this can be the same as model_path
134 threads (int, optional): Number of CPU threads for inference. Defaults to 1.
135 n_gpu_layers (int, optional): Number of layers to offload to GPU.
136 -1 = all layers, 0 = CPU only. Defaults to 0.
137 use_mmap (bool, optional): Use memory mapping for model loading. Defaults to True.
138 use_kv_quant (bool, optional): Use INT8 quantization for KV cache on GPU.
139 Reduces VRAM usage. Defaults to False.
140 use_batch_generation (bool, optional): Enable optimized batch generation mode.
141 Defaults to False.
142 max_batch_size (int, optional): Maximum number of sequences for batch processing.
143 Defaults to 1.
144
145 Raises:
146 RuntimeError: If model loading fails due to invalid paths, unsupported format,
147 or insufficient resources.
148
149 Example:
150 # Basic CPU usage
151 session = TinyLlamaSession("model.gguf", "tokenizer.json", threads=4)
152
153 # Using pathlib.Path objects
154 from pathlib import Path
155 session = TinyLlamaSession(
156 model_path=Path("model.gguf"),
157 tokenizer_path=Path("tokenizer.json"),
158 threads=4,
159 n_gpu_layers=-1,
160 use_kv_quant=True
161 )
162 )pbdoc"
163 )
164
165
Represents an active TinyLlama session holding the loaded model and tokenizer.
Model configuration structure holding architecture and hyperparameters.
std::string chat_template_string
std::string pre_tokenizer_type
std::string chat_template_type
TokenizerFamily tokenizer_family
int max_position_embeddings