• 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
168TEST(${TEST_NAME}, qmin) {
169  $if ISA_CHECK:
170    ${ISA_CHECK};
171  for (size_t rows = 1; rows <= ${ROW_TILE*3}; rows += ${max(1, ROW_TILE-1)}) {
172    for (size_t channels = 1; channels <= ${CHANNEL_TILE*5}; channels += ${max(1, CHANNEL_TILE-1)}) {
173      PReLUMicrokernelTester()
174        .rows(rows)
175        .channels(channels)
176        .qmin(128)
177        .iterations(1)
178        .Test(${", ".join(TEST_ARGS)});
179    }
180  }
181}
182
183TEST(${TEST_NAME}, qmax) {
184  $if ISA_CHECK:
185    ${ISA_CHECK};
186  for (size_t rows = 1; rows <= ${ROW_TILE*3}; rows += ${max(1, ROW_TILE-1)}) {
187    for (size_t channels = 1; channels <= ${CHANNEL_TILE*5}; channels += ${max(1, CHANNEL_TILE-1)}) {
188      PReLUMicrokernelTester()
189        .rows(rows)
190        .channels(channels)
191        .qmax(128)
192        .iterations(1)
193        .Test(${", ".join(TEST_ARGS)});
194    }
195  }
196}
197"""
198
199
200def generate_test_cases(ukernel, row_tile, channel_tile, isa):
201  """Generates all tests cases for a PRELU micro-kernel.
202
203  Args:
204    ukernel: C name of the micro-kernel function.
205    row_tile: Number of rows (pixels) processed per one iteration of the outer
206              loop of the micro-kernel.
207    channel_tile: Number of channels processed per one iteration of the inner
208                  loop of the micro-kernel.
209    isa: instruction set required to run the micro-kernel. Generated unit test
210         will skip execution if the host processor doesn't support this ISA.
211
212  Returns:
213    Code for the test case.
214  """
215  _, test_name = ukernel.split("_", 1)
216  _, datatype, ukernel_type, _ = ukernel.split("_", 3)
217  test_args = [ukernel]
218  if not isa or isa == "psimd":
219    test_args.append("PReLUMicrokernelTester::Variant::Scalar")
220  return xngen.preprocess(PRELU_TEST_TEMPLATE, {
221      "TEST_NAME": test_name.upper().replace("UKERNEL_", ""),
222      "TEST_ARGS": test_args,
223      "DATATYPE": datatype,
224      "ROW_TILE": row_tile,
225      "CHANNEL_TILE": channel_tile,
226      "ISA_CHECK": xnncommon.generate_isa_check_macro(isa),
227      "next_prime": next_prime,
228    })
229
230
231def main(args):
232  options = parser.parse_args(args)
233
234  with codecs.open(options.spec, "r", encoding="utf-8") as spec_file:
235    spec_yaml = yaml.safe_load(spec_file)
236    if not isinstance(spec_yaml, list):
237      raise ValueError("expected a list of micro-kernels in the spec")
238
239    tests = """\
240// Copyright 2019 Google LLC
241//
242// This source code is licensed under the BSD-style license found in the
243// LICENSE file in the root directory of this source tree.
244//
245// Auto-generated file. Do not edit!
246//   Specification: {specification}
247//   Generator: {generator}
248
249
250#include <gtest/gtest.h>
251
252#include <xnnpack/common.h>
253#include <xnnpack/isa-checks.h>
254
255#include <xnnpack/prelu.h>
256#include "prelu-microkernel-tester.h"
257""".format(specification=options.spec, generator=sys.argv[0])
258
259    for ukernel_spec in spec_yaml:
260      name = ukernel_spec["name"]
261      row_tile, channel_tile, arch, isa = split_ukernel_name(name)
262
263      # specification can override architecture
264      arch = ukernel_spec.get("arch", arch)
265
266      test_case = generate_test_cases(name, row_tile, channel_tile, isa)
267      tests += "\n\n" + xnncommon.postprocess_test_case(test_case, arch, isa)
268
269    with codecs.open(options.output, "w", encoding="utf-8") as output_file:
270      output_file.write(tests)
271
272
273if __name__ == "__main__":
274  main(sys.argv[1:])
275