Blame view

egs/wsj/s5/steps/nnet3/dot/nnet3_to_dot.py 19.3 KB
8dcb6dfcb   Yannick Estève   first commit
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
  #!/usr/bin/env python
  
  # Copyright      2015  Johns Hopkins University (Author: Vijayaditya Peddinti)
  # Apache 2.0
  
  # script to convert nnet3-am-info output to a dot graph
  
  
  # we're using python 3.x style print but want it to work in python 2.x,
  from __future__ import print_function
  import re
  import os
  import argparse
  import sys
  import math
  import warnings
  import descriptor_parser
  import pprint
  
  node_attributes = {
      'input-node':{
          'shape':'oval'
      },
      'output-node':{
          'shape':'oval'
      },
      'NaturalGradientAffineComponent':{
          'color':'lightgrey',
          'shape':'box',
          'style':'filled'
      },
      'NaturalGradientPerElementScaleComponent':{
          'color':'lightpink',
          'shape':'box',
          'style':'filled'
      },
      'ConvolutionComponent':{
          'color':'lightpink',
          'shape':'box',
          'style':'filled'
      },
      'FixedScaleComponent':{
          'color':'blueviolet',
          'shape':'box',
          'style':'filled'
      },
      'FixedAffineComponent':{
          'color':'darkolivegreen1',
          'shape':'box',
          'style':'filled'
      },
      'SigmoidComponent':{
          'color':'bisque',
          'shape':'rectangle',
          'style':'filled'
      },
      'TanhComponent':{
          'color':'bisque',
          'shape':'rectangle',
          'style':'filled'
      },
      'NormalizeComponent':{
          'color':'aquamarine',
          'shape':'rectangle',
          'style':'filled'
      },
      'RectifiedLinearComponent':{
          'color':'bisque',
          'shape':'rectangle',
          'style':'filled'
      },
      'ClipGradientComponent':{
          'color':'bisque',
          'shape':'rectangle',
          'style':'filled'
      },
      'ElementwiseProductComponent':{
          'color':'green',
          'shape':'rectangle',
          'style':'filled'
      },
      'LogSoftmaxComponent':{
          'color':'cyan',
          'shape':'rectangle',
          'style':'filled'
      }
  }
  
  def GetDotNodeName(name_string, is_component = False):
      # this function is required as dot does not allow all the component names
      # allowed by nnet3.
      # Identified incompatibilities :
      #   1. dot does not allow hyphen(-) and dot(.) in names
      #   2. Nnet3 names can be shared among components and component nodes
      #      dot does not allow common names
      #
      node_name_string = re.sub("-", "hyphen", name_string)
      node_name_string = re.sub("\.", "_dot_", node_name_string)
      if is_component:
          node_name_string += node_name_string.strip() + "_component"
      return {"label":name_string, "node":node_name_string}
  
  def ProcessAppendDescriptor(segment, parent_node_name, affix, edge_attributes = None):
      dot_graph = []
      names = []
      desc_name = 'Append_{0}'.format(affix)
      for i in range(len(segment['sub_segments'])):
          sub_segment = segment['sub_segments'][i]
          part_name = "{0}{1}{2}".format(desc_name, sub_segment['name'], i)
          names.append("<{0}> part {1}".format(GetDotNodeName(part_name)['node'], i))
          dot_graph += DescriptorSegmentToDot(sub_segment, "{0}:{1}".format(desc_name, part_name), desc_name)
  
      part_index = len(segment['sub_segments'])
      for i in range(len(segment['arguments'])):
          part_name = "{0}{1}{2}".format(desc_name, segment['arguments'][i], part_index + i)
          names.append("<{0}> part {1}".format(GetDotNodeName(part_name)['node'], part_index + i))
          dot_graph.append("{0} -> {1}:{2}".format(GetDotNodeName(segment['arguments'][i])['node'], GetDotNodeName(desc_name)['node'], GetDotNodeName(part_name)['node']))
  
      label = "|".join(names)
      label = "{{"+label+"}|Append}"
      dot_graph.append('{0} [shape=Mrecord, label="{1}"];'.format(GetDotNodeName(desc_name)['node'], label))
  
      attr_string = ''
      if edge_attributes is not None:
          if 'label' in edge_attributes:
              attr_string += " label={0} ".format(edge_attributes['label'])
          if 'style' in edge_attributes:
              attr_string += ' style={0} '.format(edge_attributes['style'])
  
      dot_string = '{0} -> {1} [tailport=s]'.format(GetDotNodeName(desc_name)['node'], GetDotNodeName(parent_node_name)['node'])
  
      if attr_string != '':
          dot_string += ' [{0}] '.format(attr_string)
      dot_graph.append(dot_string)
  
  
      return dot_graph
  
  def ProcessRoundDescriptor(segment, parent_node_name, affix, edge_attributes = None):
      dot_graph = []
  
      label = 'Round ({0})'.format(segment['arguments'][1])
      style = None
      if edge_attributes is not None:
          if 'label' in edge_attributes:
              label = "{0} {1}".format(edge_attributes['label'], label)
          if 'style' in edge_attributes:
              style  = 'style={0}'.format(edge_attributes['style'])
  
      attr_string = 'label="{0}"'.format(label)
      if style is not None:
          attr_string += ' {0}'.format(style)
      dot_graph.append('{0}->{1} [ {2} ]'.format(GetDotNodeName(segment['arguments'][0])['node'],
                                                                      GetDotNodeName(parent_node_name)['node'],
                                                                      attr_string))
      if segment['sub_segments']:
          raise Exception("Round can just deal with forwarding descriptor, no sub-segments allowed")
      return dot_graph
  
  
  def ProcessOffsetDescriptor(segment, parent_node_name, affix, edge_attributes = None):
      dot_graph = []
  
      label = 'Offset ({0})'.format(segment['arguments'][1])
      style = None
      if edge_attributes is not None:
          if 'label' in edge_attributes:
              label = "{0} {1}".format(edge_attributes['label'], label)
          if 'style' in edge_attributes:
              style  = 'style={0}'.format(edge_attributes['style'])
  
      attr_string = 'label="{0}"'.format(label)
      if style is not None:
          attr_string += ' {0}'.format(style)
  
      dot_graph.append('{0}->{1} [ {2} ]'.format(GetDotNodeName(segment['arguments'][0])['node'],
                                                                      GetDotNodeName(parent_node_name)['node'],
                                                                      attr_string))
      if segment['sub_segments']:
          raise Exception("Offset can just deal with forwarding descriptor, no sub-segments allowed")
      return dot_graph
  
  def ProcessSumDescriptor(segment, parent_node_name, affix, edge_attributes = None):
      dot_graph = []
      names = []
      desc_name = 'Sum_{0}'.format(affix)
      # create the sum node
      for i in range(len(segment['sub_segments'])):
          sub_segment = segment['sub_segments'][i]
          part_name = "{0}{1}{2}".format(desc_name, sub_segment['name'], i)
          names.append("<{0}> part {1}".format(GetDotNodeName(part_name)['node'], i))
          dot_graph += DescriptorSegmentToDot(sub_segment, "{0}:{1}".format(desc_name, part_name), "{0}_{1}".format(desc_name, i))
  
      # link the sum node parts to corresponding segments
      part_index = len(segment['sub_segments'])
      for i in range(len(segment['arguments'])):
          part_name = "{0}{1}{2}".format(desc_name, segment['arguments'][i], part_index + i)
          names.append("<{0}> part {1}".format(GetDotNodeName(part_name)['node'], part_index + i))
          dot_graph.append("{0} -> {1}:{2}".format(GetDotNodeName(segment['arguments'][i])['node'], GetDotNodeName(desc_name)['node'], GetDotNodeName(part_name)['node']))
  
      label = "|".join(names)
      label = '{{'+label+'}|Sum}'
      dot_graph.append('{0} [shape=Mrecord, label="{1}", color=red];'.format(GetDotNodeName(desc_name)['node'], label))
  
      attr_string = ''
      if edge_attributes is not None:
          if 'label' in edge_attributes:
              attr_string += " label={0} ".format(edge_attributes['label'])
          if 'style' in edge_attributes:
              attr_string += ' style={0} '.format(edge_attributes['style'])
  
      dot_string = '{0} -> {1}'.format(GetDotNodeName(desc_name)['node'], GetDotNodeName(parent_node_name)['node'])
  
      dot_string += ' [{0} tailport=s ] '.format(attr_string)
      dot_graph.append(dot_string)
      return dot_graph
  
  def ProcessReplaceIndexDescriptor(segment, parent_node_name, affix, edge_attributes = None):
      dot_graph = []
  
      label = 'ReplaceIndex({0}, {1})'.format(segment['arguments'][1], segment['arguments'][2])
      style = None
      if edge_attributes is not None:
          if 'label' in edge_attributes:
              label = "{0} {1}".format(edge_attributes['label'], label)
          if 'style' in edge_attributes:
              style  = 'style={0}'.format(edge_attributes['style'])
  
      attr_string = 'label="{0}"'.format(label)
      if style is not None:
          attr_string += ' {0}'.format(style)
  
      dot_graph.append('{0}->{1} [{2}]'.format(GetDotNodeName(segment['arguments'][0])['node'],
                                                                      GetDotNodeName(parent_node_name)['node'],
                                                                      attr_string))
      if segment['sub_segments']:
          raise Exception("ReplaceIndex can just deal with forwarding descriptor, no sub-segments allowed")
      return dot_graph
  
  def ProcessIfDefinedDescriptor(segment, parent_node_name, affix, edge_attributes = None):
      # IfDefined adds attributes to the edges
      if edge_attributes is not None:
          raise Exception("edge_attributes was not None, this means an IfDefined descriptor was calling the current IfDefined descriptor. This is not allowed")
      dot_graph = []
      dot_graph.append('#ProcessIfDefinedDescriptor')
      names = []
  
      if segment['sub_segments']:
          sub_segment = segment['sub_segments'][0]
          dot_graph += DescriptorSegmentToDot(sub_segment, parent_node_name, parent_node_name, edge_attributes={'style':'dotted', 'label':'IfDefined'})
  
      if segment['arguments']:
          dot_graph.append('{0} -> {1} [style=dotted, label="IfDefined"]'.format(GetDotNodeName(segment['arguments'][0])['node'], GetDotNodeName(parent_node_name)['node']))
  
      return dot_graph
  
  def DescriptorSegmentToDot(segment, parent_node_name, affix, edge_attributes = None):
      # segment is a dicionary which corresponds to a descriptor
      dot_graph = []
      if segment['name'] == "Append":
          dot_graph += ProcessAppendDescriptor(segment, parent_node_name, affix, edge_attributes)
      elif segment['name'] == "Offset":
          dot_graph += ProcessOffsetDescriptor(segment, parent_node_name, affix, edge_attributes)
      elif segment['name'] == "Sum":
          dot_graph += ProcessSumDescriptor(segment, parent_node_name, affix, edge_attributes)
      elif segment['name'] == "IfDefined":
          dot_graph += ProcessIfDefinedDescriptor(segment, parent_node_name, affix, edge_attributes)
      elif segment['name'] == "ReplaceIndex":
          dot_graph += ProcessReplaceIndexDescriptor(segment, parent_node_name, affix, edge_attributes)
      elif segment['name'] == "Round":
          dot_graph += ProcessRoundDescriptor(segment, parent_node_name, affix, edge_attributes)
      else:
          raise Exception('Descriptor {0}, is not recognized by this script. Please add Process{0}Descriptor method'.format(segment['name']))
      return dot_graph
  
  def Nnet3DescriptorToDot(descriptor, parent_node_name):
      dot_lines = []
      [segments, arguments] = descriptor_parser.IdentifyNestedSegments(descriptor)
      if segments:
          for segment in segments:
              dot_lines += DescriptorSegmentToDot(segment, parent_node_name, parent_node_name)
      elif arguments:
          assert(len(arguments) == 1)
          dot_lines.append("{0} -> {1}".format(GetDotNodeName(arguments[0])['node'], GetDotNodeName(parent_node_name)['node']))
      return dot_lines
  
  def ParseNnet3String(string):
      if re.search('^input-node|^component|^output-node|^component-node|^dim-range-node', string.strip()) is None:
          return [None, None]
  
      parts = string.split()
      config_type = parts[0]
      fields = []
      prev_field = ''
      for i in range(1, len(parts)):
          if re.search('=', parts[i]) is None:
              prev_field += ' '+parts[i]
          else:
              if not (prev_field.strip() == ''):
                  fields.append(prev_field)
              sub_parts = parts[i].split('=')
              if (len(sub_parts) != 2):
                  raise Exception('Malformed config line {0}'.format(string))
              fields.append(sub_parts[0])
              prev_field = sub_parts[1]
      fields.append(prev_field)
  
      parsed_string = {}
      try:
          while len(fields) > 0:
              value = re.sub(',$', '', fields.pop().strip())
              key = fields.pop()
              parsed_string[key.strip()] = value.strip()
      except IndexError:
          raise Exception('Malformed config line {0}'.format(string))
      return [config_type, parsed_string]
  
  # sample component config line
  # component name=L0_lda type=FixedAffineComponent, input-dim=300, output-dim=300, linear-params-stddev=0.00992724, bias-params-stddev=0.573973
  def Nnet3ComponentToDot(component_config, component_attributes = None):
      label = ''
      if component_attributes is None:
          component_attributes = component_config.keys()
      attributes_to_print = set(component_attributes).intersection(list(component_config.keys()))
      # process the known fields
      for key in attributes_to_print:
          if key in component_config:
              label += '{0} = {1}\
  '.format(key, component_config[key])
  
      attr_string = ''
      try:
          attributes = node_attributes[component_config['type']]
          for key in attributes.keys():
              attr_string += ' {0}={1} '.format(key, attributes[key])
      except KeyError:
          pass
  
      return ['{0} [label="{1}" {2}]'.format(GetDotNodeName(component_config['name'], is_component = True)['node'], label, attr_string)]
  
  
  # input-node name=input dim=40
  def Nnet3InputToDot(parsed_config):
      return ['{0} [ label="{1}\
  dim={2}"]'.format(GetDotNodeName(parsed_config['name'])['node'], parsed_config['name'], parsed_config['dim'] )]
  
  # output-node name=output input=Final_log_softmax dim=3940 objective=linear
  #output-node name=output input=Offset(Final_log_softmax, 5) dim=3940 objective=linear
  def Nnet3OutputToDot(parsed_config):
      dot_graph = []
      dot_graph += Nnet3DescriptorToDot(parsed_config['input'], parsed_config['name'])
      dot_graph.append('{0} [ label="{1}\
  objective={2}"]'.format(GetDotNodeName(parsed_config['name'])['node'], parsed_config['name'], parsed_config['objective']))
      return dot_graph
  
  # dim-range-node name=Lstm1_r_t input-node=Lstm1_rp_t dim-offset=0 dim=256
  def Nnet3DimrangeToDot(parsed_config):
      dot_graph = []
      dot_node = GetDotNodeName(parsed_config['name'])
      dot_graph.append('{0} [shape=rectangle, label="{1}"]'.format(dot_node['node'], dot_node['label']))
      dot_graph.append('{0} -> {1} [taillabel="dimrange({2}, {3})"]'.format(GetDotNodeName(parsed_config['input-node'])['node'],
                                                             GetDotNodeName(parsed_config['name'])['node'],
                                                             parsed_config['dim-offset'],
                                                             parsed_config['dim']))
      return dot_graph
  
  def Nnet3ComponentNodeToDot(parsed_config):
      dot_graph = []
      dot_graph += Nnet3DescriptorToDot(parsed_config['input'], parsed_config['name'])
      dot_node = GetDotNodeName(parsed_config['name'])
      dot_graph.append('{0} [ label="{1}", shape=box ]'.format(dot_node['node'], dot_node['label']))
      dot_graph.append('{0} -> {1} [ weight=10 ]'.format(GetDotNodeName(parsed_config['component'], is_component = True)['node'],
                                                         GetDotNodeName(parsed_config['name'])['node']))
      return dot_graph
  
  def GroupConfigs(configs, node_prefixes = None):
      if node_prefixes is None:
          node_prefixes = []
      # we make the assumption that nodes belonging to the same sub-graph have a
      # commong prefix.
      grouped_configs = {}
      for node_prefix in node_prefixes:
          group = []
          rest = []
          for config in configs:
              if re.search('^{0}'.format(node_prefix), config[1]['name']) is not None:
                  group.append(config)
              else:
                  rest.append(config)
          configs = rest
          grouped_configs[node_prefix] = group
      grouped_configs[None] = configs
  
      return grouped_configs
  
  def ParseConfigLines(lines, node_prefixes = None, component_attributes = None ):
      if node_prefixes is None:
          node_prefixes = []
      config_lines = []
      dot_graph=[]
      configs = []
      for line in lines:
          config_type, parsed_config = ParseNnet3String(line)
          if config_type is not None:
              configs.append([config_type, parsed_config])
  
      # process the config lines
      grouped_configs = GroupConfigs(configs, node_prefixes)
      for group in grouped_configs.keys():
          configs = grouped_configs[group]
          if not configs:
              continue
          if group is not None:
              # subgraphs prefixed with cluster will be treated differently by
              # dot
              dot_graph.append('subgraph cluster_{0} '.format(group) + "{")
              dot_graph.append('color=blue')
  
          for config in configs:
              config_type = config[0]
              parsed_config = config[1]
              if config_type is None:
                  continue
              if config_type == 'input-node':
                  dot_graph += Nnet3InputToDot(parsed_config)
              elif config_type == 'output-node':
                  dot_graph += Nnet3OutputToDot(parsed_config)
              elif config_type == 'component-node':
                  dot_graph += Nnet3ComponentNodeToDot(parsed_config)
              elif config_type == 'dim-range-node':
                  dot_graph += Nnet3DimrangeToDot(parsed_config)
              elif config_type == 'component':
                  dot_graph += Nnet3ComponentToDot(parsed_config, component_attributes)
  
          if group is not None:
              dot_graph.append('label = "{0}"'.format(group))
              dot_graph.append('}')
  
      dot_graph.insert(0, 'digraph nnet3graph {')
      dot_graph.append('}')
  
      return dot_graph
  
  if __name__ == "__main__":
      parser = argparse.ArgumentParser(description="Converts the output of nnet3-am-info "
                                                   "to dot graph. The output has to be compiled"
                                                   " with dot to generate a displayable graph",
                                      epilog="See steps/nnet3/nnet3_to_dot.sh for example.");
      parser.add_argument("--component-attributes", type=str,
                          help="Attributes of the components which should be displayed in the dot-graph "
                               "e.g. --component-attributes name,type,input-dim,output-dim", default=None)
      parser.add_argument("--node-prefixes", type=str,
                          help="list of prefixes. Nnet3 components/component-nodes with the same prefix"
                          " will be clustered together in the dot-graph"
                          " --node-prefixes Lstm1,Lstm2,Layer1", default=None)
  
      parser.add_argument("dotfile", help="name of the dot output file")
  
      print(' '.join(sys.argv), file=sys.stderr)
  
      args = parser.parse_args()
      component_attributes = None
      if args.component_attributes is not None:
          component_attributes = args.component_attributes.split(',')
      node_prefixes = []
      if args.node_prefixes is not None:
          node_prefixes = args.node_prefixes.split(',')
  
      lines = sys.stdin.readlines()
      dot_graph = ParseConfigLines(lines, component_attributes = component_attributes, node_prefixes = node_prefixes)
  
      dotfile_handle = open(args.dotfile, "w")
      dotfile_handle.write("
  ".join(dot_graph))
      dotfile_handle.close()