diff options
Diffstat (limited to 'compiler/tflchef/tflite')
70 files changed, 3536 insertions, 0 deletions
diff --git a/compiler/tflchef/tflite/CMakeLists.txt b/compiler/tflchef/tflite/CMakeLists.txt new file mode 100644 index 000000000..645c16144 --- /dev/null +++ b/compiler/tflchef/tflite/CMakeLists.txt @@ -0,0 +1,9 @@ +file(GLOB_RECURSE SOURCES "src/*.cpp") + +add_library(tflchef_tflite STATIC ${SOURCES}) +target_include_directories(tflchef_tflite PUBLIC include) +target_include_directories(tflchef_tflite PRIVATE src) +target_link_libraries(tflchef_tflite tflchef_proto) +target_link_libraries(tflchef_tflite mio_tflite) +target_link_libraries(tflchef_tflite stdex) +target_link_libraries(tflchef_tflite cwrap) diff --git a/compiler/tflchef/tflite/include/tflchef/RawModel.h b/compiler/tflchef/tflite/include/tflchef/RawModel.h new file mode 100644 index 000000000..a8c8fefb7 --- /dev/null +++ b/compiler/tflchef/tflite/include/tflchef/RawModel.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2018 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 __RAW_MODEL_H__ +#define __RAW_MODEL_H__ + +#include <mio/tflite/schema_generated.h> + +namespace tflchef +{ + +struct RawModel +{ + virtual ~RawModel() = default; + + virtual const ::tflite::Model *model(void) const = 0; +}; + +/** + * @brief Load TensorFlow Lite model (as a RawModel) from a given path + * + * @note May return a nullptr + */ +std::unique_ptr<RawModel> load_tflite(const std::string &path); + +} // namespace tflchef + +#endif // __RAW_MODEL_H__ diff --git a/compiler/tflchef/tflite/include/tflchef/RecipeChef.h b/compiler/tflchef/tflite/include/tflchef/RecipeChef.h new file mode 100644 index 000000000..2d292c3d5 --- /dev/null +++ b/compiler/tflchef/tflite/include/tflchef/RecipeChef.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2018 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 __RECIPE_CHEF_H__ +#define __RECIPE_CHEF_H__ + +#include <mio/tflite/schema_generated.h> +#include <tflchef.pb.h> + +#include <memory> +#include <string> + +namespace tflchef +{ + +/** + * @brief Create ModelRecipe from tflite::Model + */ +std::unique_ptr<ModelRecipe> generate_recipe(const tflite::Model *model); + +/** + * @brief Write ModelRecipe to file with given name + */ +bool write_recipe(const std::string &filename, std::unique_ptr<ModelRecipe> &recipe); + +} // namespace tflchef + +#endif // __RECIPE_CHEF_H__ diff --git a/compiler/tflchef/tflite/src/Convert.cpp b/compiler/tflchef/tflite/src/Convert.cpp new file mode 100644 index 000000000..dc60e0087 --- /dev/null +++ b/compiler/tflchef/tflite/src/Convert.cpp @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2018 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 "Convert.h" + +namespace tflchef +{ + +tflchef::TensorType as_tflchef_type(const tflite::TensorType type) +{ + switch (type) + { + case tflite::TensorType_FLOAT32: + return tflchef::FLOAT32; + case tflite::TensorType_INT32: + return tflchef::INT32; + case tflite::TensorType_INT64: + return tflchef::INT64; + case tflite::TensorType_UINT8: + return tflchef::UINT8; + case tflite::TensorType_BOOL: + return tflchef::BOOL; + // TODO handle other types + // TensorType_FLOAT16 + // TensorType_STRING + // TensorType_INT16 + // TensorType_COMPLEX64 + default: + throw std::runtime_error{"unsupported tensor type"}; + } +} + +tflchef::Activation as_tflchef_activation(const tflite::ActivationFunctionType type) +{ + switch (type) + { + case tflite::ActivationFunctionType_NONE: + return tflchef::NONE; + case tflite::ActivationFunctionType_RELU: + return tflchef::RELU; + case tflite::ActivationFunctionType_RELU6: + return tflchef::RELU6; + // TODO handle other types + // ActivationFunctionType_RELU_N1_TO_1 + // ActivationFunctionType_TANH + // ActivationFunctionType_SIGN_BIT + default: + throw std::runtime_error{"unsupported activation type"}; + } +} + +tflchef::Padding as_tflchef_padding(const tflite::Padding padding) +{ + switch (padding) + { + case tflite::Padding_SAME: + return tflchef::SAME; + case tflite::Padding_VALID: + return tflchef::VALID; + default: + throw std::runtime_error{"unsupported padding"}; + } +} + +} // namespace tflchef diff --git a/compiler/tflchef/tflite/src/Convert.h b/compiler/tflchef/tflite/src/Convert.h new file mode 100644 index 000000000..8623e7b78 --- /dev/null +++ b/compiler/tflchef/tflite/src/Convert.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2018 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 __CONVERT_H__ +#define __CONVERT_H__ + +#include <mio/tflite/schema_generated.h> + +#include <tflchef.pb.h> + +namespace tflchef +{ + +tflchef::TensorType as_tflchef_type(const tflite::TensorType type); +tflchef::Activation as_tflchef_activation(const tflite::ActivationFunctionType type); +tflchef::Padding as_tflchef_padding(const tflite::Padding padding); + +/** + * @brief extract buffer data to std::vector<DT> + */ +template <typename DT> std::vector<DT> extract_buffer(const tflite::Buffer *buffer) +{ + auto buffer_length = buffer->data()->size(); + auto num_elements = buffer_length / sizeof(DT); + std::vector<DT> result(num_elements); + std::memcpy(result.data(), buffer->data()->data(), buffer_length); + return result; +} + +template <typename T> std::vector<T> as_index_vector(const flatbuffers::Vector<T> *flat_array) +{ + 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 tflchef + +#endif // __CONVERT_H__ diff --git a/compiler/tflchef/tflite/src/Op/Abs.cpp b/compiler/tflchef/tflite/src/Op/Abs.cpp new file mode 100644 index 000000000..7d769e344 --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/Abs.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2020 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 "Abs.h" + +#include "Convert.h" + +namespace tflchef +{ + +void TFliteOpAbs::filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + // Nothing to do with filler +} + +tflchef::Operation *TFliteOpAbs::build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + auto operation = model_recipe->add_operation(); + + operation->set_type("Abs"); + + return operation; +} + +} // namespace tflchef diff --git a/compiler/tflchef/tflite/src/Op/Abs.h b/compiler/tflchef/tflite/src/Op/Abs.h new file mode 100644 index 000000000..d99b0d593 --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/Abs.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2020 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 __TFLITE_OP_ABS_H__ +#define __TFLITE_OP_ABS_H__ + +#include "TFliteOpChef.h" + +namespace tflchef +{ + +/** + * @brief tflchef operator builder for abs + */ +class TFliteOpAbs : public TFliteOpChef +{ +public: + void filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; + tflchef::Operation *build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; +}; + +} // namespace tflchef + +#endif // __TFLITE_OP_ABS_H__ diff --git a/compiler/tflchef/tflite/src/Op/Add.cpp b/compiler/tflchef/tflite/src/Op/Add.cpp new file mode 100644 index 000000000..7e669ecc9 --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/Add.cpp @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2020 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 "Add.h" + +#include "Convert.h" + +namespace tflchef +{ + +void TFliteOpAdd::filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + // Nothing to do with filler +} + +tflchef::Operation *TFliteOpAdd::build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + auto op_params = op->builtin_options_as_AddOptions(); + assert(op_params != nullptr); + + auto operation = model_recipe->add_operation(); + + operation->set_type("Add"); + + auto op_options = operation->mutable_add_options(); + + op_options->set_activation(as_tflchef_activation(op_params->fused_activation_function())); + + return operation; +} + +} // namespace tflchef diff --git a/compiler/tflchef/tflite/src/Op/Add.h b/compiler/tflchef/tflite/src/Op/Add.h new file mode 100644 index 000000000..49d945f8b --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/Add.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2020 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 __TFLITE_OP_ADD_H__ +#define __TFLITE_OP_ADD_H__ + +#include "TFliteOpChef.h" + +namespace tflchef +{ + +/** + * @brief tflchef operator builder for add + */ +class TFliteOpAdd : public TFliteOpChef +{ +public: + void filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; + tflchef::Operation *build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; +}; + +} // namespace tflchef + +#endif // __TFLITE_OP_ADD_H__ diff --git a/compiler/tflchef/tflite/src/Op/ArgMax.cpp b/compiler/tflchef/tflite/src/Op/ArgMax.cpp new file mode 100644 index 000000000..f4d1c5e66 --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/ArgMax.cpp @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2020 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 "ArgMax.h" + +#include "Convert.h" + +namespace tflchef +{ + +void TFliteOpArgMax::filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + // filler for second input, argmax/dim + const auto &inputs = *op->inputs(); + + const tflite::Tensor *dim_tensor = import->tensors()->Get(inputs[1]); + assert(dim_tensor->type() == tflite::TensorType::TensorType_INT32); + const tflite::Buffer *buffer = import->buffers()->Get(dim_tensor->buffer()); + auto vec = extract_buffer<int32_t>(buffer); + import->set_tensor_filler(inputs[1], vec); +} + +tflchef::Operation *TFliteOpArgMax::build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + auto op_params = op->builtin_options_as_ArgMaxOptions(); + assert(op_params != nullptr); + + auto operation = model_recipe->add_operation(); + + operation->set_type("ArgMax"); + + auto op_options = operation->mutable_argmax_options(); + + op_options->set_output_type(as_tflchef_type(op_params->output_type())); + + return operation; +} + +} // namespace tflchef diff --git a/compiler/tflchef/tflite/src/Op/ArgMax.h b/compiler/tflchef/tflite/src/Op/ArgMax.h new file mode 100644 index 000000000..30068ecf2 --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/ArgMax.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2020 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 __TFLITE_OP_ARGMAX_H__ +#define __TFLITE_OP_ARGMAX_H__ + +#include "TFliteOpChef.h" + +namespace tflchef +{ + +/** + * @brief tflchef operator builder for ArgMax + */ +class TFliteOpArgMax : public TFliteOpChef +{ +public: + void filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; + tflchef::Operation *build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; +}; + +} // namespace tflchef + +#endif // __TFLITE_OP_ARGMAX_H__ diff --git a/compiler/tflchef/tflite/src/Op/AveragePool2D.cpp b/compiler/tflchef/tflite/src/Op/AveragePool2D.cpp new file mode 100644 index 000000000..1f269e45e --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/AveragePool2D.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2018 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 "AveragePool2D.h" + +#include "Convert.h" + +namespace tflchef +{ + +void TFliteOpAveragePool2D::filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + // Nothing to do with filler +} + +tflchef::Operation *TFliteOpAveragePool2D::build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + auto op_params = op->builtin_options_as_Pool2DOptions(); + assert(op_params != nullptr); + + auto operation = model_recipe->add_operation(); + + operation->set_type("AveragePool2D"); + + auto op_options = operation->mutable_averagepool2d_options(); + + op_options->set_padding(as_tflchef_padding(op_params->padding())); + op_options->set_stride_h(op_params->stride_h()); + op_options->set_stride_w(op_params->stride_w()); + op_options->set_filter_height(op_params->filter_height()); + op_options->set_filter_width(op_params->filter_width()); + op_options->set_activation(as_tflchef_activation(op_params->fused_activation_function())); + + return operation; +} + +} // namespace tflchef diff --git a/compiler/tflchef/tflite/src/Op/AveragePool2D.h b/compiler/tflchef/tflite/src/Op/AveragePool2D.h new file mode 100644 index 000000000..f9e9fb254 --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/AveragePool2D.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2018 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 __TFLITE_OP_AVERAGEPOOL2D_H__ +#define __TFLITE_OP_AVERAGEPOOL2D_H__ + +#include "TFliteOpChef.h" + +namespace tflchef +{ + +/** + * @brief tflchef operator builder for AveragePool2D + */ +class TFliteOpAveragePool2D : public TFliteOpChef +{ +public: + void filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; + tflchef::Operation *build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; +}; + +} // namespace tflchef + +#endif // __TFLITE_OP_AVERAGEPOOL2D_H__ diff --git a/compiler/tflchef/tflite/src/Op/BatchToSpaceND.cpp b/compiler/tflchef/tflite/src/Op/BatchToSpaceND.cpp new file mode 100644 index 000000000..d5d9606d1 --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/BatchToSpaceND.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2020 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 "BatchToSpaceND.h" + +#include "Convert.h" + +namespace tflchef +{ + +void TFliteOpBatchToSpaceND::filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + // filler for second, third input + const auto &inputs = *op->inputs(); + + const tflite::Tensor *tensor = import->tensors()->Get(inputs[1]); + assert(tensor->type() == tflite::TensorType::TensorType_INT32); + const tflite::Buffer *buffer = import->buffers()->Get(tensor->buffer()); + auto vec = extract_buffer<int32_t>(buffer); + import->set_tensor_filler(inputs[1], vec); + + tensor = import->tensors()->Get(inputs[2]); + assert(tensor->type() == tflite::TensorType::TensorType_INT32); + buffer = import->buffers()->Get(tensor->buffer()); + vec = extract_buffer<int32_t>(buffer); + import->set_tensor_filler(inputs[2], vec); +} + +tflchef::Operation *TFliteOpBatchToSpaceND::build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + auto operation = model_recipe->add_operation(); + + operation->set_type("BatchToSpaceND"); + + return operation; +} + +} // namespace tflchef diff --git a/compiler/tflchef/tflite/src/Op/BatchToSpaceND.h b/compiler/tflchef/tflite/src/Op/BatchToSpaceND.h new file mode 100644 index 000000000..ae2114c97 --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/BatchToSpaceND.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2020 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 __TFLITE_OP_BATCHTOSPACEND_H__ +#define __TFLITE_OP_BATCHTOSPACEND_H__ + +#include "TFliteOpChef.h" + +namespace tflchef +{ + +/** + * @brief tflchef operator builder for BatchToSpaceND + */ +class TFliteOpBatchToSpaceND : public TFliteOpChef +{ +public: + void filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; + tflchef::Operation *build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; +}; + +} // namespace tflchef + +#endif // __TFLITE_OP_BATCHTOSPACEND_H__ diff --git a/compiler/tflchef/tflite/src/Op/Concatenation.cpp b/compiler/tflchef/tflite/src/Op/Concatenation.cpp new file mode 100644 index 000000000..126402f14 --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/Concatenation.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2018 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 "Concatenation.h" + +#include "Convert.h" + +namespace tflchef +{ + +void TFliteOpConcatenation::filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + // Nothing to do with filler +} + +tflchef::Operation *TFliteOpConcatenation::build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + auto op_params = op->builtin_options_as_ConcatenationOptions(); + assert(op_params != nullptr); + + auto operation = model_recipe->add_operation(); + + operation->set_type("Concatenation"); + + auto op_options = operation->mutable_concatenation_options(); + + op_options->set_axis(op_params->axis()); + op_options->set_activation(as_tflchef_activation(op_params->fused_activation_function())); + + return operation; +} + +} // namespace tflchef diff --git a/compiler/tflchef/tflite/src/Op/Concatenation.h b/compiler/tflchef/tflite/src/Op/Concatenation.h new file mode 100644 index 000000000..4a7ea5791 --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/Concatenation.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2018 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 __TFLITE_OP_CONCATENATION_H__ +#define __TFLITE_OP_CONCATENATION_H__ + +#include "TFliteOpChef.h" + +namespace tflchef +{ + +/** + * @brief tflchef operator builder for Concatenation + */ +class TFliteOpConcatenation : public TFliteOpChef +{ +public: + void filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; + tflchef::Operation *build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; +}; + +} // namespace tflchef + +#endif // __TFLITE_OP_CONCATENATION_H__ diff --git a/compiler/tflchef/tflite/src/Op/Conv2D.cpp b/compiler/tflchef/tflite/src/Op/Conv2D.cpp new file mode 100644 index 000000000..5d48ee24f --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/Conv2D.cpp @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2018 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 "Conv2D.h" + +#include "Convert.h" + +namespace tflchef +{ + +void TFliteOpConv2D::filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + const std::vector<int32_t> &inputs = as_index_vector(op->inputs()); + + bool hasBias = (inputs.size() == 3); + assert(inputs.size() == 2 || hasBias); + + import->set_tensor_filler(inputs.at(1)); // kernel + if (hasBias) + import->set_tensor_filler(inputs.at(2)); // bias +} + +tflchef::Operation *TFliteOpConv2D::build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + auto op_params = op->builtin_options_as_Conv2DOptions(); + assert(op_params != nullptr); + + auto operation = model_recipe->add_operation(); + + operation->set_type("Conv2D"); + + auto op_options = operation->mutable_conv2d_options(); + + op_options->set_activation(as_tflchef_activation(op_params->fused_activation_function())); + op_options->set_stride_h(op_params->stride_h()); + op_options->set_stride_w(op_params->stride_w()); + op_options->set_padding(as_tflchef_padding(op_params->padding())); + // TODO support dilation + + return operation; +} + +} // namespace tflchef diff --git a/compiler/tflchef/tflite/src/Op/Conv2D.h b/compiler/tflchef/tflite/src/Op/Conv2D.h new file mode 100644 index 000000000..0216e9ce9 --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/Conv2D.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2018 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 __TFLITE_OP_CONV2D_H__ +#define __TFLITE_OP_CONV2D_H__ + +#include "TFliteOpChef.h" + +namespace tflchef +{ + +/** + * @brief tflchef operator builder for Conv2D + */ +class TFliteOpConv2D : public TFliteOpChef +{ +public: + void filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; + tflchef::Operation *build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; +}; + +} // namespace tflchef + +#endif // __TFLITE_OP_CONV2D_H__ diff --git a/compiler/tflchef/tflite/src/Op/Cos.cpp b/compiler/tflchef/tflite/src/Op/Cos.cpp new file mode 100644 index 000000000..9f2c49d49 --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/Cos.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2020 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 "Cos.h" + +#include "Convert.h" + +namespace tflchef +{ + +void TFliteOpCos::filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + // Nothing to do with filler +} + +tflchef::Operation *TFliteOpCos::build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + auto operation = model_recipe->add_operation(); + + operation->set_type("Cos"); + + return operation; +} + +} // namespace tflchef diff --git a/compiler/tflchef/tflite/src/Op/Cos.h b/compiler/tflchef/tflite/src/Op/Cos.h new file mode 100644 index 000000000..8f3dbe3a6 --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/Cos.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2020 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 __TFLITE_OP_COS_H__ +#define __TFLITE_OP_COS_H__ + +#include "TFliteOpChef.h" + +namespace tflchef +{ + +/** + * @brief tflchef operator builder for Cos + */ +class TFliteOpCos : public TFliteOpChef +{ +public: + void filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; + tflchef::Operation *build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; +}; + +} // namespace tflchef + +#endif // __TFLITE_OP_COS_H__ diff --git a/compiler/tflchef/tflite/src/Op/DepthwiseConv2D.cpp b/compiler/tflchef/tflite/src/Op/DepthwiseConv2D.cpp new file mode 100644 index 000000000..b19f9330f --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/DepthwiseConv2D.cpp @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2018 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 "DepthwiseConv2D.h" + +#include "Convert.h" + +namespace tflchef +{ + +void TFliteOpDepthwiseConv2D::filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + const std::vector<int32_t> &inputs = as_index_vector(op->inputs()); + + bool hasBias = (inputs.size() == 3); + assert(inputs.size() == 2 || hasBias); + + import->set_tensor_filler(inputs.at(1)); // kernel + if (hasBias) + import->set_tensor_filler(inputs.at(2)); // bias +} + +tflchef::Operation *TFliteOpDepthwiseConv2D::build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + auto op_params = op->builtin_options_as_DepthwiseConv2DOptions(); + assert(op_params != nullptr); + + auto operation = model_recipe->add_operation(); + + operation->set_type("DepthwiseConv2D"); + + auto op_options = operation->mutable_depthwiseconv2d_options(); + + op_options->set_activation(as_tflchef_activation(op_params->fused_activation_function())); + op_options->set_stride_h(op_params->stride_h()); + op_options->set_stride_w(op_params->stride_w()); + op_options->set_depth_multiplier(op_params->depth_multiplier()); + // TODO support dilation + // op_params->dilation_w_factor() + // op_params->dilation_h_factor() + op_options->set_padding(as_tflchef_padding(op_params->padding())); + + return operation; +} + +} // namespace tflchef diff --git a/compiler/tflchef/tflite/src/Op/DepthwiseConv2D.h b/compiler/tflchef/tflite/src/Op/DepthwiseConv2D.h new file mode 100644 index 000000000..c172536b4 --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/DepthwiseConv2D.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2018 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 __TFLITE_OP_DEPTHWISECONV2D_H__ +#define __TFLITE_OP_DEPTHWISECONV2D_H__ + +#include "TFliteOpChef.h" + +namespace tflchef +{ + +/** + * @brief tflchef operator builder for DepthwiseConv2D + */ +class TFliteOpDepthwiseConv2D : public TFliteOpChef +{ +public: + void filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; + tflchef::Operation *build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; +}; + +} // namespace tflchef + +#endif // __TFLITE_OP_DEPTHWISECONV2D_H__ diff --git a/compiler/tflchef/tflite/src/Op/Div.cpp b/compiler/tflchef/tflite/src/Op/Div.cpp new file mode 100644 index 000000000..0fd87dc05 --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/Div.cpp @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2018 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 "Div.h" + +#include "Convert.h" + +namespace tflchef +{ + +void TFliteOpDiv::filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + // Nothing to do with filler +} + +tflchef::Operation *TFliteOpDiv::build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + auto op_params = op->builtin_options_as_DivOptions(); + assert(op_params != nullptr); + + auto operation = model_recipe->add_operation(); + + operation->set_type("Div"); + + auto op_options = operation->mutable_div_options(); + + op_options->set_activation(as_tflchef_activation(op_params->fused_activation_function())); + + return operation; +} + +} // namespace tflchef diff --git a/compiler/tflchef/tflite/src/Op/Div.h b/compiler/tflchef/tflite/src/Op/Div.h new file mode 100644 index 000000000..254a4cd99 --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/Div.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2018 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 __TFLITE_OP_DIV_H__ +#define __TFLITE_OP_DIV_H__ + +#include "TFliteOpChef.h" + +namespace tflchef +{ + +/** + * @brief tflchef operator builder for division + */ +class TFliteOpDiv : public TFliteOpChef +{ +public: + void filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; + tflchef::Operation *build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; +}; + +} // namespace tflchef + +#endif // __TFLITE_OP_DIV_H__ diff --git a/compiler/tflchef/tflite/src/Op/Equal.cpp b/compiler/tflchef/tflite/src/Op/Equal.cpp new file mode 100644 index 000000000..a51586228 --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/Equal.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2020 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 "Equal.h" + +#include "Convert.h" + +namespace tflchef +{ + +void TFliteOpEqual::filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + // Nothing to do with filler +} + +tflchef::Operation *TFliteOpEqual::build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + auto operation = model_recipe->add_operation(); + + operation->set_type("Equal"); + + return operation; +} + +} // namespace tflchef diff --git a/compiler/tflchef/tflite/src/Op/Equal.h b/compiler/tflchef/tflite/src/Op/Equal.h new file mode 100644 index 000000000..fd4b40001 --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/Equal.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2020 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 __TFLITE_OP_EQUAL_H__ +#define __TFLITE_OP_EQUAL_H__ + +#include "TFliteOpChef.h" + +namespace tflchef +{ + +/** + * @brief tflchef operator builder for Equal + */ +class TFliteOpEqual : public TFliteOpChef +{ +public: + void filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; + tflchef::Operation *build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; +}; + +} // namespace tflchef + +#endif // __TFLITE_OP_EQUAL_H__ diff --git a/compiler/tflchef/tflite/src/Op/Exp.cpp b/compiler/tflchef/tflite/src/Op/Exp.cpp new file mode 100644 index 000000000..f715da6ef --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/Exp.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2020 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 "Exp.h" + +#include "Convert.h" + +namespace tflchef +{ + +void TFliteOpExp::filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + // Nothing to do with filler +} + +tflchef::Operation *TFliteOpExp::build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + auto operation = model_recipe->add_operation(); + + operation->set_type("Exp"); + + return operation; +} + +} // namespace tflchef diff --git a/compiler/tflchef/tflite/src/Op/Exp.h b/compiler/tflchef/tflite/src/Op/Exp.h new file mode 100644 index 000000000..5ff3ddc8b --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/Exp.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2020 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 __TFLITE_OP_EXP_H__ +#define __TFLITE_OP_EXP_H__ + +#include "TFliteOpChef.h" + +namespace tflchef +{ + +/** + * @brief tflchef operator builder for Exp + */ +class TFliteOpExp : public TFliteOpChef +{ +public: + void filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; + tflchef::Operation *build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; +}; + +} // namespace tflchef + +#endif // __TFLITE_OP_EXP_H__ diff --git a/compiler/tflchef/tflite/src/Op/FloorDiv.cpp b/compiler/tflchef/tflite/src/Op/FloorDiv.cpp new file mode 100644 index 000000000..492c6941f --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/FloorDiv.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2020 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 "FloorDiv.h" + +#include "Convert.h" + +namespace tflchef +{ + +void TFliteOpFloorDiv::filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + // Nothing to do with filler +} + +tflchef::Operation *TFliteOpFloorDiv::build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + auto operation = model_recipe->add_operation(); + + operation->set_type("FloorDiv"); + + return operation; +} + +} // namespace tflchef diff --git a/compiler/tflchef/tflite/src/Op/FloorDiv.h b/compiler/tflchef/tflite/src/Op/FloorDiv.h new file mode 100644 index 000000000..5d049a668 --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/FloorDiv.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2020 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 __TFLITE_OP_FLOORDIV_H__ +#define __TFLITE_OP_FLOORDIV_H__ + +#include "TFliteOpChef.h" + +namespace tflchef +{ + +/** + * @brief tflchef operator builder for floor division + */ +class TFliteOpFloorDiv : public TFliteOpChef +{ +public: + void filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; + tflchef::Operation *build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; +}; + +} // namespace tflchef + +#endif // __TFLITE_OP_FLOORDIV_H__ diff --git a/compiler/tflchef/tflite/src/Op/FullyConnected.cpp b/compiler/tflchef/tflite/src/Op/FullyConnected.cpp new file mode 100644 index 000000000..4291c844b --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/FullyConnected.cpp @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2019 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 "FullyConnected.h" + +#include "Convert.h" + +namespace tflchef +{ + +void TFliteOpFullyConnected::filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + // Nothing to do with filler +} + +tflchef::Operation *TFliteOpFullyConnected::build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + auto op_params = op->builtin_options_as_FullyConnectedOptions(); + assert(op_params != nullptr); + + auto operation = model_recipe->add_operation(); + + operation->set_type("FullyConnected"); + + auto op_options = operation->mutable_fullyconnected_options(); + + op_options->set_activation(as_tflchef_activation(op_params->fused_activation_function())); + + return operation; +} + +} // namespace tflchef diff --git a/compiler/tflchef/tflite/src/Op/FullyConnected.h b/compiler/tflchef/tflite/src/Op/FullyConnected.h new file mode 100644 index 000000000..8fbe1f3ed --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/FullyConnected.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2019 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 __TFLITE_OP_FULLYCONNECTED_H__ +#define __TFLITE_OP_FULLYCONNECTED_H__ + +#include "TFliteOpChef.h" + +namespace tflchef +{ + +/** + * @brief tflchef operator builder for FullyConnected + */ +class TFliteOpFullyConnected : public TFliteOpChef +{ +public: + void filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; + tflchef::Operation *build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; +}; + +} // namespace tflchef + +#endif // __TFLITE_OP_FULLYCONNECTED_H__ diff --git a/compiler/tflchef/tflite/src/Op/LogicalNot.cpp b/compiler/tflchef/tflite/src/Op/LogicalNot.cpp new file mode 100644 index 000000000..ecd5b903c --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/LogicalNot.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2020 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 "LogicalNot.h" + +#include "Convert.h" + +namespace tflchef +{ + +void TFliteOpLogicalNot::filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + // Nothing to do with filler +} + +tflchef::Operation *TFliteOpLogicalNot::build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + auto operation = model_recipe->add_operation(); + + operation->set_type("LogicalNot"); + + return operation; +} + +} // namespace tflchef diff --git a/compiler/tflchef/tflite/src/Op/LogicalNot.h b/compiler/tflchef/tflite/src/Op/LogicalNot.h new file mode 100644 index 000000000..b75d33554 --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/LogicalNot.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2020 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 __TFLITE_OP_LOGICALNOT_H__ +#define __TFLITE_OP_LOGICALNOT_H__ + +#include "TFliteOpChef.h" + +namespace tflchef +{ + +/** + * @brief tflchef operator builder for LogicalNot + */ +class TFliteOpLogicalNot : public TFliteOpChef +{ +public: + void filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; + tflchef::Operation *build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; +}; + +} // namespace tflchef + +#endif // __TFLITE_OP_LOGICALNOT_H__ diff --git a/compiler/tflchef/tflite/src/Op/LogicalOr.cpp b/compiler/tflchef/tflite/src/Op/LogicalOr.cpp new file mode 100644 index 000000000..b91f4cfca --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/LogicalOr.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2020 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 "LogicalOr.h" + +#include "Convert.h" + +namespace tflchef +{ + +void TFliteOpLogicalOr::filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + // Nothing to do with filler +} + +tflchef::Operation *TFliteOpLogicalOr::build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + auto operation = model_recipe->add_operation(); + + operation->set_type("LogicalOr"); + + return operation; +} + +} // namespace tflchef diff --git a/compiler/tflchef/tflite/src/Op/LogicalOr.h b/compiler/tflchef/tflite/src/Op/LogicalOr.h new file mode 100644 index 000000000..5331a0d65 --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/LogicalOr.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2020 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 __TFLITE_OP_LOGICALOR_H__ +#define __TFLITE_OP_LOGICALOR_H__ + +#include "TFliteOpChef.h" + +namespace tflchef +{ + +/** + * @brief tflchef operator builder for LogicalOr + */ +class TFliteOpLogicalOr : public TFliteOpChef +{ +public: + void filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; + tflchef::Operation *build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; +}; + +} // namespace tflchef + +#endif // __TFLITE_OP_LOGICALOR_H__ diff --git a/compiler/tflchef/tflite/src/Op/MaxPool2D.cpp b/compiler/tflchef/tflite/src/Op/MaxPool2D.cpp new file mode 100644 index 000000000..1366366ac --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/MaxPool2D.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2018 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 "MaxPool2D.h" + +#include "Convert.h" + +namespace tflchef +{ + +void TFliteOpMaxPool2D::filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + // Nothing to do with filler +} + +tflchef::Operation *TFliteOpMaxPool2D::build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + auto op_params = op->builtin_options_as_Pool2DOptions(); + assert(op_params != nullptr); + + auto operation = model_recipe->add_operation(); + + operation->set_type("MaxPool2D"); + + auto op_options = operation->mutable_maxpool2d_options(); + + op_options->set_padding(as_tflchef_padding(op_params->padding())); + op_options->set_stride_h(op_params->stride_h()); + op_options->set_stride_w(op_params->stride_w()); + op_options->set_filter_height(op_params->filter_height()); + op_options->set_filter_width(op_params->filter_width()); + op_options->set_activation(as_tflchef_activation(op_params->fused_activation_function())); + + return operation; +} + +} // namespace tflchef diff --git a/compiler/tflchef/tflite/src/Op/MaxPool2D.h b/compiler/tflchef/tflite/src/Op/MaxPool2D.h new file mode 100644 index 000000000..36533f80c --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/MaxPool2D.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2018 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 __TFLITE_OP_MAXPOOL2D_H__ +#define __TFLITE_OP_MAXPOOL2D_H__ + +#include "TFliteOpChef.h" + +namespace tflchef +{ + +/** + * @brief tflchef operator builder for MaxPool2D + */ +class TFliteOpMaxPool2D : public TFliteOpChef +{ +public: + void filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; + tflchef::Operation *build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; +}; + +} // namespace tflchef + +#endif // __TFLITE_OP_MAXPOOL2D_H__ diff --git a/compiler/tflchef/tflite/src/Op/Mean.cpp b/compiler/tflchef/tflite/src/Op/Mean.cpp new file mode 100644 index 000000000..1c2975781 --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/Mean.cpp @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2020 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 "Mean.h" + +#include "Convert.h" + +namespace tflchef +{ + +void TFliteOpMean::filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + // filler for second input + const auto &inputs = *op->inputs(); + + const tflite::Tensor *tensor = import->tensors()->Get(inputs[1]); + assert(tensor->type() == tflite::TensorType::TensorType_INT32); + const tflite::Buffer *buffer = import->buffers()->Get(tensor->buffer()); + auto vec = extract_buffer<int32_t>(buffer); + import->set_tensor_filler(inputs[1], vec); +} + +tflchef::Operation *TFliteOpMean::build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + auto op_params = op->builtin_options_as_ReducerOptions(); + assert(op_params != nullptr); + + auto operation = model_recipe->add_operation(); + + operation->set_type("Mean"); + + auto op_options = operation->mutable_mean_options(); + + op_options->set_keep_dims(op_params->keep_dims()); + + return operation; +} + +} // namespace tflchef diff --git a/compiler/tflchef/tflite/src/Op/Mean.h b/compiler/tflchef/tflite/src/Op/Mean.h new file mode 100644 index 000000000..532c40c66 --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/Mean.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2020 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 __TFLITE_OP_MEAN_H__ +#define __TFLITE_OP_MEAN_H__ + +#include "TFliteOpChef.h" + +namespace tflchef +{ + +/** + * @brief tflchef operator builder for mean + */ +class TFliteOpMean : public TFliteOpChef +{ +public: + void filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; + tflchef::Operation *build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; +}; + +} // namespace tflchef + +#endif // __TFLITE_OP_MEAN_H__ diff --git a/compiler/tflchef/tflite/src/Op/Pack.cpp b/compiler/tflchef/tflite/src/Op/Pack.cpp new file mode 100644 index 000000000..ddf8c7d5d --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/Pack.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2020 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 "Pack.h" + +#include "Convert.h" + +namespace tflchef +{ + +void TFliteOpPack::filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + // Nothing to do with filler +} + +tflchef::Operation *TFliteOpPack::build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + auto op_params = op->builtin_options_as_PackOptions(); + assert(op_params != nullptr); + + auto operation = model_recipe->add_operation(); + + operation->set_type("Pack"); + + auto op_options = operation->mutable_pack_options(); + + op_options->set_axis(op_params->axis()); + op_options->set_values_count(op_params->values_count()); + + return operation; +} + +} // namespace tflchef diff --git a/compiler/tflchef/tflite/src/Op/Pack.h b/compiler/tflchef/tflite/src/Op/Pack.h new file mode 100644 index 000000000..7779f64ed --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/Pack.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2020 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 __TFLITE_OP_PACK_H__ +#define __TFLITE_OP_PACK_H__ + +#include "TFliteOpChef.h" + +namespace tflchef +{ + +/** + * @brief tflchef operator builder for pack + */ +class TFliteOpPack : public TFliteOpChef +{ +public: + void filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; + tflchef::Operation *build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; +}; + +} // namespace tflchef + +#endif // __TFLITE_OP_PACK_H__ diff --git a/compiler/tflchef/tflite/src/Op/Pad.cpp b/compiler/tflchef/tflite/src/Op/Pad.cpp new file mode 100644 index 000000000..2978e4422 --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/Pad.cpp @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2020 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 "Pad.h" + +#include "Convert.h" + +namespace tflchef +{ + +void TFliteOpPad::filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + // filler for second input + const auto &inputs = *op->inputs(); + + const tflite::Tensor *tensor = import->tensors()->Get(inputs[1]); + assert(tensor->type() == tflite::TensorType::TensorType_INT32); + const tflite::Buffer *buffer = import->buffers()->Get(tensor->buffer()); + auto vec = extract_buffer<int32_t>(buffer); + import->set_tensor_filler(inputs[1], vec); +} + +tflchef::Operation *TFliteOpPad::build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + auto operation = model_recipe->add_operation(); + + operation->set_type("Pad"); + + return operation; +} + +} // namespace tflchef diff --git a/compiler/tflchef/tflite/src/Op/Pad.h b/compiler/tflchef/tflite/src/Op/Pad.h new file mode 100644 index 000000000..99998d418 --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/Pad.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2020 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 __TFLITE_OP_PAD_H__ +#define __TFLITE_OP_PAD_H__ + +#include "TFliteOpChef.h" + +namespace tflchef +{ + +/** + * @brief tflchef operator builder for PAD + */ +class TFliteOpPad : public TFliteOpChef +{ +public: + void filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; + tflchef::Operation *build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; +}; + +} // namespace tflchef + +#endif // __TFLITE_OP_PAD_H__ diff --git a/compiler/tflchef/tflite/src/Op/ReLU.cpp b/compiler/tflchef/tflite/src/Op/ReLU.cpp new file mode 100644 index 000000000..e4474b6fa --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/ReLU.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2018 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 "ReLU.h" + +#include "Convert.h" + +namespace tflchef +{ + +void TFliteOpReLU::filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + // Nothing to do with filler +} + +tflchef::Operation *TFliteOpReLU::build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + auto operation = model_recipe->add_operation(); + + operation->set_type("ReLU"); + + return operation; +} + +} // namespace tflchef diff --git a/compiler/tflchef/tflite/src/Op/ReLU.h b/compiler/tflchef/tflite/src/Op/ReLU.h new file mode 100644 index 000000000..be1090270 --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/ReLU.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2018 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 __TFLITE_OP_RELU_H__ +#define __TFLITE_OP_RELU_H__ + +#include "TFliteOpChef.h" + +namespace tflchef +{ + +/** + * @brief tflchef operator builder for ReLU + */ +class TFliteOpReLU : public TFliteOpChef +{ +public: + void filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; + tflchef::Operation *build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; +}; + +} // namespace tflchef + +#endif // __TFLITE_OP_RELU_H__ diff --git a/compiler/tflchef/tflite/src/Op/ReLU6.cpp b/compiler/tflchef/tflite/src/Op/ReLU6.cpp new file mode 100644 index 000000000..14371884b --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/ReLU6.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2018 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 "ReLU6.h" + +#include "Convert.h" + +namespace tflchef +{ + +void TFliteOpReLU6::filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + // Nothing to do with filler +} + +tflchef::Operation *TFliteOpReLU6::build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + auto operation = model_recipe->add_operation(); + + operation->set_type("ReLU6"); + + return operation; +} + +} // namespace tflchef diff --git a/compiler/tflchef/tflite/src/Op/ReLU6.h b/compiler/tflchef/tflite/src/Op/ReLU6.h new file mode 100644 index 000000000..64ddb6a2e --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/ReLU6.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2018 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 __TFLITE_OP_RELU6_H__ +#define __TFLITE_OP_RELU6_H__ + +#include "TFliteOpChef.h" + +namespace tflchef +{ + +/** + * @brief tflchef operator builder for ReLU6 + */ +class TFliteOpReLU6 : public TFliteOpChef +{ +public: + void filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; + tflchef::Operation *build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; +}; + +} // namespace tflchef + +#endif // __TFLITE_OP_RELU6_H__ diff --git a/compiler/tflchef/tflite/src/Op/Reshape.cpp b/compiler/tflchef/tflite/src/Op/Reshape.cpp new file mode 100644 index 000000000..663ab3ec3 --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/Reshape.cpp @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2018 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 "Reshape.h" + +#include "Convert.h" + +namespace tflchef +{ + +void TFliteOpReshape::filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + const std::vector<int32_t> &inputs = as_index_vector(op->inputs()); + + bool hasShape = (inputs.size() == 2); + assert(inputs.size() == 1 || hasShape); + + if (hasShape) + { + auto op_params = op->builtin_options_as_ReshapeOptions(); + std::vector<int32_t> new_shape = as_index_vector(op_params->new_shape()); + import->set_tensor_filler(inputs.at(1), new_shape); + } +} + +tflchef::Operation *TFliteOpReshape::build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + auto op_params = op->builtin_options_as_ReshapeOptions(); + assert(op_params != nullptr); + + auto operation = model_recipe->add_operation(); + + operation->set_type("Reshape"); + + auto op_options = operation->mutable_reshape_options(); + + std::vector<int32_t> new_shape = as_index_vector(op_params->new_shape()); + + for (auto shape : new_shape) + { + op_options->add_new_shape(shape); + } + + return operation; +} + +} // namespace tflchef diff --git a/compiler/tflchef/tflite/src/Op/Reshape.h b/compiler/tflchef/tflite/src/Op/Reshape.h new file mode 100644 index 000000000..be9fdac08 --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/Reshape.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2018 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 __TFLITE_OP_RESHAPE_H__ +#define __TFLITE_OP_RESHAPE_H__ + +#include "TFliteOpChef.h" + +namespace tflchef +{ + +/** + * @brief tflchef operator builder for Reshape + */ +class TFliteOpReshape : public TFliteOpChef +{ +public: + void filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; + tflchef::Operation *build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; +}; + +} // namespace tflchef + +#endif // __TFLITE_OP_RESHAPE_H__ diff --git a/compiler/tflchef/tflite/src/Op/Rsqrt.cpp b/compiler/tflchef/tflite/src/Op/Rsqrt.cpp new file mode 100644 index 000000000..1639214e4 --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/Rsqrt.cpp @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2020 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 "Rsqrt.h" + +#include "Convert.h" + +namespace tflchef +{ + +void TFliteOpRsqrt::filler(const tflite::Operator *, TFliteImport *, tflchef::ModelRecipe *) const +{ + // Nothing to do with filler +} + +tflchef::Operation *TFliteOpRsqrt::build(const tflite::Operator *, TFliteImport *, + tflchef::ModelRecipe *model_recipe) const +{ + auto operation = model_recipe->add_operation(); + + operation->set_type("Rsqrt"); + + return operation; +} + +} // namespace tflchef diff --git a/compiler/tflchef/tflite/src/Op/Rsqrt.h b/compiler/tflchef/tflite/src/Op/Rsqrt.h new file mode 100644 index 000000000..5d68344c2 --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/Rsqrt.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2020 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 __TFLITE_OP_RSQRT_H__ +#define __TFLITE_OP_RSQRT_H__ + +#include "TFliteOpChef.h" + +namespace tflchef +{ + +/** + * @brief tflchef operator builder for Rsqrt + */ +class TFliteOpRsqrt : public TFliteOpChef +{ +public: + void filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; + tflchef::Operation *build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; +}; + +} // namespace tflchef + +#endif // __TFLITE_OP_RSQRT_H__ diff --git a/compiler/tflchef/tflite/src/Op/Softmax.cpp b/compiler/tflchef/tflite/src/Op/Softmax.cpp new file mode 100644 index 000000000..5b5c94f7e --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/Softmax.cpp @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2018 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 "Softmax.h" + +#include "Convert.h" + +namespace tflchef +{ + +void TFliteOpSoftmax::filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + // Nothing to do with filler +} + +tflchef::Operation *TFliteOpSoftmax::build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + auto op_params = op->builtin_options_as_SoftmaxOptions(); + assert(op_params != nullptr); + + auto operation = model_recipe->add_operation(); + + operation->set_type("Softmax"); + + auto op_options = operation->mutable_softmax_options(); + + op_options->set_beta(op_params->beta()); + + return operation; +} + +} // namespace tflchef diff --git a/compiler/tflchef/tflite/src/Op/Softmax.h b/compiler/tflchef/tflite/src/Op/Softmax.h new file mode 100644 index 000000000..cf168bdd9 --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/Softmax.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2018 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 __TFLITE_OP_SOFTMAX_H__ +#define __TFLITE_OP_SOFTMAX_H__ + +#include "TFliteOpChef.h" + +namespace tflchef +{ + +/** + * @brief tflchef operator builder for Softmax + */ +class TFliteOpSoftmax : public TFliteOpChef +{ +public: + void filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; + tflchef::Operation *build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; +}; + +} // namespace tflchef + +#endif // __TFLITE_OP_SOFTMAX_H__ diff --git a/compiler/tflchef/tflite/src/Op/Sqrt.cpp b/compiler/tflchef/tflite/src/Op/Sqrt.cpp new file mode 100644 index 000000000..dd6bfcab0 --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/Sqrt.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2019 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 "Sqrt.h" + +#include "Convert.h" + +namespace tflchef +{ + +void TFliteOpSqrt::filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + // Nothing to do with filler + // But input has filler for constant inputs + const auto &inputs = *op->inputs(); + + const tflite::Tensor *tensor = import->tensors()->Get(inputs[0]); + if (tensor->type() == tflite::TensorType::TensorType_FLOAT32) + { + const tflite::Buffer *buffer = import->buffers()->Get(tensor->buffer()); + if (buffer && buffer->data()) + { + auto vec = extract_buffer<float>(buffer); + import->set_tensor_filler(inputs[0], vec); + } + } +} + +tflchef::Operation *TFliteOpSqrt::build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + auto operation = model_recipe->add_operation(); + + operation->set_type("Sqrt"); + + return operation; +} + +} // namespace tflchef diff --git a/compiler/tflchef/tflite/src/Op/Sqrt.h b/compiler/tflchef/tflite/src/Op/Sqrt.h new file mode 100644 index 000000000..9f0ad04ae --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/Sqrt.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2019 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 __TFLITE_OP_SQRT_H__ +#define __TFLITE_OP_SQRT_H__ + +#include "TFliteOpChef.h" + +namespace tflchef +{ + +/** + * @brief tflchef operator builder for Sqrt + */ +class TFliteOpSqrt : public TFliteOpChef +{ +public: + void filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; + tflchef::Operation *build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; +}; + +} // namespace tflchef + +#endif // __TFLITE_OP_SQRT_H__ diff --git a/compiler/tflchef/tflite/src/Op/Sub.cpp b/compiler/tflchef/tflite/src/Op/Sub.cpp new file mode 100644 index 000000000..db77fddf7 --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/Sub.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2018 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 "Sub.h" + +#include "Convert.h" + +namespace tflchef +{ + +void TFliteOpSub::filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + // Nothing to do with filler +} + +tflchef::Operation *TFliteOpSub::build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + auto op_params = op->builtin_options_as_SubOptions(); + assert(op_params != nullptr); + + auto operation = model_recipe->add_operation(); + + operation->set_type("Sub"); + + auto op_options = operation->mutable_sub_options(); + + auto tflchef_activation = as_tflchef_activation(op_params->fused_activation_function()); + op_options->set_activation(tflchef_activation); + + return operation; +} + +} // namespace tflchef diff --git a/compiler/tflchef/tflite/src/Op/Sub.h b/compiler/tflchef/tflite/src/Op/Sub.h new file mode 100644 index 000000000..2168e5e0d --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/Sub.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2018 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 __TFLITE_OP_SUB_H__ +#define __TFLITE_OP_SUB_H__ + +#include "TFliteOpChef.h" + +namespace tflchef +{ + +/** + * @brief tflchef operator builder for Sub + */ +class TFliteOpSub : public TFliteOpChef +{ +public: + void filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; + tflchef::Operation *build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; +}; + +} // namespace tflchef + +#endif // __TFLITE_OP_SUB_H__ diff --git a/compiler/tflchef/tflite/src/Op/Tanh.cpp b/compiler/tflchef/tflite/src/Op/Tanh.cpp new file mode 100644 index 000000000..cab8ca460 --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/Tanh.cpp @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2020 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 "Tanh.h" + +#include "Convert.h" + +namespace tflchef +{ + +void TFliteOpTanh::filler(const tflite::Operator *, TFliteImport *, tflchef::ModelRecipe *) const +{ + // Nothing to do with filler +} + +tflchef::Operation *TFliteOpTanh::build(const tflite::Operator *, TFliteImport *, + tflchef::ModelRecipe *model_recipe) const +{ + auto operation = model_recipe->add_operation(); + + operation->set_type("Tanh"); + + return operation; +} + +} // namespace tflchef diff --git a/compiler/tflchef/tflite/src/Op/Tanh.h b/compiler/tflchef/tflite/src/Op/Tanh.h new file mode 100644 index 000000000..7339e4103 --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/Tanh.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2020 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 __TFLITE_OP_TANH_H__ +#define __TFLITE_OP_TANH_H__ + +#include "TFliteOpChef.h" + +namespace tflchef +{ + +/** + * @brief tflchef operator builder for Tanh + */ +class TFliteOpTanh : public TFliteOpChef +{ +public: + void filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; + tflchef::Operation *build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; +}; + +} // namespace tflchef + +#endif // __TFLITE_OP_TANH_H__ diff --git a/compiler/tflchef/tflite/src/Op/Transpose.cpp b/compiler/tflchef/tflite/src/Op/Transpose.cpp new file mode 100644 index 000000000..ae97a19e2 --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/Transpose.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2020 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 "Transpose.h" + +#include "Convert.h" + +namespace tflchef +{ + +void TFliteOpTranspose::filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + const auto &inputs = *op->inputs(); + + const tflite::Tensor *perm_tensor = import->tensors()->Get(inputs[1]); + assert(perm_tensor->type() == tflite::TensorType::TensorType_INT32); + const tflite::Buffer *buffer = import->buffers()->Get(perm_tensor->buffer()); + auto vec = extract_buffer<int32_t>(buffer); + import->set_tensor_filler(inputs[1], vec); +} + +tflchef::Operation *TFliteOpTranspose::build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const +{ + auto op_params = op->builtin_options_as<tflite::TransposeOptions>(); + assert(op_params != nullptr); + (void)op_params; + + auto operation = model_recipe->add_operation(); + + operation->set_type("Transpose"); + + auto op_options = operation->mutable_transpose_options(); + (void)op_options; + + return operation; +} + +} // namespace tflchef diff --git a/compiler/tflchef/tflite/src/Op/Transpose.h b/compiler/tflchef/tflite/src/Op/Transpose.h new file mode 100644 index 000000000..f0d944b6b --- /dev/null +++ b/compiler/tflchef/tflite/src/Op/Transpose.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2020 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 __TFLITE_OP_TRANSPOSE_H__ +#define __TFLITE_OP_TRANSPOSE_H__ + +#include "TFliteOpChef.h" + +namespace tflchef +{ + +/** + * @brief tflchef operator builder for Transpose + */ +class TFliteOpTranspose : public TFliteOpChef +{ +public: + void filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; + tflchef::Operation *build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const override; +}; + +} // namespace tflchef + +#endif // __TFLITE_OP_TRANSPOSE_H__ diff --git a/compiler/tflchef/tflite/src/RawModelLoader.cpp b/compiler/tflchef/tflite/src/RawModelLoader.cpp new file mode 100644 index 000000000..e9ef8ec8b --- /dev/null +++ b/compiler/tflchef/tflite/src/RawModelLoader.cpp @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2018 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 <tflchef/RawModel.h> + +#include <cwrap/Fildes.h> + +#include <fcntl.h> +#include <unistd.h> +#include <sys/stat.h> +#include <sys/mman.h> + +namespace +{ + +class MemoryMappedRawModel final : public tflchef::RawModel +{ +public: + /** + * @require fd and data SHOULD be valid + */ + explicit MemoryMappedRawModel(int fd, void *data, size_t size) : _fd{fd}, _data{data}, _size{size} + { + // DO NOTHING + } + +public: + ~MemoryMappedRawModel() + { + munmap(_data, _size); + close(_fd); + } + +public: + MemoryMappedRawModel(const MemoryMappedRawModel &) = delete; + MemoryMappedRawModel(MemoryMappedRawModel &&) = delete; + +public: + const ::tflite::Model *model(void) const override { return ::tflite::GetModel(_data); } + +private: + int _fd = -1; + void *_data = nullptr; + size_t _size = 0; +}; + +} // namespace + +namespace tflchef +{ + +std::unique_ptr<RawModel> load_tflite(const std::string &path) +{ + cwrap::Fildes fildes{open(path.c_str(), O_RDONLY)}; + + if (fildes.get() == -1) + { + // Return nullptr on open failure + return nullptr; + } + + struct stat st; + if (fstat(fildes.get(), &st) == -1) + { + // Return nullptr on fstat failure + return nullptr; + } + + auto size = st.st_size; + auto data = mmap(nullptr, size, PROT_READ, MAP_SHARED, fildes.get(), 0); + + if (data == MAP_FAILED) + { + // Return nullptr on mmap failure + return nullptr; + } + + return std::unique_ptr<tflchef::RawModel>{new MemoryMappedRawModel(fildes.release(), data, size)}; +} + +} // namespace tflchef diff --git a/compiler/tflchef/tflite/src/RecipeChef.cpp b/compiler/tflchef/tflite/src/RecipeChef.cpp new file mode 100644 index 000000000..407006b26 --- /dev/null +++ b/compiler/tflchef/tflite/src/RecipeChef.cpp @@ -0,0 +1,241 @@ +/* + * Copyright (c) 2018 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 <tflchef/RecipeChef.h> + +#include "Convert.h" +#include "TFliteImport.h" +#include "TFliteOpChef.h" +#include "TFliteOpChefs.h" +#include "TFliteOpRegistry.h" + +#include <fstream> +#include <sstream> + +namespace tflchef +{ + +void set_inputs(TFliteImport *import, tflchef::Operation *operation, const tflite::Operator *op) +{ + auto tensors = import->tensors(); + const std::vector<int32_t> &inputs = as_index_vector(op->inputs()); + + for (auto input : inputs) + { + auto tensor = tensors->Get(input); + std::string name = tensor_name(tensor); + operation->add_input(name); + } +} + +void set_outputs(TFliteImport *import, tflchef::Operation *operation, const tflite::Operator *op) +{ + auto tensors = import->tensors(); + const std::vector<int32_t> &outputs = as_index_vector(op->outputs()); + + for (auto output : outputs) + { + auto tensor = tensors->Get(output); + std::string name = tensor_name(tensor); + operation->add_output(name); + } +} + +/** + * @brief This will build ModelRecipe from tflite::Model + * First to check operand filler options by scanning all operators, + * then translate all operands and operators. + * Last will set network inputs and outputs. + */ +std::unique_ptr<ModelRecipe> generate_recipe(const tflite::Model *model) +{ + std::unique_ptr<ModelRecipe> model_recipe{new ModelRecipe()}; + + TFliteImport tflite_import(model); + + assert(tflite_import.num_subgraph() == 1); + tflite_import.select_sub_graph(0); + + auto tensors = tflite_import.tensors(); + auto buffers = tflite_import.buffers(); + auto operators = tflite_import.operators(); + + // operand fillers for adding all operators + for (uint32_t i = 0; i < operators->Length(); ++i) + { + const auto *op = operators->Get(i); + tflite::BuiltinOperator builtincode = tflite_import.builtin_code(op); + + if (const auto *graph_builder = TFliteOpRegistry::get().lookup(builtincode)) + { + graph_builder->filler(op, &tflite_import, model_recipe.get()); + } + else + { + std::string opcodename = tflite_import.opcode_name(op); + throw std::runtime_error{"Not supported: " + opcodename}; + } + } + + // add all operands(tensors) + for (uint32_t i = 0; i < tensors->Length(); ++i) + { + auto tensor = tensors->Get(i); + + // check buffer + if (tensor->buffer() >= buffers->size()) + throw std::runtime_error{"file load failed"}; + + ::tflchef::Operand *operand = model_recipe->add_operand(); + + operand->set_name(tensor_name(tensor)); + operand->set_type(as_tflchef_type(tensor->type())); + + std::vector<int32_t> dims = as_index_vector(tensor->shape()); + ::tflchef::TensorShape *shape = operand->mutable_shape(); + for (auto dim : dims) + { + shape->add_dim(dim); + } + + // filler for weights, bias and so on + std::vector<int32_t> expvalues; + std::vector<float> expfvalues; + if (tflite_import.get_tensor_filler(i)) + { + tflchef::TensorFiller *filler = operand->mutable_filler(); + // Note: it is OK to use random weights for functionality validation + filler->set_tag("gaussian"); + filler->add_arg("0.0"); // average + filler->add_arg("0.1"); // standard deviation + } + else if (tflite_import.get_tensor_filler(i, expvalues)) + { + tflchef::TensorFiller *filler = operand->mutable_filler(); + filler->set_tag("explicit"); + for (auto value : expvalues) + { + std::ostringstream ss; + ss << value; + filler->add_arg(ss.str()); + } + } + else if (tflite_import.get_tensor_filler(i, expfvalues)) + { + tflchef::TensorFiller *filler = operand->mutable_filler(); + filler->set_tag("explicit"); + for (auto value : expfvalues) + { + std::ostringstream ss; + ss << value; + filler->add_arg(ss.str()); + } + } + + auto quant = tensor->quantization(); + if (quant != nullptr) + { + // Note: Calling 'operand->mutable_quant()' will create empty 'quant' node + // in the recipe file. We want this only when valid parameter exist. + if (quant->min() != nullptr && quant->min()->size() > 0) + { + tflchef::TensorQuantization *chef_quant = operand->mutable_quant(); + for (uint32_t idx = 0; idx < quant->min()->size(); ++idx) + chef_quant->add_min(quant->min()->Get(idx)); + } + if (quant->max() != nullptr && quant->max()->size() > 0) + { + tflchef::TensorQuantization *chef_quant = operand->mutable_quant(); + for (uint32_t idx = 0; idx < quant->max()->size(); idx++) + chef_quant->add_max(quant->max()->Get(idx)); + } + if (quant->scale() != nullptr && quant->scale()->size() > 0) + { + tflchef::TensorQuantization *chef_quant = operand->mutable_quant(); + for (uint32_t idx = 0; idx < quant->scale()->size(); ++idx) + chef_quant->add_scale(quant->scale()->Get(idx)); + } + if (quant->zero_point() != nullptr && quant->zero_point()->size() > 0) + { + tflchef::TensorQuantization *chef_quant = operand->mutable_quant(); + for (uint32_t idx = 0; idx < quant->zero_point()->size(); ++idx) + chef_quant->add_zero_point(quant->zero_point()->Get(idx)); + } + } + } + + // add all operators + for (uint32_t i = 0; i < operators->Length(); ++i) + { + const auto *op = operators->Get(i); + tflite::BuiltinOperator builtincode = tflite_import.builtin_code(op); + + if (const auto *graph_builder = TFliteOpRegistry::get().lookup(builtincode)) + { + auto operation = graph_builder->build(op, &tflite_import, model_recipe.get()); + + // common for all operators: inputs, outputs + set_inputs(&tflite_import, operation, op); + set_outputs(&tflite_import, operation, op); + } + else + { + std::string opcodename = tflite_import.opcode_name(op); + throw std::runtime_error{"Not supported: " + opcodename}; + } + } + + // network inputs/outputs + const std::vector<int32_t> &inputs = tflite_import.inputs(); + const std::vector<int32_t> &outputs = tflite_import.outputs(); + + for (const auto input : inputs) + { + auto tensor = tensors->Get(input); + std::string name = tensor_name(tensor); + + model_recipe->add_input(name); + } + for (const auto output : outputs) + { + auto tensor = tensors->Get(output); + std::string name = tensor_name(tensor); + + model_recipe->add_output(name); + } + + return std::move(model_recipe); +} + +bool write_recipe(const std::string &filename, std::unique_ptr<ModelRecipe> &recipe) +{ + std::fstream fo(filename, std::ios::binary | std::ios::out); + + if (!fo.is_open()) + { + throw std::runtime_error{"file store failed"}; + } + + // Note: SerializeToString() or SerializeToOstream() writes in binary mode + // DebugString() and Utf8DebugString() will print as a human readable text + fo << recipe->Utf8DebugString(); + + fo.close(); + + return true; +} + +} // namespace tflchef diff --git a/compiler/tflchef/tflite/src/TFliteImport.cpp b/compiler/tflchef/tflite/src/TFliteImport.cpp new file mode 100644 index 000000000..51d9b5ffa --- /dev/null +++ b/compiler/tflchef/tflite/src/TFliteImport.cpp @@ -0,0 +1,145 @@ +/* + * Copyright (c) 2018 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 "TFliteImport.h" + +#include "Convert.h" + +#include <sstream> + +namespace tflchef +{ + +const char *kEmptyTensorName = "(noname)"; + +const char *tensor_type(const tflite::Tensor *tensor) +{ + return tflite::EnumNameTensorType(tensor->type()); +} + +const char *tensor_name(const tflite::Tensor *tensor) +{ + auto name = tensor->name(); + if (name) + return name->c_str(); + return kEmptyTensorName; +} + +bool is_valid(const tflite::OperatorCode *opcode) +{ + tflite::BuiltinOperator code = opcode->builtin_code(); + return (tflite::BuiltinOperator_MIN <= code && code <= tflite::BuiltinOperator_MAX); +} + +bool is_custom(const tflite::OperatorCode *opcode) +{ + tflite::BuiltinOperator code = opcode->builtin_code(); + return (code == tflite::BuiltinOperator_CUSTOM); +} + +TFliteImport::TFliteImport(const tflite::Model *model) +{ + _subgraphs = model->subgraphs(); + _buffers = model->buffers(); + + auto opcodes = model->operator_codes(); + for (const ::tflite::OperatorCode *opcode : *opcodes) + { + _op_codes.push_back(opcode); + } +} + +bool TFliteImport::select_sub_graph(uint32_t sgindex) +{ + _tensors = nullptr; + _operators = nullptr; + _inputs.clear(); + _outputs.clear(); + + if (_subgraphs->Length() <= sgindex) + { + assert(false); + return false; + } + + const tflite::SubGraph *subgraph = (*_subgraphs)[sgindex]; + + _tensors = subgraph->tensors(); + _operators = subgraph->operators(); + + _inputs = as_index_vector(subgraph->inputs()); + _outputs = as_index_vector(subgraph->outputs()); + + return true; +} + +tflite::BuiltinOperator TFliteImport::builtin_code(const tflite::Operator *op) const +{ + uint32_t index = op->opcode_index(); + assert(index < _op_codes.size()); + const tflite::OperatorCode *opcode = _op_codes.at(index); + + return opcode->builtin_code(); +} + +std::string TFliteImport::opcode_name(const tflite::Operator *op) const +{ + uint32_t index = op->opcode_index(); + assert(index < _op_codes.size()); + const tflite::OperatorCode *opcode = _op_codes.at(index); + + if (!is_valid(opcode)) + { + std::ostringstream oss; + oss << "(invalid: " << index << ")"; + return oss.str(); + } + + if (is_custom(opcode)) + { + if (!opcode->custom_code()) + return "(invalid custom)"; + + return opcode->custom_code()->c_str(); + } + + tflite::BuiltinOperator code = opcode->builtin_code(); + return EnumNameBuiltinOperator(code); +} + +size_t TFliteImport::buffer_info(const tflite::Tensor *tensor, const uint8_t **buff_data) +{ + *buff_data = nullptr; + + if (tensor->buffer() == 0) + return 0; + + if (auto *buffer = (*_buffers)[tensor->buffer()]) + { + if (auto *array = buffer->data()) + { + if (size_t size = array->size()) + { + *buff_data = reinterpret_cast<const uint8_t *>(array->data()); + return size; + } + } + } + + return 0; +} + +} // namespace tflchef diff --git a/compiler/tflchef/tflite/src/TFliteImport.h b/compiler/tflchef/tflite/src/TFliteImport.h new file mode 100644 index 000000000..fa8196405 --- /dev/null +++ b/compiler/tflchef/tflite/src/TFliteImport.h @@ -0,0 +1,140 @@ +/* + * Copyright (c) 2018 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 __TFLITE_IMPORT_H__ +#define __TFLITE_IMPORT_H__ + +#include <mio/tflite/schema_generated.h> + +#include <tflchef.pb.h> + +#include <map> +#include <vector> + +namespace tflchef +{ + +using TFliteSubGraphs_t = flatbuffers::Vector<flatbuffers::Offset<tflite::SubGraph>>; +using TFliteTensors_t = flatbuffers::Vector<flatbuffers::Offset<tflite::Tensor>>; +using TFliteBuffers_t = flatbuffers::Vector<flatbuffers::Offset<tflite::Buffer>>; +using TFliteOperators_t = flatbuffers::Vector<flatbuffers::Offset<tflite::Operator>>; + +const char *tensor_type(const tflite::Tensor *tensor); +const char *tensor_name(const tflite::Tensor *tensor); +bool is_valid(const tflite::OperatorCode *opcode); +bool is_custom(const tflite::OperatorCode *opcode); + +/** + * @brief Loads TF lite file and provides helpers to access attributes + */ +class TFliteImport +{ +public: + TFliteImport(const tflite::Model *model); + + TFliteImport() = delete; + +public: + bool select_sub_graph(uint32_t subgraph); + +public: + const TFliteBuffers_t *buffers() { return _buffers; } + const TFliteTensors_t *tensors() { return _tensors; } + const TFliteOperators_t *operators() { return _operators; } + const std::vector<int32_t> &inputs() const { return _inputs; } + const std::vector<int32_t> &outputs() const { return _outputs; } + + uint32_t num_subgraph() const { return _subgraphs->Length(); } + + tflite::BuiltinOperator builtin_code(const tflite::Operator *op) const; + std::string opcode_name(const tflite::Operator *op) const; + size_t buffer_info(const tflite::Tensor *tensor, const uint8_t **buff_data); + + /** + * @brief This will record the tensor by index, if it needs filler option, + * such as kernel, bias. + */ + void set_tensor_filler(uint32_t tensor_index) { _tensor_filler[tensor_index] = true; } + + /** + * @brief This will store int32 filler values such as reshape information for the tensor + */ + void set_tensor_filler(uint32_t tensor_index, std::vector<int32_t> &expvalues) + { + _tensor_filler_vint32[tensor_index] = expvalues; + } + + void set_tensor_filler(uint32_t tensor_index, std::vector<float> &expvalues) + { + _tensor_filler_vfloat[tensor_index] = expvalues; + } + + /** + * @brief This will return true if the tensor by index, needs a filler option. + */ + bool get_tensor_filler(uint32_t tensor_index) + { + auto it = _tensor_filler.find(tensor_index); + if (it != _tensor_filler.end()) + { + return it->second; + } + return false; + } + + /** + * @brief This will return true if the tensor by index, needs a int array filler option. + */ + bool get_tensor_filler(uint32_t tensor_index, std::vector<int32_t> &expvalues) + { + auto it = _tensor_filler_vint32.find(tensor_index); + if (it != _tensor_filler_vint32.end()) + { + expvalues = it->second; + return true; + } + return false; + } + + bool get_tensor_filler(uint32_t tensor_index, std::vector<float> &expvalues) + { + auto it = _tensor_filler_vfloat.find(tensor_index); + if (it != _tensor_filler_vfloat.end()) + { + expvalues = it->second; + return true; + } + return false; + } + +private: + const TFliteSubGraphs_t *_subgraphs; + const TFliteBuffers_t *_buffers; + const TFliteTensors_t *_tensors; + const TFliteOperators_t *_operators; + + std::vector<const tflite::OperatorCode *> _op_codes; + std::vector<int32_t> _inputs; + std::vector<int32_t> _outputs; + + std::map<uint32_t, bool> _tensor_filler; + std::map<uint32_t, std::vector<int32_t>> _tensor_filler_vint32; + std::map<uint32_t, std::vector<float>> _tensor_filler_vfloat; +}; + +} // namespace tflchef + +#endif // __TFLITE_IMPORT_H__ diff --git a/compiler/tflchef/tflite/src/TFliteOpChef.h b/compiler/tflchef/tflite/src/TFliteOpChef.h new file mode 100644 index 000000000..98564293b --- /dev/null +++ b/compiler/tflchef/tflite/src/TFliteOpChef.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2018 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 __TFLITE_OP_CHEF_H__ +#define __TFLITE_OP_CHEF_H__ + +#include <mio/tflite/schema_generated.h> + +#include <tflchef.pb.h> + +#include "TFliteImport.h" + +namespace tflchef +{ + +/** + * @brief Interface for each operators to build tflchef + */ +class TFliteOpChef +{ +public: + virtual void filler(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const = 0; + virtual ::tflchef::Operation *build(const tflite::Operator *op, TFliteImport *import, + tflchef::ModelRecipe *model_recipe) const = 0; + virtual ~TFliteOpChef() {} +}; + +} // namespace tflchef + +#endif // __TFLITE_OP_CHEF_H__ diff --git a/compiler/tflchef/tflite/src/TFliteOpChefs.h b/compiler/tflchef/tflite/src/TFliteOpChefs.h new file mode 100644 index 000000000..685d6861b --- /dev/null +++ b/compiler/tflchef/tflite/src/TFliteOpChefs.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2018 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 __TFLITE_OP_CHEFS_H__ +#define __TFLITE_OP_CHEFS_H__ + +// In alphabet order +#include "Op/Abs.h" +#include "Op/Add.h" +#include "Op/ArgMax.h" +#include "Op/AveragePool2D.h" +#include "Op/BatchToSpaceND.h" +#include "Op/Concatenation.h" +#include "Op/Conv2D.h" +#include "Op/Cos.h" +#include "Op/DepthwiseConv2D.h" +#include "Op/Div.h" +#include "Op/Equal.h" +#include "Op/Exp.h" +#include "Op/FloorDiv.h" +#include "Op/FullyConnected.h" +#include "Op/LogicalNot.h" +#include "Op/LogicalOr.h" +#include "Op/MaxPool2D.h" +#include "Op/Mean.h" +#include "Op/Pack.h" +#include "Op/Pad.h" +#include "Op/ReLU.h" +#include "Op/ReLU6.h" +#include "Op/Reshape.h" +#include "Op/Rsqrt.h" +#include "Op/Softmax.h" +#include "Op/Sqrt.h" +#include "Op/Sub.h" +#include "Op/Tanh.h" +#include "Op/Transpose.h" + +#endif // __TFLITE_OP_CHEFS_H__ diff --git a/compiler/tflchef/tflite/src/TFliteOpRegistry.h b/compiler/tflchef/tflite/src/TFliteOpRegistry.h new file mode 100644 index 000000000..f0aed2113 --- /dev/null +++ b/compiler/tflchef/tflite/src/TFliteOpRegistry.h @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2018 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 __TFLITE_OP_REGISTRY_H__ +#define __TFLITE_OP_REGISTRY_H__ + +#include "TFliteOpChef.h" +#include "TFliteOpChefs.h" + +#include <memory> + +namespace tflchef +{ + +/** + * @brief tflchef operator registry + */ +class TFliteOpRegistry +{ +public: + /** + * @brief Returns registered TFliteOpChef pointer for BuiltinOperator or + * nullptr if not registered + */ + const TFliteOpChef *lookup(tflite::BuiltinOperator op) const + { + if (_tfliteop_map.find(op) == _tfliteop_map.end()) + return nullptr; + + return _tfliteop_map.at(op).get(); + } + + static TFliteOpRegistry &get() + { + static TFliteOpRegistry me; + return me; + } + +private: + TFliteOpRegistry() + { +#define REG_TFL_OP(OPCODE, CLASS) \ + _tfliteop_map[tflite::BuiltinOperator_##OPCODE] = std::make_unique<CLASS>() + + REG_TFL_OP(ABS, TFliteOpAbs); + REG_TFL_OP(ADD, TFliteOpAdd); + REG_TFL_OP(ARG_MAX, TFliteOpArgMax); + REG_TFL_OP(AVERAGE_POOL_2D, TFliteOpAveragePool2D); + REG_TFL_OP(BATCH_TO_SPACE_ND, TFliteOpBatchToSpaceND); + REG_TFL_OP(CONCATENATION, TFliteOpConcatenation); + REG_TFL_OP(CONV_2D, TFliteOpConv2D); + REG_TFL_OP(COS, TFliteOpCos); + REG_TFL_OP(DEPTHWISE_CONV_2D, TFliteOpDepthwiseConv2D); + REG_TFL_OP(DIV, TFliteOpDiv); + REG_TFL_OP(EQUAL, TFliteOpEqual); + REG_TFL_OP(EXP, TFliteOpExp); + REG_TFL_OP(FLOOR_DIV, TFliteOpFloorDiv); + REG_TFL_OP(FULLY_CONNECTED, TFliteOpFullyConnected); + REG_TFL_OP(LOGICAL_NOT, TFliteOpLogicalNot); + REG_TFL_OP(LOGICAL_OR, TFliteOpLogicalOr); + REG_TFL_OP(MAX_POOL_2D, TFliteOpMaxPool2D); + REG_TFL_OP(MEAN, TFliteOpMean); + REG_TFL_OP(PACK, TFliteOpPack); + REG_TFL_OP(PAD, TFliteOpPad); + REG_TFL_OP(RELU, TFliteOpReLU); + REG_TFL_OP(RELU6, TFliteOpReLU6); + REG_TFL_OP(RESHAPE, TFliteOpReshape); + REG_TFL_OP(RSQRT, TFliteOpRsqrt); + REG_TFL_OP(SOFTMAX, TFliteOpSoftmax); + REG_TFL_OP(SQRT, TFliteOpSqrt); + REG_TFL_OP(SUB, TFliteOpSub); + REG_TFL_OP(TANH, TFliteOpTanh); + REG_TFL_OP(TRANSPOSE, TFliteOpTranspose); + +#undef REG_TFL_OP + } + +private: + std::map<tflite::BuiltinOperator, std::unique_ptr<TFliteOpChef>> _tfliteop_map; +}; + +} // namespace tflchef + +#endif // __TFLITE_OP_REGISTRY_H__ |