From 16999ae49d4f12aaa7b56c519fd16e004fca2fc0 Mon Sep 17 00:00:00 2001 From: Zhang Ting Date: Mon, 12 Oct 2020 16:15:35 +0800 Subject: [PATCH] use IndexList to improve performance of instance_norm op (#25132) * use IndexList to improve performance, test=develop * remove EIGEN_HAS_INDEX_LIST, test=develop * use IndexList only when EIGEN_HAS_INDEX_LIST is true --- paddle/fluid/operators/instance_norm_op.cc | 33 ++++++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/paddle/fluid/operators/instance_norm_op.cc b/paddle/fluid/operators/instance_norm_op.cc index 03279a9b2c1..1018adcd930 100644 --- a/paddle/fluid/operators/instance_norm_op.cc +++ b/paddle/fluid/operators/instance_norm_op.cc @@ -181,10 +181,22 @@ class InstanceNormKernel auto &dev_ctx = ctx.template device_context(); auto *place = dev_ctx.eigen_device(); + Eigen::DSizes shape(NxC, sample_size); +// Once eigen on Windows is updated, the if branch can be removed. +#ifndef EIGEN_HAS_INDEX_LIST Eigen::DSizes bcast(1, sample_size); Eigen::DSizes C_shape(C, 1); Eigen::DSizes NxC_shape(NxC, 1); - Eigen::DSizes shape(NxC, sample_size); + Eigen::DSizes rdims(1); +#else + Eigen::IndexList, int> bcast; + bcast.set(1, sample_size); + Eigen::IndexList> C_shape; + C_shape.set(0, C); + Eigen::IndexList> NxC_shape; + NxC_shape.set(0, NxC); + Eigen::IndexList> rdims; +#endif math::SetConstant set_constant; @@ -201,8 +213,6 @@ class InstanceNormKernel auto x_e = framework::EigenVector::Flatten(*x); auto x_arr = x_e.reshape(shape); - Eigen::DSizes rdims(1); - saved_mean_e.device(*place) = x_arr.mean(rdims); auto saved_variance_arr = (x_arr - saved_mean_e.broadcast(bcast)).square().mean(rdims) + epsilon; @@ -316,14 +326,25 @@ class InstanceNormGradKernel auto &dev_ctx = ctx.template device_context(); auto *place = dev_ctx.eigen_device(); + Eigen::DSizes rshape(NxC, sample_size); + Eigen::DSizes param_shape(N, C); + Eigen::DSizes shape(NxC, sample_size); +#ifndef EIGEN_HAS_INDEX_LIST Eigen::DSizes rdims(0); Eigen::DSizes mean_rdims(1); - Eigen::DSizes rshape(NxC, sample_size); Eigen::DSizes bcast(1, sample_size); Eigen::DSizes C_shape(C, 1); Eigen::DSizes NxC_shape(NxC, 1); - Eigen::DSizes param_shape(N, C); - Eigen::DSizes shape(NxC, sample_size); +#else + Eigen::IndexList> rdims; + Eigen::IndexList> mean_rdims; + Eigen::IndexList, int> bcast; + bcast.set(1, sample_size); + Eigen::IndexList> C_shape; + C_shape.set(0, C); + Eigen::IndexList> NxC_shape; + NxC_shape.set(0, NxC); +#endif math::SetConstant set_constant; -- GitLab