MNN/source/backend/opencl/execution/image/InterpExecution.cpp

117 lines
4.0 KiB
C++
Raw Normal View History

2019-04-17 10:49:11 +08:00
//
// InterpExecution.cpp
// MNN
//
// Created by MNN on 2019/02/28.
// Copyright © 2018, Alibaba Group Holding Limited
//
#include "backend/opencl/execution/image/InterpExecution.hpp"
2019-12-27 22:16:57 +08:00
#include "core/TensorUtils.hpp"
2019-04-17 10:49:11 +08:00
namespace MNN {
namespace OpenCL {
InterpExecution::InterpExecution(const std::vector<Tensor *> &inputs, const MNN::Op *op, Backend *backend)
: Execution(backend) {
mOpenCLBackend = static_cast<OpenCLBackend *>(backend);
auto runtime = mOpenCLBackend->getOpenCLRuntime();
2020-11-05 16:41:56 +08:00
auto interpParam = op->main_as_Interp();
mCordTransform[0] = interpParam->widthScale();
mCordTransform[1] = interpParam->widthOffset();
mCordTransform[2] = interpParam->heightScale();
mCordTransform[3] = interpParam->heightOffset();
2019-04-17 10:49:11 +08:00
std::set<std::string> buildOptions;
std::string kernelName = "interp";
if (op->main_as_Interp()->resizeType() == 1) {
mKernel = runtime->buildKernel("nearest", kernelName, buildOptions);
} else {
mKernel = runtime->buildKernel("interp", kernelName, buildOptions);
}
mMaxWorkGroupSize = static_cast<uint32_t>(runtime->getMaxWorkGroupSize(mKernel));
}
ErrorCode InterpExecution::onResize(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) {
2019-04-17 10:49:11 +08:00
Tensor *input = inputs[0];
Tensor *output = outputs[0];
2020-11-05 16:41:56 +08:00
auto runtime = ((OpenCLBackend *)backend())->getOpenCLRuntime();
2023-06-16 09:42:45 +08:00
startRecord(runtime, mRecording);
2019-04-17 10:49:11 +08:00
std::vector<int> inputShape = tensorShapeFormat(input);
std::vector<int> outputShape = tensorShapeFormat(output);
const int inputBatch = input->batch();
const int inputHeight = input->height();
const int inputWidth = input->width();
const int inputChannels = input->channel();
const int channelBlocks = UP_DIV(inputChannels, 4);
const int outputHeight = output->height();
const int outputWidth = output->width();
mGWS = {static_cast<uint32_t>(channelBlocks),
static_cast<uint32_t>(outputWidth),
static_cast<uint32_t>(outputHeight * inputBatch)};
MNN_ASSERT(outputHeight > 0 && outputWidth > 0);
uint32_t idx = 0;
mKernel.setArg(idx++, mGWS[0]);
mKernel.setArg(idx++, mGWS[1]);
mKernel.setArg(idx++, mGWS[2]);
mKernel.setArg(idx++, openCLImage(input));
mKernel.setArg(idx++, openCLImage(output));
2020-11-05 16:41:56 +08:00
mKernel.setArg(idx++, mCordTransform[2]);
mKernel.setArg(idx++, mCordTransform[0]);
mKernel.setArg(idx++, mCordTransform[3]);
mKernel.setArg(idx++, mCordTransform[1]);
mKernel.setArg(idx++, static_cast<int32_t>(inputHeight));
mKernel.setArg(idx++, static_cast<int32_t>(inputWidth));
mKernel.setArg(idx++, static_cast<int32_t>(outputHeight));
2020-11-05 16:41:56 +08:00
std::string name = "interp";
mLWS = localWS3DDefault(mGWS, mMaxWorkGroupSize, runtime, name, mKernel).first;
2023-06-16 09:42:45 +08:00
recordKernel3d(mKernel, mGWS, mLWS, mOpenCLBackend->getOpenCLRuntime());
endRecord(runtime, mRecording);
2020-06-16 17:11:54 +08:00
return NO_ERROR;
}
2019-04-17 10:49:11 +08:00
ErrorCode InterpExecution::onExecute(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) {
#ifdef LOG_VERBOSE
MNN_PRINT("Start InterpExecution onExecute... \n");
#endif
#ifdef ENABLE_OPENCL_TIME_PROFILER
cl::Event event;
run3DKernelDefault(mKernel, mGWS, mLWS,
mOpenCLBackend->getOpenCLRuntime(), &event);
int costTime = (int)mOpenCLBackend->getOpenCLRuntime()->getCostTime(&event);
MNN_PRINT("kernel cost:%d us Interp\n",costTime);
#else
2023-06-16 09:42:45 +08:00
if(mOpenCLBackend->getOpenCLRuntime()->isUseRecordQueue()){
mOpenCLBackend->getOpenCLRuntime()->getRecordings()->emplace_back(mRecording);
#ifdef LOG_VERBOSE
MNN_PRINT("End InterpExecution onExecute... \n");
#endif
return NO_ERROR;
}
run3DKernelDefault(mKernel, mGWS, mLWS, mOpenCLBackend->getOpenCLRuntime());
#endif
2019-04-17 10:49:11 +08:00
#ifdef LOG_VERBOSE
MNN_PRINT("end InterpExecution onExecute... \n");
#endif
return NO_ERROR;
}
OpenCLCreatorRegister<TypedCreator<InterpExecution>> __Interp_op_(OpType_Interp, IMAGE);
2019-04-17 10:49:11 +08:00
} // namespace OpenCL
} // namespace MNN