Skip to content

Commit

Permalink
Tensor sparsity metric (#1240) (#1293)
Browse files Browse the repository at this point in the history
  • Loading branch information
lutzroeder committed Jun 15, 2024
1 parent dba6cf5 commit eee821e
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions source/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -3164,6 +3164,21 @@ view.TensorSidebar = class extends view.ObjectSidebar {
this.add('type', item);
item.toggle();
}

/*
// TODO
if (value.initializer) {
const tensor = new view.Tensor(value.initializer);
if (!tensor.empty) {
this.addHeader('Metrics');
const metrics = tensor.metrics;
for (const metric of metrics) {
const value = metric.type === 'percentage' ? `${(metric.value * 100).toFixed(1)}%` : metric.value;
this.addProperty(metric.name, [value]);
}
}
}
*/
}
};

Expand Down Expand Up @@ -4119,12 +4134,31 @@ view.Tensor = class {
}

get metrics() {
const metrics = Array.from(this._tensor.metrics || []);
const keys = new Set(metrics.map((metrics) => metrics.name));
if (!keys.has('sparisity')) {
// metrics.push(new view.Argument('sparisity', 0, 'float32'));
if (!this._metrics) {
const data = this.value;
this._metrics = Array.from(this._tensor.metrics || []);
const keys = new Set(this._metrics.map((metrics) => metrics.name));
if (!keys.has('sparsity')) {
let zeros = 0;
let parameters = 0;
const stack = [data];
while (stack.length > 0) {
const data = stack.pop();
if (Array.isArray(data)) {
for (const element of data) {
stack.push(element);
}
} else {
zeros += data === 0 || data === 0n || data === '';
parameters += 1;
}
}
const value = parameters > 0 ? zeros / parameters : 0;
const argument = new view.Argument('sparsity', value, 'percentage');
this._metrics.push(argument);
}
}
return metrics;
return this._metrics;
}
};

Expand Down

0 comments on commit eee821e

Please sign in to comment.