Skip to content

Commit

Permalink
Update view.js (#637)
Browse files Browse the repository at this point in the history
  • Loading branch information
lutzroeder committed Jul 7, 2024
1 parent 08aa769 commit 7648424
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 173 deletions.
51 changes: 24 additions & 27 deletions source/pickle.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ pickle.Node = class {
this.name = name || '';
this.inputs = [];
this.outputs = [];
this.attributes = [];
const isArray = (obj) => {
return obj && obj.__class__ &&
((obj.__class__.__module__ === 'numpy' && obj.__class__.__name__ === 'ndarray') ||
Expand All @@ -111,38 +110,38 @@ pickle.Node = class {
return false;
};
if (type === 'builtins.bytearray') {
const attribute = new pickle.Argument('value', Array.from(obj), 'byte[]');
this.attributes.push(attribute);
const argument = new pickle.Argument('value', Array.from(obj), 'byte[]');
this.inputs.push(argument);
} else {
const entries = obj instanceof Map ? Array.from(obj) : Object.entries(obj);
for (const [name, value] of entries) {
if (name === '__class__') {
continue;
} else if (value && isArray(value)) {
const tensor = new pickle.Tensor(value);
const attribute = new pickle.Argument(name, tensor, 'tensor');
this.attributes.push(attribute);
const argument = new pickle.Argument(name, tensor, 'tensor');
this.inputs.push(argument);
} else if (Array.isArray(value) && value.length > 0 && value.every((obj) => isArray(obj))) {
const tensors = value.map((obj) => new pickle.Tensor(obj));
const attribute = new pickle.Argument(name, tensors, 'tensor[]');
this.attributes.push(attribute);
const argument = new pickle.Argument(name, tensors, 'tensor[]');
this.inputs.push(argument);
} else if (value && value.__class__ && value.__class__.__module__ === 'builtins' && (value.__class__.__name__ === 'function' || value.__class__.__name__ === 'type')) {
const obj = {};
obj.__class__ = value;
const node = new pickle.Node(obj, '', stack);
const attribute = new pickle.Argument(name, node, 'object');
this.attributes.push(attribute);
const argument = new pickle.Argument(name, node, 'object');
this.inputs.push(argument);
} else if (isByteArray(value)) {
const attribute = new pickle.Argument(name, Array.from(value), 'byte[]');
this.attributes.push(attribute);
const argument = new pickle.Argument(name, Array.from(value), 'byte[]');
this.inputs.push(argument);
} else {
stack = stack || new Set();
if (value && Array.isArray(value) && value.every((obj) => typeof obj === 'string')) {
const attribute = new pickle.Argument(name, value, 'string[]');
this.attributes.push(attribute);
const argument = new pickle.Argument(name, value, 'string[]');
this.inputs.push(argument);
} else if (value && Array.isArray(value) && value.every((obj) => typeof obj === 'number')) {
const attribute = new pickle.Argument(name, value);
this.attributes.push(attribute);
const argument = new pickle.Argument(name, value, 'attribute');
this.inputs.push(argument);
} else if (value && Array.isArray(value) && value.length > 0 && value.every((obj) => obj && (obj.__class__ || obj === Object(obj)))) {
const values = value.filter((value) => !stack.has(value));
const nodes = values.map((value) => {
Expand All @@ -151,19 +150,17 @@ pickle.Node = class {
stack.delete(value);
return node;
});
const attribute = new pickle.Argument(name, nodes, 'object[]');
this.attributes.push(attribute);
} else if (value && (value.__class__ || isObject(value))) {
if (!stack.has(value)) {
stack.add(value);
const node = new pickle.Node(value, '', stack);
const attribute = new pickle.Argument(name, node, 'object');
this.attributes.push(attribute);
stack.delete(value);
}
const argument = new pickle.Argument(name, nodes, 'object[]');
this.inputs.push(argument);
} else if (value && (value.__class__ || isObject(value)) && !stack.has(value)) {
stack.add(value);
const node = new pickle.Node(value, '', stack);
const argument = new pickle.Argument(name, node, 'object');
this.inputs.push(argument);
stack.delete(value);
} else {
const attribute = new pickle.Argument(name, value);
this.attributes.push(attribute);
const argument = new pickle.Argument(name, value, 'attribute');
this.inputs.push(argument);
}
}
}
Expand Down
49 changes: 28 additions & 21 deletions source/pytorch.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ pytorch.Node = class {
};
const createAttribute = (metadata, name, value) => {
let visible = true;
let type = null;
let type = 'attribute';
if (name === 'training') {
visible = false;
type = 'boolean';
Expand Down Expand Up @@ -386,18 +386,22 @@ pytorch.Node = class {
const argument = new pytorch.Argument(name, values, null, visible);
this.inputs.push(argument);
}
this.attributes = Array.from(attributes).map(([name, value]) => {
for (const [name, value] of attributes) {
const type = this.type.identifier;
if (pytorch.Utility.isTensor(value)) {
const tensor = new pytorch.Tensor('', value);
return new pytorch.Argument(name, tensor, 'tensor');
const argument = new pytorch.Argument(name, tensor, 'tensor');
this.inputs.push(argument);
} else if (Array.isArray(value) && value.every((value) => pytorch.Utility.isTensor(value))) {
const tensors = value.map((value) => new pytorch.Tensor('', value));
return new pytorch.Argument(name, tensors, 'tensor[]');
const argument = new pytorch.Argument(name, tensors, 'tensor[]');
this.inputs.push(argument);
} else if (Array.isArray(value) && value.every((value) => typeof value === 'string')) {
return new pytorch.Argument(name, value, 'string[]');
const argument = new pytorch.Argument(name, value, 'string[]');
this.inputs.push(argument);
} else if (Array.isArray(value) && value.every((value) => typeof value === 'number')) {
return new pytorch.Argument(name, value);
const argument = new pytorch.Argument(name, value, 'attribute');
this.inputs.push(argument);
} else if (name === '_modules' && value && value.__class__ && value.__class__.__module__ === 'collections' && value.__class__.__name__ === 'OrderedDict' &&
value instanceof Map && Array.from(value).every(([, value]) => value === null || value.__class__)) {
const values = Array.from(value).filter(([, value]) => !stack.has(value)).map(([name, obj]) => {
Expand All @@ -407,7 +411,8 @@ pytorch.Node = class {
stack.delete(value);
return node;
});
return new pytorch.Argument(name, values, 'object[]');
const argument = new pytorch.Argument(name, values, 'object[]');
this.inputs.push(argument);
} else if (value && Array.isArray(value) && value.length > 0 && value.every((obj) => obj && (obj.__class__ || obj === Object(obj)))) {
const values = value.filter((value) => !stack.has(value));
const nodes = values.map((value) => {
Expand All @@ -420,21 +425,23 @@ pytorch.Node = class {
stack.delete(value);
return node;
});
return new pytorch.Argument(name, nodes, 'object[]');
} else if (value && (value.__class__ || isObject(value))) {
if (!stack.has(value)) {
stack.add(value);
const item = {
type: value.__class__ ? `${value.__class__.__module__}.${value.__class__.__name__}` : 'builtins.object',
obj: value
};
const node = new pytorch.Node(metadata, group, item, initializers, values, stack);
stack.delete(value);
return new pytorch.Argument(name, node, 'object');
}
const argument = new pytorch.Argument(name, nodes, 'object[]');
this.inputs.push(argument);
} else if (value && (value.__class__ || isObject(value)) && !stack.has(value)) {
stack.add(value);
const item = {
type: value.__class__ ? `${value.__class__.__module__}.${value.__class__.__name__}` : 'builtins.object',
obj: value
};
const node = new pytorch.Node(metadata, group, item, initializers, values, stack);
stack.delete(value);
const argument = new pytorch.Argument(name, node, 'object');
this.inputs.push(argument);
} else {
const argument = createAttribute(metadata.attribute(type, name), name, value);
this.inputs.push(argument);
}
return createAttribute(metadata.attribute(type, name), name, value);
});
}
} else {
this.attributes = [];
this.inputs = [];
Expand Down
56 changes: 25 additions & 31 deletions source/sklearn.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ sklearn.Node = class {
this.type = metadata.type(type) || { name: type };
this.inputs = inputs.map((input) => new sklearn.Argument(input, [values.map(input)]));
this.outputs = outputs.map((output) => new sklearn.Argument(output, [values.map(output)]));
this.attributes = [];
const isArray = (obj) => {
return obj && obj.__class__ &&
((obj.__class__.__module__ === 'numpy' && obj.__class__.__name__ === 'ndarray') ||
Expand All @@ -220,37 +219,37 @@ sklearn.Node = class {
};
if (type === 'builtins.bytearray') {
const attribute = new sklearn.Argument('value', Array.from(obj), 'byte[]');
this.attributes.push(attribute);
this.inputs.push(attribute);
} else {
const entries = Object.entries(obj);
for (const [name, value] of entries) {
if (name === '__class__') {
continue;
} else if (value && isArray(value)) {
const tensor = new sklearn.Tensor(value);
const attribute = new sklearn.Argument(name, tensor, 'tensor');
this.attributes.push(attribute);
const argument = new sklearn.Argument(name, tensor, 'tensor');
this.inputs.push(argument);
} else if (Array.isArray(value) && value.length > 0 && value.every((obj) => isArray(obj))) {
const tensors = value.map((obj) => new sklearn.Tensor(obj));
const attribute = new sklearn.Argument(name, tensors, 'tensor[]');
this.attributes.push(attribute);
const argument = new sklearn.Argument(name, tensors, 'tensor[]');
this.inputs.push(argument);
} else if (isByteArray(value)) {
const attribute = new sklearn.Argument(name, Array.from(value), 'byte[]');
this.attributes.push(attribute);
const argument = new sklearn.Argument(name, Array.from(value), 'byte[]');
this.inputs.push(argument);
} else {
stack = stack || new Set();
if (value && Array.isArray(value) && value.every((obj) => typeof obj === 'string')) {
const attribute = new sklearn.Argument(name, value, 'string[]');
this.attributes.push(attribute);
const argument = new sklearn.Argument(name, value, 'string[]');
this.inputs.push(argument);
} else if (value && Array.isArray(value) && value.every((obj) => typeof obj === 'number')) {
const attribute = new sklearn.Argument(name, value);
this.attributes.push(attribute);
const argument = new sklearn.Argument(name, value, 'attribute');
this.inputs.push(argument);
} else if (value && value.__class__ && value.__class__.__module__ === 'builtins' && (value.__class__.__name__ === 'function' || value.__class__.__name__ === 'type')) {
const obj = {};
obj.__class__ = value;
const node = new sklearn.Node(metadata, group, '', obj, [], [], null, stack);
const attribute = new sklearn.Argument(name, node, 'object');
this.attributes.push(attribute);
const argument = new sklearn.Argument(name, node, 'object');
this.inputs.push(argument);
} else if (value && Array.isArray(value) && value.length > 0 && value.every((obj) => obj && (obj.__class__ || obj === Object(obj)))) {
const values = value.filter((value) => !stack.has(value));
const nodes = values.map((value) => {
Expand All @@ -259,21 +258,19 @@ sklearn.Node = class {
stack.delete(value);
return node;
});
const attribute = new sklearn.Argument(name, nodes, 'object[]');
this.attributes.push(attribute);
} else if (value && (value.__class__ || isObject(value))) {
if (!stack.has(value)) {
stack.add(value);
const node = new sklearn.Node(metadata, group, '', value, [], [], null, stack);
const attribute = new sklearn.Argument(name, node, 'object');
this.attributes.push(attribute);
stack.delete(value);
}
const argument = new sklearn.Argument(name, nodes, 'object[]');
this.inputs.push(argument);
} else if (value && (value.__class__ || isObject(value)) && !stack.has(value)) {
stack.add(value);
const node = new sklearn.Node(metadata, group, '', value, [], [], null, stack);
const argument = new sklearn.Argument(name, node, 'object');
this.inputs.push(argument);
stack.delete(value);
} else {
let type = 'attribute';
let visible = true;
const schema = metadata.attribute(type, name);
if (schema) {
let type = '';
let visible = true;
if (schema.type) {
type = schema.type;
}
Expand All @@ -290,12 +287,9 @@ sklearn.Node = class {
visible = value !== schema.default;
}
}
const attribute = new sklearn.Argument(name, value, type, visible);
this.attributes.push(attribute);
} else {
const attribute = new sklearn.Argument(name, value);
this.attributes.push(attribute);
}
const argument = new sklearn.Argument(name, value, type, visible);
this.inputs.push(argument);
}
}
}
Expand Down
Loading

0 comments on commit 7648424

Please sign in to comment.