-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
Copy pathp_norm_kernel.cu
254 lines (231 loc) · 8.3 KB
/
p_norm_kernel.cu
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
// Copyright (c) 2022 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.
#include "paddle/phi/kernels/p_norm_kernel.h"
#include "paddle/phi/common/amp_type_traits.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/funcs/elementwise_base.h"
#include "paddle/phi/kernels/funcs/p_norm_utils.h"
#include "paddle/phi/kernels/funcs/reduce_function.h"
#include "paddle/phi/kernels/gpu/reduce.h"
namespace phi {
template <typename T>
struct NonzeroFunctor {
HOSTDEVICE explicit inline NonzeroFunctor() = default;
HOSTDEVICE inline T operator()(const T x) const {
return static_cast<T>(static_cast<double>(x) != 0);
}
};
template <typename T>
struct AbsFunctor {
HOSTDEVICE explicit inline AbsFunctor() = default;
HOSTDEVICE inline T operator()(const T x) const {
return static_cast<T>(inline_abs(x));
}
};
template <typename T>
struct UnsignedPowFunctor {
HOSTDEVICE explicit inline UnsignedPowFunctor(float porder) {
this->porder = porder;
}
HOSTDEVICE inline T operator()(const T x) const {
return static_cast<T>(inline_pow(inline_abs(x), static_cast<T>(porder)));
}
float porder;
};
#ifndef _WIN32
// To avoid large .so size in Windows cuda11.8
template <typename T>
struct FabsFunctor {
HOSTDEVICE explicit inline FabsFunctor() = default;
HOSTDEVICE inline T operator()(const T x) const {
return static_cast<T>(inline_fabs(x));
}
};
template <typename T>
struct SquareFunctor {
HOSTDEVICE explicit inline SquareFunctor() = default;
HOSTDEVICE inline T operator()(const T x) const {
return static_cast<T>(inline_square(x));
}
};
template <typename T>
struct FabsCubicFunctor {
HOSTDEVICE explicit inline FabsCubicFunctor() = default;
HOSTDEVICE inline T operator()(const T x) const {
return static_cast<T>(inline_fabs_cubic(x));
}
};
#endif
#ifndef PADDLE_WITH_XPU_KP
inline void GetDims(const phi::DDim& dim,
int axis,
int* pre,
int* n,
int* post,
bool asvector) {
*pre = 1;
*post = 1;
*n = static_cast<int>(dim[axis]);
if (asvector) {
*n = static_cast<int>(product(dim));
} else {
for (int i = 0; i < axis; ++i) {
(*pre) *= static_cast<int>(dim[i]);
}
for (int i = axis + 1; i < dim.size(); ++i) {
(*post) *= static_cast<int>(dim[i]);
}
}
}
template <typename T, typename Context>
void ReducePNormEigen(const Context& dev_ctx,
const DenseTensor& x,
float porder,
int axis,
float epsilon,
bool keepdim,
bool asvector,
DenseTensor* out) {
auto xdim = x.dims();
if (axis < 0) axis = xdim.size() + axis;
int pre = 0, n = 0, post = 0;
GetDims(xdim, axis, &pre, &n, &post, asvector);
for (int i = 0; i < xdim.size(); i++) {
PADDLE_ENFORCE_LT(0,
xdim[i],
errors::InvalidArgument(
"The dims of Input(X) should be greater than 0."));
}
auto* place = dev_ctx.eigen_device();
Eigen::DSizes<int, 3> shape(pre, n, post);
Eigen::DSizes<int, 2> norm_shape(pre, post);
auto x_e = phi::EigenVector<T>::Flatten(x);
auto norm_e = phi::EigenVector<T>::Flatten(*out);
auto xr = x_e.reshape(shape);
auto norm = norm_e.reshape(norm_shape);
// p=0 means number of non-zero elements of (xr)
// p=inf means the maximum of |xr|
// p=-inf means the minimum of |xr|
// otherwise, Lp-norm = pow(sum(pow(|xr|, p)), 1/p)
Eigen::DSizes<int, 1> rdim(1);
if (porder == 0) {
norm.device(*place) =
(xr != xr.constant(static_cast<T>(0))).template cast<T>().sum(rdim);
} else if (porder == INFINITY) {
norm.device(*place) = xr.abs().maximum(rdim);
} else if (porder == -INFINITY) {
norm.device(*place) = xr.abs().minimum(rdim);
} else {
norm.device(*place) = xr.abs()
.pow(static_cast<T>(porder))
.sum(rdim)
.pow(static_cast<T>(1.0f / porder));
}
}
#endif
template <typename T, typename Context>
void PNormKernel(const Context& dev_ctx,
const DenseTensor& x,
float porder,
int axis,
float epsilon,
bool keepdim,
bool asvector,
DenseTensor* out) {
auto* in_x = &x;
auto* out_norm = out;
T* norm = dev_ctx.template Alloc<T>(out);
auto xdim = in_x->dims();
std::vector<int64_t> axis_dims = {static_cast<int64_t>(axis)};
std::vector<int> reduce_axis =
funcs::details::GetReduceDim(axis_dims, xdim.size(), asvector);
for (int i = 0; i < xdim.size(); i++) {
PADDLE_ENFORCE_LT(0,
xdim[i],
errors::InvalidArgument(
"The dims of Input(X) should be greater than 0."));
}
if (x.numel() > std::numeric_limits<int32_t>::max()) {
#ifndef PADDLE_WITH_XPU_KP
ReducePNormEigen<T, Context>(
dev_ctx, *in_x, porder, axis, epsilon, keepdim, asvector, out_norm);
#else
PADDLE_THROW(common::errors::Fatal(
"If Input.numel() > INT32_MAX, reduce_sum kernel uses EigenTensor "
"sum for reduce_sum function. Such case is only supported on GPU "
"now."));
#endif
} else {
using MT = typename dtype::MPTypeTrait<T>::Type;
if (porder == 0) {
phi::funcs::ReduceKernel<T, T, kps::AddFunctor, NonzeroFunctor<T>>(
dev_ctx, *in_x, out_norm, NonzeroFunctor<T>(), reduce_axis);
} else if (porder == INFINITY) {
phi::funcs::ReduceKernel<T, T, kps::MaxFunctor, AbsFunctor<T>>(
dev_ctx, *in_x, out_norm, AbsFunctor<T>(), reduce_axis);
} else if (porder == -INFINITY) {
phi::funcs::ReduceKernel<T, T, kps::MinFunctor, AbsFunctor<T>>(
dev_ctx, *in_x, out_norm, AbsFunctor<T>(), reduce_axis);
} else {
#ifdef _WIN32
phi::funcs::ReduceKernel<T, T, kps::AddFunctor, UnsignedPowFunctor<T>>(
dev_ctx, *in_x, out_norm, UnsignedPowFunctor<T>(porder), reduce_axis);
const DenseTensor* tmp_norm = out_norm;
std::vector<const DenseTensor*> ins = {tmp_norm};
std::vector<DenseTensor*> outs = {out_norm};
phi::funcs::ElementwiseKernel<T>(
dev_ctx, ins, &outs, UnsignedPowFunctor<T>(1. / porder));
#else
if (porder == 1.0) {
// fast 1-norm
phi::funcs::ReduceKernel<T, T, kps::AddFunctor, FabsFunctor<T>>(
dev_ctx, *in_x, out_norm, FabsFunctor<T>(), reduce_axis);
} else if (porder == 2.0) {
// fast 2-norm
phi::funcs::ReduceKernel<T, T, kps::AddFunctor, SquareFunctor<T>>(
dev_ctx, *in_x, out_norm, SquareFunctor<T>(), reduce_axis);
} else if (porder == 3.0) {
// fast 3-norm
phi::funcs::ReduceKernel<T, T, kps::AddFunctor, FabsCubicFunctor<T>>(
dev_ctx, *in_x, out_norm, FabsCubicFunctor<T>(), reduce_axis);
} else {
// vanilla norm
phi::funcs::ReduceKernel<T, T, kps::AddFunctor, UnsignedPowFunctor<T>>(
dev_ctx,
*in_x,
out_norm,
UnsignedPowFunctor<T>(porder),
reduce_axis);
}
if (porder != 1.0) {
// save computation when porder is 1.0
const DenseTensor* tmp_norm = out_norm;
std::vector<const DenseTensor*> ins = {tmp_norm};
std::vector<DenseTensor*> outs = {out_norm};
phi::funcs::ElementwiseKernel<T>(
dev_ctx, ins, &outs, UnsignedPowFunctor<T>(1. / porder));
}
#endif
}
}
}
} // namespace phi
PD_REGISTER_KERNEL(p_norm,
GPU,
ALL_LAYOUT,
phi::PNormKernel,
float,
double,
phi::dtype::float16,
phi::dtype::bfloat16) {}