From 84a99f8bc4ce91f66a4ac7c1d9c5a126c7bf3967 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: Tue, 17 Sep 2019 20:00:28 +0900 Subject: [PATCH] [exo-tflite] Introducing locoex::TFLConst (#7519) locoex::TFLConst is added to represent tensor data. (note that this is not for a specific tflite op) Signed-off-by: Hyun Sik Yoon --- compiler/exo-tflite/src/Dialect/IR/TFLNodes.cpp | 48 ++++++++++++++++++++++++- compiler/exo-tflite/src/Dialect/IR/TFLNodes.h | 22 ++++++++++++ compiler/exo-tflite/src/Dialect/IR/TFLNodes.lst | 1 + compiler/exo-tflite/src/TFLFormattedGraph.cpp | 6 ++++ 4 files changed, 76 insertions(+), 1 deletion(-) diff --git a/compiler/exo-tflite/src/Dialect/IR/TFLNodes.cpp b/compiler/exo-tflite/src/Dialect/IR/TFLNodes.cpp index 12f5781..7287fc5 100644 --- a/compiler/exo-tflite/src/Dialect/IR/TFLNodes.cpp +++ b/compiler/exo-tflite/src/Dialect/IR/TFLNodes.cpp @@ -16,4 +16,50 @@ #include "TFLNodes.h" -// This file exists to make compiler build TFLNodes.h +#include + +#include + +namespace locoex +{ + +template uint32_t TFLConst::size(void) const +{ + assert(dtype() == DT); + assert(_data.size() % sizeof(typename loco::DataTypeImpl
::Type) == 0); + return _data.size() / sizeof(typename loco::DataTypeImpl
::Type); +} + +template void TFLConst::size(uint32_t l) +{ + assert(dtype() == DT); + _data.resize(l * sizeof(typename loco::DataTypeImpl
::Type)); +} + +template +const typename loco::DataTypeImpl
::Type &TFLConst::at(uint32_t n) const +{ + assert(dtype() == DT); + assert(n < size
()); + return *(reinterpret_cast::Type *>(_data.data()) + n); +} + +template typename loco::DataTypeImpl
::Type &TFLConst::at(uint32_t n) +{ + assert(dtype() == DT); + assert(n < size
()); + return *(reinterpret_cast::Type *>(_data.data()) + n); +} + +#define INSTANTIATE(DT) \ + template uint32_t TFLConst::size
(void) const; \ + template void TFLConst::size
(uint32_t); \ + template const typename loco::DataTypeImpl
::Type &TFLConst::at
(uint32_t) const; \ + template typename loco::DataTypeImpl
::Type &TFLConst::at
(uint32_t); + +INSTANTIATE(loco::DataType::S32); +INSTANTIATE(loco::DataType::FLOAT32); + +#undef INSTANTIATE + +} // namespace locoex diff --git a/compiler/exo-tflite/src/Dialect/IR/TFLNodes.h b/compiler/exo-tflite/src/Dialect/IR/TFLNodes.h index 42de748..f12b62d 100644 --- a/compiler/exo-tflite/src/Dialect/IR/TFLNodes.h +++ b/compiler/exo-tflite/src/Dialect/IR/TFLNodes.h @@ -22,6 +22,7 @@ #include #include +#include #include @@ -162,6 +163,27 @@ private: // TODO TFLConcatenation +/** + * @brief Class to build tensor data + * @note This will not be exported as a specific op + */ +class TFLConst final : public FixedArityNode<0, TFLNodeImpl>, + public loco::NodeMixin, + public loco::NodeMixin +{ +public: + TFLConst() = default; + +public: + template uint32_t size(void) const; + template void size(uint32_t size); + template const typename loco::DataTypeImpl
::Type &at(uint32_t n) const; + template typename loco::DataTypeImpl
::Type &at(uint32_t n); + +private: + std::vector _data; +}; + // TODO TFLConv2D // TODO TFLDepthwiseConv2D diff --git a/compiler/exo-tflite/src/Dialect/IR/TFLNodes.lst b/compiler/exo-tflite/src/Dialect/IR/TFLNodes.lst index a77ac80..9e9c410 100644 --- a/compiler/exo-tflite/src/Dialect/IR/TFLNodes.lst +++ b/compiler/exo-tflite/src/Dialect/IR/TFLNodes.lst @@ -8,6 +8,7 @@ TFL_NODE(ADD, locoex::TFLAdd) TFL_NODE(AVERAGE_POOL_2D, locoex::TFLAveragePool2D) // TODO TFLConcatenation +TFL_NODE(NOP_CONSTGEN, locoex::TFLConst) // TODO TFLConv2D // TODO TFLDepthwiseConv2D TFL_NODE(DIV, locoex::TFLDiv) diff --git a/compiler/exo-tflite/src/TFLFormattedGraph.cpp b/compiler/exo-tflite/src/TFLFormattedGraph.cpp index 52ce0ff..d01fe7b 100644 --- a/compiler/exo-tflite/src/TFLFormattedGraph.cpp +++ b/compiler/exo-tflite/src/TFLFormattedGraph.cpp @@ -127,6 +127,12 @@ bool TFLNodeSummaryBuilder::summary(const locoex::TFLAveragePool2D *node, // TODO TFLConcatenation +bool TFLNodeSummaryBuilder::summary(const locoex::TFLConst *, locop::NodeSummary &s) const +{ + s.state(locop::NodeSummary::State::PartiallyKnown); + return true; +} + // TODO TFLConv2D // TODO TFLDepthwiseConv2D -- 2.7.4