norm_arm_func.h 2.6 KB
Newer Older
Z
zhaojiaying01 已提交
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
/* Copyright (c) 2018 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. */

#ifdef NORM_OP

#pragma once

#include <cmath>
#include "operators/op_param.h"

namespace paddle_mobile {
namespace operators {

inline void GetDims(const framework::DDim &dim, int axis, int *pre, int *n,
                    int *post) {
  *pre = 1;
  *post = 1;
  *n = dim[axis];
  for (int i = 0; i < axis; ++i) {
    (*pre) *= dim[i];
  }
  for (int i = axis + 1; i < dim.size(); ++i) {
    (*post) *= dim[i];
  }
}

template <typename P>
void NormCompute(const NormParam<CPU> &param) {
  const float epsilon = param.Epsilon();
  int axis = param.Axis();

  const framework::Tensor *input = param.InputX();
  framework::Tensor *norm = param.OutputNorm();
  framework::Tensor *out = param.Out();

  auto x_dims = input->dims();
  if (axis < 0) {
    axis += x_dims.size();
  }

  int pre, n, post;
  GetDims(x_dims, axis, &pre, &n, &post);

  const float *input_ptr = input->data<float>();
  float *norm_ptr = norm->mutable_data<float>();
  float *out_ptr = out->mutable_data<float>();

Z
zhaojiaying01 已提交
59
  // in_ch = 0; norm = epsilon + x * x
Z
zhaojiaying01 已提交
60
  const float *in_tmp = input_ptr;
Z
zhaojiaying01 已提交
61 62 63 64 65
  float *norm_tmp = norm_ptr;
  for (int i = 0; i < post; ++i) {
    *norm_tmp = epsilon;
    *norm_tmp += (*in_tmp) * (*in_tmp);
    norm_tmp++;
Z
zhaojiaying01 已提交
66 67 68
    in_tmp++;
  }

Z
zhaojiaying01 已提交
69 70 71 72 73
  // in_ch >= 1; norm += x * x
  for (int j = 1; j < n; ++j) {
    norm_tmp = norm_ptr;
    for (int i = 0; i < post; ++i) {
      *norm_tmp += (*in_tmp) * (*in_tmp);
Z
zhaojiaying01 已提交
74
      norm_tmp++;
Z
zhaojiaying01 已提交
75
      in_tmp++;
Z
zhaojiaying01 已提交
76 77 78
    }
  }

Z
zhaojiaying01 已提交
79 80 81 82 83 84 85 86 87
  // norm = sqart(norm)
  norm_tmp = norm_ptr;
  for (int i = 0; i < post; ++i) {
    float sqrt = sqrtf(*norm_tmp);
    *norm_tmp = sqrt;
    norm_tmp++;
  }

  // out = input / norm
Z
zhaojiaying01 已提交
88 89 90 91 92 93 94 95 96 97 98
  in_tmp = input_ptr;
  norm_tmp = norm_ptr;
  float *out_tmp = out_ptr;
  for (int i = 0; i < pre; ++i) {
    for (int k = 0; k < n; ++k) {
      for (int j = 0; j < post; ++j) {
        *out_tmp = *in_tmp / *norm_tmp;
        in_tmp++;
        norm_tmp++;
        out_tmp++;
      }
Z
zhaojiaying01 已提交
99
      norm_tmp = norm_ptr + i * post;
Z
zhaojiaying01 已提交
100 101 102 103 104 105 106 107
    }
  }
}

}  // namespace operators
}  // namespace paddle_mobile

#endif