Re-use profiler visualization for extra networks

This commit is contained in:
catboxanon 2024-03-04 18:33:22 -05:00
parent eee46a5094
commit 801461eea2
3 changed files with 177 additions and 92 deletions

View File

@ -78,6 +78,8 @@ module.exports = {
//extraNetworks.js //extraNetworks.js
requestGet: "readonly", requestGet: "readonly",
popup: "readonly", popup: "readonly",
// profilerVisualization.js
createVisualizationTable: "readonly",
// from python // from python
localization: "readonly", localization: "readonly",
// progrssbar.js // progrssbar.js

View File

@ -528,12 +528,74 @@ function popupId(id) {
popup(storedPopupIds[id]); popup(storedPopupIds[id]);
} }
function extraNetworksFlattenMetadata(obj) {
const result = {};
// Convert any stringified JSON objects to actual objects
for (const key of Object.keys(obj)) {
if (typeof obj[key] === 'string') {
try {
const parsed = JSON.parse(obj[key]);
if (parsed && typeof parsed === 'object') {
obj[key] = parsed;
}
} catch (error) {
continue;
}
}
}
// Flatten the object
for (const key of Object.keys(obj)) {
if (typeof obj[key] === 'object' && obj[key] !== null) {
const nested = extraNetworksFlattenMetadata(obj[key]);
for (const nestedKey of Object.keys(nested)) {
result[`${key}/${nestedKey}`] = nested[nestedKey];
}
} else {
result[key] = obj[key];
}
}
// Special case for handling modelspec keys
for (const key of Object.keys(result)) {
if (key.startsWith("modelspec.")) {
result[key.replaceAll(".", "/")] = result[key];
delete result[key];
}
}
// Add empty keys to designate hierarchy
for (const key of Object.keys(result)) {
const parts = key.split("/");
for (let i = 1; i < parts.length; i++) {
const parent = parts.slice(0, i).join("/");
if (!result[parent]) {
result[parent] = "";
}
}
}
return result;
}
function extraNetworksShowMetadata(text) { function extraNetworksShowMetadata(text) {
try {
let parsed = JSON.parse(text);
if (parsed && typeof parsed === 'object') {
parsed = extraNetworksFlattenMetadata(parsed);
const table = createVisualizationTable(parsed, 0);
popup(table);
return;
}
} catch (error) { console.debug(error); }
var elem = document.createElement('pre'); var elem = document.createElement('pre');
elem.classList.add('popup-metadata'); elem.classList.add('popup-metadata');
elem.textContent = text; elem.textContent = text;
popup(elem); popup(elem);
return;
} }
function requestGet(url, data, handler, errorHandler) { function requestGet(url, data, handler, errorHandler) {

View File

@ -33,120 +33,141 @@ function createRow(table, cellName, items) {
return res; return res;
} }
function showProfile(path, cutoff = 0.05) { function createVisualizationTable(data, cutoff = 0, sort = "") {
requestGet(path, {}, function(data) { var table = document.createElement('table');
var table = document.createElement('table'); table.className = 'popup-table';
table.className = 'popup-table';
data.records['total'] = data.total; var keys = Object.keys(data);
var keys = Object.keys(data.records).sort(function(a, b) { if (sort === "number") {
return data.records[b] - data.records[a]; keys = keys.sort(function(a, b) {
return data[b] - data[a];
}); });
var items = keys.map(function(x) { } else {
return {key: x, parts: x.split('/'), time: data.records[x]}; keys = keys.sort();
}
var items = keys.map(function(x) {
return {key: x, parts: x.split('/'), value: data[x]};
});
var maxLength = items.reduce(function(a, b) {
return Math.max(a, b.parts.length);
}, 0);
var cols = createRow(
table,
'th',
[
cutoff === 0 ? 'key' : 'record',
cutoff === 0 ? 'value' : 'seconds'
]
);
cols[0].colSpan = maxLength;
function arraysEqual(a, b) {
return !(a < b || b < a);
}
var addLevel = function(level, parent, hide) {
var matching = items.filter(function(x) {
return x.parts[level] && !x.parts[level + 1] && arraysEqual(x.parts.slice(0, level), parent);
}); });
var maxLength = items.reduce(function(a, b) { if (sort === "number") {
return Math.max(a, b.parts.length); matching = matching.sort(function(a, b) {
}, 0); return b.value - a.value;
});
var cols = createRow(table, 'th', ['record', 'seconds']); } else {
cols[0].colSpan = maxLength; matching = matching.sort();
function arraysEqual(a, b) {
return !(a < b || b < a);
} }
var othersTime = 0;
var othersList = [];
var othersRows = [];
var childrenRows = [];
matching.forEach(function(x) {
var visible = (cutoff === 0 && !hide) || (x.value >= cutoff && !hide);
var addLevel = function(level, parent, hide) { var cells = [];
var matching = items.filter(function(x) { for (var i = 0; i < maxLength; i++) {
return x.parts[level] && !x.parts[level + 1] && arraysEqual(x.parts.slice(0, level), parent); cells.push(x.parts[i]);
}); }
var sorted = matching.sort(function(a, b) { cells.push(cutoff === 0 ? x.value : x.value.toFixed(3));
return b.time - a.time; var cols = createRow(table, 'td', cells);
}); for (i = 0; i < level; i++) {
var othersTime = 0; cols[i].className = 'muted';
var othersList = []; }
var othersRows = [];
var childrenRows = [];
sorted.forEach(function(x) {
var visible = x.time >= cutoff && !hide;
var cells = []; var tr = cols[0].parentNode;
for (var i = 0; i < maxLength; i++) { if (!visible) {
cells.push(x.parts[i]); tr.classList.add("hidden");
} }
cells.push(x.time.toFixed(3));
var cols = createRow(table, 'td', cells);
for (i = 0; i < level; i++) {
cols[i].className = 'muted';
}
var tr = cols[0].parentNode; if (cutoff === 0 || x.value >= cutoff) {
if (!visible) { childrenRows.push(tr);
tr.classList.add("hidden"); } else {
} othersTime += x.value;
othersList.push(x.parts[level]);
if (x.time >= cutoff) { othersRows.push(tr);
childrenRows.push(tr); }
} else {
othersTime += x.time;
othersList.push(x.parts[level]);
othersRows.push(tr);
}
var children = addLevel(level + 1, parent.concat([x.parts[level]]), true);
if (children.length > 0) {
var cell = cols[level];
var onclick = function() {
cell.classList.remove("link");
cell.removeEventListener("click", onclick);
children.forEach(function(x) {
x.classList.remove("hidden");
});
};
cell.classList.add("link");
cell.addEventListener("click", onclick);
}
});
if (othersTime > 0) {
var cells = [];
for (var i = 0; i < maxLength; i++) {
cells.push(parent[i]);
}
cells.push(othersTime.toFixed(3));
cells[level] = 'others';
var cols = createRow(table, 'td', cells);
for (i = 0; i < level; i++) {
cols[i].className = 'muted';
}
var children = addLevel(level + 1, parent.concat([x.parts[level]]), true);
if (children.length > 0) {
var cell = cols[level]; var cell = cols[level];
var tr = cell.parentNode;
var onclick = function() { var onclick = function() {
tr.classList.add("hidden");
cell.classList.remove("link"); cell.classList.remove("link");
cell.removeEventListener("click", onclick); cell.removeEventListener("click", onclick);
othersRows.forEach(function(x) { children.forEach(function(x) {
x.classList.remove("hidden"); x.classList.remove("hidden");
}); });
}; };
cell.title = othersList.join(", ");
cell.classList.add("link"); cell.classList.add("link");
cell.addEventListener("click", onclick); cell.addEventListener("click", onclick);
}
});
if (hide) { if (othersTime > 0) {
tr.classList.add("hidden"); var cells = [];
} for (var i = 0; i < maxLength; i++) {
cells.push(parent[i]);
childrenRows.push(tr); }
cells.push(othersTime.toFixed(3));
cells[level] = 'others';
var cols = createRow(table, 'td', cells);
for (i = 0; i < level; i++) {
cols[i].className = 'muted';
} }
return childrenRows; var cell = cols[level];
}; var tr = cell.parentNode;
var onclick = function() {
tr.classList.add("hidden");
cell.classList.remove("link");
cell.removeEventListener("click", onclick);
othersRows.forEach(function(x) {
x.classList.remove("hidden");
});
};
addLevel(0, []); cell.title = othersList.join(", ");
cell.classList.add("link");
cell.addEventListener("click", onclick);
if (hide) {
tr.classList.add("hidden");
}
childrenRows.push(tr);
}
return childrenRows;
};
addLevel(0, []);
return table;
}
function showProfile(path, cutoff = 0.05) {
requestGet(path, {}, function(data) {
data.records['total'] = data.total;
const table = createVisualizationTable(data.records, cutoff, "number");
popup(table); popup(table);
}); });
} }