operation.cpp 4.41 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
#include <migraphx/context.hpp>
Paul's avatar
Paul committed
4
5
6
7
8
9
#include <sstream>
#include <string>
#include "test.hpp"

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

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

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

Paul Fultz II's avatar
Paul Fultz II committed
60
61
62
63
64
65
66
67
68
TEST_CASE(operation_copy_assign_test)
{
    simple_operation s{};
    migraphx::operation op;
    op = s;
    // cppcheck-suppress duplicateExpression
    EXPECT(s == op);
}

Paul's avatar
Paul committed
69
TEST_CASE(operation_equal_test)
Paul's avatar
Paul committed
70
71
{
    simple_operation s{};
Paul's avatar
Paul committed
72
    migraphx::operation op1 = s;
Paul's avatar
Paul committed
73
    s.data                  = 2;
Paul's avatar
Paul committed
74
75
    migraphx::operation op2 = op1; // NOLINT
    migraphx::operation op3 = s;   // NOLINT
Paul's avatar
Paul committed
76
77
78
79
80

    EXPECT(s != op1);
    EXPECT(op2 == op1);
    EXPECT(op3 != op2);
    EXPECT(op3 != op1);
Paul's avatar
Paul committed
81
82
}

Paul's avatar
Paul committed
83
84
85
struct not_operation
{
};
Paul's avatar
Paul committed
86

Paul's avatar
Paul committed
87
TEST_CASE(operation_any_cast)
Paul's avatar
Paul committed
88
{
Paul's avatar
Paul committed
89
90
91
92
93
94
95
    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
96
97
}

Paul's avatar
Paul committed
98
TEST_CASE(operation_print)
Paul's avatar
Paul committed
99
{
Paul's avatar
Paul committed
100
    migraphx::operation op = simple_operation{};
Paul's avatar
Paul committed
101
102
103
    std::stringstream ss;
    ss << op;
    std::string s = ss.str();
Paul's avatar
Paul committed
104
    EXPECT(s == "simple[1]");
Paul's avatar
Paul committed
105
106
}

Paul's avatar
Paul committed
107
TEST_CASE(operation_default_print)
Paul's avatar
Paul committed
108
{
Paul's avatar
Paul committed
109
    migraphx::operation op = simple_operation_no_print{};
Paul's avatar
Paul committed
110
111
112
113
114
115
    std::stringstream ss;
    ss << op;
    std::string s = ss.str();
    EXPECT(s == "simple");
}

Paul's avatar
Paul committed
116
117
118
119
120
121
122
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
123
124
125
126
    void
    finalize(migraphx::context&, const migraphx::shape&, const std::vector<migraphx::shape>&) const
    {
    }
Paul's avatar
Paul committed
127
128
129
130
131
132
133
134
135
};

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
136
137
    [[gnu::noreturn]] void
    finalize(migraphx::context&, const migraphx::shape&, const std::vector<migraphx::shape>&) const
Paul's avatar
Paul committed
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
163
164
165
166
167
168
169
170
171
172
    {
        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
173
    EXPECT(test::throws([&] { op.finalize(ctx, {}, {}); }));
Paul's avatar
Paul committed
174
175
}

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