Marat Dukhan | 35dacfb | 2019-11-07 19:18:16 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright 2019 Google LLC |
| 3 | # |
| 4 | # This source code is licensed under the BSD-style license found in the |
| 5 | # LICENSE file in the root directory of this source tree. |
| 6 | |
| 7 | import argparse |
| 8 | import codecs |
| 9 | import os |
| 10 | import re |
| 11 | import sys |
| 12 | import yaml |
| 13 | |
| 14 | sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) |
| 15 | from primes import next_prime |
| 16 | import xngen |
| 17 | import xnncommon |
| 18 | |
| 19 | |
| 20 | parser = argparse.ArgumentParser( |
Marat Dukhan | 660fd19 | 2020-03-10 04:55:30 -0700 | [diff] [blame] | 21 | description='IBILINEAR microkernel test generator') |
Marat Dukhan | 35dacfb | 2019-11-07 19:18:16 -0800 | [diff] [blame] | 22 | parser.add_argument("-s", "--spec", metavar="FILE", required=True, |
| 23 | help="Specification (YAML) file") |
| 24 | parser.add_argument("-o", "--output", metavar="FILE", required=True, |
| 25 | help='Output (C++ source) file') |
| 26 | parser.set_defaults(defines=list()) |
| 27 | |
| 28 | |
| 29 | def split_ukernel_name(name): |
Marat Dukhan | 660fd19 | 2020-03-10 04:55:30 -0700 | [diff] [blame] | 30 | match = re.match(r"^xnn_(f16|f32)_ibilinear_ukernel__(.+)_c(\d+)$", name) |
Marat Dukhan | 35dacfb | 2019-11-07 19:18:16 -0800 | [diff] [blame] | 31 | assert match is not None |
| 32 | channel_tile = int(match.group(3)) |
| 33 | pixel_tile = 1 |
| 34 | |
| 35 | arch, isa = xnncommon.parse_target_name(target_name=match.group(2)) |
| 36 | return channel_tile, pixel_tile, arch, isa |
| 37 | |
| 38 | |
Marat Dukhan | 660fd19 | 2020-03-10 04:55:30 -0700 | [diff] [blame] | 39 | IBILINEAR_TEST_TEMPLATE = """\ |
Marat Dukhan | 35dacfb | 2019-11-07 19:18:16 -0800 | [diff] [blame] | 40 | TEST(${TEST_NAME}, channels_eq_${CHANNEL_TILE}) { |
| 41 | $if ISA_CHECK: |
| 42 | ${ISA_CHECK}; |
Marat Dukhan | 660fd19 | 2020-03-10 04:55:30 -0700 | [diff] [blame] | 43 | IBilinearMicrokernelTester() |
Marat Dukhan | 35dacfb | 2019-11-07 19:18:16 -0800 | [diff] [blame] | 44 | .pixels(${PIXEL_TILE}) |
| 45 | .channels(${CHANNEL_TILE}) |
| 46 | .Test(${TEST_FUNC}); |
| 47 | } |
| 48 | |
| 49 | $if CHANNEL_TILE > 1: |
| 50 | TEST(${TEST_NAME}, channels_div_${CHANNEL_TILE}) { |
| 51 | $if ISA_CHECK: |
| 52 | ${ISA_CHECK}; |
| 53 | for (size_t channels = ${CHANNEL_TILE*2}; channels < ${CHANNEL_TILE*10}; channels += ${CHANNEL_TILE}) { |
Marat Dukhan | 660fd19 | 2020-03-10 04:55:30 -0700 | [diff] [blame] | 54 | IBilinearMicrokernelTester() |
Marat Dukhan | 35dacfb | 2019-11-07 19:18:16 -0800 | [diff] [blame] | 55 | .pixels(${PIXEL_TILE}) |
| 56 | .channels(channels) |
| 57 | .Test(${TEST_FUNC}); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | TEST(${TEST_NAME}, channels_lt_${CHANNEL_TILE}) { |
| 62 | $if ISA_CHECK: |
| 63 | ${ISA_CHECK}; |
| 64 | for (size_t channels = 1; channels < ${CHANNEL_TILE}; channels++) { |
Marat Dukhan | 660fd19 | 2020-03-10 04:55:30 -0700 | [diff] [blame] | 65 | IBilinearMicrokernelTester() |
Marat Dukhan | 35dacfb | 2019-11-07 19:18:16 -0800 | [diff] [blame] | 66 | .pixels(${PIXEL_TILE}) |
| 67 | .channels(channels) |
| 68 | .Test(${TEST_FUNC}); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | TEST(${TEST_NAME}, channels_gt_${CHANNEL_TILE}) { |
| 73 | $if ISA_CHECK: |
| 74 | ${ISA_CHECK}; |
| 75 | for (size_t channels = ${CHANNEL_TILE+1}; channels < ${10 if CHANNEL_TILE == 1 else CHANNEL_TILE*2}; channels++) { |
Marat Dukhan | 660fd19 | 2020-03-10 04:55:30 -0700 | [diff] [blame] | 76 | IBilinearMicrokernelTester() |
Marat Dukhan | 35dacfb | 2019-11-07 19:18:16 -0800 | [diff] [blame] | 77 | .pixels(${PIXEL_TILE}) |
| 78 | .channels(channels) |
| 79 | .Test(${TEST_FUNC}); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | $if PIXEL_TILE > 1: |
| 84 | TEST(${TEST_NAME}, pixels_div_${PIXEL_TILE}) { |
| 85 | $if ISA_CHECK: |
| 86 | ${ISA_CHECK}; |
| 87 | for (size_t pixels = ${PIXEL_TILE*2}; pixels < ${PIXEL_TILE*10}; pixels += ${PIXEL_TILE}) { |
| 88 | for (size_t channels = 1; channels <= ${CHANNEL_TILE * 5}; channels += ${max(1, CHANNEL_TILE - 1)}) { |
Marat Dukhan | 660fd19 | 2020-03-10 04:55:30 -0700 | [diff] [blame] | 89 | IBilinearMicrokernelTester() |
Marat Dukhan | 35dacfb | 2019-11-07 19:18:16 -0800 | [diff] [blame] | 90 | .pixels(pixels) |
| 91 | .channels(channels) |
| 92 | .Test(${TEST_FUNC}); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | TEST(${TEST_NAME}, pixels_lt_${PIXEL_TILE}) { |
| 98 | $if ISA_CHECK: |
| 99 | ${ISA_CHECK}; |
| 100 | for (size_t pixels = 1; pixels < ${PIXEL_TILE}; pixels++) { |
| 101 | for (size_t channels = 1; channels <= ${CHANNEL_TILE * 5}; channels += ${max(1, CHANNEL_TILE - 1)}) { |
Marat Dukhan | 660fd19 | 2020-03-10 04:55:30 -0700 | [diff] [blame] | 102 | IBilinearMicrokernelTester() |
Marat Dukhan | 35dacfb | 2019-11-07 19:18:16 -0800 | [diff] [blame] | 103 | .pixels(pixels) |
| 104 | .channels(channels) |
| 105 | .Test(${TEST_FUNC}); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | TEST(${TEST_NAME}, pixels_gt_${PIXEL_TILE}) { |
| 111 | $if ISA_CHECK: |
| 112 | ${ISA_CHECK}; |
| 113 | for (size_t pixels = ${PIXEL_TILE+1}; pixels < ${max(PIXEL_TILE*2, 3)}; pixels++) { |
| 114 | for (size_t channels = 1; channels <= ${CHANNEL_TILE * 5}; channels += ${max(1, CHANNEL_TILE - 1)}) { |
Marat Dukhan | 660fd19 | 2020-03-10 04:55:30 -0700 | [diff] [blame] | 115 | IBilinearMicrokernelTester() |
Marat Dukhan | 35dacfb | 2019-11-07 19:18:16 -0800 | [diff] [blame] | 116 | .pixels(pixels) |
| 117 | .channels(channels) |
| 118 | .Test(${TEST_FUNC}); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
Marat Dukhan | 9fab3f9 | 2019-11-08 14:55:19 -0800 | [diff] [blame] | 123 | TEST(${TEST_NAME}, input_offset) { |
| 124 | $if ISA_CHECK: |
| 125 | ${ISA_CHECK}; |
| 126 | for (size_t pixels = 1; pixels < ${PIXEL_TILE * 5}; pixels += ${max(1, PIXEL_TILE - 1)}) { |
| 127 | for (size_t channels = 1; channels <= ${CHANNEL_TILE * 5}; channels += ${max(1, CHANNEL_TILE - 1)}) { |
Marat Dukhan | 660fd19 | 2020-03-10 04:55:30 -0700 | [diff] [blame] | 128 | IBilinearMicrokernelTester() |
Marat Dukhan | 9fab3f9 | 2019-11-08 14:55:19 -0800 | [diff] [blame] | 129 | .pixels(pixels) |
| 130 | .channels(channels) |
| 131 | .input_offset(${next_prime(CHANNEL_TILE * 5 + 1)}) |
| 132 | .Test(${TEST_FUNC}); |
| 133 | } |
| 134 | } |
| 135 | } |
XNNPACK Team | 6be46b2 | 2020-10-22 23:34:54 -0700 | [diff] [blame] | 136 | |
Marat Dukhan | 35dacfb | 2019-11-07 19:18:16 -0800 | [diff] [blame] | 137 | TEST(${TEST_NAME}, output_stride) { |
| 138 | $if ISA_CHECK: |
| 139 | ${ISA_CHECK}; |
| 140 | for (size_t pixels = 1; pixels < ${PIXEL_TILE * 5}; pixels += ${max(1, PIXEL_TILE - 1)}) { |
| 141 | for (size_t channels = 1; channels <= ${CHANNEL_TILE * 5}; channels += ${max(1, CHANNEL_TILE - 1)}) { |
Marat Dukhan | 660fd19 | 2020-03-10 04:55:30 -0700 | [diff] [blame] | 142 | IBilinearMicrokernelTester() |
Marat Dukhan | 35dacfb | 2019-11-07 19:18:16 -0800 | [diff] [blame] | 143 | .pixels(pixels) |
| 144 | .channels(channels) |
| 145 | .output_stride(${next_prime(CHANNEL_TILE * 5 + 1)}) |
| 146 | .Test(${TEST_FUNC}); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | """ |
| 151 | |
| 152 | |
| 153 | def generate_test_cases(ukernel, channel_tile, pixel_tile, isa): |
| 154 | """Generates all tests cases for a BILINEAR micro-kernel. |
| 155 | |
| 156 | Args: |
| 157 | ukernel: C name of the micro-kernel function. |
| 158 | channel_tile: Number of channels processed per one iteration of the inner |
| 159 | loop of the micro-kernel. |
| 160 | pixel_tile: Number of pixels processed per one iteration of the outer loop |
| 161 | of the micro-kernel. |
| 162 | isa: instruction set required to run the micro-kernel. Generated unit test |
| 163 | will skip execution if the host processor doesn't support this ISA. |
| 164 | |
| 165 | Returns: |
| 166 | Code for the test case. |
| 167 | """ |
| 168 | _, test_name = ukernel.split("_", 1) |
| 169 | _, datatype, ukernel_type, _ = ukernel.split("_", 3) |
| 170 | test_args = [ukernel] |
Marat Dukhan | 660fd19 | 2020-03-10 04:55:30 -0700 | [diff] [blame] | 171 | return xngen.preprocess(IBILINEAR_TEST_TEMPLATE, { |
Marat Dukhan | 35dacfb | 2019-11-07 19:18:16 -0800 | [diff] [blame] | 172 | "TEST_NAME": test_name.upper().replace("UKERNEL_", ""), |
| 173 | "TEST_FUNC": ukernel, |
| 174 | "UKERNEL_TYPE": ukernel_type.upper(), |
| 175 | "DATATYPE": datatype, |
| 176 | "CHANNEL_TILE": channel_tile, |
| 177 | "PIXEL_TILE": pixel_tile, |
| 178 | "ISA_CHECK": xnncommon.generate_isa_check_macro(isa), |
| 179 | "next_prime": next_prime, |
| 180 | }) |
| 181 | |
| 182 | |
| 183 | def main(args): |
| 184 | options = parser.parse_args(args) |
| 185 | |
| 186 | with codecs.open(options.spec, "r", encoding="utf-8") as spec_file: |
| 187 | spec_yaml = yaml.safe_load(spec_file) |
| 188 | if not isinstance(spec_yaml, list): |
| 189 | raise ValueError("expected a list of micro-kernels in the spec") |
| 190 | |
| 191 | tests = """\ |
| 192 | // Copyright 2019 Google LLC |
| 193 | // |
| 194 | // This source code is licensed under the BSD-style license found in the |
| 195 | // LICENSE file in the root directory of this source tree. |
| 196 | // |
| 197 | // Auto-generated file. Do not edit! |
| 198 | // Specification: {specification} |
| 199 | // Generator: {generator} |
| 200 | |
| 201 | |
| 202 | #include <gtest/gtest.h> |
| 203 | |
| 204 | #include <xnnpack/common.h> |
| 205 | #include <xnnpack/isa-checks.h> |
| 206 | |
Marat Dukhan | 660fd19 | 2020-03-10 04:55:30 -0700 | [diff] [blame] | 207 | #include <xnnpack/ibilinear.h> |
| 208 | #include "ibilinear-microkernel-tester.h" |
Marat Dukhan | 35dacfb | 2019-11-07 19:18:16 -0800 | [diff] [blame] | 209 | """.format(specification=options.spec, generator=sys.argv[0]) |
| 210 | |
| 211 | for ukernel_spec in spec_yaml: |
| 212 | name = ukernel_spec["name"] |
| 213 | channel_tile, pixel_tile, arch, isa = split_ukernel_name(name) |
| 214 | |
| 215 | # specification can override architecture |
| 216 | arch = ukernel_spec.get("arch", arch) |
| 217 | |
| 218 | test_case = generate_test_cases(name, channel_tile, pixel_tile, isa) |
| 219 | tests += "\n\n" + xnncommon.postprocess_test_case(test_case, arch, isa) |
| 220 | |
| 221 | with codecs.open(options.output, "w", encoding="utf-8") as output_file: |
| 222 | output_file.write(tests) |
| 223 | |
| 224 | |
| 225 | if __name__ == "__main__": |
| 226 | main(sys.argv[1:]) |