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
|
import halide as hl
test_exterior = 42
test_min = -25
test_extent = 100
x, y = hl.vars("x y")
def expect_eq(actual, expected):
assert expected == actual, f"Failed: expected {expected}, actual {actual}"
def schedule_test(f, vector_width, target, partition_policy):
if vector_width != 1:
f.vectorize(x, vector_width)
f.partition(x, partition_policy)
f.partition(y, partition_policy)
if target.has_gpu_feature() and vector_width <= 16:
xo, yo, xi, yi = hl.vars("xo yo xi yi")
f.gpu_tile(x, y, xo, yo, xi, yi, 2, 2)
def realize_and_check(
f,
checker,
input,
test_min_x,
test_extent_x,
test_min_y,
test_extent_y,
vector_width,
target,
partition_policy,
):
result = hl.Buffer(hl.UInt(8), [test_extent_x, test_extent_y])
result.set_min([test_min_x, test_min_y])
f2 = hl.lambda_func(x, y, f[x, y])
schedule_test(f2, vector_width, target, partition_policy)
f2.realize(result, target)
result.copy_to_host()
for r in range(test_min_y, test_min_y + test_extent_y):
for c in range(test_min_x, test_min_x + test_extent_x):
checker(input, result, c, r)
def check_constant_exterior(input, result, c, r):
if c < 0 or r < 0 or c >= input.width() or r >= input.height():
expect_eq(result[c, r], test_exterior)
else:
expect_eq(result[c, r], input[c, r])
def check_repeat_edge(input, result, c, r):
clamped_y = min(input.height() - 1, max(0, r))
clamped_x = min(input.width() - 1, max(0, c))
expect_eq(result[c, r], input[clamped_x, clamped_y])
def check_repeat_image(input, result, c, r):
mapped_x = c
mapped_y = r
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()
expect_eq(result[c, r], input[mapped_x, mapped_y])
def check_mirror_image(input, result, c, r):
mapped_x = -(c + 1) if c < 0 else c
mapped_x = mapped_x % (2 * input.width())
if mapped_x > (input.width() - 1):
mapped_x = (2 * input.width() - 1) - mapped_x
mapped_y = -(r + 1) if r < 0 else r
mapped_y = mapped_y % (2 * input.height())
if mapped_y > (input.height() - 1):
mapped_y = (2 * input.height() - 1) - mapped_y
expect_eq(result[c, r], input[mapped_x, mapped_y])
def check_mirror_interior(input, result, c, r):
mapped_x = abs(c) % (input.width() * 2 - 2)
if mapped_x > input.width() - 1:
mapped_x = input.width() * 2 - 2 - mapped_x
mapped_y = abs(r) % (input.height() * 2 - 2)
if mapped_y > input.height() - 1:
mapped_y = input.height() * 2 - 2 - mapped_y
expect_eq(result[c, r], input[mapped_x, mapped_y])
def test_all(vector_width, target, partition_policy):
# print("target is %s, partition_policy is %s " % (str(target), str(partition_policy)))
W = 32
H = 32
input = hl.Buffer(hl.UInt(8), [W, H])
for r in range(H):
for c in range(W):
input[c, r] = (c + r * W) & 0xFF
input_f = hl.Func()
input_f[x, y] = input[x, y]
tests = [
(hl.BoundaryConditions.constant_exterior, check_constant_exterior),
(hl.BoundaryConditions.repeat_edge, check_repeat_edge),
(hl.BoundaryConditions.repeat_image, check_repeat_image),
(hl.BoundaryConditions.mirror_image, check_mirror_image),
(hl.BoundaryConditions.mirror_interior, check_mirror_interior),
]
for bc, checker in tests:
# print(' Testing %s:%d...' % (bc.__name__, vector_width))
func_input_args = {"f": input_f, "bounds": [(0, W), (0, H)]}
image_input_args = {"f": input, "bounds": [(0, W), (0, H)]}
undef_min_args = {"f": input, "bounds": [(hl.Expr(), hl.Expr()), (0, H)]}
undef_max_args = {"f": input, "bounds": [(0, W), (hl.Expr(), hl.Expr())]}
implicit_bounds_args = {"f": input}
if bc == hl.BoundaryConditions.constant_exterior:
func_input_args["exterior"] = test_exterior
image_input_args["exterior"] = test_exterior
undef_min_args["exterior"] = test_exterior
undef_max_args["exterior"] = test_exterior
implicit_bounds_args["exterior"] = test_exterior
realize_and_check(
bc(**func_input_args),
checker,
input,
test_min,
test_extent,
test_min,
test_extent,
vector_width,
target,
partition_policy,
)
realize_and_check(
bc(**image_input_args),
checker,
input,
test_min,
test_extent,
test_min,
test_extent,
vector_width,
target,
partition_policy,
)
realize_and_check(
bc(**undef_min_args),
checker,
input,
0,
W,
test_min,
test_extent,
vector_width,
target,
partition_policy,
)
realize_and_check(
bc(**undef_max_args),
checker,
input,
test_min,
test_extent,
0,
H,
vector_width,
target,
partition_policy,
)
realize_and_check(
bc(**implicit_bounds_args),
checker,
input,
test_min,
test_extent,
test_min,
test_extent,
vector_width,
target,
partition_policy,
)
if __name__ == "__main__":
target = hl.get_jit_target_from_environment()
vector_width_power_max = 6
# https://github.com/halide/Halide/issues/2148
if (
target.has_feature(hl.TargetFeature.Metal)
or target.has_feature(hl.TargetFeature.Vulkan)
or target.has_feature(hl.TargetFeature.D3D12Compute)
):
vector_width_power_max = 2
for i in range(0, vector_width_power_max):
vector_width = 1 << i
test_all(vector_width, target, hl.Partition.Auto)
test_all(vector_width, target, hl.Partition.Never)
|