Skip to content

Commit fbee457

Browse files
authored
ORT_Web - JS graph parsing update (microsoft#15185)
### Description Simplified the JS graph parsing logic - addressing gitHub issue microsoft#15006 bug fix
1 parent 3093561 commit fbee457

File tree

1 file changed

+42
-24
lines changed

1 file changed

+42
-24
lines changed

web/lib/onnxjs/graph.ts

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -585,33 +585,49 @@ class GraphImpl implements Graph, Graph.Transformer {
585585
finalizeGraph() {
586586
let offset = 0;
587587
// delete all nodes that are not being executed
588+
// The graph is represented using these two arrays
589+
// this._nodes - Array holding the kernels to execute - each entry is a kernel pointing to this._allData
590+
// this._allData - hold 2 fields - to [] & from - these feileds hold the graph map for inputs and outputs per node
591+
// newIndices - remapping the graph after reading the flag 'executeNode'
592+
const newIndices = new Array<number>(this._nodes.length, 0);
593+
let nodePossition = 0;
594+
588595
for (let i = 0; i < this._nodes.length; i++) {
589-
if (!this._nodes[i].executeNode) {
590-
// delete this node and shift all subsequent nodes up
591-
offset++;
596+
// giving new indexes to the nodes based on execution flag
597+
newIndices[i] = nodePossition;
598+
if (this._nodes[i].executeNode) {
599+
if (nodePossition !== i) {
600+
this._nodes[nodePossition] = this._nodes[i];
601+
}
602+
nodePossition++;
603+
604+
} else {
592605
// delete all output values
593606
this._nodes[i].outputs.forEach(ind => {
594607
this._allData[ind]._from = -2;
595608
});
596-
this._nodes.splice(i, 1);
597-
i--;
598-
continue;
599609
}
600-
if (offset > 0) {
601-
// update the value table
602-
this._nodes[i].inputs.forEach(value => {
603-
const ind = this._allData[value]._to.indexOf(i + offset);
604-
if (ind !== -1) {
605-
this._allData[value]._to[ind] = i;
606-
}
607-
});
608-
this._nodes[i].outputs.forEach(value => {
609-
if (this._allData[value]._from && this._allData[value]._from! === i + offset) {
610-
this._allData[value]._from! = i;
611-
}
612-
});
610+
}
611+
612+
// removing the unused nodes
613+
this._nodes.splice(nodePossition, this._nodes.length - nodePossition);
614+
615+
// Updating this._allData according to the new this._nodes
616+
for (let i = 0; i < this._allData.length; i++) {
617+
const currentData = this._allData[i];
618+
if (currentData._from !== undefined && currentData._from !== -1 && currentData._from !== -2) {
619+
currentData._from = newIndices[currentData._from];
620+
}
621+
622+
for (let j = 0; j < currentData._to.length; j++) {
623+
if (currentData._to[j] >= 0) {
624+
currentData._to[j] = newIndices[currentData._to[j]];
625+
} else {
626+
throw new Error('Trying to update a removed node');
627+
}
613628
}
614629
}
630+
615631
offset = 0;
616632
// delete all values that are not being referenced
617633
for (let i = 0; i < this._allData.length; i++) {
@@ -679,12 +695,14 @@ class GraphImpl implements Graph, Graph.Transformer {
679695
const nodesConsumingOutput = this._allData[outputValueIndex].to;
680696

681697
// remove this node from the to property of the input Value
682-
const delIndex = this._allData[inputValueIndex].to.indexOf(nodeIndex);
683-
// should not happen
684-
if (delIndex === -1) {
685-
throw new Error('The Value object doesn\'t have the current Node in it\'s \'to\' property ');
698+
for (let i = 0; i < node.inputs.length; i++) {
699+
const delIndex = this._allData[node.inputs[i]].to.indexOf(nodeIndex);
700+
// should not happen
701+
if (delIndex === -1) {
702+
throw new Error('The Value object doesn\'t have the current Node in it\'s \'to\' property ');
703+
}
704+
this._allData[node.inputs[i]].to.splice(delIndex, 1);
686705
}
687-
this._allData[inputValueIndex].to.splice(delIndex, 1);
688706

689707
// clear node indices consuming this output Value
690708
this._allData[outputValueIndex]._to = [];

0 commit comments

Comments
 (0)