-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathsimd_op_check.h
More file actions
568 lines (480 loc) · 19.3 KB
/
simd_op_check.h
File metadata and controls
568 lines (480 loc) · 19.3 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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
#ifndef SIMD_OP_CHECK_H
#define SIMD_OP_CHECK_H
#include "Halide.h"
#include "halide_test_dirs.h"
#include "halide_thread_pool.h"
#include "test_sharding.h"
#include <fstream>
#include <iostream>
namespace {
using namespace Halide;
// Some exprs of each type to use in checked expressions. These will turn
// into loads to thread-local image params.
Expr input(const Type &t, const Expr &arg) {
return Internal::Call::make(t, "input", {arg}, Internal::Call::Extern);
}
Expr in_f16(const Expr &arg) {
return input(Float(16), arg);
}
Expr in_bf16(const Expr &arg) {
return input(BFloat(16), arg);
}
Expr in_f32(const Expr &arg) {
return input(Float(32), arg);
}
Expr in_f64(const Expr &arg) {
return input(Float(64), arg);
}
Expr in_i8(const Expr &arg) {
return input(Int(8), arg);
}
Expr in_i16(const Expr &arg) {
return input(Int(16), arg);
}
Expr in_i32(const Expr &arg) {
return input(Int(32), arg);
}
Expr in_i64(const Expr &arg) {
return input(Int(64), arg);
}
Expr in_u8(const Expr &arg) {
return input(UInt(8), arg);
}
Expr in_u16(const Expr &arg) {
return input(UInt(16), arg);
}
Expr in_u32(const Expr &arg) {
return input(UInt(32), arg);
}
Expr in_u64(const Expr &arg) {
return input(UInt(64), arg);
}
} // namespace
namespace Halide {
struct TestResult {
std::string op;
std::string error_msg;
};
struct Task {
std::string op;
std::string name;
int vector_width;
Expr expr;
};
class SimdOpCheckTest {
public:
static constexpr int max_i8 = 127;
static constexpr int max_i16 = 32767;
static constexpr int max_i32 = 0x7fffffff;
static constexpr int max_u8 = 255;
static constexpr int max_u16 = 65535;
const Expr max_u32 = UInt(32).max();
std::string filter{"*"};
std::string output_directory{Internal::get_test_tmp_dir()};
std::vector<Task> tasks;
Target target;
int W;
int H;
int rng_seed;
using Sharder = Halide::Internal::Test::Sharder;
SimdOpCheckTest(const Target t, int w, int h)
: target(t), W(w), H(h), rng_seed(0) {
target = target
.with_feature(Target::NoBoundsQuery)
.with_feature(Target::NoAsserts)
.with_feature(Target::NoRuntime);
}
virtual ~SimdOpCheckTest() = default;
void set_seed(int seed) {
rng_seed = seed;
}
virtual bool can_run_code() const {
if (target.arch == Target::WebAssembly) {
return Halide::Internal::WasmModule::can_jit_target(Target("wasm-32-wasmrt"));
}
// If we can (target matches host), run the error checking Halide::Func.
Target host_target = get_host_target();
bool can_run_the_code =
(target.arch == host_target.arch &&
target.bits == host_target.bits &&
target.os == host_target.os);
// A bunch of feature flags also need to match between the
// compiled code and the host in order to run the code.
for (Target::Feature f : {
Target::ARMDotProd,
Target::ARMFp16,
Target::ARMv7s,
Target::ARMv8a,
Target::ARMv81a,
Target::ARMv82a,
Target::ARMv83a,
Target::ARMv84a,
Target::ARMv85a,
Target::ARMv86a,
Target::ARMv87a,
Target::ARMv88a,
Target::ARMv89a,
Target::AVX,
Target::AVX2,
Target::AVXVNNI,
Target::AVX512,
Target::AVX512_Cannonlake,
Target::AVX512_KNL,
Target::AVX512_SapphireRapids,
Target::AVX512_Skylake,
Target::F16C,
Target::FMA,
Target::FMA4,
Target::NoNEON,
Target::POWER_ARCH_2_07,
Target::RVV,
Target::SSE41,
Target::SVE,
Target::SVE2,
Target::VSX,
}) {
if (target.has_feature(f) != host_target.has_feature(f)) {
can_run_the_code = false;
}
}
return can_run_the_code;
}
virtual void compile_and_check(Func error,
const std::string &op,
const std::string &name,
int vector_width,
const std::vector<Argument> &arg_types,
std::ostringstream &error_msg) {
std::string fn_name = "test_" + name;
std::string file_name = output_directory + fn_name;
auto ext = Internal::get_output_info(target);
std::map<OutputFileType, std::string> outputs = {
{OutputFileType::c_header, file_name + ext.at(OutputFileType::c_header).extension},
{OutputFileType::object, file_name + ext.at(OutputFileType::object).extension},
{OutputFileType::assembly, file_name + ".s"},
};
error.compile_to(outputs, arg_types, fn_name, target);
std::ifstream asm_file;
asm_file.open(file_name + ".s");
bool found_it = false;
std::ostringstream msg;
msg << op << " did not generate for target=" << get_run_target().to_string() << " vector_width=" << vector_width << ". Instead we got:\n";
std::string line;
while (getline(asm_file, line)) {
msg << line << "\n";
// Check for the op in question
found_it |= wildcard_search(op, line) && !wildcard_search("_" + op, line);
}
if (!found_it) {
error_msg << "Failed: " << msg.str() << "\n";
}
asm_file.close();
}
// Check if pattern p matches str, allowing for wildcards (*).
bool wildcard_match(const char *p, const char *str) const {
// Match all non-wildcard characters.
while (*p && *str && *p == *str && *p != '*') {
str++;
p++;
}
if (!*p) {
return *str == 0;
} else if (*p == '*') {
p++;
do {
if (wildcard_match(p, str)) {
return true;
}
} while (*str++);
} else if (*p == ' ') { // ignore whitespace in pattern
p++;
if (wildcard_match(p, str)) {
return true;
}
} else if (*str == ' ') { // ignore whitespace in string
str++;
if (wildcard_match(p, str)) {
return true;
}
}
return !*p;
}
bool wildcard_match(const std::string &p, const std::string &str) const {
return wildcard_match(p.c_str(), str.c_str());
}
// Check if a substring of str matches a pattern p.
bool wildcard_search(const std::string &p, const std::string &str) const {
return wildcard_match("*" + p + "*", str);
}
Target get_run_target() const {
return target
.without_feature(Target::NoRuntime)
.without_feature(Target::NoAsserts)
.without_feature(Target::NoBoundsQuery);
}
TestResult check_one(const std::string &op, const std::string &name, int vector_width, Expr e) {
std::ostringstream error_msg;
// Map the input calls in the Expr to loads to local
// imageparams, so that we're not sharing state across threads.
std::vector<ImageParam> image_params{
ImageParam{Float(32), 1, "in_f32"},
ImageParam{Float(64), 1, "in_f64"},
ImageParam{Float(16), 1, "in_f16"},
ImageParam{BFloat(16), 1, "in_bf16"},
ImageParam{Int(8), 1, "in_i8"},
ImageParam{UInt(8), 1, "in_u8"},
ImageParam{Int(16), 1, "in_i16"},
ImageParam{UInt(16), 1, "in_u16"},
ImageParam{Int(32), 1, "in_i32"},
ImageParam{UInt(32), 1, "in_u32"},
ImageParam{Int(64), 1, "in_i64"},
ImageParam{UInt(64), 1, "in_u64"}};
for (auto &p : image_params) {
const int alignment_bytes = image_param_alignment();
p.set_host_alignment(alignment_bytes);
const int alignment = alignment_bytes / p.type().bytes();
p.dim(0).set_min((p.dim(0).min() / alignment) * alignment);
}
const std::vector<Argument> arg_types(image_params.begin(), image_params.end());
class HookUpImageParams : public Internal::IRMutator {
using Internal::IRMutator::visit;
Expr visit(const Internal::Call *op) override {
if (op->name == "input") {
for (auto &p : image_params) {
if (p.type() == op->type) {
return p(mutate(op->args[0]));
}
}
} else if (op->call_type == Internal::Call::Halide && !op->func.weak) {
Internal::Function f(op->func);
f.mutate(this);
}
return Internal::IRMutator::visit(op);
}
const std::vector<ImageParam> &image_params;
public:
HookUpImageParams(const std::vector<ImageParam> &image_params)
: image_params(image_params) {
}
} hook_up_image_params(image_params);
e = hook_up_image_params.mutate(e);
class HasInlineReduction : public Internal::IRVisitor {
using Internal::IRVisitor::visit;
void visit(const Internal::Call *op) override {
if (op->call_type == Internal::Call::Halide) {
Internal::Function f(op->func);
if (f.has_update_definition() &&
f.update(0).schedule().rvars().size() > 0) {
inline_reduction = f;
result = true;
}
}
IRVisitor::visit(op);
}
public:
Internal::Function inline_reduction;
bool result = false;
} has_inline_reduction;
e.accept(&has_inline_reduction);
// Define a vectorized Halide::Func that uses the pattern.
Halide::Func f(name);
f(x, y) = e;
f.bound(x, 0, W).vectorize(x, vector_width);
f.compute_root();
// Include a scalar version
Halide::Func f_scalar("scalar_" + name);
f_scalar(x, y) = e;
if (has_inline_reduction.result) {
// If there's an inline reduction, we want to vectorize it
// over the RVar.
Var xo, xi;
RVar rxi;
Func g{has_inline_reduction.inline_reduction};
// Do the reduction separately in f_scalar
g.clone_in(f_scalar);
g.compute_at(f, x)
.update()
.split(x, xo, xi, vector_width)
.atomic(true)
.vectorize(g.rvars()[0])
.vectorize(xi);
}
// The output to the pipeline is the maximum absolute difference as a double.
RDom r_check(0, W, 0, H);
Halide::Func error("error_" + name);
error() = Halide::cast<double>(maximum(absd(f(r_check.x, r_check.y), f_scalar(r_check.x, r_check.y))));
compile_and_check(error, op, name, vector_width, arg_types, error_msg);
bool can_run_the_code = can_run_code();
if (can_run_the_code) {
Target run_target = get_run_target();
// Make some unallocated input buffers
std::vector<Runtime::Buffer<>> inputs(image_params.size());
std::vector<Argument> args(image_params.size());
for (size_t i = 0; i < args.size(); i++) {
args[i] = image_params[i];
inputs[i] = Runtime::Buffer<>(args[i].type, nullptr, 0);
}
auto callable = error.compile_to_callable(args, run_target);
Runtime::Buffer<double> output = Runtime::Buffer<double>::make_scalar();
output(0) = 1; // To ensure we'll fail if it's never written to
// Do the bounds query call
assert(inputs.size() == 12);
(void)callable(inputs[0], inputs[1], inputs[2], inputs[3],
inputs[4], inputs[5], inputs[6], inputs[7],
inputs[8], inputs[9], inputs[10], inputs[11],
output);
std::mt19937 rng;
rng.seed(rng_seed);
// Allocate the input buffers and fill them with noise
for (size_t i = 0; i < inputs.size(); i++) {
if (inputs[i].size_in_bytes()) {
inputs[i].allocate();
Type t = inputs[i].type();
// For floats/doubles, we only use values that aren't
// subject to rounding error that may differ between
// vectorized and non-vectorized versions
if (t == Float(32)) {
inputs[i].as<float>().for_each_value([&](float &f) { f = (rng() & 0xfff) / 8.0f - 0xff; });
} else if (t == Float(64)) {
inputs[i].as<double>().for_each_value([&](double &f) { f = (rng() & 0xfff) / 8.0 - 0xff; });
} else if (t == Float(16)) {
inputs[i].as<float16_t>().for_each_value([&](float16_t &f) { f = float16_t((rng() & 0xff) / 8.0f - 0xf); });
} else {
// Random bits is fine
for (uint32_t *ptr = (uint32_t *)inputs[i].data();
ptr != (uint32_t *)inputs[i].data() + inputs[i].size_in_bytes() / 4;
ptr++) {
// Never use the top four bits, to avoid
// signed integer overflow.
*ptr = ((uint32_t)rng()) & 0x0fffffff;
}
}
}
}
// Do the real call
(void)callable(inputs[0], inputs[1], inputs[2], inputs[3],
inputs[4], inputs[5], inputs[6], inputs[7],
inputs[8], inputs[9], inputs[10], inputs[11],
output);
double e = output(0);
// Use a very loose tolerance for floating point tests. The
// kinds of bugs we're looking for are codegen bugs that
// return the wrong value entirely, not floating point
// accuracy differences between vectors and scalars.
if (e > 0.001) {
error_msg << "The vector and scalar versions of " << name << " disagree. Maximum error: " << e << "\n";
std::string error_filename = output_directory + "error_" + name + ".s";
error.compile_to_assembly(error_filename, arg_types, target);
std::ifstream error_file;
error_file.open(error_filename);
error_msg << "Error assembly: \n";
std::string line;
while (getline(error_file, line)) {
error_msg << line << "\n";
}
error_file.close();
}
}
return {op, error_msg.str()};
}
void check(std::string op, int vector_width, Expr e) {
// Make a name for the test by uniquing then sanitizing the op name
std::string name = "op_" + op;
for (size_t i = 0; i < name.size(); i++) {
if (!isalnum(name[i])) name[i] = '_';
}
name += "_" + std::to_string(tasks.size());
// Bail out after generating the unique_name, so that names are
// unique across different processes and don't depend on filter
// settings.
if (!wildcard_match(filter, op)) return;
tasks.emplace_back(Task{op, name, vector_width, e});
}
virtual void add_tests() = 0;
virtual int image_param_alignment() {
return 16;
}
virtual bool use_multiple_threads() const {
return true;
}
virtual bool test_all() {
/* First add some tests based on the target */
add_tests();
// Remove irrelevant noise from output
const Target run_target = get_run_target();
const std::string run_target_str = run_target.to_string();
Sharder sharder;
Halide::Tools::ThreadPool<TestResult> pool(
use_multiple_threads() ?
Halide::Tools::ThreadPool<TestResult>::num_processors_online() :
1);
std::vector<std::future<TestResult>> futures;
for (size_t t = 0; t < tasks.size(); t++) {
if (!sharder.should_run(t)) continue;
const auto &task = tasks.at(t);
futures.push_back(pool.async([&]() {
return check_one(task.op, task.name, task.vector_width, task.expr);
}));
}
for (auto &f : futures) {
auto result = f.get();
constexpr int tabstop = 32;
const int spaces = std::max(1, tabstop - (int)result.op.size());
std::cout << result.op << std::string(spaces, ' ') << "(" << run_target_str << ")\n";
if (!result.error_msg.empty()) {
std::cerr << result.error_msg;
// The thread-pool destructor will block until in-progress tasks
// are done, and then will discard any tasks that haven't been
// launched yet.
return false;
}
}
return true;
}
template<typename SIMDOpCheckT>
static int main(int argc, char **argv, const std::vector<Target> &targets_to_test) {
Target host = get_host_target();
std::cout << "host is: " << host << "\n";
const int seed = argc > 2 ? atoi(argv[2]) : time(nullptr);
std::cout << "simd_op_check test seed: " << seed << "\n";
for (const auto &t : targets_to_test) {
if (!t.supported()) {
std::cout << "[SKIP] Unsupported target: " << t << "\n";
return 0;
}
SIMDOpCheckT test(t);
if (!t.supported()) {
std::cout << "Halide was compiled without support for " << t.to_string() << ". Skipping.\n";
continue;
}
if (argc > 1) {
test.filter = argv[1];
}
if (getenv("HL_SIMD_OP_CHECK_FILTER")) {
test.filter = getenv("HL_SIMD_OP_CHECK_FILTER");
}
test.set_seed(seed);
if (argc > 2) {
// Don't forget: if you want to run the standard tests to a specific output
// directory, you'll need to invoke with the first arg enclosed
// in quotes (to avoid it being wildcard-expanded by the shell):
//
// correctness_simd_op_check "*" /path/to/output
//
test.output_directory = argv[2];
}
bool success = test.test_all();
// Compile a runtime for this target, for use in the static test.
compile_standalone_runtime(test.output_directory + "simd_op_check_runtime.o", test.target);
if (!success) {
return 1;
}
}
std::cout << "Success!\n";
return 0;
}
private:
const Halide::Var x{"x"}, y{"y"};
};
} // namespace Halide
#endif // SIMD_OP_CHECK_H