Skip to content

Commit ed2760e

Browse files
committed
Add HDF5 Weights test file (#467)
1 parent a420f86 commit ed2760e

File tree

2 files changed

+73
-10
lines changed

2 files changed

+73
-10
lines changed

source/hdf5.js

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,13 @@ hdf5.Variable = class {
239239
}
240240
const data = new Uint8Array(data_size * item_size);
241241
for (const node of tree.nodes) {
242-
if (node.filterMask !== 0) {
243-
return null;
244-
}
245242
let chunk = node.data;
246243
if (this._filterPipeline) {
247-
for (const filter of this._filterPipeline.filters) {
248-
chunk = filter.decode(chunk);
244+
for (let i = 0; i < this._filterPipeline.filters.length; i++) {
245+
if ((node.filterMask & (1 << i)) === 0) {
246+
const filter = this._filterPipeline.filters[i];
247+
chunk = filter.decode(chunk);
248+
}
249249
}
250250
}
251251
const chunk_offset = node.fields.map((x) => x.toNumber());
@@ -1364,14 +1364,70 @@ hdf5.Filter = class {
13641364

13651365
decode(data) {
13661366
switch (this.id) {
1367-
case 1: { // gzip
1368-
const archive = zip.Archive.open(data);
1369-
return archive.entries.get('').peek();
1367+
case 1: return this._deflate(data);
1368+
case 32000: return this._lzf(data);
1369+
default: throw new hdf5.Error(`Unsupported filter '${this.id}:${this.name}'.`);
1370+
}
1371+
}
1372+
1373+
_deflate(input) {
1374+
const archive = zip.Archive.open(input);
1375+
return archive.entries.get('').peek();
1376+
}
1377+
1378+
_lzf(input) {
1379+
let i = 0;
1380+
let o = 0;
1381+
while (i < input.length) {
1382+
let c = input[i++];
1383+
if (c < (1 << 5)) {
1384+
c++;
1385+
i += c;
1386+
o += c;
1387+
if (i > input.length) {
1388+
throw new Error('Invalid LZF compressed data.');
1389+
}
1390+
} else {
1391+
let length = c >> 5;
1392+
if (i >= input.length) {
1393+
throw new Error('Invalid LZF compressed data.');
1394+
}
1395+
if (length === 7) {
1396+
length += input[i++];
1397+
if (i >= input.length) {
1398+
throw new Error('Invalid LZF compressed data.');
1399+
}
1400+
}
1401+
const ref = (o - ((c & 0x1f) << 8) - 1) - input[i++];
1402+
if (ref < 0) {
1403+
throw new Error('Invalid LZF compressed data.');
1404+
}
1405+
o += length + 2;
13701406
}
1371-
default: {
1372-
throw new hdf5.Error(`Unsupported filter '${this.name}'.`);
1407+
}
1408+
const output = new Uint8Array(o);
1409+
i = 0;
1410+
o = 0;
1411+
while (i < input.length) {
1412+
let c = input[i++];
1413+
if (c < (1 << 5)) {
1414+
c++;
1415+
output.set(input.subarray(i, i + c), o);
1416+
i += c;
1417+
o += c;
1418+
} else {
1419+
let length = c >> 5;
1420+
if (length === 7) {
1421+
length += input[i++];
1422+
}
1423+
length += 2;
1424+
let ref = o - ((c & 0x1f) << 8) - 1 - input[i++];
1425+
do {
1426+
output[o++] = output[ref++];
1427+
} while (--length);
13731428
}
13741429
}
1430+
return output;
13751431
}
13761432
};
13771433

test/models.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2420,6 +2420,13 @@
24202420
"format": "Keras",
24212421
"link": "https://github.com/lutzroeder/netron/issues/57"
24222422
},
2423+
{
2424+
"type": "keras",
2425+
"target": "lzf.h5",
2426+
"source": "https://github.com/user-attachments/files/17692525/lzf.hdf5.zip[lzf.hdf5]",
2427+
"format": "HDF5 Weights",
2428+
"link": "https://github.com/lutzroeder/netron/issues/467"
2429+
},
24232430
{
24242431
"type": "keras",
24252432
"target": "mlp.h5",

0 commit comments

Comments
 (0)