Spaces:
Running
Running
File size: 12,274 Bytes
9bd422a | 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 | /**
* GraphProcessor - Bα» Xα» LΓ½ Δα» Thα»
* Chuyα»n Δα»i dα»― liα»u Δα» thα» tα»« ONNXParser sang Δα»nh dαΊ‘ng Cytoscape.js
* Requirements: 4.2, 4.3, 4.6, 15.5
*/
class GraphProcessor {
/**
* Xα» lΓ½ mΓ΄ hΓ¬nh ΔΓ£ phΓ’n tΓch vΓ tαΊ‘o cαΊ₯u trΓΊc dα»― liα»u cho Cytoscape.js
* @param {Object} parsedModel - MΓ΄ hΓ¬nh ΔΓ£ phΓ’n tΓch tα»« ONNXParser
* @returns {{ elements: Array, stats: Object }}
*/
processGraph(parsedModel) {
if (!parsedModel || !parsedModel.graph) {
return {
elements: [],
stats: {
nodeCount: 0,
edgeCount: 0,
initializerCount: 0,
inputCount: 0,
outputCount: 0
}
};
}
const { graph, inputs = [], outputs = [], initializers = [] } = parsedModel;
const { nodes = [], edges = [], initializers: graphInitializers = [] } = graph;
// TαΊp hợp tΓͺn ΔαΊ§u vΓ o vΓ ΔαΊ§u ra Δα» gΓ‘n class
const inputNames = new Set((inputs || []).map(i => i.name));
const outputNames = new Set((outputs || []).map(o => o.name));
// XΓ’y dα»±ng map: tΓͺn tensor β nodeId (Δα» tΓ¬m nΓΊt ΔαΊ§u vΓ o/ΔαΊ§u ra)
const outputTensorToNodeId = new Map();
for (const node of nodes) {
for (const out of (node.outputs || [])) {
if (out) outputTensorToNodeId.set(out, node.id);
}
}
const elements = [];
// ββ NΓΊt ΔαΊ§u vΓ o mΓ΄ hΓ¬nh (input-node) ββββββββββββββββββββββββββββββββββ
for (const input of inputs) {
elements.push({
data: {
id: `input_${input.name}`,
label: input.name,
name: input.name,
opType: 'Input',
shape: input.shape,
dataType: input.dataType,
description: input.description || '',
nodeType: 'input'
},
classes: 'input-node'
});
}
// ββ NΓΊt ΔαΊ§u ra mΓ΄ hΓ¬nh (output-node) ββββββββββββββββββββββββββββββββββ
for (const output of outputs) {
elements.push({
data: {
id: `output_${output.name}`,
label: output.name,
name: output.name,
opType: 'Output',
shape: output.shape,
dataType: output.dataType,
description: output.description || '',
nodeType: 'output'
},
classes: 'output-node'
});
}
// ββ NΓΊt bα» khα»i tαΊ‘o (initializer-node) ββββββββββββββββββββββββββββββββ
const allInitializers = initializers.length > 0 ? initializers : graphInitializers;
for (const init of allInitializers) {
elements.push({
data: {
id: `init_${init.name}`,
label: init.name,
name: init.name,
opType: 'Initializer',
shape: init.shape,
dataType: init.dataType,
size: init.size,
elementCount: init.elementCount,
nodeType: 'initializer'
},
classes: 'initializer-node'
});
}
// ββ Kiα»m tra Δα» thα» lα»n β clustering ββββββββββββββββββββββββββββββββββ
const threshold = (typeof CONFIG !== 'undefined' && CONFIG.PERFORMANCE)
? CONFIG.PERFORMANCE.LAZY_LOAD_THRESHOLD
: 1000;
if (nodes.length > threshold) {
return this._processLargeGraph(
nodes, edges, inputs, outputs, allInitializers, elements, threshold
);
}
// ββ NΓΊt toΓ‘n tα» (op-node) ββββββββββββββββββββββββββββββββββββββββββββββ
for (const node of nodes) {
elements.push({
data: {
id: node.id,
label: node.opType || node.name || node.id,
name: node.name,
opType: node.opType || '',
attributes: node.attributes || {},
inputs: node.inputs || [],
outputs: node.outputs || [],
domain: node.domain || '',
nodeType: 'op'
},
classes: 'op-node'
});
}
// ββ CαΊ‘nh giα»―a cΓ‘c nΓΊt toΓ‘n tα» βββββββββββββββββββββββββββββββββββββββββ
let edgeIndex = 0;
for (const edge of edges) {
elements.push({
data: {
id: `edge_${edgeIndex++}`,
source: edge.source,
target: edge.target,
label: edge.label || ''
},
classes: 'edge'
});
}
// ββ CαΊ‘nh tα»« input-node β op-node ββββββββββββββββββββββββββββββββββββββ
for (const node of nodes) {
for (const inputTensor of (node.inputs || [])) {
if (inputNames.has(inputTensor)) {
elements.push({
data: {
id: `edge_${edgeIndex++}`,
source: `input_${inputTensor}`,
target: node.id,
label: inputTensor
},
classes: 'edge'
});
}
}
}
// ββ CαΊ‘nh tα»« op-node β output-node βββββββββββββββββββββββββββββββββββββ
for (const node of nodes) {
for (const outputTensor of (node.outputs || [])) {
if (outputNames.has(outputTensor)) {
elements.push({
data: {
id: `edge_${edgeIndex++}`,
source: node.id,
target: `output_${outputTensor}`,
label: outputTensor
},
classes: 'edge'
});
}
}
}
const stats = {
nodeCount: nodes.length,
edgeCount: edges.length,
initializerCount: allInitializers.length,
inputCount: inputs.length,
outputCount: outputs.length
};
return { elements, stats };
}
// βββ Private Helpers ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
/**
* Xα» lΓ½ Δα» thα» lα»n bαΊ±ng cΓ‘ch nhΓ³m cΓ‘c nΓΊt theo opType (clustering)
* @private
*/
_processLargeGraph(nodes, edges, inputs, outputs, initializers, baseElements, threshold) {
// NhΓ³m nΓΊt theo opType
const clusters = new Map();
for (const node of nodes) {
const opType = node.opType || 'Unknown';
if (!clusters.has(opType)) {
clusters.set(opType, []);
}
clusters.get(opType).push(node);
}
const elements = [...baseElements];
let edgeIndex = 0;
// TαΊ‘o nΓΊt cluster cho mα»i opType
for (const [opType, clusterNodes] of clusters) {
const clusterId = `cluster_${opType}`;
elements.push({
data: {
id: clusterId,
label: `${opType} (${clusterNodes.length})`,
name: opType,
opType: opType,
nodeCount: clusterNodes.length,
nodeIds: clusterNodes.map(n => n.id),
nodeType: 'cluster',
isCluster: true
},
classes: 'op-node cluster-node'
});
}
// XΓ’y dα»±ng map: nodeId β clusterId
const nodeToCluster = new Map();
for (const [opType, clusterNodes] of clusters) {
const clusterId = `cluster_${opType}`;
for (const node of clusterNodes) {
nodeToCluster.set(node.id, clusterId);
}
}
// TαΊ‘o cαΊ‘nh giα»―a cΓ‘c cluster (trΓ‘nh trΓΉng lαΊ·p)
const clusterEdgeSet = new Set();
for (const edge of edges) {
const srcCluster = nodeToCluster.get(edge.source);
const tgtCluster = nodeToCluster.get(edge.target);
if (srcCluster && tgtCluster && srcCluster !== tgtCluster) {
const key = `${srcCluster}β${tgtCluster}`;
if (!clusterEdgeSet.has(key)) {
clusterEdgeSet.add(key);
elements.push({
data: {
id: `edge_${edgeIndex++}`,
source: srcCluster,
target: tgtCluster,
label: ''
},
classes: 'edge'
});
}
}
}
// CαΊ‘nh tα»« input-node β cluster
const inputNames = new Set((inputs || []).map(i => i.name));
const outputNames = new Set((outputs || []).map(o => o.name));
const inputClusterEdgeSet = new Set();
for (const node of nodes) {
const clusterId = nodeToCluster.get(node.id);
for (const inputTensor of (node.inputs || [])) {
if (inputNames.has(inputTensor)) {
const key = `input_${inputTensor}β${clusterId}`;
if (!inputClusterEdgeSet.has(key)) {
inputClusterEdgeSet.add(key);
elements.push({
data: {
id: `edge_${edgeIndex++}`,
source: `input_${inputTensor}`,
target: clusterId,
label: inputTensor
},
classes: 'edge'
});
}
}
}
for (const outputTensor of (node.outputs || [])) {
if (outputNames.has(outputTensor)) {
const key = `${clusterId}βoutput_${outputTensor}`;
if (!inputClusterEdgeSet.has(key)) {
inputClusterEdgeSet.add(key);
elements.push({
data: {
id: `edge_${edgeIndex++}`,
source: clusterId,
target: `output_${outputTensor}`,
label: outputTensor
},
classes: 'edge'
});
}
}
}
}
const stats = {
nodeCount: nodes.length,
edgeCount: edges.length,
initializerCount: initializers.length,
inputCount: inputs.length,
outputCount: outputs.length,
clusterCount: clusters.size,
isClustered: true
};
return { elements, stats };
}
}
// Export as global for browser usage
window.GraphProcessor = GraphProcessor;
|