gather_test.cc 1.8 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Z
zchen0211 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

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. */

15 16
#include "paddle/phi/kernels/funcs/gather.h"

Z
zchen0211 已提交
17 18
#include <gtest/gtest.h>

19 20
#include "paddle/fluid/framework/tensor.h"
#include "paddle/fluid/platform/place.h"
Z
zchen0211 已提交
21

22
TEST(Gather, GatherData) {
23 24 25
  phi::DenseTensor* src = new phi::DenseTensor();
  phi::DenseTensor* index = new phi::DenseTensor();
  phi::DenseTensor* output = new phi::DenseTensor();
Z
zchen0211 已提交
26 27 28

  int* p_src = nullptr;
  int* p_index = nullptr;
29
  p_src = src->mutable_data<int>(phi::make_ddim({3, 4}),
30
                                 paddle::platform::CPUPlace());
31
  p_index = index->mutable_data<int>(phi::make_ddim({2}),
32
                                     paddle::platform::CPUPlace());
Z
zchen0211 已提交
33

Z
zchen0211 已提交
34
  for (int i = 0; i < 12; ++i) p_src[i] = i;
Z
zchen0211 已提交
35 36 37
  p_index[0] = 1;
  p_index[1] = 0;

38
  int* p_output = output->mutable_data<int>(phi::make_ddim({2, 4}),
39
                                            paddle::platform::CPUPlace());
Z
zchen0211 已提交
40

Z
zchen0211 已提交
41
  auto* cpu_place = new paddle::platform::CPUPlace();
L
Leo Chen 已提交
42
  phi::CPUContext ctx(*cpu_place);
43
  phi::funcs::CPUGather<int>(ctx, *src, *index, output);
44
  delete cpu_place;
45
  cpu_place = nullptr;
Z
zchen0211 已提交
46 47
  for (int i = 0; i < 4; ++i) EXPECT_EQ(p_output[i], i + 4);
  for (int i = 4; i < 8; ++i) EXPECT_EQ(p_output[i], i - 4);
48 49 50 51

  delete src;
  delete index;
  delete output;
Z
zchen0211 已提交
52
}