• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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__)))
16import xngen
17import xnncommon
18
19
20parser = argparse.ArgumentParser(
21  description='Vector unary operation 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_(f16|f32)_(sigmoid)_(fact_)?ukernel__(.+)_x(\d+)$", name)
31  if match is None:
32    raise ValueError("Unexpected microkernel name: " + name)
33  op_type = {
34    "sigmoid": "Sigmoid",
35  }[match.group(2)]
36  batch_tile = int(match.group(5))
37
38  arch, isa = xnncommon.parse_target_name(target_name=match.group(4))
39  return op_type, batch_tile, arch, isa
40
41
42BINOP_TEST_TEMPLATE = """\
43TEST(${TEST_NAME}, batch_eq_${BATCH_TILE}) {
44  $if ISA_CHECK:
45    ${ISA_CHECK};
46  VUnOpMicrokernelTester()
47    .batch_size(${BATCH_TILE})
48    .Test(${", ".join(TEST_ARGS)});
49}
50
51$if BATCH_TILE > 1:
52  TEST(${TEST_NAME}, batch_div_${BATCH_TILE}) {
53    $if ISA_CHECK:
54      ${ISA_CHECK};
55    for (size_t batch_size = ${BATCH_TILE*2}; batch_size < ${BATCH_TILE*10}; batch_size += ${BATCH_TILE}) {
56      VUnOpMicrokernelTester()
57        .batch_size(batch_size)
58        .Test(${", ".join(TEST_ARGS)});
59    }
60  }
61
62  TEST(${TEST_NAME}, batch_lt_${BATCH_TILE}) {
63    $if ISA_CHECK:
64      ${ISA_CHECK};
65    for (size_t batch_size = 1; batch_size < ${BATCH_TILE}; batch_size++) {
66      VUnOpMicrokernelTester()
67        .batch_size(batch_size)
68        .Test(${", ".join(TEST_ARGS)});
69    }
70  }
71
72TEST(${TEST_NAME}, batch_gt_${BATCH_TILE}) {
73  $if ISA_CHECK:
74    ${ISA_CHECK};
75  for (size_t batch_size = ${BATCH_TILE+1}; batch_size < ${10 if BATCH_TILE == 1 else BATCH_TILE*2}; batch_size++) {
76    VUnOpMicrokernelTester()
77      .batch_size(batch_size)
78      .Test(${", ".join(TEST_ARGS)});
79  }
80}
81
82TEST(${TEST_NAME}, inplace) {
83  $if ISA_CHECK:
84    ${ISA_CHECK};
85  for (size_t batch_size = 1; batch_size <= ${BATCH_TILE*5}; batch_size += ${max(1, BATCH_TILE-1)}) {
86    VUnOpMicrokernelTester()
87      .batch_size(batch_size)
88      .inplace(true)
89      .Test(${", ".join(TEST_ARGS)});
90  }
91}
92"""
93
94
95def generate_test_cases(ukernel, op_type, batch_tile, isa):
96  """Generates all tests cases for a Vector Unary Operation micro-kernel.
97
98  Args:
99    ukernel: C name of the micro-kernel function.
100    op_type: Operation type.
101    batch_tile: Number of batch elements processed per one iteration of the
102                inner loop of the micro-kernel.
103    isa: instruction set required to run the micro-kernel. Generated unit test
104         will skip execution if the host processor doesn't support this ISA.
105
106  Returns:
107    Code for the test case.
108  """
109  _, test_name = ukernel.split("_", 1)
110  _, datatype, _ = ukernel.split("_", 2)
111  test_args = [
112    ukernel,
113    "VUnOpMicrokernelTester::OpType::%s" % op_type,
114  ]
115  if not isa or isa == "psimd":
116    test_args.append("VUnOpMicrokernelTester::Variant::Scalar")
117  return xngen.preprocess(BINOP_TEST_TEMPLATE, {
118      "TEST_NAME": test_name.upper().replace("UKERNEL_", ""),
119      "TEST_ARGS": test_args,
120      "DATATYPE": datatype,
121      "BATCH_TILE": batch_tile,
122      "OP_TYPE": op_type,
123      "ISA_CHECK": xnncommon.generate_isa_check_macro(isa),
124    })
125
126
127def main(args):
128  options = parser.parse_args(args)
129
130  with codecs.open(options.spec, "r", encoding="utf-8") as spec_file:
131    spec_yaml = yaml.safe_load(spec_file)
132    if not isinstance(spec_yaml, list):
133      raise ValueError("expected a list of micro-kernels in the spec")
134
135    tests = """\
136// Copyright 2019 Google LLC
137//
138// This source code is licensed under the BSD-style license found in the
139// LICENSE file in the root directory of this source tree.
140//
141// Auto-generated file. Do not edit!
142//   Specification: {specification}
143//   Generator: {generator}
144
145
146#include <gtest/gtest.h>
147
148#include <xnnpack/common.h>
149#include <xnnpack/isa-checks.h>
150
151#include <xnnpack/vunary.h>
152#include "vunary-microkernel-tester.h"
153""".format(specification=options.spec, generator=sys.argv[0])
154
155    for ukernel_spec in spec_yaml:
156      name = ukernel_spec["name"]
157      op_type, batch_tile, arch, isa = split_ukernel_name(name)
158
159      # specification can override architecture
160      arch = ukernel_spec.get("arch", arch)
161
162      test_case = generate_test_cases(name, op_type, batch_tile, isa)
163      tests += "\n\n" + xnncommon.postprocess_test_case(test_case, arch, isa)
164
165    with codecs.open(options.output, "w", encoding="utf-8") as output_file:
166      output_file.write(tests)
167
168
169if __name__ == "__main__":
170  main(sys.argv[1:])
171