Skip to content

Commit

Permalink
Update text.js
Browse files Browse the repository at this point in the history
  • Loading branch information
lutzroeder committed Jun 2, 2024
1 parent 69d82c1 commit 78be4a9
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 141 deletions.
13 changes: 5 additions & 8 deletions source/coreml.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

import * as base from './base.js';
import * as text from './text.js';

const coreml = {};

Expand Down Expand Up @@ -53,13 +52,11 @@ coreml.ModelFactory = class {
}
if (identifier === 'model.mil') {
try {
const reader = text.Reader.open(context.stream, 2048);
const signature = reader.read();
if (signature !== undefined) {
if (signature.trim().startsWith('program')) {
context.type = 'coreml.mil';
return;
}
const reader = context.read('text', 2048);
const signature = reader.read('\n');
if (signature && signature.trim().startsWith('program')) {
context.type = 'coreml.mil';
return;
}
} catch {
// continue regardless of error
Expand Down
59 changes: 31 additions & 28 deletions source/darknet.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

import * as text from './text.js';

const darknet = {};

darknet.ModelFactory = class {
Expand All @@ -16,19 +14,21 @@ darknet.ModelFactory = class {
}
return;
}
try {
const reader = text.Reader.open(context.stream, 65536);
for (let line = reader.read(); line !== undefined; line = reader.read()) {
const content = line.trim();
if (content.length > 0 && !content.startsWith('#')) {
if (content.startsWith('[') && content.endsWith(']')) {
context.type = 'darknet.model';
}
return;
const reader = context.read('text', 65536);
if (reader) {
try {
for (let line = reader.read('\n'); line !== undefined; line = reader.read('\n')) {
const content = line.trim();
if (content.length > 0 && !content.startsWith('#')) {
if (content.startsWith('[') && content.endsWith(']')) {
context.type = 'darknet.model';
}
return;
}
}
} catch {
// continue regardless of error
}
} catch {
// continue regardless of error
}
}

Expand All @@ -43,19 +43,22 @@ darknet.ModelFactory = class {
const weights = context.target;
const name = `${basename}.cfg`;
const content = await context.fetch(name);
const reader = new darknet.Reader(content.stream, content.identifier);
return new darknet.Model(metadata, reader, weights);
const reader = content.read('text');
const configuration = new darknet.Configuration(reader, content.identifier);
return new darknet.Model(metadata, configuration, weights);
}
case 'darknet.model': {
try {
const name = `${basename}.weights`;
const content = await context.fetch(name);
const weights = darknet.Weights.open(content);
const reader = new darknet.Reader(context.stream, context.identifier);
return new darknet.Model(metadata, reader, weights);
const reader = context.read('text');
const configuration = new darknet.Configuration(reader, context.identifier);
return new darknet.Model(metadata, configuration, weights);
} catch {
const reader = new darknet.Reader(context.stream, context.identifier);
return new darknet.Model(metadata, reader, null);
const reader = context.read('text');
const configuration = new darknet.Configuration(reader, context.identifier);
return new darknet.Model(metadata, configuration, null);
}
}
default: {
Expand All @@ -67,20 +70,20 @@ darknet.ModelFactory = class {

darknet.Model = class {

constructor(metadata, reader, weights) {
constructor(metadata, configuration, weights) {
this.format = 'Darknet';
this.graphs = [new darknet.Graph(metadata, reader, weights)];
this.graphs = [new darknet.Graph(metadata, configuration, weights)];
}
};

darknet.Graph = class {

constructor(metadata, reader, weights) {
constructor(metadata, configuration, weights) {
this.inputs = [];
this.outputs = [];
this.nodes = [];
const params = {};
const sections = reader.read();
const sections = configuration.read();
const globals = new Map();
const net = sections.shift();
const option_find_int = (options, key, defaultValue) => {
Expand Down Expand Up @@ -872,21 +875,21 @@ darknet.TensorShape = class {
}
};

darknet.Reader = class {
darknet.Configuration = class {

constructor(stream, identifier) {
this.stream = stream;
constructor(reader, identifier) {
this.reader = reader;
this.identifier = identifier;
}

read() {
// read_cfg
const sections = [];
let section = null;
const reader = text.Reader.open(this.stream);
const reader = this.reader;
let lineNumber = 0;
const setup = /^setup.*\.cfg$/.test(this.identifier);
for (let content = reader.read(); content !== undefined; content = reader.read()) {
for (let content = reader.read('\n'); content !== undefined; content = reader.read('\n')) {
lineNumber++;
const line = content.replace(/\s/g, '');
if (line.length > 0) {
Expand Down
2 changes: 1 addition & 1 deletion source/dlc.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ dlc.Container = class {
delete this._metadata;
const reader = text.Reader.open(stream);
for (;;) {
const line = reader.read();
const line = reader.read('\n');
if (line === undefined) {
break;
}
Expand Down
17 changes: 12 additions & 5 deletions source/mlir.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@
// Experimental
// contributor @tucan9389

import * as text from './text.js';

const mlir = {};

mlir.ModelFactory = class {

match(context) {
context.type = 'mlir';
try {
const reader = context.read('text', 0x10000);
for (let line = reader.read('\n'); line !== undefined; line = reader.read('\n')) {
if (/module\s+(\w+\s+)?{/.test(line) || /tensor<\w+>/.test(line)) {
context.type = 'mlir';
return;
}
}
} catch {
// continue regardless of error
}
}

async open(context) {
const stream = context.stream;
const decoder = text.Decoder.open(stream);
const decoder = context.read('text.decoder');
const parser = new mlir.Parser(decoder);
const obj = parser.read();
return new mlir.Model(obj);
Expand Down
54 changes: 26 additions & 28 deletions source/ncnn.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

import * as base from './base.js';
import * as text from './text.js';

const ncnn = {};

Expand All @@ -22,21 +21,23 @@ ncnn.ModelFactory = class {
}
}
} else if (identifier.endsWith('.param') || identifier.endsWith('.cfg.ncnn')) {
try {
const reader = text.Reader.open(context.stream, 2048);
const signature = reader.read();
if (signature !== undefined) {
if (signature.trim() === '7767517') {
context.type = 'ncnn.model';
return;
}
const header = signature.trim().split(' ');
if (header.length === 2 && header.every((value) => value >>> 0 === parseFloat(value))) {
context.type = 'ncnn.model';
const reader = context.read('text', 0x10000);
if (reader) {
try {
const signature = reader.read('\n');
if (signature !== undefined) {
if (signature.trim() === '7767517') {
context.type = 'ncnn.model';
return;
}
const header = signature.trim().split(' ');
if (header.length === 2 && header.every((value) => value >>> 0 === parseFloat(value))) {
context.type = 'ncnn.model';
}
}
} catch {
// continue regardless of error
}
} catch {
// continue regardless of error
}
} else if (identifier.endsWith('.bin') || identifier.endsWith('.weights.ncnn')) {
const stream = context.stream;
Expand Down Expand Up @@ -80,8 +81,8 @@ ncnn.ModelFactory = class {
const reader = new ncnn.BinaryParamReader(param);
return new ncnn.Model(metadata, reader, bin);
};
const openText = (param, bin) => {
const reader = new ncnn.TextParamReader(param);
const openText = (reader, bin) => {
reader = new ncnn.TextParamReader(reader);
return new ncnn.Model(metadata, reader, bin);
};
const identifier = context.identifier.toLowerCase();
Expand All @@ -93,12 +94,13 @@ ncnn.ModelFactory = class {
} else if (identifier.endsWith('.cfg.ncnn')) {
bin = `${context.identifier.substring(0, context.identifier.length - 9)}.weights.ncnn`;
}
const reader = context.read('text');
try {
const content = await context.fetch(bin);
const buffer = content.stream.peek();
return openText(context.stream.peek(), buffer);
return openText(reader, buffer);
} catch {
return openText(context.stream.peek(), null);
return openText(reader, null);
}
}
case 'ncnn.model.bin': {
Expand All @@ -120,8 +122,8 @@ ncnn.ModelFactory = class {
}
try {
const content = await context.fetch(file);
const buffer = content.stream.peek();
return openText(buffer, context.stream.peek());
const reader = content.read('text');
return openText(reader, context.stream.peek());
} catch {
const content = await context.fetch(`${file}.bin`);
const buffer = content.stream.peek();
Expand Down Expand Up @@ -634,15 +636,11 @@ ncnn.Utility = class {

ncnn.TextParamReader = class {

constructor(buffer) {
const reader = text.Reader.open(buffer);
constructor(reader) {
const lines = [];
for (;;) {
const line = reader.read();
if (line === undefined) {
break;
}
lines.push(line.trim());
for (let line = reader.read('\n'); line !== undefined; line = reader.read('\n')) {
line = line.trim();
lines.push(line);
}
const signature = lines.shift();
const header = (signature === '7767517' ? lines.shift() : signature).split(' ');
Expand Down
9 changes: 3 additions & 6 deletions source/nnabla.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

import * as text from './text.js';

const nnabla = {};

nnabla.ModelFactory = class {
Expand Down Expand Up @@ -29,10 +27,9 @@ nnabla.ModelFactory = class {
let version = '';
if (contexts.has('nnp_version.txt')) {
const context = contexts.get('nnp_version.txt');
const stream = context.stream;
const reader = text.Reader.open(stream);
version = reader.read();
version = version.split('\r').shift();
const reader = context.read('text');
const line = reader.read('\n');
version = line.split('\r').shift();
}
if (contexts.has('parameter.protobuf')) {
const context = contexts.get('parameter.protobuf');
Expand Down
36 changes: 17 additions & 19 deletions source/nnef.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@

import * as text from './text.js';

const nnef = {};

nnef.ModelFactory = class {

match(context) {
const identifier = context.identifier;
const extension = identifier.split('.').pop().toLowerCase();
const stream = context.stream;
switch (extension) {
case 'nnef':
if (nnef.TextReader.open(stream)) {
case 'nnef': {
const reader = nnef.TextReader.open(context);
if (reader) {
context.type = 'nnef.graph';
context.target = reader;
}
break;
case 'dat':
}
case 'dat': {
const stream = context.stream;
if (stream && stream.length > 2) {
const buffer = stream.peek(2);
if (buffer[0] === 0x4E && buffer[1] === 0xEF) {
context.type = 'nnef.dat';
context.target = stream;
}
}
break;
}
default:
break;
}
Expand All @@ -35,8 +38,7 @@ nnef.ModelFactory = class {
async open(context) {
switch (context.type) {
case 'nnef.graph': {
const stream = context.stream;
const reader = nnef.TextReader.open(stream);
const reader = context.target;
throw new nnef.Error(`NNEF v${reader.version} support not implemented.`);
}
case 'nnef.dat': {
Expand All @@ -51,13 +53,13 @@ nnef.ModelFactory = class {

nnef.TextReader = class {

static open(stream) {
const reader = text.Reader.open(stream);
static open(context) {
const reader = context.read('text', 65536);
for (let i = 0; i < 32; i++) {
const line = reader.read();
const line = reader.read('\n');
const match = /version\s*(\d+\.\d+);/.exec(line);
if (match) {
return new nnef.TextReader(stream, match[1]);
return new nnef.TextReader(context, match[1]);
}
if (line === undefined) {
break;
Expand All @@ -66,13 +68,9 @@ nnef.TextReader = class {
return null;
}

constructor(stream, version) {
this._stream = stream;
this._version = version;
}

get version() {
return this._version;
constructor(context, version) {
this.context = context;
this.version = version;
}
};

Expand Down
Loading

0 comments on commit 78be4a9

Please sign in to comment.