提交 c436fd15 编写于 作者: D dolphin8

reshape & relu & softmax

上级 a01da691
/* 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
/* 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
/* 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);
}
...@@ -11,6 +11,7 @@ distributed under the License is distributed on an "AS IS" BASIS, ...@@ -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. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. */ limitations under the License. */
#ifdef RELU_OP
#include "operators/kernel/relu_kernel.h" #include "operators/kernel/relu_kernel.h"
...@@ -19,13 +20,25 @@ namespace operators { ...@@ -19,13 +20,25 @@ namespace operators {
template <> template <>
bool ReluKernel<GPU_CL, float>::Init(ReluParam<GPU_CL> *param) { bool ReluKernel<GPU_CL, float>::Init(ReluParam<GPU_CL> *param) {
this->cl_helper_.AddKernel("relu", "relu.cl");
return true; return true;
} }
template <> template <>
void ReluKernel<GPU_CL, float>::Compute(const ReluParam<GPU_CL> &param) {} void ReluKernel<GPU_CL, float>::Compute(const ReluParam<GPU_CL> &param) {
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<GPU_CL, float>; template class ReluKernel<GPU_CL, float>;
} // namespace operators } // namespace operators
} // namespace paddle_mobile } // namespace paddle_mobile
#endif
\ No newline at end of file
...@@ -21,11 +21,28 @@ namespace operators { ...@@ -21,11 +21,28 @@ namespace operators {
template <> template <>
bool SoftmaxKernel<GPU_CL, float>::Init(SoftmaxParam<GPU_CL> *param) { bool SoftmaxKernel<GPU_CL, float>::Init(SoftmaxParam<GPU_CL> *param) {
this->cl_helper_.AddKernel("softmax", "softmax.cl");
return true; return true;
} }
template <> template <>
void SoftmaxKernel<GPU_CL, float>::Compute(const SoftmaxParam<GPU_CL> &param) {} void SoftmaxKernel<GPU_CL, float>::Compute(const SoftmaxParam<GPU_CL> &param) {
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<GPU_CL, float>; template class SoftmaxKernel<GPU_CL, float>;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册