Skip to content

Commit

Permalink
tf fix function
Browse files Browse the repository at this point in the history
  • Loading branch information
Chizkiyahu committed Oct 8, 2023
1 parent 8f57949 commit 1af3feb
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 12 deletions.
75 changes: 73 additions & 2 deletions source/tf.js
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,7 @@ tf.Attribute = class {
}
case 'func': {
this._type = 'function';
this._value = new tf.Node(metadata, { op: value.func.name, attr: value.func.attr }, null, new tf.Context());
this._value = tf.Builder.buildFuncAttrObj(value.func);
break;
}
case 'placeholder': {
Expand All @@ -1131,7 +1131,7 @@ tf.Attribute = class {
this._value = list.shape.map((shape) => new tf.TensorShape(shape));
} else if (list.func && list.func.length > 0) {
this._type = 'function[]';
this._value = list.func.map((func) => new tf.Node(metadata, { op: func.name, attr: func.attr }));
this._value = list.func.map((func) => tf.Builder.buildFuncAttrObj(func));
} else {
this._value = [];
}
Expand Down Expand Up @@ -2485,6 +2485,77 @@ tf.JsonReader = class {
return message;
}
};
class FuncCont {
constructor(name, attr) {
this.name = name;
this.attr = attr;
}
}

class FuncAttr {
constructor(type, value) {
this.type = type;
this.value = value;
}
}

class Builder {
static buildFuncAttrObj(func) {
const objectContent = new Map();
const attr = func.attr;
if (attr === null || typeof attr === 'undefined') {
return new FuncCont(func.name, objectContent);
}
Object.keys(attr).forEach(key => {
const value = attr[key];
switch (value.value) {
case 'i':
objectContent.set(key, new FuncAttr('i', value.i));
break;
case 'f':
objectContent.set(key, new FuncAttr('f', value.f));
break;
case 'b':
objectContent.set(key, new FuncAttr('b', value.b));
break;
case 's':
objectContent.set(key, new FuncAttr('s', tf.Utility.decodeText(value.s)));
break;
case 'func': {
objectContent.set(key, new FuncAttr('func', Builder.buildFuncAttrObj(value.func)));
break;
}
case 'list': {
const list = value.list;
if (list.s && list.s.length > 0) {
objectContent.set(key, new FuncAttr('list', list.s.map(s => tf.Utility.decodeText(s))));
} else if (list.i && list.i.length > 0) {
objectContent.set(key, new FuncAttr('list', list.i));
} else if (list.f && list.f.length > 0) {
objectContent.set(key, new FuncAttr('list', list.f));
} else if (list.func && list.func.length > 0) {
if (attr.attr === null || typeof attr.attr === 'undefined') {
objectContent.set(key, new FuncAttr('s', JSON.stringify(attr)));
} else {
objectContent.set(key, new tf.FuncAttr("list", list.func.map(attr=>this.buildFuncAttrObj(attr.attr))));
}
} else {
objectContent.set(key, new FuncAttr('list', []));
}
break;
}
default:
break;
}
});

return new FuncCont(func.name, objectContent);
}
}

tf.FuncCont = FuncCont;
tf.FuncAttr = FuncAttr;
tf.Builder = Builder;

tf.Error = class extends Error {

Expand Down
98 changes: 88 additions & 10 deletions source/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -2520,10 +2520,7 @@ view.AttributeView = class extends view.Control {
}
case 'function': {
const line = this.createElement('div', 'sidebar-item-value-line-link');
line.innerHTML = value.type.name;
line.addEventListener('click', () => {
this.emit('show-graph', value.type);
});
line.innerHTML = value.name;
this._element.appendChild(line);
break;
}
Expand All @@ -2550,20 +2547,101 @@ view.AttributeView = class extends view.Control {
}

toggle() {
if (this._expander.innerText == '+') {
if (this._expander.innerText === '+') {
this._expander.innerText = '-';
const typeLine = this.createElement('div', 'sidebar-item-value-line-link');
const type = this._attribute.type;
const value = this._attribute.value;
const content = type == 'tensor' && value && value.type ? value.type.toString() : this._attribute.type;
const typeLine = this.createElement('div', 'sidebar-item-value-line-border');
typeLine.innerHTML = 'type: ' + '<code><b>' + content + '</b></code>';
this._element.appendChild(typeLine);
if (type === 'tensor' && value && value.type) {
typeLine.innerHTML = 'type: ' + '<code><b>' + value.type.toString() + '</b></code>';
this._element.appendChild(typeLine);
} else if (!type.startsWith('function')) {
typeLine.innerHTML = 'type: ' + '<code><b>' + this._attribute.type + '</b></code>';
this._element.appendChild(typeLine);
}
const description = this._attribute.description;
if (description) {
const descriptionLine = this.createElement('div', 'sidebar-item-value-line-border');
descriptionLine.innerHTML = description;
this._element.appendChild(descriptionLine);
}
if (this._attribute.type === 'tensor' && value) {
const state = value.state;
const valueLine = this.createElement('div', 'sidebar-item-value-line-border');
const contentLine = this.createElement('pre');
contentLine.innerHTML = state || value.toString();
valueLine.appendChild(contentLine);
this._element.appendChild(valueLine);
} else if (this._attribute.type.startsWith('function') && value) {


// eslint-disable-next-line no-inner-declarations
function createInnerAttr(host, nav, innerAttr, indentNum, index) {
const valueLineName = host.createElement('div');

const expander = host.createElement('div');
expander.className = 'sidebar-item-value-expander';
expander.innerText = '+';
valueLineName.appendChild(expander);
expander.addEventListener('click', () => {
// eslint-disable-next-line no-use-before-define
toggleInnerFunc(host, valueLineName, expander, innerAttr, indentNum+1);
});

const contentLineName = host.createElement('div');
contentLineName.className= 'sidebar-item-value-line';
contentLineName.innerHTML = (innerAttr.name + (index!=undefined ? '['+index + ']' : '')).bold();
contentLineName.style.paddingLeft = (6+indentNum*4).toString()+'px';
valueLineName.appendChild(contentLineName);

nav.appendChild(valueLineName);
}
// eslint-disable-next-line no-inner-declarations
function openFunction(host, nav, iInnerAttr, indentNum) {
iInnerAttr.attr.forEach((v, k) => {
const iValueLine = host.createElement('div', 'sidebar-item-value-line');
iValueLine.style.paddingLeft=(6+indentNum*4).toString()+'px';
switch (v.type) {
case 'func':
iValueLine.innerAttr =createInnerAttr (host, nav, v.value, indentNum+1);
break;
default:
iValueLine.innerHTML = k.bold() + ":" + v.value.toString();
nav.appendChild(iValueLine);
}
});
}


// eslint-disable-next-line no-inner-declarations
function openFunctionList(host, nav, l) {
l.forEach((v, k) => {
const indent = 1;
const iValueLine = host.createElement('div', 'sidebar-item-value-line');
iValueLine.innerAttr = createInnerAttr(host, nav, v, indent, k);
iValueLine.style.paddingLeft = (6+indent*4).toString + 'px';
});
}
if (this._attribute.type==='function') {
openFunction(this._host.document, this._element, value, 1);
} else if (this._attribute.type==='function[]') {
openFunctionList(this, this._element, value);
}

// eslint-disable-next-line no-inner-declarations
function toggleInnerFunc(host, nav, expander, iInnerAttr, indentNum) {
if (expander.innerText==='+') {
expander.innerText='-';
openFunction(host, nav, iInnerAttr, indentNum);
} else {
expander.innerText='+';
while (nav.childElementCount > 2) {
nav.removeChild(nav.lastChild);
}
}

}
}
} else {
this._expander.innerText = '+';
while (this._element.childElementCount > 2) {
Expand Down Expand Up @@ -3950,7 +4028,7 @@ view.Formatter = class {
return value.type.name;
case 'object[]':
case 'function[]':
return value ? value.map((item) => item.type.name).join(', ') : '(null)';
return "List";
case 'type':
return value ? value.toString() : '(null)';
case 'type[]':
Expand Down

0 comments on commit 1af3feb

Please sign in to comment.