From 9a3e3997d396981f147365de31e2e76cb9169523 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EC=98=A4=ED=98=95=EC=84=9D/=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: Thu, 6 Sep 2018 19:23:45 +0900 Subject: [PATCH] Diff: initialize output tensor (#2627) Initialize output tensor as zero Need to RNN hidden state initialize Signed-off-by: Hyeongseok Oh --- libs/support/tflite/src/Diff.cpp | 87 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/libs/support/tflite/src/Diff.cpp b/libs/support/tflite/src/Diff.cpp index ff607e6..e875571 100644 --- a/libs/support/tflite/src/Diff.cpp +++ b/libs/support/tflite/src/Diff.cpp @@ -286,6 +286,7 @@ int RandomTestRunner::run(const nnfw::support::tflite::interp::Builder &builder) using Initializer = std::function; std::map initializers; + std::map reseters; // Generate singed 32-bit integer (s32) input initializers[kTfLiteInt32] = [&](int id, Interpreter *tfl_interp, Interpreter *nnapi) { @@ -308,6 +309,26 @@ int RandomTestRunner::run(const nnfw::support::tflite::interp::Builder &builder) }; }; + // Generate singed 32-bit integer (s32) input + reseters[kTfLiteInt32] = [&](int id, Interpreter *tfl_interp, Interpreter *nnapi) { + assert(tfl_interp->tensor(id)->type == kTfLiteInt32); + assert(nnapi->tensor(id)->type == kTfLiteInt32); + + auto tfl_interp_view = nnfw::support::tflite::TensorView::make(*tfl_interp, id); + auto nnapi_view = nnfw::support::tflite::TensorView::make(*nnapi, id); + + assert(tfl_interp_view.shape() == nnapi_view.shape()); + + int32_t value = 0; + + nnfw::util::tensor::iterate(tfl_interp_view.shape()) + << [&](const nnfw::util::tensor::Index &ind) { + // TODO Generate random values + tfl_interp_view.at(ind) = value; + nnapi_view.at(ind) = value; + }; + }; + initializers[kTfLiteUInt8] = [&](int id, Interpreter *tfl_interp, Interpreter *nnapi) { assert(tfl_interp->tensor(id)->type == kTfLiteUInt8); assert(nnapi->tensor(id)->type == kTfLiteUInt8); @@ -333,6 +354,31 @@ int RandomTestRunner::run(const nnfw::support::tflite::interp::Builder &builder) }; }; + reseters[kTfLiteUInt8] = [&](int id, Interpreter *tfl_interp, Interpreter *nnapi) { + assert(tfl_interp->tensor(id)->type == kTfLiteUInt8); + assert(nnapi->tensor(id)->type == kTfLiteUInt8); + + auto tfl_interp_view = nnfw::support::tflite::TensorView::make(*tfl_interp, id); + auto nnapi_view = nnfw::support::tflite::TensorView::make(*nnapi, id); + + assert(tfl_interp_view.shape() == nnapi_view.shape()); + + auto fp = static_cast( + &RandomGenerator::generate); + const nnfw::util::tensor::Object data(tfl_interp_view.shape(), + std::bind(fp, _randgen, _1, _2)); + assert(tfl_interp_view.shape() == data.shape()); + + uint8_t value = 0; + + nnfw::util::tensor::iterate(tfl_interp_view.shape()) + << [&](const nnfw::util::tensor::Index &ind) { + tfl_interp_view.at(ind) = value; + nnapi_view.at(ind) = value; + }; + }; + initializers[kTfLiteFloat32] = [&](int id, Interpreter *tfl_interp, Interpreter *nnapi) { assert(tfl_interp->tensor(id)->type == kTfLiteFloat32); assert(nnapi->tensor(id)->type == kTfLiteFloat32); @@ -359,6 +405,32 @@ int RandomTestRunner::run(const nnfw::support::tflite::interp::Builder &builder) }; }; + reseters[kTfLiteFloat32] = [&](int id, Interpreter *tfl_interp, Interpreter *nnapi) { + assert(tfl_interp->tensor(id)->type == kTfLiteFloat32); + assert(nnapi->tensor(id)->type == kTfLiteFloat32); + + auto tfl_interp_view = nnfw::support::tflite::TensorView::make(*tfl_interp, id); + auto nnapi_view = nnfw::support::tflite::TensorView::make(*nnapi, id); + + assert(tfl_interp_view.shape() == nnapi_view.shape()); + + auto fp = static_cast( + &RandomGenerator::generate); + const nnfw::util::tensor::Object data(tfl_interp_view.shape(), + std::bind(fp, _randgen, _1, _2)); + + assert(tfl_interp_view.shape() == data.shape()); + + float value = 0; + + nnfw::util::tensor::iterate(tfl_interp_view.shape()) + << [&](const nnfw::util::tensor::Index &ind) { + tfl_interp_view.at(ind) = value; + nnapi_view.at(ind) = value; + }; + }; + // Fill IFM with random numbers for (const auto id : tfl_interp->inputs()) { @@ -374,6 +446,21 @@ int RandomTestRunner::run(const nnfw::support::tflite::interp::Builder &builder) it->second(id, tfl_interp.get(), nnapi.get()); } + // Fill OFM with 0 + for (const auto id : tfl_interp->outputs()) + { + assert(tfl_interp->tensor(id)->type == nnapi->tensor(id)->type); + + auto it = reseters.find(tfl_interp->tensor(id)->type); + + if (it == reseters.end()) + { + throw std::runtime_error{"Not supported input type"}; + } + + it->second(id, tfl_interp.get(), nnapi.get()); + } + std::cout << "[NNAPI TEST] Run T/F Lite Interpreter without NNAPI" << std::endl; tfl_interp->Invoke(); -- 2.7.4