Skip to content

Commit f75129b

Browse files
committed
Tensor sparsity metric (#1240) (#1293)
1 parent dba6cf5 commit f75129b

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

source/view.js

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3162,8 +3162,23 @@ view.TensorSidebar = class extends view.ObjectSidebar {
31623162
if (value.type) {
31633163
const item = new view.ValueView(this._view, value, '');
31643164
this.add('type', item);
3165-
item.toggle();
3165+
// item.toggle();
3166+
}
3167+
3168+
/*
3169+
// TODO
3170+
if (value.initializer) {
3171+
const tensor = new view.Tensor(value.initializer);
3172+
if (!tensor.empty) {
3173+
this.addHeader('Metrics');
3174+
const metrics = tensor.metrics;
3175+
for (const metric of metrics) {
3176+
const value = metric.type === 'percentage' ? `${(metric.value * 100).toFixed(1)}%` : metric.value;
3177+
this.addProperty(metric.name, [value]);
3178+
}
3179+
}
31663180
}
3181+
*/
31673182
}
31683183
};
31693184

@@ -4119,12 +4134,31 @@ view.Tensor = class {
41194134
}
41204135

41214136
get metrics() {
4122-
const metrics = Array.from(this._tensor.metrics || []);
4123-
const keys = new Set(metrics.map((metrics) => metrics.name));
4124-
if (!keys.has('sparisity')) {
4125-
// metrics.push(new view.Argument('sparisity', 0, 'float32'));
4137+
if (!this._metrics) {
4138+
const data = this.value;
4139+
this._metrics = Array.from(this._tensor.metrics || []);
4140+
const keys = new Set(this._metrics.map((metrics) => metrics.name));
4141+
if (!keys.has('sparsity')) {
4142+
let zeros = 0;
4143+
let parameters = 0;
4144+
const stack = [data];
4145+
while (stack.length > 0) {
4146+
const data = stack.pop();
4147+
if (Array.isArray(data)) {
4148+
for (const element of data) {
4149+
stack.push(element);
4150+
}
4151+
} else {
4152+
zeros += data === 0 || data === 0n || data === '';
4153+
parameters += 1;
4154+
}
4155+
}
4156+
const value = parameters > 0 ? zeros / parameters : 0;
4157+
const argument = new view.Argument('sparsity', value, 'percentage');
4158+
this._metrics.push(argument);
4159+
}
41264160
}
4127-
return metrics;
4161+
return this._metrics;
41284162
}
41294163
};
41304164

0 commit comments

Comments
 (0)