Marat Dukhan | 346a9e5 | 2019-11-15 09:06:30 -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 math |
| 10 | import os |
| 11 | import re |
| 12 | import sys |
| 13 | import yaml |
| 14 | |
| 15 | sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) |
| 16 | import xngen |
| 17 | import xnncommon |
| 18 | |
| 19 | |
| 20 | parser = argparse.ArgumentParser( |
| 21 | description='Vector unary operation microkernel test generator') |
| 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 | e79acb7 | 2021-08-16 19:03:53 -0700 | [diff] [blame] | 30 | match = re.match(r"^xnn_(s8|u8|f16|f32)_v(abs|clamp|elu|hswish|lrelu|neg|relu|rndd|rndne|rndu|rndz|sigmoid|sqr|sqrt)_(fact_)?ukernel__(.+)_x(\d+)$", name) |
Marat Dukhan | 346a9e5 | 2019-11-15 09:06:30 -0800 | [diff] [blame] | 31 | if match is None: |
| 32 | raise ValueError("Unexpected microkernel name: " + name) |
| 33 | op_type = { |
Marat Dukhan | 6674d69 | 2021-05-05 22:27:00 -0700 | [diff] [blame] | 34 | "abs": "Abs", |
Marat Dukhan | 60d3f24 | 2021-05-13 11:59:02 -0700 | [diff] [blame] | 35 | "clamp": "Clamp", |
Marat Dukhan | 6674d69 | 2021-05-05 22:27:00 -0700 | [diff] [blame] | 36 | "elu": "ELU", |
Marat Dukhan | 949b6e7 | 2021-05-13 11:21:06 -0700 | [diff] [blame] | 37 | "hswish": "HardSwish", |
Marat Dukhan | 6674d69 | 2021-05-05 22:27:00 -0700 | [diff] [blame] | 38 | "lrelu": "LeakyReLU", |
| 39 | "neg": "Negate", |
Marat Dukhan | 949b6e7 | 2021-05-13 11:21:06 -0700 | [diff] [blame] | 40 | "relu": "ReLU", |
| 41 | "rndd": "RoundDown", |
Marat Dukhan | 6674d69 | 2021-05-05 22:27:00 -0700 | [diff] [blame] | 42 | "rndne": "RoundToNearestEven", |
| 43 | "rndz": "RoundTowardsZero", |
| 44 | "rndu": "RoundUp", |
Marat Dukhan | 949b6e7 | 2021-05-13 11:21:06 -0700 | [diff] [blame] | 45 | "sigmoid": "Sigmoid", |
Marat Dukhan | 6674d69 | 2021-05-05 22:27:00 -0700 | [diff] [blame] | 46 | "sqr": "Square", |
| 47 | "sqrt": "SquareRoot", |
Marat Dukhan | 346a9e5 | 2019-11-15 09:06:30 -0800 | [diff] [blame] | 48 | }[match.group(2)] |
Erich Elsen | 8fd7b5f | 2019-11-18 10:50:41 -0800 | [diff] [blame] | 49 | batch_tile = int(match.group(5)) |
Marat Dukhan | 346a9e5 | 2019-11-15 09:06:30 -0800 | [diff] [blame] | 50 | |
Erich Elsen | 8fd7b5f | 2019-11-18 10:50:41 -0800 | [diff] [blame] | 51 | arch, isa = xnncommon.parse_target_name(target_name=match.group(4)) |
Marat Dukhan | 346a9e5 | 2019-11-15 09:06:30 -0800 | [diff] [blame] | 52 | return op_type, batch_tile, arch, isa |
| 53 | |
| 54 | |
| 55 | BINOP_TEST_TEMPLATE = """\ |
| 56 | TEST(${TEST_NAME}, batch_eq_${BATCH_TILE}) { |
| 57 | $if ISA_CHECK: |
| 58 | ${ISA_CHECK}; |
Marat Dukhan | 87ed45c | 2021-05-13 12:25:22 -0700 | [diff] [blame] | 59 | VUnaryMicrokernelTester() |
Marat Dukhan | 346a9e5 | 2019-11-15 09:06:30 -0800 | [diff] [blame] | 60 | .batch_size(${BATCH_TILE}) |
| 61 | .Test(${", ".join(TEST_ARGS)}); |
| 62 | } |
| 63 | |
| 64 | $if BATCH_TILE > 1: |
| 65 | TEST(${TEST_NAME}, batch_div_${BATCH_TILE}) { |
| 66 | $if ISA_CHECK: |
| 67 | ${ISA_CHECK}; |
| 68 | for (size_t batch_size = ${BATCH_TILE*2}; batch_size < ${BATCH_TILE*10}; batch_size += ${BATCH_TILE}) { |
Marat Dukhan | 87ed45c | 2021-05-13 12:25:22 -0700 | [diff] [blame] | 69 | VUnaryMicrokernelTester() |
Marat Dukhan | 346a9e5 | 2019-11-15 09:06:30 -0800 | [diff] [blame] | 70 | .batch_size(batch_size) |
| 71 | .Test(${", ".join(TEST_ARGS)}); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | TEST(${TEST_NAME}, batch_lt_${BATCH_TILE}) { |
| 76 | $if ISA_CHECK: |
| 77 | ${ISA_CHECK}; |
| 78 | for (size_t batch_size = 1; batch_size < ${BATCH_TILE}; batch_size++) { |
Marat Dukhan | 87ed45c | 2021-05-13 12:25:22 -0700 | [diff] [blame] | 79 | VUnaryMicrokernelTester() |
Marat Dukhan | 346a9e5 | 2019-11-15 09:06:30 -0800 | [diff] [blame] | 80 | .batch_size(batch_size) |
| 81 | .Test(${", ".join(TEST_ARGS)}); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | TEST(${TEST_NAME}, batch_gt_${BATCH_TILE}) { |
| 86 | $if ISA_CHECK: |
| 87 | ${ISA_CHECK}; |
| 88 | for (size_t batch_size = ${BATCH_TILE+1}; batch_size < ${10 if BATCH_TILE == 1 else BATCH_TILE*2}; batch_size++) { |
Marat Dukhan | 87ed45c | 2021-05-13 12:25:22 -0700 | [diff] [blame] | 89 | VUnaryMicrokernelTester() |
Marat Dukhan | 346a9e5 | 2019-11-15 09:06:30 -0800 | [diff] [blame] | 90 | .batch_size(batch_size) |
| 91 | .Test(${", ".join(TEST_ARGS)}); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | TEST(${TEST_NAME}, inplace) { |
| 96 | $if ISA_CHECK: |
| 97 | ${ISA_CHECK}; |
| 98 | for (size_t batch_size = 1; batch_size <= ${BATCH_TILE*5}; batch_size += ${max(1, BATCH_TILE-1)}) { |
Marat Dukhan | 87ed45c | 2021-05-13 12:25:22 -0700 | [diff] [blame] | 99 | VUnaryMicrokernelTester() |
Marat Dukhan | 346a9e5 | 2019-11-15 09:06:30 -0800 | [diff] [blame] | 100 | .batch_size(batch_size) |
| 101 | .inplace(true) |
| 102 | .Test(${", ".join(TEST_ARGS)}); |
| 103 | } |
| 104 | } |
Marat Dukhan | 8cc7efe | 2020-06-10 16:24:27 -0700 | [diff] [blame] | 105 | |
Marat Dukhan | 60d3f24 | 2021-05-13 11:59:02 -0700 | [diff] [blame] | 106 | $if OP_TYPE == "Clamp": |
| 107 | TEST(${TEST_NAME}, qmin) { |
Marat Dukhan | 8cc7efe | 2020-06-10 16:24:27 -0700 | [diff] [blame] | 108 | $if ISA_CHECK: |
| 109 | ${ISA_CHECK}; |
Marat Dukhan | 60d3f24 | 2021-05-13 11:59:02 -0700 | [diff] [blame] | 110 | for (uint8_t qmin = 1; qmin < 255; qmin++) { |
Marat Dukhan | 8cc7efe | 2020-06-10 16:24:27 -0700 | [diff] [blame] | 111 | for (size_t batch_size = 1; batch_size <= ${BATCH_TILE*5}; batch_size += ${max(1, BATCH_TILE-1)}) { |
Marat Dukhan | 87ed45c | 2021-05-13 12:25:22 -0700 | [diff] [blame] | 112 | VUnaryMicrokernelTester() |
Marat Dukhan | 8cc7efe | 2020-06-10 16:24:27 -0700 | [diff] [blame] | 113 | .batch_size(batch_size) |
Marat Dukhan | 60d3f24 | 2021-05-13 11:59:02 -0700 | [diff] [blame] | 114 | .qmin(qmin) |
| 115 | .Test(${", ".join(TEST_ARGS)}); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | TEST(${TEST_NAME}, qmax) { |
| 121 | $if ISA_CHECK: |
| 122 | ${ISA_CHECK}; |
| 123 | for (uint8_t qmax = 1; qmax < 255; qmax++) { |
| 124 | for (size_t batch_size = 1; batch_size <= ${BATCH_TILE*5}; batch_size += ${max(1, BATCH_TILE-1)}) { |
Marat Dukhan | 87ed45c | 2021-05-13 12:25:22 -0700 | [diff] [blame] | 125 | VUnaryMicrokernelTester() |
Marat Dukhan | 60d3f24 | 2021-05-13 11:59:02 -0700 | [diff] [blame] | 126 | .batch_size(batch_size) |
| 127 | .qmax(qmax) |
Marat Dukhan | 8cc7efe | 2020-06-10 16:24:27 -0700 | [diff] [blame] | 128 | .Test(${", ".join(TEST_ARGS)}); |
| 129 | } |
| 130 | } |
| 131 | } |
Marat Dukhan | ed6baaf | 2020-12-01 15:07:08 -0800 | [diff] [blame] | 132 | |
| 133 | $if OP_TYPE == "ELU": |
| 134 | TEST(${TEST_NAME}, prescale) { |
| 135 | $if ISA_CHECK: |
| 136 | ${ISA_CHECK}; |
| 137 | for (float prescale : std::vector<float>({0.1f, 10.0f})) { |
| 138 | for (size_t batch_size = 1; batch_size <= ${BATCH_TILE*5}; batch_size += ${max(1, BATCH_TILE-1)}) { |
Marat Dukhan | 87ed45c | 2021-05-13 12:25:22 -0700 | [diff] [blame] | 139 | VUnaryMicrokernelTester() |
Marat Dukhan | ed6baaf | 2020-12-01 15:07:08 -0800 | [diff] [blame] | 140 | .batch_size(batch_size) |
| 141 | .prescale(prescale) |
| 142 | .Test(${", ".join(TEST_ARGS)}); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | TEST(${TEST_NAME}, alpha) { |
| 148 | $if ISA_CHECK: |
| 149 | ${ISA_CHECK}; |
| 150 | for (float alpha : std::vector<float>({0.3f, 3.0f})) { |
| 151 | for (size_t batch_size = 1; batch_size <= ${BATCH_TILE*5}; batch_size += ${max(1, BATCH_TILE-1)}) { |
Marat Dukhan | 87ed45c | 2021-05-13 12:25:22 -0700 | [diff] [blame] | 152 | VUnaryMicrokernelTester() |
Marat Dukhan | ed6baaf | 2020-12-01 15:07:08 -0800 | [diff] [blame] | 153 | .batch_size(batch_size) |
| 154 | .alpha(alpha) |
| 155 | .Test(${", ".join(TEST_ARGS)}); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | TEST(${TEST_NAME}, beta) { |
| 161 | $if ISA_CHECK: |
| 162 | ${ISA_CHECK}; |
| 163 | for (float beta : std::vector<float>({0.3f, 3.0f})) { |
| 164 | for (size_t batch_size = 1; batch_size <= ${BATCH_TILE*5}; batch_size += ${max(1, BATCH_TILE-1)}) { |
Marat Dukhan | 87ed45c | 2021-05-13 12:25:22 -0700 | [diff] [blame] | 165 | VUnaryMicrokernelTester() |
Marat Dukhan | ed6baaf | 2020-12-01 15:07:08 -0800 | [diff] [blame] | 166 | .batch_size(batch_size) |
| 167 | .beta(beta) |
| 168 | .Test(${", ".join(TEST_ARGS)}); |
| 169 | } |
| 170 | } |
| 171 | } |
Marat Dukhan | 60d3f24 | 2021-05-13 11:59:02 -0700 | [diff] [blame] | 172 | |
| 173 | $if OP_TYPE == "LeakyReLU": |
| 174 | TEST(${TEST_NAME}, slope) { |
| 175 | $if ISA_CHECK: |
| 176 | ${ISA_CHECK}; |
| 177 | for (float slope : std::vector<float>({-0.7f, 0.3f, 1.3f})) { |
| 178 | for (size_t batch_size = 1; batch_size <= ${BATCH_TILE*5}; batch_size += ${max(1, BATCH_TILE-1)}) { |
Marat Dukhan | 87ed45c | 2021-05-13 12:25:22 -0700 | [diff] [blame] | 179 | VUnaryMicrokernelTester() |
Marat Dukhan | 60d3f24 | 2021-05-13 11:59:02 -0700 | [diff] [blame] | 180 | .batch_size(batch_size) |
| 181 | .slope(slope) |
| 182 | .Test(${", ".join(TEST_ARGS)}); |
| 183 | } |
| 184 | } |
| 185 | } |
Marat Dukhan | 346a9e5 | 2019-11-15 09:06:30 -0800 | [diff] [blame] | 186 | """ |
| 187 | |
| 188 | |
Marat Dukhan | 1f5b108 | 2021-08-16 17:01:44 -0700 | [diff] [blame] | 189 | def generate_test_cases(ukernel, op_type, init_fn, batch_tile, isa): |
Marat Dukhan | 346a9e5 | 2019-11-15 09:06:30 -0800 | [diff] [blame] | 190 | """Generates all tests cases for a Vector Unary Operation micro-kernel. |
| 191 | |
| 192 | Args: |
| 193 | ukernel: C name of the micro-kernel function. |
| 194 | op_type: Operation type. |
Marat Dukhan | 1f5b108 | 2021-08-16 17:01:44 -0700 | [diff] [blame] | 195 | init_fn: C name of the function to initialize microkernel parameters. |
Marat Dukhan | 346a9e5 | 2019-11-15 09:06:30 -0800 | [diff] [blame] | 196 | batch_tile: Number of batch elements processed per one iteration of the |
| 197 | inner loop of the micro-kernel. |
| 198 | isa: instruction set required to run the micro-kernel. Generated unit test |
| 199 | will skip execution if the host processor doesn't support this ISA. |
| 200 | |
| 201 | Returns: |
| 202 | Code for the test case. |
| 203 | """ |
| 204 | _, test_name = ukernel.split("_", 1) |
| 205 | _, datatype, _ = ukernel.split("_", 2) |
Marat Dukhan | 0d10cc7 | 2021-12-23 19:49:19 -0800 | [diff] [blame] | 206 | test_args = [ukernel] |
Marat Dukhan | 1f5b108 | 2021-08-16 17:01:44 -0700 | [diff] [blame] | 207 | if init_fn: |
| 208 | test_args.append(init_fn) |
Marat Dukhan | 0d10cc7 | 2021-12-23 19:49:19 -0800 | [diff] [blame] | 209 | else: |
| 210 | test_args.append("VUnaryMicrokernelTester::OpType::" + op_type) |
| 211 | if not isa: |
| 212 | test_args.append("VUnaryMicrokernelTester::Variant::Scalar") |
Marat Dukhan | 346a9e5 | 2019-11-15 09:06:30 -0800 | [diff] [blame] | 213 | return xngen.preprocess(BINOP_TEST_TEMPLATE, { |
| 214 | "TEST_NAME": test_name.upper().replace("UKERNEL_", ""), |
| 215 | "TEST_ARGS": test_args, |
| 216 | "DATATYPE": datatype, |
| 217 | "BATCH_TILE": batch_tile, |
| 218 | "OP_TYPE": op_type, |
| 219 | "ISA_CHECK": xnncommon.generate_isa_check_macro(isa), |
| 220 | }) |
| 221 | |
| 222 | |
| 223 | def main(args): |
| 224 | options = parser.parse_args(args) |
| 225 | |
| 226 | with codecs.open(options.spec, "r", encoding="utf-8") as spec_file: |
| 227 | spec_yaml = yaml.safe_load(spec_file) |
| 228 | if not isinstance(spec_yaml, list): |
| 229 | raise ValueError("expected a list of micro-kernels in the spec") |
| 230 | |
| 231 | tests = """\ |
| 232 | // Copyright 2019 Google LLC |
| 233 | // |
| 234 | // This source code is licensed under the BSD-style license found in the |
| 235 | // LICENSE file in the root directory of this source tree. |
| 236 | // |
| 237 | // Auto-generated file. Do not edit! |
| 238 | // Specification: {specification} |
| 239 | // Generator: {generator} |
| 240 | |
| 241 | |
| 242 | #include <gtest/gtest.h> |
| 243 | |
| 244 | #include <xnnpack/common.h> |
| 245 | #include <xnnpack/isa-checks.h> |
| 246 | |
Marat Dukhan | 1e782c4 | 2019-11-21 17:02:40 -0800 | [diff] [blame] | 247 | #include <xnnpack/vunary.h> |
| 248 | #include "vunary-microkernel-tester.h" |
Marat Dukhan | 346a9e5 | 2019-11-15 09:06:30 -0800 | [diff] [blame] | 249 | """.format(specification=options.spec, generator=sys.argv[0]) |
| 250 | |
| 251 | for ukernel_spec in spec_yaml: |
| 252 | name = ukernel_spec["name"] |
Marat Dukhan | 1f5b108 | 2021-08-16 17:01:44 -0700 | [diff] [blame] | 253 | init_fn = ukernel_spec.get("init") |
Marat Dukhan | 346a9e5 | 2019-11-15 09:06:30 -0800 | [diff] [blame] | 254 | op_type, batch_tile, arch, isa = split_ukernel_name(name) |
| 255 | |
| 256 | # specification can override architecture |
| 257 | arch = ukernel_spec.get("arch", arch) |
| 258 | |
Marat Dukhan | 1f5b108 | 2021-08-16 17:01:44 -0700 | [diff] [blame] | 259 | test_case = generate_test_cases(name, op_type, init_fn, batch_tile, isa) |
Marat Dukhan | 346a9e5 | 2019-11-15 09:06:30 -0800 | [diff] [blame] | 260 | tests += "\n\n" + xnncommon.postprocess_test_case(test_case, arch, isa) |
| 261 | |
Frank Barchard | 1f83cf9 | 2021-09-07 14:13:03 -0700 | [diff] [blame] | 262 | txt_changed = True |
| 263 | if os.path.exists(options.output): |
| 264 | with codecs.open(options.output, "r", encoding="utf-8") as output_file: |
| 265 | txt_changed = output_file.read() != tests |
| 266 | |
| 267 | if txt_changed: |
| 268 | with codecs.open(options.output, "w", encoding="utf-8") as output_file: |
| 269 | output_file.write(tests) |
Marat Dukhan | 346a9e5 | 2019-11-15 09:06:30 -0800 | [diff] [blame] | 270 | |
| 271 | |
| 272 | if __name__ == "__main__": |
| 273 | main(sys.argv[1:]) |