Skip to content

Commit

Permalink
Upgrade pre-commit to fix the code style.
Browse files Browse the repository at this point in the history
  • Loading branch information
qingqing01 committed Jun 11, 2018
1 parent 0f66adf commit 3a2724b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
8 changes: 3 additions & 5 deletions paddle/fluid/operators/norm_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ namespace operators {
class NormOpMaker : public framework::OpProtoAndCheckerMaker {
public:
void Make() override {
AddInput("X",
"(Tensor) A tensor of rank >= axis.");
AddInput("X", "(Tensor) A tensor of rank >= axis.");
AddAttr<int>("axis",
"The axis on which to apply normalization. If axis < 0, "
"the dimension to normalization is rank(X) + axis. -1 is "
"the last dimension.");
"the last dimension.");
AddAttr<float>("epsilon",
"(float, default 1e-10) The epsilon value is used "
"to avoid division by zero.")
Expand All @@ -33,8 +32,7 @@ class NormOpMaker : public framework::OpProtoAndCheckerMaker {
"(Tensor) A tensor saved the `sqrt(sum(x) + epsion)` will "
"be used in backward kernel.")
.AsIntermediate();
AddOutput("Out",
"(Tensor) A tensor of the same shape as X.");
AddOutput("Out", "(Tensor) A tensor of the same shape as X.");
AddComment(R"DOC(
Given a tensor, apply 2-normalization along the provided axis.
Expand Down
16 changes: 7 additions & 9 deletions paddle/fluid/operators/norm_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ limitations under the License. */
namespace paddle {
namespace operators {

inline void GetDims(const framework::DDim& dim, int axis,
int* pre, int* n, int* post) {
inline void GetDims(const framework::DDim& dim, int axis, int* pre, int* n,
int* post) {
*pre = 1;
*post = 1;
*n = dim[axis];
Expand Down Expand Up @@ -49,7 +49,7 @@ class NormKernel : public framework::OpKernel<T> {
if (axis < 0) axis = xdim.size() + axis;
int pre, n, post;
GetDims(xdim, axis, &pre, &n, &post);

auto* place = ctx.template device_context<DeviceContext>().eigen_device();

Eigen::DSizes<int, 3> shape(pre, n, post);
Expand All @@ -61,14 +61,13 @@ class NormKernel : public framework::OpKernel<T> {
auto x = x_e.reshape(shape);
auto y = y_e.reshape(shape);
auto norm = norm_e.reshape(norm_shape);

Eigen::DSizes<int, 1> rdim(1);
auto x_pow = x * x;
auto& device_ctx = ctx.template device_context<DeviceContext>();
math::SetConstant<DeviceContext, T>()(device_ctx, out_norm, eps);

// y = x / sqrt((sum(x * x) + epsilon))

// norm = sqrt(sum(x * x) + epsilon)
norm.device(*place) = norm + x_pow.eval().sum(rdim) + eps;
norm.device(*place) = norm.sqrt();
Expand All @@ -93,9 +92,8 @@ class NormGradKernel : public framework::OpKernel<T> {
if (axis < 0) axis = xdim.size() + axis;
int pre, n, post;
GetDims(xdim, axis, &pre, &n, &post);

auto* place =
ctx.template device_context<DeviceContext>().eigen_device();

auto* place = ctx.template device_context<DeviceContext>().eigen_device();

auto x_e = framework::EigenVector<T>::Flatten(*in_x);
auto dy_e = framework::EigenVector<T>::Flatten(*in_dy);
Expand All @@ -112,7 +110,7 @@ class NormGradKernel : public framework::OpKernel<T> {
framework::Tensor rsum;
rsum.mutable_data<T>({pre, post}, ctx.GetPlace());
auto sum = framework::EigenTensor<T, 2>::From(rsum);

Eigen::DSizes<int, 1> rdim(1);
Eigen::DSizes<int, 3> bcast(1, n, 1);
Eigen::DSizes<int, 3> rshape(pre, 1, post);
Expand Down
3 changes: 0 additions & 3 deletions python/paddle/fluid/tests/unittests/test_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,6 @@ def test_polygon_box_transform(self):
self.assertIsNotNone(output)
print(str(program))


def test_l2_normalize(self):
program = Program()
with program_guard(program):
Expand All @@ -397,7 +396,5 @@ def test_l2_normalize(self):
print(str(program))




if __name__ == '__main__':
unittest.main()

0 comments on commit 3a2724b

Please sign in to comment.