1 //
2 //  SoftmaxBufExecution.cpp
3 //  MNN
4 //
5 //  Created by MNN on 2019/02/28.
6 //  Copyright © 2018, Alibaba Group Holding Limited
7 //
8 
9 #ifndef MNN_OPENCL_BUFFER_CLOSED
10 
11 #include "backend/opencl/execution/buffer/SoftmaxBufExecution.hpp"
12 #include "core/Macro.h"
13 #include "backend/opencl/core/OpenCLRunningUtils.hpp"
14 
15 namespace MNN {
16 namespace OpenCL {
17 
SoftmaxBufExecution(const std::vector<Tensor * > & inputs,int axis,Backend * backend)18 SoftmaxBufExecution::SoftmaxBufExecution(const std::vector<Tensor *> &inputs, int axis, Backend *backend)
19     : Execution(backend) {
20     mAxis          = axis;
21     mOpenCLBackend = static_cast<OpenCLBackend *>(backend);
22     buildSoftmaxKernel();
23 }
24 
buildSoftmaxKernel()25 bool SoftmaxBufExecution::buildSoftmaxKernel() {
26     auto runtime = mOpenCLBackend->getOpenCLRuntime();
27     if (mKernel.get() == nullptr) {
28         std::set<std::string> buildOptions;
29         std::string kernelName;
30         if (mAxis == 1) {
31             mKernel           = runtime->buildKernel("softmax_buf", "softmax_channel", buildOptions);
32         } else if (mAxis == 2) {
33             mKernel           = runtime->buildKernel("softmax_buf", "softmax_height", buildOptions);
34         } else {
35             MNN_ASSERT(mAxis == 3);
36             mKernel           = runtime->buildKernel("softmax_buf", "softmax_width", buildOptions);
37         }
38         mMaxWorkGroupSize = static_cast<uint32_t>(runtime->getMaxWorkGroupSize(mKernel));
39     }
40     return true;
41 }
42 
onResize(const std::vector<Tensor * > & inputs,const std::vector<Tensor * > & outputs)43 ErrorCode SoftmaxBufExecution::onResize(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) {
44     Tensor *input  = inputs[0];
45     Tensor *output = outputs[0];
46 
47     std::vector<int> inputShape  = tensorShapeFormat(input);
48     std::vector<int> outputShape = tensorShapeFormat(output);
49 
50     const int outputBatch    = outputShape.at(0);
51     const int outputHeight   = outputShape.at(1);
52     const int outputWidth    = outputShape.at(2);
53     const int outputChannels = outputShape.at(3);
54 
55     const int channelBlocks  = UP_DIV(outputChannels, 4);
56     const int remainChannels = channelBlocks * 4 - outputChannels;
57     if (mAxis == 1) {
58         mGlobalWorkSize = {static_cast<uint32_t>(channelBlocks), static_cast<uint32_t>(outputWidth),
59             static_cast<uint32_t>(outputHeight * outputBatch)};
60         int shape[] = {outputBatch, channelBlocks, outputHeight, outputWidth};
61 
62         uint32_t idx    = 0;
63         mKernel.setArg(idx++, mGlobalWorkSize[0]);
64         mKernel.setArg(idx++, mGlobalWorkSize[1]);
65         mKernel.setArg(idx++, mGlobalWorkSize[2]);
66 
67         mKernel.setArg(idx++, openCLBuffer(input));
68         mKernel.setArg(idx++, openCLBuffer(output));
69         mKernel.setArg(idx++, static_cast<int>(outputChannels));
70         mKernel.setArg(idx++, remainChannels);
71         mKernel.setArg(idx++, shape);
72 
73         std::string kernelName = "softmax_buf_channel";
74         mLocalWorkSize =
75         localWS3DDefault(mGlobalWorkSize, mMaxWorkGroupSize, mOpenCLBackend->getOpenCLRuntime(), kernelName, mKernel).first;
76     } else if (mAxis == 2){
77         mGlobalWorkSize = {(uint32_t)channelBlocks*outputWidth, (uint32_t)outputBatch, 1};
78         int shape[] = {outputBatch, channelBlocks, outputHeight, outputWidth};
79         mKernel.setArg(0, openCLBuffer(input));
80         mKernel.setArg(1, openCLBuffer(output));
81         mKernel.setArg(2, shape);
82 
83         std::string kernelName = "softmax_buf_height";
84         mLocalWorkSize =
85         localWS3DDefault(mGlobalWorkSize, mMaxWorkGroupSize, mOpenCLBackend->getOpenCLRuntime(), kernelName, mKernel).first;
86     } else {
87         MNN_ASSERT(mAxis == 3);
88         mGlobalWorkSize = {(uint32_t)channelBlocks, (uint32_t)outputBatch*outputHeight, 1};
89         int shape[] = {outputBatch, channelBlocks, outputHeight, outputWidth};
90         mKernel.setArg(0, openCLBuffer(input));
91         mKernel.setArg(1, openCLBuffer(output));
92         mKernel.setArg(2, shape);
93 
94         std::string kernelName = "softmax_buf_width";
95         mLocalWorkSize =
96         localWS3DDefault(mGlobalWorkSize, mMaxWorkGroupSize, mOpenCLBackend->getOpenCLRuntime(), kernelName, mKernel).first;
97     }
98 
99     return NO_ERROR;
100 }
101 
onExecute(const std::vector<Tensor * > & inputs,const std::vector<Tensor * > & outputs)102 ErrorCode SoftmaxBufExecution::onExecute(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs) {
103 #ifdef LOG_VERBOSE
104     MNN_PRINT("start SoftmaxBufExecution onExecute !\n");
105 #endif
106 
107 #ifdef ENABLE_OPENCL_TIME_PROFILER
108     cl::Event event;
109     run3DKernelDefault(mKernel, mGlobalWorkSize, mLocalWorkSize,
110                        mOpenCLBackend->getOpenCLRuntime(), &event);
111 
112     int costTime = (int)mOpenCLBackend->getOpenCLRuntime()->getCostTime(&event);
113     MNN_PRINT("kernel cost:%d    us Softmax\n",costTime);
114 #else
115     run3DKernelDefault(mKernel, mGlobalWorkSize, mLocalWorkSize, mOpenCLBackend->getOpenCLRuntime());
116 #endif
117 
118 #ifdef LOG_VERBOSE
119     MNN_PRINT("end SoftmaxBufExecution onExecute !\n");
120 #endif
121 
122     return NO_ERROR;
123 }
124 
125 class SoftmaxBufCreator : public OpenCLBackend::Creator {
126 public:
onCreate(const std::vector<Tensor * > & inputs,const std::vector<Tensor * > & outputs,const MNN::Op * op,Backend * backend) const127     virtual Execution *onCreate(const std::vector<Tensor *> &inputs, const std::vector<Tensor *> &outputs,
128                                 const MNN::Op *op, Backend *backend) const override {
129         if(inputs[0]->dimensions() == 3 || outputs[0]->dimensions() == 3){
130             MNN_PRINT("softmax not support dimensions == 3 \n");
131             return nullptr;
132         }
133         auto dimType = inputs[0]->getDimensionType();
134         if (dimType == Tensor::TENSORFLOW && inputs[0]->dimensions() == 4) {
135             int index[4] = {0, 2, 3, 1};
136             auto axis = op->main_as_Axis()->axis();
137             if (axis < 0) {
138                 axis = inputs[0]->dimensions() + axis;
139             }
140 
141             axis = index[axis];
142             //1 : channel //2 : height
143             if (1 == axis || 2 == axis || 3 == axis) {
144                 return new SoftmaxBufExecution(inputs, axis, backend);
145             }
146             return nullptr;
147         } else {
148             auto axis = op->main_as_Axis()->axis();
149             if (axis < 0) {
150                 axis = inputs[0]->dimensions() + axis;
151             }
152 
153             if (1 == axis || 2 == axis || 3 == axis) {
154                 return new SoftmaxBufExecution(inputs, axis, backend);
155             }
156             return nullptr;
157         }
158     }
159 };
160 OpenCLCreatorRegister<SoftmaxBufCreator> __SoftmaxBuf_op(OpType_Softmax, BUFFER);
161 
162 } // namespace OpenCL
163 } // namespace MNN
164 #endif/* MNN_OPENCL_BUFFER_CLOSED */
165 
166