Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Tensorflow Serving Tflite Session Test

From Leeroopedia
Knowledge Sources
Domains Testing, TFLite
Last Updated 2026-02-13 00:00 GMT

Overview

Test suite validating the TfLiteSession class which wraps TensorFlow Lite models behind the TensorFlow Session interface.

Description

This test file provides extensive coverage of the TfLiteSession implementation, which adapts TFLite models to the standard TensorFlow Session interface. Tests cover model loading, signature definition extraction, tensor name handling (with and without :0 suffix), input tensor resizing, string processing, batch parallelism, and scheduler integration.

Multiple test models are used:

  • half_plus_two_tflite - basic arithmetic model
  • half_plus_two_tflite_with_sigdef - model with embedded signature defs
  • mobilenet_v1_quant_tflite - quantized MobileNet model
  • parse_example_tflite - model with ParseExample ops

Key areas tested include:

  • Basic model loading and inference
  • Input tensor resizing with same number of elements
  • Legacy converter model compatibility
  • String tensor processing (standard and Flex delegate)
  • Thread pool options
  • Signature definition extraction (single and multiple)
  • Batch parallelism for float and string inputs
  • Custom scheduler integration

Usage

Run these tests to validate changes to the TFLite session wrapper, signature handling, or TFLite model serving infrastructure.

Code Reference

Source Location

  • Repository: Tensorflow_Serving
  • File: tensorflow_serving/servables/tensorflow/tflite_session_test.cc
  • Lines: 1-1003

Test Fixture

constexpr char kTestModel[] =
    "/servables/tensorflow/testdata/saved_model_half_plus_two_tflite/"
    "00000123/model.tflite";

// Basic test creates a TfLiteSession and runs inference
TEST(TfLiteSession, BasicTest) {
  string model_bytes;
  TF_ASSERT_OK(ReadFileToString(tensorflow::Env::Default(),
                                test_util::TestSrcDirPath(kTestModel),
                                &model_bytes));
  ::google::protobuf::Map<string, SignatureDef> signatures;
  std::unique_ptr<TfLiteSession> session;
  tensorflow::SessionOptions options;
  TF_ASSERT_OK(TfLiteSession::Create(
      std::move(model_bytes), options, num_pools,
      num_tflite_interpreters, &session, &signatures));
}

Build Target

bazel test //tensorflow_serving/servables/tensorflow:tflite_session_test

Test Coverage

Key Test Cases

Test Name Category Description
BasicTest Integration Tests basic model load and inference with half_plus_two
ResizeWithSameNumElementsTest Input Handling Tests input tensor resizing when element count matches
ModelFromLegacyConverterWithSigdef Compatibility Tests models from legacy TF v1 converter
ProcessStrings Input Handling Tests string tensor processing
ProcessStringsFlex Input Handling Tests string tensor processing with Flex delegate
ThreadPoolOptions Configuration Tests thread pool options support
SimpleSignatureDef Signature Tests single signature definition extraction
MultipleSignatureDef Signature Tests multiple signature definition extraction
SignatureDefWithCommonTensorPrefix Signature Tests signature defs with shared tensor name prefixes
SimpleSignatureDefAndRun Integration Tests signature extraction followed by inference
TestBatchParallelismForFloat Performance Tests batch parallel execution for float tensors
TestBatchParallelismForString Performance Tests batch parallel execution for string tensors
TestSetScheduler Configuration Tests custom scheduler integration

Usage Examples

Test Pattern

TEST(TfLiteSession, BasicTest) {
  string model_bytes;
  TF_ASSERT_OK(ReadFileToString(tensorflow::Env::Default(),
                                test_util::TestSrcDirPath(kTestModel),
                                &model_bytes));
  ::google::protobuf::Map<string, SignatureDef> signatures;
  std::unique_ptr<TfLiteSession> session;
  tensorflow::SessionOptions options;
  TF_ASSERT_OK(TfLiteSession::Create(
      std::move(model_bytes), options, absl::GetFlag(FLAGS_num_pools),
      absl::GetFlag(FLAGS_num_tflite_interpreters), &session, &signatures));
  Tensor input = test::AsTensor<float>({1.0, 2.0, 3.0}, TensorShape({3}));
  std::vector<Tensor> outputs;
  TF_EXPECT_OK(session->Run({{"x", input}}, {"y"}, {}, &outputs));
  ASSERT_EQ(outputs.size(), 1);
  test::ExpectTensorEqual<float>(
      outputs[0], test::AsTensor<float>({2.5, 3, 3.5}, TensorShape({3})));
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment