blob: 1db947a07de129488445f5fa07420c4166be68d9 [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='RAddStoreExpMinusMax 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)_raddstoreexpminusmax_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
39RADDSTOREEXPMINUSMAX_TEST_TEMPLATE = """\
40TEST(${TEST_NAME}, elements_eq_${ELEMENTS_TILE}) {
41 $if ISA_CHECK:
42 ${ISA_CHECK};
43 RAddStoreExpMinusMaxMicrokernelTester()
44 .elements(${ELEMENTS_TILE})
Marat Dukhan4a5c7712022-01-05 22:43:13 -080045 .Test(${TEST_FUNCTION}, ${INIT_FUNCTION});
Marat Dukhan4c4eb002019-12-08 21:27:49 -080046}
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 RAddStoreExpMinusMaxMicrokernelTester()
54 .elements(elements)
Marat Dukhan4a5c7712022-01-05 22:43:13 -080055 .Test(${TEST_FUNCTION}, ${INIT_FUNCTION});
Marat Dukhan4c4eb002019-12-08 21:27:49 -080056 }
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 RAddStoreExpMinusMaxMicrokernelTester()
64 .elements(elements)
Marat Dukhan4a5c7712022-01-05 22:43:13 -080065 .Test(${TEST_FUNCTION}, ${INIT_FUNCTION});
Marat Dukhan4c4eb002019-12-08 21:27:49 -080066 }
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 RAddStoreExpMinusMaxMicrokernelTester()
74 .elements(elements)
Marat Dukhan4a5c7712022-01-05 22:43:13 -080075 .Test(${TEST_FUNCTION}, ${INIT_FUNCTION});
Marat Dukhan4c4eb002019-12-08 21:27:49 -080076 }
77}
78"""
79
80
Marat Dukhan4a5c7712022-01-05 22:43:13 -080081def generate_test_cases(ukernel, init_fn, elements_tile, isa):
Marat Dukhan4c4eb002019-12-08 21:27:49 -080082 """Generates all tests cases for a RAddStoreExpMinusMax micro-kernel.
83
84 Args:
85 ukernel: C name of the micro-kernel function.
Marat Dukhan4a5c7712022-01-05 22:43:13 -080086 init_fn: C name of the function to initialize microkernel parameters.
Marat Dukhan4c4eb002019-12-08 21:27:49 -080087 elements_tile: Number of batch elements processed per one iteration of the
88 inner loop of the micro-kernel.
89 isa: instruction set required to run the micro-kernel. Generated unit test
90 will skip execution if the host processor doesn't support this ISA.
91
92 Returns:
93 Code for the test case.
94 """
95 _, test_name = ukernel.split("_", 1)
96 _, datatype, _ = ukernel.split("_", 2)
97 return xngen.preprocess(RADDSTOREEXPMINUSMAX_TEST_TEMPLATE, {
98 "TEST_FUNCTION": ukernel,
Marat Dukhan4a5c7712022-01-05 22:43:13 -080099 "INIT_FUNCTION": init_fn,
Marat Dukhan4c4eb002019-12-08 21:27:49 -0800100 "TEST_NAME": test_name.upper().replace("UKERNEL_", ""),
101 "DATATYPE": datatype,
102 "ELEMENTS_TILE": elements_tile,
103 "ISA_CHECK": xnncommon.generate_isa_check_macro(isa),
104 })
105
106
107def main(args):
108 options = parser.parse_args(args)
109
110 with codecs.open(options.spec, "r", encoding="utf-8") as spec_file:
111 spec_yaml = yaml.safe_load(spec_file)
112 if not isinstance(spec_yaml, list):
113 raise ValueError("expected a list of micro-kernels in the spec")
114
115 tests = """\
116// Copyright 2019 Google LLC
117//
118// This source code is licensed under the BSD-style license found in the
119// LICENSE file in the root directory of this source tree.
120//
121// Auto-generated file. Do not edit!
122// Specification: {specification}
123// Generator: {generator}
124
125
126#include <gtest/gtest.h>
127
128#include <xnnpack/common.h>
129#include <xnnpack/isa-checks.h>
130
131#include <xnnpack/raddstoreexpminusmax.h>
132#include "raddstoreexpminusmax-microkernel-tester.h"
133""".format(specification=options.spec, generator=sys.argv[0])
134
135 for ukernel_spec in spec_yaml:
136 name = ukernel_spec["name"]
Marat Dukhan4a5c7712022-01-05 22:43:13 -0800137 init_fn = ukernel_spec.get("init")
Marat Dukhan4c4eb002019-12-08 21:27:49 -0800138 elements_tile, arch, isa = split_ukernel_name(name)
139
140 # specification can override architecture
141 arch = ukernel_spec.get("arch", arch)
142
Marat Dukhan4a5c7712022-01-05 22:43:13 -0800143 test_case = generate_test_cases(name, init_fn, elements_tile, isa)
Marat Dukhan4c4eb002019-12-08 21:27:49 -0800144 tests += "\n\n" + xnncommon.postprocess_test_case(test_case, arch, isa)
145
Frank Barchard1f83cf92021-09-07 14:13:03 -0700146 txt_changed = True
147 if os.path.exists(options.output):
148 with codecs.open(options.output, "r", encoding="utf-8") as output_file:
149 txt_changed = output_file.read() != tests
150
151 if txt_changed:
152 with codecs.open(options.output, "w", encoding="utf-8") as output_file:
153 output_file.write(tests)
Marat Dukhan4c4eb002019-12-08 21:27:49 -0800154
155
156if __name__ == "__main__":
157 main(sys.argv[1:])