From 7b66e3ce3a2d05aea748b7a3ede43aec0d9fe91b Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Staff=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Mon, 26 Nov 2018 18:46:24 +0900 Subject: [PATCH] [tflchef] Support explicit initialization over INT32 tensors (#2408) * [tflchef] Support explicit initialization over INT32 tensors This commit introduces 'explicit' filler which allows users to explicitly specifies the values of operands. Currently, 'explicit' filler is available for INT32 types. Signed-off-by: Jonghyun Park * Use -1 as a new_shape parameter --- contrib/tflchef/core/src/Data/Explicit.h | 82 ++++++++++++++++++++++ contrib/tflchef/core/src/DataChef.def | 1 + contrib/tflchef/core/src/DataChefs.h | 1 + contrib/tflchef/core/src/LexicalCast.cpp | 1 + contrib/tflchef/core/src/ModelChef.cpp | 3 + .../tflchef/tests/explicit_datachef/test.recipe | 28 ++++++++ 6 files changed, 116 insertions(+) create mode 100644 contrib/tflchef/core/src/Data/Explicit.h create mode 100644 contrib/tflchef/tests/explicit_datachef/test.recipe diff --git a/contrib/tflchef/core/src/Data/Explicit.h b/contrib/tflchef/core/src/Data/Explicit.h new file mode 100644 index 0000000..f4175f4 --- /dev/null +++ b/contrib/tflchef/core/src/Data/Explicit.h @@ -0,0 +1,82 @@ +/* + * 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 __EXPLICIT_FILLER_H__ +#define __EXPLICIT_FILLER_H__ + +#include "DataChef.h" +#include "LexicalCast.h" + +#include + +/** + * @brief Return the identity of arithmetic addition + */ +template T zero(void); + +template <> int zero(void) { return 0; } + +template class ExplicitDataChef final : public DataChef +{ +public: + ExplicitDataChef() + { + // DO NOTHING + } + +public: + std::vector generate(int32_t count) const override + { + std::vector res; + + for (uint32_t n = 0; n < count; ++n) + { + T const value = (n < _values.size()) ? _values.at(n) : zero(); + const uint8_t *arr = reinterpret_cast(&value); + + for (uint32_t b = 0; b < sizeof(T); ++b) + { + res.emplace_back(arr[b]); + } + } + + return res; + } + +public: + void insert(const T &value) { _values.emplace_back(value); } + +private: + std::vector _values; +}; + +template struct ExplicitDataChefFactory : public DataChefFactory +{ + std::unique_ptr create(const Arguments &args) const + { + std::unique_ptr> res{new ExplicitDataChef}; + + for (uint32_t n = 0; n < args.count(); ++n) + { + auto const value = to_number(args.value(n)); + res->insert(value); + } + + return std::move(res); + } +}; + +#endif // __EXPLICIT_FILLER_H__ diff --git a/contrib/tflchef/core/src/DataChef.def b/contrib/tflchef/core/src/DataChef.def index 10a8b33..9e89497 100644 --- a/contrib/tflchef/core/src/DataChef.def +++ b/contrib/tflchef/core/src/DataChef.def @@ -5,4 +5,5 @@ // DATA_CHEF(TYPE, NAME, FACTORY_CLASS) // "TYPE" SHOULD BE an enum tag of tflchef::TensorType DATA_CHEF(FLOAT32, constant, ConstantDataChefFactory) +DATA_CHEF(INT32, explicit, ExplicitDataChefFactory) DATA_CHEF(FLOAT32, gaussian, GaussianFloat32DataChefFactory) diff --git a/contrib/tflchef/core/src/DataChefs.h b/contrib/tflchef/core/src/DataChefs.h index ca9b31e..2310ae8 100644 --- a/contrib/tflchef/core/src/DataChefs.h +++ b/contrib/tflchef/core/src/DataChefs.h @@ -18,6 +18,7 @@ #define __DATA_CHEFS_H__ #include "Data/Constant.h" +#include "Data/Explicit.h" #include "Data/Gaussian.h" #endif // __DATA_CHEFS_H__ diff --git a/contrib/tflchef/core/src/LexicalCast.cpp b/contrib/tflchef/core/src/LexicalCast.cpp index 4709088..334729d 100644 --- a/contrib/tflchef/core/src/LexicalCast.cpp +++ b/contrib/tflchef/core/src/LexicalCast.cpp @@ -17,3 +17,4 @@ #include "LexicalCast.h" template <> float to_number(const std::string &s) { return std::stof(s); } +template <> int to_number(const std::string &s) { return std::stoi(s); } diff --git a/contrib/tflchef/core/src/ModelChef.cpp b/contrib/tflchef/core/src/ModelChef.cpp index c6feba1..ba09cfb 100644 --- a/contrib/tflchef/core/src/ModelChef.cpp +++ b/contrib/tflchef/core/src/ModelChef.cpp @@ -165,10 +165,13 @@ struct DataChefRegistry final : public Registry DataChefRegistry &data_chef_registry(const tflchef::TensorType &type) { + static DataChefRegistry s32; static DataChefRegistry fp32; switch (type) { + case tflchef::INT32: + return s32; case tflchef::FLOAT32: return fp32; default: diff --git a/contrib/tflchef/tests/explicit_datachef/test.recipe b/contrib/tflchef/tests/explicit_datachef/test.recipe new file mode 100644 index 0000000..bd5213f --- /dev/null +++ b/contrib/tflchef/tests/explicit_datachef/test.recipe @@ -0,0 +1,28 @@ +operand { + name: "ifm" + type: FLOAT32 + shape { dim: 1 dim: 1 dim: 1 dim: 10 } +} +operand { + name: "shape" + type: INT32 + shape { dim: 2 } + filler { tag: "explicit" arg: "-1" arg: "10" } +} +operand { + name: "ofm" + type: FLOAT32 + shape { dim: 1 dim: 10 } +} +operation { + type: "Reshape" + reshape_options { + new_shape: -1 + new_shape: 10 + } + input: "ifm" + input: "shape" + output: "ofm" +} +input: "ifm" +output: "ofm" -- 2.7.4