Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mio] Introduce mio-circle06 #11423

Merged
merged 1 commit into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions compiler/mio-circle06/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
nnas_find_package(FlatBuffers EXACT 2.0 QUIET)

if(NOT FlatBuffers_FOUND)
message(STATUS "mio-circle06 skip: FlatBuffers 2.0 NOT FOUND")
return()
endif(NOT FlatBuffers_FOUND)

message(STATUS "Build mio-circle06: TRUE")

# TODO Find a better way
# TODO use nnpackage
# set(SCHEMA_FILE "${NNAS_PROJECT_SOURCE_DIR}/nnpackage/schema/circle_schema.fbs")
set(SCHEMA_FILE "${NNAS_PROJECT_SOURCE_DIR}/res/CircleSchema/0.6/circle_schema.fbs")

# NOTE Copy circle_schema.fbs as schema.fbs to generate "schema_generated.fbs" instead of "circle_schema_generated.fbs"
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_circle06
OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/gen/mio/circle"
INCLUDE_DIR "${CMAKE_CURRENT_BINARY_DIR}/gen"
SCHEMA_DIR "${CMAKE_CURRENT_BINARY_DIR}"
SCHEMA_FILES "schema.fbs"
)

# This example shows how to use "mio-circle06" library
add_executable(mio_circle06_example example.cpp)
target_link_libraries(mio_circle06_example mio_circle06)

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

add_library(mio_circle06_helper STATIC ${SOURCES})
set_target_properties(mio_circle06_helper PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_include_directories(mio_circle06_helper PRIVATE src)
target_include_directories(mio_circle06_helper PUBLIC include)
target_link_libraries(mio_circle06_helper mio_circle06)

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

nnas_find_package(GTest REQUIRED)

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

Let's make it easy to read and write Circle models.
41 changes: 41 additions & 0 deletions compiler/mio-circle06/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-circle06"
//
#include <mio/circle/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 (!circle::VerifyModelBuffer(verifier))
{
std::cout << "Fail" << std::endl;
return 255;
}

std::cout << "Pass" << std::endl;
return 0;
}
54 changes: 54 additions & 0 deletions compiler/mio-circle06/include/mio_circle/Helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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_CIRCLE06_HELPER_H__
#define __MIO_CIRCLE06_HELPER_H__

#include <mio/circle/schema_generated.h>

#include <vector>

namespace mio
{
namespace circle
{

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

template <typename T> std::vector<T> as_index_vector(const flatbuffers::Vector<T> *flat_array)
{
if (flat_array == nullptr)
{
throw std::runtime_error("flat array is nullptr");
}

std::vector<T> ret(flat_array->Length());
for (uint32_t i = 0; i < flat_array->Length(); i++)
{
ret[i] = flat_array->Get(i);
}
return ret;
}

} // namespace circle
} // namespace mio

#endif // __MIO_CIRCLE06_HELPER_H__
101 changes: 101 additions & 0 deletions compiler/mio-circle06/include/mio_circle/Reader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* 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_CIRCLE06_READER_H__
#define __MIO_CIRCLE06_READER_H__

#include <mio/circle/schema_generated.h>

#include <map>
#include <string>
#include <vector>

// NOTE Reader class originated from circledump and for circle-tensordump
// where this class has more work to be done for stability
// as the tools are for developers not customores.

namespace mio
{
namespace circle
{

/**
* @brief Loads Circle file and provides helpers to access attributes
*/
class Reader
{
private:
using CircleSubGraphs_t = flatbuffers::Vector<flatbuffers::Offset<::circle::SubGraph>>;
using CircleBuffers_t = flatbuffers::Vector<flatbuffers::Offset<::circle::Buffer>>;
using CircleTensors_t = flatbuffers::Vector<flatbuffers::Offset<::circle::Tensor>>;
using CircleOperators_t = flatbuffers::Vector<flatbuffers::Offset<::circle::Operator>>;
using CircleMetadata_t = flatbuffers::Vector<flatbuffers::Offset<::circle::Metadata>>;
using CircleSignatureDef_t = flatbuffers::Vector<flatbuffers::Offset<::circle::SignatureDef>>;

public:
Reader(const ::circle::Model *model);

Reader() = delete;

public:
uint32_t version() const { return _version; }

const std::vector<const ::circle::OperatorCode *> &opcodes() { return _op_codes; }
const CircleBuffers_t *buffers() { return _buffers; }
const CircleTensors_t *tensors() { return _tensors; }
const CircleOperators_t *operators() { return _operators; }
const std::vector<int32_t> &inputs() const { return _inputs; }
const std::vector<int32_t> &outputs() const { return _outputs; }
const ::circle::DataFormat &data_format() const { return _data_format; }
const CircleMetadata_t *metadata() const { return _metadata; }
const CircleSignatureDef_t *signature_defs() const { return _signature_defs; }

uint32_t num_subgraph() const { return _subgraphs->Length(); }

size_t buffer_info(uint32_t buf_idx, const uint8_t **buff_data);
::circle::BuiltinOperator builtin_code(const ::circle::Operator *op) const;
std::string opcode_name(const ::circle::Operator *op) const;
std::vector<int32_t> outputs(const ::circle::Operator *op) const;
std::string tensor_name(const ::circle::Tensor *tensor) const;
std::string tensor_dtype(const ::circle::Tensor *tensor) const;

public:
bool select_subgraph(uint32_t subgraph);
const std::string &subgraph_name(void) const { return _subgraph_name; }
uint32_t subgraph_index(void) const { return _subgraph_index; }

private:
uint32_t _version;

const CircleSubGraphs_t *_subgraphs{nullptr};
const CircleBuffers_t *_buffers{nullptr};
const CircleTensors_t *_tensors{nullptr};
const CircleOperators_t *_operators{nullptr};
const CircleMetadata_t *_metadata{nullptr};
const CircleSignatureDef_t *_signature_defs{nullptr};

uint32_t _subgraph_index = 0;
std::string _subgraph_name;
std::vector<const ::circle::OperatorCode *> _op_codes;
std::vector<int32_t> _inputs;
std::vector<int32_t> _outputs;
::circle::DataFormat _data_format = ::circle::DataFormat::DataFormat_CHANNELS_FIRST;
};

} // namespace circle
} // namespace mio

#endif // __MIO_CIRCLE06_READER_H__
110 changes: 110 additions & 0 deletions compiler/mio-circle06/src/Helper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* 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.
*/

#include "mio_circle/Helper.h"

#include <algorithm>
#include <sstream>

namespace mio
{
namespace circle
{

/**
* This will provide v3/v3a/v3b format neutral BuiltinOperator
* NOTE circle has minus value opcode (252~254 as uint8_t)
* we cannot use std::max() like tflite as deprecated_builtin_code can be
* minus and builtin_code being 0 for v0.3 files.
*/
::circle::BuiltinOperator builtin_code_neutral(const ::circle::OperatorCode *opcode)
{
assert(opcode != nullptr);
if (opcode->deprecated_builtin_code() == 127)
{
assert(opcode->builtin_code() >= 127);
return opcode->builtin_code();
}
// There was no 255(-1) value in v0.3
assert(opcode->deprecated_builtin_code() != -1);
return static_cast<::circle::BuiltinOperator>(opcode->deprecated_builtin_code());
}

bool is_valid(const ::circle::OperatorCode *opcode)
{
// Valid Range : BuiltinOperator_MIN <= deprecated_builtin_code <= 127
const int8_t deprecated_builtin_code = opcode->deprecated_builtin_code();
if (deprecated_builtin_code < ::circle::BuiltinOperator_MIN)
return false;
// There was no 255(-1) value in v0.3
if (deprecated_builtin_code == -1)
return false;

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

return true;
}

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

std::string opcode_name(const ::circle::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;
}

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

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

const char *tensor_name(const ::circle::Tensor *tensor)
{
if (tensor->name() == nullptr || std::string(tensor->name()->c_str()).empty())
return "(noname)";

return tensor->name()->c_str();
}

} // namespace circle
} // namespace mio
Loading