Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / mkl-dnn / scripts / generate_mkldnn_debug.py
1 #!/usr/bin/env python
2 #===============================================================================
3 # Copyright 2018 Intel Corporation
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #===============================================================================
17
18 import sys
19 import datetime
20 import xml.etree.ElementTree as ET
21
22 def banner():
23     year_now = datetime.datetime.now().year
24     banner_year = '2018' if year_now == 2018 else '2018 - %d' % year_now
25     return '''\
26 /*******************************************************************************
27 * Copyright %s Intel Corporation
28 *
29 * Licensed under the Apache License, Version 2.0 (the "License");
30 * you may not use this file except in compliance with the License.
31 * You may obtain a copy of the License at
32 *
33 *     http://www.apache.org/licenses/LICENSE-2.0
34 *
35 * Unless required by applicable law or agreed to in writing, software
36 * distributed under the License is distributed on an "AS IS" BASIS,
37 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
38 * See the License for the specific language governing permissions and
39 * limitations under the License.
40 *******************************************************************************/
41
42 /* DO NOT EDIT, AUTO-GENERATED */
43
44 ''' % banner_year
45
46
47 def template(body):
48     return '%s%s' % (banner(), body)
49
50
51 def header(body):
52     return '''\
53 #ifndef MKLDNN_DEBUG_H
54 #define MKLDNN_DEBUG_H
55
56 #ifndef DOXYGEN_SHOULD_SKIP_THIS
57
58 /* All symbols shall be internal unless marked as MKLDNN_API */
59 #if defined _WIN32 || defined __CYGWIN__
60 #   define MKLDNN_HELPER_DLL_IMPORT __declspec(dllimport)
61 #   define MKLDNN_HELPER_DLL_EXPORT __declspec(dllexport)
62 #else
63 #   if __GNUC__ >= 4
64 #       define MKLDNN_HELPER_DLL_IMPORT __attribute__ ((visibility ("default")))
65 #       define MKLDNN_HELPER_DLL_EXPORT __attribute__ ((visibility ("default")))
66 #   else
67 #       define MKLDNN_HELPER_DLL_IMPORT
68 #       define MKLDNN_HELPER_DLL_EXPORT
69 #   endif
70 #endif
71
72 #ifdef MKLDNN_DLL
73 #   ifdef MKLDNN_DLL_EXPORTS
74 #       define MKLDNN_API MKLDNN_HELPER_DLL_EXPORT
75 #   else
76 #       define MKLDNN_API MKLDNN_HELPER_DLL_IMPORT
77 #   endif
78 #else
79 #   define MKLDNN_API
80 #endif
81
82 #if defined (__GNUC__)
83 #   define MKLDNN_DEPRECATED __attribute__((deprecated))
84 #elif defined(_MSC_VER)
85 #   define MKLDNN_DEPRECATED __declspec(deprecated)
86 #else
87 #   define MKLDNN_DEPRECATED
88 #endif
89
90 #include "mkldnn_types.h"
91 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
92
93 #ifdef __cplusplus
94 extern "C" {
95 #endif
96
97 %s
98 #ifdef __cplusplus
99 }
100 #endif
101
102 #endif
103 ''' % body
104
105
106 def source(body):
107     return '''\
108 #include <assert.h>
109
110 #include "mkldnn_debug.h"
111 #include "mkldnn_types.h"
112
113 %s
114 ''' % body
115
116
117 def maybe_skip(enum):
118     return enum in (
119         'mkldnn_padding_kind_t',
120         'mkldnn_batch_normalization_flag_t',
121         'mkldnn_wino_memory_format_t',
122         'mkldnn_rnn_cell_flags_t',
123         'mkldnn_engine_kind_t',
124         'mkldnn_query_t',
125         'mkldnn_stream_kind_t',
126         )
127
128
129 def enum_abbrev(enum):
130     return {
131         'mkldnn_status_t': 'status',
132         'mkldnn_data_type_t': 'dt',
133         'mkldnn_round_mode_t': 'rmode',
134         'mkldnn_memory_format_t': 'fmt',
135         'mkldnn_prop_kind_t': 'prop_kind',
136         'mkldnn_primitive_kind_t': 'prim_kind',
137         'mkldnn_alg_kind_t': 'alg_kind',
138         'mkldnn_rnn_direction_t': 'rnn_direction',
139     }.get(enum, enum)
140
141
142 def sanitize_value(v):
143     if 'undef' in v:
144         return 'undef'
145     v = v.split('mkldnn_')[-1]
146     return v
147
148
149 def func_decl(enum, is_header = False):
150     abbrev = enum_abbrev(enum)
151     return 'const char %s*mkldnn_%s2str(%s v)' % \
152             ('MKLDNN_API ' if is_header else '', abbrev, enum)
153
154
155 def func_to_str(enum, values):
156     indent = '    '
157     abbrev = enum_abbrev(enum)
158     func = ''
159     func += func_decl(enum) + ' {\n'
160     for v in values:
161         func += '%sif (v == %s) return "%s";\n' \
162                 % (indent, v, sanitize_value(v))
163     func += '%sassert(!"unknown %s");\n' % (indent, abbrev)
164     func += '%sreturn "unknown %s";\n}\n' % (indent, abbrev)
165     return func
166
167
168 def generate(ifile):
169     h_body = ''
170     s_body = ''
171     root = ET.parse(ifile).getroot()
172     for v_enum in root.findall('Enumeration'):
173         enum = v_enum.attrib['name']
174         if maybe_skip(enum):
175             continue
176         values = [v_value.attrib['name'] \
177                 for v_value in v_enum.findall('EnumValue')]
178         h_body += func_decl(enum, is_header = True) + ';\n'
179         s_body += func_to_str(enum, values) + '\n'
180     return (template(header(h_body)), template(source(s_body)))
181
182
183 def usage():
184     print '''\
185 %s types.xml [output_dir]
186
187 Generates MKL-DNN debug header and source files with enum to string mapping.
188 Input types.xml file can be obtained with CastXML[1]:
189 $ castxml --castxml-cc-gnu-c clang --castxml-output=1 \\
190         include/mkldnn_types.h -o types.xml
191
192 [1] https://github.com/CastXML/CastXML''' % sys.argv[0]
193     sys.exit(1)
194
195
196 for arg in sys.argv:
197     if '-help' in arg:
198         usage()
199
200 ifile = sys.argv[1] if len(sys.argv) > 1 else usage()
201 odir = sys.argv[2] if len(sys.argv) > 2 else '.'
202 ofile_h = odir + '/mkldnn_debug.h'
203 ofile_s = odir + '/mkldnn_debug.cpp'
204
205 (h, s) = generate(ifile)
206
207 with open(ofile_h, 'w') as fh:
208     fh.write(h)
209
210 with open(ofile_s, 'w') as fs:
211     fs.write(s)