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
|
#include "Halide.h"
#include "test_sharding.h"
#include <algorithm>
#include <cstdio>
using namespace Halide;
using namespace Halide::BoundaryConditions;
Var x("x"), y("y"), xo("xo"), yo("yo"), xi("xi"), yi("yi");
template<typename T>
bool expect_eq(T actual, T expected) {
if (expected != actual) {
fprintf(stderr, "Failed: expected %d, actual %d\n", (int)expected, (int)actual);
return false;
}
return true;
}
void schedule_test(Func f, int vector_width, Partition partition_policy, const Target &t) {
if (vector_width != 1) {
f.vectorize(x, vector_width);
}
f.partition(x, partition_policy);
f.partition(y, partition_policy);
if (t.has_gpu_feature()) {
f.gpu_tile(x, y, xo, yo, xi, yi, 2, 2);
} else if (t.has_feature(Target::HVX)) {
// TODO: Non-native vector widths hang the compiler here.
// f.hexagon();
}
}
template<typename T>
bool check_constant_exterior(const Buffer<T> &input, T exterior, Func f,
int test_min_x, int test_extent_x, int test_min_y, int test_extent_y,
int vector_width, Partition partition_policy,
Target t) {
bool success = true;
Buffer<T> result(test_extent_x, test_extent_y);
result.set_min(test_min_x, test_min_y);
f = lambda(x, y, f(x, y));
schedule_test(f, vector_width, partition_policy, t);
f.realize(result, t);
result.copy_to_host();
for (int32_t y = test_min_y; y < test_min_y + test_extent_y; y++) {
for (int32_t x = test_min_x; x < test_min_x + test_extent_x; x++) {
if (x < 0 || y < 0 || x >= input.width() || y >= input.height()) {
success &= expect_eq(result(x, y), exterior);
} else {
success &= expect_eq(result(x, y), input(x, y));
}
}
}
return success;
}
template<typename T>
bool check_repeat_edge(const Buffer<T> &input, Func f,
int test_min_x, int test_extent_x, int test_min_y, int test_extent_y,
int vector_width, Partition partition_policy,
Target t) {
bool success = true;
Buffer<T> result(test_extent_x, test_extent_y);
result.set_min(test_min_x, test_min_y);
f = lambda(x, y, f(x, y));
schedule_test(f, vector_width, partition_policy, t);
f.realize(result, t);
result.copy_to_host();
for (int32_t y = test_min_y; y < test_min_y + test_extent_y; y++) {
for (int32_t x = test_min_x; x < test_min_x + test_extent_x; x++) {
int32_t clamped_y = std::min(input.height() - 1, std::max(0, y));
int32_t clamped_x = std::min(input.width() - 1, std::max(0, x));
success &= expect_eq(result(x, y), input(clamped_x, clamped_y));
}
}
return success;
}
template<typename T>
bool check_repeat_image(const Buffer<T> &input, Func f,
int test_min_x, int test_extent_x, int test_min_y, int test_extent_y,
int vector_width, Partition partition_policy,
Target t) {
bool success = true;
Buffer<T> result(test_extent_x, test_extent_y);
result.set_min(test_min_x, test_min_y);
f = lambda(x, y, f(x, y));
schedule_test(f, vector_width, partition_policy, t);
f.realize(result, t);
result.copy_to_host();
for (int32_t y = test_min_y; y < test_min_y + test_extent_y; y++) {
for (int32_t x = test_min_x; x < test_min_x + test_extent_x; x++) {
int32_t mapped_x = x;
int32_t mapped_y = y;
while (mapped_x < 0)
mapped_x += input.width();
while (mapped_x > input.width() - 1)
mapped_x -= input.width();
while (mapped_y < 0)
mapped_y += input.height();
while (mapped_y > input.height() - 1)
mapped_y -= input.height();
success &= expect_eq(result(x, y), input(mapped_x, mapped_y));
}
}
return success;
}
template<typename T>
bool check_mirror_image(const Buffer<T> &input, Func f,
int test_min_x, int test_extent_x, int test_min_y, int test_extent_y,
int vector_width, Partition partition_policy,
Target t) {
bool success = true;
Buffer<T> result(test_extent_x, test_extent_y);
result.set_min(test_min_x, test_min_y);
f = lambda(x, y, f(x, y));
schedule_test(f, vector_width, partition_policy, t);
f.realize(result, t);
result.copy_to_host();
for (int32_t y = test_min_y; y < test_min_y + test_extent_y; y++) {
for (int32_t x = test_min_x; x < test_min_x + test_extent_x; x++) {
int32_t mapped_x = (x < 0) ? -(x + 1) : x;
mapped_x = mapped_x % (2 * input.width());
if (mapped_x > (input.width() - 1)) {
mapped_x = (2 * input.width() - 1) - mapped_x;
}
int32_t mapped_y = (y < 0) ? -(y + 1) : y;
mapped_y = mapped_y % (2 * input.height());
if (mapped_y > (input.height() - 1)) {
mapped_y = (2 * input.height() - 1) - mapped_y;
}
success &= expect_eq(result(x, y), input(mapped_x, mapped_y));
}
}
return success;
}
template<typename T>
bool check_mirror_interior(const Buffer<T> &input, Func f,
int test_min_x, int test_extent_x, int test_min_y, int test_extent_y,
int vector_width, Partition partition_policy,
Target t) {
bool success = true;
Buffer<T> result(test_extent_x, test_extent_y);
result.set_min(test_min_x, test_min_y);
f = lambda(x, y, f(x, y));
schedule_test(f, vector_width, partition_policy, t);
f.realize(result, t);
result.copy_to_host();
for (int32_t y = test_min_y; y < test_min_y + test_extent_y; y++) {
for (int32_t x = test_min_x; x < test_min_x + test_extent_x; x++) {
int32_t mapped_x = abs(x) % (input.width() * 2 - 2);
if (mapped_x > input.width() - 1) {
mapped_x = input.width() * 2 - 2 - mapped_x;
}
int32_t mapped_y = abs(y) % (input.height() * 2 - 2);
if (mapped_y > input.height() - 1) {
mapped_y = input.height() * 2 - 2 - mapped_y;
}
success &= expect_eq(result(x, y), input(mapped_x, mapped_y));
}
}
return success;
}
struct Task {
std::function<bool()> fn;
};
void add_all(int vector_width, Partition partition_policy, Target t, std::vector<Task> &tasks) {
const int W = 32;
const int H = 32;
Buffer<uint8_t> input(W, H);
for (int32_t y = 0; y < H; y++) {
for (int32_t x = 0; x < W; x++) {
input(x, y) = x + y * W;
}
}
Func input_f("input_f");
input_f(x, y) = input(x, y);
// repeat_edge:
{
const int32_t test_min = -25;
const int32_t test_extent = 100;
// Func input.
tasks.push_back({[=]() { return check_repeat_edge(
input,
repeat_edge(input_f, {{0, W}, {0, H}}),
test_min, test_extent, test_min, test_extent,
vector_width, partition_policy, t); }});
// Image input.
tasks.push_back({[=]() { return check_repeat_edge(
input,
repeat_edge(input, {{0, W}, {0, H}}),
test_min, test_extent, test_min, test_extent,
vector_width, partition_policy, t); }});
// Undefined bounds.
tasks.push_back({[=]() { return check_repeat_edge(
input,
repeat_edge(input, {{Expr(), Expr()}, {0, H}}),
0, W, test_min, test_extent,
vector_width, partition_policy, t); }});
tasks.push_back({[=]() { return check_repeat_edge(
input,
repeat_edge(input, {{0, W}, {Expr(), Expr()}}),
test_min, test_extent, 0, H,
vector_width, partition_policy, t); }});
// Implicitly determined bounds.
tasks.push_back({[=]() { return check_repeat_edge(
input,
repeat_edge(input),
test_min, test_extent, test_min, test_extent,
vector_width, partition_policy, t); }});
}
// constant_exterior:
{
const int32_t test_min = -25;
const int32_t test_extent = 100;
const uint8_t exterior = 42;
// Func input.
tasks.push_back({[=]() { return check_constant_exterior(
input, exterior,
constant_exterior(input_f, exterior, {{0, W}, {0, H}}),
test_min, test_extent, test_min, test_extent,
vector_width, partition_policy, t); }});
// Image input.
tasks.push_back({[=]() { return check_constant_exterior(
input, exterior,
constant_exterior(input, exterior, {{0, W}, {0, H}}),
test_min, test_extent, test_min, test_extent,
vector_width, partition_policy, t); }});
// Undefined bounds.
tasks.push_back({[=]() { return check_constant_exterior(
input, exterior,
constant_exterior(input, exterior, {{Expr(), Expr()}, {0, H}}),
0, W, test_min, test_extent,
vector_width, partition_policy, t); }});
tasks.push_back({[=]() { return check_constant_exterior(
input, exterior,
constant_exterior(input, exterior, {{0, W}, {Expr(), Expr()}}),
test_min, test_extent, 0, H,
vector_width, partition_policy, t); }});
// Implicitly determined bounds.
tasks.push_back({[=]() { return check_constant_exterior(
input, exterior,
constant_exterior(input, exterior),
test_min, test_extent, test_min, test_extent,
vector_width, partition_policy, t); }});
}
// repeat_image:
{
const int32_t test_min = -25;
const int32_t test_extent = 100;
// Func input.
tasks.push_back({[=]() { return check_repeat_image(
input,
repeat_image(input_f, {{0, W}, {0, H}}),
test_min, test_extent, test_min, test_extent,
vector_width, partition_policy, t); }});
// Image input.
tasks.push_back({[=]() { return check_repeat_image(
input,
repeat_image(input, {{0, W}, {0, H}}),
test_min, test_extent, test_min, test_extent,
vector_width, partition_policy, t); }});
// Undefined bounds.
tasks.push_back({[=]() { return check_repeat_image(
input,
repeat_image(input, {{Expr(), Expr()}, {0, H}}),
0, W, test_min, test_extent,
vector_width, partition_policy, t); }});
tasks.push_back({[=]() { return check_repeat_image(
input,
repeat_image(input, {{0, W}, {Expr(), Expr()}}),
test_min, test_extent, 0, H,
vector_width, partition_policy, t); }});
// Implicitly determined bounds.
tasks.push_back({[=]() { return check_repeat_image(
input,
repeat_image(input),
test_min, test_extent, test_min, test_extent,
vector_width, partition_policy, t); }});
}
// mirror_image:
{
const int32_t test_min = -25;
const int32_t test_extent = 100;
// Func input.
tasks.push_back({[=]() { return check_mirror_image(
input,
mirror_image(input_f, {{0, W}, {0, H}}),
test_min, test_extent, test_min, test_extent,
vector_width, partition_policy, t); }});
// Image input.
tasks.push_back({[=]() { return check_mirror_image(
input,
mirror_image(input, {{0, W}, {0, H}}),
test_min, test_extent, test_min, test_extent,
vector_width, partition_policy, t); }});
// Undefined bounds.
tasks.push_back({[=]() { return check_mirror_image(
input,
mirror_image(input, {{Expr(), Expr()}, {0, H}}),
0, W, test_min, test_extent,
vector_width, partition_policy, t); }});
tasks.push_back({[=]() { return check_mirror_image(
input,
mirror_image(input, {{0, W}, {Expr(), Expr()}}),
test_min, test_extent, 0, H,
vector_width, partition_policy, t); }});
// Implicitly determined bounds.
tasks.push_back({[=]() { return check_mirror_image(
input,
mirror_image(input),
test_min, test_extent, test_min, test_extent,
vector_width, partition_policy, t); }});
}
// mirror_interior:
{
const int32_t test_min = -25;
const int32_t test_extent = 100;
// Func input.
tasks.push_back({[=]() { return check_mirror_interior(
input,
mirror_interior(input_f, {{0, W}, {0, H}}),
test_min, test_extent, test_min, test_extent,
vector_width, partition_policy, t); }});
// Image input.
tasks.push_back({[=]() { return check_mirror_interior(
input,
mirror_interior(input, {{0, W}, {0, H}}),
test_min, test_extent, test_min, test_extent,
vector_width, partition_policy, t); }});
// Undefined bounds.
tasks.push_back({[=]() { return check_mirror_interior(
input,
mirror_interior(input, {{Expr(), Expr()}, {0, H}}),
0, W, test_min, test_extent,
vector_width, partition_policy, t); }});
tasks.push_back({[=]() { return check_mirror_interior(
input,
mirror_interior(input, {{0, W}, {Expr(), Expr()}}),
test_min, test_extent, 0, H,
vector_width, partition_policy, t); }});
// Implicitly determined bounds.
tasks.push_back({[=]() { return check_mirror_interior(
input,
mirror_interior(input),
test_min, test_extent, test_min, test_extent,
vector_width, partition_policy, t); }});
}
}
int main(int argc, char **argv) {
Target target = get_jit_target_from_environment();
int vector_width_max = 32;
if (target.has_feature(Target::Metal) ||
target.has_feature(Target::Vulkan) ||
target.has_feature(Target::D3D12Compute) ||
target.has_feature(Target::WebGPU)) {
// https://github.com/halide/Halide/issues/2148
vector_width_max = 4;
}
if (target.has_feature(Target::OpenCL)) {
vector_width_max = 16;
}
if (target.arch == Target::WebAssembly) {
// The wasm jit is very slow, so shorten this test here.
vector_width_max = 8;
}
std::vector<Task> tasks;
for (int vector_width = 1; vector_width <= vector_width_max; vector_width *= 2) {
add_all(vector_width, Partition::Auto, target, tasks);
add_all(vector_width, Partition::Never, target, tasks);
}
using Sharder = Halide::Internal::Test::Sharder;
Sharder sharder;
for (size_t t = 0; t < tasks.size(); t++) {
if (!sharder.should_run(t)) continue;
const auto &task = tasks.at(t);
if (!task.fn()) {
exit(1);
}
}
printf("Success!\n");
return 0;
}
|