diff --git a/mace/core/operator.cc b/mace/core/operator.cc index 10e74a8e6a5cf6449d98fdad573c510dc5603652..ea8dab31de9be2869f2dd6f447f22eb3894ed288 100644 --- a/mace/core/operator.cc +++ b/mace/core/operator.cc @@ -63,7 +63,6 @@ std::unique_ptr OperatorRegistry::CreateOperator( } namespace ops { - // Keep in lexicographical order extern void Register_Activation(OperatorRegistry *op_registry); extern void Register_AddN(OperatorRegistry *op_registry); @@ -74,6 +73,7 @@ extern void Register_BufferToImage(OperatorRegistry *op_registry); extern void Register_ChannelShuffle(OperatorRegistry *op_registry); extern void Register_Concat(OperatorRegistry *op_registry); extern void Register_Conv2D(OperatorRegistry *op_registry); +extern void Register_DepthToSpace(OperatorRegistry *op_registry); extern void Register_DepthwiseConv2d(OperatorRegistry *op_registry); extern void Register_Eltwise(OperatorRegistry *op_registry); extern void Register_FoldedBatchNorm(OperatorRegistry *op_registry); @@ -90,6 +90,7 @@ extern void Register_ResizeBilinear(OperatorRegistry *op_registry); extern void Register_Slice(OperatorRegistry *op_registry); extern void Register_Softmax(OperatorRegistry *op_registry); extern void Register_SpaceToBatchND(OperatorRegistry *op_registry); +extern void Register_SpaceToDepth(OperatorRegistry *op_registry); extern void Register_WinogradInverseTransform(OperatorRegistry *op_registry); extern void Register_WinogradTransform(OperatorRegistry *op_registry); @@ -107,6 +108,7 @@ OperatorRegistry::OperatorRegistry() { ops::Register_ChannelShuffle(this); ops::Register_Concat(this); ops::Register_Conv2D(this); + ops::Register_DepthToSpace(this); ops::Register_DepthwiseConv2d(this); ops::Register_Eltwise(this); ops::Register_FoldedBatchNorm(this); @@ -123,6 +125,7 @@ OperatorRegistry::OperatorRegistry() { ops::Register_Slice(this); ops::Register_Softmax(this); ops::Register_SpaceToBatchND(this); + ops::Register_SpaceToDepth(this); ops::Register_WinogradInverseTransform(this); ops::Register_WinogradTransform(this); } diff --git a/mace/kernels/depth_to_space.h b/mace/kernels/depth_to_space.h new file mode 100644 index 0000000000000000000000000000000000000000..3f6577f32159309bba931eaef58011902ecc2045 --- /dev/null +++ b/mace/kernels/depth_to_space.h @@ -0,0 +1,119 @@ +// +// Copyright (c) 2017 XiaoMi All rights reserved. +// + +#ifndef MACE_KERNELS_DEPTH_TO_SPACE_H_ +#define MACE_KERNELS_DEPTH_TO_SPACE_H_ +#include + +#include "mace/core/future.h" +#include "mace/core/runtime/opencl/cl2_header.h" +#include "mace/core/tensor.h" +#include "mace/public/mace.h" + +namespace mace { +namespace kernels { + +template +struct DepthToSpaceOpFunctor { + explicit DepthToSpaceOpFunctor(const int block_size, bool d2s) + : block_size_(block_size), d2s_(d2s) {} + void operator()(const Tensor *input, Tensor *output, StatsFuture *future) { + const int batch_size = input->dim(0); + const int input_height = input->dim(1); + const int input_width = input->dim(2); + const int input_depth = input->dim(3); + + index_t output_depth, output_width, output_height; + + if (d2s_) { + output_depth = input_depth / (block_size_ * block_size_); + output_width = input_width * block_size_; + output_height = input_height * block_size_; + } else { + output_depth = input_depth * block_size_ * block_size_; + output_width = input_width / block_size_; + output_height = input_height / block_size_; + } + std::vector output_shape = {batch_size, output_height, + output_width, output_depth}; + + output->Resize(output_shape); + + Tensor::MappingGuard logits_guard(input); + Tensor::MappingGuard output_guard(output); + const T *input_ptr = input->data(); + T *output_ptr = output->mutable_data(); + + if (d2s_) { +#pragma omp parallel for + for (int b = 0; b < batch_size; ++b) { + for (int h = 0; h < output_height; ++h) { + const int in_h = h / block_size_; + const int offset_h = (h % block_size_); + for (int w = 0; w < output_width; ++w) { + const int in_w = w / block_size_; + const int offset_w = w % block_size_; + const int offset_d = + (offset_h * block_size_ + offset_w) * output_depth; + for (int d = 0; d < output_depth; ++d) { + const int in_d = d + offset_d; + const int o_index = + ((b * output_height + h) * output_width + w) * output_depth + + d; + const int i_index = + ((b * input_height + in_h) * input_width + in_w) * + input_depth + + in_d; + output_ptr[o_index] = input_ptr[i_index]; + } + } + } + } + } else { +#pragma omp parallel for + for (int b = 0; b < batch_size; ++b) { + for (int h = 0; h < input_height; ++h) { + const int out_h = h / block_size_; + const int offset_h = (h % block_size_); + for (int w = 0; w < input_width; ++w) { + const int out_w = w / block_size_; + const int offset_w = (w % block_size_); + const int offset_d = + (offset_h * block_size_ + offset_w) * input_depth; + for (int d = 0; d < input_depth; ++d) { + const int out_d = d + offset_d; + const int o_index = + ((b * output_height + out_h) * output_width + out_w) * + output_depth + + out_d; + const int i_index = + ((b * input_height + h) * input_width + w) * input_depth + d; + output_ptr[o_index] = input_ptr[i_index]; + } + } + } + } + } + } + + const int block_size_; + bool d2s_; +}; + +template +struct DepthToSpaceOpFunctor { + DepthToSpaceOpFunctor(const int block_size, bool d2s) + : block_size_(block_size), d2s_(d2s) {} + void operator()(const Tensor *input, Tensor *output, StatsFuture *future); + + cl::Kernel kernel_; + const int block_size_; + bool d2s_; + std::vector input_shape_; +}; + +} // namespace kernels +} // namespace mace + +#endif // MACE_KERNELS_DEPTH_TO_SPACE_H_ diff --git a/mace/kernels/opencl/cl/depth_to_space.cl b/mace/kernels/opencl/cl/depth_to_space.cl new file mode 100644 index 0000000000000000000000000000000000000000..824f82665542975da3b000d2e0b1865ceabf4a3c --- /dev/null +++ b/mace/kernels/opencl/cl/depth_to_space.cl @@ -0,0 +1,52 @@ +#include + +__kernel void depth_to_space(__read_only image2d_t input, + __private const int block_size, + __private const int output_depth, + __write_only image2d_t output) { + const int out_d = get_global_id(0); + const int out_w = get_global_id(1); + const int out_h = get_global_id(2); + const int output_width = get_global_size(1); + + const int out_pos = mad24(out_d, output_width, out_w); + + const int input_width = output_width / block_size; + + const int in_h = out_h / block_size; + const int offset_h = out_h % block_size; + const int in_w = out_w / block_size; + const int offset_w = out_w % block_size; + + const int offset_d = (offset_h * block_size + offset_w) * output_depth; + const int in_d = out_d + offset_d; + + const int in_pos = mad24(in_d, input_width, in_w); + + DATA_TYPE4 in_data = READ_IMAGET(input, SAMPLER, (int2)(in_pos, in_h)); + WRITE_IMAGET(output, (int2)(out_pos, out_h), in_data); +} + +__kernel void space_to_depth(__read_only image2d_t input, + __private const int block_size, + __private const int input_depth, + __write_only image2d_t output) { + const int d = get_global_id(0); + const int w = get_global_id(1); + const int h = get_global_id(2); + const int input_width = get_global_size(1); + const int in_pos = mad24(d, input_width, w); + const int output_width = input_width / block_size; + + const int out_h = h / block_size; + const int offset_h = h % block_size; + const int out_w = w / block_size; + const int offset_w = w % block_size; + const int offset_d = (offset_h * block_size + offset_w) * input_depth; + const int out_d = d + offset_d; + const int out_pos = mad24(out_d, output_width, out_w); + + DATA_TYPE4 in_data = READ_IMAGET(input, SAMPLER, (int2)(in_pos, h)); + + WRITE_IMAGET(output, (int2)(out_pos, out_h), in_data); +} diff --git a/mace/kernels/opencl/depth_to_space_opencl.cc b/mace/kernels/opencl/depth_to_space_opencl.cc new file mode 100644 index 0000000000000000000000000000000000000000..c39c1a342c837e7aef4e9b5da03e401b012fc5e2 --- /dev/null +++ b/mace/kernels/opencl/depth_to_space_opencl.cc @@ -0,0 +1,96 @@ +// +// Copyright (c) 2018 XiaoMi All rights reserved. +// + +#include "mace/kernels/depth_to_space.h" +#include "mace/core/runtime/opencl/cl2_header.h" +#include "mace/core/runtime/opencl/opencl_runtime.h" +#include "mace/kernels/opencl/helper.h" +#include "mace/utils/tuner.h" +#include "mace/utils/utils.h" + +namespace mace { +namespace kernels { + +template +void DepthToSpaceOpFunctor::operator()( + const Tensor *input, Tensor *output, StatsFuture *future) { + const index_t batch = input->dim(0); + const index_t input_height = input->dim(1); + const index_t input_width = input->dim(2); + const index_t input_depth = input->dim(3); + + int depth_blocks = 1; + const char *kernel_name = nullptr; + + index_t output_height, output_width, output_depth; + if (d2s_) { + output_height = input_height * block_size_; + output_width = input_width * block_size_; + output_depth = input_depth / (block_size_ * block_size_); + depth_blocks = RoundUpDiv4(output_depth); + kernel_name = "depth_to_space"; + } else { + output_height = input_height / block_size_; + output_width = input_width / block_size_; + output_depth = input_depth * block_size_ * block_size_; + depth_blocks = RoundUpDiv4(input_depth); + kernel_name = "space_to_depth"; + } + + std::vector output_shape = {batch, output_height, output_width, + output_depth}; + + std::vector image_shape; + CalImage2DShape(output_shape, BufferType::IN_OUT_CHANNEL, &image_shape); + output->ResizeImage(output_shape, image_shape); + + if (kernel_.get() == nullptr) { + auto runtime = OpenCLRuntime::Global(); + std::set built_options; + std::string obfuscated_kernel_name = MACE_OBFUSCATE_SYMBOL(kernel_name); + std::stringstream kernel_name_ss; + kernel_name_ss << "-D" << kernel_name << "=" << obfuscated_kernel_name; + built_options.emplace(kernel_name_ss.str()); + auto dt = DataTypeToEnum::value; + built_options.emplace("-DDATA_TYPE=" + DtToUpstreamCLDt(dt)); + built_options.emplace("-DCMD_DATA_TYPE=" + DtToUpstreamCLCMDDt(dt)); + kernel_ = + runtime->BuildKernel("depth_to_space", kernel_name, built_options); + } + if (!IsVecEqual(input_shape_, input->shape())) { + uint32_t idx = 0; + kernel_.setArg(idx++, *(input->opencl_image())); + kernel_.setArg(idx++, block_size_); + kernel_.setArg(idx++, depth_blocks); + kernel_.setArg(idx++, *(output->opencl_image())); + input_shape_ = input->shape(); + } + + if (d2s_) { + const uint32_t gws[3] = {static_cast(depth_blocks), + static_cast(output_width), + static_cast(output_height * batch)}; + const std::vector lws = {8, 16, 8, 1}; + std::stringstream ss; + ss << "depth_to_space_opencl_kernel_" << output->dim(0) << "_" + << output->dim(1) << "_" << output->dim(2) << "_" << output->dim(3); + + TuningOrRun3DKernel(kernel_, ss.str(), gws, lws, future); + } else { + const uint32_t gws[3] = {static_cast(depth_blocks), + static_cast(input_width), + static_cast(input_height * batch)}; + const std::vector lws = {8, 16, 8, 1}; + std::stringstream ss; + ss << "space_to_depth_opencl_kernel_" << input->dim(0) << "_" + << input->dim(1) << "_" << input->dim(2) << "_" << input->dim(3); + TuningOrRun3DKernel(kernel_, ss.str(), gws, lws, future); + } +} + +template struct DepthToSpaceOpFunctor; +template struct DepthToSpaceOpFunctor; + +} // namespace kernels +} // namespace mace diff --git a/mace/ops/depth_to_space.cc b/mace/ops/depth_to_space.cc new file mode 100644 index 0000000000000000000000000000000000000000..a8c4ef55bdef9dfe2c4290f7cf4e3215a852e6fb --- /dev/null +++ b/mace/ops/depth_to_space.cc @@ -0,0 +1,31 @@ +// +// Copyright (c) 2017 XiaoMi All rights reserved. +// + +#include "mace/ops/depth_to_space.h" + +namespace mace { +namespace ops { + +void Register_DepthToSpace(OperatorRegistry *op_registry) { + REGISTER_OPERATOR(op_registry, OpKeyBuilder("DepthToSpace") + .Device(DeviceType::CPU) + .TypeConstraint("T") + .Build(), + DepthToSpaceOp); + + REGISTER_OPERATOR(op_registry, OpKeyBuilder("DepthToSpace") + .Device(DeviceType::OPENCL) + .TypeConstraint("T") + .Build(), + DepthToSpaceOp); + + REGISTER_OPERATOR(op_registry, OpKeyBuilder("DepthToSpace") + .Device(DeviceType::OPENCL) + .TypeConstraint("T") + .Build(), + DepthToSpaceOp); +} + +} // namespace ops +} // namespace mace diff --git a/mace/ops/depth_to_space.h b/mace/ops/depth_to_space.h new file mode 100644 index 0000000000000000000000000000000000000000..78ff39191943f1cc7c215e219fcdec607d3e6718 --- /dev/null +++ b/mace/ops/depth_to_space.h @@ -0,0 +1,53 @@ +// +// Copyright (c) 2017 XiaoMi All rights reserved. +// + +#ifndef MACE_OPS_DEPTH_TO_SPACE_H_ +#define MACE_OPS_DEPTH_TO_SPACE_H_ + +#include +#include + +#include "mace/core/operator.h" +#include "mace/kernels/depth_to_space.h" + +namespace mace { +namespace ops { + +template +class DepthToSpaceOp : public Operator { + public: + DepthToSpaceOp(const OperatorDef &op_def, Workspace *ws) + : Operator(op_def, ws), + functor_(OperatorBase::GetSingleArgument("block_size", 1), true) {} + + bool Run(StatsFuture *future) override { + const Tensor *input = this->Input(INPUT); + Tensor *output = this->Output(OUTPUT); + MACE_CHECK(input->dim_size() == 4, "input dim should be 4"); + + const int block_size = + OperatorBase::GetSingleArgument("block_size", 1); + + int input_depth = input->dim(3); + MACE_CHECK(input_depth % (block_size * block_size) == 0, + "input depth should be dividable by block_size * block_size", + input->dim(3)); + MACE_CHECK((input_depth % 4) == 0, + "input channel should be dividable by 4"); + functor_(input, output, future); + return true; + } + + protected: + OP_INPUT_TAGS(INPUT); + OP_OUTPUT_TAGS(OUTPUT); + + private: + kernels::DepthToSpaceOpFunctor functor_; +}; + +} // namespace ops +} // namespace mace + +#endif // MACE_OPS_DEPTH_TO_SPACE_H_ diff --git a/mace/ops/depth_to_space_benchmark.cc b/mace/ops/depth_to_space_benchmark.cc new file mode 100644 index 0000000000000000000000000000000000000000..c90a8bd81c278dc5dfc3a2470097234c6dbb39f6 --- /dev/null +++ b/mace/ops/depth_to_space_benchmark.cc @@ -0,0 +1,74 @@ +// +// Copyright (c) 2017 XiaoMi All rights reserved. +// + +#include "mace/core/operator.h" +#include "mace/core/testing/test_benchmark.h" +#include "mace/ops/ops_test_util.h" + +namespace mace { +namespace ops { +namespace test { + +template +static void DepthToSpace( + int iters, int batch, int channels, int height, int width, int block_size) { + mace::testing::StopTiming(); + + OpsTestNet net; + + // Add input data + net.AddRandomInput("Input", {batch, height, width, channels}); + + if (D == DeviceType::OPENCL) { + BufferToImage(&net, "Input", "InputImage", + kernels::BufferType::IN_OUT_CHANNEL); + + OpDefBuilder("DepthToSpace", "DepthToSpaceBM") + .Input("InputImage") + .Output("Output") + .AddIntArg("block_size", block_size) + .Finalize(net.NewOperatorDef()); + } else { + OpDefBuilder("DepthToSpace", "DepthToSpaceBM") + .Input("Input") + .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(); +} + +#define BM_DEPTH_TO_SPACE_MACRO(N, C, H, W, G, TYPE, DEVICE) \ + static void \ + BM_DEPTH_TO_SPACE_##N##_##C##_##H##_##W##_##G##_##TYPE##_##DEVICE( \ + int iters) { \ + const int64_t tot = static_cast(iters) * N * C * H * W; \ + mace::testing::MaccProcessed(tot); \ + mace::testing::BytesProcessed(tot *(sizeof(TYPE))); \ + DepthToSpace(iters, N, C, H, W, G); \ + } \ + BENCHMARK(BM_DEPTH_TO_SPACE_##N##_##C##_##H##_##W##_##G##_##TYPE##_##DEVICE) + +#define BM_DEPTH_TO_SPACE(N, C, H, W, G) \ + BM_DEPTH_TO_SPACE_MACRO(N, C, H, W, G, float, CPU); \ + BM_DEPTH_TO_SPACE_MACRO(N, C, H, W, G, float, OPENCL); \ + BM_DEPTH_TO_SPACE_MACRO(N, C, H, W, G, half, OPENCL); + +BM_DEPTH_TO_SPACE(1, 64, 64, 64, 4); +BM_DEPTH_TO_SPACE(1, 64, 128, 128, 4); +BM_DEPTH_TO_SPACE(1, 64, 256, 256, 4); + +} // namespace test +} // namespace ops +} // namespace mace diff --git a/mace/ops/depth_to_space_test.cc b/mace/ops/depth_to_space_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..ba31174d5362001d5484bec51130a0a0b1f3c018 --- /dev/null +++ b/mace/ops/depth_to_space_test.cc @@ -0,0 +1,177 @@ +// +// Copyright (c) 2017 XiaoMi All rights reserved. +// + +#include "mace/core/operator.h" +#include "mace/ops/ops_test_util.h" + +namespace mace { +namespace ops { +namespace test { + +template +void RunDepthToSpace(const bool d2s, + const std::vector &input_shape, + const std::vector &input_data, + const int block_size, + const std::vector &expected_shape, + const std::vector &expected_data) { + OpsTestNet net; + net.AddInputFromArray("Input", input_shape, input_data); + const char *ops_name = (d2s) ? "DepthToSpace" : "SpaceToDepth"; + const char *ops_test_name = (d2s) ? "DepthToSpaceTest" : "SpaceToDepthTest"; + // Construct graph + if (D == DeviceType::CPU) { + OpDefBuilder(ops_name, ops_test_name) + .Input("Input") + .Output("Output") + .AddIntArg("block_size", block_size) + .Finalize(net.NewOperatorDef()); + + } else { + BufferToImage(&net, "Input", "InputImage", + kernels::BufferType::IN_OUT_CHANNEL); + OpDefBuilder(ops_name, ops_test_name) + .Input("InputImage") + .Output("OutputImage") + .AddIntArg("block_size", block_size) + .Finalize(net.NewOperatorDef()); + } + // Run + net.RunOp(D); + + if (D == DeviceType::OPENCL) { + ImageToBuffer(&net, "OutputImage", "Output", + kernels::BufferType::IN_OUT_CHANNEL); + } + auto expected = CreateTensor(expected_shape, expected_data); + ExpectTensorNear(*expected, *net.GetOutput("Output"), 0.001); +} + +class SpaceToDepthOpTest : public OpsTestBase {}; + +TEST_F(SpaceToDepthOpTest, Input2x4x4_B2_CPU) { + RunDepthToSpace(false, {1, 2, 4, 4}, + {0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23, + 8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31}, + 2, + {1, 1, 2, 16}, + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}); +} + +TEST_F(SpaceToDepthOpTest, Input2x4x4_B2_OPENCL) { + RunDepthToSpace(false, {1, 2, 4, 4}, + {0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23, + 8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31}, + 2, + {1, 1, 2, 16}, + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}); +} + +TEST_F(SpaceToDepthOpTest, Input2x2x4_B2_CPU) { + RunDepthToSpace(false, {1, 2, 2, 4}, + {1, 2, 3, 4, 5, 6, 7, 8, + 9, 10, 11, 12, 13, 14, 15, 16}, + 2, + {1, 1, 1, 16}, + {1, 2, 3, 4, 5, 6, 7, 8, + 9, 10, 11, 12, 13, 14, 15, 16}); +} + +TEST_F(SpaceToDepthOpTest, Input4x4x1_B2_OPENCL) { + RunDepthToSpace(false, {1, 2, 2, 4}, + {1, 2, 3, 4, 5, 6, 7, 8, + 9, 10, 11, 12, 13, 14, 15, 16}, + 2, + {1, 1, 1, 16}, + {1, 2, 3, 4, 5, 6, 7, 8, + 9, 10, 11, 12, 13, 14, 15, 16}); +} + +class DepthToSpaceOpTest : public OpsTestBase {}; + +TEST_F(DepthToSpaceOpTest, Input1x2x16_B2_CPU) { + RunDepthToSpace(true, {1, 1, 2, 16}, + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, + 2, + {1, 2, 4, 4}, + {0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23, + 8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31}); +} + +TEST_F(DepthToSpaceOpTest, Input1x2x16_B2_OPENCL) { + RunDepthToSpace(true, {1, 1, 2, 16}, + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, + 2, + {1, 2, 4, 4}, + {0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23, + 8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31}); +} + +TEST_F(DepthToSpaceOpTest, Input1x1x16_B2_CPU) { + RunDepthToSpace(true, {1, 1, 1, 16}, + {1, 2, 3, 4, 5, 6, 7, 8, + 9, 10, 11, 12, 13, 14, 15, 16}, + 2, + {1, 2, 2, 4}, + {1, 2, 3, 4, 5, 6, 7, 8, + 9, 10, 11, 12, 13, 14, 15, 16}); +} + +TEST_F(DepthToSpaceOpTest, Input1x1x16_B2_OPENCL) { + RunDepthToSpace(true, {1, 1, 1, 16}, + {1, 2, 3, 4, 5, 6, 7, 8, + 9, 10, 11, 12, 13, 14, 15, 16}, + 2, + {1, 2, 2, 4}, + {1, 2, 3, 4, 5, 6, 7, 8, + 9, 10, 11, 12, 13, 14, 15, 16}); +} + +/* +TEST_F(DepthToSpaceOpTest, Input2x2x3_B2_CPU) { + + RunDepthToSpace({1, 2, 2, 3}, + {1, 2, 3, 4, 5, 6, + 7, 8, 9, 10, 11, 12}, + 2, + {1, 1, 1, 12}, + {1, 2, 3, 4, 5, 6, 7, 8, + 9, 10, 11, 12}); +} + +TEST_F(DepthToSpaceOpTest, Input2x2x3_B2_OPENCL) { + RunDepthToSpace({1, 2, 2, 6}, + {1, 2, 3, 4, 5, 6, + 7, 8, 9, 10, 11, 12 + }, + 2, + {1, 1, 1, 12}, + {1, 2, 3, 4, 5, 6, 7, 8, + 9, 10, 11, 12}); +} + +TEST_F(DepthToSpaceOpTest, Input2x2x2_B2_CPU) { + + RunDepthToSpace({1, 2, 2, 2}, + {1, 10, 2, 20, 3, 30, 4, 40}, + 2, + {1, 1, 1, 8}, + {1, 10, 2, 20, 3, 30, 4, 40}); +} + +TEST_F(DepthToSpaceOpTest, Input2x2x2_B2_OPENCL) { + + RunDepthToSpace({1, 2, 2, 2}, + {1, 10, 2, 20, 3, 30, 4, 40}, + 2, + {1, 1, 1, 8}, + {1, 10, 2, 20, 3, 30, 4, 40}); +}*/ +} // namespace test +} // namespace ops +} // namespace mace diff --git a/mace/ops/space_to_depth.cc b/mace/ops/space_to_depth.cc new file mode 100644 index 0000000000000000000000000000000000000000..55f1a13a4f80b5a88c1f318733f11db1abf2a872 --- /dev/null +++ b/mace/ops/space_to_depth.cc @@ -0,0 +1,31 @@ +// +// Copyright (c) 2017 XiaoMi All rights reserved. +// + +#include "mace/ops/space_to_depth.h" + +namespace mace { +namespace ops { + +void Register_SpaceToDepth(OperatorRegistry *op_registry) { + REGISTER_OPERATOR(op_registry, OpKeyBuilder("SpaceToDepth") + .Device(DeviceType::CPU) + .TypeConstraint("T") + .Build(), + SpaceToDepthOp); + + REGISTER_OPERATOR(op_registry, OpKeyBuilder("SpaceToDepth") + .Device(DeviceType::OPENCL) + .TypeConstraint("T") + .Build(), + SpaceToDepthOp); + + REGISTER_OPERATOR(op_registry, OpKeyBuilder("SpaceToDepth") + .Device(DeviceType::OPENCL) + .TypeConstraint("T") + .Build(), + SpaceToDepthOp); +} + +} // namespace ops +} // namespace mace diff --git a/mace/ops/space_to_depth.h b/mace/ops/space_to_depth.h new file mode 100644 index 0000000000000000000000000000000000000000..517d8ccc8f8938214aefc50cfea091133d455466 --- /dev/null +++ b/mace/ops/space_to_depth.h @@ -0,0 +1,55 @@ +// +// Copyright (c) 2017 XiaoMi All rights reserved. +// + +#ifndef MACE_OPS_SPACE_TO_DEPTH_H_ +#define MACE_OPS_SPACE_TO_DEPTH_H_ + +#include +#include + +#include "mace/core/operator.h" +#include "mace/kernels/depth_to_space.h" + +namespace mace { +namespace ops { + +template +class SpaceToDepthOp : public Operator { + public: + SpaceToDepthOp(const OperatorDef &op_def, Workspace *ws) + : Operator(op_def, ws), + functor_(OperatorBase::GetSingleArgument("block_size", 1), false) { + } + + bool Run(StatsFuture *future) override { + const Tensor *input = this->Input(INPUT); + Tensor *output = this->Output(OUTPUT); + MACE_CHECK(input->dim_size() == 4, "input dim should be 4"); + const int block_size = + OperatorBase::GetSingleArgument("block_size", 1); + const int input_height = input->dim(1); + const int input_width = input->dim(2); + const int input_depth = input->dim(3); + MACE_CHECK((input_depth % 4) == 0, + "input channel should be dividable by 4"); + MACE_CHECK( + (input_width%block_size == 0)&&(input_height%block_size == 0), + "input width and height should be dividable by block_size", + input->dim(3)); + functor_(input, output, future); + return true; + } + + protected: + OP_INPUT_TAGS(INPUT); + OP_OUTPUT_TAGS(OUTPUT); + + private: + kernels::DepthToSpaceOpFunctor functor_; +}; + +} // namespace ops +} // namespace mace + +#endif // MACE_OPS_SPACE_TO_DEPTH_H_ diff --git a/mace/ops/space_to_depth_benchmark.cc b/mace/ops/space_to_depth_benchmark.cc new file mode 100644 index 0000000000000000000000000000000000000000..c97028c4c85cd792769f4fd69fc19ffe9a1280c0 --- /dev/null +++ b/mace/ops/space_to_depth_benchmark.cc @@ -0,0 +1,74 @@ +// +// Copyright (c) 2017 XiaoMi All rights reserved. +// + +#include "mace/core/operator.h" +#include "mace/core/testing/test_benchmark.h" +#include "mace/ops/ops_test_util.h" + +namespace mace { +namespace ops { +namespace test { + +template +static void SpaceToDepth( + int iters, int batch, int channels, int height, int width, int block_size) { + mace::testing::StopTiming(); + + OpsTestNet net; + + // Add input data + net.AddRandomInput("Input", {batch, height, width, channels}); + + if (D == DeviceType::OPENCL) { + BufferToImage(&net, "Input", "InputImage", + kernels::BufferType::IN_OUT_CHANNEL); + + OpDefBuilder("SpaceToDepth", "SpaceToDepthBM") + .Input("InputImage") + .Output("Output") + .AddIntArg("block_size", block_size) + .Finalize(net.NewOperatorDef()); + } else { + OpDefBuilder("SpaceToDepth", "SpaceToDepthBM") + .Input("Input") + .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(); +} + +#define BM_SPACE_TO_DEPTH_MACRO(N, C, H, W, G, TYPE, DEVICE) \ + static void \ + BM_SPACE_TO_DEPTH_##N##_##C##_##H##_##W##_##G##_##TYPE##_##DEVICE( \ + int iters) { \ + const int64_t tot = static_cast(iters) * N * C * H * W; \ + mace::testing::MaccProcessed(tot); \ + mace::testing::BytesProcessed(tot *(sizeof(TYPE))); \ + SpaceToDepth(iters, N, C, H, W, G); \ + } \ + BENCHMARK(BM_SPACE_TO_DEPTH_##N##_##C##_##H##_##W##_##G##_##TYPE##_##DEVICE) + +#define BM_SPACE_TO_DEPTH(N, C, H, W, G) \ + BM_SPACE_TO_DEPTH_MACRO(N, C, H, W, G, float, CPU); \ + BM_SPACE_TO_DEPTH_MACRO(N, C, H, W, G, float, OPENCL); \ + BM_SPACE_TO_DEPTH_MACRO(N, C, H, W, G, half, OPENCL); + +BM_SPACE_TO_DEPTH(1, 64, 64, 64, 4); +BM_SPACE_TO_DEPTH(1, 64, 128, 128, 4); +BM_SPACE_TO_DEPTH(1, 64, 256, 256, 4); + +} // namespace test +} // namespace ops +} // namespace mace