1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
|
#!/usr/bin/env python
################################################################################
# Copyright 2018-2024 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
################################################################################
from __future__ import print_function
import os
import re
import sys
import datetime
import xml.etree.ElementTree as ET
def banner(year_from):
year_now = str(datetime.datetime.now().year)
banner_year = (
year_from if year_now == year_from else "%s-%s" % (year_from, year_now)
)
return """\
/*******************************************************************************
* Copyright %s Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
// DO NOT EDIT, AUTO-GENERATED
// Use this script to update the file: scripts/%s
// clang-format off
""" % (
banner_year,
os.path.basename(__file__),
)
def template(body, year_from):
return "%s%s" % (banner(year_from), body)
def header(body):
return (
"""\
#ifndef ONEAPI_DNNL_DNNL_DEBUG_H
#define ONEAPI_DNNL_DNNL_DEBUG_H
/// @file
/// Debug capabilities
#include "oneapi/dnnl/dnnl_config.h"
#include "oneapi/dnnl/dnnl_types.h"
#ifdef __cplusplus
extern "C" {
#endif
%s
const char DNNL_API *dnnl_runtime2str(unsigned v);
const char DNNL_API *dnnl_fmt_kind2str(dnnl_format_kind_t v);
#ifdef __cplusplus
}
#endif
#endif
"""
% body
)
def source(body):
return (
"""\
#include <assert.h>
#include "oneapi/dnnl/dnnl_debug.h"
#include "oneapi/dnnl/dnnl_types.h"
#include "common/c_types_map.hpp"
%s
"""
% body
)
def header_benchdnn(body):
return (
"""\
#ifndef DNNL_DEBUG_HPP
#define DNNL_DEBUG_HPP
#include "oneapi/dnnl/dnnl.h"
%s
/* status */
const char *status2str(dnnl_status_t status);
/* data type */
const char *dt2str(dnnl_data_type_t dt);
/* format */
const char *fmt_tag2str(dnnl_format_tag_t tag);
/* encoding */
#ifdef DNNL_EXPERIMENTAL_SPARSE
const char *sparse_encoding2str(dnnl_sparse_encoding_t encoding);
#endif
/* engine kind */
const char *engine_kind2str(dnnl_engine_kind_t kind);
/* scratchpad mode */
const char *scratchpad_mode2str(dnnl_scratchpad_mode_t mode);
/* fpmath mode */
const char *fpmath_mode2str(dnnl_fpmath_mode_t mode);
/* accumulation mode */
const char *accumulation_mode2str(dnnl_accumulation_mode_t mode);
/* rounding mode */
const char *rounding_mode2str(dnnl_rounding_mode_t mode);
#endif
"""
% body
)
def source_benchdnn(body):
return (
"""\
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "oneapi/dnnl/dnnl_debug.h"
#include "dnnl_debug.hpp"
#include "src/common/z_magic.hpp"
%s
const char *status2str(dnnl_status_t status) {
return dnnl_status2str(status);
}
const char *dt2str(dnnl_data_type_t dt) {
return dnnl_dt2str(dt);
}
const char *fmt_tag2str(dnnl_format_tag_t tag) {
return dnnl_fmt_tag2str(tag);
}
#ifdef DNNL_EXPERIMENTAL_SPARSE
const char *sparse_encoding2str(dnnl_sparse_encoding_t encoding) {
return dnnl_sparse_encoding2str(encoding);
}
#endif
const char *engine_kind2str(dnnl_engine_kind_t kind) {
return dnnl_engine_kind2str(kind);
}
const char *scratchpad_mode2str(dnnl_scratchpad_mode_t mode) {
return dnnl_scratchpad_mode2str(mode);
}
const char *fpmath_mode2str(dnnl_fpmath_mode_t mode) {
return dnnl_fpmath_mode2str(mode);
}
const char *accumulation_mode2str(dnnl_accumulation_mode_t mode) {
return dnnl_accumulation_mode2str(mode);
}
const char *rounding_mode2str(dnnl_rounding_mode_t mode) {
return dnnl_rounding_mode2str(mode);
}
"""
% body.rstrip()
)
def maybe_skip(enum):
return enum in (
"dnnl_memory_extra_flags_t",
"dnnl_normalization_flags_t",
"dnnl_query_t",
"dnnl_rnn_cell_flags_t",
"dnnl_stream_flags_t",
"dnnl_format_kind_t",
)
def enum_abbrev(enum):
def_enum = re.sub(r"^dnnl_", "", enum)
def_enum = re.sub(r"_t$", "", def_enum)
return {
"dnnl_data_type_t": "dt",
"dnnl_format_tag_t": "fmt_tag",
"dnnl_primitive_kind_t": "prim_kind",
"dnnl_engine_kind_t": "engine_kind",
}.get(enum, def_enum)
def sanitize_value(v):
if "undef" in v:
return "undef"
if "any" in v:
return "any"
v = v.split("dnnl_fpmath_mode_")[-1]
v = v.split("dnnl_accumulation_mode_")[-1]
v = v.split("dnnl_rounding_mode_")[-1]
v = v.split("dnnl_scratchpad_mode_")[-1]
v = v.split("dnnl_")[-1]
return v
def func_to_str_decl(enum, is_header=False):
abbrev = enum_abbrev(enum)
return "const char %s*dnnl_%s2str(%s v)" % (
"DNNL_API " if is_header else "",
abbrev,
enum,
)
def func_to_str(enum, values):
indent = " "
abbrev = enum_abbrev(enum)
func = ""
func += func_to_str_decl(enum) + " {\n"
for v in values:
func += '%sif (v == %s) return "%s";\n' % (indent, v, sanitize_value(v))
if (enum == "dnnl_primitive_kind_t"):
func += '%sif (v == dnnl::impl::primitive_kind::sdpa) return "sdpa";\n' % indent
func += '%sassert(!"unknown %s");\n' % (indent, abbrev)
func += '%sreturn "unknown %s";\n}\n' % (indent, abbrev)
return func
def str_to_func_decl(enum, is_header=False, is_dnnl=True):
attr = "DNNL_API " if is_header and is_dnnl else ""
prefix = "dnnl_" if is_dnnl else ""
abbrev = enum_abbrev(enum)
return "%s %s%sstr2%s(const char *str)" % (enum, attr, prefix, abbrev)
def str_to_func(enum, values, is_dnnl=True):
indent = " "
abbrev = enum_abbrev(enum)
func = ""
func += str_to_func_decl(enum, is_dnnl=is_dnnl) + " {\n"
func += """#define CASE(_case) do { \\
if (!strcmp(STRINGIFY(_case), str) \\
|| !strcmp("dnnl_" STRINGIFY(_case), str)) \\
return CONCAT2(dnnl_, _case); \\
} while (0)
"""
special_values = []
for v in values:
if "last" in v:
continue
if "undef" in v:
v_undef = v
special_values.append(v)
continue
if "any" in v:
special_values.append(v)
continue
func += "%sCASE(%s);\n" % (indent, sanitize_value(v))
func += "#undef CASE\n"
for v in special_values:
v_short = re.search(r"(any|undef)", v).group()
func += """%sif (!strcmp("%s", str) || !strcmp("%s", str))
return %s;
""" % (
indent,
v_short,
v,
v,
)
if enum != "dnnl_format_tag_t":
func += (
'%sprintf("Error: %s ' % (indent, abbrev)
+ '`%s` is not supported.\\n", str);\n'
)
func += '%sassert(!"unknown %s");\n' % (indent, abbrev)
func += "%sreturn %s;\n}\n" % (
indent,
v_undef if enum != "dnnl_format_tag_t" else "dnnl_format_tag_last",
)
return func
def generate(ifile, banner_years):
h_body, s_body = "", ""
h_benchdnn_body, s_benchdnn_body = "", ""
root = ET.parse(ifile).getroot()
for v_enum in root.findall("Enumeration"):
enum = v_enum.attrib["name"]
if maybe_skip(enum):
continue
values = [v_value.attrib["name"] for v_value in v_enum.findall("EnumValue")]
if enum in ["dnnl_sparse_encoding_t"]:
h_body += "#ifdef DNNL_EXPERIMENTAL_SPARSE\n"
s_body += "#ifdef DNNL_EXPERIMENTAL_SPARSE\n"
h_body += func_to_str_decl(enum, is_header=True) + ";\n"
s_body += func_to_str(enum, values) + "\n"
if enum in ["dnnl_sparse_encoding_t"]:
h_body += "#endif\n"
s_body += "#endif\n"
if enum in ["dnnl_format_tag_t", "dnnl_data_type_t", "dnnl_sparse_encoding_t"]:
if enum in ["dnnl_sparse_encoding_t"]:
h_benchdnn_body += "#ifdef DNNL_EXPERIMENTAL_SPARSE\n"
s_benchdnn_body += "#ifdef DNNL_EXPERIMENTAL_SPARSE\n"
h_benchdnn_body += (
str_to_func_decl(enum, is_header=True, is_dnnl=False) + ";\n"
)
s_benchdnn_body += str_to_func(enum, values, is_dnnl=False) + "\n"
if enum in ["dnnl_sparse_encoding_t"]:
h_benchdnn_body += "#endif\n"
s_benchdnn_body += "#endif\n"
bodies = [
header(h_body),
source(s_body),
header_benchdnn(h_benchdnn_body),
source_benchdnn(s_benchdnn_body),
]
return [template(b, y) for b, y in zip(bodies, banner_years)]
def usage():
print(
"""\
%s types.xml
Generates oneDNN debug header and source files with enum to string mapping.
Input types.xml file can be obtained with CastXML[1]:
$ castxml --castxml-cc-gnu-c clang --castxml-output=1 \\
-DDNNL_EXPERIMENTAL_SPARSE -Iinclude -Ibuild/include \\
include/oneapi/dnnl/dnnl_types.h -o types.xml
[1] https://github.com/CastXML/CastXML"""
% sys.argv[0]
)
sys.exit(1)
for arg in sys.argv:
if "-help" in arg:
usage()
script_root = os.path.dirname(os.path.realpath(__file__))
ifile = sys.argv[1] if len(sys.argv) > 1 else usage()
file_paths = (
"%s/../include/oneapi/dnnl/dnnl_debug.h" % script_root,
"%s/../src/common/dnnl_debug_autogenerated.cpp" % script_root,
"%s/../tests/benchdnn/dnnl_debug.hpp" % script_root,
"%s/../tests/benchdnn/dnnl_debug_autogenerated.cpp" % script_root,
)
banner_years = []
for file_path in file_paths:
with open(file_path, "r") as f:
m = re.search(r"Copyright (.*) Intel", f.read())
banner_years.append(m.group(1).split("-")[0])
for file_path, file_body in zip(file_paths, generate(ifile, banner_years)):
with open(file_path, "w") as f:
f.write(file_body)
|