From ce8845e1a047721f2ab8bf41a9b70d9947ca7fc7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EA=B9=80=EC=A0=95=ED=98=84/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Senior=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Thu, 19 Apr 2018 12:57:33 +0900 Subject: [PATCH] Unify NEON and OpenCL implementation of reshape layer (#800) NEON and OpenCL implementations of reshape layer are very similar. This commit - extracts common features to `src/Reshape.h` - implement different parts into `neon/Reshape.cpp` and `cl/Reshape.cpp`. Signed-off-by: Junghyun Kim --- src/kernel/acl/src/Reshape.h | 54 +++++++++++++++++++++++++++++++++++++ src/kernel/acl/src/cl/Reshape.cpp | 33 ++++++----------------- src/kernel/acl/src/neon/Reshape.cpp | 34 ++++++++--------------- 3 files changed, 73 insertions(+), 48 deletions(-) create mode 100644 src/kernel/acl/src/Reshape.h diff --git a/src/kernel/acl/src/Reshape.h b/src/kernel/acl/src/Reshape.h new file mode 100644 index 0000000..1d4d5dc --- /dev/null +++ b/src/kernel/acl/src/Reshape.h @@ -0,0 +1,54 @@ +#ifndef __NNFW_KERNEL_ACL_RESHAPE_COMMON_H__ +#define __NNFW_KERNEL_ACL_RESHAPE_COMMON_H__ +#include +#include +#include + +// TODO: fix include path in CMakeFiles +#include "IO_accessor.h" +#include "shape.h" + +namespace nnfw { +namespace kernel { +namespace acl { + +namespace common { + +typedef std::function sync_scheduler_f; + +template +bool reshapeGeneric(const void* inputData, const android::nn::Shape& inputShape, + void* outputData, const android::nn::Shape& outputShape, + sync_scheduler_f sync_scheduler) { + + auto input_shape = util::fromNNShape(inputShape); + auto output_shape = util::fromNNShape(outputShape); + + TensorT input(arm_compute::TensorInfo(input_shape, arm_compute::Format::F32)); + TensorT output(arm_compute::TensorInfo(output_shape, arm_compute::Format::F32)); + + LayerT l; + + l.configure(input.ptr(), output.ptr()); + + input.allocate(); + output.allocate(); + + TensorAccess(input.ref(), (float*)inputData, inputShape); + + l.run(); + + sync_scheduler(); + + TensorAccess(output.ref(), (float*)outputData, outputShape); + + return true; +} + +} // namespace common + +} // namespace acl +} // namespace kernel +} // namespace nnfw + +#endif // __NNFW_KERNEL_ACL_RESHAPE_COMMON_H__ diff --git a/src/kernel/acl/src/cl/Reshape.cpp b/src/kernel/acl/src/cl/Reshape.cpp index d6a3b87..c51fc20 100644 --- a/src/kernel/acl/src/cl/Reshape.cpp +++ b/src/kernel/acl/src/cl/Reshape.cpp @@ -5,38 +5,21 @@ // TODO: fix include path in CMakeFiles #include "../IO_accessor.h" #include "../shape.h" +#include "../CLUniqueTensor.h" +#include "../Reshape.h" namespace nnfw { namespace kernel { namespace acl { -bool reshapeGeneric(const void* inputData, const android::nn::Shape& inputShape, - void* outputData, const android::nn::Shape& outputShape) { - - auto input_shape = util::fromNNShape(inputShape); - auto output_shape = util::fromNNShape(outputShape); - - arm_compute::CLTensor input, output; - - input.allocator()->init(arm_compute::TensorInfo(input_shape, arm_compute::Format::F32)); - output.allocator()->init(arm_compute::TensorInfo(output_shape, arm_compute::Format::F32)); - - arm_compute::CLReshapeLayer l; - - l.configure(&input, &output); - - input.allocator()->allocate(); - output.allocator()->allocate(); - - TensorAccess(input, (float*)inputData, inputShape); - - l.run(); - +static void sync_scheduler() { arm_compute::CLScheduler::get().sync(); +} - TensorAccess(output, (float*)outputData, outputShape); - - return true; +bool reshapeGeneric(const void* inputData, const android::nn::Shape& inputShape, + void* outputData, const android::nn::Shape& outputShape) { + return common::reshapeGeneric + (inputData, inputShape, outputData, outputShape, sync_scheduler); } } // namespace acl diff --git a/src/kernel/acl/src/neon/Reshape.cpp b/src/kernel/acl/src/neon/Reshape.cpp index 6ac181e..a126fe3 100644 --- a/src/kernel/acl/src/neon/Reshape.cpp +++ b/src/kernel/acl/src/neon/Reshape.cpp @@ -5,40 +5,28 @@ // TODO: fix include path in CMakeFiles #include "../IO_accessor.h" #include "../shape.h" +#include "../NEUniqueTensor.h" +#include "../Reshape.h" namespace nnfw { namespace kernel { namespace acl { + namespace neon { +static void sync_scheduler() { + arm_compute::CLScheduler::get().sync(); +} + bool reshapeGeneric(const void* inputData, const android::nn::Shape& inputShape, void* outputData, const android::nn::Shape& outputShape) { - - auto input_shape = util::fromNNShape(inputShape); - auto output_shape = util::fromNNShape(outputShape); - - arm_compute::Tensor input, output; - - input.allocator()->init(arm_compute::TensorInfo(input_shape, arm_compute::Format::F32)); - output.allocator()->init(arm_compute::TensorInfo(output_shape, arm_compute::Format::F32)); - - arm_compute::NEReshapeLayer l; - - l.configure(&input, &output); - - input.allocator()->allocate(); - output.allocator()->allocate(); - - TensorAccess(input, (float*)inputData, inputShape); - - l.run(); - - TensorAccess(output, (float*)outputData, outputShape); - - return true; + return common::reshapeGeneric + (inputData, inputShape, outputData, outputShape, sync_scheduler); } } // namespace neon + } // namespace acl } // namespace kernel } // namespace nnfw + -- 2.7.4