xref: /aosp_15_r20/external/XNNPACK/tools/generate-vsquareabs-test.py (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1#!/usr/bin/env python
2# Copyright 2022 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(description='VSquareAbs 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.fullmatch(r"xnn_cs16_vsquareabs_ukernel__(.+)_x(\d+)", name)
31  assert match is not None
32  batch_tile = int(match.group(2))
33
34  arch, isa = xnncommon.parse_target_name(target_name=match.group(1))
35  return batch_tile, arch, isa
36
37
38VSQUAREABS_TEST_TEMPLATE = """\
39TEST(${TEST_NAME}, batch_eq_${BATCH_TILE}) {
40  $if ISA_CHECK:
41    ${ISA_CHECK};
42  VSquareAbsMicrokernelTester()
43    .batch_size(${BATCH_TILE})
44    .Test(${", ".join(TEST_ARGS)});
45}
46
47$if BATCH_TILE > 1:
48  TEST(${TEST_NAME}, batch_div_${BATCH_TILE}) {
49    $if ISA_CHECK:
50      ${ISA_CHECK};
51    for (size_t batch_size = ${BATCH_TILE*2}; batch_size < ${BATCH_TILE*10}; batch_size += ${BATCH_TILE}) {
52      VSquareAbsMicrokernelTester()
53        .batch_size(batch_size)
54        .Test(${", ".join(TEST_ARGS)});
55    }
56  }
57
58  TEST(${TEST_NAME}, batch_lt_${BATCH_TILE}) {
59    $if ISA_CHECK:
60      ${ISA_CHECK};
61    for (size_t batch_size = 1; batch_size < ${BATCH_TILE}; batch_size++) {
62      VSquareAbsMicrokernelTester()
63        .batch_size(batch_size)
64        .Test(${", ".join(TEST_ARGS)});
65    }
66  }
67
68TEST(${TEST_NAME}, batch_gt_${BATCH_TILE}) {
69  $if ISA_CHECK:
70    ${ISA_CHECK};
71  for (size_t batch_size = ${BATCH_TILE+1}; batch_size < ${10 if BATCH_TILE == 1 else BATCH_TILE*2}; batch_size++) {
72    VSquareAbsMicrokernelTester()
73      .batch_size(batch_size)
74      .Test(${", ".join(TEST_ARGS)});
75  }
76}
77
78"""
79
80
81def generate_test_cases(ukernel, batch_tile, isa):
82  """Generates all tests cases for a VSquareAbs micro-kernel.
83
84  Args:
85    ukernel: C name of the micro-kernel function.
86    batch_tile: Number of batch_size processed per one iteration of the inner
87                  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_type, _ = ukernel.split("_", 3)
96  return xngen.preprocess(VSQUAREABS_TEST_TEMPLATE, {
97      "TEST_NAME": test_name.upper().replace("UKERNEL_", ""),
98      "TEST_ARGS": [ukernel],
99      "DATATYPE": datatype,
100      "BATCH_TILE": batch_tile,
101      "ISA_CHECK": xnncommon.generate_isa_check_macro(isa),
102      "next_prime": next_prime,
103    })
104
105
106def main(args):
107  options = parser.parse_args(args)
108
109  with codecs.open(options.spec, "r", encoding="utf-8") as spec_file:
110    spec_yaml = yaml.safe_load(spec_file)
111    if not isinstance(spec_yaml, list):
112      raise ValueError("expected a list of micro-kernels in the spec")
113
114    tests = """\
115// Copyright 2022 Google LLC
116//
117// This source code is licensed under the BSD-style license found in the
118// LICENSE file in the root directory of this source tree.
119//
120// Auto-generated file. Do not edit!
121//   Specification: {specification}
122//   Generator: {generator}
123
124
125#include <gtest/gtest.h>
126
127#include <xnnpack/common.h>
128#include <xnnpack/isa-checks.h>
129
130#include <xnnpack/vsquareabs.h>
131#include "vsquareabs-microkernel-tester.h"
132""".format(specification=options.spec, generator=sys.argv[0])
133
134    for ukernel_spec in spec_yaml:
135      name = ukernel_spec["name"]
136      batch_tile, arch, isa = split_ukernel_name(name)
137
138      # specification can override architecture
139      arch = ukernel_spec.get("arch", arch)
140
141      test_case = generate_test_cases(name, batch_tile, isa)
142      tests += "\n\n" + xnncommon.postprocess_test_case(test_case, arch, isa)
143
144    txt_changed = True
145    if os.path.exists(options.output):
146      with codecs.open(options.output, "r", encoding="utf-8") as output_file:
147        txt_changed = output_file.read() != tests
148
149    if txt_changed:
150      with codecs.open(options.output, "w", encoding="utf-8") as output_file:
151        output_file.write(tests)
152
153
154if __name__ == "__main__":
155  main(sys.argv[1:])
156