operation.cpp 4.21 KB
Newer Older
Paul's avatar
Paul committed
1

Paul's avatar
Paul committed
2
#include <migraphx/operation.hpp>
Paul's avatar
Paul committed
3
4
5
6
7
8
#include <sstream>
#include <string>
#include "test.hpp"

struct simple_operation
{
Paul's avatar
Paul committed
9
    template <class T, class F>
Paul's avatar
Paul committed
10
11
    static auto reflect(T& x, F f)
    {
Paul's avatar
Paul committed
12
        return migraphx::pack(f(x.data, "data"));
Paul's avatar
Paul committed
13
    }
Paul's avatar
Paul committed
14
    int data = 1;
Paul's avatar
Paul committed
15
    std::string name() const { return "simple"; }
Paul's avatar
Paul committed
16
    migraphx::shape compute_shape(const std::vector<migraphx::shape>&) const
Paul's avatar
Paul committed
17
    {
Paul's avatar
Paul committed
18
        MIGRAPHX_THROW("not computable");
Paul's avatar
Paul committed
19
    }
Paul's avatar
Paul committed
20
21
22
    migraphx::argument compute(migraphx::context&,
                               const migraphx::shape&,
                               const std::vector<migraphx::argument>&) const
Paul's avatar
Paul committed
23
    {
Paul's avatar
Paul committed
24
        MIGRAPHX_THROW("not computable");
Paul's avatar
Paul committed
25
    }
Paul's avatar
Paul committed
26
    friend std::ostream& operator<<(std::ostream& os, const simple_operation& op)
Paul's avatar
Paul committed
27
    {
Paul's avatar
Paul committed
28
        os << op.name() << "[" << op.data << "]";
Paul's avatar
Paul committed
29
30
        return os;
    }
Paul's avatar
Paul committed
31
32
};

Paul's avatar
Paul committed
33
34
35
struct simple_operation_no_print
{
    std::string name() const { return "simple"; }
Paul's avatar
Paul committed
36
    migraphx::shape compute_shape(const std::vector<migraphx::shape>&) const
Paul's avatar
Paul committed
37
    {
Paul's avatar
Paul committed
38
        MIGRAPHX_THROW("not computable");
Paul's avatar
Paul committed
39
    }
Paul's avatar
Paul committed
40
41
42
    migraphx::argument compute(migraphx::context&,
                               const migraphx::shape&,
                               const std::vector<migraphx::argument>&) const
Paul's avatar
Paul committed
43
    {
Paul's avatar
Paul committed
44
        MIGRAPHX_THROW("not computable");
Paul's avatar
Paul committed
45
    }
Paul's avatar
Paul committed
46
47
};

Paul's avatar
Paul committed
48
TEST_CASE(operation_copy_test)
Paul's avatar
Paul committed
49
50
{
    simple_operation s{};
Paul's avatar
Paul committed
51
52
    migraphx::operation op1 = s;   // NOLINT
    migraphx::operation op2 = op1; // NOLINT
Paul's avatar
Paul committed
53
    // cppcheck-suppress duplicateExpression
Paul's avatar
Paul committed
54
    EXPECT(s == op1);
Paul's avatar
Paul committed
55
    // cppcheck-suppress duplicateExpression
Paul's avatar
Paul committed
56
57
58
    EXPECT(op2 == op1);
}

Paul's avatar
Paul committed
59
TEST_CASE(operation_equal_test)
Paul's avatar
Paul committed
60
61
{
    simple_operation s{};
Paul's avatar
Paul committed
62
    migraphx::operation op1 = s;
Paul's avatar
Paul committed
63
    s.data                  = 2;
Paul's avatar
Paul committed
64
65
    migraphx::operation op2 = op1; // NOLINT
    migraphx::operation op3 = s;   // NOLINT
Paul's avatar
Paul committed
66
67
68
69
70

    EXPECT(s != op1);
    EXPECT(op2 == op1);
    EXPECT(op3 != op2);
    EXPECT(op3 != op1);
Paul's avatar
Paul committed
71
72
}

Paul's avatar
Paul committed
73
74
75
struct not_operation
{
};
Paul's avatar
Paul committed
76

Paul's avatar
Paul committed
77
TEST_CASE(operation_any_cast)
Paul's avatar
Paul committed
78
{
Paul's avatar
Paul committed
79
80
81
82
83
84
85
    migraphx::operation op1 = simple_operation{};
    EXPECT(migraphx::any_cast<simple_operation>(op1).data == 1);
    EXPECT(migraphx::any_cast<not_operation*>(&op1) == nullptr);
    EXPECT(test::throws([&] { migraphx::any_cast<not_operation&>(op1); }));
    migraphx::operation op2 = simple_operation{2};
    EXPECT(migraphx::any_cast<simple_operation>(op2).data == 2);
    EXPECT(migraphx::any_cast<not_operation*>(&op2) == nullptr);
Paul's avatar
Paul committed
86
87
}

Paul's avatar
Paul committed
88
TEST_CASE(operation_print)
Paul's avatar
Paul committed
89
{
Paul's avatar
Paul committed
90
    migraphx::operation op = simple_operation{};
Paul's avatar
Paul committed
91
92
93
    std::stringstream ss;
    ss << op;
    std::string s = ss.str();
Paul's avatar
Paul committed
94
    EXPECT(s == "simple[1]");
Paul's avatar
Paul committed
95
96
}

Paul's avatar
Paul committed
97
TEST_CASE(operation_default_print)
Paul's avatar
Paul committed
98
{
Paul's avatar
Paul committed
99
    migraphx::operation op = simple_operation_no_print{};
Paul's avatar
Paul committed
100
101
102
103
104
105
    std::stringstream ss;
    ss << op;
    std::string s = ss.str();
    EXPECT(s == "simple");
}

Paul's avatar
Paul committed
106
107
108
109
110
111
112
struct final_operation
{
    std::string name() const { return "final"; }
    migraphx::shape compute_shape(const std::vector<migraphx::shape>&) const
    {
        MIGRAPHX_THROW("not computable");
    }
Paul's avatar
Paul committed
113
114
115
116
    void
    finalize(migraphx::context&, const migraphx::shape&, const std::vector<migraphx::shape>&) const
    {
    }
Paul's avatar
Paul committed
117
118
119
120
121
122
123
124
125
};

struct final_operation_throw
{
    std::string name() const { return "final"; }
    migraphx::shape compute_shape(const std::vector<migraphx::shape>&) const
    {
        MIGRAPHX_THROW("not computable");
    }
Paul's avatar
Paul committed
126
127
    [[gnu::noreturn]] void
    finalize(migraphx::context&, const migraphx::shape&, const std::vector<migraphx::shape>&) const
Paul's avatar
Paul committed
128
129
130
131
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
158
159
160
161
162
    {
        MIGRAPHX_THROW("finalize");
    }
};

TEST_CASE(check_has_finalize_simple)
{
    migraphx::operation op = simple_operation{};
    EXPECT(not migraphx::has_finalize(op));
}

TEST_CASE(check_has_finalize)
{
    migraphx::operation op = final_operation{};
    EXPECT(migraphx::has_finalize(op));
}

TEST_CASE(check_run_finalize)
{
    migraphx::operation op = final_operation{};
    migraphx::context ctx{};
    op.finalize(ctx, {}, {});
}

TEST_CASE(check_run_finalize_simple)
{
    migraphx::operation op = simple_operation{};
    migraphx::context ctx{};
    op.finalize(ctx, {}, {});
}

TEST_CASE(check_run_finalize_throw)
{
    migraphx::operation op = final_operation_throw{};
    migraphx::context ctx{};
Paul's avatar
Paul committed
163
    EXPECT(test::throws([&] { op.finalize(ctx, {}, {}); }));
Paul's avatar
Paul committed
164
165
}

Paul's avatar
Paul committed
166
int main(int argc, const char* argv[]) { test::run(argc, argv); }