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 Accumulate 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_accumulate_ukernel__(.+)(_x(\d+))?", name) 31 assert match is not None 32 row_tile = 1 33 batch_tile = 1 34 if match.group(3): 35 batch_tile = int(match.group(3)) 36 37 arch, isa = xnncommon.parse_target_name(target_name=match.group(1)) 38 return row_tile, batch_tile, arch, isa 39 40 41FILTERBANK_ACCUMULATE_TEST_TEMPLATE = """\ 42TEST(${TEST_NAME}, rows_eq_1) { 43 $if ISA_CHECK: 44 ${ISA_CHECK}; 45 FilterbankAccumulateMicrokernelTester() 46 .rows(1) 47 .Test(${", ".join(TEST_ARGS)}); 48} 49 50TEST(${TEST_NAME}, rows_gt_1) { 51 $if ISA_CHECK: 52 ${ISA_CHECK}; 53 for (size_t rows = 2; rows < 10; rows++) { 54 FilterbankAccumulateMicrokernelTester() 55 .rows(rows) 56 .Test(${", ".join(TEST_ARGS)}); 57 } 58} 59""" 60 61 62def generate_test_cases(ukernel, row_tile, batch_tile, isa): 63 """Generates all tests cases for a Filterbank Accumulate micro-kernel. 64 65 Args: 66 ukernel: C name of the micro-kernel function. 67 row_tile: Number of rows (pixels) processed per one iteration of the outer 68 loop of the micro-kernel. 69 batch_tile: Number of batch processed per one iteration of the inner 70 loop of the micro-kernel. 71 isa: instruction set required to run the micro-kernel. Generated unit test 72 will skip execution if the host processor doesn't support this ISA. 73 74 Returns: 75 Code for the test case. 76 """ 77 _, test_name = ukernel.split("_", 1) 78 _, datatype, ukernel_type, _ = ukernel.split("_", 3) 79 return xngen.preprocess(FILTERBANK_ACCUMULATE_TEST_TEMPLATE, { 80 "TEST_NAME": test_name.upper().replace("UKERNEL_", ""), 81 "TEST_ARGS": [ukernel], 82 "DATATYPE": datatype, 83 "ROW_TILE": row_tile, 84 "BATCH_TILE": batch_tile, 85 "ISA_CHECK": xnncommon.generate_isa_check_macro(isa), 86 "next_prime": next_prime, 87 }) 88 89 90def main(args): 91 options = parser.parse_args(args) 92 93 with codecs.open(options.spec, "r", encoding="utf-8") as spec_file: 94 spec_yaml = yaml.safe_load(spec_file) 95 if not isinstance(spec_yaml, list): 96 raise ValueError("expected a list of micro-kernels in the spec") 97 98 tests = """\ 99// Copyright 2022 Google LLC 100// 101// This source code is licensed under the BSD-style license found in the 102// LICENSE file in the root directory of this source tree. 103// 104// Auto-generated file. Do not edit! 105// Specification: {specification} 106// Generator: {generator} 107 108 109#include <gtest/gtest.h> 110 111#include <xnnpack/common.h> 112#include <xnnpack/isa-checks.h> 113 114#include <xnnpack/filterbank.h> 115#include "filterbank-accumulate-microkernel-tester.h" 116""".format(specification=options.spec, generator=sys.argv[0]) 117 118 for ukernel_spec in spec_yaml: 119 name = ukernel_spec["name"] 120 row_tile, batch_tile, arch, isa = split_ukernel_name(name) 121 122 # specification can override architecture 123 arch = ukernel_spec.get("arch", arch) 124 125 test_case = generate_test_cases(name, row_tile, batch_tile, isa) 126 tests += "\n\n" + xnncommon.postprocess_test_case(test_case, arch, isa) 127 128 txt_changed = True 129 if os.path.exists(options.output): 130 with codecs.open(options.output, "r", encoding="utf-8") as output_file: 131 txt_changed = output_file.read() != tests 132 133 if txt_changed: 134 with codecs.open(options.output, "w", encoding="utf-8") as output_file: 135 output_file.write(tests) 136 137 138if __name__ == "__main__": 139 main(sys.argv[1:]) 140