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