blob: 5a88f93ac3e7a075e541aac5abf3d096203b968b [file] [log] [blame]
Marat Dukhan5c5fa962020-03-10 18:38:33 -07001#!/usr/bin/env python
2# Copyright 2020 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='Clamp 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_(u8|f16|f32)_clamp_ukernel__(.+)_x(\d+)$", name)
31 if match is None:
32 raise ValueError("Unexpected microkernel name: " + name)
33 batch_tile = int(match.group(3))
34
35 arch, isa = xnncommon.parse_target_name(target_name=match.group(2))
36 return batch_tile, arch, isa
37
38
39CLAMP_TEST_TEMPLATE = """\
40TEST(${TEST_NAME}, batch_eq_${BATCH_TILE}) {
41 $if ISA_CHECK:
42 ${ISA_CHECK};
43 ClampMicrokernelTester()
44 .batch_size(${BATCH_TILE})
45 .Test(${", ".join(TEST_ARGS)});
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 ClampMicrokernelTester()
54 .batch_size(batch_size)
55 .Test(${", ".join(TEST_ARGS)});
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 ClampMicrokernelTester()
64 .batch_size(batch_size)
65 .Test(${", ".join(TEST_ARGS)});
66 }
67 }
68
69TEST(${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 ClampMicrokernelTester()
74 .batch_size(batch_size)
75 .Test(${", ".join(TEST_ARGS)});
76 }
77}
78
79TEST(${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 ClampMicrokernelTester()
84 .batch_size(batch_size)
85 .inplace(true)
86 .Test(${", ".join(TEST_ARGS)});
87 }
88}
89
90TEST(${TEST_NAME}, qmin) {
91 $if ISA_CHECK:
92 ${ISA_CHECK};
93 for (size_t batch_size = 1; batch_size <= ${BATCH_TILE*5}; batch_size += ${max(1, BATCH_TILE-1)}) {
94 for (uint8_t qmin = 1; qmin < 255; qmin++) {
95 ClampMicrokernelTester()
96 .batch_size(batch_size)
97 .qmin(qmin)
98 .qmax(255)
99 .Test(${", ".join(TEST_ARGS)});
100 }
101 }
102}
103
104TEST(${TEST_NAME}, qmax) {
105 $if ISA_CHECK:
106 ${ISA_CHECK};
107 for (size_t batch_size = 1; batch_size <= ${BATCH_TILE*5}; batch_size += ${max(1, BATCH_TILE-1)}) {
108 for (uint8_t qmax = 1; qmax < 255; qmax++) {
109 ClampMicrokernelTester()
110 .batch_size(batch_size)
111 .qmin(0)
112 .qmax(qmax)
113 .Test(${", ".join(TEST_ARGS)});
114 }
115 }
116}
117"""
118
119
120def generate_test_cases(ukernel, batch_tile, isa):
121 """Generates all tests cases for a Clamp micro-kernel.
122
123 Args:
124 ukernel: C name of the micro-kernel function.
125 batch_tile: Number of batch elements processed per one iteration of the
126 inner loop of the micro-kernel.
127 isa: instruction set required to run the micro-kernel. Generated unit test
128 will skip execution if the host processor doesn't support this ISA.
129
130 Returns:
131 Code for the test case.
132 """
133 _, test_name = ukernel.split("_", 1)
134 _, datatype, _ = ukernel.split("_", 2)
135 test_args = [ukernel]
Marat Dukhan3de5dfa2020-12-10 11:19:47 -0800136 if not isa:
Marat Dukhan5c5fa962020-03-10 18:38:33 -0700137 test_args.append("ClampMicrokernelTester::Variant::Scalar")
138 return xngen.preprocess(CLAMP_TEST_TEMPLATE, {
139 "TEST_NAME": test_name.upper().replace("UKERNEL_", ""),
140 "TEST_ARGS": test_args,
141 "DATATYPE": datatype,
142 "BATCH_TILE": batch_tile,
143 "ISA_CHECK": xnncommon.generate_isa_check_macro(isa),
144 })
145
146
147def main(args):
148 options = parser.parse_args(args)
149
150 with codecs.open(options.spec, "r", encoding="utf-8") as spec_file:
151 spec_yaml = yaml.safe_load(spec_file)
152 if not isinstance(spec_yaml, list):
153 raise ValueError("expected a list of micro-kernels in the spec")
154
155 tests = """\
156// Copyright 2020 Google LLC
157//
158// This source code is licensed under the BSD-style license found in the
159// LICENSE file in the root directory of this source tree.
160//
161// Auto-generated file. Do not edit!
162// Specification: {specification}
163// Generator: {generator}
164
165
166#include <gtest/gtest.h>
167
168#include <xnnpack/common.h>
169#include <xnnpack/isa-checks.h>
170
171#include <xnnpack/clamp.h>
172#include "clamp-microkernel-tester.h"
173""".format(specification=options.spec, generator=sys.argv[0])
174
175 for ukernel_spec in spec_yaml:
176 name = ukernel_spec["name"]
177 batch_tile, arch, isa = split_ukernel_name(name)
178
179 # specification can override architecture
180 arch = ukernel_spec.get("arch", arch)
181
182 test_case = generate_test_cases(name, batch_tile, isa)
183 tests += "\n\n" + xnncommon.postprocess_test_case(test_case, arch, isa)
184
185 with codecs.open(options.output, "w", encoding="utf-8") as output_file:
186 output_file.write(tests)
187
188
189if __name__ == "__main__":
190 main(sys.argv[1:])