softmax_compute_test.cc 3.7 KB
Newer Older
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
// Copyright (c) 2019 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.

#include <gtest/gtest.h>
#include "lite/api/paddle_use_kernels.h"
#include "lite/api/paddle_use_ops.h"
#include "lite/core/arena/framework.h"
#include "lite/tests/utils/fill_data.h"

namespace paddle {
namespace lite {

class SoftmaxComputeTest : public arena::TestCase {
 protected:
  // common attributes for this op.
  std::string op_type_ = "softmax";
28 29 30
  DDim x_dims_{{1, 2, 3, 4}};
  std::string x_ = "x";
  std::string out_ = "out";
31 32 33 34 35
  int axis_ = 1;

 public:
  SoftmaxComputeTest(const Place& place,
                     const std::string& alias,
36
                     DDim x_dims,
37
                     int axis)
38
      : TestCase(place, alias), x_dims_(x_dims), axis_(axis) {}
39 40

  void RunBaseline(Scope* scope) override {
41 42
    auto x = scope->FindTensor(x_);
    auto out = scope->NewTensor(out_);
43
    CHECK(out);
44
    out->Resize(x_dims_);
45 46 47

    auto x_data = x->data<float>();
    auto out_data = out->mutable_data<float>();
48
    auto x_rank = x_dims_.size();
49 50 51
    if (axis_ < 0) {
      axis_ += x_rank;
    }
52 53 54
    int axis_size = x_dims_[axis_];
    int outer_num = x_dims_.Slice(0, axis_).production();
    int inner_num = x_dims_.Slice(axis_ + 1, x_rank).production();
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    int compute_size = outer_num * inner_num;
    for (int i = 0; i < compute_size; i++) {
      int idx_inner = i % inner_num;
      int idx_outer = (i / inner_num) * axis_size;
      int start = idx_outer * inner_num + idx_inner;
      int offset;

      offset = start;
      float max_data = std::numeric_limits<float>::lowest();
      for (int j = 0; j < axis_size; j++) {
        max_data = x_data[offset] > max_data ? x_data[offset] : max_data;
        offset += inner_num;
      }

      offset = start;
      float sum_data = 0.f;
      for (int j = 0; j < axis_size; j++) {
        out_data[offset] = exp(x_data[offset] - max_data);
        sum_data += out_data[offset];
        offset += inner_num;
      }

      offset = start;
      for (int j = 0; j < axis_size; j++) {
        out_data[offset] /= sum_data;
        offset += inner_num;
      }
    }
  }

  void PrepareOpDesc(cpp::OpDesc* op_desc) {
    op_desc->SetType(op_type_);
87 88
    op_desc->SetInput("X", {x_});
    op_desc->SetOutput("Out", {out_});
89 90 91 92
    op_desc->SetAttr("axis", axis_);
  }

  void PrepareData() override {
93 94 95
    std::vector<float> x(x_dims_.production());
    fill_data_rand(x.data(), -1.f, 1.f, x_dims_.production());
    SetCommonTensor(x_, x_dims_, x.data());
96 97 98 99 100 101 102
  }
};

TEST(Softmax, precision) {
  LOG(INFO) << "test softmax op";
  float abs_error = 2e-5;
  Place place;
103 104 105
#if defined(LITE_WITH_NPU)
  place = TARGET(kNPU);
  abs_error = 4e-3;  // Using fp16 in NPU
H
hong19860320 已提交
106
#elif defined(LITE_WITH_XPU) && defined(LITE_WITH_XTCL)
107 108 109 110 111
  place = TARGET(kXPU);
#else
  return;
#endif

112 113
  for (auto x_dims :
       std::vector<std::vector<int64_t>>{{1, 2, 3, 4}, {2, 3, 4}, {3, 4}}) {
114
    for (auto axis : {-1, 0, 1, 2, 3}) {
115
      if (axis >= x_dims.size()) continue;
116
      std::unique_ptr<arena::TestCase> tester(
117
          new SoftmaxComputeTest(place, "def", DDim(x_dims), axis));
118 119 120 121 122 123 124 125
      arena::Arena arena(std::move(tester), place, abs_error);
      arena.TestPrecision();
    }
  }
}

}  // namespace lite
}  // namespace paddle