diff --git a/src/operators/kernel/cl/cl_kernel/relu.cl b/src/operators/kernel/cl/cl_kernel/relu.cl new file mode 100644 index 0000000000000000000000000000000000000000..e773d1c2577461abb35fabfa752ffc272970492b --- /dev/null +++ b/src/operators/kernel/cl/cl_kernel/relu.cl @@ -0,0 +1,25 @@ +/* Copyright (c) 2018 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. */ + +__kernel void relu(__read_only image2d_t input, + __write_only image2d_t output) + const int x = get_global_id(0); + const int y = get_global_id(1); + const sampler_t sampler = CLK_NORMALIZED_COORDS_TRUE | + CLK_ADDRESS_CLAMP | + CLK_FILTER_NEAREST; + half4 r = read_imageh(input, sampler, int2(x, y)); + r = max(half4(0, 0, 0, 0), r); + write_imageh(output, int2(x, y), r); +} \ No newline at end of file diff --git a/src/operators/kernel/cl/cl_kernel/reshape.cl b/src/operators/kernel/cl/cl_kernel/reshape.cl new file mode 100644 index 0000000000000000000000000000000000000000..4055445d1576b2ca54919ed03ad187d08cff14c2 --- /dev/null +++ b/src/operators/kernel/cl/cl_kernel/reshape.cl @@ -0,0 +1,49 @@ +/* Copyright (c) 2018 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. */ + +__kernel void reshape(__read_only image2d_t input, + __write_only image2d_t output, + __private const int d0, + __private const int d1, + __private const int d2, + __private const int d3, + __private const int x0, + __private const int x1, + __private const int x2, + __private const int x3) { + const int x = get_global_id(0); + const int y = get_global_id(1); + int obx = x / x3; + int oby = y / x2; + int ox = x % x3; + int oy = y % x2; + const sampler_t sampler = CLK_NORMALIZED_COORDS_TRUE | + CLK_ADDRESS_CLAMP | + CLK_FILTER_NEAREST; + half4 r; + for (int i = 0; i < 4; i++) { + int t = obx * 4 + i; + if (t > x1) break; + int oindex = oby * x1 * x2 * x3 + t * x2 * x3 + ox * x3 + oy; + int i0, i1, i2, i3; + int i3 = oindex % d3; oindex /= d3; + int i2 = oindex % d2; oindex /= d2; + int i1 = oindex % d1; oindex /= d1; + int i0 = oindex; + int ix = (i1 / 4) * d3 + i3; + int iy = i0 * d2 + i2; + r[i] = read_imageh(input, sampler, int2(ix, iy))[i1%4]; + } + write_imageh(output, int2(x, y), r); +} \ No newline at end of file diff --git a/src/operators/kernel/cl/cl_kernel/softmax.cl b/src/operators/kernel/cl/cl_kernel/softmax.cl new file mode 100644 index 0000000000000000000000000000000000000000..60f0cf409596632b67817cd236f9621010522571 --- /dev/null +++ b/src/operators/kernel/cl/cl_kernel/softmax.cl @@ -0,0 +1,41 @@ +/* Copyright (c) 2018 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. */ + +__kernel void softmax(__read_only image2d_t input, + __write_only image2d_t output, + __private const int d0, + __private const int d1, + __private const int d2, + __private const int d3) { + const int z = get_global_id(0); + const int x = get_global_id(1); + const int y = get_global_id(2); + const sampler_t sampler = CLK_NORMALIZED_COORDS_TRUE | + CLK_ADDRESS_CLAMP | + CLK_FILTER_NEAREST; + half4 maxv = read_imageh(input, sampler, int2(z * d3, y)); + half4 buf[d3] = {piece}; + for (int i = 1; i < d3; i++) { + buf[i] = read_imageh(input, sampler, int2(z * d3 + i, y)); + maxv = max(maxv, buf[i]); + } + float4 sum = 0; + for (int i = 0; i < d3; i++) { + buf[i] = exp(buf[i] - maxv); + sum += buf[i]; + } + half4 r = buf[x] / sum; + + write_imageh(output, int2(z * d3 + x, y), r); +} diff --git a/src/operators/kernel/cl/relu_kernel.cpp b/src/operators/kernel/cl/relu_kernel.cpp index f38c29f1827cd61b18a0dd59773e63169a4445a7..223841096c88e2705e6b2e4ca915a5f8067d2d8d 100644 --- a/src/operators/kernel/cl/relu_kernel.cpp +++ b/src/operators/kernel/cl/relu_kernel.cpp @@ -11,6 +11,7 @@ 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. */ +#ifdef RELU_OP #include "operators/kernel/relu_kernel.h" @@ -19,13 +20,25 @@ namespace operators { template <> bool ReluKernel::Init(ReluParam *param) { + this->cl_helper_.AddKernel("relu", "relu.cl"); return true; } template <> -void ReluKernel::Compute(const ReluParam ¶m) {} +void ReluKernel::Compute(const ReluParam ¶m) { + auto kernel = this->cl_helper_.KernelAt(0); + const auto* input = param.InputX(); + auto* output = parma.Out(); + auto default_work_size = this->cl_helper_.DefaultWorkSize(*output); + clSetKernelArg((kernel, 0, sizeof(cl_mem), &input.getCLImage()); + clSetKernelArg((kernel, 1, sizeof(cl_mem), &output.getCLImage()); + int work_size[2] = { input.ImageWidth(), input.ImageHeight() }; + clEnqueueNDRangeKernel(this->cl_helper_.CLCommandQueue(), kernel, 3, NULL, + work_size, NULL, 0, NULL, NULL); +} template class ReluKernel; } // namespace operators } // namespace paddle_mobile +#endif \ No newline at end of file diff --git a/src/operators/kernel/cl/softmax_kernel.cpp b/src/operators/kernel/cl/softmax_kernel.cpp index d0a97cf076c5fe22c7b2612629616053c63dec6c..f3c0de357c31b68d239c61910415fcce756dd4e7 100644 --- a/src/operators/kernel/cl/softmax_kernel.cpp +++ b/src/operators/kernel/cl/softmax_kernel.cpp @@ -21,11 +21,28 @@ namespace operators { template <> bool SoftmaxKernel::Init(SoftmaxParam *param) { + this->cl_helper_.AddKernel("softmax", "softmax.cl"); return true; } template <> -void SoftmaxKernel::Compute(const SoftmaxParam ¶m) {} +void SoftmaxKernel::Compute(const SoftmaxParam ¶m) { + auto kernel = this->cl_helper_.KernelAt(0); + auto default_work_size = this->cl_helper_.DefaultWorkSize(*(param.Out())); + auto & input = param.InputX(); + auto & output = param.Out(); + clSetKernelArg(kernel, 0, sizeof(cl_mem), &input.getCLImage()); + clSetKernelArg(kernel, 1, sizeof(cl_mem), &output.getCLImage()); + const auto & inputDim = input.dims(); + int dims[4] = {inputDim[0], inputDim[1], inputDim[2], inputDim[3]}; + clSetKernelArg(kernel, 2, sizeof(int), dims); + clSetKernelArg(kernel, 3, sizeof(int), dims+1); + clSetKernelArg(kernel, 4, sizeof(int), dims+2); + clSetKernelArg(kernel, 5, sizeof(int), dims+3); + + clEnqueueNDRangeKernel(this->cl_helper_.CLCommandQueue(), kernel, 3, NULL, + default_work_size.data(), NULL, 0, NULL, NULL); +} template class SoftmaxKernel;