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
|
#include "Halide.h"
#include <iostream>
#include <stdio.h>
using namespace Halide;
using namespace Halide::ConciseCasts;
using namespace Halide::Internal;
int main(int arch, char **argv) {
Halide::Target target = get_jit_target_from_environment();
if (target.has_feature(Target::Vulkan) && ((target.os == Target::IOS) || target.os == Target::OSX)) {
printf("[SKIP] Skipping test for Vulkan on iOS/OSX (MoltenVK fails to convert max/min intrinsics correctly)!\n");
return 0;
}
const int W = 256, H = 256;
Buffer<uint8_t> in(W, H);
// Set up the input.
for (int y = 0; y < H; y++) {
for (int x = 0; x < W; x++) {
in(x, y) = rand() & 0xff;
}
}
// Define a convolution kernel, and its sum.
Buffer<int8_t> kernel(3, 3);
kernel.set_min(-1, -1);
for (int y = -1; y <= 1; y++) {
for (int x = -1; x <= 1; x++) {
kernel(x, y) = rand() % 8 - 4;
}
}
Var x("x"), y("y"), xi("xi"), yi("yi");
RDom r(-1, 3, -1, 3);
// Boundary condition.
Func input = BoundaryConditions::repeat_edge(in);
input.compute_root();
// Test a widening reduction, followed by a narrowing.
{
Func f;
f(x, y) = u8_sat(sum(i16(input(x + r.x, y + r.y)) * kernel(r.x, r.y)) / 16);
// Schedule.
if (target.has_gpu_feature()) {
f.gpu_tile(x, y, xi, yi, 16, 16);
} else if (target.has_feature(Target::HVX)) {
f.hexagon().vectorize(x, 128);
} else {
f.vectorize(x, target.natural_vector_size<uint8_t>());
}
// Run the pipeline and verify the results are correct.
Buffer<uint8_t> out = f.realize({W, H}, target);
for (int y = 1; y < H - 1; y++) {
for (int x = 1; x < W - 1; x++) {
int16_t correct = 0;
for (int ry = -1; ry <= 1; ry++) {
for (int rx = -1; rx <= 1; rx++) {
correct += static_cast<int16_t>(in(x + rx, y + ry)) * kernel(rx, ry);
}
}
correct = std::min(std::max(correct / 16, 0), 255);
if (correct != out(x, y)) {
std::cout << "out(" << x << ", " << y << ") = " << (int)out(x, y) << " instead of " << correct << "\n";
return 1;
}
}
}
}
// Test a tuple reduction with widening, followed by narrowing the result.
{
Func f;
f(x, y) = {i16(0), i8(0)};
f(x, y) = {
f(x, y)[0] + i16(input(x + r.x, y + r.y)) * kernel(r.x, r.y),
f(x, y)[1] + kernel(r.x, r.y),
};
Func g;
g(x, y) = u8_sat((f(x, y)[0] + f(x, y)[1]) / 16);
// Schedule.
if (target.has_gpu_feature()) {
g.gpu_tile(x, y, xi, yi, 16, 16);
} else if (target.has_feature(Target::HVX)) {
g.hexagon().vectorize(x, 128);
} else {
g.vectorize(x, target.natural_vector_size<uint8_t>());
}
// Run the pipeline and verify the results are correct.
Buffer<uint8_t> out = g.realize({W, H}, target);
for (int y = 1; y < H - 1; y++) {
for (int x = 1; x < W - 1; x++) {
int16_t correct = 0;
for (int ry = -1; ry <= 1; ry++) {
for (int rx = -1; rx <= 1; rx++) {
correct += static_cast<int16_t>(in(x + rx, y + ry)) * kernel(rx, ry);
correct += kernel(rx, ry);
}
}
correct = std::min(std::max(correct / 16, 0), 255);
if (correct != out(x, y)) {
std::cout << "out(" << x << ", " << y << ") = " << (int)out(x, y) << " instead of " << correct << "\n";
return 1;
}
}
}
}
// Test a widening, followed by a narrowing reduction with an
// unaligned output. This triggered a bug in EliminateInterleaves
// on Hexagon.
{
Func f;
f(x, y) = i16(input(x, y));
Func g;
g(x, y) = u8_sat((f(x, y) + f(x + 1, y)) / 2);
// Schedule.
if (target.has_gpu_feature()) {
g.gpu_tile(x, y, xi, yi, 16, 16);
} else if (target.has_feature(Target::HVX)) {
g.hexagon().vectorize(x, 128);
f.compute_at(g, y).vectorize(x, 128, TailStrategy::RoundUp);
} else {
g.vectorize(x, target.natural_vector_size<uint8_t>());
}
g.output_buffer().dim(0).set_min(0).set_extent(W - 2);
g.output_buffer().dim(1).set_min(0).set_extent(H);
// Run the pipeline and verify the results are correct.
Buffer<uint8_t> out = g.realize({W - 2, H}, target);
for (int y = 1; y < H - 1; y++) {
for (int x = 0; x < W - 3; x++) {
uint8_t correct = (static_cast<int16_t>(in(x, y)) + in(x + 1, y)) / 2;
if (correct != out(x, y)) {
std::cout << "out(" << x << ", " << y << ") = " << (int)out(x, y) << " instead of " << (int)correct << "\n";
return 1;
}
}
}
}
std::cout << "Success!\n";
return 0;
}
|