Skip to content

Commit

Permalink
[mio] Introduce mio-tflite2121 (#11432)
Browse files Browse the repository at this point in the history
This will introduce mio-tflite1212 for tflite version 2.12.1.

ONE-DCO-1.0-Signed-off-by: SaeHie Park <[email protected]>
  • Loading branch information
seanshpark authored Sep 1, 2023
1 parent 85a8fba commit b0cfbe0
Show file tree
Hide file tree
Showing 6 changed files with 404 additions and 0 deletions.
60 changes: 60 additions & 0 deletions compiler/mio-tflite2121/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
nnas_find_package(FlatBuffers EXACT 2.0 QUIET)

if(NOT FlatBuffers_FOUND)
message(STATUS "Build mio-tflite2121: FAILED (missing Flatbuffers 2.0)")
return()
endif(NOT FlatBuffers_FOUND)

nnas_find_package(TensorFlowSource EXACT 2.12.1 QUIET)

if(NOT TensorFlowSource_FOUND)
message(STATUS "Build mio-tflite2121: FAILED (missing TensorFlowSource 2.12.1)")
return()
endif(NOT TensorFlowSource_FOUND)

message(STATUS "Build mio-tflite2121: TRUE")
message(STATUS "Build mio-tflite2121: with ${TensorFlowSource_DIR}")

set(SCHEMA_FILE "${TensorFlowSource_DIR}/tensorflow/lite/schema/schema.fbs")

# NOTE Use copy of schema.fbs as to provide unified way for circle also
add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/schema.fbs"
COMMAND ${CMAKE_COMMAND} -E copy "${SCHEMA_FILE}" schema.fbs
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
DEPENDS "${SCHEMA_FILE}"
)

FlatBuffers_Target(mio_tflite2121
OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/gen/mio/tflite"
INCLUDE_DIR "${CMAKE_CURRENT_BINARY_DIR}/gen"
SCHEMA_DIR "${CMAKE_CURRENT_BINARY_DIR}"
SCHEMA_FILES "schema.fbs"
)

add_executable(mio_tflite2121_example example.cpp)
target_link_libraries(mio_tflite2121_example mio_tflite2121)

# Temporay tflite validation tool to replace nnkit-tflite
# TODO provide full tflite validation with runtime/interpreter
add_executable(mio_tflite2121_validate example.cpp)
target_link_libraries(mio_tflite2121_validate mio_tflite2121)

file(GLOB_RECURSE SOURCES "src/*.cpp")
file(GLOB_RECURSE TESTS "src/*.test.cpp")
list(REMOVE_ITEM SOURCES ${TESTS})

add_library(mio_tflite2121_helper STATIC ${SOURCES})
target_include_directories(mio_tflite2121_helper PRIVATE src)
target_include_directories(mio_tflite2121_helper PUBLIC include)
target_link_libraries(mio_tflite2121_helper mio_tflite2121)

if(NOT ENABLE_TEST)
return()
endif(NOT ENABLE_TEST)

nnas_find_package(GTest REQUIRED)

GTest_AddTest(mio_tflite2121_helper_test ${TESTS})
target_include_directories(mio_tflite2121_helper_test PRIVATE src)
target_link_libraries(mio_tflite2121_helper_test mio_tflite2121)
target_link_libraries(mio_tflite2121_helper_test mio_tflite2121_helper)
3 changes: 3 additions & 0 deletions compiler/mio-tflite2121/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# mio-tflite2121

_mio-tflite2121_ provides a library to access TensorFlow lite model files with V2.12.1.
41 changes: 41 additions & 0 deletions compiler/mio-tflite2121/example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2023 Samsung Electronics Co., Ltd. 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.
*/

//
// This example shows how to include and use "mio-tflite2121"
//
#include <mio/tflite/schema_generated.h>

#include <fstream>
#include <iostream>
#include <vector>

int main(int argc, char **argv)
{
std::ifstream ifs(argv[1], std::ios_base::binary);
std::vector<char> buf(std::istreambuf_iterator<char>{ifs}, std::istreambuf_iterator<char>{});

flatbuffers::Verifier verifier{reinterpret_cast<uint8_t *>(buf.data()), buf.size()};

if (!tflite::VerifyModelBuffer(verifier))
{
std::cout << "Fail" << std::endl;
return 255;
}

std::cout << "Pass" << std::endl;
return 0;
}
37 changes: 37 additions & 0 deletions compiler/mio-tflite2121/include/mio_tflite2121/Helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2023 Samsung Electronics Co., Ltd. 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 __MIO_TFLITE2121_HELPER_H__
#define __MIO_TFLITE2121_HELPER_H__

#include <mio/tflite/schema_generated.h>

namespace mio
{
namespace tflite
{

::tflite::BuiltinOperator builtin_code_neutral(const ::tflite::OperatorCode *opcode);
bool is_valid(const ::tflite::OperatorCode *opcode);
bool is_custom(const ::tflite::OperatorCode *opcode);
std::string opcode_name(const ::tflite::OperatorCode *opcode);
const char *tensor_type(const ::tflite::Tensor *tensor);
const char *tensor_name(const ::tflite::Tensor *tensor);

} // namespace tflite
} // namespace mio

#endif // __MIO_TFLITE2121_HELPER_H__
104 changes: 104 additions & 0 deletions compiler/mio-tflite2121/src/Helper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
* Copyright 2020 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.
*/

#include "mio_tflite2121/Helper.h"

#include <sstream>

namespace mio
{
namespace tflite
{

/**
* This will provide v3/v3a format neutral BuiltinOperator
*
* This function referenced
* https://github.com/tensorflow/tensorflow/blob/7d12007d7800d3714a02e05059f3ea602d1aec78/tensorflow/lite/schema/schema_utils.cc
*/
::tflite::BuiltinOperator builtin_code_neutral(const ::tflite::OperatorCode *opcode)
{
assert(opcode != nullptr);
return std::max(opcode->builtin_code(),
static_cast<::tflite::BuiltinOperator>(opcode->deprecated_builtin_code()));
}

bool is_valid(const ::tflite::OperatorCode *opcode)
{
// Valid Range : 0 <= deprecated_builtin_code <= 127
const int8_t deprecated_builtin_code = opcode->deprecated_builtin_code();
if (deprecated_builtin_code < 0)
return false;

const ::tflite::BuiltinOperator builtin_code = opcode->builtin_code();
if (!(::tflite::BuiltinOperator_MIN <= builtin_code &&
builtin_code <= ::tflite::BuiltinOperator_MAX))
return false;

return true;
}

bool is_custom(const ::tflite::OperatorCode *opcode)
{
::tflite::BuiltinOperator code = builtin_code_neutral(opcode);
return (code == ::tflite::BuiltinOperator_CUSTOM);
}

std::string opcode_name(const ::tflite::OperatorCode *opcode)
{
assert(opcode);

if (!is_valid(opcode))
{
std::ostringstream oss;
oss << "(invalid)";
return oss.str();
}

if (is_custom(opcode))
{
if (!opcode->custom_code())
return "(invalid custom)";

std::string custom_op = "CUSTOM(";
custom_op += opcode->custom_code()->c_str();
custom_op += ")";
return custom_op;
}

::tflite::BuiltinOperator code = builtin_code_neutral(opcode);
return ::tflite::EnumNameBuiltinOperator(code);
}

const char *tensor_type(const ::tflite::Tensor *tensor)
{
return ::tflite::EnumNameTensorType(tensor->type());
}

const char *tensor_name(const ::tflite::Tensor *tensor)
{
static const char *kEmptyTensorName = "(noname)";

auto name = tensor->name();
if (name)
return name->c_str();

return kEmptyTensorName;
}

} // namespace tflite
} // namespace mio
Loading

0 comments on commit b0cfbe0

Please sign in to comment.