• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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='Filterbank Subtract 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_filterbank_subtract_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
38FILTERBANK_SUBTRACT_TEST_TEMPLATE = """\
39TEST(${TEST_NAME}, batch_eq_${BATCH_TILE}) {
40  $if ISA_CHECK:
41    ${ISA_CHECK};
42  FilterbankSubtractMicrokernelTester()
43    .batch(${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 = ${BATCH_TILE*2}; batch < ${BATCH_TILE*10}; batch += ${BATCH_TILE}) {
52      FilterbankSubtractMicrokernelTester()
53        .batch(batch)
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 = 2; batch < ${BATCH_TILE}; batch += 2) {
62      FilterbankSubtractMicrokernelTester()
63        .batch(batch)
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 = ${BATCH_TILE+2}; batch < ${10 if BATCH_TILE == 1 else BATCH_TILE*2}; batch += 2) {
72    FilterbankSubtractMicrokernelTester()
73      .batch(batch)
74      .Test(${", ".join(TEST_ARGS)});
75  }
76}
77
78TEST(${TEST_NAME}, inplace) {
79  $if ISA_CHECK:
80    ${ISA_CHECK};
81  for (size_t batch = ${BATCH_TILE+2}; batch < ${10 if BATCH_TILE == 1 else BATCH_TILE*2}; batch += 2) {
82    FilterbankSubtractMicrokernelTester()
83      .batch(batch)
84      .inplace(true)
85      .Test(${", ".join(TEST_ARGS)});
86  }
87}
88"""
89
90
91def generate_test_cases(ukernel, batch_tile, isa):
92  """Generates all tests cases for a Filterbank Subtract micro-kernel.
93
94  Args:
95    ukernel: C name of the micro-kernel function.
96    batch_tile: Number of batch processed per one iteration of the inner
97                  loop of the micro-kernel.
98    isa: instruction set required to run the micro-kernel. Generated unit test
99         will skip execution if the host processor doesn't support this ISA.
100
101  Returns:
102    Code for the test case.
103  """
104  _, test_name = ukernel.split("_", 1)
105  _, datatype, ukernel_type, _ = ukernel.split("_", 3)
106  return xngen.preprocess(FILTERBANK_SUBTRACT_TEST_TEMPLATE, {
107      "TEST_NAME": test_name.upper().replace("UKERNEL_", ""),
108      "TEST_ARGS": [ukernel],
109      "DATATYPE": datatype,
110      "BATCH_TILE": batch_tile,
111      "ISA_CHECK": xnncommon.generate_isa_check_macro(isa),
112      "next_prime": next_prime,
113    })
114
115
116def main(args):
117  options = parser.parse_args(args)
118
119  with codecs.open(options.spec, "r", encoding="utf-8") as spec_file:
120    spec_yaml = yaml.safe_load(spec_file)
121    if not isinstance(spec_yaml, list):
122      raise ValueError("expected a list of micro-kernels in the spec")
123
124    tests = """\
125// Copyright 2022 Google LLC
126//
127// This source code is licensed under the BSD-style license found in the
128// LICENSE file in the root directory of this source tree.
129//
130// Auto-generated file. Do not edit!
131//   Specification: {specification}
132//   Generator: {generator}
133
134
135#include <gtest/gtest.h>
136
137#include <xnnpack/common.h>
138#include <xnnpack/isa-checks.h>
139
140#include <xnnpack/filterbank.h>
141#include "filterbank-subtract-microkernel-tester.h"
142""".format(specification=options.spec, generator=sys.argv[0])
143
144    for ukernel_spec in spec_yaml:
145      name = ukernel_spec["name"]
146      batch_tile, arch, isa = split_ukernel_name(name)
147
148      # specification can override architecture
149      arch = ukernel_spec.get("arch", arch)
150
151      test_case = generate_test_cases(name, batch_tile, isa)
152      tests += "\n\n" + xnncommon.postprocess_test_case(test_case, arch, isa)
153
154    txt_changed = True
155    if os.path.exists(options.output):
156      with codecs.open(options.output, "r", encoding="utf-8") as output_file:
157        txt_changed = output_file.read() != tests
158
159    if txt_changed:
160      with codecs.open(options.output, "w", encoding="utf-8") as output_file:
161        output_file.write(tests)
162
163
164if __name__ == "__main__":
165  main(sys.argv[1:])
166