tflite-micro/python/tflite_micro/compression_utils.h
Ryan Kuester 74b9db940a
feat(python): throw error when loading compressed models without support (#3167)
When a model contains COMPRESSION_METADATA but the interpreter was built
without compression support, throw a RuntimeError with a helpful message
directing users to build with --//:with_compression=true.

The implementation uses inline functions in compression_utils.h
that are optimized away when compression is disabled, ensuring
all code paths remain compile-checked, and readable without
preprocessor clutter.

Includes test_compression_unsupported.py to verify the error detection,
which only runs when compression is disabled.

BUG=#3125
2025-08-04 23:11:45 +00:00

56 lines
No EOL
1.8 KiB
C++

/* Copyright 2025 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#ifndef TENSORFLOW_LITE_MICRO_PYTHON_COMPRESSION_UTILS_H_
#define TENSORFLOW_LITE_MICRO_PYTHON_COMPRESSION_UTILS_H_
#include <cstring>
#include "tensorflow/lite/schema/schema_generated.h"
namespace tflite {
// Returns true if interpreter was built with compression support.
// When USE_TFLM_COMPRESSION is defined, this always returns true and
// the compiler can optimize away any if (!IsCompressionSupported()) branches.
inline constexpr bool IsCompressionSupported() {
#ifdef USE_TFLM_COMPRESSION
return true;
#else
return false;
#endif
}
// Helper to check if model has compression metadata.
// This is always compiled in, but when used with IsCompressionSupported()
// the entire check can be optimized away.
inline bool HasCompressionMetadata(const Model& model) {
if (!model.metadata()) {
return false;
}
for (size_t i = 0; i < model.metadata()->size(); ++i) {
const auto* metadata = model.metadata()->Get(i);
if (metadata && metadata->name() &&
strcmp(metadata->name()->c_str(), "COMPRESSION_METADATA") == 0) {
return true;
}
}
return false;
}
} // namespace tflite
#endif // TENSORFLOW_LITE_MICRO_PYTHON_COMPRESSION_UTILS_H_