test_dim.cu 2.5 KB
Newer Older
1
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2 3 4 5 6
//
// 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
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8 9 10 11 12 13
//
// 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.
14

F
fengjiayi 已提交
15 16 17 18
#include <thrust/device_vector.h>
#include <sstream>

#include "gtest/gtest.h"
19
#include "paddle/pten/core/utils/dim.h"
F
fengjiayi 已提交
20

21 22 23
namespace pten {
namespace tests {

24 25
__global__ void test(pten::framework::Dim<2>* o) {
  o[0] = pten::framework::make_dim(5, 6);
F
fengjiayi 已提交
26 27
}

Q
qijun 已提交
28
__global__ void dyn_idx_gpu(int64_t* o) {
29
  auto d = pten::framework::make_dim(5, 6);
30
  o[0] = d[1];
F
fengjiayi 已提交
31 32 33
}

TEST(Dim, Equality) {
34
  // construct a Dim on the CPU
35
  auto a = pten::framework::make_dim(3, 4);
36 37
  EXPECT_EQ(a[0], 3);
  EXPECT_EQ(a[1], 4);
38 39

  // construct a Dim on the GPU
40
  thrust::device_vector<pten::framework::Dim<2>> t(2);
41
#ifdef PADDLE_WITH_HIP
42 43
  hipLaunchKernelGGL(
      test, dim3(1), dim3(1), 0, 0, thrust::raw_pointer_cast(t.data()));
44
#else
45
  test<<<1, 1>>>(thrust::raw_pointer_cast(t.data()));
46
#endif
47
  a = t[0];
48 49
  EXPECT_EQ(a[0], 5);
  EXPECT_EQ(a[1], 6);
50 51

  // product
52
  EXPECT_EQ(pten::framework::product(a), 30);
53 54

  // mutate a Dim
55
  auto b = pten::framework::make_dim(7, 8);
56 57 58
  b[1] = 10;
  EXPECT_EQ(b[0], 7);
  EXPECT_EQ(b[1], 10);
59

60
  b[0] = 8;
61
  b[1] = 11;
62
  EXPECT_EQ(b[0], 8);
63 64 65
  EXPECT_EQ(b[1], 11);

  // dynamic access on GPU
Q
qijun 已提交
66
  thrust::device_vector<int64_t> r(1);
67
#ifdef PADDLE_WITH_HIP
68 69
  hipLaunchKernelGGL(
      dyn_idx_gpu, dim3(1), dim3(1), 0, 0, thrust::raw_pointer_cast(r.data()));
70
#else
71
  dyn_idx_gpu<<<1, 1>>>(thrust::raw_pointer_cast(r.data()));
72
#endif
Q
qijun 已提交
73
  int64_t res = r[0];
74
  EXPECT_EQ(res, 6);
F
fengjiayi 已提交
75 76 77
}

TEST(Dim, Bool) {
78 79 80
  auto a = pten::framework::make_dim(3, 4);
  auto b = pten::framework::make_dim(5, 6);
  auto c = pten::framework::make_dim(3, 4);
81 82 83 84 85

  // comparison
  EXPECT_TRUE(a == a);
  EXPECT_FALSE(a == b);
  EXPECT_TRUE(a == c);
F
fengjiayi 已提交
86 87 88
}

TEST(Dim, Print) {
89 90
  {
    std::stringstream ss;
91
    auto a = pten::framework::make_dim(2, 3);
92 93 94 95 96
    ss << a;
    EXPECT_EQ(ss.str(), "2, 3");
  }
  {
    std::stringstream ss;
97
    ss << pten::framework::make_dim(8);
98 99
    EXPECT_EQ(ss.str(), "8");
  }
100 101 102
}

}  // namespace tests
103
}  // namespace pten