Skip to content

Commit 97c6eab

Browse files
committed
feat(compression): reject empty compression spec
An empty spec list passed to compress() previously returned an unmodified model silently. Fail early with a clear error instead, since an empty spec is almost certainly a mistake. BUG=part of #3256
1 parent c495ea1 commit 97c6eab

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

tensorflow/lite/micro/compression/compress.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ def compress(model_in: ByteString, specs: Iterable[spec.Tensor]) -> bytearray:
132132
Returns:
133133
A compressed flatbuffer with DECODE operators inserted.
134134
"""
135+
specs = list(specs)
136+
if not specs:
137+
raise compressor.CompressionError(
138+
"Compression spec is empty; no tensors to compress")
139+
135140
model = model_editor.read(model_in)
136141
compression_results: dict[tuple[int, int], compressor.CompressionResult] = {}
137142

tensorflow/lite/micro/compression/compress_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,11 @@ def test_ancillary_data_format(self):
313313
self.assertEqual(dcm_bytes[5] & 0x07, 4) # bitwidth = 4
314314
self.assertEqual(dcm_bytes[6], 4) # stride = num unique values
315315

316+
def test_empty_spec_raises(self):
317+
"""Empty compression spec is an error, not a silent no-op."""
318+
self.assertRaisesRegex(compressor.CompressionError, "empty",
319+
lambda: compress.compress(self.flatbuffer, []))
320+
316321
def test_smaller_bitwidth_raises(self):
317322
"""Specifying LUT compression with too small a bitwidth fails."""
318323
specs = [

0 commit comments

Comments
 (0)