Skip to content

Commit 4ac817a

Browse files
authored
fix DataLayout in kernels/cpu (#76664)
1 parent 6fd4cb4 commit 4ac817a

12 files changed

+64
-64
lines changed

paddle/phi/kernels/cpu/affine_channel_grad_kernel.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void AffineChannelGradKernel(const Context& dev_ctx,
5555
auto dims = x->dims();
5656
int N = static_cast<int>(dims[0]);
5757
int C = static_cast<int>(
58-
layout == phi::DataLayout::kNCHW ? dims[1] : dims[dims.size() - 1]);
58+
layout == phi::DataLayout::NCHW ? dims[1] : dims[dims.size() - 1]);
5959
int HxW = static_cast<int>(x->numel() / N / C);
6060

6161
auto* dy_d = dy->data<T>();
@@ -68,7 +68,7 @@ void AffineChannelGradKernel(const Context& dev_ctx,
6868
EigenVectorArrayMap<T> dscale_e(dscale_d, C);
6969
EigenVectorArrayMap<T> dbias_e(dbias_d, C);
7070

71-
if (layout == phi::DataLayout::kNCHW) {
71+
if (layout == phi::DataLayout::NCHW) {
7272
// compute dscale and dbias
7373
int stride = C * HxW;
7474
auto* original_dy_d = dy_d;

paddle/phi/kernels/cpu/affine_channel_kernel.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void AffineChannelKernel(const Context& dev_ctx,
5151
auto dims = x->dims();
5252
int N = static_cast<int>(dims[0]);
5353
int C = static_cast<int>(
54-
layout == phi::DataLayout::kNCHW ? dims[1] : dims[dims.size() - 1]);
54+
layout == phi::DataLayout::NCHW ? dims[1] : dims[dims.size() - 1]);
5555
int HxW = static_cast<int>(x->numel() / N / C);
5656

5757
auto* scale_d = scale->data<T>();
@@ -61,7 +61,7 @@ void AffineChannelKernel(const Context& dev_ctx,
6161

6262
auto* x_d = x->data<T>();
6363
auto* y_d = y->data<T>();
64-
if (layout == phi::DataLayout::kNCHW) {
64+
if (layout == phi::DataLayout::NCHW) {
6565
int stride = C * HxW;
6666
for (int i = 0; i < N; i++) {
6767
ConstEigenArrayMap<T> x_e(x_d, HxW, C);

paddle/phi/kernels/cpu/batch_norm_grad_kernel.cc

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,13 @@ void BatchNormGradFunctor(const Context& dev_ctx,
107107
x_dims.size()));
108108
const int N = static_cast<int>(x_dims[0]);
109109
const int C = static_cast<int>(
110-
data_layout == DataLayout::kNCHW ? x_dims[1] : x_dims[x_dims.size() - 1]);
110+
data_layout == DataLayout::NCHW ? x_dims[1] : x_dims[x_dims.size() - 1]);
111111
const int sample_size = static_cast<int>(x.numel() / N / C);
112112

113113
// input dimension is 2 and the format is NCHW. The input can be regarded as
114114
// NHWC format
115-
if (x_dims.size() == 2 && data_layout == DataLayout::kNCHW) {
116-
data_layout = DataLayout::kNHWC;
115+
if (x_dims.size() == 2 && data_layout == DataLayout::NCHW) {
116+
data_layout = DataLayout::NHWC;
117117
}
118118

119119
// init output
@@ -207,7 +207,7 @@ void BatchNormGradFunctor(const Context& dev_ctx,
207207
// formula transform ====>
208208
// (y - bias) / (scale * inv_var) + est_mean
209209
switch (data_layout) {
210-
case DataLayout::kNCHW: {
210+
case DataLayout::NCHW: {
211211
if (is_inplace) {
212212
auto px = x;
213213
EigenArrayMap<T> x_data(
@@ -256,7 +256,7 @@ void BatchNormGradFunctor(const Context& dev_ctx,
256256
}
257257
break;
258258
}
259-
case DataLayout::kNHWC: {
259+
case DataLayout::NHWC: {
260260
if (is_inplace) {
261261
auto px = x;
262262
EigenArrayMap<T> x_data(
@@ -413,7 +413,7 @@ void BatchNormDoubleGradKernel(
413413

414414
const auto& x_dims = X->dims();
415415
const int C = static_cast<int>(
416-
data_layout == DataLayout::kNCHW ? x_dims[1] : x_dims[x_dims.size() - 1]);
416+
data_layout == DataLayout::NCHW ? x_dims[1] : x_dims[x_dims.size() - 1]);
417417
const int sample_size = static_cast<int>(X->numel() / C);
418418
phi::funcs::SetConstant<Context, T> set_constant;
419419

@@ -442,7 +442,7 @@ void BatchNormDoubleGradKernel(
442442

443443
DenseTensor transformed_dx(dX->type());
444444
DenseTensor transformed_ddy(ddY->type());
445-
if (data_layout == DataLayout::kNCHW && x_dims.size() > 2) {
445+
if (data_layout == DataLayout::NCHW && x_dims.size() > 2) {
446446
VLOG(3) << "Transform batchnorm output from NCHW to NHWC";
447447
// Input Tensor
448448
ResizeToChannelLast<Context, T>(dev_ctx, X, &transformed_x);
@@ -585,7 +585,7 @@ void BatchNormDoubleGradKernel(
585585
ddscale_tile_data;
586586
}
587587
}
588-
if (data_layout == DataLayout::kNCHW) {
588+
if (data_layout == DataLayout::NCHW) {
589589
VLOG(3) << "Transform batchnorm output from NHWC to NCHW";
590590
TransToChannelFirst<Context, T>(dev_ctx, &transformed_dx, dX);
591591
}
@@ -674,7 +674,7 @@ void BatchNormDoubleGradKernel(
674674
ddy_arr += ddbias_tile_data;
675675
}
676676

677-
if (data_layout == DataLayout::kNCHW) {
677+
if (data_layout == DataLayout::NCHW) {
678678
VLOG(3) << "Transform batchnorm output from NHWC to NCHW";
679679
TransToChannelFirst<Context, T>(dev_ctx, &transformed_ddy, ddY);
680680
}

paddle/phi/kernels/cpu/batch_norm_kernel.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ void BatchNormKernel(const Context& dev_ctx,
8787
x_dims.size()));
8888
const int N = static_cast<int>(x_dims[0]);
8989
const int C = static_cast<int>(
90-
data_layout == DataLayout::kNCHW ? x_dims[1] : x_dims[x_dims.size() - 1]);
90+
data_layout == DataLayout::NCHW ? x_dims[1] : x_dims[x_dims.size() - 1]);
9191
const int sample_size = static_cast<int>(x.numel() / N / C);
9292

9393
// alloc memory
@@ -103,8 +103,8 @@ void BatchNormKernel(const Context& dev_ctx,
103103

104104
// input dimension is 2 and the format is NCHW. The input can be regarded
105105
// as NHWC format
106-
if (x_dims.size() == 2 && data_layout == DataLayout::kNCHW) {
107-
data_layout = DataLayout::kNHWC;
106+
if (x_dims.size() == 2 && data_layout == DataLayout::NCHW) {
107+
data_layout = DataLayout::NHWC;
108108
}
109109

110110
if (!global_stats) {
@@ -132,7 +132,7 @@ void BatchNormKernel(const Context& dev_ctx,
132132
}
133133

134134
switch (data_layout) {
135-
case DataLayout::kNCHW: {
135+
case DataLayout::NCHW: {
136136
ConstEigenArrayMap<T> x_arr(x.data<T>(), sample_size, N * C);
137137
for (int nc = 0; nc < N * C; ++nc) {
138138
saved_mean_e(nc % C) += x_arr.col(nc).sum();
@@ -145,7 +145,7 @@ void BatchNormKernel(const Context& dev_ctx,
145145
saved_variance_e /= N * sample_size;
146146
break;
147147
}
148-
case DataLayout::kNHWC: {
148+
case DataLayout::NHWC: {
149149
ConstEigenArrayMap<T> x_arr(x.data<T>(), C, N * sample_size);
150150
for (int i = 0; i < N * sample_size; ++i) {
151151
saved_mean_e += x_arr.col(i);
@@ -253,15 +253,15 @@ void BatchNormKernel(const Context& dev_ctx,
253253
}
254254

255255
switch (data_layout) {
256-
case DataLayout::kNCHW: {
256+
case DataLayout::NCHW: {
257257
EigenArrayMap<T> y_arr(dev_ctx.template Alloc<T>(y), sample_size, N * C);
258258
ConstEigenArrayMap<T> x_arr(x.data<T>(), sample_size, N * C);
259259
for (int nc = 0; nc < N * C; ++nc) {
260260
y_arr.col(nc) = x_arr.col(nc) * new_scale(nc % C) + new_bias(nc % C);
261261
}
262262
break;
263263
}
264-
case DataLayout::kNHWC: {
264+
case DataLayout::NHWC: {
265265
EigenArrayMap<T>(dev_ctx.template Alloc<T>(y), C, N * sample_size) =
266266
(ConstEigenArrayMap<T>(x.data<T>(), C, N * sample_size).colwise() *
267267
new_scale)

paddle/phi/kernels/cpu/group_norm_grad_kernel.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void GroupNormGradKernel(const Context& dev_ctx,
7272
const auto bias_ptr = bias.get_ptr();
7373
const auto& x_dims = y.dims();
7474
const int C = static_cast<int>(
75-
data_layout == DataLayout::kNCHW ? x_dims[1] : x_dims[x_dims.size() - 1]);
75+
data_layout == DataLayout::NCHW ? x_dims[1] : x_dims[x_dims.size() - 1]);
7676
const int group_size = C / groups;
7777

7878
phi::funcs::SetConstant<CPUContext, T> set_zero;
@@ -107,7 +107,7 @@ void GroupNormGradKernel(const Context& dev_ctx,
107107
if (bias_ptr) bias_data = bias_ptr->data<T>();
108108

109109
int imsize = 1;
110-
if (data_layout == DataLayout::kNCHW) {
110+
if (data_layout == DataLayout::NCHW) {
111111
for (int i = 2; i < x_dims.size(); ++i) {
112112
imsize *= static_cast<int>(x_dims[i]);
113113
}
@@ -135,7 +135,7 @@ void GroupNormGradKernel(const Context& dev_ctx,
135135
auto* iter_d_x_data_backup = iter_d_x_data;
136136
T dp_scale = 0, dp_bias = 0;
137137

138-
if (data_layout == DataLayout::kNCHW) {
138+
if (data_layout == DataLayout::NCHW) {
139139
for (int cid = 0; cid < number; cid++) {
140140
for (int imid = 0; imid < imsize;
141141
imid++, iter_x_data++, iter_y_data++) {
@@ -221,7 +221,7 @@ void GroupNormGradKernel(const Context& dev_ctx,
221221
}
222222
}
223223
}
224-
if (data_layout == DataLayout::kNHWC) {
224+
if (data_layout == DataLayout::NHWC) {
225225
iter_x_data = x_data + (bid + 1) * C * imsize;
226226
if (d_x_data) {
227227
iter_d_x_data = d_x_data + (bid + 1) * C * imsize;

paddle/phi/kernels/cpu/group_norm_kernel.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void GroupNormKernel(const Context& dev_ctx,
6060

6161
const auto x_dims = x.dims();
6262
const int C = static_cast<int>(
63-
data_layout == DataLayout::kNCHW ? x_dims[1] : x_dims[x_dims.size() - 1]);
63+
data_layout == DataLayout::NCHW ? x_dims[1] : x_dims[x_dims.size() - 1]);
6464
const int group_size = C / groups;
6565

6666
dev_ctx.template Alloc<T>(y);
@@ -78,7 +78,7 @@ void GroupNormKernel(const Context& dev_ctx,
7878
if (bias_ptr) bias_data = bias_ptr->data<T>();
7979

8080
int imsize = 1;
81-
if (data_layout == DataLayout::kNCHW) {
81+
if (data_layout == DataLayout::NCHW) {
8282
for (int i = 2; i < x_dims.size(); ++i) {
8383
imsize *= static_cast<int>(x_dims[i]);
8484
}
@@ -103,7 +103,7 @@ void GroupNormKernel(const Context& dev_ctx,
103103
auto* tmp_y = iter_y_data;
104104
auto* y_src_data = iter_y_data;
105105

106-
if (data_layout == DataLayout::kNCHW) {
106+
if (data_layout == DataLayout::NCHW) {
107107
for (int cid = 0; cid < number; cid++) {
108108
int imid = 0;
109109
for (imid = 0; imid < imsize - (imsize % M);
@@ -186,7 +186,7 @@ void GroupNormKernel(const Context& dev_ctx,
186186
mean_data[bid * groups + gid] = x_mean;
187187
var_data[bid * groups + gid] = x_var;
188188

189-
if (data_layout == DataLayout::kNCHW) {
189+
if (data_layout == DataLayout::NCHW) {
190190
for (int cid = 0; cid < number; cid++) {
191191
for (int imid = 0; imid < imsize; imid++, tmp_x++, iter_y_data++) {
192192
T val = (tmp_x[0] - x_mean) * var_inv;
@@ -210,7 +210,7 @@ void GroupNormKernel(const Context& dev_ctx,
210210
iter_y_data = tmp_y + group_size;
211211
}
212212
}
213-
if (data_layout == DataLayout::kNHWC) {
213+
if (data_layout == DataLayout::NHWC) {
214214
iter_x_data = x_data + (bid + 1) * C * imsize;
215215
iter_y_data = y_data + (bid + 1) * C * imsize;
216216
}

paddle/phi/kernels/cpu/interpolate_grad_kernel.cc

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static void LinearInterpolationGrad(const DenseTensor& output_grad,
5656
for (int i = 0; i < n; i++) { // loop for batches
5757
for (int j = 0; j < c; j++) { // loop for channels
5858
// linear interpolation grad
59-
if (data_layout == DataLayout::kNCHW) {
59+
if (data_layout == DataLayout::NCHW) {
6060
const MT grad = static_cast<MT>(output_grad_t(i, j, l));
6161
input_grad_t(i, j, x_w) += static_cast<T>(grad * d_e);
6262
input_grad_t(i, j, x_e) += static_cast<T>(grad * d_w);
@@ -118,7 +118,7 @@ static void BilinearInterpolationGrad(const DenseTensor& output_grad,
118118
for (int i = 0; i < n; i++) { // loop for batches
119119
for (int j = 0; j < c; j++) { // loop for channels
120120
// bilinear interpolation grad
121-
if (data_layout == DataLayout::kNCHW) {
121+
if (data_layout == DataLayout::NCHW) {
122122
const MT grad = static_cast<MT>(output_grad_t(i, j, k, l));
123123
input_grad_t(i, j, y_n, x_w) += static_cast<T>(grad * d_s * d_e);
124124
input_grad_t(i, j, y_s, x_w) += static_cast<T>(grad * d_n * d_e);
@@ -163,7 +163,7 @@ static void NearestNeighborInterpolateGrad(const DenseTensor& output_grad,
163163

164164
for (int i = 0; i < n; i++) { // loop for batches
165165
for (int j = 0; j < c; j++) { // loop for channels
166-
if (data_layout == DataLayout::kNCHW) {
166+
if (data_layout == DataLayout::NCHW) {
167167
input_grad_t(i, j, in_k, in_l) += output_grad_t(i, j, k, l);
168168
} else {
169169
input_grad_t(i, in_k, in_l, j) += output_grad_t(i, k, l, j);
@@ -218,7 +218,7 @@ static void BicubicInterpolationGrad(const DenseTensor& output_grad,
218218
static_cast<int64_t>(0));
219219
int access_y = std::max(std::min(input_y - 1 + jj, in_h - 1),
220220
static_cast<int64_t>(0));
221-
if (data_layout == DataLayout::kNCHW) {
221+
if (data_layout == DataLayout::NCHW) {
222222
MT grad = static_cast<MT>(output_grad_t(i, j, k, l));
223223
input_grad_t(i, j, access_y, access_x) +=
224224
static_cast<T>(grad * y_coeffs[jj] * x_coeffs[ii]);
@@ -298,7 +298,7 @@ static void TrilinearInterpolationGrad(const DenseTensor& output_grad,
298298
for (int b = 0; b < n; b++) { // loop for batches
299299
for (int i = 0; i < c; i++) { // loop for channels
300300
// trilinear interpolation grad
301-
if (data_layout == DataLayout::kNCHW) {
301+
if (data_layout == DataLayout::NCHW) {
302302
const MT grad = static_cast<MT>(output_grad_t(b, i, j, k, l));
303303
input_grad_t(b, i, t_f, y_n, x_w) +=
304304
static_cast<T>(grad * d_b * d_s * d_e);
@@ -377,7 +377,7 @@ static void NearestNeighbor3DInterpolateGrad(const DenseTensor& output_grad,
377377

378378
for (int i = 0; i < n; i++) { // loop for batches
379379
for (int j = 0; j < c; j++) { // loop for channels
380-
if (data_layout == DataLayout::kNCHW) {
380+
if (data_layout == DataLayout::NCHW) {
381381
input_grad_t(i, j, in_d, in_k, in_l) +=
382382
output_grad_t(i, j, d, k, l);
383383
} else {
@@ -449,7 +449,7 @@ static void Interpolate1DCPUBwd(
449449
}
450450

451451
phi::DDim dim_grad;
452-
if (data_layout == DataLayout::kNCHW) {
452+
if (data_layout == DataLayout::NCHW) {
453453
dim_grad = {n, c, in_w};
454454
} else {
455455
dim_grad = {n, in_w, c};
@@ -575,7 +575,7 @@ static void Interpolate2DCPUBwd(
575575
}
576576

577577
phi::DDim dim_grad;
578-
if (data_layout == DataLayout::kNCHW) {
578+
if (data_layout == DataLayout::NCHW) {
579579
dim_grad = {n, c, in_h, in_w};
580580
} else {
581581
dim_grad = {n, in_h, in_w, c};
@@ -753,7 +753,7 @@ static void Interpolate3DCPUBwd(
753753
}
754754

755755
phi::DDim dim_grad;
756-
if (data_layout == DataLayout::kNCHW) {
756+
if (data_layout == DataLayout::NCHW) {
757757
dim_grad = {n, c, in_d, in_h, in_w};
758758
} else {
759759
dim_grad = {n, in_d, in_h, in_w, c};

0 commit comments

Comments
 (0)