-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathlitert_lm.cc
More file actions
668 lines (586 loc) · 25.6 KB
/
litert_lm.cc
File metadata and controls
668 lines (586 loc) · 25.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
// Copyright 2026 The ODML Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <deque>
#include <map>
#include <sstream>
#include <stdexcept>
#include <utility>
#include "nanobind/nanobind.h"
#include "nanobind/stl/optional.h" // IWYU pragma: keep
#include "nanobind/stl/shared_ptr.h"
#include "nanobind/stl/string_view.h" // IWYU pragma: keep
#include "nanobind/stl/unique_ptr.h" // IWYU pragma: keep
#include "nanobind/stl/variant.h" // IWYU pragma: keep
#include "nanobind/stl/vector.h" // IWYU pragma: keep
#include "absl/base/log_severity.h" // from @com_google_absl
#include "absl/log/globals.h" // from @com_google_absl
#include "absl/status/statusor.h" // from @com_google_absl
#include "absl/synchronization/mutex.h" // from @com_google_absl
#include "absl/time/time.h" // from @com_google_absl
#include "nanobind_json/nanobind_json.hpp" // from @nanobind_json // IWYU pragma: keep
#include "litert/c/internal/litert_logging.h" // from @litert
#include "runtime/conversation/conversation.h"
#include "runtime/engine/engine.h"
#include "runtime/engine/engine_factory.h"
#include "tflite/core/c/c_api_types.h" // from @litert
#include "tflite/logger.h" // from @litert
#include "tflite/minimal_logging.h" // from @litert
#define VALUE_OR_THROW(status_or) \
([&]() { \
auto status_or_value = (status_or); \
if (!status_or_value.ok()) { \
std::stringstream ss; \
ss << __FILE__ << ":" << __LINE__ << ": " << __func__ << ": " \
<< status_or_value.status(); \
throw std::runtime_error(ss.str()); \
} \
return std::move(status_or_value).value(); \
}())
namespace litert::lm {
namespace nb = nanobind;
// The maximum number of times the model can call tools in a single turn before
// an error is thrown.
constexpr int kRecurringToolCallLimit = 25;
// Helper to convert Python dict or str to JSON message.
nlohmann::json ParseJsonMessage(const nb::handle& message) {
if (nb::isinstance<nb::dict>(message)) {
return nb::cast<nb::dict>(message);
}
if (nb::isinstance<nb::str>(message)) {
return {{"role", "user"}, {"content", nb::cast<std::string>(message)}};
}
throw std::runtime_error("Message must be a dict or a str.");
}
// Helper to extract C++ Backend from Python Backend enum.
Backend ParseBackend(const nb::handle& handle,
Backend default_val = Backend::CPU) {
if (handle.is_none()) return default_val;
return static_cast<Backend>(nb::cast<int>(nb::object(handle.attr("value"))));
}
// Helper to handle tool calls.
nlohmann::json HandleToolCalls(const nlohmann::json& response,
const nb::dict& tool_map) {
nlohmann::json tool_responses = nlohmann::json::array();
for (const auto& tool_call : response["tool_calls"]) {
if (!tool_call.contains("function")) continue;
std::string name = tool_call["function"]["name"];
nlohmann::json arguments = tool_call["function"]["arguments"];
nlohmann::json json_result;
if (tool_map.contains(name.c_str())) {
nb::object tool_obj = tool_map[name.c_str()];
nb::object py_args = nb::cast(arguments);
try {
nb::object py_result = tool_obj.attr("execute")(py_args);
json_result = nb::cast<nlohmann::json>(py_result);
} catch (const std::exception& e) {
json_result = "Error executing tool: " + std::string(e.what());
}
} else {
json_result = "Error: Tool not found.";
}
tool_responses.push_back({
{"type", "tool_response"},
{"tool_response",
{
{"name", name},
{"response", json_result},
}},
});
}
return {{"role", "tool"}, {"content", tool_responses}};
}
// Helper to inject Python backend attribute.
void SetBackendAttr(nb::object& py_engine, const nb::handle& backend_handle) {
if (backend_handle.is_none()) {
py_engine.attr("backend") =
nb::module_::import_(
"litert_lm.python.interfaces")
.attr("Backend")
.attr("CPU");
} else {
py_engine.attr("backend") = backend_handle;
}
}
// Note: Consider move to C++ API.
enum class LogSeverity {
VERBOSE = 0,
DEBUG = 1,
INFO = 2,
WARNING = 3,
ERROR = 4,
FATAL = 5,
SILENT = 1000,
};
// MessageIterator bridges the asynchronous, callback-based C++ API
// (Conversation::SendMessageAsync) to Python's synchronous iterator protocol
// (__iter__ / __next__).
//
// It provides a thread-safe queue where the background C++ inference thread
// pushes generated message chunks. The Python main thread can then safely
// pull these chunks one by one by iterating over this object.
//
// This design keeps the C++ background thread completely free from Python's
// Global Interpreter Lock (GIL), maximizing concurrency and preventing
// deadlocks.
class MessageIterator {
public:
MessageIterator() = default;
MessageIterator(const MessageIterator&) = delete;
MessageIterator& operator=(const MessageIterator&) = delete;
void Push(absl::StatusOr<Message> message) {
absl::MutexLock lock(&mutex_);
queue_.push_back(std::move(message));
}
nlohmann::json Next() {
absl::StatusOr<Message> message;
{
nb::gil_scoped_release release;
absl::MutexLock lock(&mutex_);
mutex_.Await(absl::Condition(this, &MessageIterator::HasData));
message = std::move(queue_.front());
queue_.pop_front();
}
if (!message.ok()) {
if (absl::IsCancelled(message.status())) {
throw nb::stop_iteration();
}
throw std::runtime_error(message.status().ToString());
}
if (!std::holds_alternative<JsonMessage>(*message)) {
throw std::runtime_error(
"SendMessageAsync did not return a JsonMessage.");
}
auto& json_msg = std::get<JsonMessage>(*message);
if (json_msg.empty()) {
throw nb::stop_iteration();
}
return static_cast<nlohmann::json>(json_msg);
}
bool HasData() const ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_) {
return !queue_.empty();
}
private:
absl::Mutex mutex_;
std::deque<absl::StatusOr<Message>> queue_ ABSL_GUARDED_BY(mutex_);
};
struct PyBenchmarkInfo {
double init_time_in_second;
double time_to_first_token_in_second;
int last_prefill_token_count;
double last_prefill_tokens_per_second;
int last_decode_token_count;
double last_decode_tokens_per_second;
};
class Benchmark {
public:
Benchmark(std::string model_path, Backend backend, int prefill_tokens,
int decode_tokens, std::string cache_dir)
: model_path_(std::move(model_path)),
backend_(backend),
prefill_tokens_(prefill_tokens),
decode_tokens_(decode_tokens),
cache_dir_(std::move(cache_dir)) {}
PyBenchmarkInfo Run() {
auto model_assets = VALUE_OR_THROW(ModelAssets::Create(model_path_));
auto settings =
VALUE_OR_THROW(EngineSettings::CreateDefault(model_assets, backend_));
if (!cache_dir_.empty()) {
settings.GetMutableMainExecutorSettings().SetCacheDir(cache_dir_);
}
auto& benchmark_params = settings.GetMutableBenchmarkParams();
benchmark_params.set_num_prefill_tokens(prefill_tokens_);
benchmark_params.set_num_decode_tokens(decode_tokens_);
auto engine =
VALUE_OR_THROW(EngineFactory::CreateDefault(std::move(settings)));
auto conversation_config =
VALUE_OR_THROW(ConversationConfig::CreateDefault(*engine));
auto conversation =
VALUE_OR_THROW(Conversation::Create(*engine, conversation_config));
// Trigger benchmark
nlohmann::json dummy_message = {
{"role", "user"},
{"content", "Engine ignore this message in this mode."}};
(void)VALUE_OR_THROW(conversation->SendMessage(dummy_message));
auto benchmark_info_cpp = VALUE_OR_THROW(conversation->GetBenchmarkInfo());
PyBenchmarkInfo result;
double total_init_time_ms = 0.0;
for (const auto& phase : benchmark_info_cpp.GetInitPhases()) {
total_init_time_ms += absl::ToDoubleMilliseconds(phase.second);
}
result.init_time_in_second = total_init_time_ms / 1000.0;
result.time_to_first_token_in_second =
benchmark_info_cpp.GetTimeToFirstToken();
int last_prefill_token_count = 0;
double last_prefill_tokens_per_second = 0.0;
if (benchmark_info_cpp.GetTotalPrefillTurns() > 0) {
int last_index =
static_cast<int>(benchmark_info_cpp.GetTotalPrefillTurns()) - 1;
auto turn = benchmark_info_cpp.GetPrefillTurn(last_index);
if (turn.ok()) {
last_prefill_token_count = static_cast<int>(turn->num_tokens);
}
last_prefill_tokens_per_second =
benchmark_info_cpp.GetPrefillTokensPerSec(last_index);
}
result.last_prefill_token_count = last_prefill_token_count;
result.last_prefill_tokens_per_second = last_prefill_tokens_per_second;
int last_decode_token_count = 0;
double last_decode_tokens_per_second = 0.0;
if (benchmark_info_cpp.GetTotalDecodeTurns() > 0) {
int last_index =
static_cast<int>(benchmark_info_cpp.GetTotalDecodeTurns()) - 1;
auto turn = benchmark_info_cpp.GetDecodeTurn(last_index);
if (turn.ok()) {
last_decode_token_count = static_cast<int>(turn->num_tokens);
}
last_decode_tokens_per_second =
benchmark_info_cpp.GetDecodeTokensPerSec(last_index);
}
result.last_decode_token_count = last_decode_token_count;
result.last_decode_tokens_per_second = last_decode_tokens_per_second;
return result;
}
private:
// Path to the model file.
std::string model_path_;
// Hardware backend used for inference.
Backend backend_;
// Number of tokens for the prefill phase.
int prefill_tokens_;
// Number of tokens for the decode phase.
int decode_tokens_;
// Directory for caching compiled model artifacts.
std::string cache_dir_;
};
NB_MODULE(litert_lm_ext, module) {
nb::enum_<LogSeverity>(module, "LogSeverity")
.value("VERBOSE", LogSeverity::VERBOSE)
.value("DEBUG", LogSeverity::DEBUG)
.value("INFO", LogSeverity::INFO)
.value("WARNING", LogSeverity::WARNING)
.value("ERROR", LogSeverity::ERROR)
.value("FATAL", LogSeverity::FATAL)
.value("SILENT", LogSeverity::SILENT)
.export_values();
module.def(
"Engine",
[](absl::string_view model_path, const nb::handle& backend,
int max_num_tokens, absl::string_view cache_dir,
const nb::handle& vision_backend, const nb::handle& audio_backend,
absl::string_view input_prompt_as_hint) {
Backend main_backend = ParseBackend(backend);
std::optional<Backend> vision_backend_opt = std::nullopt;
if (!vision_backend.is_none()) {
vision_backend_opt = ParseBackend(vision_backend);
}
std::optional<Backend> audio_backend_opt = std::nullopt;
if (!audio_backend.is_none()) {
audio_backend_opt = ParseBackend(audio_backend);
}
auto model_assets = VALUE_OR_THROW(ModelAssets::Create(model_path));
auto settings = VALUE_OR_THROW(EngineSettings::CreateDefault(
model_assets, main_backend, vision_backend_opt, audio_backend_opt));
settings.GetMutableMainExecutorSettings().SetMaxNumTokens(
max_num_tokens);
if (!cache_dir.empty()) {
settings.GetMutableMainExecutorSettings().SetCacheDir(
std::string(cache_dir));
}
auto engine = VALUE_OR_THROW(
EngineFactory::CreateDefault(settings, input_prompt_as_hint));
nb::object py_engine = nb::cast(std::move(engine));
py_engine.attr("model_path") = model_path;
SetBackendAttr(py_engine, backend);
py_engine.attr("max_num_tokens") = max_num_tokens;
py_engine.attr("cache_dir") = cache_dir;
return py_engine;
},
nb::arg("model_path"), nb::arg("backend") = nb::none(),
nb::arg("max_num_tokens") = 4096, nb::arg("cache_dir") = "",
nb::arg("vision_backend") = nb::none(),
nb::arg("audio_backend") = nb::none(),
nb::arg("input_prompt_as_hint") = "");
module.def(
"set_min_log_severity",
[](LogSeverity log_severity) {
struct SeverityMapping {
absl::LogSeverityAtLeast absl_severity;
LiteRtLogSeverity litert_severity;
tflite::LogSeverity tflite_severity;
};
static const std::map<LogSeverity, SeverityMapping> mapping = {
{LogSeverity::VERBOSE,
{absl::LogSeverityAtLeast::kInfo, kLiteRtLogSeverityVerbose,
tflite::TFLITE_LOG_VERBOSE}},
{LogSeverity::DEBUG,
{absl::LogSeverityAtLeast::kInfo, kLiteRtLogSeverityDebug,
tflite::TFLITE_LOG_VERBOSE}},
{LogSeverity::INFO,
{absl::LogSeverityAtLeast::kInfo, kLiteRtLogSeverityInfo,
tflite::TFLITE_LOG_INFO}},
{LogSeverity::WARNING,
{absl::LogSeverityAtLeast::kWarning, kLiteRtLogSeverityWarning,
tflite::TFLITE_LOG_WARNING}},
{LogSeverity::ERROR,
{absl::LogSeverityAtLeast::kError, kLiteRtLogSeverityError,
tflite::TFLITE_LOG_ERROR}},
{LogSeverity::FATAL,
{absl::LogSeverityAtLeast::kFatal, kLiteRtLogSeverityError,
tflite::TFLITE_LOG_ERROR}},
{LogSeverity::SILENT,
{absl::LogSeverityAtLeast::kInfinity, kLiteRtLogSeveritySilent,
tflite::TFLITE_LOG_SILENT}},
};
auto mapping_it = mapping.find(log_severity);
const SeverityMapping& severity_mapping =
(mapping_it != mapping.end()) ? mapping_it->second
: mapping.at(LogSeverity::SILENT);
absl::SetMinLogLevel(severity_mapping.absl_severity);
LiteRtSetMinLoggerSeverity(LiteRtGetDefaultLogger(),
severity_mapping.litert_severity);
tflite::logging_internal::MinimalLogger::SetMinimumLogSeverity(
severity_mapping.tflite_severity);
},
nb::arg("log_severity"));
nb::class_<Engine>(module, "_Engine", nb::dynamic_attr())
// Support for Python context managers (with statement).
// __enter__ returns the object itself.
.def("__enter__", [](nb::handle self) { return self; })
// __exit__ immediately destroys the underlying C++ instance to free
// resources deterministically, instead of waiting for garbage collection.
.def(
"__exit__",
[](nb::handle self, nb::handle exc_type, nb::handle exc_value,
nb::handle traceback) { nb::inst_destruct(self); },
nb::arg("exc_type").none(), nb::arg("exc_value").none(),
nb::arg("traceback").none())
.def(
"create_conversation",
[](const nb::object& self, const nb::handle& messages,
const nb::handle& tools) {
Engine& engine = nb::cast<Engine&>(self);
auto builder = ConversationConfig::Builder();
nb::dict py_tool_map;
bool has_preface = false;
JsonPreface json_preface;
if (!messages.is_none()) {
json_preface.messages = nb::cast<nlohmann::json>(messages);
has_preface = true;
}
if (!tools.is_none()) {
static const nb::object tool_from_function =
nb::module_::import_(
"litert_lm.python.tools")
.attr("tool_from_function");
nlohmann::json json_tools = nlohmann::json::array();
for (auto tool : nb::cast<nb::list>(tools)) {
auto tool_obj = tool_from_function(tool);
auto description = tool_obj.attr("get_tool_description")();
std::string name = nb::cast<std::string>(description["name"]);
py_tool_map[name.c_str()] = tool_obj;
json_tools.push_back(nb::cast<nlohmann::json>(description));
}
json_preface.tools = std::move(json_tools);
has_preface = true;
builder.SetEnableConstrainedDecoding(true);
}
if (has_preface) {
builder.SetPreface(json_preface);
}
auto config = VALUE_OR_THROW(builder.Build(engine));
auto conversation =
VALUE_OR_THROW(Conversation::Create(engine, config));
nb::object py_conversation = nb::cast(std::move(conversation));
py_conversation.attr("_tool_map") = py_tool_map;
if (messages.is_none()) {
py_conversation.attr("messages") = nb::list();
} else {
py_conversation.attr("messages") = messages;
}
if (tools.is_none()) {
py_conversation.attr("tools") = nb::list();
} else {
py_conversation.attr("tools") = tools;
}
return py_conversation;
},
nb::kw_only(), nb::arg("messages") = nb::none(),
nb::arg("tools") = nb::none());
nb::class_<Conversation>(module, "Conversation", nb::dynamic_attr())
// Support for Python context managers (with statement).
// __enter__ returns the object itself.
.def("__enter__", [](nb::handle self) { return self; })
// __exit__ immediately destroys the underlying C++ instance to free
// resources deterministically, instead of waiting for garbage collection.
.def(
"__exit__",
[](nb::handle self, nb::handle exc_type, nb::handle exc_value,
nb::handle traceback) { nb::inst_destruct(self); },
nb::arg("exc_type").none(), nb::arg("exc_value").none(),
nb::arg("traceback").none())
.def("cancel_process", &Conversation::CancelProcess)
.def(
"send_message",
[](nb::object self, const nb::handle& message) {
Conversation& conversation = nb::cast<Conversation&>(self);
nlohmann::json current_message = ParseJsonMessage(message);
nb::dict tool_map;
if (nb::hasattr(self, "_tool_map")) {
tool_map = nb::cast<nb::dict>(self.attr("_tool_map"));
}
for (int i = 0; i < kRecurringToolCallLimit; ++i) {
absl::StatusOr<Message> result =
conversation.SendMessage(current_message);
Message message_variant = VALUE_OR_THROW(std::move(result));
if (!std::holds_alternative<JsonMessage>(message_variant)) {
throw std::runtime_error(
"SendMessage did not return a JsonMessage.");
}
nlohmann::json response = std::get<JsonMessage>(message_variant);
if (response.contains("tool_calls") &&
!response["tool_calls"].empty()) {
current_message = HandleToolCalls(response, tool_map);
} else {
return response;
}
}
throw std::runtime_error("Exceeded recurring tool call limit of " +
std::to_string(kRecurringToolCallLimit));
},
nb::arg("message"))
.def(
"send_message_async",
[](nb::object self, const nb::handle& message) {
Conversation& conversation = nb::cast<Conversation&>(self);
nlohmann::json json_message = ParseJsonMessage(message);
auto iterator = std::make_shared<MessageIterator>();
nb::dict tool_map;
if (nb::hasattr(self, "_tool_map")) {
tool_map = nb::cast<nb::dict>(self.attr("_tool_map"));
}
struct AsyncState {
int tool_call_count = 0;
nlohmann::json pending_tool_response = nullptr;
};
auto state = std::make_shared<AsyncState>();
struct Callback {
nb::object self;
std::shared_ptr<MessageIterator> iterator;
nb::dict tool_map;
std::shared_ptr<AsyncState> state;
void operator()(absl::StatusOr<Message> message) const {
if (!message.ok()) {
iterator->Push(std::move(message));
return;
}
if (!std::holds_alternative<JsonMessage>(*message)) {
iterator->Push(absl::InternalError(
"SendMessageAsync did not return a JsonMessage."));
return;
}
auto& json_msg = std::get<JsonMessage>(*message);
if (json_msg.contains("tool_calls") &&
!json_msg["tool_calls"].empty()) {
nb::gil_scoped_acquire acquire;
state->pending_tool_response =
HandleToolCalls(json_msg, tool_map);
}
if (json_msg.contains("content")) {
iterator->Push(std::move(message));
} else if (json_msg.empty()) {
if (state->pending_tool_response != nullptr) {
if (state->tool_call_count >= kRecurringToolCallLimit) {
iterator->Push(absl::InternalError(
"Exceeded recurring tool call limit of " +
std::to_string(kRecurringToolCallLimit)));
return;
}
state->tool_call_count++;
nlohmann::json next_message =
std::move(state->pending_tool_response);
state->pending_tool_response = nullptr;
nb::gil_scoped_acquire acquire;
Conversation& conv = nb::cast<Conversation&>(self);
absl::Status status =
conv.SendMessageAsync(next_message, *this);
if (!status.ok()) {
iterator->Push(status);
}
} else {
iterator->Push(std::move(message));
}
}
}
};
absl::Status status = conversation.SendMessageAsync(
json_message, Callback{self, iterator, tool_map, state});
if (!status.ok()) {
std::stringstream error_msg_stream;
error_msg_stream << "SendMessageAsync failed: " << status;
throw std::runtime_error(error_msg_stream.str());
}
return iterator;
},
nb::arg("message"));
// Expose the MessageIterator to Python so that it can be used in a
// standard `for chunk in stream:` loop. We bind Python's iterator protocol
// (__iter__ and __next__) to our C++ implementation.
nb::class_<MessageIterator>(module, "MessageIterator")
.def("__iter__", [](nb::handle self) { return self; })
.def("__next__", &MessageIterator::Next);
module.def(
"Benchmark",
[](absl::string_view model_path, const nb::handle& backend,
int prefill_tokens, int decode_tokens, absl::string_view cache_dir) {
auto benchmark = std::make_unique<Benchmark>(
std::string(model_path), ParseBackend(backend), prefill_tokens,
decode_tokens, std::string(cache_dir));
nb::object py_benchmark = nb::cast(std::move(benchmark));
py_benchmark.attr("model_path") = model_path;
SetBackendAttr(py_benchmark, backend);
py_benchmark.attr("prefill_tokens") = prefill_tokens;
py_benchmark.attr("decode_tokens") = decode_tokens;
py_benchmark.attr("cache_dir") = cache_dir;
return py_benchmark;
},
nb::arg("model_path"), nb::arg("backend") = nb::none(),
nb::arg("prefill_tokens") = 256, nb::arg("decode_tokens") = 256,
nb::arg("cache_dir") = "");
nb::class_<PyBenchmarkInfo>(module, "BenchmarkInfo",
"Data class to hold benchmark information.")
.def_rw("init_time_in_second", &PyBenchmarkInfo::init_time_in_second,
"The time in seconds to initialize the engine and the "
"conversation.")
.def_rw("time_to_first_token_in_second",
&PyBenchmarkInfo::time_to_first_token_in_second,
"The time in seconds to the first token.")
.def_rw(
"last_prefill_token_count",
&PyBenchmarkInfo::last_prefill_token_count,
"The number of tokens in the last prefill. Returns 0 if there was "
"no prefill.")
.def_rw("last_prefill_tokens_per_second",
&PyBenchmarkInfo::last_prefill_tokens_per_second,
"The number of tokens processed per second in the last prefill.")
.def_rw("last_decode_token_count",
&PyBenchmarkInfo::last_decode_token_count,
"The number of tokens in the last decode. Returns 0 if there was "
"no decode.")
.def_rw("last_decode_tokens_per_second",
&PyBenchmarkInfo::last_decode_tokens_per_second,
"The number of tokens processed per second in the last decode.");
nb::class_<Benchmark>(module, "_Benchmark", nb::dynamic_attr())
.def("run", &Benchmark::Run);
}
} // namespace litert::lm