test_relu_op.cpp 2.5 KB
Newer Older
E
eclipsess 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

H
hjchen2 已提交
15 16
#include <cmath>
#include <iostream>
E
eclipsess 已提交
17
#include "../test_include.h"
18
#include "operators/activation_op.h"
E
eclipsess 已提交
19

H
hjchen2 已提交
20
namespace paddle_mobile {
E
eclipsess 已提交
21

H
hjchen2 已提交
22 23 24
void Relu(const framework::Tensor *X, framework::Tensor *Y) {
  const float *x = X->data<float>();
  float *y = Y->mutable_data<float>();
E
eclipsess 已提交
25

H
hjchen2 已提交
26 27 28 29 30
  for (int i = 0; i < X->numel(); ++i) {
    float q = x[i];
    y[i] = std::max(0.f, q);
  }
}
E
eclipsess 已提交
31

H
hjchen2 已提交
32 33 34 35 36 37 38
int TestReluOp(const std::vector<int> input_shape) {
  framework::DDim dims = framework::make_ddim(input_shape);
  VariableNameMap inputs;
  VariableNameMap outputs;
  auto scope = std::make_shared<framework::Scope>();
  inputs["X"] = std::vector<std::string>({"input"});
  outputs["Out"] = std::vector<std::string>({"output"});
E
eclipsess 已提交
39

H
hjchen2 已提交
40 41 42
  auto input_var = scope.get()->Var("input");
  auto input = input_var->template GetMutable<framework::LoDTensor>();
  SetupTensor<float>(input, dims, -100.0, 100.0);
E
eclipsess 已提交
43

H
hjchen2 已提交
44
  auto output_var = scope.get()->Var("output");
E
eclipsess 已提交
45

H
hjchen2 已提交
46 47 48 49 50 51
  framework::AttributeMap attrs;
  auto *op =
      new operators::ReluOp<CPU, float>("relu", inputs, outputs, attrs, scope);
  op->InferShape();
  op->Init();
  op->Run();
E
eclipsess 已提交
52

H
hjchen2 已提交
53
  auto output = output_var->template Get<framework::LoDTensor>();
E
eclipsess 已提交
54

H
hjchen2 已提交
55 56 57
  framework::Tensor output_cmp;
  float *output_cmp_data = output_cmp.mutable_data<float>(output->dims());
  Relu(input, &output_cmp);
E
eclipsess 已提交
58

H
hjchen2 已提交
59 60 61 62 63 64 65 66 67 68
  const float *output_data = output->data<float>();
  for (int i = 0; i < output->numel(); ++i) {
    float gap = output_data[i] - output_cmp_data[i];
    if (std::abs(gap / (output_data[i] + 1e-5)) > 1e-3) {
      LOG(kLOG_INFO) << "output_data[" << i << "] = " << output_data[i]
                     << ", output_cmp_data[" << i
                     << "] = " << output_cmp_data[i];
      delete op;
      exit(1);
    }
E
eclipsess 已提交
69
  }
H
hjchen2 已提交
70 71 72 73 74 75 76 77 78 79 80
  delete op;
  return 0;
}

}  // namespace paddle_mobile

int main() {
  paddle_mobile::TestReluOp({1, 1, 2, 3});
  paddle_mobile::TestReluOp({1, 3, 11, 22});
  paddle_mobile::TestReluOp({1, 32, 112, 112});
  std::cout << "test relu op pass." << std::endl;
E
eclipsess 已提交
81 82
  return 0;
}