-
Notifications
You must be signed in to change notification settings - Fork 39
/
brightness.hpp
257 lines (221 loc) · 12.4 KB
/
brightness.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include <hip/hip_runtime.h>
#include "rpp_hip_common.hpp"
__device__ void brightness_hip_compute(uchar *srcPtr, d_float8 *src_f8, d_float8 *dst_f8, float4 *alpha_f4, float4 *beta_f4)
{
dst_f8->f4[0] = src_f8->f4[0] * *alpha_f4 + *beta_f4;
dst_f8->f4[1] = src_f8->f4[1] * *alpha_f4 + *beta_f4;
}
__device__ void brightness_hip_compute(float *srcPtr, d_float8 *src_f8, d_float8 *dst_f8, float4 *alpha_f4, float4 *beta_f4)
{
dst_f8->f4[0] = src_f8->f4[0] * *alpha_f4 + *beta_f4 * (float4) ONE_OVER_255;
dst_f8->f4[1] = src_f8->f4[1] * *alpha_f4 + *beta_f4 * (float4) ONE_OVER_255;
}
__device__ void brightness_hip_compute(signed char *srcPtr, d_float8 *src_f8, d_float8 *dst_f8, float4 *alpha_f4, float4 *beta_f4)
{
dst_f8->f4[0] = rpp_hip_pixel_check_0to255((src_f8->f4[0] + (float4)128) * *alpha_f4 + *beta_f4) - (float4)128;
dst_f8->f4[1] = rpp_hip_pixel_check_0to255((src_f8->f4[1] + (float4)128) * *alpha_f4 + *beta_f4) - (float4)128;
}
__device__ void brightness_hip_compute(half *srcPtr, d_float8 *src_f8, d_float8 *dst_f8, float4 *alpha_f4, float4 *beta_f4)
{
dst_f8->f4[0] = src_f8->f4[0] * *alpha_f4 + *beta_f4 * (float4) ONE_OVER_255;
dst_f8->f4[1] = src_f8->f4[1] * *alpha_f4 + *beta_f4 * (float4) ONE_OVER_255;
}
template <typename T>
__global__ void brightness_pkd_hip_tensor(T *srcPtr,
uint2 srcStridesNH,
T *dstPtr,
uint2 dstStridesNH,
float *alpha,
float *beta,
RpptROIPtr roiTensorPtrSrc)
{
int id_x = (hipBlockIdx_x * hipBlockDim_x + hipThreadIdx_x) * 8;
int id_y = hipBlockIdx_y * hipBlockDim_y + hipThreadIdx_y;
int id_z = hipBlockIdx_z * hipBlockDim_z + hipThreadIdx_z;
if ((id_y >= roiTensorPtrSrc[id_z].xywhROI.roiHeight) || (id_x >= roiTensorPtrSrc[id_z].xywhROI.roiWidth * 3))
{
return;
}
uint srcIdx = (id_z * srcStridesNH.x) + ((id_y + roiTensorPtrSrc[id_z].xywhROI.xy.y) * srcStridesNH.y) + (id_x + roiTensorPtrSrc[id_z].xywhROI.xy.x * 3);
uint dstIdx = (id_z * dstStridesNH.x) + (id_y * dstStridesNH.y) + id_x;
float4 alpha_f4 = (float4)alpha[id_z];
float4 beta_f4 = (float4)beta[id_z];
d_float8 src_f8, dst_f8;
rpp_hip_load8_and_unpack_to_float8(srcPtr + srcIdx, &src_f8);
brightness_hip_compute(srcPtr, &src_f8, &dst_f8, &alpha_f4, &beta_f4);
rpp_hip_pack_float8_and_store8(dstPtr + dstIdx, &dst_f8);
}
template <typename T>
__global__ void brightness_pln_hip_tensor(T *srcPtr,
uint3 srcStridesNCH,
T *dstPtr,
uint3 dstStridesNCH,
int channelsDst,
float *alpha,
float *beta,
RpptROIPtr roiTensorPtrSrc)
{
int id_x = (hipBlockIdx_x * hipBlockDim_x + hipThreadIdx_x) * 8;
int id_y = hipBlockIdx_y * hipBlockDim_y + hipThreadIdx_y;
int id_z = hipBlockIdx_z * hipBlockDim_z + hipThreadIdx_z;
if ((id_y >= roiTensorPtrSrc[id_z].xywhROI.roiHeight) || (id_x >= roiTensorPtrSrc[id_z].xywhROI.roiWidth))
{
return;
}
uint srcIdx = (id_z * srcStridesNCH.x) + ((id_y + roiTensorPtrSrc[id_z].xywhROI.xy.y) * srcStridesNCH.z) + (id_x + roiTensorPtrSrc[id_z].xywhROI.xy.x);
uint dstIdx = (id_z * dstStridesNCH.x) + (id_y * dstStridesNCH.z) + id_x;
float4 alpha_f4 = (float4)(alpha[id_z]);
float4 beta_f4 = (float4)(beta[id_z]);
d_float8 src_f8, dst_f8;
rpp_hip_load8_and_unpack_to_float8(srcPtr + srcIdx, &src_f8);
brightness_hip_compute(srcPtr, &src_f8, &dst_f8, &alpha_f4, &beta_f4);
rpp_hip_pack_float8_and_store8(dstPtr + dstIdx, &dst_f8);
if (channelsDst == 3)
{
srcIdx += srcStridesNCH.y;
dstIdx += dstStridesNCH.y;
rpp_hip_load8_and_unpack_to_float8(srcPtr + srcIdx, &src_f8);
brightness_hip_compute(srcPtr, &src_f8, &dst_f8, &alpha_f4, &beta_f4);
rpp_hip_pack_float8_and_store8(dstPtr + dstIdx, &dst_f8);
srcIdx += srcStridesNCH.y;
dstIdx += dstStridesNCH.y;
rpp_hip_load8_and_unpack_to_float8(srcPtr + srcIdx, &src_f8);
brightness_hip_compute(srcPtr, &src_f8, &dst_f8, &alpha_f4, &beta_f4);
rpp_hip_pack_float8_and_store8(dstPtr + dstIdx, &dst_f8);
}
}
template <typename T>
__global__ void brightness_pkd3_pln3_hip_tensor(T *srcPtr,
uint2 srcStridesNH,
T *dstPtr,
uint3 dstStridesNCH,
float *alpha,
float *beta,
RpptROIPtr roiTensorPtrSrc)
{
int id_x = (hipBlockIdx_x * hipBlockDim_x + hipThreadIdx_x) * 8;
int id_y = hipBlockIdx_y * hipBlockDim_y + hipThreadIdx_y;
int id_z = hipBlockIdx_z * hipBlockDim_z + hipThreadIdx_z;
if ((id_y >= roiTensorPtrSrc[id_z].xywhROI.roiHeight) || (id_x >= roiTensorPtrSrc[id_z].xywhROI.roiWidth))
{
return;
}
uint srcIdx = (id_z * srcStridesNH.x) + ((id_y + roiTensorPtrSrc[id_z].xywhROI.xy.y) * srcStridesNH.y) + ((id_x + roiTensorPtrSrc[id_z].xywhROI.xy.x) * 3);
uint dstIdx = (id_z * dstStridesNCH.x) + (id_y * dstStridesNCH.z) + id_x;
float4 alpha_f4 = (float4)alpha[id_z];
float4 beta_f4 = (float4)beta[id_z];
d_float24 src_f24, dst_f24;
rpp_hip_load24_pkd3_and_unpack_to_float24_pln3(srcPtr + srcIdx, &src_f24);
brightness_hip_compute(srcPtr, &src_f24.f8[0], &dst_f24.f8[0], &alpha_f4, &beta_f4);
brightness_hip_compute(srcPtr, &src_f24.f8[1], &dst_f24.f8[1], &alpha_f4, &beta_f4);
brightness_hip_compute(srcPtr, &src_f24.f8[2], &dst_f24.f8[2], &alpha_f4, &beta_f4);
rpp_hip_pack_float24_pln3_and_store24_pln3(dstPtr + dstIdx, dstStridesNCH.y, &dst_f24);
}
template <typename T>
__global__ void brightness_pln3_pkd3_hip_tensor(T *srcPtr,
uint3 srcStridesNCH,
T *dstPtr,
uint2 dstStridesNH,
float *alpha,
float *beta,
RpptROIPtr roiTensorPtrSrc)
{
int id_x = (hipBlockIdx_x * hipBlockDim_x + hipThreadIdx_x) * 8;
int id_y = hipBlockIdx_y * hipBlockDim_y + hipThreadIdx_y;
int id_z = hipBlockIdx_z * hipBlockDim_z + hipThreadIdx_z;
if ((id_y >= roiTensorPtrSrc[id_z].xywhROI.roiHeight) || (id_x >= roiTensorPtrSrc[id_z].xywhROI.roiWidth))
{
return;
}
uint srcIdx = (id_z * srcStridesNCH.x) + ((id_y + roiTensorPtrSrc[id_z].xywhROI.xy.y) * srcStridesNCH.z) + (id_x + roiTensorPtrSrc[id_z].xywhROI.xy.x);
uint dstIdx = (id_z * dstStridesNH.x) + (id_y * dstStridesNH.y) + id_x * 3;
float4 alpha_f4 = (float4)(alpha[id_z]);
float4 beta_f4 = (float4)(beta[id_z]);
d_float24 src_f24, dst_f24;
rpp_hip_load24_pln3_and_unpack_to_float24_pkd3(srcPtr + srcIdx, srcStridesNCH.y, &src_f24);
brightness_hip_compute(srcPtr, &src_f24.f8[0], &dst_f24.f8[0], &alpha_f4, &beta_f4);
brightness_hip_compute(srcPtr, &src_f24.f8[1], &dst_f24.f8[1], &alpha_f4, &beta_f4);
brightness_hip_compute(srcPtr, &src_f24.f8[2], &dst_f24.f8[2], &alpha_f4, &beta_f4);
rpp_hip_pack_float24_pkd3_and_store24_pkd3(dstPtr + dstIdx, &dst_f24);
}
template <typename T>
RppStatus hip_exec_brightness_tensor(T *srcPtr,
RpptDescPtr srcDescPtr,
T *dstPtr,
RpptDescPtr dstDescPtr,
RpptROIPtr roiTensorPtrSrc,
RpptRoiType roiType,
rpp::Handle& handle)
{
if (roiType == RpptRoiType::LTRB)
hip_exec_roi_converison_ltrb_to_xywh(roiTensorPtrSrc, handle);
int globalThreads_x = (dstDescPtr->strides.hStride + 7) >> 3;
int globalThreads_y = dstDescPtr->h;
int globalThreads_z = handle.GetBatchSize();
if ((srcDescPtr->layout == RpptLayout::NHWC) && (dstDescPtr->layout == RpptLayout::NHWC))
{
hipLaunchKernelGGL(brightness_pkd_hip_tensor,
dim3(ceil((float)globalThreads_x/LOCAL_THREADS_X), ceil((float)globalThreads_y/LOCAL_THREADS_Y), ceil((float)globalThreads_z/LOCAL_THREADS_Z)),
dim3(LOCAL_THREADS_X, LOCAL_THREADS_Y, LOCAL_THREADS_Z),
0,
handle.GetStream(),
srcPtr,
make_uint2(srcDescPtr->strides.nStride, srcDescPtr->strides.hStride),
dstPtr,
make_uint2(dstDescPtr->strides.nStride, dstDescPtr->strides.hStride),
handle.GetInitHandle()->mem.mgpu.floatArr[0].floatmem,
handle.GetInitHandle()->mem.mgpu.floatArr[1].floatmem,
roiTensorPtrSrc);
}
else if ((srcDescPtr->layout == RpptLayout::NCHW) && (dstDescPtr->layout == RpptLayout::NCHW))
{
hipLaunchKernelGGL(brightness_pln_hip_tensor,
dim3(ceil((float)globalThreads_x/LOCAL_THREADS_X), ceil((float)globalThreads_y/LOCAL_THREADS_Y), ceil((float)globalThreads_z/LOCAL_THREADS_Z)),
dim3(LOCAL_THREADS_X, LOCAL_THREADS_Y, LOCAL_THREADS_Z),
0,
handle.GetStream(),
srcPtr,
make_uint3(srcDescPtr->strides.nStride, srcDescPtr->strides.cStride, srcDescPtr->strides.hStride),
dstPtr,
make_uint3(dstDescPtr->strides.nStride, dstDescPtr->strides.cStride, dstDescPtr->strides.hStride),
dstDescPtr->c,
handle.GetInitHandle()->mem.mgpu.floatArr[0].floatmem,
handle.GetInitHandle()->mem.mgpu.floatArr[1].floatmem,
roiTensorPtrSrc);
}
else if ((srcDescPtr->c == 3) && (dstDescPtr->c == 3))
{
if ((srcDescPtr->layout == RpptLayout::NHWC) && (dstDescPtr->layout == RpptLayout::NCHW))
{
hipLaunchKernelGGL(brightness_pkd3_pln3_hip_tensor,
dim3(ceil((float)globalThreads_x/LOCAL_THREADS_X), ceil((float)globalThreads_y/LOCAL_THREADS_Y), ceil((float)globalThreads_z/LOCAL_THREADS_Z)),
dim3(LOCAL_THREADS_X, LOCAL_THREADS_Y, LOCAL_THREADS_Z),
0,
handle.GetStream(),
srcPtr,
make_uint2(srcDescPtr->strides.nStride, srcDescPtr->strides.hStride),
dstPtr,
make_uint3(dstDescPtr->strides.nStride, dstDescPtr->strides.cStride, dstDescPtr->strides.hStride),
handle.GetInitHandle()->mem.mgpu.floatArr[0].floatmem,
handle.GetInitHandle()->mem.mgpu.floatArr[1].floatmem,
roiTensorPtrSrc);
}
else if ((srcDescPtr->layout == RpptLayout::NCHW) && (dstDescPtr->layout == RpptLayout::NHWC))
{
globalThreads_x = (srcDescPtr->strides.hStride + 7) >> 3;
hipLaunchKernelGGL(brightness_pln3_pkd3_hip_tensor,
dim3(ceil((float)globalThreads_x/LOCAL_THREADS_X), ceil((float)globalThreads_y/LOCAL_THREADS_Y), ceil((float)globalThreads_z/LOCAL_THREADS_Z)),
dim3(LOCAL_THREADS_X, LOCAL_THREADS_Y, LOCAL_THREADS_Z),
0,
handle.GetStream(),
srcPtr,
make_uint3(srcDescPtr->strides.nStride, srcDescPtr->strides.cStride, srcDescPtr->strides.hStride),
dstPtr,
make_uint2(dstDescPtr->strides.nStride, dstDescPtr->strides.hStride),
handle.GetInitHandle()->mem.mgpu.floatArr[0].floatmem,
handle.GetInitHandle()->mem.mgpu.floatArr[1].floatmem,
roiTensorPtrSrc);
}
}
return RPP_SUCCESS;
}