@@ -41,13 +41,11 @@ static constexpr auto kUseSDPAWithKVCache = "use_sdpa_with_kv_cache";
41
41
Runner::Runner (
42
42
const std::string& model_path,
43
43
const std::string& tokenizer_path,
44
- const float temperature,
45
44
std::optional<const std::string> data_path)
46
45
// NOTE: we observed ~2x loading performance increase on iPhone 15
47
46
// and a ~5% improvement on Galaxy S22 by switching to
48
47
// FileDataLoader instead of MmapDataLoader + UseMlockIgnoreErrors.
49
- : temperature_(temperature),
50
- tokenizer_path_ (tokenizer_path),
48
+ : tokenizer_path_(tokenizer_path),
51
49
metadata_ ({
52
50
{kEnableDynamicShape , false },
53
51
{kMaxSeqLen , 128 },
@@ -134,10 +132,7 @@ Error Runner::load() {
134
132
}
135
133
}
136
134
text_decoder_runner_ = std::make_unique<llm::TextDecoderRunner>(
137
- module_.get (),
138
- metadata_.at (kUseKVCache ),
139
- metadata_.at (kVocabSize ),
140
- temperature_);
135
+ module_.get (), metadata_.at (kUseKVCache ), metadata_.at (kVocabSize ));
141
136
text_prefiller_ = std::make_unique<llm::TextPrefiller>(
142
137
text_decoder_runner_.get (),
143
138
metadata_.at (kUseKVCache ),
@@ -164,11 +159,9 @@ Error Runner::load() {
164
159
165
160
Error Runner::generate (
166
161
const std::string& prompt,
167
- int32_t seq_len ,
162
+ const ::executorch::extension::llm::GenerationConfig& config ,
168
163
std::function<void (const std::string&)> token_callback,
169
- std::function<void(const llm::Stats&)> stats_callback,
170
- bool echo,
171
- bool warmup) {
164
+ std::function<void(const llm::Stats&)> stats_callback) {
172
165
// Prepare the inputs.
173
166
// Use ones-initialized inputs.
174
167
ET_CHECK_MSG (!prompt.empty (), " Prompt cannot be null" );
@@ -178,19 +171,19 @@ Error Runner::generate(
178
171
stats_.model_load_end_ms = llm::time_in_ms ();
179
172
}
180
173
181
- if (warmup ) {
174
+ if (config. warming ) {
182
175
ET_LOG (Info, " Doing a warmup run..." );
183
176
}
184
177
185
178
RUNNER_ET_LOG (
186
- warmup ,
179
+ config. warming ,
187
180
" RSS after loading model: %f MiB (0 if unsupported)" ,
188
181
llm::get_rss_bytes () / 1024.0 / 1024.0 );
189
182
190
183
// Wrap the token_callback with print function
191
184
std::function<void (const std::string&)> wrapped_callback =
192
- [token_callback, warmup ](const std::string& piece) {
193
- if (!warmup ) {
185
+ [token_callback, config ](const std::string& piece) {
186
+ if (!config. warming ) {
194
187
llm::safe_printf (piece.c_str ());
195
188
fflush (stdout);
196
189
}
@@ -204,11 +197,6 @@ Error Runner::generate(
204
197
stats_.inference_start_ms = llm::time_in_ms ();
205
198
shouldStop_ = false ;
206
199
207
- // Set the sequence length to the max seq length if not provided
208
- seq_len = (seq_len > 0 && seq_len <= metadata_.at (kMaxContextLen ))
209
- ? seq_len
210
- : metadata_.at (kMaxContextLen );
211
-
212
200
::tokenizers::Result<std::vector<uint64_t >> encode_res = tokenizer_->encode (
213
201
prompt,
214
202
/* bos */ 0 ,
@@ -225,21 +213,22 @@ Error Runner::generate(
225
213
ET_CHECK_MSG (
226
214
num_prompt_tokens < metadata_.at (kMaxContextLen ),
227
215
" num_prompt_tokens %d >= max_seq_len_ %" PRId64
228
- " , Max seq length exceeded - please increase max seq len value in .../llama2/model.py " ,
216
+ " , Max seq length exceeded - please increase max seq len value in your export script " ,
229
217
num_prompt_tokens,
230
218
metadata_.at (kMaxContextLen ));
231
- ET_CHECK_MSG (
232
- num_prompt_tokens < seq_len,
233
- " num_prompt_tokens %d >= seq_len %d, Sequence length exceeded - please increase the seq_len value passed to generate()" ,
234
- num_prompt_tokens,
235
- seq_len);
219
+
220
+ // Determine max_new_tokens using the GenerationConfig's resolve method
221
+ int max_new_tokens = config.resolve_max_new_tokens (
222
+ metadata_.at (kMaxContextLen ), num_prompt_tokens);
223
+
224
+ ET_LOG (Info, " Max new tokens resolved: %d" , max_new_tokens);
236
225
237
226
// Prefill first
238
227
// Here feed all tokens to the model and get the next predicted token
239
228
// after the prompt. After that we will enter generate loop.
240
229
241
230
// print prompts
242
- if (echo) {
231
+ if (config. echo ) {
243
232
wrapped_callback (prompt);
244
233
}
245
234
int64_t pos = 0 ;
@@ -253,32 +242,38 @@ Error Runner::generate(
253
242
wrapped_callback (
254
243
ET_UNWRAP_TOKENIZER (tokenizer_->decode (cur_token, cur_token)));
255
244
RUNNER_ET_LOG (
256
- warmup ,
245
+ config. warming ,
257
246
" RSS after prompt prefill: %f MiB (0 if unsupported)" ,
258
247
llm::get_rss_bytes () / 1024.0 / 1024.0 );
259
248
260
249
// start the main loop
261
250
prompt_tokens.push_back (cur_token);
251
+
252
+ // Generate max_new_tokens - 1 because prefill already generated 1 token.
262
253
int64_t num_generated_tokens = ET_UNWRAP (text_token_generator_->generate (
263
- prompt_tokens, num_prompt_tokens, seq_len, wrapped_callback));
254
+ prompt_tokens,
255
+ num_prompt_tokens,
256
+ max_new_tokens - 1 ,
257
+ config.temperature ,
258
+ wrapped_callback));
264
259
265
260
stats_.inference_end_ms = llm::time_in_ms ();
266
- if (!warmup ) {
261
+ if (!config. warming ) {
267
262
printf (" \n " );
268
263
}
269
264
RUNNER_ET_LOG (
270
- warmup ,
265
+ config. warming ,
271
266
" RSS after finishing text generation: %f MiB (0 if unsupported)" ,
272
267
llm::get_rss_bytes () / 1024.0 / 1024.0 );
273
268
274
- if (num_prompt_tokens + num_generated_tokens == seq_len ) {
275
- RUNNER_ET_LOG (warmup , " Sequence length ( %i tokens) reached!" , seq_len );
269
+ if (num_generated_tokens == max_new_tokens ) {
270
+ RUNNER_ET_LOG (config. warming , " Max new tokens %i reached!" , max_new_tokens );
276
271
}
277
272
278
273
stats_.num_prompt_tokens = num_prompt_tokens;
279
274
stats_.num_generated_tokens = num_generated_tokens;
280
275
281
- if (warmup ) {
276
+ if (config. warming ) {
282
277
ET_LOG (Info, " Warmup run finished!" );
283
278
} else {
284
279
// Do not print report during warmup
@@ -291,14 +286,15 @@ Error Runner::generate(
291
286
return Error::Ok;
292
287
}
293
288
294
- Error Runner::warmup (const std::string& prompt, int32_t seq_len) {
295
- Error err = generate (
296
- prompt,
297
- seq_len,
298
- /* token_callback=*/ nullptr ,
299
- /* stats_callbak=*/ nullptr ,
300
- /* echo=*/ false ,
301
- /* warmup=*/ true );
289
+ Error Runner::warmup (const std::string& prompt, int32_t max_new_tokens) {
290
+ // Create a GenerationConfig for warmup
291
+ llm::GenerationConfig config{
292
+ .echo = false , .max_new_tokens = max_new_tokens, .warming = true };
293
+
294
+ // Call generate with the warmup config
295
+ Error err = generate (prompt, config);
296
+
297
+ // Reset stats after warmup
302
298
stats_.reset ();
303
299
return err;
304
300
}
0 commit comments