Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions python/tvm/relax/frontend/tflite/tflite_flexbuffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,15 @@ def decode_map(self, end, byte_width, parent_byte_width):
size_fmt = {1: "<b", 2: "<h", 4: "<i", 8: "<q"}[byte_width]
map_size = struct.unpack(size_fmt, self.buffer[mid_loc - byte_width : mid_loc])[0]

# Find keys
# Find keys. The map header also stores the byte width of the keys vector, which is
# wider than one byte once a key string sits more than 255 bytes before it.
keys_offset = mid_loc - byte_width * 3
keys_end = self.indirect_jump(keys_offset, byte_width)
keys = self.decode_keys(keys_end, map_size, 1)
keys_byte_width_fmt = {1: "<B", 2: "<H", 4: "<I", 8: "<Q"}[byte_width]
keys_byte_width = struct.unpack(
keys_byte_width_fmt, self.buffer[mid_loc - byte_width * 2 : mid_loc - byte_width]
)[0]
keys = self.decode_keys(keys_end, map_size, keys_byte_width)

# Find values
values_end = self.indirect_jump(end, parent_byte_width)
Expand Down
17 changes: 17 additions & 0 deletions tests/python/relax/test_frontend_tflite.py
Original file line number Diff line number Diff line change
Expand Up @@ -3741,6 +3741,23 @@ def test_detection_postprocess_rejects_invalid_class_count():
_build_detection_postprocess_mod(num_classes=2, input_num_classes=5)


@pytest.mark.parametrize(
"custom_options",
[
pytest.param({f"option_{i}_" + "x" * 40: i for i in range(8)}, id="long_keys"),
pytest.param({f"key_{i:03d}": i for i in range(40)}, id="many_keys"),
],
)
def test_flexbuffer_decoder_wide_key_vector(custom_options):
# Over 255 bytes of key strings make the keys vector use 2-byte offsets.
from flatbuffers import flexbuffers

from tvm.relax.frontend.tflite.tflite_flexbuffer import FlexBufferDecoder

flexbuffer = bytes(flexbuffers.Dumps(custom_options))
assert FlexBufferDecoder(flexbuffer).decode() == custom_options


def _make_resize_expected(
input_shape, output_size, method, coordinate_transformation_mode, rounding_method
):
Expand Down