xref: /aosp_15_r20/external/XNNPACK/tools/generate-vmulcaddc-test.py (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
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
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__)))
16from primes import next_prime
17import xngen
18import xnncommon
19
20
21parser = argparse.ArgumentParser(
22  description='VMulCAddC microkernel test generator')
23parser.add_argument("-s", "--spec", metavar="FILE", required=True,
24                    help="Specification (YAML) file")
25parser.add_argument("-o", "--output", metavar="FILE", required=True,
26                    help='Output (C++ source) file')
27parser.set_defaults(defines=list())
28
29
30def split_ukernel_name(name):
31  match = re.fullmatch(r"xnn_(f16|f32)_vmulcaddc(_(minmax))?_ukernel_c(\d+)__(.+)_(\d+)x", name)
32  assert match is not None
33  channel_tile = int(match.group(4))
34  row_tile = int(match.group(6))
35
36  arch, isa = xnncommon.parse_target_name(target_name=match.group(5))
37  return channel_tile, row_tile, arch, isa
38
39
40VMULCADDC_TEST_TEMPLATE = """\
41TEST(${TEST_NAME}, channels_eq_${CHANNEL_TILE}) {
42  $if ISA_CHECK:
43    ${ISA_CHECK};
44  VMulCAddCMicrokernelTester()
45    .channel_tile(${CHANNEL_TILE})
46    .channels(${CHANNEL_TILE})
47    .rows(${ROW_TILE})
48    .Test(${", ".join(TEST_ARGS)});
49}
50
51$if CHANNEL_TILE > 1:
52  TEST(${TEST_NAME}, channels_div_${CHANNEL_TILE}) {
53    $if ISA_CHECK:
54      ${ISA_CHECK};
55    for (size_t channels = ${CHANNEL_TILE*2}; channels < ${CHANNEL_TILE*10}; channels += ${CHANNEL_TILE}) {
56      VMulCAddCMicrokernelTester()
57        .channel_tile(${CHANNEL_TILE})
58        .channels(channels)
59        .rows(${ROW_TILE})
60        .Test(${", ".join(TEST_ARGS)});
61    }
62  }
63
64  TEST(${TEST_NAME}, channels_lt_${CHANNEL_TILE}) {
65    $if ISA_CHECK:
66      ${ISA_CHECK};
67    for (size_t channels = 1; channels < ${CHANNEL_TILE}; channels++) {
68      VMulCAddCMicrokernelTester()
69        .channel_tile(${CHANNEL_TILE})
70        .channels(channels)
71        .rows(${ROW_TILE})
72        .Test(${", ".join(TEST_ARGS)});
73    }
74  }
75
76TEST(${TEST_NAME}, channels_gt_${CHANNEL_TILE}) {
77  $if ISA_CHECK:
78    ${ISA_CHECK};
79  for (size_t channels = ${CHANNEL_TILE+1}; channels < ${10 if CHANNEL_TILE == 1 else CHANNEL_TILE*2}; channels++) {
80    VMulCAddCMicrokernelTester()
81      .channel_tile(${CHANNEL_TILE})
82      .channels(channels)
83      .rows(${ROW_TILE})
84      .Test(${", ".join(TEST_ARGS)});
85  }
86}
87
88$if ROW_TILE > 1:
89  TEST(${TEST_NAME}, rows_lt_${ROW_TILE}) {
90    $if ISA_CHECK:
91      ${ISA_CHECK};
92    for (size_t rows = 1; rows < ${ROW_TILE}; rows++) {
93      for (size_t channels = 1; channels <= ${CHANNEL_TILE*5}; channels += ${max(1, CHANNEL_TILE-1)}) {
94        VMulCAddCMicrokernelTester()
95          .channel_tile(${CHANNEL_TILE})
96          .channels(channels)
97          .rows(rows)
98          .Test(${", ".join(TEST_ARGS)});
99      }
100    }
101  }
102
103  TEST(${TEST_NAME}, rows_div_${ROW_TILE}) {
104    $if ISA_CHECK:
105      ${ISA_CHECK};
106    for (size_t rows = ${ROW_TILE*2}; rows <= ${ROW_TILE*4}; rows += ${ROW_TILE}) {
107      for (size_t channels = 1; channels <= ${CHANNEL_TILE*5}; channels += ${max(1, CHANNEL_TILE-1)}) {
108        VMulCAddCMicrokernelTester()
109          .channel_tile(${CHANNEL_TILE})
110          .channels(channels)
111          .rows(rows)
112          .Test(${", ".join(TEST_ARGS)});
113      }
114    }
115  }
116
117TEST(${TEST_NAME}, rows_gt_${ROW_TILE}) {
118  $if ISA_CHECK:
119    ${ISA_CHECK};
120  for (size_t rows = ${ROW_TILE+1}; rows < ${ROW_TILE*2}; rows++) {
121    for (size_t channels = 1; channels <= ${CHANNEL_TILE*5}; channels += ${max(1, CHANNEL_TILE-1)}) {
122      VMulCAddCMicrokernelTester()
123        .channel_tile(${CHANNEL_TILE})
124        .channels(channels)
125        .rows(rows)
126        .Test(${", ".join(TEST_ARGS)});
127    }
128  }
129}
130
131TEST(${TEST_NAME}, input_stride) {
132  $if ISA_CHECK:
133    ${ISA_CHECK};
134  for (size_t rows = 1; rows <= ${ROW_TILE*3}; rows += ${max(1, ROW_TILE-1)}) {
135    for (size_t channels = 1; channels <= ${CHANNEL_TILE*5}; channels += ${max(1, CHANNEL_TILE-1)}) {
136      VMulCAddCMicrokernelTester()
137        .channel_tile(${CHANNEL_TILE})
138        .channels(channels)
139        .rows(rows)
140        .input_stride(${next_prime(CHANNEL_TILE*5+1)})
141        .Test(${", ".join(TEST_ARGS)});
142    }
143  }
144}
145
146TEST(${TEST_NAME}, output_stride) {
147  $if ISA_CHECK:
148    ${ISA_CHECK};
149  for (size_t rows = 1; rows <= ${ROW_TILE*3}; rows += ${max(1, ROW_TILE-1)}) {
150    for (size_t channels = 1; channels <= ${CHANNEL_TILE*5}; channels += ${max(1, CHANNEL_TILE-1)}) {
151      VMulCAddCMicrokernelTester()
152        .channel_tile(${CHANNEL_TILE})
153        .channels(channels)
154        .rows(rows)
155        .output_stride(${next_prime(CHANNEL_TILE*5+1)})
156        .Test(${", ".join(TEST_ARGS)});
157    }
158  }
159}
160
161TEST(${TEST_NAME}, inplace) {
162  $if ISA_CHECK:
163    ${ISA_CHECK};
164  for (size_t rows = 1; rows <= ${ROW_TILE*3}; rows += ${max(1, ROW_TILE-1)}) {
165    for (size_t channels = 1; channels <= ${CHANNEL_TILE*5}; channels += ${max(1, CHANNEL_TILE-1)}) {
166      VMulCAddCMicrokernelTester()
167        .channel_tile(${CHANNEL_TILE})
168        .channels(channels)
169        .rows(rows)
170        .inplace(true)
171        .Test(${", ".join(TEST_ARGS)});
172    }
173  }
174}
175
176TEST(${TEST_NAME}, qmin) {
177  $if ISA_CHECK:
178    ${ISA_CHECK};
179  for (size_t rows = 1; rows <= ${ROW_TILE*3}; rows += ${max(1, ROW_TILE-1)}) {
180    for (size_t channels = 1; channels <= ${CHANNEL_TILE*5}; channels += ${max(1, CHANNEL_TILE-1)}) {
181      VMulCAddCMicrokernelTester()
182        .channel_tile(${CHANNEL_TILE})
183        .channels(channels)
184        .rows(rows)
185        .qmin(128)
186        .Test(${", ".join(TEST_ARGS)});
187    }
188  }
189}
190
191TEST(${TEST_NAME}, qmax) {
192  $if ISA_CHECK:
193    ${ISA_CHECK};
194  for (size_t rows = 1; rows <= ${ROW_TILE*3}; rows += ${max(1, ROW_TILE-1)}) {
195    for (size_t channels = 1; channels <= ${CHANNEL_TILE*5}; channels += ${max(1, CHANNEL_TILE-1)}) {
196      VMulCAddCMicrokernelTester()
197        .channel_tile(${CHANNEL_TILE})
198        .channels(channels)
199        .rows(rows)
200        .qmax(128)
201        .Test(${", ".join(TEST_ARGS)});
202    }
203  }
204}
205"""
206
207
208def generate_test_cases(ukernel, channel_tile, row_tile, init_fn, isa):
209  """Generates all tests cases for a VMULCADDC micro-kernel.
210
211  Args:
212    ukernel: C name of the micro-kernel function.
213    channel_tile: Number of channels processed per one iteration of the inner
214                  loop of the micro-kernel.
215    row_tile: Number of rows processed per one iteration of the outer loop of
216              the micro-kernel.
217    init_fn: C name of the function to initialize microkernel parameters.
218    isa: instruction set required to run the micro-kernel. Generated unit test
219         will skip execution if the host processor doesn't support this ISA.
220
221  Returns:
222    Code for the test case.
223  """
224  _, test_name = ukernel.split("_", 1)
225  _, datatype, ukernel_type, _ = ukernel.split("_", 3)
226  test_args = [ukernel]
227  if init_fn:
228    test_args.append(init_fn)
229  return xngen.preprocess(VMULCADDC_TEST_TEMPLATE, {
230      "TEST_NAME": test_name.upper().replace("UKERNEL_", ""),
231      "TEST_ARGS": test_args,
232      "DATATYPE": datatype,
233      "CHANNEL_TILE": channel_tile,
234      "ROW_TILE": row_tile,
235      "ISA_CHECK": xnncommon.generate_isa_check_macro(isa),
236      "next_prime": next_prime,
237    })
238
239
240def main(args):
241  options = parser.parse_args(args)
242
243  with codecs.open(options.spec, "r", encoding="utf-8") as spechannels_file:
244    spechannels_yaml = yaml.safe_load(spechannels_file)
245    if not isinstance(spechannels_yaml, list):
246      raise ValueError("expected a list of micro-kernels in the spec")
247
248    tests = """\
249// Copyright 2019 Google LLC
250//
251// This source code is licensed under the BSD-style license found in the
252// LICENSE file in the root directory of this source tree.
253//
254// Auto-generated file. Do not edit!
255//   Specification: {specification}
256//   Generator: {generator}
257
258
259#include <gtest/gtest.h>
260
261#include <xnnpack/common.h>
262#include <xnnpack/isa-checks.h>
263
264#include <xnnpack/vmulcaddc.h>
265#include "vmulcaddc-microkernel-tester.h"
266""".format(specification=options.spec, generator=sys.argv[0])
267
268    for ukernel_spec in spechannels_yaml:
269      name = ukernel_spec["name"]
270      init_fn = ukernel_spec.get("init")
271      channel_tile, row_tile, arch, isa = split_ukernel_name(name)
272
273      # specification can override architecture
274      arch = ukernel_spec.get("arch", arch)
275
276      test_case = generate_test_cases(
277        name, channel_tile, row_tile, init_fn, isa)
278      tests += "\n\n" + xnncommon.postprocess_test_case(test_case, arch, isa)
279
280    txt_changed = True
281    if os.path.exists(options.output):
282      with codecs.open(options.output, "r", encoding="utf-8") as output_file:
283        txt_changed = output_file.read() != tests
284
285    if txt_changed:
286      with codecs.open(options.output, "w", encoding="utf-8") as output_file:
287        output_file.write(tests)
288
289
290if __name__ == "__main__":
291  main(sys.argv[1:])
292