operation.cpp 4.24 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's avatar
Paul committed
60
TEST_CASE(operation_equal_test)
Paul's avatar
Paul committed
61
62
{
    simple_operation s{};
Paul's avatar
Paul committed
63
    migraphx::operation op1 = s;
Paul's avatar
Paul committed
64
    s.data                  = 2;
Paul's avatar
Paul committed
65
66
    migraphx::operation op2 = op1; // NOLINT
    migraphx::operation op3 = s;   // NOLINT
Paul's avatar
Paul committed
67
68
69
70
71

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

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

Paul's avatar
Paul committed
78
TEST_CASE(operation_any_cast)
Paul's avatar
Paul committed
79
{
Paul's avatar
Paul committed
80
81
82
83
84
85
86
    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
87
88
}

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

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

Paul's avatar
Paul committed
107
108
109
110
111
112
113
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
114
115
116
117
    void
    finalize(migraphx::context&, const migraphx::shape&, const std::vector<migraphx::shape>&) const
    {
    }
Paul's avatar
Paul committed
118
119
120
121
122
123
124
125
126
};

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
127
128
    [[gnu::noreturn]] void
    finalize(migraphx::context&, const migraphx::shape&, const std::vector<migraphx::shape>&) const
Paul's avatar
Paul committed
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
163
    {
        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
164
    EXPECT(test::throws([&] { op.finalize(ctx, {}, {}); }));
Paul's avatar
Paul committed
165
166
}

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