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
|
#include "Halide.h"
#include "halide_benchmark.h"
using namespace Halide;
int main(int argc, char **argv) {
Target target = get_jit_target_from_environment();
if (target.arch == Target::WebAssembly) {
printf("[SKIP] Performance tests are meaningless and/or misleading under WebAssembly interpreter.\n");
return 0;
}
// 8-bit mat-mul into 32-bit accumulator
{
double times[2];
for (int use_nested_vectorization = 0; use_nested_vectorization < 2; use_nested_vectorization++) {
Var x, y;
ImageParam f(UInt(8), 2), g(UInt(8), 2);
RDom r(0, 128);
Func prod;
prod(x, y) += cast<int32_t>(f(x, r)) * g(r, y);
Var xi, yi, xo, yo;
Var bx, tx, by, ty;
RVar ro, ri, rio, rii;
const int vec = target.natural_vector_size<uint8_t>();
if (use_nested_vectorization) {
if (target.arch == Target::X86) {
// x86 schedule. Exploits the ability of pmaddwd
// to pull one arg from memory. Because we'll be
// intentionally spilling, the tile will be
// absurdly large for a gemm.
prod.in()
.tile(x, y, xi, yi, vec, vec / 2)
.vectorize(xi)
.unroll(yi);
f.in().compute_at(prod, ro).vectorize(_0).unroll(_1);
g.in().compute_at(prod, y).vectorize(_0).unroll(_1);
prod.compute_at(prod.in(), x)
.vectorize(x)
.unroll(y)
.update()
.split(r, ro, ri, vec / 2)
.reorder(ri, x, y, ro)
.vectorize(x)
.unroll(y)
.atomic()
.vectorize(ri, 2)
.unroll(ri);
} else {
// ARM schedule. Exploits SDOT when available.
const int reduce = target.has_feature(Target::ARMDotProd) ? 4 : 2;
prod.in()
.tile(x, y, xi, yi, vec, 4)
.vectorize(xi)
.unroll(yi);
f.in().compute_at(prod, ro).vectorize(_0).unroll(_1);
g.in().compute_at(prod, y).vectorize(_0).unroll(_1);
prod.compute_at(prod.in(), x)
.vectorize(x)
.unroll(y)
.update()
.split(r, ro, ri, reduce)
.reorder(ri, x, y, ro)
.vectorize(x)
.unroll(y)
.atomic()
.vectorize(ri, reduce)
.unroll(ri);
}
} else {
prod.in()
.tile(x, y, xi, yi, vec, 4, TailStrategy::RoundUp)
.vectorize(xi)
.unroll(yi);
prod.compute_at(prod.in(), x)
.vectorize(x)
.unroll(y)
.update()
.reorder(x, y, r)
.vectorize(x)
.unroll(y);
}
Buffer<uint8_t> f_buf(1024, 1024);
f_buf.fill(100);
Buffer<uint8_t> g_buf(1024, 1024);
f_buf.fill(100);
f.set(f_buf);
g.set(g_buf);
Buffer<int32_t> out(1024, 1024);
Func result = prod.in();
// Uncomment to check the asm
// result.compile_to_assembly("/dev/stdout", {f, g}, target.with_feature(Target::NoAsserts).with_feature(Target::NoBoundsQuery));
times[use_nested_vectorization] =
Tools::benchmark(20, 20, [&]() {
result.realize(out, target);
out.device_sync();
});
}
double speed_up = times[0] / times[1];
printf("8-bit gemm\n"
"Time with nested vectorization: %0.2f ms \n"
"Time without: %0.2f ms \n"
"Speed-up: %0.2fx\n",
times[1] * 1000,
times[0] * 1000,
speed_up);
if (speed_up < 0.5) {
printf("The nested vectorization schedule was supposed to be faster!\n");
return 1;
}
}
// 8-bit blur into 32-bit accumulator
{
double times[2];
for (int use_nested_vectorization = 0; use_nested_vectorization < 2; use_nested_vectorization++) {
Var x, y;
ImageParam f(UInt(8), 1), g(UInt(8), 1);
RDom r(0, 128);
Func prod;
prod(x) += cast<int32_t>(f(x + r)) * g(r);
Func result;
result(x) = cast<uint8_t>(prod(x) >> 24);
RVar ro, ri;
f.in().compute_at(prod, ro).vectorize(_0).bound_extent(_0, 16);
g.in().compute_at(prod, ro).vectorize(_0);
result
.vectorize(x, 8, TailStrategy::RoundUp);
if (use_nested_vectorization) {
int reduce;
if (target.arch == Target::X86) {
reduce = 8;
} else if (target.has_feature(Target::ARMDotProd)) {
reduce = 4;
} else {
reduce = 2;
}
prod.compute_at(result, x)
.vectorize(x)
.update()
.split(r, ro, ri, 8)
.reorder(ri, x, ro)
.vectorize(x)
.atomic()
.vectorize(ri, reduce)
.unroll(ri);
} else {
prod.compute_at(result, x)
.vectorize(x)
.update()
.split(r, ro, ri, 8)
.reorder(ri, x, ro)
.vectorize(x)
.unroll(ri);
}
Buffer<uint8_t> f_buf(1024 * 1024);
f_buf.fill(100);
Buffer<uint8_t> g_buf(128);
f_buf.fill(100);
f.set(f_buf);
g.set(g_buf);
Buffer<uint8_t> out(f_buf.width() - g_buf.width() - 128);
// Uncomment to check the asm
// result.compile_to_assembly("/dev/stdout", {f, g}, target);
times[use_nested_vectorization] =
Tools::benchmark(10, 10, [&]() {
result.realize(out, target);
out.device_sync();
});
}
double speed_up = times[0] / times[1];
printf("8-bit blur\n"
"Time with nested vectorization: %0.2f ms \n"
"Time without: %0.2f ms \n"
"Speed-up: %0.2fx\n",
times[1] * 1000,
times[0] * 1000,
speed_up);
if (speed_up < 0.5) {
printf("The nested vectorization schedule was supposed to be faster!\n");
return 1;
}
}
// 16-bit blur into 32-bit accumulator, with reduction over
// adjacent vector lanes at the same time as reduction over slices
// of the vector. This is only a win on platforms with a pmaddwd-like instruction.
if (target.arch == Target::X86) {
double times[2];
for (int use_nested_vectorization = 0; use_nested_vectorization < 2; use_nested_vectorization++) {
Var x, y;
ImageParam f(Int(16), 1), g(Int(16), 1);
RDom r(0, 128);
Func prod;
prod(x) += cast<int32_t>(f(x + r)) * g(r);
Func result;
result(x) = cast<int16_t>(prod(x) >> 16);
RVar ro, ri, rio, rii;
result
.vectorize(x, 16, TailStrategy::RoundUp);
if (use_nested_vectorization) {
f.in().compute_at(prod, ro).vectorize(_0).bound_extent(_0, 32);
// It's faster to compute this at rio and unroll rio,
// but that's not what we're testing.
g.in().compute_at(prod, ro).vectorize(_0);
prod.compute_at(result, x)
.vectorize(x)
.update()
.split(r, ro, ri, 4)
.split(ri, rio, rii, 2)
.reorder(rii, x, rio, ro)
.vectorize(x)
.atomic()
.vectorize(rio)
.vectorize(rii);
} else {
prod.compute_at(result, x)
.vectorize(x)
.update()
.split(r, ro, ri, 4)
.reorder(ri, x, ro)
.vectorize(x)
.unroll(ri);
}
Buffer<int16_t> f_buf(1024 * 1024);
f_buf.fill(100);
Buffer<int16_t> g_buf(128);
f_buf.fill(100);
f.set(f_buf);
g.set(g_buf);
Buffer<int16_t> out(f_buf.width() - g_buf.width() - 128);
// Uncomment to check the asm
// result.compile_to_assembly("/dev/stdout", {f, g}, target);
times[use_nested_vectorization] =
Tools::benchmark(10, 10, [&]() {
result.realize(out, target);
out.device_sync();
});
}
double speed_up = times[0] / times[1];
printf("16-bit blur with reduction dimension outermost vector dim\n"
"Time with nested vectorization: %0.2f ms \n"
"Time without: %0.2f ms \n"
"Speed-up: %0.2fx\n",
times[1] * 1000,
times[0] * 1000,
speed_up);
if (speed_up < 0.5) {
printf("The nested vectorization schedule was supposed to be faster!\n");
return 1;
}
}
printf("Success!\n");
// 8-bit sparse blur into 32-bit accumulator
{
double times[2];
for (int use_nested_vectorization = 0; use_nested_vectorization < 2; use_nested_vectorization++) {
Var x, y;
ImageParam f(UInt(8), 1), g(UInt(8), 1);
// 128 filter taps at unknown locations, which we will
// promise are bounded.
ImageParam taps(Int(32), 1);
RDom r(0, 128);
Func prod;
prod(x) += cast<uint32_t>(f(x + unsafe_promise_clamped(taps(r), 0, 127))) * g(r);
Func result;
result(x) = prod(x);
RVar ro, ri;
g.in().compute_at(prod, ro).vectorize(_0);
result
.vectorize(x, 8, TailStrategy::RoundUp);
if (use_nested_vectorization) {
int reduce;
if (target.has_feature(Target::ARMDotProd)) {
reduce = 4;
} else {
reduce = 2;
}
prod.compute_at(result, x)
.vectorize(x)
.update()
.split(r, ro, ri, 16)
.reorder(ri, x, ro)
.vectorize(x)
.atomic()
.vectorize(ri, reduce)
.unroll(ri);
} else {
prod.compute_at(result, x)
.vectorize(x)
.update()
.split(r, ro, ri, 16)
.reorder(ri, x, ro)
.vectorize(x);
}
Buffer<uint8_t> f_buf(1024 * 1024);
f_buf.fill(100);
Buffer<uint8_t> g_buf(128);
f_buf.fill(100);
f.set(f_buf);
g.set(g_buf);
Buffer<int> taps_buf(128);
for (int i = 0; i < 128; i++) {
taps_buf(i) = (i * i) & 127;
}
taps.set(taps_buf);
Buffer<uint32_t> out(f_buf.width() - g_buf.width() - 128);
// Uncomment to check the asm
// result.compile_to_assembly("/dev/stdout", {f, g, taps}, target);
times[use_nested_vectorization] =
Tools::benchmark(10, 10, [&]() {
result.realize(out, target);
out.device_sync();
});
}
// We don't actually get any win from this on X86, as the
// basic version also manages to use pmaddwd well.
double speed_up = times[0] / times[1];
printf("8-bit sparse blur\n"
"Time with nested vectorization: %0.2f ms \n"
"Time without: %0.2f ms \n"
"Speed-up: %0.2fx\n",
times[1] * 1000,
times[0] * 1000,
speed_up);
if (speed_up < 0.5) {
printf("The nested vectorization schedule was supposed to be faster!\n");
return 1;
}
}
return 0;
}
|