MCPcopy Index your code
hub / github.com/abetlen/llama-cpp-python / test_real_model

Function test_real_model

tests/test_llama.py:100–156  ·  view source on GitHub ↗
(llama_cpp_model_path)

Source from the content-addressed store, hash-verified

98
99
100def test_real_model(llama_cpp_model_path):
101 import os
102
103 assert os.path.exists(llama_cpp_model_path)
104
105 params = llama_cpp.llama_model_default_params()
106 params.use_mmap = llama_cpp.llama_supports_mmap()
107 params.use_mlock = llama_cpp.llama_supports_mlock()
108 params.check_tensors = False
109
110 model = internals.LlamaModel(path_model=llama_cpp_model_path, params=params)
111
112 cparams = llama_cpp.llama_context_default_params()
113 cparams.n_ctx = 16
114 cparams.n_batch = 16
115 cparams.n_ubatch = 16
116 cparams.n_threads = multiprocessing.cpu_count()
117 cparams.n_threads_batch = multiprocessing.cpu_count()
118 cparams.logits_all = False
119 cparams.flash_attn_type = llama_cpp.LLAMA_FLASH_ATTN_TYPE_ENABLED
120
121 context = internals.LlamaContext(model=model, params=cparams)
122 tokens = model.tokenize(b"Hello, world!", add_bos=True, special=True)
123
124 assert tokens == [9419, 11, 1814, 0]
125
126 tokens = model.tokenize(
127 b"The quick brown fox jumps over the lazy dog. The quick brown fox jumps ",
128 add_bos=True,
129 special=True,
130 )
131 prompt_token_count = len(tokens)
132
133 batch = internals.LlamaBatch(n_tokens=len(tokens), embd=0, n_seq_max=1)
134
135 seed = 1337
136 sampler = internals.LlamaSampler()
137 sampler.add_top_k(50)
138 sampler.add_top_p(0.9, 1)
139 sampler.add_temp(0.8)
140 sampler.add_dist(seed)
141
142 result = tokens
143 n_eval = 0
144 for _ in range(4):
145 batch.set_batch(tokens, n_past=n_eval, logits_all=False)
146 context.decode(batch)
147 n_eval += len(tokens)
148 token_id = sampler.sample(context, -1)
149 tokens = [token_id]
150 result += tokens
151
152 output = result[prompt_token_count:]
153 output_text = model.detokenize(output, special=True)
154 # Low-level sampling output varies across CPU and Metal backends.
155 assert len(output) == 4
156 assert output_text
157

Callers

nothing calls this directly

Calls 9

tokenizeMethod · 0.95
add_top_kMethod · 0.95
add_top_pMethod · 0.95
add_tempMethod · 0.95
add_distMethod · 0.95
set_batchMethod · 0.95
decodeMethod · 0.95
sampleMethod · 0.95
detokenizeMethod · 0.95

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…