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