Skip to content

Commit 190652e

Browse files
committed
[Relax][Frontend][TFLite] Fix DENSIFY tests for CI compatibility
This commit fixes two issues in the DENSIFY test suite that caused failures in TVM's CI environment: 1. Remove dependency on tfl.Int32VectorStartValuesVector - This flatbuffers helper is absent in older tflite package versions used in CI Docker images. - _tflite_int32_table now builds the int32 vector manually using builder.StartVector(4, len(values), 4). 2. Fix flatbuffers IsNestedError in _build_buffer - The original implementation called _tflite_byte_vector (which invokes builder.StartVector) after tfl.BufferStart, violating flatbuffers' no-nesting rule. - Fixed by creating the data vector before entering the Buffer table. Also simplifies convert_fully_connected to use get_tensor_expr directly, as suggested by gemini-code-assist, since the prefetched node handling is already encapsulated there.
1 parent 6155997 commit 190652e

2 files changed

Lines changed: 39 additions & 12 deletions

File tree

python/tvm/relax/frontend/tflite/tflite_frontend.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,6 @@ def convert_op_to_relax(self):
337337
continue
338338

339339
ret = self.bb.normalize(ret)
340-
# print("Op Code:", op_code_str, " Shape:", ret.struct_info)
341340

342341
if len(output_tensors) == 1:
343342
tensor_idx = output_tensors[0].tensor_idx
@@ -1899,15 +1898,8 @@ def convert_fully_connected(self, op):
18991898
TensorType.UINT8,
19001899
TensorType.FLOAT32,
19011900
)
1902-
weight_tensor_type_str = self.get_tensor_type_str(weight_tensor_type)
19031901

1904-
if self.has_expr(weight_tensor.tensor_idx):
1905-
weight_expr = self.get_expr(weight_tensor.tensor_idx)
1906-
else:
1907-
weight_value = self.get_tensor_value_or_prefetched(weight_tensor)
1908-
weight_expr = self.exp_tab.new_const(
1909-
weight_value, dtype=weight_tensor_type_str, source_name=weight_tensor.tensor.Name()
1910-
)
1902+
weight_expr = self.get_tensor_expr(weight_tensor)
19111903
weight_shape = weight_expr.struct_info.shape
19121904
weight_expr = relax.op.permute_dims(weight_expr, [1, 0])
19131905

tests/python/relax/test_frontend_tflite.py

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2461,6 +2461,30 @@ def main(
24612461
# Since TensorFlow does not provide an API to create sparse TFLite models,
24622462
# we manually build them using the flatbuffers API.
24632463

2464+
# Compatibility shim: schema-generated tflite packages (as used in CI) do not
2465+
# re-export builder helpers at the package top-level. Bind them from submodules
2466+
# so that the rest of the test file can use tfl.XXXStart / tfl.XXXEnd uniformly.
2467+
if not hasattr(tfl, "Int32VectorStart"):
2468+
_tflite_helper_modules = [
2469+
"AddOptions",
2470+
"Buffer",
2471+
"Conv2DOptions",
2472+
"DimensionMetadata",
2473+
"FullyConnectedOptions",
2474+
"Int32Vector",
2475+
"Model",
2476+
"Operator",
2477+
"OperatorCode",
2478+
"SparsityParameters",
2479+
"SubGraph",
2480+
"Tensor",
2481+
]
2482+
for _mod_name in _tflite_helper_modules:
2483+
_mod = __import__(f"tflite.{_mod_name}", fromlist=[_mod_name])
2484+
for _attr_name in dir(_mod):
2485+
if not _attr_name.startswith("_"):
2486+
setattr(tfl, _attr_name, getattr(_mod, _attr_name))
2487+
24642488
_DENSIFY_TEST_VALUES = np.array([1.0, 2.0], dtype=np.float32)
24652489
_DENSIFY_TEST_DENSE = np.array([[1.0, 0.0], [0.0, 2.0]], dtype=np.float32)
24662490
_DENSIFY_ROW_PTRS = [0, 1, 2]
@@ -2494,7 +2518,13 @@ def _tflite_byte_vector(builder, data):
24942518

24952519

24962520
def _tflite_int32_table(builder, values):
2497-
values_vec = _tflite_int32_vector(builder, tfl.Int32VectorStartValuesVector, values)
2521+
# Build the values vector directly without relying on version-specific
2522+
# helper tfl.Int32VectorStartValuesVector, which is absent in older
2523+
# tflite package versions used in CI.
2524+
builder.StartVector(4, len(values), 4)
2525+
for value in reversed(values):
2526+
builder.PrependInt32(value)
2527+
values_vec = builder.EndVector()
24982528
tfl.Int32VectorStart(builder)
24992529
tfl.Int32VectorAddValues(builder, values_vec)
25002530
return tfl.Int32VectorEnd(builder)
@@ -2519,9 +2549,14 @@ def _build_tensor(builder, buffer_idx, shape, sparsity=None):
25192549

25202550

25212551
def _build_buffer(builder, data=None):
2522-
tfl.BufferStart(builder)
2552+
# Build the data vector before starting the Buffer table to avoid
2553+
# flatbuffers IsNestedError (vectors cannot be created inside tables).
2554+
data_offset = None
25232555
if data is not None:
2524-
tfl.BufferAddData(builder, _tflite_byte_vector(builder, data))
2556+
data_offset = _tflite_byte_vector(builder, data)
2557+
tfl.BufferStart(builder)
2558+
if data_offset is not None:
2559+
tfl.BufferAddData(builder, data_offset)
25252560
return tfl.BufferEnd(builder)
25262561

25272562

0 commit comments

Comments
 (0)