litert-lm-api 0.14.0__py3-none-android_23_arm64_v8a.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
litert_lm/__init__.py ADDED
@@ -0,0 +1,69 @@
1
+ # Copyright 2026 The ODML Authors.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """LiteRT LM is a library for running GenAI models on devices."""
16
+
17
+ from ._ffi import ActivationDataType
18
+ from ._ffi import LogSeverity
19
+ from ._ffi import set_min_log_severity
20
+ from ._messages import Content
21
+ from ._messages import Contents
22
+ from ._messages import Message
23
+ from ._messages import Role
24
+ from ._messages import ToolCall
25
+ from .benchmark import Benchmark
26
+ from .conversation import Conversation
27
+ from .engine import Engine
28
+ from .interfaces import AbstractBenchmark
29
+ from .interfaces import AbstractConversation
30
+ from .interfaces import AbstractEngine
31
+ from .interfaces import AbstractSession
32
+ from .interfaces import Backend
33
+ from .interfaces import BenchmarkInfo
34
+ from .interfaces import LoraConfig
35
+ from .interfaces import LoraRankConfig
36
+ from .interfaces import Responses
37
+ from .interfaces import SamplerConfig
38
+ from .interfaces import Tool
39
+ from .interfaces import ToolEventHandler
40
+ from .session import Session
41
+ from .tools import tool_from_function
42
+
43
+ __all__ = (
44
+ "AbstractBenchmark",
45
+ "AbstractConversation",
46
+ "AbstractEngine",
47
+ "AbstractSession",
48
+ "ActivationDataType",
49
+ "Backend",
50
+ "Benchmark",
51
+ "BenchmarkInfo",
52
+ "Content",
53
+ "Contents",
54
+ "Conversation",
55
+ "Engine",
56
+ "LogSeverity",
57
+ "LoraConfig",
58
+ "LoraRankConfig",
59
+ "Message",
60
+ "Responses",
61
+ "Role",
62
+ "SamplerConfig",
63
+ "Session",
64
+ "Tool",
65
+ "ToolCall",
66
+ "ToolEventHandler",
67
+ "set_min_log_severity",
68
+ "tool_from_function",
69
+ )
litert_lm/_ffi.py ADDED
@@ -0,0 +1,540 @@
1
+ # Copyright 2026 The ODML Authors.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ """Low-level C library loading and FFI signatures."""
15
+
16
+ from __future__ import annotations
17
+
18
+ import ctypes
19
+ import enum
20
+ from importlib import resources
21
+ import os
22
+
23
+
24
+ class c_string_p(ctypes.c_char_p): # pylint: disable=invalid-name
25
+ """Custom ctypes type that automatically encodes Python strings to UTF-8 bytes."""
26
+
27
+ @classmethod
28
+ def from_param(cls, obj):
29
+ if obj is None:
30
+ return None
31
+ if isinstance(obj, str):
32
+ return obj.encode("utf-8")
33
+ return obj
34
+
35
+
36
+ class InputDataType(enum.IntEnum):
37
+ TEXT = 0
38
+ IMAGE = 1
39
+ IMAGE_END = 2
40
+ AUDIO = 3
41
+ AUDIO_END = 4
42
+
43
+
44
+ class TokenUnionType(enum.IntEnum):
45
+ STRING = 0
46
+ IDS = 1
47
+
48
+
49
+ class SamplerType(enum.IntEnum):
50
+ TOP_K = 1
51
+ TOP_P = 2
52
+ GREEDY = 3
53
+
54
+
55
+ # C-compatible callback type that matches 'LiteRtLmStreamCallback' in engine.h.
56
+ STREAM_CALLBACK_TYPE = ctypes.CFUNCTYPE(
57
+ None, ctypes.c_void_p, ctypes.c_char_p, ctypes.c_bool, ctypes.c_char_p
58
+ )
59
+
60
+
61
+ class LogSeverity(enum.IntEnum):
62
+ VERBOSE = 0
63
+ DEBUG = 1
64
+ INFO = 2
65
+ WARNING = 3
66
+ ERROR = 4
67
+ FATAL = 5
68
+ SILENT = 1000
69
+
70
+
71
+ class ActivationDataType(enum.IntEnum):
72
+ """Activation data type for inference."""
73
+
74
+ FLOAT32 = 0
75
+ FLOAT16 = 1
76
+ INT16 = 2
77
+ INT8 = 3
78
+
79
+ @classmethod
80
+ def from_str(cls, val: str) -> ActivationDataType | None:
81
+ mapping = {
82
+ "fp32": cls.FLOAT32,
83
+ "fp16": cls.FLOAT16,
84
+ "int16": cls.INT16,
85
+ "int8": cls.INT8,
86
+ }
87
+ return mapping.get(val.lower())
88
+
89
+
90
+ _LIB: ctypes.CDLL | None = None
91
+
92
+
93
+ def _get_lib() -> ctypes.CDLL:
94
+ """Loads and returns the LiteRT-LM C shared library."""
95
+ global _LIB
96
+ if _LIB is not None:
97
+ return _LIB
98
+
99
+ import sys
100
+ if sys.platform == "win32":
101
+ lib_name = "litert-lm.dll"
102
+ else:
103
+ extension = "dylib" if sys.platform == "darwin" else "so"
104
+ lib_name = f"liblitert-lm.{extension}"
105
+
106
+ # 1. Try loading using importlib.resources (handles .par and package files)
107
+ try:
108
+ ref = resources.files(__package__) / lib_name
109
+ with resources.as_file(ref) as path:
110
+ if path.exists():
111
+ _LIB = ctypes.CDLL(str(path))
112
+ except (ImportError, FileNotFoundError, TypeError):
113
+ pass
114
+
115
+ # 2. Fallback to direct path in runfiles for local development/Bazel
116
+ if _LIB is None:
117
+ path = os.path.join(os.path.dirname(__file__), "../../c", lib_name)
118
+ if os.path.exists(path):
119
+ _LIB = ctypes.CDLL(path)
120
+
121
+ if _LIB is None:
122
+ raise RuntimeError(
123
+ f"Could not find {lib_name}. Ensure it is built and included in the"
124
+ " package or runfiles."
125
+ )
126
+
127
+ _setup_lib_signatures(_LIB)
128
+ return _LIB
129
+
130
+
131
+ def _setup_lib_signatures(lib):
132
+ """Configures the argument and return types for C API functions."""
133
+ # Log level
134
+ lib.litert_lm_set_min_log_level.argtypes = [ctypes.c_int]
135
+
136
+ # Input Data
137
+ lib.litert_lm_input_data_create.restype = ctypes.c_void_p
138
+ lib.litert_lm_input_data_create.argtypes = [
139
+ ctypes.c_int,
140
+ ctypes.c_void_p,
141
+ ctypes.c_size_t,
142
+ ]
143
+ lib.litert_lm_input_data_delete.restype = None
144
+ lib.litert_lm_input_data_delete.argtypes = [ctypes.c_void_p]
145
+
146
+ # Engine Settings
147
+ lib.litert_lm_engine_settings_create.restype = ctypes.c_void_p
148
+ lib.litert_lm_engine_settings_create.argtypes = [
149
+ c_string_p,
150
+ c_string_p,
151
+ c_string_p,
152
+ c_string_p,
153
+ ]
154
+ lib.litert_lm_engine_settings_delete.argtypes = [ctypes.c_void_p]
155
+ lib.litert_lm_engine_settings_set_max_num_tokens.argtypes = [
156
+ ctypes.c_void_p,
157
+ ctypes.c_int,
158
+ ]
159
+ lib.litert_lm_engine_settings_set_max_num_images.argtypes = [
160
+ ctypes.c_void_p,
161
+ ctypes.c_int,
162
+ ]
163
+ lib.litert_lm_engine_settings_set_num_threads.argtypes = [
164
+ ctypes.c_void_p,
165
+ ctypes.c_int,
166
+ ]
167
+ lib.litert_lm_engine_settings_set_audio_num_threads.argtypes = [
168
+ ctypes.c_void_p,
169
+ ctypes.c_int,
170
+ ]
171
+ lib.litert_lm_engine_settings_set_cache_dir.argtypes = [
172
+ ctypes.c_void_p,
173
+ c_string_p,
174
+ ]
175
+ lib.litert_lm_engine_settings_set_litert_dispatch_lib_dir.argtypes = [
176
+ ctypes.c_void_p,
177
+ c_string_p,
178
+ ]
179
+ lib.litert_lm_engine_settings_set_enable_speculative_decoding.argtypes = [
180
+ ctypes.c_void_p,
181
+ ctypes.c_bool,
182
+ ]
183
+ lib.litert_lm_engine_settings_set_activation_data_type.argtypes = [
184
+ ctypes.c_void_p,
185
+ ctypes.c_int,
186
+ ]
187
+ lib.litert_lm_engine_settings_set_lora_rank.argtypes = [
188
+ ctypes.c_void_p,
189
+ ctypes.c_int,
190
+ ]
191
+ lib.litert_lm_engine_settings_set_supported_lora_ranks.restype = ctypes.c_int
192
+ lib.litert_lm_engine_settings_set_supported_lora_ranks.argtypes = [
193
+ ctypes.c_void_p,
194
+ ctypes.POINTER(ctypes.c_int),
195
+ ctypes.c_size_t,
196
+ ]
197
+ lib.litert_lm_engine_settings_set_audio_lora_rank.argtypes = [
198
+ ctypes.c_void_p,
199
+ ctypes.c_int,
200
+ ]
201
+ lib.litert_lm_engine_settings_set_supported_audio_lora_ranks.restype = (
202
+ ctypes.c_int
203
+ )
204
+ lib.litert_lm_engine_settings_set_supported_audio_lora_ranks.argtypes = [
205
+ ctypes.c_void_p,
206
+ ctypes.POINTER(ctypes.c_int),
207
+ ctypes.c_size_t,
208
+ ]
209
+ lib.litert_lm_engine_settings_enable_benchmark.argtypes = [ctypes.c_void_p]
210
+ lib.litert_lm_engine_settings_set_num_prefill_tokens.argtypes = [
211
+ ctypes.c_void_p,
212
+ ctypes.c_int,
213
+ ]
214
+ lib.litert_lm_engine_settings_set_num_decode_tokens.argtypes = [
215
+ ctypes.c_void_p,
216
+ ctypes.c_int,
217
+ ]
218
+
219
+ # Engine
220
+ lib.litert_lm_engine_create.restype = ctypes.c_void_p
221
+ lib.litert_lm_engine_create.argtypes = [ctypes.c_void_p]
222
+ lib.litert_lm_engine_delete.argtypes = [ctypes.c_void_p]
223
+
224
+ # Sampler Params
225
+ lib.litert_lm_sampler_params_create.restype = ctypes.c_void_p
226
+ lib.litert_lm_sampler_params_create.argtypes = [ctypes.c_int]
227
+ lib.litert_lm_sampler_params_delete.argtypes = [ctypes.c_void_p]
228
+ lib.litert_lm_sampler_params_set_top_k.argtypes = [
229
+ ctypes.c_void_p,
230
+ ctypes.c_int,
231
+ ]
232
+ lib.litert_lm_sampler_params_set_top_p.argtypes = [
233
+ ctypes.c_void_p,
234
+ ctypes.c_float,
235
+ ]
236
+ lib.litert_lm_sampler_params_set_temperature.argtypes = [
237
+ ctypes.c_void_p,
238
+ ctypes.c_float,
239
+ ]
240
+ lib.litert_lm_sampler_params_set_seed.argtypes = [
241
+ ctypes.c_void_p,
242
+ ctypes.c_int,
243
+ ]
244
+
245
+ # Session Config
246
+ lib.litert_lm_session_config_create.restype = ctypes.c_void_p
247
+ lib.litert_lm_session_config_create.argtypes = []
248
+ lib.litert_lm_session_config_delete.argtypes = [ctypes.c_void_p]
249
+ lib.litert_lm_session_config_set_max_output_tokens.argtypes = [
250
+ ctypes.c_void_p,
251
+ ctypes.c_int,
252
+ ]
253
+ lib.litert_lm_session_config_set_apply_prompt_template.argtypes = [
254
+ ctypes.c_void_p,
255
+ ctypes.c_bool,
256
+ ]
257
+ lib.litert_lm_session_config_set_sampler_params.argtypes = [
258
+ ctypes.c_void_p,
259
+ ctypes.c_void_p,
260
+ ]
261
+ lib.litert_lm_session_config_set_lora_path.restype = ctypes.c_int
262
+ lib.litert_lm_session_config_set_lora_path.argtypes = [
263
+ ctypes.c_void_p,
264
+ c_string_p,
265
+ ]
266
+ lib.litert_lm_session_config_set_audio_lora_path.restype = ctypes.c_int
267
+ lib.litert_lm_session_config_set_audio_lora_path.argtypes = [
268
+ ctypes.c_void_p,
269
+ c_string_p,
270
+ ]
271
+
272
+ # Session
273
+ lib.litert_lm_engine_create_session.restype = ctypes.c_void_p
274
+ lib.litert_lm_engine_create_session.argtypes = [
275
+ ctypes.c_void_p,
276
+ ctypes.c_void_p,
277
+ ]
278
+ lib.litert_lm_session_delete.argtypes = [ctypes.c_void_p]
279
+ lib.litert_lm_session_cancel_process.argtypes = [ctypes.c_void_p]
280
+ lib.litert_lm_session_run_prefill.restype = ctypes.c_int
281
+ lib.litert_lm_session_run_prefill.argtypes = [
282
+ ctypes.c_void_p,
283
+ ctypes.POINTER(ctypes.c_void_p),
284
+ ctypes.c_size_t,
285
+ ]
286
+ lib.litert_lm_session_run_decode.restype = ctypes.c_void_p
287
+ lib.litert_lm_session_run_decode.argtypes = [ctypes.c_void_p]
288
+ lib.litert_lm_session_run_decode_async.restype = ctypes.c_int
289
+ lib.litert_lm_session_run_decode_async.argtypes = [
290
+ ctypes.c_void_p,
291
+ STREAM_CALLBACK_TYPE,
292
+ ctypes.c_void_p,
293
+ ]
294
+ lib.litert_lm_session_run_text_scoring.restype = ctypes.c_void_p
295
+ lib.litert_lm_session_run_text_scoring.argtypes = [
296
+ ctypes.c_void_p,
297
+ ctypes.POINTER(ctypes.c_char_p),
298
+ ctypes.c_size_t,
299
+ ctypes.c_bool,
300
+ ]
301
+ lib.litert_lm_session_generate_content.restype = ctypes.c_void_p
302
+ lib.litert_lm_session_generate_content.argtypes = [
303
+ ctypes.c_void_p,
304
+ ctypes.POINTER(ctypes.c_void_p),
305
+ ctypes.c_size_t,
306
+ ]
307
+ lib.litert_lm_session_generate_content_stream.restype = ctypes.c_int
308
+ lib.litert_lm_session_generate_content_stream.argtypes = [
309
+ ctypes.c_void_p,
310
+ ctypes.POINTER(ctypes.c_void_p),
311
+ ctypes.c_size_t,
312
+ STREAM_CALLBACK_TYPE,
313
+ ctypes.c_void_p,
314
+ ]
315
+
316
+ # Conversation Config
317
+ lib.litert_lm_conversation_config_create.restype = ctypes.c_void_p
318
+ lib.litert_lm_conversation_config_create.argtypes = []
319
+ lib.litert_lm_conversation_config_delete.argtypes = [ctypes.c_void_p]
320
+ lib.litert_lm_conversation_config_set_session_config.argtypes = [
321
+ ctypes.c_void_p,
322
+ ctypes.c_void_p,
323
+ ]
324
+ lib.litert_lm_conversation_config_set_system_message.argtypes = [
325
+ ctypes.c_void_p,
326
+ c_string_p,
327
+ ]
328
+ lib.litert_lm_conversation_config_set_tools.argtypes = [
329
+ ctypes.c_void_p,
330
+ c_string_p,
331
+ ]
332
+ lib.litert_lm_conversation_config_set_messages.argtypes = [
333
+ ctypes.c_void_p,
334
+ c_string_p,
335
+ ]
336
+ lib.litert_lm_conversation_config_set_extra_context.argtypes = [
337
+ ctypes.c_void_p,
338
+ c_string_p,
339
+ ]
340
+ lib.litert_lm_conversation_config_set_enable_constrained_decoding.argtypes = [
341
+ ctypes.c_void_p,
342
+ ctypes.c_bool,
343
+ ]
344
+ lib.litert_lm_conversation_config_set_filter_channel_content_from_kv_cache.argtypes = [
345
+ ctypes.c_void_p,
346
+ ctypes.c_bool,
347
+ ]
348
+
349
+ # Conversation Optional Args
350
+ lib.litert_lm_conversation_optional_args_create.restype = ctypes.c_void_p
351
+ lib.litert_lm_conversation_optional_args_create.argtypes = []
352
+ lib.litert_lm_conversation_optional_args_delete.argtypes = [ctypes.c_void_p]
353
+ lib.litert_lm_conversation_optional_args_set_max_output_tokens.argtypes = [
354
+ ctypes.c_void_p,
355
+ ctypes.c_int,
356
+ ]
357
+
358
+ # Conversation
359
+ lib.litert_lm_conversation_create.restype = ctypes.c_void_p
360
+ lib.litert_lm_conversation_create.argtypes = [
361
+ ctypes.c_void_p,
362
+ ctypes.c_void_p,
363
+ ]
364
+ lib.litert_lm_conversation_delete.argtypes = [ctypes.c_void_p]
365
+ lib.litert_lm_conversation_send_message.restype = ctypes.c_void_p
366
+ lib.litert_lm_conversation_send_message.argtypes = [
367
+ ctypes.c_void_p,
368
+ c_string_p,
369
+ c_string_p,
370
+ ctypes.c_void_p,
371
+ ]
372
+ lib.litert_lm_conversation_send_message_stream.restype = ctypes.c_int
373
+ lib.litert_lm_conversation_send_message_stream.argtypes = [
374
+ ctypes.c_void_p,
375
+ c_string_p,
376
+ c_string_p,
377
+ ctypes.c_void_p,
378
+ STREAM_CALLBACK_TYPE,
379
+ ctypes.c_void_p,
380
+ ]
381
+ lib.litert_lm_conversation_cancel_process.argtypes = [ctypes.c_void_p]
382
+ lib.litert_lm_conversation_render_message_to_string.restype = ctypes.c_char_p
383
+ lib.litert_lm_conversation_render_message_to_string.argtypes = [
384
+ ctypes.c_void_p,
385
+ c_string_p,
386
+ ]
387
+ lib.litert_lm_conversation_get_token_count.restype = ctypes.c_int
388
+ lib.litert_lm_conversation_get_token_count.argtypes = [ctypes.c_void_p]
389
+
390
+ # interfaces.Responses
391
+ lib.litert_lm_responses_delete.argtypes = [ctypes.c_void_p]
392
+ lib.litert_lm_responses_get_num_candidates.restype = ctypes.c_int
393
+ lib.litert_lm_responses_get_num_candidates.argtypes = [ctypes.c_void_p]
394
+ lib.litert_lm_responses_get_response_text_at.restype = ctypes.c_char_p
395
+ lib.litert_lm_responses_get_response_text_at.argtypes = [
396
+ ctypes.c_void_p,
397
+ ctypes.c_int,
398
+ ]
399
+ lib.litert_lm_responses_has_score_at.restype = ctypes.c_bool
400
+ lib.litert_lm_responses_has_score_at.argtypes = [
401
+ ctypes.c_void_p,
402
+ ctypes.c_int,
403
+ ]
404
+ lib.litert_lm_responses_get_score_at.restype = ctypes.c_float
405
+ lib.litert_lm_responses_get_score_at.argtypes = [
406
+ ctypes.c_void_p,
407
+ ctypes.c_int,
408
+ ]
409
+ lib.litert_lm_responses_has_token_length_at.restype = ctypes.c_bool
410
+ lib.litert_lm_responses_has_token_length_at.argtypes = [
411
+ ctypes.c_void_p,
412
+ ctypes.c_int,
413
+ ]
414
+ lib.litert_lm_responses_get_token_length_at.restype = ctypes.c_int
415
+ lib.litert_lm_responses_get_token_length_at.argtypes = [
416
+ ctypes.c_void_p,
417
+ ctypes.c_int,
418
+ ]
419
+ lib.litert_lm_responses_has_token_scores_at.restype = ctypes.c_bool
420
+ lib.litert_lm_responses_has_token_scores_at.argtypes = [
421
+ ctypes.c_void_p,
422
+ ctypes.c_int,
423
+ ]
424
+ lib.litert_lm_responses_get_num_token_scores_at.restype = ctypes.c_int
425
+ lib.litert_lm_responses_get_num_token_scores_at.argtypes = [
426
+ ctypes.c_void_p,
427
+ ctypes.c_int,
428
+ ]
429
+ lib.litert_lm_responses_get_token_scores_at.restype = ctypes.POINTER(
430
+ ctypes.c_float
431
+ )
432
+ lib.litert_lm_responses_get_token_scores_at.argtypes = [
433
+ ctypes.c_void_p,
434
+ ctypes.c_int,
435
+ ]
436
+
437
+ # JSON Response
438
+ lib.litert_lm_json_response_delete.argtypes = [ctypes.c_void_p]
439
+ lib.litert_lm_json_response_get_string.restype = ctypes.c_char_p
440
+ lib.litert_lm_json_response_get_string.argtypes = [ctypes.c_void_p]
441
+
442
+ # Benchmark Info
443
+ lib.litert_lm_session_get_benchmark_info.restype = ctypes.c_void_p
444
+ lib.litert_lm_session_get_benchmark_info.argtypes = [ctypes.c_void_p]
445
+ lib.litert_lm_conversation_get_benchmark_info.restype = ctypes.c_void_p
446
+ lib.litert_lm_conversation_get_benchmark_info.argtypes = [ctypes.c_void_p]
447
+ lib.litert_lm_benchmark_info_delete.argtypes = [ctypes.c_void_p]
448
+ lib.litert_lm_benchmark_info_get_time_to_first_token.restype = ctypes.c_double
449
+ lib.litert_lm_benchmark_info_get_time_to_first_token.argtypes = [
450
+ ctypes.c_void_p
451
+ ]
452
+ lib.litert_lm_benchmark_info_get_total_init_time_in_second.restype = (
453
+ ctypes.c_double
454
+ )
455
+ lib.litert_lm_benchmark_info_get_total_init_time_in_second.argtypes = [
456
+ ctypes.c_void_p
457
+ ]
458
+ lib.litert_lm_benchmark_info_get_num_prefill_turns.restype = ctypes.c_int
459
+ lib.litert_lm_benchmark_info_get_num_prefill_turns.argtypes = [
460
+ ctypes.c_void_p
461
+ ]
462
+ lib.litert_lm_benchmark_info_get_num_decode_turns.restype = ctypes.c_int
463
+ lib.litert_lm_benchmark_info_get_num_decode_turns.argtypes = [ctypes.c_void_p]
464
+ lib.litert_lm_benchmark_info_get_prefill_token_count_at.restype = ctypes.c_int
465
+ lib.litert_lm_benchmark_info_get_prefill_token_count_at.argtypes = [
466
+ ctypes.c_void_p,
467
+ ctypes.c_int,
468
+ ]
469
+ lib.litert_lm_benchmark_info_get_decode_token_count_at.restype = ctypes.c_int
470
+ lib.litert_lm_benchmark_info_get_decode_token_count_at.argtypes = [
471
+ ctypes.c_void_p,
472
+ ctypes.c_int,
473
+ ]
474
+ lib.litert_lm_benchmark_info_get_prefill_tokens_per_sec_at.restype = (
475
+ ctypes.c_double
476
+ )
477
+ lib.litert_lm_benchmark_info_get_prefill_tokens_per_sec_at.argtypes = [
478
+ ctypes.c_void_p,
479
+ ctypes.c_int,
480
+ ]
481
+ lib.litert_lm_benchmark_info_get_decode_tokens_per_sec_at.restype = (
482
+ ctypes.c_double
483
+ )
484
+ lib.litert_lm_benchmark_info_get_decode_tokens_per_sec_at.argtypes = [
485
+ ctypes.c_void_p,
486
+ ctypes.c_int,
487
+ ]
488
+
489
+ # Tokenizer
490
+ lib.litert_lm_engine_tokenize.restype = ctypes.c_void_p
491
+ lib.litert_lm_engine_tokenize.argtypes = [ctypes.c_void_p, c_string_p]
492
+ lib.litert_lm_tokenize_result_delete.argtypes = [ctypes.c_void_p]
493
+ lib.litert_lm_tokenize_result_get_tokens.restype = ctypes.POINTER(
494
+ ctypes.c_int
495
+ )
496
+ lib.litert_lm_tokenize_result_get_tokens.argtypes = [ctypes.c_void_p]
497
+ lib.litert_lm_tokenize_result_get_num_tokens.restype = ctypes.c_size_t
498
+ lib.litert_lm_tokenize_result_get_num_tokens.argtypes = [ctypes.c_void_p]
499
+
500
+ lib.litert_lm_engine_detokenize.restype = ctypes.c_void_p
501
+ lib.litert_lm_engine_detokenize.argtypes = [
502
+ ctypes.c_void_p,
503
+ ctypes.POINTER(ctypes.c_int),
504
+ ctypes.c_size_t,
505
+ ]
506
+ lib.litert_lm_detokenize_result_delete.argtypes = [ctypes.c_void_p]
507
+ lib.litert_lm_detokenize_result_get_string.restype = ctypes.c_char_p
508
+ lib.litert_lm_detokenize_result_get_string.argtypes = [ctypes.c_void_p]
509
+
510
+ # Token Union / Metadata
511
+ lib.litert_lm_token_union_delete.argtypes = [ctypes.c_void_p]
512
+ lib.litert_lm_token_union_get_type.restype = ctypes.c_int
513
+ lib.litert_lm_token_union_get_type.argtypes = [ctypes.c_void_p]
514
+ lib.litert_lm_token_union_get_string.restype = ctypes.c_char_p
515
+ lib.litert_lm_token_union_get_string.argtypes = [ctypes.c_void_p]
516
+ lib.litert_lm_token_union_get_ids.restype = ctypes.c_int
517
+ lib.litert_lm_token_union_get_ids.argtypes = [
518
+ ctypes.c_void_p,
519
+ ctypes.POINTER(ctypes.POINTER(ctypes.c_int)),
520
+ ctypes.POINTER(ctypes.c_size_t),
521
+ ]
522
+
523
+ lib.litert_lm_token_unions_delete.argtypes = [ctypes.c_void_p]
524
+ lib.litert_lm_token_unions_get_num_tokens.restype = ctypes.c_size_t
525
+ lib.litert_lm_token_unions_get_num_tokens.argtypes = [ctypes.c_void_p]
526
+ lib.litert_lm_token_unions_get_token_at.restype = ctypes.c_void_p
527
+ lib.litert_lm_token_unions_get_token_at.argtypes = [
528
+ ctypes.c_void_p,
529
+ ctypes.c_size_t,
530
+ ]
531
+
532
+ lib.litert_lm_engine_get_start_token.restype = ctypes.c_void_p
533
+ lib.litert_lm_engine_get_start_token.argtypes = [ctypes.c_void_p]
534
+ lib.litert_lm_engine_get_stop_tokens.restype = ctypes.c_void_p
535
+ lib.litert_lm_engine_get_stop_tokens.argtypes = [ctypes.c_void_p]
536
+
537
+
538
+ def set_min_log_severity(severity: LogSeverity):
539
+ """Sets the minimum logging severity for the C library."""
540
+ _get_lib().litert_lm_set_min_log_level(int(severity))