xref: /aosp_15_r20/external/XNNPACK/tools/generate-vlog-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='VLog 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_u32_vlog_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
38VLOG_TEST_TEMPLATE = """\
39TEST(${TEST_NAME}, DISABLED_batch_eq_${BATCH_TILE}) {
40  $if ISA_CHECK:
41    ${ISA_CHECK};
42  VLogMicrokernelTester()
43    .batch(${BATCH_TILE})
44    .Test(${", ".join(TEST_ARGS)});
45}
46
47$if BATCH_TILE > 1:
48  TEST(${TEST_NAME}, DISABLED_batch_div_${BATCH_TILE}) {
49    $if ISA_CHECK:
50      ${ISA_CHECK};
51    for (size_t batch = ${BATCH_TILE*2}; batch < ${BATCH_TILE*10}; batch += ${BATCH_TILE}) {
52      VLogMicrokernelTester()
53        .batch(batch)
54        .Test(${", ".join(TEST_ARGS)});
55    }
56  }
57
58  TEST(${TEST_NAME}, DISABLED_batch_lt_${BATCH_TILE}) {
59    $if ISA_CHECK:
60      ${ISA_CHECK};
61    for (size_t batch = 1; batch < ${BATCH_TILE}; batch++) {
62      VLogMicrokernelTester()
63        .batch(batch)
64        .Test(${", ".join(TEST_ARGS)});
65    }
66  }
67
68TEST(${TEST_NAME}, DISABLED_batch_gt_${BATCH_TILE}) {
69  $if ISA_CHECK:
70    ${ISA_CHECK};
71  for (size_t batch = ${BATCH_TILE+1}; batch < ${10 if BATCH_TILE == 1 else BATCH_TILE*2}; batch++) {
72    VLogMicrokernelTester()
73      .batch(batch)
74      .Test(${", ".join(TEST_ARGS)});
75  }
76}
77
78TEST(${TEST_NAME}, DISABLED_input_lshift) {
79  $if ISA_CHECK:
80    ${ISA_CHECK};
81  for (uint32_t input_lshift = 0; input_lshift < 32; input_lshift++) {
82    VLogMicrokernelTester()
83      .batch(${BATCH_TILE})
84      .input_lshift(input_lshift)
85      .Test(${", ".join(TEST_ARGS)});
86  }
87}
88
89TEST(${TEST_NAME}, DISABLED_output_scale) {
90  $if ISA_CHECK:
91    ${ISA_CHECK};
92  for (uint32_t output_scale = 0; output_scale < 65536; output_scale += ${next_prime(BATCH_TILE + 1)}) {
93    VLogMicrokernelTester()
94      .batch(${BATCH_TILE})
95      .output_scale(output_scale)
96      .Test(${", ".join(TEST_ARGS)});
97  }
98}
99
100TEST(${TEST_NAME}, DISABLED_inplace) {
101  $if ISA_CHECK:
102    ${ISA_CHECK};
103  for (size_t batch = ${BATCH_TILE+1}; batch < ${10 if BATCH_TILE == 1 else BATCH_TILE*2}; batch++) {
104    VLogMicrokernelTester()
105      .batch(batch)
106      .inplace(true)
107      .Test(${", ".join(TEST_ARGS)});
108  }
109}
110
111"""
112
113
114def generate_test_cases(ukernel, batch_tile, isa):
115  """Generates all tests cases for a VLog micro-kernel.
116
117  Args:
118    ukernel: C name of the micro-kernel function.
119    batch_tile: Number of batch processed per one iteration of the inner
120                  loop of the micro-kernel.
121    isa: instruction set required to run the micro-kernel. Generated unit test
122         will skip execution if the host processor doesn't support this ISA.
123
124  Returns:
125    Code for the test case.
126  """
127  _, test_name = ukernel.split("_", 1)
128  _, datatype, ukernel_type, _ = ukernel.split("_", 3)
129  return xngen.preprocess(VLOG_TEST_TEMPLATE, {
130      "TEST_NAME": test_name.upper().replace("UKERNEL_", ""),
131      "TEST_ARGS": [ukernel],
132      "DATATYPE": datatype,
133      "BATCH_TILE": batch_tile,
134      "ISA_CHECK": xnncommon.generate_isa_check_macro(isa),
135      "next_prime": next_prime,
136    })
137
138
139def main(args):
140  options = parser.parse_args(args)
141
142  with codecs.open(options.spec, "r", encoding="utf-8") as spec_file:
143    spec_yaml = yaml.safe_load(spec_file)
144    if not isinstance(spec_yaml, list):
145      raise ValueError("expected a list of micro-kernels in the spec")
146
147    tests = """\
148// Copyright 2022 Google LLC
149//
150// This source code is licensed under the BSD-style license found in the
151// LICENSE file in the root directory of this source tree.
152//
153// Auto-generated file. Do not edit!
154//   Specification: {specification}
155//   Generator: {generator}
156
157
158#include <gtest/gtest.h>
159
160#include <xnnpack/common.h>
161#include <xnnpack/isa-checks.h>
162
163#include <xnnpack/vlog.h>
164#include "vlog-microkernel-tester.h"
165""".format(specification=options.spec, generator=sys.argv[0])
166
167    for ukernel_spec in spec_yaml:
168      name = ukernel_spec["name"]
169      batch_tile, arch, isa = split_ukernel_name(name)
170
171      # specification can override architecture
172      arch = ukernel_spec.get("arch", arch)
173
174      test_case = generate_test_cases(name, batch_tile, isa)
175      tests += "\n\n" + xnncommon.postprocess_test_case(test_case, arch, isa)
176
177    txt_changed = True
178    if os.path.exists(options.output):
179      with codecs.open(options.output, "r", encoding="utf-8") as output_file:
180        txt_changed = output_file.read() != tests
181
182    if txt_changed:
183      with codecs.open(options.output, "w", encoding="utf-8") as output_file:
184        output_file.write(tests)
185
186
187if __name__ == "__main__":
188  main(sys.argv[1:])
189