Marat Dukhan | 4c4eb00 | 2019-12-08 21:27:49 -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='RAddExpMinusMax 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_(f16|f32)_raddexpminusmax_ukernel__(.+)_x(\d+)(_acc(\d+))?$", name) |
| 31 | if match is None: |
| 32 | raise ValueError("Unexpected microkernel name: " + name) |
| 33 | elements_tile = int(match.group(3)) |
| 34 | |
| 35 | arch, isa = xnncommon.parse_target_name(target_name=match.group(2)) |
| 36 | return elements_tile, arch, isa |
| 37 | |
| 38 | |
| 39 | RADDEXPMINUSMAX_TEST_TEMPLATE = """\ |
| 40 | TEST(${TEST_NAME}, elements_eq_${ELEMENTS_TILE}) { |
| 41 | $if ISA_CHECK: |
| 42 | ${ISA_CHECK}; |
| 43 | RAddExpMinusMaxMicrokernelTester() |
| 44 | .elements(${ELEMENTS_TILE}) |
| 45 | .Test(${TEST_FUNCTION}); |
| 46 | } |
| 47 | |
| 48 | $if ELEMENTS_TILE > 1: |
| 49 | TEST(${TEST_NAME}, elements_div_${ELEMENTS_TILE}) { |
| 50 | $if ISA_CHECK: |
| 51 | ${ISA_CHECK}; |
| 52 | for (size_t elements = ${ELEMENTS_TILE*2}; elements < ${ELEMENTS_TILE*10}; elements += ${ELEMENTS_TILE}) { |
| 53 | RAddExpMinusMaxMicrokernelTester() |
| 54 | .elements(elements) |
| 55 | .Test(${TEST_FUNCTION}); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | TEST(${TEST_NAME}, elements_lt_${ELEMENTS_TILE}) { |
| 60 | $if ISA_CHECK: |
| 61 | ${ISA_CHECK}; |
| 62 | for (size_t elements = 1; elements < ${ELEMENTS_TILE}; elements++) { |
| 63 | RAddExpMinusMaxMicrokernelTester() |
| 64 | .elements(elements) |
| 65 | .Test(${TEST_FUNCTION}); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | TEST(${TEST_NAME}, elements_gt_${ELEMENTS_TILE}) { |
| 70 | $if ISA_CHECK: |
| 71 | ${ISA_CHECK}; |
| 72 | for (size_t elements = ${ELEMENTS_TILE+1}; elements < ${10 if ELEMENTS_TILE == 1 else ELEMENTS_TILE*2}; elements++) { |
| 73 | RAddExpMinusMaxMicrokernelTester() |
| 74 | .elements(elements) |
| 75 | .Test(${TEST_FUNCTION}); |
| 76 | } |
| 77 | } |
| 78 | """ |
| 79 | |
| 80 | |
| 81 | def generate_test_cases(ukernel, elements_tile, isa): |
| 82 | """Generates all tests cases for a RAddExpMinusMax micro-kernel. |
| 83 | |
| 84 | Args: |
| 85 | ukernel: C name of the micro-kernel function. |
| 86 | elements_tile: Number of batch elements processed per one iteration of the |
| 87 | inner loop of the micro-kernel. |
| 88 | isa: instruction set required to run the micro-kernel. Generated unit test |
| 89 | will skip execution if the host processor doesn't support this ISA. |
| 90 | |
| 91 | Returns: |
| 92 | Code for the test case. |
| 93 | """ |
| 94 | _, test_name = ukernel.split("_", 1) |
| 95 | _, datatype, _ = ukernel.split("_", 2) |
| 96 | return xngen.preprocess(RADDEXPMINUSMAX_TEST_TEMPLATE, { |
| 97 | "TEST_FUNCTION": ukernel, |
| 98 | "TEST_NAME": test_name.upper().replace("UKERNEL_", ""), |
| 99 | "DATATYPE": datatype, |
| 100 | "ELEMENTS_TILE": elements_tile, |
| 101 | "ISA_CHECK": xnncommon.generate_isa_check_macro(isa), |
| 102 | }) |
| 103 | |
| 104 | |
| 105 | def main(args): |
| 106 | options = parser.parse_args(args) |
| 107 | |
| 108 | with codecs.open(options.spec, "r", encoding="utf-8") as spec_file: |
| 109 | spec_yaml = yaml.safe_load(spec_file) |
| 110 | if not isinstance(spec_yaml, list): |
| 111 | raise ValueError("expected a list of micro-kernels in the spec") |
| 112 | |
| 113 | tests = """\ |
| 114 | // Copyright 2019 Google LLC |
| 115 | // |
| 116 | // This source code is licensed under the BSD-style license found in the |
| 117 | // LICENSE file in the root directory of this source tree. |
| 118 | // |
| 119 | // Auto-generated file. Do not edit! |
| 120 | // Specification: {specification} |
| 121 | // Generator: {generator} |
| 122 | |
| 123 | |
| 124 | #include <gtest/gtest.h> |
| 125 | |
| 126 | #include <xnnpack/common.h> |
| 127 | #include <xnnpack/isa-checks.h> |
| 128 | |
| 129 | #include <xnnpack/raddexpminusmax.h> |
| 130 | #include "raddexpminusmax-microkernel-tester.h" |
| 131 | """.format(specification=options.spec, generator=sys.argv[0]) |
| 132 | |
| 133 | for ukernel_spec in spec_yaml: |
| 134 | name = ukernel_spec["name"] |
| 135 | elements_tile, arch, isa = split_ukernel_name(name) |
| 136 | |
| 137 | # specification can override architecture |
| 138 | arch = ukernel_spec.get("arch", arch) |
| 139 | |
| 140 | test_case = generate_test_cases(name, elements_tile, isa) |
| 141 | tests += "\n\n" + xnncommon.postprocess_test_case(test_case, arch, isa) |
| 142 | |
| 143 | with codecs.open(options.output, "w", encoding="utf-8") as output_file: |
| 144 | output_file.write(tests) |
| 145 | |
| 146 | |
| 147 | if __name__ == "__main__": |
| 148 | main(sys.argv[1:]) |