File size: 12,114 Bytes
5f923cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright 2025 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.

import io
import os
import pathlib
import subprocess

from absl.testing import absltest

from litert_lm.runtime.proto import llm_metadata_pb2
from litert_lm.schema.py import litertlm_core
from litert_lm.schema.py import litertlm_peek

from python import runfiles

_TOML_TEMPLATE = """
# A template for testing the TOML parser.

[system_metadata]
entries = [
  { key = "author", value_type = "String", value = "The ODML Authors" }
]

[[section]]
# Section 0: LlmMetadataProto
section_type = "LlmMetadata"
data_path = "{LLM_METADATA_PATH}"

[[section]]
# Section 1: HF_Tokenizer
section_type = "HF_Tokenizer"
data_path = "{HF_TOKENIZER_PATH}"

[[section]]
# Section 2: TFLiteModel (Embedder)
section_type = "TFLiteModel"
model_type = "EMBEDDER"
data_path = "{EMBEDDER_PATH}"

[[section]]
# Section 3: TFLiteModel (Prefill/Decode)
section_type = "TFLiteModel"
model_type = "PREFILL_DECODE"
backend_constraint = "GPU"
data_path = "{PREFILL_DECODE_PATH}"
additional_metadata = [
  { key = "License", value_type = "String", value = "Example" }
]
"""


class LiteRTLMBuilderCLITest(absltest.TestCase):

  def setUp(self):
    super().setUp()
    self.temp_dir = self.create_tempdir().full_path

  def _create_placeholder_file(self, filename: str, content: bytes) -> str:
    filepath = os.path.join(self.temp_dir, filename)
    with litertlm_core.open_file(filepath, "wb") as f:
      f.write(content)
    return filepath

  def _get_command_path(self) -> str:
    """Returns the path to the command binary."""
    r = runfiles.Create()
    return r.Rlocation(
        os.path.join(
            os.path.dirname(os.path.abspath(__file__)), "litertlm_builder_cli"
        )
    )

  def _run_command(self, *args) -> str:
    """Runs the command with the given arguments."""
    output_path = os.path.join(self.temp_dir, "litertlm.litertlm")
    command = [
        self._get_command_path(),
        *args,
        "output",
        "--path",
        output_path,
    ]
    try:
      subprocess.run(command, check=True, capture_output=True)
    except subprocess.CalledProcessError as e:
      print("command stdout:\n", e.stdout.decode("utf-8"))
      print("command stderr:\n", e.stderr.decode("utf-8"))
      raise e
    return output_path

  def _peek_litertlm_file(self, path: str) -> str:
    """Peeks the litertlm file and returns the string representation."""
    stream = io.StringIO()
    litertlm_peek.peek_litertlm_file(path, self.temp_dir, stream)
    return stream.getvalue()

  def test_system_metadata(self):
    """Tests that system metadata can be added correctly."""
    args = ["system_metadata", "--str", "key1", "value1"]
    output_path = self._run_command(*args)
    self.assertTrue(os.path.exists(output_path))
    ss = self._peek_litertlm_file(output_path)
    self.assertIn("Key: key1, Value (String): value1", ss)
    self.assertIn("Sections (0)", ss)

  def test_llm_metadata(self):
    """Tests that LLM metadata can be added from a binary proto file."""
    llm_metadata = llm_metadata_pb2.LlmMetadata(max_num_tokens=123)
    bin_proto = llm_metadata.SerializeToString()
    metadata_path = self._create_placeholder_file("llm.pb", bin_proto)
    args = [
        "system_metadata",
        "--int",
        "my_key",
        "23",
        "llm_metadata",
        "--path",
        metadata_path,
    ]
    output_path = self._run_command(*args)
    self.assertTrue(os.path.exists(output_path))
    ss = self._peek_litertlm_file(output_path)
    self.assertIn("max_num_tokens: 123", ss)
    self.assertIn("Sections (1)", ss)

  def test_tflite_model(self):
    """Tests that a TFLite model can be added correctly."""
    tflite_path = self._create_placeholder_file(
        "model.tflite", b"dummy tflite content"
    )
    args = [
        "system_metadata",
        "--int",
        "my_key",
        "23",
        "tflite_model",
        "--path",
        tflite_path,
        "--model_type",
        "prefill_decode",
        "--str_metadata",
        "model_version",
        "1.0.1",
        "--backend_constraint",
        "CPU",
    ]
    output_path = self._run_command(*args)
    self.assertTrue(os.path.exists(output_path))
    ss = self._peek_litertlm_file(output_path)
    self.assertIn("Sections (1)", ss)
    self.assertIn("Data Type:    TFLiteModel", ss)
    self.assertIn("Key: model_type, Value (String): tf_lite_prefill_decode", ss)
    self.assertIn("Key: model_version, Value (String): 1.0.1", ss)
    self.assertIn("Key: backend_constraint, Value (String): cpu", ss)

  def test_tflite_weights(self):
    """Tests that TFLite weights can be added correctly via CLI."""
    tflite_path = self._create_placeholder_file(
        "model.weights", b"dummy tflite weights content"
    )
    args = [
        "system_metadata",
        "--int",
        "my_key",
        "23",
        "tflite_weights",
        "--path",
        tflite_path,
        "--model_type",
        "prefill_decode",
        "--str_metadata",
        "weights_version",
        "1.0.1",
    ]
    output_path = self._run_command(*args)
    self.assertTrue(os.path.exists(output_path))
    ss = self._peek_litertlm_file(output_path)
    self.assertIn("Sections (1)", ss)
    self.assertIn("Data Type:    TFLiteWeights", ss)
    self.assertIn("Key: model_type, Value (String): tf_lite_prefill_decode", ss)
    self.assertIn("Key: weights_version, Value (String): 1.0.1", ss)

  def test_sp_tokenizer(self):
    """Tests that a SentencePiece tokenizer can be added correctly."""
    sp_path = self._create_placeholder_file("sp.model", b"dummy sp content")
    args = [
        "system_metadata",
        "--int",
        "my_key",
        "23",
        "sp_tokenizer",
        "--path",
        sp_path,
        "--str_metadata",
        "tokenizer_version",
        "1.0.1",
    ]
    output_path = self._run_command(*args)
    self.assertTrue(os.path.exists(output_path))
    ss = self._peek_litertlm_file(output_path)
    self.assertIn("Sections (1)", ss)
    self.assertIn("Data Type:    SP_Tokenizer", ss)
    self.assertIn("Key: tokenizer_version, Value (String): 1.0.1", ss)

  def test_hf_tokenizer(self):
    """Tests that a HuggingFace tokenizer can be added correctly."""
    hf_path = self._create_placeholder_file(
        "tokenizer.json", b'{"version": "1.0"}'
    )
    args = [
        "system_metadata",
        "--int",
        "my_key",
        "23",
        "hf_tokenizer",
        "--path",
        hf_path,
        "--str_metadata",
        "tokenizer_version",
        "1.0.1",
    ]
    output_path = self._run_command(*args)
    self.assertTrue(os.path.exists(output_path))
    ss = self._peek_litertlm_file(output_path)
    self.assertIn("Sections (1)", ss)
    self.assertIn("Data Type:    HF_Tokenizer_Zlib", ss)
    self.assertIn("Key: tokenizer_version, Value (String): 1.0.1", ss)

  def test_end_to_end(self):
    """Tests a more complex end-to-end scenario with multiple sections."""
    sp_path = self._create_placeholder_file("sp.model", b"dummy sp content")
    tflite_path = self._create_placeholder_file(
        "model.tflite", b"dummy tflite content"
    )
    llm_metadata = llm_metadata_pb2.LlmMetadata(max_num_tokens=123)
    bin_proto = llm_metadata.SerializeToString()
    metadata_path = self._create_placeholder_file("llm.pb", bin_proto)

    args = [
        "system_metadata",
        "--str",
        "Authors",
        "ODML team",
        "sp_tokenizer",
        "--path",
        sp_path,
        "tflite_model",
        "--path",
        tflite_path,
        "--model_type",
        "embedder",
        "tflite_model",
        "--path",
        tflite_path,
        "--model_type",
        "prefill_decode",
        "--backend_constraint",
        "GPU",
        "llm_metadata",
        "--path",
        metadata_path,
    ]
    output_path = self._run_command(*args)
    self.assertTrue(os.path.exists(output_path))
    ss = self._peek_litertlm_file(output_path)
    self.assertIn("Sections (4)", ss)
    self.assertIn("Data Type:    SP_Tokenizer", ss)
    self.assertIn("Data Type:    TFLiteModel", ss)
    self.assertIn("Key: model_type, Value (String): tf_lite_embedder", ss)
    self.assertIn("Key: model_type, Value (String): tf_lite_prefill_decode", ss)
    self.assertIn("Key: backend_constraint, Value (String): gpu", ss)
    self.assertIn("Data Type:    LlmMetadataProto", ss)
    self.assertIn("max_num_tokens: 123", ss)

  def test_toml_file(self):
    """Tests that a TOML file can be added correctly."""
    hf_path = pathlib.Path(
        self._create_placeholder_file("tokenizer.json", b'{"version": "1.0"}')
    ).as_posix()
    tflite_path = pathlib.Path(
        self._create_placeholder_file("model.tflite", b"dummy tflite content")
    ).as_posix()
    llm_metadata = llm_metadata_pb2.LlmMetadata(max_num_tokens=123)
    bin_proto = llm_metadata.SerializeToString()
    metadata_path = pathlib.Path(
        self._create_placeholder_file("llm.pb", bin_proto)
    ).as_posix()
    toml_path = self._create_placeholder_file(
        "test.toml",
        _TOML_TEMPLATE.replace("{LLM_METADATA_PATH}", metadata_path)
        .replace("{HF_TOKENIZER_PATH}", hf_path)
        .replace("{EMBEDDER_PATH}", tflite_path)
        .replace("{PREFILL_DECODE_PATH}", tflite_path)
        .encode("utf-8"),
    )
    args = [
        "toml",
        "--path",
        toml_path,
    ]
    output_path = self._run_command(*args)
    self.assertTrue(os.path.exists(output_path))
    ss = self._peek_litertlm_file(output_path)
    self.assertIn("Sections (4)", ss)
    self.assertIn("Data Type:    HF_Tokenizer_Zlib", ss)
    self.assertIn("Data Type:    TFLiteModel", ss)
    self.assertIn("Key: model_type, Value (String): tf_lite_embedder", ss)
    self.assertIn("Key: model_type, Value (String): tf_lite_prefill_decode", ss)
    self.assertIn("Key: backend_constraint, Value (String): gpu", ss)
    self.assertIn("Data Type:    LlmMetadataProto", ss)
    self.assertIn("max_num_tokens: 123", ss)

  def test_toml_cannot_be_used_with_other_args(self):
    """Tests that a TOML file cannot be used with other args."""
    tflite_path = self._create_placeholder_file(
        "model.tflite", b"dummy tflite content"
    )
    toml_path = self._create_placeholder_file(
        "test.toml",
        _TOML_TEMPLATE.replace("{PREFILL_DECODE_PATH}", tflite_path).encode(
            "utf-8"
        ),
    )
    args = [
        "toml",
        "--path",
        toml_path,
        "system_metadata",
        "--str",
        "key1",
        "value1",
    ]
    with self.assertRaises(subprocess.CalledProcessError):
      self._run_command(*args)

  def test_help_root(self):
    """Tests that the help command prints the correct output."""
    command = [self._get_command_path(), "--help"]
    output = subprocess.run(command, check=True, capture_output=True)
    self.assertEqual(output.returncode, 0)
    stdout = output.stdout.decode("utf-8")
    self.assertIn(
        "Build a LiteRT-LM file from input files and metadata", stdout
    )

  def test_help_subcommand(self):
    """Tests that the help command prints the correct output for subcommand."""
    command = [self._get_command_path(), "system_metadata", "--help"]
    output = subprocess.run(command, check=True, capture_output=True)
    self.assertEqual(output.returncode, 0)
    stdout = output.stdout.decode("utf-8")
    self.assertIn(
        "Add one or more system metadata key-value pairs to the LiteRT-LM"
        " file.",
        stdout,
    )


if __name__ == "__main__":
  absltest.main()