|
| 1 | +// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <gtest/gtest.h> |
| 16 | +#include <cmath> |
| 17 | +#include "lite/api/paddle_use_kernels.h" |
| 18 | +#include "lite/api/paddle_use_ops.h" |
| 19 | +#include "lite/core/arena/framework.h" |
| 20 | +#include "lite/tests/utils/fill_data.h" |
| 21 | + |
| 22 | +namespace paddle { |
| 23 | +namespace lite { |
| 24 | + |
| 25 | +template <class Tx, class Ty> |
| 26 | +void SequenceMask(const Tx* x, Ty* y, const int x_size, const int max_len) { |
| 27 | + memset(y, 0, sizeof(Ty) * x_size * max_len); |
| 28 | + for (int i = 0; i < x_size; i++) { |
| 29 | + int step = static_cast<int>(std::ceil(static_cast<float>(x[i]))); |
| 30 | + for (int j = 0; j < step; j++) { |
| 31 | + y[j] = static_cast<Ty>(1); |
| 32 | + } |
| 33 | + y += max_len; |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +template <class T> |
| 38 | +class SequenceMaskTester : public arena::TestCase { |
| 39 | + protected: |
| 40 | + std::string x_ = "x"; |
| 41 | + std::string max_len_tensor_; |
| 42 | + std::string y_ = "y"; |
| 43 | + int max_len_{-1}; |
| 44 | + int out_type_{5}; |
| 45 | + DDim x_dims_{{2, 3, 4}}; |
| 46 | + |
| 47 | + public: |
| 48 | + SequenceMaskTester(const Place& place, |
| 49 | + const std::string& alias, |
| 50 | + const int max_len = 5, |
| 51 | + const int out_type = 5, |
| 52 | + const bool use_max_len_tensor = false) |
| 53 | + : TestCase(place, alias), max_len_(max_len), out_type_(out_type) { |
| 54 | + if (use_max_len_tensor) { |
| 55 | + max_len_tensor_ = std::string("max_len_tensor"); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + void RunBaseline(Scope* scope) override { |
| 60 | + auto* y = scope->NewTensor(y_); |
| 61 | + auto y_shape = x_dims_.Vectorize(); |
| 62 | + y_shape.push_back(static_cast<int64_t>(max_len_)); |
| 63 | + y->Resize(y_shape); |
| 64 | + |
| 65 | + auto* x = scope->FindTensor(x_); |
| 66 | + auto* x_data = x->template data<T>(); |
| 67 | + int x_size = static_cast<int>(x->numel()); |
| 68 | + |
| 69 | + switch (out_type_) { |
| 70 | + case 5: { |
| 71 | + SequenceMask( |
| 72 | + x_data, y->template mutable_data<float>(), x_size, max_len_); |
| 73 | + break; |
| 74 | + } |
| 75 | + case 2: { |
| 76 | + SequenceMask(x_data, y->template mutable_data<int>(), x_size, max_len_); |
| 77 | + break; |
| 78 | + } |
| 79 | + case 3: { |
| 80 | + SequenceMask( |
| 81 | + x_data, y->template mutable_data<int64_t>(), x_size, max_len_); |
| 82 | + break; |
| 83 | + } |
| 84 | + default: |
| 85 | + LOG(FATAL) << "unsupported out data type: " << out_type_; |
| 86 | + break; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + void PrepareOpDesc(cpp::OpDesc* op_desc) { |
| 91 | + op_desc->SetType("sequence_mask"); |
| 92 | + op_desc->SetInput("X", {x_}); |
| 93 | + if (!max_len_tensor_.empty()) { |
| 94 | + op_desc->SetInput("MaxLenTensor", {max_len_tensor_}); |
| 95 | + op_desc->SetAttr("maxlen", -1); |
| 96 | + } else { |
| 97 | + op_desc->SetAttr("maxlen", max_len_); |
| 98 | + } |
| 99 | + op_desc->SetOutput("Y", {y_}); |
| 100 | + op_desc->SetAttr("out_dtype", out_type_); |
| 101 | + } |
| 102 | + |
| 103 | + void PrepareData() override { |
| 104 | + std::vector<T> x_data(x_dims_.production()); |
| 105 | + fill_data_rand<T>(x_data.data(), 0, 4, x_dims_.production()); |
| 106 | + SetCommonTensor(x_, x_dims_, x_data.data()); |
| 107 | + |
| 108 | + if (!max_len_tensor_.empty()) { |
| 109 | + std::vector<int> max_len_tensor_data{max_len_}; |
| 110 | + SetCommonTensor(max_len_tensor_, DDim{{1}}, max_len_tensor_data.data()); |
| 111 | + } |
| 112 | + } |
| 113 | +}; |
| 114 | + |
| 115 | +template <class T> |
| 116 | +void TestSequenceMaskHelper(const Place place, |
| 117 | + const float abs_error, |
| 118 | + const int max_len = 5, |
| 119 | + const int out_type = 5, |
| 120 | + const bool use_max_len_tensor = false) { |
| 121 | + std::string alias; |
| 122 | + auto precision = lite_api::PrecisionTypeTrait<T>::Type(); |
| 123 | + switch (precision) { |
| 124 | + case PRECISION(kFloat): |
| 125 | + alias = std::string("def"); |
| 126 | + break; |
| 127 | + case PRECISION(kInt32): |
| 128 | + alias = std::string("int32"); |
| 129 | + break; |
| 130 | + case PRECISION(kInt64): |
| 131 | + alias = std::string("int64"); |
| 132 | + break; |
| 133 | + default: |
| 134 | + LOG(FATAL) << "unsupported input data type: " |
| 135 | + << lite_api::PrecisionToStr(precision); |
| 136 | + break; |
| 137 | + } |
| 138 | + std::unique_ptr<arena::TestCase> tester(new SequenceMaskTester<T>( |
| 139 | + place, alias, max_len, out_type, use_max_len_tensor)); |
| 140 | + arena::Arena arena(std::move(tester), place, abs_error); |
| 141 | + arena.TestPrecision(); |
| 142 | +} |
| 143 | + |
| 144 | +template <class T> |
| 145 | +void TestSequenceMask(const Place place, const float abs_error) { |
| 146 | + // test max_len |
| 147 | + for (int max_len : {6}) { |
| 148 | + TestSequenceMaskHelper<T>(place, abs_error, max_len); |
| 149 | + } |
| 150 | + // test out_type |
| 151 | + for (int out_type : {2, 3, 5}) { |
| 152 | + TestSequenceMaskHelper<T>(place, abs_error, 5, out_type); |
| 153 | + } |
| 154 | + // test max_len_tensor |
| 155 | + TestSequenceMaskHelper<T>(place, abs_error, 5, 5, true); |
| 156 | +} |
| 157 | + |
| 158 | +TEST(sequence_mask, precision) { |
| 159 | + Place place; |
| 160 | + float abs_error = 1e-5; |
| 161 | +#if defined(LITE_WITH_ARM) || defined(LITE_WITH_X86) |
| 162 | + place = TARGET(kHost); |
| 163 | +#else |
| 164 | + return; |
| 165 | +#endif |
| 166 | + |
| 167 | + TestSequenceMask<float>(place, abs_error); |
| 168 | + TestSequenceMask<int>(place, abs_error); |
| 169 | + TestSequenceMask<int64_t>(place, abs_error); |
| 170 | +} |
| 171 | + |
| 172 | +} // namespace lite |
| 173 | +} // namespace paddle |
0 commit comments