slice_compute_test.cc 16.0 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
// 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 "lite/kernels/x86/slice_compute.h"
#include <gtest/gtest.h>
#include <iostream>
#include <memory>
#include <utility>
#include <vector>
#include "lite/core/op_registry.h"
namespace paddle {
namespace lite {
namespace kernels {
namespace x86 {

27 28 29 30 31 32 33 34 35 36
static void slice_ref(const float* input,
                      std::vector<int64_t> in_dims,
                      std::vector<int> axes,
                      std::vector<int> starts,
                      std::vector<int> ends,
                      float* out) {
  auto out_dims = in_dims;
  std::vector<int> real_starts(in_dims.size(), 0);
  std::vector<int> real_ends(in_dims.size(), 0);
  std::vector<int> real_step(in_dims.size(), 0);
37
  for (size_t i = 0; i < in_dims.size(); i++) {
38 39
    real_ends[i] = in_dims[i];
  }
40
  for (size_t i = 0; i < axes.size(); i++) {
41 42 43 44 45 46 47 48 49 50 51 52 53 54
    int dim_value = in_dims[axes[i]];
    if (dim_value > 0) {
      int start = starts[i] < 0 ? (starts[i] + dim_value) : starts[i];
      int end = ends[i] < 0 ? (ends[i] + dim_value) : ends[i];
      start = std::max(start, 0);
      end = std::max(end, 0);
      end = std::min(end, dim_value);
      out_dims[axes[i]] = end - start;
      real_starts[axes[i]] = start;
      real_ends[axes[i]] = end;
    }
  }
  const int LEN = in_dims.size();
  int dst_step[LEN];
55
  for (size_t i = 0; i < in_dims.size(); ++i) {
56 57 58
    dst_step[i] = 1;
  }
  int src_step[LEN];
59
  for (size_t i = 0; i < in_dims.size(); ++i) {
60 61 62 63 64 65 66 67 68 69 70 71
    src_step[i] = 1;
  }
  int out_num = out_dims[in_dims.size() - 1];
  for (int i = in_dims.size() - 2; i >= 0; i--) {
    dst_step[i] = out_dims[i + 1] * dst_step[i + 1];
    src_step[i] = in_dims[i + 1] * src_step[i + 1];
    out_num *= out_dims[i];
  }

  for (int dst_id = 0; dst_id < out_num; dst_id++) {
    int src_id = 0;
    int index_id = dst_id;
72
    for (size_t j = 0; j < out_dims.size(); j++) {
73 74 75 76 77 78 79 80
      int cur_id = index_id / dst_step[j];
      index_id = index_id % dst_step[j];
      src_id += (cur_id + real_starts[j]) * src_step[j];
    }
    out[dst_id] = input[src_id];
  }
}

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
TEST(slice_x86, retrive_op) {
  auto slice =
      KernelRegistry::Global().Create<TARGET(kX86), PRECISION(kFloat)>("slice");
  ASSERT_FALSE(slice.empty());
  ASSERT_TRUE(slice.front());
}

TEST(slice_x86, init) {
  lite::kernels::x86::SliceCompute<float> slice;
  ASSERT_EQ(slice.precision(), PRECISION(kFloat));
  ASSERT_EQ(slice.target(), TARGET(kX86));
}

void test_case1(lite::Tensor x, lite::Tensor out) {
  std::vector<int64_t> x_shape({3});
  x.Resize(lite::DDim(x_shape));
  std::vector<int64_t> out_shape({3});
  out.Resize(lite::DDim(out_shape));

  auto x_data = x.mutable_data<float>();
  auto out_data = out.mutable_data<float>();

  for (int64_t i = 0; i < x.dims().production(); ++i) {
    x_data[i] = static_cast<float>(i);
  }

  std::vector<int> starts({-3});
  std::vector<int> ends({3});
  std::vector<int> axes({0});

  // SliceCompute slice;
  SliceCompute<float> slice;
  operators::SliceParam param;

  param.X = &x;
116 117 118
  param.axes = axes;
  param.starts = starts;
  param.ends = ends;
119 120 121 122 123 124 125 126
  param.Out = &out;

  std::unique_ptr<KernelContext> ctx(new KernelContext);
  ctx->As<X86Context>();
  slice.SetContext(std::move(ctx));
  slice.SetParam(param);
  slice.Run();

127 128 129
  std::vector<float> out_ref(out.numel(), 0);
  slice_ref(x_data, x_shape, axes, starts, ends, out_ref.data());

130
  for (int i = 0; i < out.dims().production(); i++) {
131
    EXPECT_NEAR(out_ref[i], out_data[i], 1e-4);
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
  }
}

void test_case2(lite::Tensor x, lite::Tensor out) {
  std::vector<int64_t> x_shape({3, 4});
  x.Resize(lite::DDim(x_shape));
  std::vector<int64_t> out_shape({3, 4});
  out.Resize(lite::DDim(out_shape));

  auto x_data = x.mutable_data<float>();
  auto out_data = out.mutable_data<float>();

  for (int64_t i = 0; i < x.dims().production(); ++i) {
    x_data[i] = static_cast<float>(i);
  }

  std::vector<int> starts({-3, 0});
  std::vector<int> ends({3, 100});
  std::vector<int> axes({0, 1});

  // SliceCompute slice;
  SliceCompute<float> slice;
  operators::SliceParam param;

  param.X = &x;
  param.Out = &out;
158 159 160
  param.axes = axes;
  param.starts = starts;
  param.ends = ends;
161 162 163 164 165 166 167

  std::unique_ptr<KernelContext> ctx(new KernelContext);
  ctx->As<X86Context>();
  slice.SetContext(std::move(ctx));
  slice.SetParam(param);
  slice.Run();

168 169 170
  std::vector<float> out_ref(out.numel(), 0);
  slice_ref(x_data, x_shape, axes, starts, ends, out_ref.data());

171
  for (int i = 0; i < out.dims().production(); i++) {
172
    EXPECT_NEAR(out_ref[i], out_data[i], 1e-4);
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
  }
}

void test_case3(lite::Tensor x, lite::Tensor out) {
  std::vector<int64_t> x_shape({3, 4, 5});
  x.Resize(lite::DDim(x_shape));
  std::vector<int64_t> out_shape({3, 4, 2});
  out.Resize(lite::DDim(out_shape));

  auto x_data = x.mutable_data<float>();
  auto out_data = out.mutable_data<float>();

  for (int64_t i = 0; i < x.dims().production(); ++i) {
    x_data[i] = static_cast<float>(i);
  }

  std::vector<int> starts({-3, 0, 2});
  std::vector<int> ends({3, 100, -1});
  std::vector<int> axes({0, 1, 2});

  // SliceCompute slice;
  SliceCompute<float> slice;
  operators::SliceParam param;

  param.X = &x;
  param.Out = &out;
199 200 201
  param.axes = axes;
  param.starts = starts;
  param.ends = ends;
202 203 204 205 206 207 208

  std::unique_ptr<KernelContext> ctx(new KernelContext);
  ctx->As<X86Context>();
  slice.SetContext(std::move(ctx));
  slice.SetParam(param);
  slice.Run();

209 210 211
  std::vector<float> out_ref(out.numel(), 0);
  slice_ref(x_data, x_shape, axes, starts, ends, out_ref.data());

212
  for (int i = 0; i < out.dims().production(); i++) {
213
    EXPECT_NEAR(out_ref[i], out_data[i], 1e-4);
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
  }
}
void test_case4(lite::Tensor x, lite::Tensor out) {
  std::vector<int64_t> x_shape({3, 4, 5, 6});
  x.Resize(lite::DDim(x_shape));
  std::vector<int64_t> out_shape({3, 4, 2, 6});
  out.Resize(lite::DDim(out_shape));

  auto x_data = x.mutable_data<float>();
  auto out_data = out.mutable_data<float>();

  for (int64_t i = 0; i < x.dims().production(); ++i) {
    x_data[i] = static_cast<float>(i);
  }

  std::vector<int> starts({-3, 0, 2});
  std::vector<int> ends({3, 100, -1});
  std::vector<int> axes({0, 1, 2});

  // SliceCompute slice;
  SliceCompute<float> slice;
  operators::SliceParam param;

  param.X = &x;
  param.Out = &out;
239 240 241
  param.axes = axes;
  param.starts = starts;
  param.ends = ends;
242 243 244 245 246 247 248

  std::unique_ptr<KernelContext> ctx(new KernelContext);
  ctx->As<X86Context>();
  slice.SetContext(std::move(ctx));
  slice.SetParam(param);
  slice.Run();

249 250 251
  std::vector<float> out_ref(out.numel(), 0);
  slice_ref(x_data, x_shape, axes, starts, ends, out_ref.data());

252
  for (int i = 0; i < out.dims().production(); i++) {
253
    EXPECT_NEAR(out_ref[i], out_data[i], 1e-4);
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
  }
}

void test_case5(lite::Tensor x, lite::Tensor out) {
  std::vector<int64_t> x_shape({3, 4, 5, 6, 3});
  x.Resize(lite::DDim(x_shape));
  std::vector<int64_t> out_shape({3, 4, 2, 6, 3});
  out.Resize(lite::DDim(out_shape));

  auto x_data = x.mutable_data<float>();
  auto out_data = out.mutable_data<float>();

  for (int64_t i = 0; i < x.dims().production(); ++i) {
    x_data[i] = static_cast<float>(i);
  }

  std::vector<int> starts({-3, 0, 2});
  std::vector<int> ends({3, 100, -1});
  std::vector<int> axes({0, 1, 2});

  // SliceCompute slice;
  SliceCompute<float> slice;
  operators::SliceParam param;

  param.X = &x;
  param.Out = &out;
280 281 282
  param.axes = axes;
  param.starts = starts;
  param.ends = ends;
283 284 285 286 287 288 289

  std::unique_ptr<KernelContext> ctx(new KernelContext);
  ctx->As<X86Context>();
  slice.SetContext(std::move(ctx));
  slice.SetParam(param);
  slice.Run();

290 291 292
  std::vector<float> out_ref(out.numel(), 0);
  slice_ref(x_data, x_shape, axes, starts, ends, out_ref.data());

293
  for (int i = 0; i < out.dims().production(); i++) {
294
    EXPECT_NEAR(out_ref[i], out_data[i], 1e-4);
295 296
  }
}
297

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
void test_case6(lite::Tensor x, lite::Tensor out) {
  std::vector<int64_t> x_shape({3, 4, 5, 6, 5, 2});
  x.Resize(lite::DDim(x_shape));
  std::vector<int64_t> out_shape({3, 4, 2, 6, 5, 2});
  out.Resize(lite::DDim(out_shape));

  auto x_data = x.mutable_data<float>();
  auto out_data = out.mutable_data<float>();

  for (int64_t i = 0; i < x.dims().production(); ++i) {
    x_data[i] = static_cast<float>(i);
  }

  std::vector<int> starts({-3, 0, 2});
  std::vector<int> ends({3, 100, -1});
  std::vector<int> axes({0, 1, 2});

  // SliceCompute slice;
  SliceCompute<float> slice;
  operators::SliceParam param;

  param.X = &x;
  param.Out = &out;
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
  param.axes = axes;
  param.starts = starts;
  param.ends = ends;

  std::unique_ptr<KernelContext> ctx(new KernelContext);
  ctx->As<X86Context>();
  slice.SetContext(std::move(ctx));
  slice.SetParam(param);
  slice.Run();

  std::vector<float> out_ref(out.numel(), 0);
  slice_ref(x_data, x_shape, axes, starts, ends, out_ref.data());

  for (int i = 0; i < out.dims().production(); i++) {
    EXPECT_NEAR(out_ref[i], out_data[i], 1e-4);
  }
}

void test_tensor_case1(lite::Tensor x, lite::Tensor out) {
  std::vector<int64_t> x_shape({10});
  x.Resize(lite::DDim(x_shape));
  std::vector<int64_t> out_shape({5});
  out.Resize(lite::DDim(out_shape));

  auto x_data = x.mutable_data<float>();
  auto out_data = out.mutable_data<float>();

  for (int64_t i = 0; i < x.dims().production(); ++i) {
    x_data[i] = static_cast<float>(i);
  }

  std::vector<int> starts({3});
  std::vector<int> ends({8});
  std::vector<int> axes({0});

  // SliceCompute slice;
  SliceCompute<float> slice;
  operators::SliceParam param;

  param.X = &x;
  param.Out = &out;
  param.axes = axes;
  lite::Tensor starts_tensor, ends_tensor;
  starts_tensor.Resize(DDim({1}));
  ends_tensor.Resize(DDim({1}));
  starts_tensor.mutable_data<int>()[0] = starts[0];
  ends_tensor.mutable_data<int>()[0] = ends[0];
  param.StartsTensor = &starts_tensor;
  param.EndsTensor = &ends_tensor;

  std::unique_ptr<KernelContext> ctx(new KernelContext);
  ctx->As<X86Context>();
  slice.SetContext(std::move(ctx));
  slice.SetParam(param);
  slice.Run();

  std::vector<float> out_ref(out.numel(), 0);
  slice_ref(x_data, x_shape, axes, starts, ends, out_ref.data());

  for (int i = 0; i < out.dims().production(); i++) {
    EXPECT_NEAR(out_ref[i], out_data[i], 1e-4);
  }
}

void test_tensor_case3(lite::Tensor x, lite::Tensor out) {
  std::vector<int64_t> x_shape({3, 4, 5});
  x.Resize(lite::DDim(x_shape));
  std::vector<int64_t> out_shape({3, 4, 2});
  out.Resize(lite::DDim(out_shape));

  auto x_data = x.mutable_data<float>();
  auto out_data = out.mutable_data<float>();

  for (int64_t i = 0; i < x.dims().production(); ++i) {
    x_data[i] = static_cast<float>(i);
  }

  std::vector<int> starts({-3, 0, 2});
  std::vector<int> ends({3, 100, -1});
  std::vector<int> axes({0, 1, 2});

  // SliceCompute slice;
  SliceCompute<float> slice;
  operators::SliceParam param;

  param.X = &x;
  param.Out = &out;
  param.axes = axes;
  lite::Tensor starts_tensor, ends_tensor;
  starts_tensor.Resize(DDim({3}));
  ends_tensor.Resize(DDim({3}));
412
  for (size_t i = 0; i < starts.size(); ++i) {
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
    starts_tensor.mutable_data<int>()[i] = starts[i];
    ends_tensor.mutable_data<int>()[i] = ends[i];
  }
  param.StartsTensor = &starts_tensor;
  param.EndsTensor = &ends_tensor;

  std::unique_ptr<KernelContext> ctx(new KernelContext);
  ctx->As<X86Context>();
  slice.SetContext(std::move(ctx));
  slice.SetParam(param);
  slice.Run();

  std::vector<float> out_ref(out.numel(), 0);
  slice_ref(x_data, x_shape, axes, starts, ends, out_ref.data());

  for (int i = 0; i < out.dims().production(); i++) {
    EXPECT_NEAR(out_ref[i], out_data[i], 1e-4);
  }
}

void test_tensor_list_case1(lite::Tensor x, lite::Tensor out) {
  std::vector<int64_t> x_shape({10});
  x.Resize(lite::DDim(x_shape));
  std::vector<int64_t> out_shape({5});
  out.Resize(lite::DDim(out_shape));

  auto x_data = x.mutable_data<float>();
  auto out_data = out.mutable_data<float>();

  for (int64_t i = 0; i < x.dims().production(); ++i) {
    x_data[i] = static_cast<float>(i);
  }

  std::vector<int> starts({3});
  std::vector<int> ends({8});
  std::vector<int> axes({0});

  // SliceCompute slice;
  SliceCompute<float> slice;
  operators::SliceParam param;

  param.X = &x;
  param.Out = &out;
  param.axes = axes;
  param.StartsTensorList.clear();
  param.EndsTensorList.clear();
  lite::Tensor starts_tensor, ends_tensor;
  for (int i = 0; i < 1; ++i) {
    starts_tensor.Resize(DDim({1}));
    ends_tensor.Resize(DDim({1}));
    starts_tensor.mutable_data<int>()[0] = starts[0];
    ends_tensor.mutable_data<int>()[0] = ends[0];
    param.StartsTensorList.push_back(&starts_tensor);
    param.EndsTensorList.push_back(&ends_tensor);
  }

  std::unique_ptr<KernelContext> ctx(new KernelContext);
  ctx->As<X86Context>();
  slice.SetContext(std::move(ctx));
  slice.SetParam(param);
  slice.Run();

  std::vector<float> out_ref(out.numel(), 0);
  slice_ref(x_data, x_shape, axes, starts, ends, out_ref.data());

  for (int i = 0; i < out.dims().production(); i++) {
    EXPECT_NEAR(out_ref[i], out_data[i], 1e-4);
  }
}

void test_tensor_list_case3(lite::Tensor x, lite::Tensor out) {
  std::vector<int64_t> x_shape({3, 4, 5});
  x.Resize(lite::DDim(x_shape));
  std::vector<int64_t> out_shape({3, 4, 2});
  out.Resize(lite::DDim(out_shape));

  auto x_data = x.mutable_data<float>();
  auto out_data = out.mutable_data<float>();

  for (int64_t i = 0; i < x.dims().production(); ++i) {
    x_data[i] = static_cast<float>(i);
  }

  std::vector<int> starts({-3, 0, 2});
  std::vector<int> ends({3, 100, -1});
  std::vector<int> axes({0, 1, 2});

  // SliceCompute slice;
  SliceCompute<float> slice;
  operators::SliceParam param;

  param.X = &x;
  param.Out = &out;
  param.axes = axes;
  param.StartsTensorList.clear();
  param.EndsTensorList.clear();
  lite::Tensor starts_tensor0, ends_tensor0;
  lite::Tensor starts_tensor1, ends_tensor1;
  lite::Tensor starts_tensor2, ends_tensor2;
  starts_tensor0.Resize(DDim({1}));
  starts_tensor1.Resize(DDim({1}));
  starts_tensor2.Resize(DDim({1}));
  ends_tensor0.Resize(DDim({1}));
  ends_tensor1.Resize(DDim({1}));
  ends_tensor2.Resize(DDim({1}));
  starts_tensor0.mutable_data<int>()[0] = starts[0];
  starts_tensor1.mutable_data<int>()[0] = starts[1];
  starts_tensor2.mutable_data<int>()[0] = starts[2];
  ends_tensor0.mutable_data<int>()[0] = ends[0];
  ends_tensor1.mutable_data<int>()[0] = ends[1];
  ends_tensor2.mutable_data<int>()[0] = ends[2];
  param.StartsTensorList.emplace_back(&starts_tensor0);
  param.StartsTensorList.emplace_back(&starts_tensor1);
  param.StartsTensorList.emplace_back(&starts_tensor2);
  param.EndsTensorList.emplace_back(&ends_tensor0);
  param.EndsTensorList.emplace_back(&ends_tensor1);
  param.EndsTensorList.emplace_back(&ends_tensor2);
530 531 532 533 534 535 536

  std::unique_ptr<KernelContext> ctx(new KernelContext);
  ctx->As<X86Context>();
  slice.SetContext(std::move(ctx));
  slice.SetParam(param);
  slice.Run();

537 538 539
  std::vector<float> out_ref(out.numel(), 0);
  slice_ref(x_data, x_shape, axes, starts, ends, out_ref.data());

540
  for (int i = 0; i < out.dims().production(); i++) {
541
    EXPECT_NEAR(out_ref[i], out_data[i], 1e-4);
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
  }
}

TEST(slice_x86, run_test) {
  lite::Tensor x;
  lite::Tensor out;

  test_case1(x, out);
  test_case2(x, out);
  test_case3(x, out);
  test_case4(x, out);
  test_case5(x, out);
  test_case6(x, out);
}

557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
TEST(slice_x86, test_tensor) {
  lite::Tensor x;
  lite::Tensor out;

  test_tensor_case1(x, out);
  test_tensor_case3(x, out);
}

TEST(slice_x86, test_tensor_list) {
  lite::Tensor x;
  lite::Tensor out;

  test_tensor_list_case1(x, out);
  test_tensor_list_case3(x, out);
}

573 574 575 576 577 578
}  // namespace x86
}  // namespace kernels
}  // namespace lite
}  // namespace paddle

USE_LITE_KERNEL(slice, kX86, kFloat, kNCHW, def);