"""Read the sections of a .litertlm bundle without the LiteRT-LM package, and keep them in a local cache. The file layout is the one LiteRT-LM reads (schema/core/litertlm_header_schema.fbs, litertlm_read.cc): bytes 0..7 b'LITERTLM' bytes 8..19 major, minor, patch version (uint32 little endian); major must be 1 bytes 20..23 padding bytes 24..31 header end offset (uint64) bytes 32..end a FlatBuffer LiteRTLMMetaData: section_metadata.objects[i] = (items, begin_offset, end_offset, data_type) The TFLite sections are copied out byte for byte; the HF tokenizer section (8-byte uncompressed size + zlib stream) is decompressed to tokenizer.json. The cache folder is named by the bundle's SHA-256 and is written once. """ import hashlib import json import os import struct import zlib from pathlib import Path ROOT = Path(__file__).resolve().parents[1] DEFAULT_CACHE = ROOT / '.cache/readout' DATA_TYPES = {0: 'NONE', 1: 'GenericBinaryData', 2: 'Deprecated', 3: 'TFLiteModel', 4: 'SP_Tokenizer', 5: 'LlmMetadataProto', 6: 'HF_Tokenizer_Zlib', 7: 'TFLiteWeights', 8: 'EmbeddingMetadataProto', 9: 'ExecutorMetadataProto'} STRING_VALUE = 9 # VData union index of StringValue def sha256(path): with open(path, 'rb') as stream: return hashlib.file_digest(stream, 'sha256').hexdigest() class _Table: """Minimal FlatBuffer table reader (little endian, offsets relative to the buffer start).""" def __init__(self, buf, pos): self.buf, self.pos = buf, pos vtable = pos - struct.unpack_from('= self.vt_len: return 0 return struct.unpack_from('