graphUtils.ts 13.5 KB
Newer Older
Yuge Zhang's avatar
Yuge Zhang committed
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
function path2module(path: any[]): string {
  return path.map(p => p.name ? `${p.type}[${p.name}]` : p.type).join('/');
}

function opName(rawName: string): string {
  if (rawName.includes('::')) {
    return rawName.split('::')[1];
  } else {
    return rawName;
  }
}

export class NodeTs {
  readonly id: string;
  readonly tail: string;
  parent: NodeTs | undefined;
  children: NodeTs[];
  op: string;
  attributes: string;

  constructor(id: string, tail: string, op: string, attributes: string) {
    this.children = [];
    this.id = id;
    this.tail = tail;
    this.parent = undefined;
    this.op = op;
    this.attributes = attributes;
  }

  descendants(leafOnly: boolean): NodeTs[] {
    // return all descendants includinng itself
    const result: NodeTs[] = [];
    if (!leafOnly || this.isChildless())
      result.push(this);
    for (const child of this.children) {
      const childDesc = child.descendants(leafOnly);
      if (childDesc.length > 0)
        result.push(...childDesc);
    }
    return result;
  }

  isChildless(): boolean {
    return this.children.length === 0;
  }

  isParent(): boolean {
    return this.children.length > 0;
  }
50
51
52
53

  toString(): string {
    return `Node(id=${this.id})`;
  }
Yuge Zhang's avatar
Yuge Zhang committed
54
55
56
57
58
59
60
61
62
63
64
65
};

export class Edge {
  readonly source: NodeTs;
  readonly target: NodeTs;
  readonly id: string;

  constructor(source: NodeTs, target: NodeTs) {
    this.source = source;
    this.target = target;
    this.id = JSON.stringify([this.source.id, this.target.id]);
  }
66
67
68
69

  toString(): string {
    return `Edge(${this.source} -> ${this.target})`;
  }
Yuge Zhang's avatar
Yuge Zhang committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
};

interface NodeSummary {
  name: string,
  nodeCount: number,
  edgeCount: number,
  inputs: string[],
  outputs: string[],
  attributes: string,
  op: string
};

export class Graph {
  root: NodeTs;
  nodes: NodeTs[];
  edges: Edge[];
  defaultExpandSet: Set<string>;
87
  mutableEdges: Map<string, Edge[][]>;
Yuge Zhang's avatar
Yuge Zhang committed
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
  private id2idx: Map<string, number>;
  private edgeId2idx: Map<string, number>;
  private forwardGraph: Map<string, string[]>;
  private backwardGraph: Map<string, string[]>;
  private node2edge: Map<string, Edge[]>;

  private build() {
    this.id2idx.clear();
    this.nodes.forEach((node, i) => {
      this.id2idx.set(node.id, i);
    });
    this.edgeId2idx.clear();
    this.edges.forEach((edge, i) => {
      this.edgeId2idx.set(edge.id, i);
    });

    this.forwardGraph.clear();
    this.backwardGraph.clear();
    this.node2edge.clear();
    this.edges.forEach(edge => {
      if (!this.forwardGraph.has(edge.source.id))
        this.forwardGraph.set(edge.source.id, []);
      this.forwardGraph.get(edge.source.id)!.push(edge.target.id);
      if (!this.backwardGraph.has(edge.target.id))
        this.backwardGraph.set(edge.target.id, []);
      this.backwardGraph.get(edge.target.id)!.push(edge.source.id);
      if (!this.node2edge.has(edge.source.id))
        this.node2edge.set(edge.source.id, []);
      if (!this.node2edge.has(edge.target.id))
        this.node2edge.set(edge.target.id, []);
      this.node2edge.get(edge.source.id)!.push(edge);
      this.node2edge.get(edge.target.id)!.push(edge);
    });

    this.root.children = this.nodes.filter(node => node.parent === undefined);
    // won't set parent for these nodes, leave them as undefined
    this.nodes.forEach(node => {
      node.children = node.children.filter(child => this.getNodeById(child.id) !== undefined);
    });
  }

  getNodeById(id: string): NodeTs | undefined {
    const idx = this.id2idx.get(id);
    if (idx === undefined) return undefined;
    return this.nodes[idx];
  }

  getEdgeById(source: string, target: string): Edge | undefined {
    const idx = this.edgeId2idx.get(JSON.stringify([source, target]));
    if (idx === undefined) return undefined;
    return this.edges[idx];
  }

  constructor(graphData: any, eliminateSidechains: boolean) {
    this.id2idx = new Map<string, number>();
    this.edgeId2idx = new Map<string, number>();
    this.forwardGraph = new Map<string, string[]>();
    this.backwardGraph = new Map<string, string[]>();
    this.node2edge = new Map<string, Edge[]>();
    this.root = new NodeTs('', '', '', '');
    const cluster = new Map<string, NodeTs>();
    const parentMap = new Map<string, string>();
    this.nodes = graphData.node.map((node: any): NodeTs => {
      const split = node.name.split('/');
      const attr = node.hasOwnProperty('attr') ? atob(node.attr.attr.s) : '';
      if (split.length === 1) {
        return new NodeTs(node.name, node.name, opName(node.op), attr);
      } else {
        parentMap.set(node.name, split.slice(0, -1).join('/'));
        // create clusters
        for (let i = 1; i < split.length; ++i) {
          const name = split.slice(0, i).join('/');
          if (!cluster.has(name)) {
            const parent = i > 1 ? split.slice(0, i - 1).join('/') : '';
            const tail = split[i - 1];
            cluster.set(name, new NodeTs(name, tail, '', ''));
            parentMap.set(name, parent);
          }
        }
        return new NodeTs(node.name, split.slice(-1)[0], opName(node.op), attr);
      }
    });
    cluster.forEach(node => this.nodes.push(node));
    this.nodes.forEach((node, i) => {
      this.id2idx.set(node.id, i);
    });
    parentMap.forEach((parent, child) => {
      const [childNode, parentNode] = [child, parent].map(this.getNodeById.bind(this));
      if (childNode !== undefined && parentNode !== undefined) {
        childNode.parent = parentNode;
        parentNode.children.push(childNode);
      }
    });

    // build edges
    this.edges = [];
    graphData.node.forEach((node: any) => {
      if (!node.hasOwnProperty('input')) return;
      const target = this.getNodeById(node.name);
      if (target === undefined) return;
      node.input.forEach((input: string) => {
        const source = this.getNodeById(input);
        if (source !== undefined) {
          this.edges.push(new Edge(source, target));
        }
      })
    })
    this.build();

    if (eliminateSidechains) {
      this.eliminateSidechains();
    }
    this.defaultExpandSet = this.getDefaultExpandSet(graphData.mutable);
    this.mutableEdges = this.inferMutableEdges(graphData.mutable);
  }

  private eliminateSidechains(): void {
    const sources = this.nodes
      .map(node => node.id)
      .filter(id => id.startsWith('input'));

    const visitedNodes = new Set(sources);
    const dfsStack = sources;
    while (dfsStack.length > 0) {
      const u = dfsStack.pop()!;
      if (this.forwardGraph.has(u)) {
        this.forwardGraph.get(u)!.forEach((v: string) => {
          if (!visitedNodes.has(v)) {
            visitedNodes.add(v);
            dfsStack.push(v);
          }
        });
      }
    }
    const compoundCheck = (node: NodeTs) => {
      if (node.isChildless())
        return visitedNodes.has(node.id);
      for (const child of node.children)
        if (compoundCheck(child))
          visitedNodes.add(node.id);
      return visitedNodes.has(node.id);
    }
    compoundCheck(this.root);
    this.nodes = this.nodes.filter(node => visitedNodes.has(node.id));
    this.edges = this.edges.filter(edge =>
      visitedNodes.has(edge.source.id) && visitedNodes.has(edge.target.id));
    this.build();
  }

  private getDefaultExpandSet(graphDataMutable: any): Set<string> {
    // if multiple, only expand first
    const whitelistModuleList = Object.values(graphDataMutable)
      .filter(Boolean)
      .map((paths: any) => path2module(paths[0]));
    const whitelistModule = new Set(whitelistModuleList);

    const result = new Set<string>();
    const dfs = (node: NodeTs): number => {
      // node with mutableCount greater than 0 won't be collapsed
      let mutableCount = 0;
      if (node.id === '') {
        // root node
        mutableCount++;
      } else if (whitelistModule.has(node.id)) {
        mutableCount++;
      } else if (node.parent !== undefined && whitelistModule.has(node.parent.id)) {
        mutableCount++;
      }
      mutableCount += node.children.map(child => dfs(child)).reduce((a, b) => a + b, 0);
      if (mutableCount > 0 && node.isParent())
        result.add(node.id);
      return mutableCount;
    };
    dfs(this.root);
    return result;
  }

  private inferMutableModule(moduleName: string): Edge[][] {
    let inputs: string[] | undefined = undefined;
    let listConstructNode: string | undefined = undefined;
    const moduleNode = this.getNodeById(moduleName);
    if (moduleNode === undefined) return [];
    for (const node of moduleNode.children)
      if (node.op === 'ListConstruct') {
        inputs = this.backwardGraph.get(node.id);
        listConstructNode = node.id;
        break;
      }
    if (inputs === undefined || listConstructNode === undefined)
      return [];
    return inputs.map((input: string): Edge[] => {
      const visitedNodes = new Set<string>();
      const edgeSet: Edge[] = [];
      const dfs = (node: string, backward: boolean) => {
        const nodeData = this.getNodeById(node)!;
        if (visitedNodes.has(node)) return;
        visitedNodes.add(node);
        if (nodeData.parent === undefined || !nodeData.parent.id.startsWith(moduleName)) {
          // in another module now
          return;
        }
        const g = backward ? this.backwardGraph : this.forwardGraph;
        const glist = g.get(node);
        if (glist !== undefined) {
          glist
            .forEach((to: string) => {
              edgeSet.push(backward ?
                this.getEdgeById(to, node)! :
                this.getEdgeById(node, to)!);
              dfs(to, backward);
            });
        }
      };
      edgeSet.push(this.getEdgeById(input, listConstructNode!)!);
      dfs(input, true);
      visitedNodes.clear();
      dfs(listConstructNode!, false);
      return edgeSet;
    });
  }

  private inferMutableEdges(graphDataMutable: any): Map<string, Edge[][]> {
    const result = new Map<string, Edge[][]>();
    Object.entries(graphDataMutable).forEach(obj => {
      const [key, paths] = obj;
      const modules = (paths as any[]).map(path2module);
      const moduleEdge = modules.map(this.inferMutableModule.bind(this));
      const edges: Edge[][] = [];
      for (let i = 0; ; ++i) {
        if (moduleEdge.filter(me => i < me.length).length === 0)
          break;
        edges.push([]);
        moduleEdge
          .filter(me => i < me.length)
          .forEach(me => edges[i].push(...me[i]));
      }
      result.set(key, edges);
    });
    return result;
  }

  private connectedEdges(node: string): Edge[] {
    const result = this.node2edge.get(node);
    if (result === undefined)
      return [];
    return result;
  }

  nodeSummary(node: NodeTs | string): NodeSummary | undefined {
    if (typeof node === 'string') {
      const nodeData = this.getNodeById(node);
      if (nodeData === undefined) return undefined;
      return this.nodeSummary(nodeData);
    }
    const descendants = node.descendants(false);
    const descendantSet = new Set(descendants.map(node => node.id));
    const inputs = new Set<string>();
    const outputs = new Set<string>();
    let domesticEdges = 0;
    for (const edge of this.edges) {
      const [source, target] = [edge.source.id, edge.target.id];
      if (descendantSet.has(target) && !descendantSet.has(source))
        inputs.add(source);
      if (descendantSet.has(source) && !descendantSet.has(target))
        outputs.add(target);
      if (descendantSet.has(source) && descendantSet.has(target))
        domesticEdges++;
    }
    return {
      name: node.id,
      nodeCount: descendants.length,
      edgeCount: domesticEdges,
      inputs: Array.from(inputs),
      outputs: Array.from(outputs),
      attributes: node.attributes,
      op: node.op
    }
  }

  weightFromMutables(mutable: any): Map<string, number> {
    const elemWeight = new Map<string, number>();
    Object.entries(mutable).forEach(entry => {
370
      const elemWeightPartial = new Map<string, number>();
Yuge Zhang's avatar
Yuge Zhang committed
371
372
373
374
      const key = entry[0];
      const weights = entry[1] as number[];
      this.mutableEdges.get(key)!.forEach((edges: any, i: number) => {
        edges.forEach((edge: any) => {
375
376
          if (elemWeightPartial.has(edge.id)) {
            elemWeightPartial.set(edge.id, elemWeightPartial.get(edge.id)! + weights[i]);
Yuge Zhang's avatar
Yuge Zhang committed
377
          } else {
378
            elemWeightPartial.set(edge.id, weights[i]);
Yuge Zhang's avatar
Yuge Zhang committed
379
380
381
          }
        })
      });
382
383
384
385
386
387
388
      elemWeightPartial.forEach((v, k) => {
        if (elemWeight.has(k)) {
          elemWeight.set(k, Math.min(elemWeight.get(k)!, v));
        } else {
          elemWeight.set(k, v);
        }
      });
Yuge Zhang's avatar
Yuge Zhang committed
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
    });
    this.nodes.forEach(node => {
      const edges = this.connectedEdges(node.id);
      const relatedEdges = edges.filter(edge => elemWeight.has(edge.id));
      if (relatedEdges.length > 0) {
        if (relatedEdges.length < edges.length) {
          elemWeight.set(node.id, 1.);
        } else {
          // all related edge
          const nw = edges.map(edge => elemWeight.get(edge.id)!)
            .reduce((a, b) => Math.max(a, b));
          elemWeight.set(node.id, nw);
        }
      }
    });
    elemWeight.forEach((v, k) => elemWeight.set(k, Math.min(v, 1.)));

    // set compound weight
    const gatherWeightsFromChildren = (node: NodeTs): number | undefined => {
      if (node.isParent()) {
        const childrenWeights =
          node.children.map(gatherWeightsFromChildren)
            .filter(val => val !== undefined);
        if (childrenWeights.length > 0) {
          const nw = childrenWeights.reduce((a, b) => Math.max(a!, b!));
          elemWeight.set(node.id, nw!);
          return nw;
        } else {
          return undefined;
        }
      } else {
        return elemWeight.get(node.id);
      }
    };
    gatherWeightsFromChildren(this.root);
    return elemWeight;
  }
};