• 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__)))
16from primes import next_prime
17import xngen
18import xnncommon
19
20
21parser = argparse.ArgumentParser(description='PReLU 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)_prelu_ukernel__(.+)_(\d+)x(\d+)$", name)
31  assert match is not None
32  row_tile = int(match.group(3))
33  channel_tile = int(match.group(4))
34
35  arch, isa = xnncommon.parse_target_name(target_name=match.group(2))
36  return row_tile, channel_tile, arch, isa
37
38
39PRELU_TEST_TEMPLATE = """\
40TEST(${TEST_NAME}, channels_eq_${CHANNEL_TILE}) {
41  $if ISA_CHECK:
42    ${ISA_CHECK};
43  PReLUMicrokernelTester()
44    .rows(${ROW_TILE})
45    .channels(${CHANNEL_TILE})
46    .Test(${", ".join(TEST_ARGS)});
47}
48
49$if CHANNEL_TILE > 1:
50  TEST(${TEST_NAME}, channels_div_${CHANNEL_TILE}) {
51    $if ISA_CHECK:
52      ${ISA_CHECK};
53    for (size_t channels = ${CHANNEL_TILE*2}; channels < ${CHANNEL_TILE*10}; channels += ${CHANNEL_TILE}) {
54      PReLUMicrokernelTester()
55        .rows(${ROW_TILE})
56        .channels(channels)
57        .Test(${", ".join(TEST_ARGS)});
58    }
59  }
60
61  TEST(${TEST_NAME}, channels_lt_${CHANNEL_TILE}) {
62    $if ISA_CHECK:
63      ${ISA_CHECK};
64    for (size_t channels = 1; channels < ${CHANNEL_TILE}; channels++) {
65      PReLUMicrokernelTester()
66        .rows(${ROW_TILE})
67        .channels(channels)
68        .Test(${", ".join(TEST_ARGS)});
69    }
70  }
71
72TEST(${TEST_NAME}, channels_gt_${CHANNEL_TILE}) {
73  $if ISA_CHECK:
74    ${ISA_CHECK};
75  for (size_t channels = ${CHANNEL_TILE+1}; channels < ${10 if CHANNEL_TILE == 1 else CHANNEL_TILE*2}; channels++) {
76    PReLUMicrokernelTester()
77      .rows(${ROW_TILE})
78      .channels(channels)
79      .Test(${", ".join(TEST_ARGS)});
80  }
81}
82
83$if ROW_TILE > 1:
84  TEST(${TEST_NAME}, rows_lt_${ROW_TILE}) {
85    $if ISA_CHECK:
86      ${ISA_CHECK};
87    for (size_t rows = 1; rows < ${ROW_TILE}; rows++) {
88      for (size_t channels = 1; channels <= ${CHANNEL_TILE*5}; channels += ${max(1, CHANNEL_TILE-1)}) {
89        PReLUMicrokernelTester()
90          .rows(rows)
91          .channels(channels)
92          .Test(${", ".join(TEST_ARGS)});
93      }
94    }
95  }
96
97  TEST(${TEST_NAME}, rows_div_${ROW_TILE}) {
98    $if ISA_CHECK:
99      ${ISA_CHECK};
100    for (size_t rows = ${ROW_TILE*2}; rows <= ${ROW_TILE*4}; rows += ${ROW_TILE}) {
101      for (size_t channels = 1; channels <= ${CHANNEL_TILE*5}; channels += ${max(1, CHANNEL_TILE-1)}) {
102        PReLUMicrokernelTester()
103          .rows(rows)
104          .channels(channels)
105          .Test(${", ".join(TEST_ARGS)});
106      }
107    }
108  }
109
110TEST(${TEST_NAME}, rows_gt_${ROW_TILE}) {
111  $if ISA_CHECK:
112    ${ISA_CHECK};
113  for (size_t rows = ${ROW_TILE+1}; rows < ${ROW_TILE*2}; rows++) {
114    for (size_t channels = 1; channels <= ${CHANNEL_TILE*5}; channels += ${max(1, CHANNEL_TILE-1)}) {
115      PReLUMicrokernelTester()
116        .rows(rows)
117        .channels(channels)
118        .Test(${", ".join(TEST_ARGS)});
119    }
120  }
121}
122
123TEST(${TEST_NAME}, input_stride) {
124  $if ISA_CHECK:
125    ${ISA_CHECK};
126  for (size_t rows = 1; rows <= ${ROW_TILE*3}; rows += ${max(1, ROW_TILE-1)}) {
127    for (size_t channels = 1; channels <= ${CHANNEL_TILE*5}; channels += ${max(1, CHANNEL_TILE-1)}) {
128      PReLUMicrokernelTester()
129        .rows(rows)
130        .channels(channels)
131        .input_stride(${next_prime(CHANNEL_TILE*5+1)})
132        .iterations(1)
133        .Test(${", ".join(TEST_ARGS)});
134    }
135  }
136}
137
138TEST(${TEST_NAME}, output_stride) {
139  $if ISA_CHECK:
140    ${ISA_CHECK};
141  for (size_t rows = 1; rows <= ${ROW_TILE*3}; rows += ${max(1, ROW_TILE-1)}) {
142    for (size_t channels = 1; channels <= ${CHANNEL_TILE*5}; channels += ${max(1, CHANNEL_TILE-1)}) {
143      PReLUMicrokernelTester()
144        .rows(rows)
145        .channels(channels)
146        .output_stride(${next_prime(CHANNEL_TILE*5+1)})
147        .iterations(1)
148        .Test(${", ".join(TEST_ARGS)});
149    }
150  }
151}
152
153TEST(${TEST_NAME}, inplace) {
154  $if ISA_CHECK:
155    ${ISA_CHECK};
156  for (size_t rows = 1; rows <= ${ROW_TILE*3}; rows += ${max(1, ROW_TILE-1)}) {
157    for (size_t channels = 1; channels <= ${CHANNEL_TILE*5}; channels += ${max(1, CHANNEL_TILE-1)}) {
158      PReLUMicrokernelTester()
159        .rows(rows)
160        .channels(channels)
161        .inplace(true)
162        .iterations(1)
163        .Test(${", ".join(TEST_ARGS)});
164    }
165  }
166}
167"""
168
169
170def generate_test_cases(ukernel, row_tile, channel_tile, isa):
171  """Generates all tests cases for a PRELU micro-kernel.
172
173  Args:
174    ukernel: C name of the micro-kernel function.
175    row_tile: Number of rows (pixels) processed per one iteration of the outer
176              loop of the micro-kernel.
177    channel_tile: Number of channels processed per one iteration of the inner
178                  loop of the micro-kernel.
179    isa: instruction set required to run the micro-kernel. Generated unit test
180         will skip execution if the host processor doesn't support this ISA.
181
182  Returns:
183    Code for the test case.
184  """
185  _, test_name = ukernel.split("_", 1)
186  _, datatype, ukernel_type, _ = ukernel.split("_", 3)
187  return xngen.preprocess(PRELU_TEST_TEMPLATE, {
188      "TEST_NAME": test_name.upper().replace("UKERNEL_", ""),
189      "TEST_ARGS": [ukernel],
190      "DATATYPE": datatype,
191      "ROW_TILE": row_tile,
192      "CHANNEL_TILE": channel_tile,
193      "ISA_CHECK": xnncommon.generate_isa_check_macro(isa),
194      "next_prime": next_prime,
195    })
196
197
198def main(args):
199  options = parser.parse_args(args)
200
201  with codecs.open(options.spec, "r", encoding="utf-8") as spec_file:
202    spec_yaml = yaml.safe_load(spec_file)
203    if not isinstance(spec_yaml, list):
204      raise ValueError("expected a list of micro-kernels in the spec")
205
206    tests = """\
207// Copyright 2019 Google LLC
208//
209// This source code is licensed under the BSD-style license found in the
210// LICENSE file in the root directory of this source tree.
211//
212// Auto-generated file. Do not edit!
213//   Specification: {specification}
214//   Generator: {generator}
215
216
217#include <gtest/gtest.h>
218
219#include <xnnpack/common.h>
220#include <xnnpack/isa-checks.h>
221
222#include <xnnpack/prelu.h>
223#include "prelu-microkernel-tester.h"
224""".format(specification=options.spec, generator=sys.argv[0])
225
226    for ukernel_spec in spec_yaml:
227      name = ukernel_spec["name"]
228      row_tile, channel_tile, arch, isa = split_ukernel_name(name)
229
230      # specification can override architecture
231      arch = ukernel_spec.get("arch", arch)
232
233      test_case = generate_test_cases(name, row_tile, channel_tile, isa)
234      tests += "\n\n" + xnncommon.postprocess_test_case(test_case, arch, isa)
235
236    with codecs.open(options.output, "w", encoding="utf-8") as output_file:
237      output_file.write(tests)
238
239
240if __name__ == "__main__":
241  main(sys.argv[1:])
242