Marat Dukhan | d67539d | 2021-09-08 23:06:03 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright 2021 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='LUT 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): |
| 30 | match = re.match(r"^xnn_x8_lut_ukernel__(.+)_x(\d+)$", name) |
| 31 | if match is None: |
| 32 | raise ValueError("Unexpected microkernel name: " + name) |
| 33 | batch_tile = int(match.group(2)) |
| 34 | |
| 35 | arch, isa = xnncommon.parse_target_name(target_name=match.group(1)) |
| 36 | return batch_tile, arch, isa |
| 37 | |
| 38 | |
| 39 | LUT_TEST_TEMPLATE = """\ |
| 40 | TEST(${TEST_NAME}, batch_eq_${BATCH_TILE}) { |
| 41 | $if ISA_CHECK: |
| 42 | ${ISA_CHECK}; |
| 43 | LUTMicrokernelTester() |
| 44 | .batch_size(${BATCH_TILE}) |
| 45 | .Test(${UKERNEL_NAME}); |
| 46 | } |
| 47 | |
| 48 | $if BATCH_TILE > 1: |
| 49 | TEST(${TEST_NAME}, batch_div_${BATCH_TILE}) { |
| 50 | $if ISA_CHECK: |
| 51 | ${ISA_CHECK}; |
| 52 | for (size_t batch_size = ${BATCH_TILE*2}; batch_size < ${BATCH_TILE*10}; batch_size += ${BATCH_TILE}) { |
| 53 | LUTMicrokernelTester() |
| 54 | .batch_size(batch_size) |
| 55 | .Test(${UKERNEL_NAME}); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | TEST(${TEST_NAME}, batch_lt_${BATCH_TILE}) { |
| 60 | $if ISA_CHECK: |
| 61 | ${ISA_CHECK}; |
| 62 | for (size_t batch_size = 1; batch_size < ${BATCH_TILE}; batch_size++) { |
| 63 | LUTMicrokernelTester() |
| 64 | .batch_size(batch_size) |
| 65 | .Test(${UKERNEL_NAME}); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | TEST(${TEST_NAME}, batch_gt_${BATCH_TILE}) { |
| 70 | $if ISA_CHECK: |
| 71 | ${ISA_CHECK}; |
| 72 | for (size_t batch_size = ${BATCH_TILE+1}; batch_size < ${10 if BATCH_TILE == 1 else BATCH_TILE*2}; batch_size++) { |
| 73 | LUTMicrokernelTester() |
| 74 | .batch_size(batch_size) |
| 75 | .Test(${UKERNEL_NAME}); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | TEST(${TEST_NAME}, inplace) { |
| 80 | $if ISA_CHECK: |
| 81 | ${ISA_CHECK}; |
| 82 | for (size_t batch_size = 1; batch_size <= ${BATCH_TILE*5}; batch_size += ${max(1, BATCH_TILE-1)}) { |
| 83 | LUTMicrokernelTester() |
| 84 | .batch_size(batch_size) |
| 85 | .inplace(true) |
| 86 | .Test(${UKERNEL_NAME}); |
| 87 | } |
| 88 | } |
| 89 | """ |
| 90 | |
| 91 | |
| 92 | def generate_test_cases(ukernel, batch_tile, isa): |
| 93 | """Generates all tests cases for a LUT micro-kernel. |
| 94 | |
| 95 | Args: |
| 96 | ukernel: C name of the micro-kernel function. |
| 97 | batch_tile: Number of batch elements processed per one iteration of the |
| 98 | inner loop of the micro-kernel. |
| 99 | isa: instruction set required to run the micro-kernel. Generated unit test |
| 100 | will skip execution if the host processor doesn't support this ISA. |
| 101 | |
| 102 | Returns: |
| 103 | Code for the test case. |
| 104 | """ |
| 105 | _, test_name = ukernel.split("_", 1) |
| 106 | return xngen.preprocess(LUT_TEST_TEMPLATE, { |
| 107 | "TEST_NAME": test_name.upper().replace("UKERNEL_", ""), |
| 108 | "BATCH_TILE": batch_tile, |
| 109 | "UKERNEL_NAME": ukernel, |
| 110 | "ISA_CHECK": xnncommon.generate_isa_check_macro(isa), |
| 111 | }) |
| 112 | |
| 113 | |
| 114 | def main(args): |
| 115 | options = parser.parse_args(args) |
| 116 | |
| 117 | with codecs.open(options.spec, "r", encoding="utf-8") as spec_file: |
| 118 | spec_yaml = yaml.safe_load(spec_file) |
| 119 | if not isinstance(spec_yaml, list): |
| 120 | raise ValueError("expected a list of micro-kernels in the spec") |
| 121 | |
| 122 | tests = """\ |
| 123 | // Copyright 2021 Google LLC |
| 124 | // |
| 125 | // This source code is licensed under the BSD-style license found in the |
| 126 | // LICENSE file in the root directory of this source tree. |
| 127 | // |
| 128 | // Auto-generated file. Do not edit! |
| 129 | // Specification: {specification} |
| 130 | // Generator: {generator} |
| 131 | |
| 132 | |
| 133 | #include <gtest/gtest.h> |
| 134 | |
| 135 | #include <xnnpack/common.h> |
| 136 | #include <xnnpack/isa-checks.h> |
| 137 | |
| 138 | #include <xnnpack/lut.h> |
| 139 | #include "lut-microkernel-tester.h" |
| 140 | """.format(specification=options.spec, generator=sys.argv[0]) |
| 141 | |
| 142 | for ukernel_spec in spec_yaml: |
| 143 | name = ukernel_spec["name"] |
| 144 | batch_tile, arch, isa = split_ukernel_name(name) |
| 145 | |
| 146 | # specification can override architecture |
| 147 | arch = ukernel_spec.get("arch", arch) |
| 148 | |
| 149 | test_case = generate_test_cases(name, batch_tile, isa) |
| 150 | tests += "\n\n" + xnncommon.postprocess_test_case(test_case, arch, isa) |
| 151 | |
| 152 | txt_changed = True |
| 153 | if os.path.exists(options.output): |
| 154 | with codecs.open(options.output, "r", encoding="utf-8") as output_file: |
| 155 | txt_changed = output_file.read() != tests |
| 156 | |
| 157 | if txt_changed: |
| 158 | with codecs.open(options.output, "w", encoding="utf-8") as output_file: |
| 159 | output_file.write(tests) |
| 160 | |
| 161 | |
| 162 | if __name__ == "__main__": |
| 163 | main(sys.argv[1:]) |