This file was generated by an older version of protoc

I built Flex delegate successfully with command “bazel build -c opt --config=monolithic tensorflow/lite/delegates/flex:tensorflowlite_flex”.

  • Ubuntu: 18.04
  • GCC/G++: 9.4 .0
  • Protoc: 3.20.0 (protoc --version)
  • Python: 3.10.12
  • Protobuf: 3.20.0 (pip show protobuf)
  • Tensorflow: 2.9.3
  • Bazel: 5.0.0
    Then I linked it with my source code using CMake
cmake_minimum_required(VERSION 3.16)
project(minimal C CXX)

include_directories(tensorflow_src/bazel-bin)
find_package(Protobuf REQUIRED)
include_directories(
  ${Protobuf_INCLUDE_DIRS}
)

set(TENSORFLOW_SOURCE_DIR "" CACHE PATH
  "Directory that contains the TensorFlow project"
)
if(NOT TENSORFLOW_SOURCE_DIR)
  get_filename_component(TENSORFLOW_SOURCE_DIR
    "${CMAKE_CURRENT_LIST_DIR}/../../../../"
    ABSOLUTE
  )
endif()

add_subdirectory(
  "${TENSORFLOW_SOURCE_DIR}/tensorflow/lite"
  "${CMAKE_CURRENT_BINARY_DIR}/tensorflow-lite"
  EXCLUDE_FROM_ALL
)

# find_library(TF_LIB tensorflowlite HINTS "${TENSORFLOW_SOURCE_DIR}/bazel-bin/tensorflow/lite/")
find_library(TF_LIB_FLEX tensorflowlite_flex HINTS "${TENSORFLOW_SOURCE_DIR}/bazel-bin/tensorflow/lite/delegates/flex/")

set(CMAKE_CXX_STANDARD 17)
add_executable(minimal
  minimal.cc
)
target_link_libraries(minimal
  tensorflow-lite
  # tensorflowlite_flex
  -Wl,--no-as-needed # Need --no-as-needed to link tensorflowlite_flex
  # ${TF_LIB}
  ${TF_LIB_FLEX}
)

But it notify “This file was generated by an older version of protoc …”. Please help me resolve this issue.

TensorFlow 2.16 Repo via JARaaS Hybrid RAG - Documentation - code current 6/18/2024

Note: Sources at the end of the response

The error message “This file was generated by an older version of protoc …” usually indicates that there is a mismatch between the version of protoc (the protocol buffer compiler) used to generate the .pb.h and .pb.cc files and the version of the protobuf runtime library being linked.

Here’s a step-by-step approach to resolve this issue:

  1. Verify Protoc and Protobuf Versions: Ensure that the version of protoc used during the build of TensorFlow matches the version of the protobuf library linked at runtime.

    • Verify the installed protoc version:
      protoc --version
      
    • Verify the protobuf library version:
      pip show protobuf
      
  2. Rebuild Protobuf Files: If there is a mismatch, regenerate the .pb.h and .pb.cc files using the correct version of protoc.

    • Navigate to the TensorFlow source directory where protobuf files are located and run protoc:
      protoc -I=. --cpp_out=. my_proto_file.proto
      

    Adjust the path and filenames as necessary to match your project structure.

  3. Clean Build: Clean the current Bazel build to remove any potentially corrupted or mismatched files:

    bazel clean --expunge
    
  4. Rebuild TensorFlow Lite with Flex Delegate:

    bazel build -c opt --config=monolithic tensorflow/lite/delegates/flex:tensorflowlite_flex
    
  5. Update CMake Configuration: Make sure your CMake configuration is pointing to the right directories and using the correct versions:

    cmake_minimum_required(VERSION 3.16)
    project(minimal C CXX)
    
    include_directories(tensorflow_src/bazel-bin)
    find_package(Protobuf REQUIRED)
    include_directories(
      ${Protobuf_INCLUDE_DIRS}
    )
    
    set(TENSORFLOW_SOURCE_DIR "" CACHE PATH
      "Directory that contains the TensorFlow project"
    )
    if(NOT TENSORFLOW_SOURCE_DIR)
      get_filename_component(TENSORFLOW_SOURCE_DIR
        "${CMAKE_CURRENT_LIST_DIR}/../../../../"
        ABSOLUTE
      )
    endif()
    
    add_subdirectory(
      "${TENSORFLOW_SOURCE_DIR}/tensorflow/lite"
      "${CMAKE_CURRENT_BINARY_DIR}/tensorflow-lite"
      EXCLUDE_FROM_ALL
    )
    
    find_library(TF_LIB_FLEX tensorflowlite_flex HINTS "${TENSORFLOW_SOURCE_DIR}/bazel-bin/tensorflow/lite/delegates/flex/")
    
    set(CMAKE_CXX_STANDARD 17)
    add_executable(minimal
      minimal.cc
    )
    target_link_libraries(minimal
      tensorflow-lite
      -Wl,--no-as-needed
      ${TF_LIB_FLEX}
    )
    
  6. Check Protobuf Linking: Ensure the generated protobuf files are indeed compatible with the protobuf library version in use. If necessary, update the protobuf library via pip or build it from source to match the version used with protoc.

You can find more in-depth information on compiling TensorFlow with custom configurations in the following internal documents:

Sources:

  • Custom Kernel Installation: custom-linux-kernel.md (internal document)
  • Building from Source: build_from_source.md (internal document)
  • TensorFlow Custom Operations: create_op.md (internal document)

By following these steps, you should be able to resolve the version mismatch issue and successfully link your project with the TensorFlow Lite Flex delegate.