{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathjit.cpp
More file actions
539 lines (466 loc) · 16.9 KB
/
Copy pathjit.cpp
File metadata and controls
539 lines (466 loc) · 16.9 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2026 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 <test.hpp>
#include <migraphx/ranges.hpp>
#include <migraphx/make_op.hpp>
#include <migraphx/generate.hpp>
#include <migraphx/instruction.hpp>
#include <migraphx/program.hpp>
#include <migraphx/par_for.hpp>
#include <migraphx/register_target.hpp>
#include <migraphx/value.hpp>
#include <migraphx/gpu/kernel.hpp>
#include <migraphx/gpu/hip.hpp>
#include <migraphx/gpu/context.hpp>
#include <migraphx/gpu/device_name.hpp>
#include <migraphx/gpu/compile_hip.hpp>
#include <migraphx/gpu/compile_hip_code_object.hpp>
#include <migraphx/gpu/compiler.hpp>
#include <migraphx/gpu/device_description.hpp>
// NOLINTNEXTLINE
const std::string write_2s = R"__migraphx__(
#ifndef __HIPCC_RTC__
#include <hip/hip_runtime.h>
#endif
extern "C" {
__global__ void write(char* data)
{
int num = threadIdx.x + blockDim.x * blockIdx.x;
data[num] = 2;
}
}
int main() {}
)__migraphx__";
// NOLINTNEXTLINE
const std::string add_2s_binary = R"__migraphx__(
#ifndef __HIPCC_RTC__
#include <hip/hip_runtime.h>
#endif
extern "C" {
__global__ void add_2(char* x, char* y)
{
int num = threadIdx.x + blockDim.x * blockIdx.x;
y[num] = x[num] + 2;
}
}
int main() {}
)__migraphx__";
// NOLINTNEXTLINE
const std::string simple_pointwise_increment = R"__migraphx__(
#include <migraphx/kernels/index.hpp>
#include <args.hpp>
using namespace migraphx;
extern "C" {
__global__ void kernel(void* x, void* y)
{
make_tensors()(x, y)([](auto xt, auto yt) __device__ {
auto idx = make_index();
const auto stride = idx.nglobal();
for(index_int i = idx.global; i < xt.get_shape().elements(); i += stride)
{
yt[i] = xt[i] + 1;
}
});
}
}
int main() {}
)__migraphx__";
// NOLINTNEXTLINE
const std::string check_define = R"__migraphx__(
#ifndef __DEFINE__
#error __DEFINE__ was not defined
#endif
int main() {}
)__migraphx__";
// NOLINTNEXTLINE
const std::string unused_param = R"__migraphx__(
extern "C" {
__global__ void kernel(void* x, void* y)
{}
}
int main() {}
)__migraphx__";
// NOLINTNEXTLINE
const std::string incorrect_program = R"__migraphx__(
extern "C" {
__global__ void kernel(void* x)
{
x += y;
}
}
int main() {}
)__migraphx__";
// NOLINTNEXTLINE
const std::string math_template = R"__migraphx__(
#include <migraphx/kernels/pointwise.hpp>
#include <migraphx/kernels/math.hpp>
#include <migraphx/kernels/types.hpp>
namespace migraphx {
template <class T>
struct test_implicit_conversion_op
{
T x;
template <index_int N, class U>
constexpr operator vec<U, N>() const
{
if constexpr(vec_size<T>() == 0)
{
return x;
}
else
{
static_assert(vec_size<T>() == N, "Vector mismatch size");
return __builtin_convertvector(x, vec<U, N>);
}
}
template <class U>
constexpr operator U() const
{
static_assert(is_same<T, bool>{} or not is_same<U, float>{} or is_same<T, U>{});
return static_cast<U>(x);
}
};
template <class T>
constexpr test_implicit_conversion_op<T> test_implicit_conversion(T x)
{
return {x};
}
extern "C" {
__global__ void kernel(${type}* p)
{
auto x = *p;
auto y = migraphx::vec_at(x, 0);
(void)y;
*p = migraphx::test_implicit_conversion(migraphx::${invoke});
(void)(1.f + migraphx::vec_at(migraphx::${invoke}, 0));
}
}
}
int main() {}
)__migraphx__";
static migraphx::src_file make_src_file(const std::string& name, const std::string& content)
{
return {name, content};
}
TEST_CASE(simple_compile_hip)
{
auto binaries = migraphx::gpu::compile_hip_src(
{make_src_file("main.cpp", write_2s)}, {}, migraphx::gpu::get_device_name());
EXPECT(binaries.size() == 1);
migraphx::argument input{{migraphx::shape::int8_type, {5}}};
auto ginput = migraphx::gpu::to_gpu(input);
migraphx::gpu::kernel k{binaries.front(), "write"};
k.launch(nullptr, input.get_shape().elements(), 1024)(ginput.cast<std::int8_t>());
auto output = migraphx::gpu::from_gpu(ginput);
EXPECT(output != input);
auto data = output.get<std::int8_t>();
EXPECT(migraphx::all_of(data, [](auto x) { return x == 2; }));
}
static auto check_target(const std::string& arch)
{
auto define = "__" + arch + "__";
auto content = migraphx::replace_string(check_define, "__DEFINE__", define);
return migraphx::gpu::compile_hip_src({make_src_file("main.cpp", content)}, {}, arch);
}
TEST_CASE(compile_target)
{
EXPECT(not check_target("gfx900").empty());
EXPECT(not check_target("gfx906").empty());
}
TEST_CASE(cross_compile_gpu_target_gfx1101)
{
// Verify a cross-compile gpu::context produces a code object for the requested arch.
migraphx::gpu::context ctx{migraphx::gpu::device_description{"gfx1101", 60, 1}};
auto binaries = migraphx::gpu::compile_hip_src(
{make_src_file("main.cpp", add_2s_binary)}, {}, ctx.get_current_device().get_device_name());
EXPECT(binaries.size() == 1);
std::string_view bin{binaries.front().data(), binaries.front().size()};
EXPECT(bin.find("gfx1101") != std::string_view::npos);
}
TEST_CASE(cross_compile_wavefront_size)
{
auto wavefront_size = [](const std::string& arch, std::size_t ws = 0) {
migraphx::gpu::device_description desc{arch, 1};
desc.wavefront_size = ws;
desc.normalize();
return desc.wavefront_size;
};
EXPECT(wavefront_size("gfx1101") == 32);
EXPECT(wavefront_size("gfx942") == 64);
EXPECT(wavefront_size("gfx942", 32) == 32);
EXPECT(wavefront_size("gfx12xx") == 32);
EXPECT(wavefront_size("gfx12xx", 64) == 64);
EXPECT(test::throws([&] { wavefront_size("gfx942", 16); }));
}
TEST_CASE(compile_errors)
{
EXPECT(test::throws([&] {
migraphx::gpu::compile_hip_src({make_src_file("main.cpp", incorrect_program)},
{},
migraphx::gpu::get_device_name(),
true);
}));
}
TEST_CASE(compile_warnings)
{
auto compile = [](const std::vector<std::string>& params) {
return migraphx::gpu::compile_hip_src({make_src_file("main.cpp", unused_param)},
params,
migraphx::gpu::get_device_name(),
true);
};
EXPECT(not compile({}).empty());
EXPECT(not compile({"-Wunused-parameter", "-Wno-error"}).empty());
EXPECT(not compile({"-Wno-unused-parameter", "-Werror"}).empty());
#ifdef MIGRAPHX_USE_HIPRTC
EXPECT(test::throws([&] { compile({"-Werror=unused-parameter"}); }));
EXPECT(test::throws([&] { compile({"-Wunused-parameter", "-Werror"}); }));
#else
EXPECT(test::throws([&] { compile({"-Werror=unused-parameter"}); }));
EXPECT(test::throws([&] { compile({"-Wunused-parameter", "-Werror"}); }));
#endif
}
TEST_CASE(has_flags)
{
EXPECT(migraphx::gpu::hip_has_flags({"--std=c++17"}));
EXPECT(not migraphx::gpu::hip_has_flags({"--non-existent-flag-to-test-in-migraphx"}));
EXPECT(migraphx::gpu::hip_has_flags({"-Wunused-parameter"}));
EXPECT(not migraphx::gpu::hip_has_flags(
{"-Wnon-existent-warnings-flag-to-test-in-migraphx", "-Werror"}));
}
TEST_CASE(code_object_hip)
{
auto binaries = migraphx::gpu::compile_hip_src(
{make_src_file("main.cpp", add_2s_binary)}, {}, migraphx::gpu::get_device_name());
EXPECT(binaries.size() == 1);
migraphx::shape input{migraphx::shape::int8_type, {5}};
std::vector<migraphx::shape> expected_inputs = {input, input};
auto co = migraphx::make_op("gpu::code_object",
{{"code_object", migraphx::value::binary{binaries.front()}},
{"symbol_name", "add_2"},
{"global", input.elements()},
{"local", 1024},
{"expected_inputs", migraphx::to_value(expected_inputs)},
{"output", migraphx::to_value(input)}});
migraphx::program p;
auto* mm = p.get_main_module();
auto input_literal = migraphx::generate_literal(input);
auto output_literal = migraphx::transform(input_literal, [](auto x) { return x + 2; });
auto x = mm->add_literal(input_literal);
auto y = mm->add_parameter("output", input);
mm->add_instruction(co, x, y);
migraphx::compile_options options;
p.compile(migraphx::make_target("gpu"), options);
auto result =
migraphx::gpu::from_gpu(p.eval({{"output", migraphx::gpu::allocate_gpu(input)}}).front());
EXPECT(result == output_literal.get_argument());
}
TEST_CASE(compile_code_object_hip)
{
migraphx::shape input{migraphx::shape::float_type, {5, 2}};
migraphx::gpu::hip_compile_options options;
migraphx::gpu::context ctx;
options.global = 256 * 1024;
options.local = 1024;
options.inputs = {input, input};
options.output = input;
auto co = migraphx::gpu::compile_hip_code_object(ctx, simple_pointwise_increment, options);
migraphx::program p;
auto* mm = p.get_main_module();
auto input_literal = migraphx::generate_literal(input);
auto output_literal = migraphx::transform(input_literal, [](auto x) { return x + 1; });
auto x = mm->add_literal(input_literal);
auto y = mm->add_parameter("output", input);
mm->add_instruction(co, x, y);
p.compile(migraphx::make_target("gpu"), migraphx::compile_options{});
auto result =
migraphx::gpu::from_gpu(p.eval({{"output", migraphx::gpu::allocate_gpu(input)}}).front());
EXPECT(result == output_literal.get_argument());
}
TEST_CASE(compile_pointwise)
{
migraphx::shape input{migraphx::shape::float_type, {5, 2}};
migraphx::gpu::context ctx;
auto co = migraphx::gpu::compile_op(
"pointwise", ctx, {input, input}, {{"lambda", "[](auto x) { return make_tuple(x + 1); }"}});
migraphx::program p;
auto* mm = p.get_main_module();
auto input_literal = migraphx::generate_literal(input);
auto output_literal = migraphx::transform(input_literal, [](auto x) { return x + 1; });
auto x = mm->add_literal(input_literal);
auto y = mm->add_parameter("output", input);
mm->add_instruction(co, x, y);
p.compile(migraphx::make_target("gpu"), migraphx::compile_options{});
auto result =
migraphx::gpu::from_gpu(p.eval({{"output", migraphx::gpu::allocate_gpu(input)}}).front());
EXPECT(result == output_literal.get_argument());
}
TEST_CASE(compile_math)
{
std::vector<std::string> math_invoke = {
// clang-format off
"abs(x)",
"acos(x)",
"acosh(x)",
"asin(x)",
"asinh(x)",
"atan(x)",
"atanh(x)",
"ceil(x)",
"cos(x)",
"cosh(x)",
"erf(x)",
"exp(x)",
"floor(x)",
"fmod(x, x)",
"fmod(x, y)",
"fmod(y, x)",
"isnan(x)",
"log(x)",
"max(x, x)",
"max(x, y)",
"max(y, x)",
"min(x, x)",
"min(x, y)",
"min(y, x)",
"pow(x, 0)",
"pow(x, x)",
"pow(x, y)",
"pow(y, x)",
"remainder(x,x)",
"remainder(x,y)",
"remainder(y,x)",
"round(x)",
"rsqrt(x)",
"sin(x)",
"sinh(x)",
"sqrt(x)",
"tan(x)",
"tanh(x)",
"where(true, x, x)",
"where(true, x, y)",
"where(true, y, x)",
// clang-format on
};
std::vector<std::string> data_types;
auto vec_sizes = {2, 4, 6};
for(auto&& t : migraphx::shape::types())
{
if(contains({migraphx::shape::bool_type, migraphx::shape::tuple_type}, t))
continue;
auto name = migraphx::shape::cpp_type(t);
if(contains({migraphx::shape::half_type, migraphx::shape::bf16_type}, t))
name.insert(0, "migraphx::");
data_types.push_back(name);
// fp8 doesn't have vectorization support yet, therefore skip it for now.
std::set<migraphx::shape::type_t> fp8_types = {migraphx::shape::fp8e4m3fnuz_type,
migraphx::shape::fp8e5m2fnuz_type,
migraphx::shape::fp8e4m3fn_type,
migraphx::shape::fp8e5m2_type};
if(not contains(fp8_types, t))
{
migraphx::transform(vec_sizes, std::back_inserter(data_types), [&](auto i) {
return "migraphx::vec<" + name + ", " + std::to_string(i) + ">";
});
}
}
migraphx::shape input{migraphx::shape::float_type, {5, 2}};
migraphx::gpu::hip_compile_options options;
migraphx::gpu::context ctx;
options.global = 1024;
options.local = 1024;
options.inputs = {input};
options.output = input;
migraphx::par_for(math_invoke.size() * data_types.size(), 1, [&](auto i) {
const auto& t = data_types[i % data_types.size()];
const auto& invoke = math_invoke[i / data_types.size()];
auto src = migraphx::interpolate_string(math_template, {{"type", t}, {"invoke", invoke}});
auto co = migraphx::gpu::compile_hip_code_object(ctx, src, options);
(void)co;
});
}
// NOLINTNEXTLINE
const std::string assert_template = R"__migraphx__(
#include <migraphx/kernels/math.hpp>
#include <migraphx/kernels/types.hpp>
using namespace migraphx;
extern "C" {
__global__ void kernel(void*)
{
static_assert(numeric_max<${type}>() == ${max}, "");
static_assert(numeric_lowest<${type}>() == ${min}, "");
}
}
int main() {}
)__migraphx__";
TEST_CASE(assert_type_min_max)
{
migraphx::gpu::hip_compile_options options;
migraphx::gpu::context ctx;
for(auto&& t : migraphx::shape::types())
{
if(contains(
{
migraphx::shape::bool_type,
migraphx::shape::tuple_type,
},
t))
continue;
auto name = migraphx::shape::cpp_type(t);
if(contains({migraphx::shape::half_type, migraphx::shape::bf16_type}, t))
name.insert(0, "migraphx::");
migraphx::shape::visit(t, [&](auto as) {
std::string min = "";
std::string max = "";
// Note 9223372036854775808 is a constant literal that is outside the range of long
// long type For the same reason, 18446744073709551616 needs postfix ULL to be parsed
// correctly
if(t == migraphx::shape::int64_type)
{
min = "(" + std::to_string(as.min() + 1) + "LL - 1)";
max = std::to_string(as.max());
}
else if(t == migraphx::shape::uint64_type)
{
min = std::to_string(as.min());
max = std::to_string(as.max()) + "ULL";
}
else
{
min = std::to_string(as.min());
max = std::to_string(as.max());
}
auto src = migraphx::interpolate_string(assert_template,
{{"type", name}, {"max", max}, {"min", min}});
migraphx::shape input{migraphx::shape::float_type, {5, 2}};
options.global = 1024;
options.local = 1024;
options.inputs = {input};
options.output = input;
options.emplace_param("-Wno-float-equal");
auto co = migraphx::gpu::compile_hip_code_object(ctx, src, options);
});
}
}
int main(int argc, const char* argv[]) { test::run(argc, argv); }
You can’t perform that action at this time.
