MNN/source/backend/opencl/execution/buffer/ScaleBufExecution.cpp

174 lines
6.9 KiB
C++
Raw Normal View History

2019-04-17 10:49:11 +08:00
//
// ScaleBufExecution.cpp
2019-04-17 10:49:11 +08:00
// MNN
//
// Created by MNN on 2019/02/28.
// Copyright © 2018, Alibaba Group Holding Limited
//
#ifndef MNN_OPENCL_BUFFER_CLOSED
#include "backend/opencl/execution/buffer/ScaleBufExecution.hpp"
2019-04-17 10:49:11 +08:00
namespace MNN {
namespace OpenCL {
ScaleBufExecution::ScaleBufExecution(const std::vector<Tensor *> &inputs, const MNN::Op *op, Backend *backend)
2024-04-19 11:58:21 +08:00
: CommonExecution(backend, op) {
2019-04-17 10:49:11 +08:00
#ifdef LOG_VERBOSE
MNN_PRINT("Start ScaleBufExecution init !\n");
2019-04-17 10:49:11 +08:00
#endif
2024-04-19 11:58:21 +08:00
mUnits.resize(1);
auto &unit = mUnits[0];
2019-04-17 10:49:11 +08:00
auto openclBackend = (OpenCLBackend *)backend;
mOpenCLBackend = static_cast<OpenCLBackend *>(backend);
const auto *scaleParams = op->main_as_Scale();
int scaleSize = scaleParams->scaleData()->size();
const float *scaleDataPtr = scaleParams->scaleData()->data();
2020-11-05 16:41:56 +08:00
int buffer_size = ALIGN_UP4(scaleSize);
2024-04-19 11:58:21 +08:00
if (mOpenCLBackend->getOpenCLRuntime()->isSupportedFP16()) {
2020-11-05 16:41:56 +08:00
buffer_size *= sizeof(half_float::half);
} else {
buffer_size *= sizeof(float);
}
mScale.reset(Tensor::createDevice<float>({1, 1, 1, ALIGN_UP4(scaleSize)}));
backend->onAcquireBuffer(mScale.get(), Backend::STATIC);
cl::Buffer &scaleBuffer = openCLBuffer(mScale.get());
cl_int error;
2019-04-17 10:49:11 +08:00
auto scalePtrCL = openclBackend->getOpenCLRuntime()->commandQueue().enqueueMapBuffer(
2020-11-05 16:41:56 +08:00
scaleBuffer, true, CL_MAP_WRITE, 0, buffer_size, nullptr, nullptr, &error);
if(nullptr != scalePtrCL && error == CL_SUCCESS){
2024-04-19 11:58:21 +08:00
if (mOpenCLBackend->getOpenCLRuntime()->isSupportedFP16()) {
2020-11-05 16:41:56 +08:00
for (int i = 0; i < scaleSize; i++) {
((half_float::half *)scalePtrCL)[i] = (half_float::half)(scaleDataPtr[i]);
}
for(int i=scaleSize; i<ALIGN_UP4(scaleSize); i++) {
((half_float::half*)scalePtrCL)[i] = (half_float::half)(0.0f);
}
} else {
::memset(scalePtrCL, 0, buffer_size);
::memcpy(scalePtrCL, scaleDataPtr, scaleSize * sizeof(float));
}
}else{
MNN_ERROR("Map error scalePtrCL == nullptr \n");
}
2019-04-17 10:49:11 +08:00
openclBackend->getOpenCLRuntime()->commandQueue().enqueueUnmapMemObject(scaleBuffer, scalePtrCL);
std::set<std::string> buildOptions;
if (nullptr != scaleParams->biasData() && nullptr != scaleParams->biasData()->data()) {
int biasSize = scaleParams->biasData()->size();
MNN_ASSERT(biasSize == scaleSize);
const float *biasDataPtr = scaleParams->biasData()->data();
2020-11-05 16:41:56 +08:00
int buffer_size = ALIGN_UP4(biasSize);
2024-04-19 11:58:21 +08:00
if (openclBackend->getOpenCLRuntime()->isSupportedFP16()) {
2020-11-05 16:41:56 +08:00
buffer_size *= sizeof(half_float::half);
} else {
buffer_size *= sizeof(float);
}
mBias.reset(Tensor::createDevice<float>({1, 1, 1, ALIGN_UP4(biasSize)}));
backend->onAcquireBuffer(mBias.get(), Backend::STATIC);
cl::Buffer &biasBuffer = openCLBuffer(mBias.get());
cl_int error;
2019-04-17 10:49:11 +08:00
auto biasPtrCL = openclBackend->getOpenCLRuntime()->commandQueue().enqueueMapBuffer(
2020-11-05 16:41:56 +08:00
biasBuffer, true, CL_MAP_WRITE, 0, buffer_size, nullptr, nullptr, &error);
if(nullptr != biasPtrCL && error == CL_SUCCESS){
2024-04-19 11:58:21 +08:00
if (mOpenCLBackend->getOpenCLRuntime()->isSupportedFP16()) {
2020-11-05 16:41:56 +08:00
for (int i = 0; i < biasSize; i++) {
((half_float::half *)biasPtrCL)[i] = (half_float::half)(biasDataPtr[i]);
}
for(int i=biasSize; i<ALIGN_UP4(biasSize); i++) {
((half_float::half*)biasPtrCL)[i] = (half_float::half)(0.0f);
}
} else {
::memset(biasPtrCL, 0, buffer_size);
::memcpy(biasPtrCL, biasDataPtr, biasSize * sizeof(float));
}
}else{
MNN_ERROR("Map error biasPtrCL == nullptr \n");
}
2019-04-17 10:49:11 +08:00
openclBackend->getOpenCLRuntime()->commandQueue().enqueueUnmapMemObject(biasBuffer, biasPtrCL);
2024-09-12 12:57:57 +08:00
mBuildOptions.emplace("-DBIAS");
2019-04-17 10:49:11 +08:00
mHasBias = true;
}
2019-04-17 10:49:11 +08:00
#ifdef LOG_VERBOSE
MNN_PRINT("end ScaleBufExecution init !\n");
2019-04-17 10:49:11 +08:00
#endif
}
ScaleBufExecution::~ScaleBufExecution() {
2024-04-19 11:58:21 +08:00
// Do nothing
2019-04-17 10:49:11 +08:00
}
2024-04-19 11:58:21 +08:00
ErrorCode ScaleBufExecution::onEncode(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) {
2019-04-17 10:49:11 +08:00
#ifdef LOG_VERBOSE
MNN_PRINT("Start ScaleBufExecution onResize !\n");
2019-04-17 10:49:11 +08:00
#endif
2024-04-19 11:58:21 +08:00
auto &unit = mUnits[0];
2019-04-17 10:49:11 +08:00
std::vector<int> inputShape = tensorShapeFormat(inputs[0]);
2023-12-27 17:26:44 +08:00
auto runtime = mOpenCLBackend->getOpenCLRuntime();
2019-04-17 10:49:11 +08:00
const int batch = inputShape.at(0);
const int height = inputShape.at(1);
const int width = inputShape.at(2);
const int channels = inputShape.at(3);
2024-09-12 12:57:57 +08:00
const int inside = width * height;
2019-04-17 10:49:11 +08:00
const int channelBlocks = UP_DIV(channels, 4);
2024-09-12 12:57:57 +08:00
std::set<std::string> buildOptions = mBuildOptions;
unit.kernel = runtime->buildKernel("scale_buf", "scale_buf", buildOptions);
mMaxWorkGroupSize = static_cast<uint32_t>(runtime->getMaxWorkGroupSize(unit.kernel));
2024-09-12 12:57:57 +08:00
mGlobalWorkSize = {static_cast<uint32_t>(inside),
static_cast<uint32_t>(channelBlocks * batch)};
uint32_t idx = 0;
2023-07-31 14:24:48 +08:00
cl_int ret = CL_SUCCESS;
2024-04-19 11:58:21 +08:00
ret |= unit.kernel->get().setArg(idx++, mGlobalWorkSize[0]);
ret |= unit.kernel->get().setArg(idx++, mGlobalWorkSize[1]);
ret |= unit.kernel->get().setArg(idx++, openCLBuffer(inputs[0]));
ret |= unit.kernel->get().setArg(idx++, openCLBuffer(mScale.get()));
2019-04-17 10:49:11 +08:00
if (mHasBias) {
2024-04-19 11:58:21 +08:00
ret |= unit.kernel->get().setArg(idx++, openCLBuffer(mBias.get()));
2019-04-17 10:49:11 +08:00
}
2024-04-19 11:58:21 +08:00
ret |= unit.kernel->get().setArg(idx++, openCLBuffer(outputs[0]));
2024-09-12 12:57:57 +08:00
ret |= unit.kernel->get().setArg(idx++, channelBlocks);
ret |= unit.kernel->get().setArg(idx++, batch);
ret |= unit.kernel->get().setArg(idx++, inside);
2023-07-31 14:24:48 +08:00
MNN_CHECK_CL_SUCCESS(ret, "setArg ScaleBufExecution");
std::string name = "scale_buf";
2024-04-19 11:58:21 +08:00
mLocalWorkSize = localWS2DDefault(mGlobalWorkSize, mMaxWorkGroupSize, mOpenCLBackend->getOpenCLRuntime(), name, unit.kernel).first;
2024-04-19 11:58:21 +08:00
mOpenCLBackend->recordKernel2d(unit.kernel, mGlobalWorkSize, mLocalWorkSize);
unit.globalWorkSize = {mGlobalWorkSize[0], mGlobalWorkSize[1]};
unit.localWorkSize = {mLocalWorkSize[0], mLocalWorkSize[1]};
2019-04-17 10:49:11 +08:00
return NO_ERROR;
}
2023-07-31 14:24:48 +08:00
class ScaleBufCreator : public OpenCLBackend::Creator {
public:
virtual ~ScaleBufCreator() = default;
virtual Execution *onCreate(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs,
const MNN::Op *op, Backend *backend) const override {
for (int i = 0; i < inputs.size(); ++i) {
TensorUtils::setTensorSupportPack(inputs[i], false);
}
for (int i = 0; i < outputs.size(); ++i) {
TensorUtils::setTensorSupportPack(outputs[i], false);
}
return new ScaleBufExecution(inputs, op, backend);
}
};
2023-12-27 17:26:44 +08:00
REGISTER_OPENCL_OP_CREATOR(ScaleBufCreator, OpType_Scale, BUFFER);
2019-04-17 10:49:11 +08:00
} // namespace OpenCL
} // namespace MNN
#endif /* MNN_OPENCL_BUFFER_CLOSED */