literal_test.cpp 5.21 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
Paul's avatar
Paul committed
24

Paul's avatar
Paul committed
25
#include <migraphx/literal.hpp>
26
#include <migraphx/serialize.hpp>
Paul's avatar
Paul committed
27
28
#include <sstream>
#include <string>
Paul's avatar
Paul committed
29
30
#include "test.hpp"

Paul's avatar
Paul committed
31
TEST_CASE(literal_test)
Paul's avatar
Paul committed
32
{
Paul's avatar
Paul committed
33
34
35
36
    EXPECT(migraphx::literal{1} == migraphx::literal{1});
    EXPECT(migraphx::literal{1} != migraphx::literal{2});
    EXPECT(migraphx::literal{} == migraphx::literal{});
    EXPECT(migraphx::literal{} != migraphx::literal{2});
Paul's avatar
Paul committed
37

Paul's avatar
Paul committed
38
39
    migraphx::literal l1{1};
    migraphx::literal l2 = l1; // NOLINT
Paul's avatar
Paul committed
40
41
    EXPECT(l1 == l2);
    EXPECT(l1.at<int>(0) == 1);
42
43
    EXPECT(not l1.empty());
    EXPECT(not l2.empty());
Paul's avatar
Paul committed
44

Paul's avatar
Paul committed
45
46
    migraphx::literal l3{};
    migraphx::literal l4{};
Paul's avatar
Paul committed
47
48
    EXPECT(l3 == l4);
    EXPECT(l3.empty());
Paul's avatar
Paul committed
49
    EXPECT(l4.empty());
Paul's avatar
Paul committed
50
51
}

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
TEST_CASE(literal_nstd_shape_vector)
{
    migraphx::shape nstd_shape{migraphx::shape::float_type, {1, 3, 2, 2}, {12, 1, 6, 3}};
    std::vector<float> data(12);
    std::iota(data.begin(), data.end(), 0);
    auto l0 = migraphx::literal{nstd_shape, data};

    // check data buffer is read in correctly
    std::vector<float> expected_buffer = {0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11};
    const auto* start                  = reinterpret_cast<const float*>(l0.data());
    std::vector<float> l0_data{start, start + 12};
    EXPECT(l0_data == expected_buffer);

    // check that using visit() (that uses a tensor view) gives data in correct order
    std::vector<float> results_vector(12);
    l0.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    EXPECT(results_vector == data);
}

Paul's avatar
Paul committed
71
TEST_CASE(literal_os1)
Paul's avatar
Paul committed
72
{
Paul's avatar
Paul committed
73
    migraphx::literal l{1};
Paul's avatar
Paul committed
74
75
76
77
78
    std::stringstream ss;
    ss << l;
    EXPECT(ss.str() == "1");
}

Paul's avatar
Paul committed
79
TEST_CASE(literal_os2)
Paul's avatar
Paul committed
80
{
Paul's avatar
Paul committed
81
    migraphx::literal l{};
Paul's avatar
Paul committed
82
83
    std::stringstream ss;
    ss << l;
Paul's avatar
Paul committed
84
    EXPECT(ss.str().empty());
Paul's avatar
Paul committed
85
86
}

Paul's avatar
Paul committed
87
TEST_CASE(literal_os3)
Paul's avatar
Paul committed
88
{
Paul's avatar
Paul committed
89
90
    migraphx::shape s{migraphx::shape::int64_type, {3}};
    migraphx::literal l{s, {1, 2, 3}};
Paul's avatar
Paul committed
91
92
93
94
95
    std::stringstream ss;
    ss << l;
    EXPECT(ss.str() == "1, 2, 3");
}

Paul's avatar
Paul committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
TEST_CASE(literal_visit_at)
{
    migraphx::literal x{1};
    bool visited = false;
    x.visit_at([&](int i) {
        visited = true;
        EXPECT(i == 1);
    });
    EXPECT(visited);
}

TEST_CASE(literal_visit)
{
    migraphx::literal x{1};
    migraphx::literal y{1};
    bool visited = false;
    x.visit([&](auto i) {
Paul's avatar
Paul committed
113
114
115
116
        y.visit([&](auto j) {
            visited = true;
            EXPECT(i == j);
        });
Paul's avatar
Paul committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
    });
    EXPECT(visited);
}

TEST_CASE(literal_visit_all)
{
    migraphx::literal x{1};
    migraphx::literal y{1};
    bool visited = false;
    migraphx::visit_all(x, y)([&](auto i, auto j) {
        visited = true;
        EXPECT(i == j);
    });
    EXPECT(visited);
}

TEST_CASE(literal_visit_mismatch_shape)
{
    migraphx::literal x{1};
    migraphx::shape s{migraphx::shape::int64_type, {3}};
    migraphx::literal y{s, {1, 2, 3}};
    bool visited = false;
    x.visit([&](auto i) {
Paul's avatar
Paul committed
140
141
142
143
        y.visit([&](auto j) {
            visited = true;
            EXPECT(i != j);
        });
Paul's avatar
Paul committed
144
145
146
147
148
149
150
151
152
153
    });
    EXPECT(visited);
}

TEST_CASE(literal_visit_all_mismatch_type)
{
    migraphx::shape s1{migraphx::shape::int32_type, {1}};
    migraphx::literal x{s1, {1}};
    migraphx::shape s2{migraphx::shape::int8_type, {1}};
    migraphx::literal y{s2, {1}};
Paul's avatar
Paul committed
154
155
    EXPECT(
        test::throws<migraphx::exception>([&] { migraphx::visit_all(x, y)([&](auto, auto) {}); }));
Paul's avatar
Paul committed
156
157
158
159
160
}

TEST_CASE(literal_visit_empty)
{
    migraphx::literal x{};
Paul's avatar
Paul committed
161
162
    EXPECT(test::throws([&] { x.visit([](auto) {}); }));
    EXPECT(test::throws([&] { x.visit_at([](auto) {}); }));
Paul's avatar
Paul committed
163
164
}

165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
TEST_CASE(value_literal)
{
    migraphx::shape s{migraphx::shape::int64_type, {3}};
    migraphx::literal l1{s, {1, 2, 3}};
    auto v1 = migraphx::to_value(l1);
    migraphx::literal l2{1};
    auto v2 = migraphx::to_value(l2);
    EXPECT(v1 != v2);

    auto l3 = migraphx::from_value<migraphx::literal>(v1);
    EXPECT(l3 == l1);
    auto l4 = migraphx::from_value<migraphx::literal>(v2);
    EXPECT(l4 == l2);
}

180
181
182
183
184
185
TEST_CASE(literal_to_string_float_precision)
{
    migraphx::literal x{126.99993142003703f};
    EXPECT(x.to_string() != "127");
}

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