blob: 33421c12cb3dc1be37d44bad94b3a1623d753dd6 [file] [log] [blame]
Marat Dukhan4c4eb002019-12-08 21:27:49 -08001#!/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
7import argparse
8import codecs
9import math
10import os
11import re
12import sys
13import yaml
14
15sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
16import xngen
17import xnncommon
18
19
20parser = argparse.ArgumentParser(
21 description='Vector ScaleExpMinusMax microkernel test generator')
22parser.add_argument("-s", "--spec", metavar="FILE", required=True,
23 help="Specification (YAML) file")
24parser.add_argument("-o", "--output", metavar="FILE", required=True,
25 help='Output (C++ source) file')
26parser.set_defaults(defines=list())
27
28
29def split_ukernel_name(name):
30 match = re.match(r"^xnn_(f16|f32)_vscaleexpminusmax_ukernel__(.+)_x(\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
39RADDEXTEXP_TEST_TEMPLATE = """\
40TEST(${TEST_NAME}, elements_eq_${ELEMENTS_TILE}) {
41 $if ISA_CHECK:
42 ${ISA_CHECK};
43 VScaleExpMinusMaxMicrokernelTester()
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 VScaleExpMinusMaxMicrokernelTester()
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 VScaleExpMinusMaxMicrokernelTester()
64 .elements(elements)
65 .Test(${TEST_FUNCTION});
66 }
67 }
68
69TEST(${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 VScaleExpMinusMaxMicrokernelTester()
74 .elements(elements)
75 .Test(${TEST_FUNCTION});
76 }
77}
78
79TEST(${TEST_NAME}, scale) {
80 $if ISA_CHECK:
81 ${ISA_CHECK};
82 for (size_t elements = 1; elements <= ${ELEMENTS_TILE*5}; elements += ${max(1, ELEMENTS_TILE-1)}) {
83 VScaleExpMinusMaxMicrokernelTester()
84 .elements(elements)
85 .scale(0.01f)
86 .Test(${TEST_FUNCTION});
87 VScaleExpMinusMaxMicrokernelTester()
88 .elements(elements)
89 .scale(100.0f)
90 .Test(${TEST_FUNCTION});
91 }
92}
93"""
94
95
96def generate_test_cases(ukernel, elements_tile, isa):
97 """Generates all tests cases for a Vector ScaleExpMinusMax micro-kernel.
98
99 Args:
100 ukernel: C name of the micro-kernel function.
101 elements_tile: Number of batch elements processed per one iteration of the
102 inner loop of the micro-kernel.
103 isa: instruction set required to run the micro-kernel. Generated unit test
104 will skip execution if the host processor doesn't support this ISA.
105
106 Returns:
107 Code for the test case.
108 """
109 _, test_name = ukernel.split("_", 1)
110 _, datatype, _ = ukernel.split("_", 2)
111 return xngen.preprocess(RADDEXTEXP_TEST_TEMPLATE, {
112 "TEST_FUNCTION": ukernel,
113 "TEST_NAME": test_name.upper().replace("UKERNEL_", ""),
114 "DATATYPE": datatype,
115 "ELEMENTS_TILE": elements_tile,
116 "ISA_CHECK": xnncommon.generate_isa_check_macro(isa),
117 })
118
119
120def main(args):
121 options = parser.parse_args(args)
122
123 with codecs.open(options.spec, "r", encoding="utf-8") as spec_file:
124 spec_yaml = yaml.safe_load(spec_file)
125 if not isinstance(spec_yaml, list):
126 raise ValueError("expected a list of micro-kernels in the spec")
127
128 tests = """\
129// Copyright 2019 Google LLC
130//
131// This source code is licensed under the BSD-style license found in the
132// LICENSE file in the root directory of this source tree.
133//
134// Auto-generated file. Do not edit!
135// Specification: {specification}
136// Generator: {generator}
137
138
139#include <gtest/gtest.h>
140
141#include <xnnpack/common.h>
142#include <xnnpack/isa-checks.h>
143
144#include <xnnpack/vscaleexpminusmax.h>
145#include "vscaleexpminusmax-microkernel-tester.h"
146""".format(specification=options.spec, generator=sys.argv[0])
147
148 for ukernel_spec in spec_yaml:
149 name = ukernel_spec["name"]
150 elements_tile, arch, isa = split_ukernel_name(name)
151
152 # specification can override architecture
153 arch = ukernel_spec.get("arch", arch)
154
155 test_case = generate_test_cases(name, elements_tile, isa)
156 tests += "\n\n" + xnncommon.postprocess_test_case(test_case, arch, isa)
157
Frank Barchard1f83cf92021-09-07 14:13:03 -0700158 txt_changed = True
159 if os.path.exists(options.output):
160 with codecs.open(options.output, "r", encoding="utf-8") as output_file:
161 txt_changed = output_file.read() != tests
162
163 if txt_changed:
164 with codecs.open(options.output, "w", encoding="utf-8") as output_file:
165 output_file.write(tests)
Marat Dukhan4c4eb002019-12-08 21:27:49 -0800166
167
168if __name__ == "__main__":
169 main(sys.argv[1:])