From 12139efe84ff1c1b2473eb7bb984ffda0f1c8714 Mon Sep 17 00:00:00 2001 From: yejianwu Date: Tue, 11 Sep 2018 13:03:03 +0800 Subject: [PATCH] update memory optimize for expand_dims, add reverse benchmark --- mace/kernels/expand_dims.h | 14 +++-- mace/kernels/reverse.h | 4 +- mace/ops/expand_dims.cc | 5 ++ mace/ops/expand_dims.h | 6 +-- mace/ops/reverse.h | 4 +- mace/ops/reverse_benchmark.cc | 73 +++++++++++++++++++++++++++ mace/python/tools/memory_optimizer.py | 2 +- 7 files changed, 93 insertions(+), 15 deletions(-) create mode 100644 mace/ops/reverse_benchmark.cc diff --git a/mace/kernels/expand_dims.h b/mace/kernels/expand_dims.h index 94386f35..05cac125 100644 --- a/mace/kernels/expand_dims.h +++ b/mace/kernels/expand_dims.h @@ -19,6 +19,7 @@ #include "mace/core/future.h" #include "mace/core/tensor.h" +#include "mace/kernels/kernel.h" #ifdef MACE_ENABLE_OPENCL #include "mace/core/runtime/opencl/cl2_header.h" @@ -27,18 +28,13 @@ namespace mace { namespace kernels { -struct ExpandDimsBase { - explicit ExpandDimsBase(int axis) : axis_(axis) {} - - int axis_; -}; - template struct ExpandDimsFunctor; template -struct ExpandDimsFunctor : ExpandDimsBase { - explicit ExpandDimsFunctor(int axis) : ExpandDimsBase(axis) {} +struct ExpandDimsFunctor : OpKernel { + explicit ExpandDimsFunctor(OpKernelContext *context, int axis) + : OpKernel(context), axis_(axis) {} MaceStatus operator()(const Tensor *input, Tensor *output, @@ -64,6 +60,8 @@ struct ExpandDimsFunctor : ExpandDimsBase { return MACE_SUCCESS; } + + int axis_; }; } // namespace kernels diff --git a/mace/kernels/reverse.h b/mace/kernels/reverse.h index 60cc44eb..69d5fd6d 100644 --- a/mace/kernels/reverse.h +++ b/mace/kernels/reverse.h @@ -20,6 +20,7 @@ #include "mace/core/future.h" #include "mace/core/tensor.h" +#include "mace/kernels/kernel.h" #ifdef MACE_ENABLE_OPENCL #include "mace/core/runtime/opencl/cl2_header.h" @@ -32,7 +33,8 @@ template struct ReverseFunctor; template -struct ReverseFunctor { +struct ReverseFunctor : OpKernel { + explicit ReverseFunctor(OpKernelContext *context) : OpKernel(context) {} MaceStatus operator()(const Tensor *input, const Tensor *axis, Tensor *output, diff --git a/mace/ops/expand_dims.cc b/mace/ops/expand_dims.cc index 07d940c6..5b10d5a3 100644 --- a/mace/ops/expand_dims.cc +++ b/mace/ops/expand_dims.cc @@ -28,6 +28,11 @@ void Register_ExpandDims(OperatorRegistryBase *op_registry) { .TypeConstraint("T") .Build(), ExpandDimsOp); + MACE_REGISTER_OPERATOR(op_registry, OpKeyBuilder("ExpandDims") + .Device(DeviceType::CPU) + .TypeConstraint("T") + .Build(), + ExpandDimsOp); } } // namespace ops diff --git a/mace/ops/expand_dims.h b/mace/ops/expand_dims.h index b466c7c4..b7363c3c 100644 --- a/mace/ops/expand_dims.h +++ b/mace/ops/expand_dims.h @@ -26,9 +26,9 @@ namespace ops { template class ExpandDimsOp : public Operator { public: - ExpandDimsOp(const OperatorDef &op_def, Workspace *ws) - : Operator(op_def, ws), - functor_(OperatorBase::GetOptionalArg("axis", 0)) {} + ExpandDimsOp(const OperatorDef &op_def, OpKernelContext *context) + : Operator(op_def, context), + functor_(context, OperatorBase::GetOptionalArg("axis", 0)) {} MaceStatus Run(StatsFuture *future) override { const Tensor *input = this->Input(INPUT); diff --git a/mace/ops/reverse.h b/mace/ops/reverse.h index 8f60dc66..a753a4e2 100644 --- a/mace/ops/reverse.h +++ b/mace/ops/reverse.h @@ -26,8 +26,8 @@ namespace ops { template class ReverseOp : public Operator { public: - ReverseOp(const OperatorDef &operator_def, Workspace *ws) - : Operator(operator_def, ws) {} + ReverseOp(const OperatorDef &operator_def, OpKernelContext *context) + : Operator(operator_def, context), functor_(context) {} MaceStatus Run(StatsFuture *future) override { const Tensor *input = this->Input(INPUT); diff --git a/mace/ops/reverse_benchmark.cc b/mace/ops/reverse_benchmark.cc new file mode 100644 index 00000000..c6352fab --- /dev/null +++ b/mace/ops/reverse_benchmark.cc @@ -0,0 +1,73 @@ +// Copyright 2018 Xiaomi, Inc. 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 "mace/core/operator.h" +#include "mace/core/runtime/opencl/opencl_runtime.h" +#include "mace/core/testing/test_benchmark.h" +#include "mace/ops/ops_test_util.h" + +namespace mace { +namespace ops { +namespace test { + +namespace { +template +void Reverse(int iters, int batch, int channels, int height, int width) { + mace::testing::StopTiming(); + + OpsTestNet net; + + net.AddRandomInput("Input", {batch, channels, height, width}); + net.AddRandomInput("Axis", {1}); + + OpDefBuilder("Reverse", "ReverseOpTest") + .Input("Input") + .Input("Axis") + .Output("Output") + .Finalize(net.NewOperatorDef()); + // Warm-up + for (int i = 0; i < 5; ++i) { + net.RunOp(D); + } + net.Sync(); + + mace::testing::StartTiming(); + while (iters--) { + net.RunOp(D); + } + net.Sync(); +} +} // namespace + +#define MACE_BM_REVERSE_MACRO(N, C, H, W, TYPE, DEVICE) \ + static void MACE_BM_REVERSE_##N##_##C##_##H##_##W##_##TYPE##_##DEVICE( \ + int iters) { \ + const int64_t macc = \ + static_cast(iters) * N * C * H * W; \ + const int64_t tot = static_cast(iters) * N * C * H * W; \ + mace::testing::MaccProcessed(macc); \ + mace::testing::BytesProcessed(tot *(sizeof(TYPE))); \ + Reverse(iters, N, C, H, W); \ + } \ + MACE_BENCHMARK(MACE_BM_REVERSE_##N##_##C##_##H##_##W##_##TYPE##_##DEVICE) + +#define MACE_BM_REVERSE(N, C, H, W) \ + MACE_BM_REVERSE_MACRO(N, C, H, W, float, CPU); + +MACE_BM_REVERSE(1, 1, 99, 256); +MACE_BM_REVERSE(1, 30, 99, 256); +MACE_BM_REVERSE(1, 50, 99, 256); +} // namespace test +} // namespace ops +} // namespace mace diff --git a/mace/python/tools/memory_optimizer.py b/mace/python/tools/memory_optimizer.py index 52b1867b..f3c4d6d3 100644 --- a/mace/python/tools/memory_optimizer.py +++ b/mace/python/tools/memory_optimizer.py @@ -124,7 +124,7 @@ class MemoryOptimizer(object): @staticmethod def is_memory_reuse_op(op): return op.type == 'Reshape' or op.type == 'Identity' \ - or op.type == 'Squeeze' + or op.type == 'Squeeze' or op.type == 'ExpandDims' def optimize(self): for op in self.net_def.op: -- GitLab