From 1204e93956ff4efaec3a6e554bd89bcd5937b99b Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EC=9C=A4=ED=98=84=EC=8B=9D/On-Device=20Lab=28SR=29/Princip?= =?utf8?q?al=20Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Thu, 11 Apr 2019 17:02:07 +0900 Subject: [PATCH] [nnkit/TF] adding TensorContext (#3235) * [nnkit/TF] adding TensorContext for Tensorflow background This commit adds child class of TensorContext for Tensorflow backend. Signed-off-by: Hyun Sik Yoon * deleted move & copy construcors --- .../tf/include/nnkit/support/tf/TensorContext.h | 78 ++++++++++++++++++++++ .../nnkit/libs/support/tf/src/TensorContext.cpp | 57 ++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 contrib/nnkit/libs/support/tf/include/nnkit/support/tf/TensorContext.h create mode 100644 contrib/nnkit/libs/support/tf/src/TensorContext.cpp diff --git a/contrib/nnkit/libs/support/tf/include/nnkit/support/tf/TensorContext.h b/contrib/nnkit/libs/support/tf/include/nnkit/support/tf/TensorContext.h new file mode 100644 index 0000000..d7011b4 --- /dev/null +++ b/contrib/nnkit/libs/support/tf/include/nnkit/support/tf/TensorContext.h @@ -0,0 +1,78 @@ +/* + * 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 __NNKIT_SUPPORT_TF_TENSOR_CONTEXT_H__ +#define __NNKIT_SUPPORT_TF_TENSOR_CONTEXT_H__ + +#include "nnkit/TensorContext.h" +#include "nnkit/support/tf/ParsedTensor.h" +#include "nnkit/support/tf/TensorDataMap.h" + +#include + +namespace nnkit +{ +namespace support +{ +namespace tf +{ + +class TensorContext final : public nnkit::TensorContext +{ +public: + TensorContext(const std::vector> &tensors, TensorDataMap &data_map) + : _tensors(tensors), _data_map(data_map) + { + // empty + } + + TensorContext(const TensorContext &) = delete; // prevent accidental use + TensorContext(TensorContext &&) = delete; + +public: + uint32_t size(void) const override { return _tensors.size(); } + +public: + std::string name(uint32_t n) const override // name with ":0", ":1", etc + { + return _tensors.at(n)->name(); + } + +public: + nncc::core::ADT::tensor::Shape shape(uint32_t n) const override + { + return _tensors.at(n)->shape(); + } + +public: + // Float (fp32) tensor support + bool isFloatTensor(uint32_t n) const override { return _tensors.at(n)->isFloatTensor(); } + + void getMutableFloatTensor(uint32_t n, + const nnkit::TensorContext::TypedAccessor &f) override; + void getConstFloatTensor(uint32_t n, + const nnkit::TensorContext::TypedReader &f) const override; + +private: + const std::vector> &_tensors; + TensorDataMap &_data_map; +}; + +} // namespace tf +} // namespace support +} // namespace nnkit + +#endif // __NNKIT_SUPPORT_TF_TENSOR_CONTEXT_H__ diff --git a/contrib/nnkit/libs/support/tf/src/TensorContext.cpp b/contrib/nnkit/libs/support/tf/src/TensorContext.cpp new file mode 100644 index 0000000..e5098fe --- /dev/null +++ b/contrib/nnkit/libs/support/tf/src/TensorContext.cpp @@ -0,0 +1,57 @@ +/* + * 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 "nnkit/support/tf/TensorContext.h" + +#include +#include + +namespace nnkit +{ +namespace support +{ +namespace tf +{ + +void TensorContext::getMutableFloatTensor(uint32_t n, + const nnkit::TensorContext::TypedAccessor &f) +{ // for input + using nncc::core::ADT::tensor::LexicalLayout; + using nncc::core::ADT::tensor::make_overlay; + + const ParsedTensor *parsed_tensor = _tensors.at(n).get(); + float *data = reinterpret_cast(_data_map.data(parsed_tensor)); + auto overlay = make_overlay(shape(n), data); + + f(*this, n, overlay); +} + +void TensorContext::getConstFloatTensor(uint32_t n, + const nnkit::TensorContext::TypedReader &f) const +{ // for output + using nncc::core::ADT::tensor::LexicalLayout; + using nncc::core::ADT::tensor::make_overlay; + + const ParsedTensor *parsed_tensor = _tensors.at(n).get(); + float *data = reinterpret_cast(_data_map.data(parsed_tensor)); + auto overlay = make_overlay(shape(n), data); + + f(*this, n, overlay); +} + +} // namespace tf +} // namespace support +} // namespace nnkit -- 2.7.4