test_sequence_pool_op.cpp 9.9 KB
Newer Older
H
hjchen2 已提交
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 59 60 61
/* 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. */

#include <iostream>
#include "../test_include.h"
#include "operators/sequence_ops/sequence_pool_op.h"

namespace paddle_mobile {

int TestSequencePoolOp(const framework::LoDTensor &input_x,
                       const std::string pool_type,
                       framework::LoDTensor *output) {
  VariableNameMap inputs;
  VariableNameMap outputs;
  auto scope = std::make_shared<framework::Scope>();
  inputs["X"] = std::vector<std::string>({"input_x"});
  outputs["Out"] = std::vector<std::string>({"output"});

  auto input_x_var = scope.get()->Var("input_x");
  auto *x = input_x_var->template GetMutable<framework::LoDTensor>();
  x->Resize(input_x.dims());
  x->ShareDataWith(input_x);
  x->set_lod(input_x.lod());

  auto output_var = scope.get()->Var("output");

  framework::AttributeMap attrs;
  attrs["pooltype"].SetString(pool_type);

  auto *op = new operators::SequencePoolOp<CPU, float>("sequence_pool", inputs,
                                                       outputs, attrs, scope);

  op->InferShape();
  op->Init();
  op->Run();

  auto *out = output_var->template Get<framework::LoDTensor>();
  output->Resize(out->dims());
  output->ShareDataWith(*out);
  delete op;
  return 0;
}

}  // namespace paddle_mobile

// namespace framework = paddle_mobile::framework;

int main(int argc, char *argv[]) {
  framework::LoDTensor input_x, output;
  // case 1
62
  DLOG << "running max case 1";
H
hjchen2 已提交
63 64 65 66 67 68 69 70 71 72 73
  {
    std::vector<float> data{1, 2, 3, 4};
    input_x.Resize(framework::make_ddim({4, 1}));
    float *in_data = input_x.mutable_data<float>();
    for (int i = 0; i < 4; ++i) in_data[i] = data[i];
    input_x.set_lod({{0, 2, 4}});

    TestSequencePoolOp(input_x, "MAX", &output);
    std::vector<float> expect_data{2, 4};
    for (int i = 0; i < 2; ++i) {
      if (output.data<float>()[i] != expect_data[i]) {
74 75
        DLOG << "output[" << i << "]: " << output.data<float>()[i]
             << " != expect[" << i << "]: " << expect_data[i];
H
hjchen2 已提交
76 77 78 79 80
        return 1;
      }
    }
  }
  // case 2
81
  DLOG << "running max case 2";
H
hjchen2 已提交
82 83 84 85 86 87 88 89 90 91 92
  {
    std::vector<float> data{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    input_x.Resize(framework::make_ddim({data.size(), 1}));
    float *in_data = input_x.mutable_data<float>();
    for (int i = 0; i < data.size(); ++i) in_data[i] = data[i];
    input_x.set_lod({{0, 3, 10}});

    TestSequencePoolOp(input_x, "MAX", &output);
    std::vector<float> expect_data{3, 10};
    for (int i = 0; i < 2; ++i) {
      if (output.data<float>()[i] != expect_data[i]) {
93 94
        DLOG << "output[" << i << "]: " << output.data<float>()[i]
             << " != expect[" << i << "]: " << expect_data[i];
H
hjchen2 已提交
95 96 97 98
        return 1;
      }
    }
  }
99
  DLOG << "running max case 3";
H
hjchen2 已提交
100 101 102 103 104 105 106 107 108 109 110 111
  // case 3
  {
    std::vector<float> data{1, 2, 3, 4, 5, 6, 7, 8};
    input_x.Resize(framework::make_ddim({4, 2}));
    float *in_data = input_x.mutable_data<float>();
    for (int i = 0; i < data.size(); ++i) in_data[i] = data[i];
    input_x.set_lod({{0, 2, 4}});

    TestSequencePoolOp(input_x, "MAX", &output);
    std::vector<float> expect_data{3, 4, 7, 8};
    for (int i = 0; i < 4; ++i) {
      if (output.data<float>()[i] != expect_data[i]) {
112 113
        DLOG << "output[" << i << "]: " << output.data<float>()[i]
             << " != expect[" << i << "]: " << expect_data[i];
H
hjchen2 已提交
114 115 116 117 118
        return 1;
      }
    }
  }
  // case 4
119
  DLOG << "running max case 4";
H
hjchen2 已提交
120 121 122 123 124 125 126 127 128 129 130 131
  {
    std::vector<float> data{1,  2,  3,  4,  5,  6,  7,  8,  9,  10,
                            11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
    input_x.Resize(framework::make_ddim({4, 5}));
    float *in_data = input_x.mutable_data<float>();
    for (int i = 0; i < data.size(); ++i) in_data[i] = data[i];
    input_x.set_lod({{0, 2, 4}});

    TestSequencePoolOp(input_x, "MAX", &output);
    std::vector<float> expect_data{6, 7, 8, 9, 10, 16, 17, 18, 19, 20};
    for (int i = 0; i < 10; ++i) {
      if (output.data<float>()[i] != expect_data[i]) {
132 133
        DLOG << "output[" << i << "]: " << output.data<float>()[i]
             << " != expect[" << i << "]: " << expect_data[i];
H
hjchen2 已提交
134 135 136 137 138
        return 1;
      }
    }
  }
  // case 1
139
  DLOG << "running sum case 1";
H
hjchen2 已提交
140 141 142 143 144 145 146 147 148 149 150
  {
    std::vector<float> data{1, 2, 3, 4};
    input_x.Resize(framework::make_ddim({4, 1}));
    float *in_data = input_x.mutable_data<float>();
    for (int i = 0; i < 4; ++i) in_data[i] = data[i];
    input_x.set_lod({{0, 2, 4}});

    TestSequencePoolOp(input_x, "SUM", &output);
    std::vector<float> expect_data{3, 7};
    for (int i = 0; i < 2; ++i) {
      if (output.data<float>()[i] != expect_data[i]) {
151 152
        DLOG << "output[" << i << "]: " << output.data<float>()[i]
             << " != expect[" << i << "]: " << expect_data[i];
H
hjchen2 已提交
153 154 155 156 157
        return 1;
      }
    }
  }
  // case 2
158
  DLOG << "running sum case 2";
H
hjchen2 已提交
159 160 161 162 163 164 165 166 167 168 169
  {
    std::vector<float> data{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    input_x.Resize(framework::make_ddim({data.size(), 1}));
    float *in_data = input_x.mutable_data<float>();
    for (int i = 0; i < data.size(); ++i) in_data[i] = data[i];
    input_x.set_lod({{0, 3, 10}});

    TestSequencePoolOp(input_x, "SUM", &output);
    std::vector<float> expect_data{6, 49};
    for (int i = 0; i < 2; ++i) {
      if (output.data<float>()[i] != expect_data[i]) {
170 171
        DLOG << "output[" << i << "]: " << output.data<float>()[i]
             << " != expect[" << i << "]: " << expect_data[i];
H
hjchen2 已提交
172 173 174 175 176
        return 1;
      }
    }
  }
  // case 3
177
  DLOG << "running sum case 3";
H
hjchen2 已提交
178 179 180 181 182 183 184 185 186 187 188
  {
    std::vector<float> data{1, 2, 3, 4, 5, 6, 7, 8};
    input_x.Resize(framework::make_ddim({4, 2}));
    float *in_data = input_x.mutable_data<float>();
    for (int i = 0; i < data.size(); ++i) in_data[i] = data[i];
    input_x.set_lod({{0, 2, 4}});

    TestSequencePoolOp(input_x, "SUM", &output);
    std::vector<float> expect_data{4, 6, 12, 14};
    for (int i = 0; i < 4; ++i) {
      if (output.data<float>()[i] != expect_data[i]) {
189 190
        DLOG << "output[" << i << "]: " << output.data<float>()[i]
             << " != expect[" << i << "]: " << expect_data[i];
H
hjchen2 已提交
191 192 193 194 195
        return 1;
      }
    }
  }
  // case 4
196
  DLOG << "running sum case 4";
H
hjchen2 已提交
197 198 199 200 201 202 203 204 205 206 207 208
  {
    std::vector<float> data{1,  2,  3,  4,  5,  6,  7,  8,  9,  10,
                            11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
    input_x.Resize(framework::make_ddim({4, 5}));
    float *in_data = input_x.mutable_data<float>();
    for (int i = 0; i < data.size(); ++i) in_data[i] = data[i];
    input_x.set_lod({{0, 2, 4}});

    TestSequencePoolOp(input_x, "SUM", &output);
    std::vector<float> expect_data{7, 9, 11, 13, 15, 27, 29, 31, 33, 35};
    for (int i = 0; i < 10; ++i) {
      if (output.data<float>()[i] != expect_data[i]) {
209 210
        DLOG << "output[" << i << "]: " << output.data<float>()[i]
             << " != expect[" << i << "]: " << expect_data[i];
H
hjchen2 已提交
211 212 213 214 215
        return 1;
      }
    }
  }
  // case 1
216
  DLOG << "running first case 1";
H
hjchen2 已提交
217 218 219 220 221 222 223 224 225 226 227
  {
    std::vector<float> data{1, 2, 3, 4};
    input_x.Resize(framework::make_ddim({4, 1}));
    float *in_data = input_x.mutable_data<float>();
    for (int i = 0; i < 4; ++i) in_data[i] = data[i];
    input_x.set_lod({{0, 2, 4}});

    TestSequencePoolOp(input_x, "FIRST", &output);
    std::vector<float> expect_data{1, 3};
    for (int i = 0; i < 2; ++i) {
      if (output.data<float>()[i] != expect_data[i]) {
228 229
        DLOG << "output[" << i << "]: " << output.data<float>()[i]
             << " != expect[" << i << "]: " << expect_data[i];
H
hjchen2 已提交
230 231 232 233 234
        return 1;
      }
    }
  }
  // case 2
235
  DLOG << "running first case 2";
H
hjchen2 已提交
236 237 238 239 240 241 242 243 244 245 246
  {
    std::vector<float> data{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    input_x.Resize(framework::make_ddim({data.size(), 1}));
    float *in_data = input_x.mutable_data<float>();
    for (int i = 0; i < data.size(); ++i) in_data[i] = data[i];
    input_x.set_lod({{0, 3, 10}});

    TestSequencePoolOp(input_x, "FIRST", &output);
    std::vector<float> expect_data{1, 4};
    for (int i = 0; i < 2; ++i) {
      if (output.data<float>()[i] != expect_data[i]) {
247 248
        DLOG << "output[" << i << "]: " << output.data<float>()[i]
             << " != expect[" << i << "]: " << expect_data[i];
H
hjchen2 已提交
249 250 251 252 253
        return 1;
      }
    }
  }
  // case 3
254
  DLOG << "running first case 3";
H
hjchen2 已提交
255 256 257 258 259 260 261 262 263 264 265
  {
    std::vector<float> data{1, 2, 3, 4, 5, 6, 7, 8};
    input_x.Resize(framework::make_ddim({4, 2}));
    float *in_data = input_x.mutable_data<float>();
    for (int i = 0; i < data.size(); ++i) in_data[i] = data[i];
    input_x.set_lod({{0, 2, 4}});

    TestSequencePoolOp(input_x, "FIRST", &output);
    std::vector<float> expect_data{1, 2, 5, 6};
    for (int i = 0; i < 4; ++i) {
      if (output.data<float>()[i] != expect_data[i]) {
266 267
        DLOG << "output[" << i << "]: " << output.data<float>()[i]
             << " != expect[" << i << "]: " << expect_data[i];
H
hjchen2 已提交
268 269 270 271 272
        return 1;
      }
    }
  }
  // case 4
273
  DLOG << "running first case 4";
H
hjchen2 已提交
274 275 276 277 278 279 280 281 282 283 284 285
  {
    std::vector<float> data{1,  2,  3,  4,  5,  6,  7,  8,  9,  10,
                            11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
    input_x.Resize(framework::make_ddim({4, 5}));
    float *in_data = input_x.mutable_data<float>();
    for (int i = 0; i < data.size(); ++i) in_data[i] = data[i];
    input_x.set_lod({{0, 2, 4}});

    TestSequencePoolOp(input_x, "FIRST", &output);
    std::vector<float> expect_data{1, 2, 3, 4, 5, 11, 12, 13, 14, 15};
    for (int i = 0; i < 10; ++i) {
      if (output.data<float>()[i] != expect_data[i]) {
286 287
        DLOG << "output[" << i << "]: " << output.data<float>()[i]
             << " != expect[" << i << "]: " << expect_data[i];
H
hjchen2 已提交
288 289 290 291 292 293
        return 1;
      }
    }
  }
  return 0;
}