diff options
Diffstat (limited to 'include/support/tflite')
22 files changed, 0 insertions, 1363 deletions
diff --git a/include/support/tflite/Assert.h b/include/support/tflite/Assert.h deleted file mode 100644 index f5c6bf219..000000000 --- a/include/support/tflite/Assert.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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 __NNFW_SUPPORT_TFLITE_ASSERT_H__ -#define __NNFW_SUPPORT_TFLITE_ASSERT_H__ - -#include "tensorflow/contrib/lite/context.h" - -#include <sstream> - -#define STR_DETAIL(value) #value -#define STR(value) STR_DETAIL(value) - -#define TFLITE_ENSURE(exp) { \ - const TfLiteStatus status = (exp); \ - \ - if (status != kTfLiteOk) \ - { \ - std::ostringstream ss; \ - ss << #exp << " failed (" << __FILE__ << ":" << __LINE__ << ")"; \ - throw std::runtime_error{ss.str()}; \ - } \ -} - -#endif // __NNFW_SUPPORT_TFLITE_ASSERT_H__ diff --git a/include/support/tflite/Diff.h b/include/support/tflite/Diff.h deleted file mode 100644 index f4f3f6fe8..000000000 --- a/include/support/tflite/Diff.h +++ /dev/null @@ -1,129 +0,0 @@ -/* - * 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 __NNFW_SUPPORT_TFLITE_COMPARE_H__ -#define __NNFW_SUPPORT_TFLITE_COMPARE_H__ - -#include "tensorflow/contrib/lite/interpreter.h" - -#include "util/tensor/Index.h" -#include "util/tensor/Diff.h" -#include "util/tensor/Shape.h" -#include "util/tensor/Comparator.h" - -#include "support/tflite/TensorView.h" - -#include <functional> -#include <vector> - -class TfLiteInterpMatchApp -{ -public: - TfLiteInterpMatchApp(const nnfw::util::tensor::Comparator &comparator) - : _verbose{false}, _comparator(comparator) - { - // DO NOTHING - } - -public: - int &verbose(void) { return _verbose; } - -private: - int _verbose; - -public: - bool run(::tflite::Interpreter &pure, ::tflite::Interpreter &nnapi) const; - template <typename T> - bool compareSingleTensorView(const nnfw::support::tflite::TensorView<T> &expected, - const nnfw::support::tflite::TensorView<T> &obtained, - int id) const; - -private: - const nnfw::util::tensor::Comparator &_comparator; -}; - -#include "support/tflite/interp/Builder.h" -#include "support/tflite/Quantization.h" - -#include <random> - -class RandomGenerator -{ -public: - RandomGenerator(int seed, float mean, float stddev, - const TfLiteQuantizationParams quantization = make_default_quantization()) - : _rand{seed}, _dist{mean, stddev}, _quantization{quantization} - { - // DO NOTHING - } - -public: - template <typename T> - T generate(const ::nnfw::util::tensor::Shape &, const ::nnfw::util::tensor::Index &) - { - return generate<T>(); - } - - template <typename T> T generate(void) - { - return _dist(_rand); - } - -private: - std::minstd_rand _rand; - std::normal_distribution<float> _dist; - const TfLiteQuantizationParams _quantization; -}; - -template <> -uint8_t RandomGenerator::generate<uint8_t>(void); - -// For NNAPI testing -struct RandomTestParam -{ - int verbose; - int tolerance; - int tensor_logging = 0; - std::string log_path = ""; // meaningful only when tensor_logging is 1 -}; - -class RandomTestRunner -{ -public: - RandomTestRunner(int seed, const RandomTestParam ¶m, - const TfLiteQuantizationParams quantization = make_default_quantization()) - : _randgen{seed, 0.0f, 2.0f, quantization}, _param{param} - { - // DO NOTHING - } - -public: - // NOTE this method updates '_rand' - // Return 0 if test succeeds - int run(const nnfw::support::tflite::interp::Builder &builder); - -public: - RandomGenerator &generator() { return _randgen; }; - -private: - RandomGenerator _randgen; - const RandomTestParam _param; - -public: - static RandomTestRunner make(int seed); -}; - -#endif // __NNFW_SUPPORT_TFLITE_COMPARE_H__ diff --git a/include/support/tflite/FeatureView.h b/include/support/tflite/FeatureView.h deleted file mode 100644 index 3a7d75ec4..000000000 --- a/include/support/tflite/FeatureView.h +++ /dev/null @@ -1,69 +0,0 @@ -/* - * 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 __NNFW_SUPPORT_TFLITE_FEATURE_VIEW_H__ -#define __NNFW_SUPPORT_TFLITE_FEATURE_VIEW_H__ - -#include "tensorflow/contrib/lite/interpreter.h" - -#include "support/tflite/InputIndex.h" -#include "support/tflite/OutputIndex.h" - -#include "util/feature/Shape.h" -#include "util/feature/Reader.h" - -namespace nnfw -{ -namespace support -{ -namespace tflite -{ - -template<typename T> class FeatureView; - -template<> class FeatureView<float> : public nnfw::util::feature::Reader<float> -{ -public: - FeatureView(::tflite::Interpreter &interp, const InputIndex &index); - FeatureView(::tflite::Interpreter &interp, const OutputIndex &index); - -public: - float at(uint32_t ch, uint32_t row, uint32_t col) const; - float &at(uint32_t ch, uint32_t row, uint32_t col); - -private: - uint32_t getElementOffset(uint32_t ch, uint32_t row, uint32_t col) const - { - uint32_t res = 0; - - // TensorFlow Lite assumes that NHWC ordering for tessor - res += row * _shape.W * _shape.C; - res += col * _shape.C; - res += ch; - - return res; - } - -private: - nnfw::util::feature::Shape _shape; - float *_base; -}; - -} // namespace tflite -} // namespace support -} // namespace nnfw - -#endif // __NNFW_SUPPORT_TFLITE_FEATURE_VIEW_H__ diff --git a/include/support/tflite/InputIndex.h b/include/support/tflite/InputIndex.h deleted file mode 100644 index c3ed891fe..000000000 --- a/include/support/tflite/InputIndex.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * 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 __NNFW_SUPPORT_TFLITE_INPUT_INDEX_H__ -#define __NNFW_SUPPORT_TFLITE_INPUT_INDEX_H__ - -namespace nnfw -{ -namespace support -{ -namespace tflite -{ - -class InputIndex -{ -public: - InputIndex(int index) : _index(index) - { - // DO NOTHING - } - -public: - int asInt(void) const { return _index; } - -private: - int _index; -}; - -} // namespace tflite -} // namespace support -} // namespace nnfw - -#endif // __NNFW_SUPPORT_TFLITE_INPUT_INDEX_H__ diff --git a/include/support/tflite/InterpreterSession.h b/include/support/tflite/InterpreterSession.h deleted file mode 100644 index 662de1d17..000000000 --- a/include/support/tflite/InterpreterSession.h +++ /dev/null @@ -1,73 +0,0 @@ -/* - * 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 __NNFW_SUPPORT_TFLITE_INTERPRETER_SESSION_H__ -#define __NNFW_SUPPORT_TFLITE_INTERPRETER_SESSION_H__ - -#include "Session.h" - -namespace nnfw -{ -namespace support -{ -namespace tflite -{ - -class InterpreterSession final : public Session -{ -public: - InterpreterSession(::tflite::Interpreter *interp) : _interp{interp} - { - // DO NOTHING - } - -public: - ::tflite::Interpreter *interp(void) override { return _interp; } - -public: - bool prepare(void) override - { - _interp->UseNNAPI(false); - - if (kTfLiteOk != _interp->AllocateTensors()) - { - return false; - } - - return true; - } - - bool run(void) override - { - // Return true if Invoke returns kTfLiteOk - return kTfLiteOk == _interp->Invoke(); - } - - bool teardown(void) override - { - // Do NOTHING currently - return true; - } - -private: - ::tflite::Interpreter * const _interp; -}; - -} // namespace tflite -} // namespace support -} // namespace nnfw - -#endif // __NNFW_SUPPORT_TFLITE_INTERPRETER_SESSION_H__ diff --git a/include/support/tflite/NNAPISession.h b/include/support/tflite/NNAPISession.h deleted file mode 100644 index 4a8a2162b..000000000 --- a/include/support/tflite/NNAPISession.h +++ /dev/null @@ -1,77 +0,0 @@ -/* - * 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 __NNFW_SUPPORT_TFLITE_NNAPI_SESSION_H__ -#define __NNFW_SUPPORT_TFLITE_NNAPI_SESSION_H__ - -#include "Session.h" -#include "support/tflite/nnapi_delegate.h" - -namespace nnfw -{ -namespace support -{ -namespace tflite -{ - -class NNAPISession final : public Session -{ -public: - NNAPISession(::tflite::Interpreter *interp) : _interp{interp} - { - // Construct Graph from Interpreter - _delegate.BuildGraph(_interp); - } - -public: - ::tflite::Interpreter *interp(void) override { return _interp; } - -public: - bool prepare(void) override - { - // Explicitly turn off T/F lite internal NNAPI delegation in order to use locally defined - // NNAPI delegation. - _interp->UseNNAPI(false); - - if (kTfLiteOk != _interp->AllocateTensors()) - { - return false; - } - - return true; - } - - bool run(void) override - { - return kTfLiteOk == _delegate.Invoke(_interp); - } - - bool teardown(void) override - { - // DO NOTHING - return true; - } - -private: - ::tflite::Interpreter * const _interp; - nnfw::NNAPIDelegate _delegate; -}; - -} // namespace tflite -} // namespace support -} // namespace nnfw - -#endif // __NNFW_SUPPORT_TFLITE_NNAPI_SESSION_H__ diff --git a/include/support/tflite/OutputIndex.h b/include/support/tflite/OutputIndex.h deleted file mode 100644 index be6556ce7..000000000 --- a/include/support/tflite/OutputIndex.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * 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 __NNFW_SUPPORT_TFLITE_OUTPUT_INDEX_H__ -#define __NNFW_SUPPORT_TFLITE_OUTPUT_INDEX_H__ - -namespace nnfw -{ -namespace support -{ -namespace tflite -{ - -class OutputIndex -{ -public: - OutputIndex(int index) : _index(index) - { - // DO NOTHING - } - -public: - int asInt(void) const { return _index; } - -private: - int _index; -}; - -} // namespace tflite -} // namespace support -} // namespace nnfw - -#endif // __NNFW_SUPPORT_TFLITE_OUTPUT_INDEX_H__ diff --git a/include/support/tflite/Quantization.h b/include/support/tflite/Quantization.h deleted file mode 100644 index a9027278a..000000000 --- a/include/support/tflite/Quantization.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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 __NNFW_SUPPORT_TFLITE_QUANTIZATION_H__ -#define __NNFW_SUPPORT_TFLITE_QUANTIZATION_H__ - -union BitwiseIntToFloat { - int i; - float f; -}; - -static const float FLOAT_NEAREST_TO_1 = BitwiseIntToFloat{0x3f7fffff}.f; - -#include "tensorflow/contrib/lite/context.h" - -TfLiteQuantizationParams make_default_quantization(void); - -#endif // __NNFW_SUPPORT_TFLITE_QUANTIZATION_H__ diff --git a/include/support/tflite/Session.h b/include/support/tflite/Session.h deleted file mode 100644 index 2ee2abe23..000000000 --- a/include/support/tflite/Session.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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 __NNFW_SUPPORT_TFLITE_SESSION_H__ -#define __NNFW_SUPPORT_TFLITE_SESSION_H__ - -#include <tensorflow/contrib/lite/interpreter.h> - -namespace nnfw -{ -namespace support -{ -namespace tflite -{ - -struct Session -{ - virtual ~Session() = default; - - virtual ::tflite::Interpreter *interp(void) = 0; - - virtual bool prepare(void) = 0; - virtual bool run(void) = 0; - virtual bool teardown(void) = 0; -}; - -} // namespace tflite -} // namespace support -} // namespace nnfw - -#endif // __NNFW_SUPPORT_TFLITE_INTERP_SESSION_H__ diff --git a/include/support/tflite/TensorLogger.h b/include/support/tflite/TensorLogger.h deleted file mode 100644 index 97a0a49d7..000000000 --- a/include/support/tflite/TensorLogger.h +++ /dev/null @@ -1,166 +0,0 @@ -/* - * 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 __NNFW_SUPPORT_TFLITE_TENSOR_LOGGER_H__ -#define __NNFW_SUPPORT_TFLITE_TENSOR_LOGGER_H__ - -#include "util/tensor/IndexIterator.h" -#include "support/tflite/TensorView.h" - -#include <tensorflow/contrib/lite/interpreter.h> -#include <tensorflow/contrib/lite/context.h> -#include <fstream> -#include <iomanip> - -namespace nnfw -{ -namespace support -{ -namespace tflite -{ - -/* -This is a utility to write input and output value / shape into a file in python form. -any python app can load this value by running the python code below: - - exec(open(filename).read()) - -generated python code looks like the following: - -# ------------- test name ------------- -tensor_shape_gen = [] -tensor_value_gen = [] - -tensor_shape_gen.append("{2, 1, 2}") -tensor_value_gen.append([1, 2, 3, 4]) - -tensor_shape_gen.append("{2}") -tensor_value_gen.append([1, 2]) - -tensor_shape_gen.append("{2, 1, 2}") -tensor_value_gen.append([1, 4, 3, 8]) -# ----------------------------------------- -*/ - -class TensorLogger -{ - private: - std::ofstream _outfile; - - public: - - static TensorLogger &instance() - { - static TensorLogger instance; - return instance; - } - - void save(const std::string &path, ::tflite::Interpreter &interp) - { - open(path); - - int log_index = 0; - for (const auto id : interp.inputs()) - { - _outfile << "# input tensors" << std::endl; - printTensor(interp, id, log_index++); - } - for (const auto id : interp.outputs()) - { - _outfile << "# output tensors" << std::endl; - printTensor(interp, id, log_index++); - } - close(); - } - - private: - - void open(const std::string &path) - { - if (! _outfile.is_open()) - _outfile.open(path, std::ios_base::out); - - _outfile << "# ------ file: " << path << " ------" << std::endl - << "tensor_shape_gen = []" << std::endl - << "tensor_value_gen = []" << std::endl << std::endl; - } - - void printTensor(::tflite::Interpreter &interp, const int id, const int log_index) - { - const TfLiteTensor* tensor = interp.tensor(id); - - _outfile << "# tensor name: " << tensor->name << std::endl; - _outfile << "# tflite::interpreter.tensor("<< id <<") -> " - "tensor_value_gen["<< log_index << "]" << std::endl; - - if (tensor->type == kTfLiteInt32) - { - printTensorShape(tensor); - printTensorValue<int32_t>(tensor, tensor->data.i32); - } - else if (interp.tensor(id)->type == kTfLiteUInt8) - { - printTensorShape(tensor); - printTensorValue<uint8_t>(tensor, tensor->data.uint8); - } - else if (tensor->type == kTfLiteFloat32) - { - printTensorShape(tensor); - printTensorValue<float>(tensor, tensor->data.f); - } - } - - void printTensorShape(const TfLiteTensor* tensor) - { - _outfile << "tensor_shape_gen.append('{"; - - size_t r = 0; - for (; r < tensor->dims->size - 1; r++) - { - _outfile << tensor->dims->data[r] - << ", "; - } - _outfile << tensor->dims->data[r]; - - _outfile << "}')" << std::endl; - } - - template <typename T> - void printTensorValue(const TfLiteTensor* tensor, T* tensor_data_ptr) - { - _outfile << "tensor_value_gen.append(["; - - _outfile << std::fixed << std::setprecision(10); - - const T *end = reinterpret_cast<const T *>(tensor->data.raw_const + tensor->bytes); - for (T *ptr = tensor_data_ptr; ptr < end; ptr++) - _outfile << *ptr << ", "; - - _outfile << "])" << std::endl << std::endl; - } - - void close() - { - _outfile << "# --------- tensor shape and value defined above ---------" << std::endl; - _outfile.close(); - } -}; - -} // namespace tflite -} // namespace support -} // namespace nnfw - -#endif // __NNFW_SUPPORT_TFLITE_TENSOR_LOGGER_H__ diff --git a/include/support/tflite/TensorShapeUtils.h b/include/support/tflite/TensorShapeUtils.h deleted file mode 100644 index 711128b48..000000000 --- a/include/support/tflite/TensorShapeUtils.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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 __NNFW_SUPPORT_TFLITE_TENSOR_SHAPE_UTILS_H__ -#define __NNFW_SUPPORT_TFLITE_TENSOR_SHAPE_UTILS_H__ - -#include "util/tensor/Shape.h" - -#include <vector> - -namespace nnfw -{ -namespace support -{ -namespace tflite -{ - -// Converts tensor::Shape into a vector -static inline std::vector<int32_t> as_dims(const nnfw::util::tensor::Shape &shape) -{ - std::vector<int32_t> dims; - - for (uint32_t axis = 0; axis < shape.rank(); ++axis) - { - dims.emplace_back(shape.dim(axis)); - } - - return dims; -} - -nnfw::util::tensor::Shape broadcast(const nnfw::util::tensor::Shape &lhs_shape, - const nnfw::util::tensor::Shape &rhs_shape); - -} // namespace tflite -} // namespace support -} // namespace nnfw - -#endif // __NNFW_SUPPORT_TFLITE_TENSOR_SHAPE_UTILS_H__ diff --git a/include/support/tflite/TensorUtils.h b/include/support/tflite/TensorUtils.h deleted file mode 100644 index 815cfcd29..000000000 --- a/include/support/tflite/TensorUtils.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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 __NNFW_SUPPORT_TFLITE_TENSOR_UTILS_H__ -#define __NNFW_SUPPORT_TFLITE_TENSOR_UTILS_H__ - -#include <tensorflow/contrib/lite/context.h> - -namespace nnfw -{ -namespace support -{ -namespace tflite -{ - -inline bool isFloatTensor(const TfLiteTensor *tensor) -{ - return tensor->type == kTfLiteFloat32; -} - -inline bool isFeatureTensor(const TfLiteTensor *tensor) -{ - return (tensor->dims->size == 4) && (tensor->dims->data[0] == 1); -} - -} // namespace tflite -} // namespace support -} // namespace nnfw - -#endif // __NNFW_SUPPORT_TFLITE_TENSOR_UTILS_H__ diff --git a/include/support/tflite/TensorView.h b/include/support/tflite/TensorView.h deleted file mode 100644 index 0475a4b45..000000000 --- a/include/support/tflite/TensorView.h +++ /dev/null @@ -1,90 +0,0 @@ -/* - * 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 __NNFW_SUPPORT_TFLITE_TENSOR_VIEW_H__ -#define __NNFW_SUPPORT_TFLITE_TENSOR_VIEW_H__ - -#include "tensorflow/contrib/lite/interpreter.h" - -#include "util/tensor/Shape.h" -#include "util/tensor/Index.h" -#include "util/tensor/Reader.h" -#include "util/tensor/NonIncreasingStride.h" - -namespace nnfw -{ -namespace support -{ -namespace tflite -{ - -template<typename T> class TensorView final : public nnfw::util::tensor::Reader<T> -{ -public: - TensorView(const nnfw::util::tensor::Shape &shape, T *base) - : _shape{shape}, _base{base} - { - // Set 'stride' - _stride.init(_shape); - } - -public: - const nnfw::util::tensor::Shape &shape(void) const { return _shape; } - -public: - T at(const nnfw::util::tensor::Index &index) const override - { - const auto offset = _stride.offset(index); - return *(_base + offset); - } - -public: - T &at(const nnfw::util::tensor::Index &index) - { - const auto offset = _stride.offset(index); - return *(_base + offset); - } - -private: - nnfw::util::tensor::Shape _shape; - -public: - T *_base; - nnfw::util::tensor::NonIncreasingStride _stride; - -public: - // TODO Introduce Operand ID class - static TensorView<T> make(::tflite::Interpreter &interp, int tensor_index) - { - auto tensor_ptr = interp.tensor(tensor_index); - - // Set 'shape' - nnfw::util::tensor::Shape shape(tensor_ptr->dims->size); - - for (uint32_t axis = 0; axis < shape.rank(); ++axis) - { - shape.dim(axis) = tensor_ptr->dims->data[axis]; - } - - return TensorView<T>(shape, interp.typed_tensor<T>(tensor_index)); - } -}; - -} // namespace tflite -} // namespace support -} // namespace nnfw - -#endif // __NNFW_SUPPORT_TFLITE_TENSOR_VIEW_H__ diff --git a/include/support/tflite/interp/Builder.h b/include/support/tflite/interp/Builder.h deleted file mode 100644 index 4a5a2f26f..000000000 --- a/include/support/tflite/interp/Builder.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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 __NNFW_SUPPORT_TFLITE_INTERP_BUILDER_H__ -#define __NNFW_SUPPORT_TFLITE_INTERP_BUILDER_H__ - -#include <tensorflow/contrib/lite/interpreter.h> - -namespace nnfw -{ -namespace support -{ -namespace tflite -{ -namespace interp -{ - -struct Builder -{ - virtual ~Builder() = default; - - virtual std::unique_ptr<::tflite::Interpreter> build(void) const = 0; -}; - -} // namespace interp -} // namespace tflite -} // namespace support -} // namespace nnfw - -#endif // __NNFW_SUPPORT_TFLITE_INTERP_BUILDER_H__ diff --git a/include/support/tflite/interp/FlatBufferBuilder.h b/include/support/tflite/interp/FlatBufferBuilder.h deleted file mode 100644 index dab151dcf..000000000 --- a/include/support/tflite/interp/FlatBufferBuilder.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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 __NNFW_SUPPORT_TFLITE_INTERP_FLAT_BUFFER_BUILDER_H__ -#define __NNFW_SUPPORT_TFLITE_INTERP_FLAT_BUFFER_BUILDER_H__ - -#include <tensorflow/contrib/lite/model.h> - -#include "support/tflite/interp/Builder.h" - -namespace nnfw -{ -namespace support -{ -namespace tflite -{ -namespace interp -{ - -class FlatBufferBuilder final : public Builder -{ -public: - FlatBufferBuilder(const ::tflite::FlatBufferModel &model) : _model{model} - { - // DO NOTHING - } - -public: - std::unique_ptr<::tflite::Interpreter> build(void) const override; - -private: - const ::tflite::FlatBufferModel &_model; -}; - -} // namespace interp -} // namespace tflite -} // namespace support -} // namespace nnfw - -#endif // __NNFW_SUPPORT_TFLITE_INTERP_FLAT_BUFFER_BUILDER_H__ diff --git a/include/support/tflite/interp/FunctionBuilder.h b/include/support/tflite/interp/FunctionBuilder.h deleted file mode 100644 index 1ac5918e8..000000000 --- a/include/support/tflite/interp/FunctionBuilder.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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 __NNFW_SUPPORT_TFLITE_INTERP_FUNCTION_BUILDER_H__ -#define __NNFW_SUPPORT_TFLITE_INTERP_FUNCTION_BUILDER_H__ - -#include <tensorflow/contrib/lite/model.h> - -#include "support/tflite/interp/Builder.h" - -namespace nnfw -{ -namespace support -{ -namespace tflite -{ -namespace interp -{ - -class FunctionBuilder final : public Builder -{ -public: - using SetupFunc = std::function<void (::tflite::Interpreter &)>; - -public: - FunctionBuilder(const SetupFunc &fn) : _fn{fn} - { - // DO NOTHING - } - -public: - std::unique_ptr<::tflite::Interpreter> build(void) const override; - -private: - SetupFunc _fn; -}; - -} // namespace interp -} // namespace tflite -} // namespace support -} // namespace nnfw - -#endif // __NNFW_SUPPORT_TFLITE_INTERP_FUNCTION_BUILDER_H__ diff --git a/include/support/tflite/kernels/CustomOps.h b/include/support/tflite/kernels/CustomOps.h deleted file mode 100644 index 36c1c972c..000000000 --- a/include/support/tflite/kernels/CustomOps.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * 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 __NNFW_SUPPORT_TFLITE_KERNELS_CUSTOM_OP_H__ -#define __NNFW_SUPPORT_TFLITE_KERNELS_CUSTOM_OP_H__ - -#include "tensorflow/contrib/lite/context.h" -#include "support/tflite/kernels/TensorFlowMax.h" -#include "support/tflite/kernels/RSQRT.h" -#include "support/tflite/kernels/SquaredDifference.h" - -namespace tflite -{ -namespace ops -{ -namespace custom -{ -namespace nnfw -{ - -#define REGISTER_FUNCTION(Name) \ - TfLiteRegistration *Register_##Name(void) \ - { \ - static TfLiteRegistration r = { Name::Init##Name , Name::Free##Name , Name::Prepare##Name , \ - Name::Eval##Name , 0, #Name}; \ - return &r; \ - } - -REGISTER_FUNCTION(TensorFlowMax) -REGISTER_FUNCTION(RSQRT) -REGISTER_FUNCTION(SquaredDifference) -#undef REGISTER_FUNCTION - -} // namespace nnfw -} // namespace custom -} // namespace ops -} // namespace tflite - -#endif // __NNFW_SUPPORT_TFLITE_KERNELS_CUSTOM_OP_H__ diff --git a/include/support/tflite/kernels/RSQRT.h b/include/support/tflite/kernels/RSQRT.h deleted file mode 100644 index d52442861..000000000 --- a/include/support/tflite/kernels/RSQRT.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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 __NNFW_SUPPORT_TFLITE_KERNELS_RSQRT_H__ -#define __NNFW_SUPPORT_TFLITE_KERNELS_RSQRT_H__ - -#include "tensorflow/contrib/lite/context.h" - -namespace tflite -{ -namespace ops -{ -namespace custom -{ -namespace nnfw -{ -namespace RSQRT -{ - - void *InitRSQRT(TfLiteContext *context, const char *buffer, size_t length); - void FreeRSQRT(TfLiteContext *context, void *buffer); - TfLiteStatus PrepareRSQRT(TfLiteContext *context, TfLiteNode *node); - TfLiteStatus EvalRSQRT(TfLiteContext *context, TfLiteNode *node); - -} // namespace RSQRT -} // namespace nnfw -} // namespace custom -} // namespace ops -} // namespace tflite - -#endif diff --git a/include/support/tflite/kernels/SquaredDifference.h b/include/support/tflite/kernels/SquaredDifference.h deleted file mode 100644 index d0b7d3e7b..000000000 --- a/include/support/tflite/kernels/SquaredDifference.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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 __NNFW_SUPPORT_TFLITE_KERNELS_SQUARED_DIFFERENCE_H__ -#define __NNFW_SUPPORT_TFLITE_KERNELS_SQUARED_DIFFERENCE_H__ - -#include "tensorflow/contrib/lite/context.h" - -namespace tflite -{ -namespace ops -{ -namespace custom -{ -namespace nnfw -{ -namespace SquaredDifference -{ - - void *InitSquaredDifference(TfLiteContext *context, const char *buffer, size_t length); - void FreeSquaredDifference(TfLiteContext *context, void *buffer); - TfLiteStatus PrepareSquaredDifference(TfLiteContext *context, TfLiteNode *node); - TfLiteStatus EvalSquaredDifference(TfLiteContext *context, TfLiteNode *node); - -} // namespace SquaredDifference -} // namespace nnfw -} // namespace custom -} // namespace ops -} // namespace tflite - -#endif diff --git a/include/support/tflite/kernels/TensorFlowMax.h b/include/support/tflite/kernels/TensorFlowMax.h deleted file mode 100644 index 99c9fdc68..000000000 --- a/include/support/tflite/kernels/TensorFlowMax.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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 __NNFW_SUPPORT_TFLITE_KERNELS_TENSORFLOW_MAX_H__ -#define __NNFW_SUPPORT_TFLITE_KERNELS_TENSORFLOW_MAX_H__ - -#include "tensorflow/contrib/lite/context.h" - -namespace tflite -{ -namespace ops -{ -namespace custom -{ -namespace nnfw -{ -namespace TensorFlowMax -{ - - void *InitTensorFlowMax(TfLiteContext *context, const char *buffer, size_t length); - void FreeTensorFlowMax(TfLiteContext *context, void *buffer); - TfLiteStatus PrepareTensorFlowMax(TfLiteContext *context, TfLiteNode *node); - TfLiteStatus EvalTensorFlowMax(TfLiteContext *context, TfLiteNode *node); - -} // namespace TensorFlowMax -} // namespace nnfw -} // namespace custom -} // namespace ops -} // namespace tflite - -#endif diff --git a/include/support/tflite/kernels/register.h b/include/support/tflite/kernels/register.h deleted file mode 100644 index 43a4c1a23..000000000 --- a/include/support/tflite/kernels/register.h +++ /dev/null @@ -1,40 +0,0 @@ -/* Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved - Copyright 2017 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -// NOTE This header is derived from the following file (in TensorFlow) -// 'externals/tensorflow/tensorflow/contrib/lite/kernels/register.h' -#ifndef __NNFW_SUPPORT_TFLITE_KERNELS_REGISTER_H__ -#define __NNFW_SUPPORT_TFLITE_KERNELS_REGISTER_H__ - -#include <unordered_map> -#include "tensorflow/contrib/lite/context.h" -#include "tensorflow/contrib/lite/model.h" - -// TODO Use namespace nnfw -namespace tflite { -namespace ops { -namespace builtin { - -class BuiltinOpResolver : public MutableOpResolver { - public: - BuiltinOpResolver(); -}; - -} // namespace builtin -} // namespace ops -} // namespace tflite - -#endif // __NNFW_SUPPORT_TFLITE_KERNELS_REGISTER_H__ diff --git a/include/support/tflite/nnapi_delegate.h b/include/support/tflite/nnapi_delegate.h deleted file mode 100644 index a5da8ac39..000000000 --- a/include/support/tflite/nnapi_delegate.h +++ /dev/null @@ -1,84 +0,0 @@ -/* Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved - Copyright 2017 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -// NOTE To minimize diff with upstream tensorflow, disable clang-format -// clang-format off - -// NOTE This header is derived from the following file (in TensorFlow) -// 'externals/tensorflow/tensorflow/contrib/lite/nnapi_delegate.h' -#ifndef __NNFW_SUPPORT_TFLITE_NNAPI_DELEGATE_H__ -#define __NNFW_SUPPORT_TFLITE_NNAPI_DELEGATE_H__ - -#include "tensorflow/contrib/lite/allocation.h" -#include "tensorflow/contrib/lite/context.h" -#include "tensorflow/contrib/lite/error_reporter.h" -#include "tensorflow/contrib/lite/interpreter.h" -#include "NeuralNetworksShim.h" - -class ANeuralNetworksModel; -class ANeuralNetworksCompilation; - -namespace nnfw { - -class NNAPIAllocation : public tflite::MMAPAllocation { - public: - NNAPIAllocation(const char* filename, ::tflite::ErrorReporter* error_reporter); - ~NNAPIAllocation(); - - size_t offset(const void* ptr) const { - auto signed_offset = reinterpret_cast<const uint8_t*>(ptr) - - reinterpret_cast<const uint8_t*>(mmapped_buffer_); - - return static_cast<size_t>(signed_offset); - } - - ANeuralNetworksMemory* memory() const { return handle_; } - bool valid() const override { return handle_ != nullptr; } - - private: - mutable ANeuralNetworksMemory* handle_ = nullptr; -}; - -class NNAPIDelegate { - public: - ~NNAPIDelegate(); - - // Convert a tflite graph to NNAPI - TfLiteStatus BuildGraph(::tflite::Interpreter* interpreter); - - // Run - TfLiteStatus Invoke(::tflite::Interpreter* interpreter); - - private: - // The NN API model handle - ANeuralNetworksModel* nn_model_ = nullptr; - // The NN API compilation handle - ANeuralNetworksCompilation* nn_compiled_model_ = nullptr; - - // List of state tensors for LSTM, RNN, SVDF. - // NN API does not allow ops to maintain states across multiple - // invocations. We need to manually create state input tensors from - // corresponding state output tensors of TFLite operations, and map them - // correctly. - std::vector<int> model_states_inputs_; // holds NNAPI operand ids - std::vector<int> model_states_outputs_; // holds TFLite tensor ids -}; - -} // namespace nnfw - -#endif // TENSORFLOW_CONTRIB_LITE_NNAPI_DELEGATE_H_ - -// clang-format on |