gemm_tune.cpp 7.76 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
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2023 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.
 */

#include <iostream>
#include <vector>
#include <migraphx/gpu/gemm.hpp>
#include <hip/hip_runtime_api.h>
#include <migraphx/gpu/target.hpp>
#include <migraphx/verify.hpp>
#include <test.hpp>
#include <migraphx/make_op.hpp>
#include <migraphx/iterator_for.hpp>

// includes needed for run_lowering
#include <migraphx/gpu/lowering.hpp>
#include <migraphx/auto_contiguous.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/pass_manager.hpp>

// Abbreviated lowering; we don't need the usual cleanup passes for this test
void run_lowering(migraphx::program& p, bool offload_copy = false)
{
    auto ctx = migraphx::gpu::context{};
    migraphx::run_passes(
        *p.get_main_module(),
        {migraphx::auto_contiguous{}, migraphx::gpu::lowering{&ctx, offload_copy}});
}

/**
 * Tests the automatic GEMM tuning feature.  In the finalize() method of the gemm op,
 * rocBLAS API functions are called to quickly benchmark all the GEMM solutions
 * available in the currently installed rocBLAS library and choose the index of the fastest.
 */
TEST_CASE(gemm_tune_with_rocblas)
{
    migraphx::program p;
    auto* mm = p.get_main_module();

    migraphx::shape sa{migraphx::shape::float_type, {4, 2}};
    migraphx::shape sb{migraphx::shape::float_type, {2, 3}};
    auto a = mm->add_parameter("a", sa);
    auto b = mm->add_parameter("b", sb);

    migraphx::operation dot_op = migraphx::make_op("dot");
    mm->add_instruction(dot_op, a, b);

    // lowering adds gemm implementation for dot operator
    run_lowering(p);

    migraphx::target gpu_t = migraphx::gpu::target{};
    migraphx::compile_options options;
    options.exhaustive_tune = true;
    p.compile(gpu_t, options);

    migraphx::value solution_idx(0);
    for(auto ins : iterator_for(*p.get_main_module()))
    {
        if(ins->name() == "gpu::gemm")
        {
            auto gemm_op = migraphx::get_operation(ins);

            // tuned solution index is not deterministic, but anything other than 0
            // (default, invalid, or not available) is good.
            // gemm_op.to_value().debug_print();
            solution_idx = gemm_op.to_value()["solution_idx"];
            break;
        }
    }
#ifdef MIGRAPHX_USE_ROCBLAS_TUNING_API
    EXPECT(0 != solution_idx.to<std::size_t>());
#else
    EXPECT(0 == solution_idx.to<std::size_t>());
#endif
}

// GEMM tuning of a strided-batch matrix; invokes rocblas_gemm_strided_batched_ex
TEST_CASE(gemm_tune_strided)
{
    migraphx::program p;
    auto* mm = p.get_main_module();

    migraphx::shape sa{migraphx::shape::float_type, {4, 2, 2}};
    migraphx::shape sb{migraphx::shape::float_type, {4, 2, 2}};
    migraphx::shape s_output{migraphx::shape::float_type, {4, 2, 2}};
    auto a      = mm->add_parameter("a", sa);
    auto b      = mm->add_parameter("b", sb);
    auto output = mm->add_parameter("out", s_output);

    auto gemm_oper = migraphx::make_op("gpu::gemm", {{"beta", 2}});
    mm->add_instruction(gemm_oper, a, b, output);

    migraphx::target gpu_t = migraphx::gpu::target{};
    migraphx::compile_options options;
    options.exhaustive_tune = true;
    p.compile(gpu_t, options);

    migraphx::value solution_idx(0);
    for(auto ins : iterator_for(*p.get_main_module()))
    {
        if(ins->name() == "gpu::gemm")
        {
            auto gemm_op = migraphx::get_operation(ins);
            auto gemmv   = gemm_op.to_value();

            // tuned solution index is not deterministic, but anything other than 0
            // (default, invalid, or not available) is good.
            solution_idx = gemm_op.to_value()["solution_idx"];
            break;
        }
    }
#ifdef MIGRAPHX_USE_ROCBLAS_TUNING_API
    EXPECT(0 != solution_idx.to<std::size_t>());
#else
    EXPECT(0 == solution_idx.to<std::size_t>());
#endif
}

// GEMM tuning of a strided-batch matrix; created by lowering
TEST_CASE(gemm_tune_strided_lowered)
{
    migraphx::program p;
    auto* mm = p.get_main_module();

    // At time of writing this test, gemm_impl considers a shape is strided if it has
    // at least three dimensions and the 3rd-to-last is nonzero, invoking
    // rocblas_gemm_strided_batched_ex. Also, DOT operator requires all dimensions except the last
    // two to be equal.
    migraphx::shape sa{migraphx::shape::float_type, {4, 2, 5}};
    migraphx::shape sb{migraphx::shape::float_type, {4, 5, 3}};
    auto a = mm->add_parameter("a", sa);
    auto b = mm->add_parameter("b", sb);

    migraphx::operation dot_op = migraphx::make_op("dot");
    mm->add_instruction(dot_op, a, b);

    // lowering adds gemm implementation for dot operator
    run_lowering(p);

    migraphx::target gpu_t = migraphx::gpu::target{};
    migraphx::compile_options options;
    options.exhaustive_tune = true;
    p.compile(gpu_t, options);

    migraphx::value solution_idx(0);
    for(auto ins : iterator_for(*p.get_main_module()))
    {
        if(ins->name() == "gpu::gemm")
        {
            auto gemm_op = migraphx::get_operation(ins);

            // tuned solution index is not deterministic, but anything other than 0
            // (default, invalid, or not available) is good.
            solution_idx = gemm_op.to_value()["solution_idx"];
            break;
        }
    }
#ifdef MIGRAPHX_USE_ROCBLAS_TUNING_API
    EXPECT(0 != solution_idx.to<std::size_t>());
#else
    EXPECT(0 == solution_idx.to<std::size_t>());
#endif
}

TEST_CASE(gemm_tune_invalid_sol_index)
{
    migraphx::program p;
    auto* mm = p.get_main_module();

    migraphx::shape sa{migraphx::shape::float_type, {4, 2}};
    migraphx::shape sb{migraphx::shape::float_type, {2, 3}};
    migraphx::shape s_output{migraphx::shape::float_type, {4, 3}};
    auto a      = mm->add_parameter("a", sa);
    auto b      = mm->add_parameter("b", sb);
    auto output = mm->add_parameter("out", s_output);

    auto gemm_oper = migraphx::make_op("gpu::gemm", {{"solution_idx", 987654321}});
    mm->add_instruction(gemm_oper, a, b, output);

    migraphx::target gpu_t = migraphx::gpu::target{};
    migraphx::compile_options options;
    options.exhaustive_tune = true;
    p.compile(gpu_t, options);

    migraphx::value solution_idx(0);
    for(auto ins : iterator_for(*p.get_main_module()))
    {
        if(ins->name() == "gpu::gemm")
        {
            auto gemm_op = migraphx::get_operation(ins);
            auto gemmv   = gemm_op.to_value();

            // given invalid starting index, should return default 0
            solution_idx = gemm_op.to_value()["solution_idx"];
            break;
        }
    }
#ifdef MIGRAPHX_USE_ROCBLAS_TUNING_API
    EXPECT(0 == solution_idx.to<std::size_t>());
#else
    EXPECT(0 != solution_idx.to<std::size_t>());
#endif
}

int main(int argc, const char* argv[]) { test::run(argc, argv); }