reshape_image_compute.cc 9.1 KB
Newer Older
X
xiebaiyuan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2019 PaddlePaddle Authors. 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.

15
#include "lite/backends/opencl/cl_half.h"
X
xiebaiyuan 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#include "lite/backends/opencl/cl_include.h"
#include "lite/core/kernel.h"
#include "lite/core/op_registry.h"
#include "lite/kernels/opencl/image_helper.h"
#include "lite/operators/op_params.h"
#include "lite/utils/logging.h"
#include "lite/utils/replace_stl/stream.h"

namespace paddle {
namespace lite {
namespace kernels {
namespace opencl {

// reshape operator
class ReshapeComputeFloatImage : public KernelLite<TARGET(kOpenCL),
31
                                                   PRECISION(kFP16),
X
xiebaiyuan 已提交
32 33 34 35 36 37
                                                   DATALAYOUT(kImageDefault)> {
 public:
  using param_t = operators::ReshapeParam;

  void PrepareForRun() override {
    auto& context = ctx_->As<OpenCLContext>();
38
    VLOG(1) << "kernel_func_name_:" << kernel_func_name_;
39 40 41 42
    kernel_ = context.cl_context()->CreateKernel(kernel_func_name_,
                                                 "image/reshape_kernel.cl",
                                                 build_options_,
                                                 time_stamp_);
X
xiebaiyuan 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55
  }

  void Run() override {
    auto& param = *param_.get_mutable<param_t>();
    const Tensor* const x = param.x;

    const auto x_dims = x->dims();
    const std::map<std::string, size_t>& input_image_shape =
        InitImageDimInfoWith(x_dims);

    const int64_t& input_image_width = input_image_shape.at("width");
    const int64_t& input_image_height = input_image_shape.at("height");

56
    const cl::Image2D* const x_image = x->data<half_t, cl::Image2D>();
X
xiebaiyuan 已提交
57 58 59 60 61 62 63 64

    const std::vector<int>& shape_vct = param.shape_vct;
    Tensor* const output = param.output;
    const DDimLite& out_dims = output->dims();
    VLOG(4) << "out_dims= " << out_dims;

    const std::map<std::string, size_t>& out_image_shape =
        InitImageDimInfoWith(out_dims);
65
    cl::Image2D* const out_image = output->mutable_data<half_t, cl::Image2D>(
X
xiebaiyuan 已提交
66
        out_image_shape.at("width"), out_image_shape.at("height"));
67
#ifndef LITE_SHUTDOWN_LOG
68
    VLOG(4) << "out_dims=   " << out_dims;
69
#endif
X
xiebaiyuan 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    const std::vector<size_t>& default_work_size = DefaultWorkSize(
        out_dims,
        DDim(std::vector<DDim::value_type>{
            static_cast<int64_t>(out_image_shape.at("width")),
            static_cast<int64_t>(out_image_shape.at("height"))}));

    int x_v_dims[4] = {1, 1, 1, 1};
    int out_v_dims[4] = {1, 1, 1, 1};
    // 1 1000 1 1
    for (int i = 0; i < x_dims.size(); i++) {
      x_v_dims[4 - x_dims.size() + i] = x_dims[i];
    }
    // 1 1 1 1000
    for (int i = 0; i < out_dims.size(); i++) {
      out_v_dims[4 - out_dims.size() + i] = out_dims[i];
    }

    int out_C = out_v_dims[1];
    int out_H = out_v_dims[2];
    int out_W = out_v_dims[3];
    int in_W = x_v_dims[3];
    int in_H = x_v_dims[2];
    int in_Stride0 = in_W;
    int in_Stride1 = x_v_dims[2] * x_v_dims[3];
    int in_Stride2 = x_v_dims[1] * x_v_dims[2] * x_v_dims[3];
    int out_Stride0 = out_W;
    int out_Stride1 = out_H * out_W;
    int out_Stride2 = out_C * out_H * out_W;
98 99

#ifndef LITE_SHUTDOWN_LOG
X
xiebaiyuan 已提交
100 101 102 103 104 105 106 107 108 109
    VLOG(4) << "out_C=" << out_C;
    VLOG(4) << "out_H=" << out_H;
    VLOG(4) << "out_W=" << out_W;
    VLOG(4) << "in_W=" << in_W;
    VLOG(4) << "default_work_size= " << default_work_size[0] << ", "
            << default_work_size[1] << ", " << default_work_size[2];
    VLOG(4) << "in_Stride0=" << in_Stride0;
    VLOG(4) << "in_Stride1=" << in_Stride1;
    VLOG(4) << "out_Stride0=" << out_Stride0;
    VLOG(4) << "out_Stride1=" << out_Stride1;
110
#endif
X
xiebaiyuan 已提交
111 112 113

    auto& context = ctx_->As<OpenCLContext>();
    CHECK(context.cl_context() != nullptr);
114 115

#ifndef LITE_SHUTDOWN_LOG
X
xiebaiyuan 已提交
116 117
    VLOG(4) << TargetToStr(x->target());
    VLOG(4) << TargetToStr(param.output->target());
118
#endif
X
xiebaiyuan 已提交
119 120

    cl_int status;
121
    status = kernel_->setArg(0, *x_image);
X
xiebaiyuan 已提交
122
    CL_CHECK_FATAL(status);
123
    status = kernel_->setArg(1, *out_image);
X
xiebaiyuan 已提交
124
    CL_CHECK_FATAL(status);
125
    status = kernel_->setArg(2, out_C);
X
xiebaiyuan 已提交
126
    CL_CHECK_FATAL(status);
127
    status = kernel_->setArg(3, out_H);
X
xiebaiyuan 已提交
128
    CL_CHECK_FATAL(status);
129
    status = kernel_->setArg(4, out_W);
X
xiebaiyuan 已提交
130
    CL_CHECK_FATAL(status);
131
    status = kernel_->setArg(5, in_W);
X
xiebaiyuan 已提交
132
    CL_CHECK_FATAL(status);
133
    status = kernel_->setArg(6, in_H);
X
xiebaiyuan 已提交
134
    CL_CHECK_FATAL(status);
135
    status = kernel_->setArg(7, in_Stride0);
X
xiebaiyuan 已提交
136
    CL_CHECK_FATAL(status);
137
    status = kernel_->setArg(8, in_Stride1);
X
xiebaiyuan 已提交
138
    CL_CHECK_FATAL(status);
139
    status = kernel_->setArg(9, in_Stride2);
X
xiebaiyuan 已提交
140
    CL_CHECK_FATAL(status);
141
    status = kernel_->setArg(10, out_Stride0);
X
xiebaiyuan 已提交
142
    CL_CHECK_FATAL(status);
143
    status = kernel_->setArg(11, out_Stride1);
X
xiebaiyuan 已提交
144
    CL_CHECK_FATAL(status);
145
    status = kernel_->setArg(12, out_Stride2);
X
xiebaiyuan 已提交
146 147 148 149 150 151 152 153
    CL_CHECK_FATAL(status);

    auto global_work_size =
        cl::NDRange{static_cast<size_t>(default_work_size.data()[0]),
                    static_cast<size_t>(default_work_size.data()[1]),
                    static_cast<size_t>(default_work_size.data()[2])};

    status = context.cl_context()->GetCommandQueue().enqueueNDRangeKernel(
154
        *(kernel_.get()),
X
xiebaiyuan 已提交
155 156 157 158 159 160 161 162 163 164 165
        cl::NullRange,
        global_work_size,
        cl::NullRange,
        nullptr,
        event_.get());
    CL_CHECK_FATAL(status);
    context.cl_wait_list()->emplace(out_image, event_);
  }

 private:
  std::string kernel_func_name_{"reshape"};
166
  std::string build_options_{"-DCL_DTYPE_half"};
167
  std::string time_stamp_{GetTimeStamp()};
X
xiebaiyuan 已提交
168
  std::shared_ptr<cl::Event> event_{new cl::Event};
169
  std::shared_ptr<cl::Kernel> kernel_;
X
xiebaiyuan 已提交
170 171 172 173 174 175 176 177 178
};

}  // namespace opencl
}  // namespace kernels
}  // namespace lite
}  // namespace paddle

REGISTER_LITE_KERNEL(reshape,
                     kOpenCL,
179
                     kFP16,
X
xiebaiyuan 已提交
180 181 182 183 184
                     kImageDefault,
                     paddle::lite::kernels::opencl::ReshapeComputeFloatImage,
                     image2d)
    .BindInput("X",
               {LiteType::GetTensorTy(TARGET(kOpenCL),
185
                                      PRECISION(kFP16),
X
xiebaiyuan 已提交
186 187 188 189 190
                                      DATALAYOUT(kImageDefault))})
    .BindInput("ShapeTensor", {LiteType::GetTensorTy(TARGET(kOpenCL))})
    .BindInput("Shape", {LiteType::GetTensorTy(TARGET(kOpenCL))})
    .BindOutput("Out",
                {LiteType::GetTensorTy(TARGET(kOpenCL),
191
                                       PRECISION(kFP16),
X
xiebaiyuan 已提交
192 193 194 195 196
                                       DATALAYOUT(kImageDefault))})
    .Finalize();

REGISTER_LITE_KERNEL(reshape2,
                     kOpenCL,
197
                     kFP16,
X
xiebaiyuan 已提交
198 199 200 201 202
                     kImageDefault,
                     paddle::lite::kernels::opencl::ReshapeComputeFloatImage,
                     image2d)
    .BindInput("X",
               {LiteType::GetTensorTy(TARGET(kOpenCL),
203
                                      PRECISION(kFP16),
X
xiebaiyuan 已提交
204 205
                                      DATALAYOUT(kImageDefault))})
    .BindInput("ShapeTensor", {LiteType::GetTensorTy(TARGET(kOpenCL))})
206 207
    .BindInput("Shape", {LiteType::GetTensorTy(TARGET(kARM))})
    .BindOutput("XShape", {LiteType::GetTensorTy(TARGET(kARM))})
X
xiebaiyuan 已提交
208 209
    .BindOutput("Out",
                {LiteType::GetTensorTy(TARGET(kOpenCL),
210
                                       PRECISION(kFP16),
X
xiebaiyuan 已提交
211 212
                                       DATALAYOUT(kImageDefault))})
    .Finalize();
213 214 215 216 217 218 219 220 221 222 223

REGISTER_LITE_KERNEL(flatten,
                     kOpenCL,
                     kFP16,
                     kImageDefault,
                     paddle::lite::kernels::opencl::ReshapeComputeFloatImage,
                     image2d)
    .BindInput("X",
               {LiteType::GetTensorTy(TARGET(kOpenCL),
                                      PRECISION(kFP16),
                                      DATALAYOUT(kImageDefault))})
224
    .BindInput("Shape", {LiteType::GetTensorTy(TARGET(kARM))})
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
    .BindOutput("Out",
                {LiteType::GetTensorTy(TARGET(kOpenCL),
                                       PRECISION(kFP16),
                                       DATALAYOUT(kImageDefault))})
    .Finalize();

REGISTER_LITE_KERNEL(flatten2,
                     kOpenCL,
                     kFP16,
                     kImageDefault,
                     paddle::lite::kernels::opencl::ReshapeComputeFloatImage,
                     image2d)
    .BindInput("X",
               {LiteType::GetTensorTy(TARGET(kOpenCL),
                                      PRECISION(kFP16),
                                      DATALAYOUT(kImageDefault))})
    .BindInput("Shape", {LiteType::GetTensorTy(TARGET(kOpenCL))})
242
    .BindOutput("XShape", {LiteType::GetTensorTy(TARGET(kARM))})
243 244 245 246 247
    .BindOutput("Out",
                {LiteType::GetTensorTy(TARGET(kOpenCL),
                                       PRECISION(kFP16),
                                       DATALAYOUT(kImageDefault))})
    .Finalize();