OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[TFL FE] Disable SparsityInfo when sparsity metadata is incomplete (#35627)
Evgeny Kotovd7f6c4f41afb
diff --git a/src/frontends/tensorflow_lite/include/openvino/frontend/tensorflow_lite/sparsity_info.hpp b/src/frontends/tensorflow_lite/include/openvino/frontend/tensorflow_lite/sparsity_info.hppindex 84f072d9734..ffddfaeda4b 100644--- a/src/frontends/tensorflow_lite/include/openvino/frontend/tensorflow_lite/sparsity_info.hpp+++ b/src/frontends/tensorflow_lite/include/openvino/frontend/tensorflow_lite/sparsity_info.hpp@@ -105,11 +105,7 @@ public:void disable() {m_disabled = true;}- void enable() {- // We dont count on data_desc in case other data is absent- m_disabled = (m_shape.size() == 0 || m_traversal_order.size() == 0 || m_block_map.size() == 0 ||- m_dim_format.size() == 0);- }+ void enable();// Unpack sparse tensor and returns dense datavoid* dense_data() {if (m_disabled)diff --git a/src/frontends/tensorflow_lite/src/sparsity_info.cpp b/src/frontends/tensorflow_lite/src/sparsity_info.cppindex 5cd61e73f23..a7c1707106e 100644--- a/src/frontends/tensorflow_lite/src/sparsity_info.cpp+++ b/src/frontends/tensorflow_lite/src/sparsity_info.cpp@@ -12,6 +12,55 @@ bool ov::frontend::tensorflow_lite::SparsityInfo::is_copyable() const {return false;}+// Re-evaluates m_disabled against the actually-populated metadata. Disables+// the SparsityInfo whenever any precondition densify() depends on is missing+// or inconsistent, so that TensorLitePlace falls back to the raw constant+// buffer. The contract enforced here:+//+// 1. shape is non-empty;+// 2. traversal_order length is at least the tensor rank;+// 3. block_map size equals (traversal_order.size() - shape.size()), so for+// a standard CSR layout (traversal_order length == rank) block_map must+// be empty, and for a block-sparse layout it must list exactly the+// block dimensions appended after the rank-many leading dims (per+// schema.fbs:187-211);+// 4. dim_format size matches traversal_order size (DimensionMetadata is+// one entry per traversal-order position);+// 5. data_desc size matches dim_format size, and every SPARSE_CSR entry+// carries non-null array_segments + array_indices payloads (densify()+// dereferences these without a null check).+//+// Anything tighter (e.g. integer ranges in the segments/indices buffers)+// stays inside read_sparse_data() / densify(); that level of validation+// requires the actual flatbuffers vector type and is independent of the+// "is the metadata even shaped correctly" question handled here.+void ov::frontend::tensorflow_lite::SparsityInfo::enable() {+ if (m_shape.size() == 0 || m_traversal_order.size() < m_shape.size()) {+ m_disabled = true;+ return;+ }+ if (m_block_map.size() != m_traversal_order.size() - m_shape.size()) {+ m_disabled = true;+ return;+ }+ if (m_dim_format.size() != m_traversal_order.size()) {+ m_disabled = true;+ return;+ }+ if (m_data_desc.size() != m_dim_format.size()) {+ m_disabled = true;+ return;+ }+ for (size_t i = 0; i < m_dim_format.size(); ++i) {+ if (m_dim_format[i] == ::tflite::DimensionType_SPARSE_CSR &&+ (m_data_desc[i].segments == nullptr || m_data_desc[i].indices == nullptr)) {+ m_disabled = true;+ return;+ }+ }+ m_disabled = false;+}+// Overflow-safe multiplication for size calculations.// Throws on overflow to prevent heap buffer allocation with a truncated size,// which would lead to out-of-bounds writes during sparse data deserialization.diff --git a/src/frontends/tensorflow_lite/src/utils.cpp b/src/frontends/tensorflow_lite/src/utils.cppindex 05bc86faa0f..d3be291516c 100644--- a/src/frontends/tensorflow_lite/src/utils.cpp+++ b/src/frontends/tensorflow_lite/src/utils.cpp@@ -64,6 +64,7 @@ std::shared_ptr<ov::frontend::tensorflow_lite::SparsityInfo> ov::frontend::tensosparsity->set_dim_format(dim_format);sparsity->set_data_desc(data_desc);}+ sparsity->enable(); // Enable sparsity validation; disables if fields are incompletereturn sparsity;}diff --git a/src/frontends/tensorflow_lite/tests/convert_sparse_incomplete.cpp b/src/frontends/tensorflow_lite/tests/convert_sparse_incomplete.cppnew file mode 100644index 00000000000..262c3f3f925--- /dev/null+++ b/src/frontends/tensorflow_lite/tests/convert_sparse_incomplete.cpp@@ -0,0 +1,100 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "common_test_utils/ov_test_utils.hpp"+#include "common_test_utils/test_assertions.hpp"+#include "common_test_utils/test_case.hpp"+#include "common_test_utils/test_control.hpp"+#include "convert_model.hpp"+#include "tf_utils.hpp"+#include "utils.hpp"++using namespace std;+using namespace ov;+using namespace ov::frontend;++// End-to-end regression coverage for the get_sparsity() fix in+// src/frontends/tensorflow_lite/src/utils.cpp. Two test groups, matching the+// two outcomes that get_sparsity() / SparsityInfo::enable() must produce:+//+// * Fallback group (IncompleteSparsityLoadConvertTest) — the metadata is+// malformed (missing/empty traversal_order, dim_metadata, dim_format, or+// fully empty SparsityParameters). enable() must disable the tensor and+// TensorLitePlace must expose the raw constant buffer instead of trying+// to densify(). The unifying contract is that load() and convert() both+// succeed; without the fix dense_data() throws inside TensorLitePlace's+// ctor and convert() fails.+//+// * Densify group (IncompleteSparsityDensifyTest) — the standard-CSR+// corner case where block_map is legitimately absent. enable() must+// keep the tensor enabled and densify() must run. Pure load+convert is+// not enough here: a future regression that disabled the tensor would+// keep load+convert green by silently falling back to the raw buffer.+// The test asserts the densified all-zero constant by running inference+// on CPU and checking that ADD(input, const) == input.+class IncompleteSparsityLoadConvertTest : public ::testing::TestWithParam<std::string> {+protected:+ void SetUp() override {+ FrontEndManager fem;+ OV_ASSERT_NO_THROW(m_frontEnd = fem.load_by_framework(TF_LITE_FE));+ ASSERT_NE(m_frontEnd, nullptr);+ }+ FrontEnd::Ptr m_frontEnd;+};++TEST_P(IncompleteSparsityLoadConvertTest, load_and_convert_succeed) {+ auto model_filename = FrontEndTestUtils::make_model_path(string(TEST_TENSORFLOW_LITE_MODELS_DIRNAME) + GetParam());++ InputModel::Ptr inputModel;+ OV_ASSERT_NO_THROW(inputModel = m_frontEnd->load(model_filename));+ ASSERT_NE(inputModel, nullptr);++ shared_ptr<ov::Model> model;+ OV_ASSERT_NO_THROW(model = m_frontEnd->convert(inputModel));+ ASSERT_NE(model, nullptr);+ EXPECT_GT(model->get_ops().size(), 0u)+ << "Converted model must contain at least one op (Parameter + ADD + Result for the simple "+ "graphs, with a DEQUANTIZE inserted for the int8 variant).";+}++INSTANTIATE_TEST_SUITE_P(SparseIncomplete,+ IncompleteSparsityLoadConvertTest,+ ::testing::Values(+ // Same shape as the HandsLandmarkFull regression: traversal_order+ // and block_map populated, dim_metadata field omitted entirely.+ "sparse_incomplete/missing_dim_metadata.tflite",+ // dim_metadata present but vector length 0.+ "sparse_incomplete/empty_dim_metadata.tflite",+ // traversal_order omitted; block_map and dim_metadata present.+ "sparse_incomplete/missing_traversal_order.tflite",+ // SparsityParameters table referenced but all three fields omitted.+ "sparse_incomplete/empty_sparsity.tflite",+ // INT8 const with empty SparsityParameters consumed by DEQUANTIZE+ // then ADD - mirrors the original failure context (DEQUANTIZE op+ // on an incomplete-sparsity constant).+ "sparse_incomplete/dequantize_incomplete_sparsity.tflite"));++// missing_block_map.tflite carries a valid standard-CSR layout+// (traversal_order length == tensor rank, dim_metadata fully populated) and+// intentionally omits block_map. The generator emits segments=[0, 0, 0] and+// indices=[], which densifies the (2, 2) constant to all zeros, while the+// raw FLOAT32 buffer behind the same tensor is [1, 2, 3, 4]. The graph is+// ADD(input, const), so:+//+// * if densify() ran => output == input (asserted here)+// * if fallback fired => output == input + [1, 2, 3, 4] (would fail)+//+// Without that observable, a future change that disabled this tensor+// unconditionally and silently fell back to the raw buffer would keep+// load+convert green and the regression would slip past the suite.+static std::string s_manifest = "";++OPENVINO_TEST(IncompleteSparsityDensify, missing_block_map_runs_densify) {+ auto model = ov::frontend::tensorflow_lite::tests::convert_model("sparse_incomplete/missing_block_map.tflite");++ auto test_case = ov::test::TestCase(model, ov::test::utils::DEVICE_CPU);+ test_case.add_input<float>(Shape{2, 2}, {10, 20, 30, 40});+ test_case.add_expected_output<float>(Shape{2, 2}, {10, 20, 30, 40});+ test_case.run();+}diff --git a/src/frontends/tensorflow_lite/tests/sparsity_info_test.cpp b/src/frontends/tensorflow_lite/tests/sparsity_info_test.cppnew file mode 100644index 00000000000..7f9b3321bc3--- /dev/null+++ b/src/frontends/tensorflow_lite/tests/sparsity_info_test.cpp@@ -0,0 +1,237 @@+// Copyright (C) 2018-2026 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "openvino/frontend/tensorflow_lite/sparsity_info.hpp"++#include <gtest/gtest.h>++#include "openvino/core/type/element_type.hpp"++using ov::frontend::tensorflow_lite::SparsityInfo;++// Direct unit tests for SparsityInfo::enable() and the disabled-state+// contract used by TensorLitePlace and the TFLite frontend's get_sparsity()+// factory. See src/frontends/tensorflow_lite/src/utils.cpp.++namespace {++constexpr uint8_t kDummyValues[16] = {0};+constexpr uint8_t kSegmentsSentinel[1] = {0};+constexpr uint8_t kIndicesSentinel[1] = {0};++// Standard-CSR data_desc for a (DENSE, SPARSE_CSR) dim_format. The DENSE+// entry's segments/indices are unused (densify() never indexes them); the+// SPARSE_CSR entry must carry non-null payloads for enable() to admit it.+inline std::vector<SparsityInfo::SparsityDataDesc> make_dense_then_sparse_data_desc() {+ return {+ SparsityInfo::SparsityDataDesc{0, nullptr, 0, nullptr},+ SparsityInfo::SparsityDataDesc{0, kSegmentsSentinel, 0, kIndicesSentinel},+ };+}++} // namespace++// enable() must disable a SparsityInfo whose required fields are all empty.+// The end-to-end coverage that get_sparsity() actually invokes enable() is in+// convert_sparse_incomplete.cpp; this test pins down the enable() semantics+// in isolation.+TEST(SparsityInfoTest, EnableOnEmptyObjectDisables) {+ SparsityInfo s;+ s.enable();+ EXPECT_TRUE(s.is_disabled()) << "enable() with all required fields empty must disable.";+}++TEST(SparsityInfoTest, EnableDisablesWhenShapeEmpty) {+ SparsityInfo s;+ s.set_traversal_order({0, 1});+ s.set_block_map({0});+ s.set_dim_format({0, 1});+ // shape intentionally left empty+ s.enable();+ EXPECT_TRUE(s.is_disabled());+}++TEST(SparsityInfoTest, EnableDisablesWhenTraversalOrderEmpty) {+ SparsityInfo s;+ s.set_shape({2, 2});+ s.set_block_map({0});+ s.set_dim_format({0, 1});+ s.enable();+ EXPECT_TRUE(s.is_disabled());+}++TEST(SparsityInfoTest, EnableDoesNotDisableWhenBlockMapEmpty) {+ SparsityInfo s;+ s.set_shape({2, 2});+ s.set_traversal_order({0, 1});+ s.set_dim_format({0, 1});+ s.set_data_desc(make_dense_then_sparse_data_desc());+ // block_map is intentionally absent — valid for standard CSR tensors+ s.enable();+ EXPECT_FALSE(s.is_disabled());+}++// Block-sparse layouts (traversal_order longer than shape rank) require+// block_map. With block_map missing the metadata is malformed and enable()+// must disable the tensor so densify() is never called on it.+TEST(SparsityInfoTest, EnableDisablesWhenBlockSparseMissingBlockMap) {+ SparsityInfo s;+ s.set_shape({2, 2}); // rank = 2+ s.set_traversal_order({0, 1, 2, 3}); // length = 4 → block-sparse with k=2+ s.set_dim_format({0, 1, 1, 1});+ // block_map intentionally absent — invalid for block-sparse+ s.enable();+ EXPECT_TRUE(s.is_disabled());+}++// traversal_order shorter than the tensor rank — densify() would later loop+// `for (dim = 0; dim < m_shape.size(); ++dim) m_dim_format[dim]` and read+// past the end of m_dim_format on untrusted model input. enable() must+// catch this size mismatch up front.+TEST(SparsityInfoTest, EnableDisablesWhenTraversalOrderShorterThanRank) {+ SparsityInfo s;+ s.set_shape({2, 2});+ s.set_traversal_order({0}); // length 1 < rank 2+ s.set_dim_format({0});+ s.set_data_desc({SparsityInfo::SparsityDataDesc{0, nullptr, 0, nullptr}});+ s.enable();+ EXPECT_TRUE(s.is_disabled());+}++// dim_format must carry exactly one DimensionMetadata entry per+// traversal-order position. A length mismatch is malformed; enable() must+// disable so the dispatch in densify() never indexes m_dim_format past its+// real end.+TEST(SparsityInfoTest, EnableDisablesWhenDimFormatLengthMismatchesTraversal) {+ SparsityInfo s;+ s.set_shape({2, 2});+ s.set_traversal_order({0, 1});+ s.set_dim_format({0}); // length 1 != traversal_order length 2+ s.set_data_desc({SparsityInfo::SparsityDataDesc{0, nullptr, 0, nullptr}});+ s.enable();+ EXPECT_TRUE(s.is_disabled());+}++// A SPARSE_CSR DimensionMetadata with absent array_segments or array_indices+// is recorded as a null pointer in m_data_desc. densify()'s dispatcher casts+// those pointers to flatbuffers vector types and calls ->values() without a+// null check, which would dereference null. enable() must disable the+// tensor in that case so the raw-buffer fallback runs instead.+TEST(SparsityInfoTest, EnableDisablesWhenSparseCsrDimMissesSegments) {+ SparsityInfo s;+ s.set_shape({2, 2});+ s.set_traversal_order({0, 1});+ s.set_dim_format({0, 1}); // dim 1 is SPARSE_CSR+ s.set_data_desc({+ SparsityInfo::SparsityDataDesc{0, nullptr, 0, nullptr},+ SparsityInfo::SparsityDataDesc{0, nullptr, 0, kIndicesSentinel}, // segments absent+ });+ s.enable();+ EXPECT_TRUE(s.is_disabled());+}++// HandsLandmarkFull-style: tf_sparsity is non-null but dim_metadata is empty,+// so dim_format ends up empty in get_sparsity().+TEST(SparsityInfoTest, EnableDisablesWhenDimFormatEmpty) {+ SparsityInfo s;+ s.set_shape({2, 2});+ s.set_traversal_order({0, 1});+ // block_map left empty (consistent with non-block-sparse layout)+ // dim_format intentionally left empty+ s.enable();+ EXPECT_TRUE(s.is_disabled());+}++// Block-sparse positive: traversal_order length = rank + #block dims,+// block_map lists the block dims, dim_format and data_desc match.+TEST(SparsityInfoTest, EnableEnabledOnBlockSparseLayout) {+ SparsityInfo s;+ s.set_shape({2, 2}); // rank = 2+ s.set_traversal_order({0, 1, 2, 3}); // rank + 2 block dims+ s.set_block_map({0, 1}); // 2 block dims appended after rank+ s.set_dim_format({0, 0, 1, 1});+ s.set_data_desc({+ SparsityInfo::SparsityDataDesc{0, nullptr, 0, nullptr},+ SparsityInfo::SparsityDataDesc{0, nullptr, 0, nullptr},+ SparsityInfo::SparsityDataDesc{0, kSegmentsSentinel, 0, kIndicesSentinel},+ SparsityInfo::SparsityDataDesc{0, kSegmentsSentinel, 0, kIndicesSentinel},+ });+ s.enable();+ EXPECT_FALSE(s.is_disabled());+}++TEST(SparsityInfoTest, FullCtorAlreadyEnabled) {+ SparsityInfo::SparsityDataDesc dense_desc{};+ SparsityInfo::SparsityDataDesc sparse_desc{0, kSegmentsSentinel, 0, kIndicesSentinel};+ SparsityInfo+ s({2, 2}, {0, 1}, {}, {0, 1}, {dense_desc, sparse_desc}, ov::element::f32, kDummyValues, sizeof(kDummyValues));+ EXPECT_FALSE(s.is_disabled()) << "Full ctor must call enable() and find all fields populated.";+}++// The full ctor calls enable() at the end (sparsity_info.hpp). With an+// empty dim_format the result must be disabled.+TEST(SparsityInfoTest, FullCtorWithEmptyDimFormatDisables) {+ SparsityInfo s({2, 2},+ {0, 1},+ {}, // block_map empty (non-block-sparse)+ {}, // dim_format empty+ {},+ ov::element::f32,+ kDummyValues,+ sizeof(kDummyValues));+ EXPECT_TRUE(s.is_disabled());+}++// dense_data() must throw when m_disabled is true. This is the contract that+// TensorLitePlace::ctor relies on via the is_disabled() short-circuit.+TEST(SparsityInfoTest, DenseDataThrowsWhenDisabled) {+ SparsityInfo s;+ s.enable();+ ASSERT_TRUE(s.is_disabled());+ EXPECT_THROW(s.dense_data(), std::exception);+}++// disable()/enable() round-trip: explicit disable() forces disabled; a+// subsequent enable() re-evaluates against the populated fields. Lock-down+// against future regressions that turn enable() into a one-way switch.+TEST(SparsityInfoTest, DisableSetsFlagAndEnableReevaluates) {+ SparsityInfo s;+ s.set_shape({2, 2});+ s.set_traversal_order({0, 1});+ s.set_dim_format({0, 1});+ s.set_data_desc(make_dense_then_sparse_data_desc());++ s.disable();+ EXPECT_TRUE(s.is_disabled());++ s.enable();+ EXPECT_FALSE(s.is_disabled()) << "enable() must re-evaluate from scratch, not stay disabled.";+}++// is_copyable() is fixed to false for runtime attributes — sparsity is bound+// to the original tensor. Locks the contract used by RuntimeAttribute.+TEST(SparsityInfoTest, IsCopyableReturnsFalse) {+ SparsityInfo s;+ EXPECT_FALSE(s.is_copyable());+}++// Setters and getters round-trip values without side effects. Cheap sanity+// to make sure none of the setters accidentally re-disable the object.+TEST(SparsityInfoTest, SettersRoundTripPreserveValues) {+ SparsityInfo s;+ s.set_shape({4, 8});+ s.set_traversal_order({1, 0});+ s.set_block_map({0});+ s.set_dim_format({0, 1});+ s.set_target_type(ov::element::i8);+ s.set_values(kDummyValues, sizeof(kDummyValues));++ EXPECT_EQ(s.get_shape(), std::vector<int32_t>({4, 8}));+ EXPECT_EQ(s.get_traversal_order(), std::vector<int32_t>({1, 0}));+ EXPECT_EQ(s.get_block_map(), std::vector<int32_t>({0}));+ EXPECT_EQ(s.get_dim_format(), std::vector<uint16_t>({0, 1}));+ EXPECT_EQ(s.get_target_type(), ov::element::i8);+ EXPECT_EQ(s.get_values(), kDummyValues);+ EXPECT_EQ(s.get_values_size(), sizeof(kDummyValues));+}diff --git a/src/frontends/tensorflow_lite/tests/test_models/gen_scripts/generate_sparse_incomplete.py b/src/frontends/tensorflow_lite/tests/test_models/gen_scripts/generate_sparse_incomplete.pynew file mode 100644index 00000000000..c15ed6a04a1--- /dev/null+++ b/src/frontends/tensorflow_lite/tests/test_models/gen_scripts/generate_sparse_incomplete.py@@ -0,0 +1,461 @@+# Copyright (C) 2018-2026 Intel Corporation+# SPDX-License-Identifier: Apache-2.0++# WHAT:+# Generates a family of small .tflite test models in which a constant+# tensor carries a non-null SparsityParameters table with sub-fields that+# are either incomplete (missing/empty traversal_order, dim_metadata,+# dim_format, or fully empty SparsityParameters) or that exercise the+# "valid standard CSR" corner case where block_map is absent. The unifying+# contract: every model produced here must both load() and convert()+# without throwing once the OpenVINO TFLite frontend's get_sparsity()+# factory invokes SparsityInfo::enable(). Two outcomes are expected:+# - Incomplete metadata (missing/empty traversal_order, dim_metadata,+# dim_format, or all-empty SparsityParameters) → tensor is recognized+# as "not really sparse" and falls back to the raw constant buffer.+# - Missing block_map on a non-block-sparse layout (traversal_order+# length == tensor rank) → tensor is treated as a valid standard CSR+# sparse tensor and is densified normally. block_map is required only+# for block-sparse layouts (schema.fbs:187-211).+#+# HOW:+# Uses the official Python `flatbuffers.Builder` (already a transitive+# dependency of the TensorFlow package required by tests/requirements.txt,+# so no new dep is introduced). Field offsets and table layouts follow+# src/frontends/tensorflow_lite/src/schema/schema.fbs verbatim.+# build_incomplete_sparsity_model() takes knobs for which of the three+# SparsityParameters sub-fields to include (or to include but leave empty)+# and which simple op graph to emit:+# - "add" : input + const_with_sparsity -> output (all FLOAT32)+# - "dequantize": DEQUANTIZE(const_with_sparsity) -> mid;+# ADD(input, mid) -> output (mirrors bug location)+# For both graphs the constant tensor's buffer holds exactly+# prod(shape) * elem_size bytes so that the buffer-size check in+# load_model() passes once enable() has correctly disabled the sparsity+# and TensorLitePlace consequently exposes the raw buffer.+#+# WHY:+# Regression coverage for src/frontends/tensorflow_lite/src/utils.cpp's+# get_sparsity() factory. Without sparsity->enable() at the end of that+# factory, an incomplete SparsityParameters keeps m_disabled = false,+# TensorLitePlace::ctor calls dense_data() -> densify() and the convert+# step throws "Sparse dimension isn't found for sparse tensor". With the+# fix, every model produced here loads + converts cleanly — either by+# falling back to the raw buffer (incomplete-metadata cases) or by+# densifying a valid standard CSR tensor (missing-block_map case).+#+# The missing_block_map model deliberately uses segments=[0, 0, 0] and+# indices=[] so that densify() produces an all-zero (2, 2) constant.+# Its raw FLOAT32 buffer is [1, 2, 3, 4]. The two outcomes are therefore+# observable on the converted model: ADD(input, const) == input proves+# densify() ran (asserted by IncompleteSparsityDensify in+# convert_sparse_incomplete.cpp), and a future regression that disabled+# this tensor would visibly produce input + [1, 2, 3, 4] instead.+#+# Real-world reproducer hand_landmark_full.tflite is 5.3 MB and cannot be+# committed, so the synthetic flatbuffers below stand in for it.+#+# Models produced (in subdirectory sparse_incomplete/):+# 1. missing_dim_metadata.tflite - traversal_order + block_map set; dim_metadata field omitted (HandsLandmarkFull shape)+# 2. empty_dim_metadata.tflite - traversal_order + block_map set; dim_metadata is an empty vector+# 3. missing_traversal_order.tflite - block_map + dim_metadata set; traversal_order omitted+# 4. missing_block_map.tflite - traversal_order + dim_metadata set; block_map omitted+# 5. empty_sparsity.tflite - SparsityParameters table present but all three fields omitted+# 6. dequantize_incomplete_sparsity.tflite - INT8 const with empty SparsityParameters consumed by DEQUANTIZE then ADD+#+# Generated tflite files are NOT committed to git; they are produced by the+# CMake target 'tensorflow_lite_test_models' at build time.++import os+import struct+import sys++import flatbuffers++# tflite/schema.fbs enum values (mirrored from+# src/frontends/tensorflow_lite/src/schema/schema.fbs).+TT_FLOAT32 = 0+TT_INT8 = 9++OP_ADD = 0+OP_DEQUANTIZE = 6++DIM_DENSE = 0+DIM_SPARSE_CSR = 1++# SparseIndexVector union tags+SPARSE_IDX_NONE = 0+SPARSE_IDX_INT32 = 1 # Int32Vector++# BuiltinOptions union tags (from schema.fbs:union BuiltinOptions order)+BO_NONE = 0+BO_AddOptions = 11++# ActivationFunctionType+ACT_NONE = 0+++# ---------- Helpers building schema.fbs tables ----------++def _create_int_vector(b, values, prepend_fn=None):+ """Create a Vector<int32> in the buffer, return offset."""+ prepend = prepend_fn or b.PrependInt32+ b.StartVector(4, len(values), 4)+ for v in reversed(values):+ prepend(v)+ return b.EndVector()+++def _create_uint_vector(b, values):+ b.StartVector(4, len(values), 4)+ for v in reversed(values):+ b.PrependUint32(v)+ return b.EndVector()+++def _create_byte_vector(b, payload):+ b.StartVector(1, len(payload), 1)+ for byte in reversed(payload):+ b.PrependUint8(byte)+ return b.EndVector()+++def _create_offset_vector(b, offsets):+ b.StartVector(4, len(offsets), 4)+ for off in reversed(offsets):+ b.PrependUOffsetTRelative(off)+ return b.EndVector()+++def _build_int32_vector_table(b, values):+ """schema.fbs:Int32Vector { values:[int]; } - field 0 only."""+ values_off = _create_int_vector(b, values) if values else _create_int_vector(b, [])+ b.StartObject(1)+ b.PrependUOffsetTRelativeSlot(0, values_off, 0)+ return b.EndObject()+++def _build_dim_metadata_dense(b, dense_size):+ """schema.fbs:DimensionMetadata { format, dense_size, ... }."""+ b.StartObject(6)+ b.PrependInt8Slot(0, DIM_DENSE, 0)+ b.PrependInt32Slot(1, dense_size, 0)+ return b.EndObject()+++def _build_dim_metadata_sparse_csr(b, segments_off, indices_off):+ """SPARSE_CSR DimensionMetadata referring to two Int32Vector tables."""+ b.StartObject(6)+ b.PrependInt8Slot(0, DIM_SPARSE_CSR, 0)+ b.PrependInt32Slot(1, 0, 0) # dense_size unused for sparse dims+ b.PrependUint8Slot(2, SPARSE_IDX_INT32, 0) # array_segments_type+ b.PrependUOffsetTRelativeSlot(3, segments_off, 0) # array_segments+ b.PrependUint8Slot(4, SPARSE_IDX_INT32, 0) # array_indices_type+ b.PrependUOffsetTRelativeSlot(5, indices_off, 0) # array_indices+ return b.EndObject()+++def _build_sparsity_params(b, *, include_traversal_order, include_block_map,+ dim_metadata_mode):+ """+ SparsityParameters {+ traversal_order:[int]; // field 0+ block_map:[int]; // field 1+ dim_metadata:[DimensionMetadata]; // field 2+ }++ dim_metadata_mode:+ "full" - 2 valid DimensionMetadata entries+ "empty" - empty vector+ "missing" - field absent+ """+ if include_traversal_order:+ traversal_order_off = _create_int_vector(b, [0, 1])+ else:+ traversal_order_off = 0++ if include_block_map:+ block_map_off = _create_int_vector(b, [0])+ else:+ block_map_off = 0++ if dim_metadata_mode == "full":+ # Build segments / indices Int32Vector tables BEFORE starting any+ # other table (FlatBuffers nesting rule).+ segments_table_off = _build_int32_vector_table(b, [0, 0, 0])+ indices_table_off = _build_int32_vector_table(b, [])++ dim0_off = _build_dim_metadata_dense(b, 2)+ dim1_off = _build_dim_metadata_sparse_csr(b, segments_table_off,+ indices_table_off)+ dim_metadata_off = _create_offset_vector(b, [dim0_off, dim1_off])+ elif dim_metadata_mode == "empty":+ dim_metadata_off = _create_offset_vector(b, [])+ elif dim_metadata_mode == "missing":+ dim_metadata_off = 0+ else:+ raise ValueError("dim_metadata_mode=" + repr(dim_metadata_mode))++ b.StartObject(3)+ if traversal_order_off:+ b.PrependUOffsetTRelativeSlot(0, traversal_order_off, 0)+ if block_map_off:+ b.PrependUOffsetTRelativeSlot(1, block_map_off, 0)+ if dim_metadata_mode != "missing":+ b.PrependUOffsetTRelativeSlot(2, dim_metadata_off, 0)+ return b.EndObject()+++def _build_buffer(b, payload):+ """Buffer { data:[ubyte]; } - field 0."""+ if payload is None or len(payload) == 0:+ b.StartObject(1)+ return b.EndObject()+ data_off = _create_byte_vector(b, payload)+ b.StartObject(1)+ b.PrependUOffsetTRelativeSlot(0, data_off, 0)+ return b.EndObject()+++def _build_tensor(b, *, shape, type_code, buffer_idx, name_off,+ sparsity_off=0):+ """+ Tensor {+ shape:[int]; type:TensorType; buffer:uint; name:string;+ quantization:QuantizationParameters; is_variable:bool=false;+ sparsity:SparsityParameters; ...+ }+ """+ shape_off = _create_int_vector(b, shape)+ b.StartObject(10)+ b.PrependUOffsetTRelativeSlot(0, shape_off, 0)+ b.PrependInt8Slot(1, type_code, 0)+ b.PrependUint32Slot(2, buffer_idx, 0)+ b.PrependUOffsetTRelativeSlot(3, name_off, 0)+ if sparsity_off:+ b.PrependUOffsetTRelativeSlot(6, sparsity_off, 0)+ return b.EndObject()+++def _build_opcode(b, builtin):+ """OperatorCode { deprecated_builtin_code:byte; ...; version:int; builtin_code:int; }.++ For backward compatibility, BuiltinCode() readers fall back to the legacy+ 8-bit deprecated_builtin_code (slot 0) whenever the 32-bit builtin_code+ (slot 3) is below PLACEHOLDER_FOR_GREATER_OP_CODES (127). Both ADD and+ DEQUANTIZE are below that threshold, so the value MUST go into slot 0.+ """+ b.StartObject(4)+ b.PrependInt8Slot(0, builtin, 0)+ b.PrependInt32Slot(2, 1, 0) # version = 1+ return b.EndObject()+++def _build_add_options(b):+ """schema.fbs:AddOptions { fused_activation_function:ActivationFunctionType=NONE; pot_scale_int16:bool=true; }+ All fields take their defaults; we still need a non-null table so that+ decoder_flatbuffer.h:40's `opts != nullptr` check passes when the ADD+ translator looks up fused_activation_function.+ """+ b.StartObject(2)+ # field 0 = fused_activation_function (default NONE = 0): omitted to keep+ # default; field 1 = pot_scale_int16 (default true): omitted to keep default.+ return b.EndObject()+++def _build_operator(b, opcode_index, inputs, outputs,+ builtin_options_type=BO_NONE, builtin_options_off=0):+ inputs_off = _create_int_vector(b, inputs)+ outputs_off = _create_int_vector(b, outputs)+ b.StartObject(9)+ b.PrependUint32Slot(0, opcode_index, 0)+ b.PrependUOffsetTRelativeSlot(1, inputs_off, 0)+ b.PrependUOffsetTRelativeSlot(2, outputs_off, 0)+ if builtin_options_type != BO_NONE:+ b.PrependUint8Slot(3, builtin_options_type, 0)+ b.PrependUOffsetTRelativeSlot(4, builtin_options_off, 0)+ return b.EndObject()+++def _build_subgraph(b, *, tensors, inputs, outputs, operators, name_off):+ tensors_off = _create_offset_vector(b, tensors)+ inputs_off = _create_int_vector(b, inputs)+ outputs_off = _create_int_vector(b, outputs)+ operators_off = _create_offset_vector(b, operators)+ b.StartObject(5)+ b.PrependUOffsetTRelativeSlot(0, tensors_off, 0)+ b.PrependUOffsetTRelativeSlot(1, inputs_off, 0)+ b.PrependUOffsetTRelativeSlot(2, outputs_off, 0)+ b.PrependUOffsetTRelativeSlot(3, operators_off, 0)+ b.PrependUOffsetTRelativeSlot(4, name_off, 0)+ return b.EndObject()+++# ---------- Top-level builder ----------++def build_incomplete_sparsity_model(*,+ include_traversal_order=True,+ include_block_map=True,+ dim_metadata_mode="full",+ graph="add"):+ """+ Build a tiny .tflite model whose only constant tensor carries a non-null+ SparsityParameters with the requested completeness profile.++ graph:+ "add" - a single ADD(input, const) op, all FLOAT32, shape (2, 2).+ "dequantize" - DEQUANTIZE(const) -> mid; ADD(input, mid) -> output.+ const is INT8 (2, 2) with 4-byte buffer; everything+ else is FLOAT32 (2, 2).+ """+ b = flatbuffers.Builder(1024)++ # -- Build leaf objects (strings, sparsity, buffers, tensors) bottom-up --++ # Strings (must be created before tables that reference them.)+ str_input = b.CreateString("input")+ str_const = b.CreateString("sparse_const")+ str_output = b.CreateString("output")+ str_main = b.CreateString("main")+ str_desc = b.CreateString("sparse_incomplete_test")+ str_mid = b.CreateString("dequant_out") if graph == "dequantize" else 0++ # Sparsity object referenced by the const tensor.+ sparsity_off = _build_sparsity_params(+ b,+ include_traversal_order=include_traversal_order,+ include_block_map=include_block_map,+ dim_metadata_mode=dim_metadata_mode,+ )++ # Const tensor's raw buffer: prod(shape) * elem_size bytes.+ if graph == "add":+ const_dtype = TT_FLOAT32+ const_payload = struct.pack('<4f', 1.0, 2.0, 3.0, 4.0) # 16 B+ else:+ const_dtype = TT_INT8+ const_payload = struct.pack('<4b', 1, 2, 3, 4) # 4 B++ # Buffers: index 0 must exist and be empty (TFLite convention).+ buffer0 = _build_buffer(b, b"")+ buffer1 = _build_buffer(b, const_payload) # const tensor data+ buffer2 = _build_buffer(b, b"") # input placeholder+ buffer3 = _build_buffer(b, b"") # output placeholder+ buffers = [buffer0, buffer1, buffer2, buffer3]+ if graph == "dequantize":+ buffer4 = _build_buffer(b, b"") # dequant intermediate+ buffers.append(buffer4)++ # Tensors.+ shape_22 = [2, 2]+ tensor_input = _build_tensor(b, shape=shape_22, type_code=TT_FLOAT32,+ buffer_idx=2, name_off=str_input)+ tensor_const = _build_tensor(b, shape=shape_22, type_code=const_dtype,+ buffer_idx=1, name_off=str_const,+ sparsity_off=sparsity_off)+ tensor_output = _build_tensor(b, shape=shape_22, type_code=TT_FLOAT32,+ buffer_idx=3, name_off=str_output)+ tensors = [tensor_input, tensor_const, tensor_output]+ if graph == "dequantize":+ tensor_mid = _build_tensor(b, shape=shape_22, type_code=TT_FLOAT32,+ buffer_idx=4, name_off=str_mid)+ tensors.append(tensor_mid)++ # Operator codes + Operators.+ # AddOptions table is mandatory for ADD: the translator reads+ # fused_activation_function via decoder_flatbuffer.h::get_attribute(),+ # which dereferences builtin_options_as<AddOptions>() and asserts non-null.+ if graph == "add":+ opcode_add = _build_opcode(b, OP_ADD)+ opcodes = [opcode_add]+ add_options_off = _build_add_options(b)+ operator_add = _build_operator(b, opcode_index=0,+ inputs=[0, 1], outputs=[2],+ builtin_options_type=BO_AddOptions,+ builtin_options_off=add_options_off)+ operators = [operator_add]+ else:+ opcode_deq = _build_opcode(b, OP_DEQUANTIZE)+ opcode_add = _build_opcode(b, OP_ADD)+ opcodes = [opcode_deq, opcode_add]+ add_options_off = _build_add_options(b)+ # opcode_index 0 = DEQUANTIZE on the const, output to tensor 3 (mid)+ operator_deq = _build_operator(b, opcode_index=0,+ inputs=[1], outputs=[3])+ # opcode_index 1 = ADD(input, mid) -> output+ operator_add = _build_operator(b, opcode_index=1,+ inputs=[0, 3], outputs=[2],+ builtin_options_type=BO_AddOptions,+ builtin_options_off=add_options_off)+ operators = [operator_deq, operator_add]++ # SubGraph.+ subgraph0 = _build_subgraph(b, tensors=tensors, inputs=[0], outputs=[2],+ operators=operators, name_off=str_main)++ # Top-level Model.+ opcodes_off = _create_offset_vector(b, opcodes)+ subgraphs_off = _create_offset_vector(b, [subgraph0])+ buffers_off = _create_offset_vector(b, buffers)++ b.StartObject(7)+ b.PrependUint32Slot(0, 3, 0) # version+ b.PrependUOffsetTRelativeSlot(1, opcodes_off, 0) # operator_codes+ b.PrependUOffsetTRelativeSlot(2, subgraphs_off, 0) # subgraphs+ b.PrependUOffsetTRelativeSlot(3, str_desc, 0) # description+ b.PrependUOffsetTRelativeSlot(4, buffers_off, 0) # buffers+ model = b.EndObject()++ b.Finish(model, file_identifier=b"TFL3")+ return bytes(b.Output())+++def main():+ if len(sys.argv) != 2:+ print("Usage: " + sys.argv[0] + " <output_dir>", file=sys.stderr)+ sys.exit(1)++ out_dir = os.path.join(sys.argv[1], "sparse_incomplete")+ os.makedirs(out_dir, exist_ok=True)++ cases = [+ ("missing_dim_metadata.tflite", dict(+ include_traversal_order=True,+ include_block_map=True,+ dim_metadata_mode="missing",+ graph="add")),+ ("empty_dim_metadata.tflite", dict(+ include_traversal_order=True,+ include_block_map=True,+ dim_metadata_mode="empty",+ graph="add")),+ ("missing_traversal_order.tflite", dict(+ include_traversal_order=False,+ include_block_map=True,+ dim_metadata_mode="full",+ graph="add")),+ ("missing_block_map.tflite", dict(+ include_traversal_order=True,+ include_block_map=False,+ dim_metadata_mode="full",+ graph="add")),+ ("empty_sparsity.tflite", dict(+ include_traversal_order=False,+ include_block_map=False,+ dim_metadata_mode="missing",+ graph="add")),+ ("dequantize_incomplete_sparsity.tflite", dict(+ include_traversal_order=False,+ include_block_map=False,+ dim_metadata_mode="missing",+ graph="dequantize")),+ ]++ for filename, kwargs in cases:+ data = build_incomplete_sparsity_model(**kwargs)+ with open(os.path.join(out_dir, filename), "wb") as f:+ f.write(data)+++if __name__ == "__main__":+ main()