dim_test.cu 4.3 KB
Newer Older
F
fengjiayi 已提交
1 2 3
#include <thrust/device_vector.h>
#include <sstream>

4
#include "paddle/framework/dim.h"
F
fengjiayi 已提交
5 6
#include "gtest/gtest.h"

7 8
__global__ void test(paddle::framework::Dim<2>* o) {
    o[0] = paddle::framework::make_dim(5, 6);
F
fengjiayi 已提交
9 10 11
}

__global__ void dyn_idx_gpu(int* o) {
12
    auto d = paddle::framework::make_dim(5, 6);
F
fengjiayi 已提交
13 14 15 16 17
    o[0] = d[1];
}

TEST(Dim, Equality) {
    // construct a Dim on the CPU
18 19 20
    auto a = paddle::framework::make_dim(3, 4);
    EXPECT_EQ(paddle::framework::get<0>(a), 3);
    EXPECT_EQ(paddle::framework::get<1>(a), 4);
F
fengjiayi 已提交
21 22

    // construct a Dim on the GPU
23
    thrust::device_vector<paddle::framework::Dim<2>> t(2);
F
fengjiayi 已提交
24 25
    test<<<1,1>>>(thrust::raw_pointer_cast(t.data()));
    a = t[0];
26 27
    EXPECT_EQ(paddle::framework::get<0>(a), 5);
    EXPECT_EQ(paddle::framework::get<1>(a), 6);
F
fengjiayi 已提交
28 29

    // linearization
30 31
    auto b = paddle::framework::make_dim(7, 8);
    EXPECT_EQ(paddle::framework::linearize(a, b), 83);
F
fengjiayi 已提交
32 33

    // product
34
    EXPECT_EQ(paddle::framework::product(a), 30);
F
fengjiayi 已提交
35 36

    // mutate a Dim
37 38 39
    paddle::framework::get<1>(b) = 10;
    EXPECT_EQ(paddle::framework::get<0>(b), 7);
    EXPECT_EQ(paddle::framework::get<1>(b), 10);
F
fengjiayi 已提交
40 41

    // dynamic access
42
    paddle::framework::get(b, 0) = 8;
F
fengjiayi 已提交
43
    b[1] = 11;
44 45 46
    EXPECT_EQ(paddle::framework::get<0>(b), 8);
    EXPECT_EQ(paddle::framework::get<1>(b), 11);
    EXPECT_EQ(paddle::framework::get(b, 0), 8);
F
fengjiayi 已提交
47 48 49 50 51 52 53 54 55
    EXPECT_EQ(b[1], 11);

    // dynamic access on GPU
    thrust::device_vector<int> r(1);
    dyn_idx_gpu<<<1,1>>>(thrust::raw_pointer_cast(r.data()));
    int res = r[0];
    EXPECT_EQ(res, 6);

    // ex_prefix_mul
56 57 58 59
    paddle::framework::Dim<3> c = paddle::framework::ex_prefix_mul(paddle::framework::Dim<3>(3, 4, 5));
    EXPECT_EQ(paddle::framework::get<0>(c), 1);
    EXPECT_EQ(paddle::framework::get<1>(c), 3);
    EXPECT_EQ(paddle::framework::get<2>(c), 12);
F
fengjiayi 已提交
60 61

    // contiguous_strides
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    c = paddle::framework::contiguous_strides(paddle::framework::Dim<3>(10, 1, 10));
    EXPECT_EQ(paddle::framework::get<0>(c), 1);
    EXPECT_EQ(paddle::framework::get<1>(c), 0);
    EXPECT_EQ(paddle::framework::get<2>(c), 10);
    c = paddle::framework::contiguous_strides(paddle::framework::Dim<3>(10, 10, 1));
    EXPECT_EQ(paddle::framework::get<0>(c), 1);
    EXPECT_EQ(paddle::framework::get<1>(c), 10);
    EXPECT_EQ(paddle::framework::get<2>(c), 0);
    c = paddle::framework::contiguous_strides(paddle::framework::Dim<3>(1, 10, 10));
    EXPECT_EQ(paddle::framework::get<0>(c), 0);
    EXPECT_EQ(paddle::framework::get<1>(c), 1);
    EXPECT_EQ(paddle::framework::get<2>(c), 10);
    c = paddle::framework::contiguous_strides(paddle::framework::Dim<3>(2, 3, 4));
    EXPECT_EQ(paddle::framework::get<0>(c), 1);
    EXPECT_EQ(paddle::framework::get<1>(c), 2);
    EXPECT_EQ(paddle::framework::get<2>(c), 6);
F
fengjiayi 已提交
78 79

    // generate from an index
80 81 82 83 84 85 86 87 88
    auto size = paddle::framework::make_dim(4, 5, 2);
    c = paddle::framework::Dim<3>(14, size);
    EXPECT_EQ(paddle::framework::get<0>(c), 2);
    EXPECT_EQ(paddle::framework::get<1>(c), 3);
    EXPECT_EQ(paddle::framework::get<2>(c), 0);
    c = paddle::framework::Dim<3>(25, size);
    EXPECT_EQ(paddle::framework::get<0>(c), 1);
    EXPECT_EQ(paddle::framework::get<1>(c), 1);
    EXPECT_EQ(paddle::framework::get<2>(c), 1);
F
fengjiayi 已提交
89 90 91
}

TEST(Dim, Bool) {
92 93 94
    auto a = paddle::framework::make_dim(3, 4);
    auto b = paddle::framework::make_dim(5, 6);
    auto c = paddle::framework::make_dim(3, 4);
F
fengjiayi 已提交
95 96

    // in_bounds check
97 98
    EXPECT_TRUE(paddle::framework::contained(a, b));
    EXPECT_FALSE(paddle::framework::contained(b, a));
F
fengjiayi 已提交
99 100 101 102 103 104 105 106

    // comparison
    EXPECT_TRUE(a == a);
    EXPECT_FALSE(a == b);
    EXPECT_TRUE(a == c);

    // contiguous check
    int x = 4, y = 5, z = 2;
107 108 109 110 111 112 113
    paddle::framework::Dim<3> sizef(x, y, z);
    paddle::framework::Dim<3> stridea(1, x, x*y);
    paddle::framework::Dim<3> strideb(2, 2*x, 2*x*y);
    paddle::framework::Dim<3> stridec(1, x, 2*x*y);
    EXPECT_TRUE(paddle::framework::contiguous(sizef, stridea));
    EXPECT_FALSE(paddle::framework::contiguous(sizef, strideb));
    EXPECT_FALSE(paddle::framework::contiguous(sizef, stridec));
F
fengjiayi 已提交
114 115 116 117 118
}

TEST(Dim, Print) {
    {
        std::stringstream ss;
119
        auto a = paddle::framework::make_dim(2, 3);
F
fengjiayi 已提交
120 121 122 123 124
        ss << a;
        EXPECT_EQ(ss.str(), "2, 3");
    }
    {
        std::stringstream ss;
125
        ss << paddle::framework::make_dim(8);
F
fengjiayi 已提交
126 127 128
        EXPECT_EQ(ss.str(), "8");
    }
}