| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745 |
- var tnn = {};
- var text = require('./text');
- var base = require('./base');
- tnn.ModelFactory = class {
- match(context) {
- const identifier = context.identifier.toLowerCase();
- const stream = context.stream;
- if (stream && identifier.endsWith('.tnnproto')) {
- try {
- const buffer = stream.peek();
- const reader = text.Reader.open(buffer, 2048);
- const content = reader.read();
- if (content !== undefined) {
- const line = content.trim();
- if (line.startsWith('"') && line.endsWith('"')) {
- const header = line.replace(/(^")|("$)/g, '').split(',').shift().trim().split(' ');
- if (header.length === 3 || (header.length >= 4 && (header[3] === '4206624770' || header[3] == '4206624772'))) {
- return 'tnn.model';
- }
- }
- }
- }
- catch (err) {
- // continue regardless of error
- }
- }
- if (stream && identifier.endsWith('.tnnmodel')) {
- for (const signature of [ [ 0x02, 0x00, 0xbc, 0xfa ], [ 0x04, 0x00, 0xbc, 0xfa ] ]) {
- if (signature.length <= stream.length && stream.peek(signature.length).every((value, index) => value === signature[index])) {
- return 'tnn.params';
- }
- }
- }
- return '';
- }
- open(context, match) {
- return context.metadata('tnn-metadata.json').then((metadata) => {
- switch (match) {
- case 'tnn.model': {
- const tnnmodel = context.identifier.substring(0, context.identifier.length - 9) + '.tnnmodel';
- return context.request(tnnmodel, null).then((stream) => {
- const buffer = stream.peek();
- return new tnn.Model(metadata, context.stream.peek(), buffer);
- }).catch(() => {
- return new tnn.Model(metadata, context.stream.peek(), null);
- });
- }
- case 'tnn.params': {
- const tnnproto = context.identifier.substring(0, context.identifier.length - 9) + '.tnnproto';
- return context.request(tnnproto, null).then((stream) => {
- const buffer = stream.peek();
- return new tnn.Model(metadata, buffer, context.stream.peek());
- });
- }
- default: {
- throw new tnn.Error("Unsupported TNN format '" + match + "'.");
- }
- }
- });
- }
- };
- tnn.Model = class {
- constructor(metadata, tnnproto, tnnmodel) {
- this._graphs = [
- new tnn.Graph(metadata, tnnproto, tnnmodel)
- ];
- }
- get format() {
- return 'TNN';
- }
- get graphs() {
- return this._graphs;
- }
- };
- tnn.Graph = class {
- constructor(metadata, tnnproto, tnnmodel) {
- this._inputs = [];
- this._outputs = [];
- this._nodes = [];
- const resources = new tnn.LayerResourceReader(tnnmodel);
- const reader = new tnn.TextProtoReader(tnnproto);
- for (const input of reader.inputs) {
- const shape = new tnn.TensorShape(input.shape);
- const type = new tnn.TensorType(input.data_type, shape);
- this._inputs.push(new tnn.Parameter(input.name, [ new tnn.Argument(input.name, type, null) ]));
- }
- for (const output of reader.outputs) {
- this._outputs.push(new tnn.Parameter(output.name, [ new tnn.Argument(output.name, null, null) ]));
- }
- for (const layer of reader.layers) {
- this._nodes.push(new tnn.Node(metadata, resources, layer));
- }
- }
- get inputs() {
- return this._inputs;
- }
- get outputs() {
- return this._outputs;
- }
- get nodes() {
- return this._nodes;
- }
- };
- tnn.Parameter = class {
- constructor(name, args) {
- this._name = name;
- this._arguments = args;
- }
- get name() {
- return this._name;
- }
- get visible() {
- return true;
- }
- get arguments() {
- return this._arguments;
- }
- };
- tnn.Argument = class {
- constructor(name, type, initializer) {
- if (typeof name !== 'string') {
- throw new tnn.Error("Invalid argument identifier '" + JSON.stringify(name) + "'.");
- }
- this._name = name;
- this._type = type || null;
- this._initializer = initializer || null;
- }
- get name() {
- return this._name;
- }
- get type() {
- if (this._initializer) {
- return this._initializer.type;
- }
- return this._type;
- }
- get initializer() {
- return this._initializer;
- }
- };
- tnn.Node = class {
- constructor(metadata, resources, layer) {
- this._inputs = [];
- this._outputs = [];
- this._attributes = [];
- this._name = layer.name;
- this._type = metadata.type(layer.type);
- const attributeSchemas = this._type && this._type.attributes ? this._type && this._type.attributes.slice() : [];
- const attributes = layer.attributes.slice();
- while (attributes.length > 0) {
- const attributeSchema = attributeSchemas.shift();
- let value = null;
- let name = '';
- if (attributeSchema && attributeSchema.type === 'int32[]' && attributeSchema.size) {
- name = attributeSchema.name;
- value = attributes.splice(0, layer.attr[attributeSchema.size]).map((attribute) => parseInt(attribute.value, 10));
- }
- else {
- const attribute = attributes.shift();
- name = attribute.key;
- value = attribute.value;
- }
- this._attributes.push(new tnn.Attribute(attributeSchema, name, value));
- }
- const inputs = layer.inputs;
- let inputIndex = 0;
- if (this._type && this._type.inputs) {
- for (const inputDef of this._type.inputs) {
- if (inputIndex < inputs.length || inputDef.option != 'optional') {
- const inputCount = (inputDef.option == 'variadic') ? (inputs.length - inputIndex) : 1;
- const inputArguments = inputs.slice(inputIndex, inputIndex + inputCount).filter((id) => id != '' || inputDef.option != 'optional').map((id) => {
- return new tnn.Argument(id, null, null);
- });
- this._inputs.push(new tnn.Parameter(inputDef.name, inputArguments));
- inputIndex += inputCount;
- }
- }
- }
- else {
- this._inputs.push(...inputs.slice(inputIndex).map((input, index) => {
- const inputName = ((inputIndex + index) == 0) ? 'input' : (inputIndex + index).toString();
- return new tnn.Parameter(inputName, [ new tnn.Argument(input, null, null) ]);
- }));
- }
- const outputs = layer.outputs;
- let outputIndex = 0;
- if (this._type && this._type.outputs) {
- for (const outputDef of this._type.outputs) {
- if (outputIndex < outputs.length || outputDef.option != 'optional') {
- const outputCount = (outputDef.option == 'variadic') ? (outputs.length - outputIndex) : 1;
- const outputArguments = outputs.slice(outputIndex, outputIndex + outputCount).map((id) => {
- return new tnn.Argument(id, null, null);
- });
- this._outputs.push(new tnn.Parameter(outputDef.name, outputArguments));
- outputIndex += outputCount;
- }
- }
- }
- else {
- this._outputs.push(...outputs.slice(outputIndex).map((output, index) => {
- const outputName = ((outputIndex + index) == 0) ? 'output' : (outputIndex + index).toString();
- return new tnn.Parameter(outputName, [ new tnn.Argument(output, null, null) ]);
- }));
- }
- switch (this._type.name) {
- case 'Convolution':
- case 'ConvolutionDepthWise':
- case 'Deconvolution':
- case 'DeconvolutionDepthWise': {
- const resource = resources.read(this._name);
- if (resource) {
- const num_output = parseInt(layer.attr['2'] || 0, 10);
- const kernel_w = parseInt(layer.attr['3'] || 0, 10);
- const kernel_h = parseInt(layer.attr['4'] || kernel_w, 10);
- const weight_data_size = resource.filter.length;
- this._weight(resource, 'filter', [ num_output, weight_data_size / (num_output * kernel_w * kernel_h), kernel_w, kernel_h ]);
- if (resource.bias) {
- this._weight(resource, 'bias', [ num_output ]);
- }
- if (resource.quantized) {
- this._weight(resource, 'quantized', [ num_output ]);
- }
- }
- break;
- }
- case 'Conv3D':{
- const resource = resources.read(this._name);
- if (resource) {
- const num_output = parseInt(layer.attr['2'] || 0, 10);
- const kernel_w = parseInt(layer.attr['3'] || 0, 10);
- const kernel_h = parseInt(layer.attr['4'] || kernel_w, 10);
- const kernel_d = parseInt(layer.attr['5'] || kernel_w, 10);
- const weight_data_size = resource.filter.length;
- this._weight(resource, 'weight', [ num_output, weight_data_size / (num_output * kernel_w * kernel_h * kernel_d), kernel_w, kernel_h, kernel_d ]);
- if (resource.bias) {
- this._weight(resources, 'bias', [ num_output ]);
- }
- }
- break;
- }
- case 'InnerProduct': {
- const resource = resources.read(this._name);
- if (resource) {
- const num_output = parseInt(layer.attr['0'] || 0, 10);
- const weight_data_size = resource.weight.length;
- this._weight(resource, 'weight', [ num_output, weight_data_size / num_output ]);
- this._weight(resource, 'bias', [ num_output ]);
- if (resource.weight.dataType === 'int8') {
- this._weight(resource, 'scale', [ num_output ]);
- }
- }
- break;
- }
- case 'PReLU': {
- const resource = resources.read(this._name);
- if (resource) {
- this._weight(resource, 'slope', [ resource.slope.length ]);
- }
- break;
- }
- case 'BatchNormCxx':
- case 'InstBatchNormCxx': {
- const resource = resources.read(this._name);
- if (resource) {
- this._weight(resource, 'scale', [ resource.scale.length ]);
- this._weight(resource, 'bias', [ resource.bias.length ]);
- }
- break;
- }
- case 'Div':
- case 'Sub':
- case 'Add':
- case 'Mul':
- case 'MatMul': {
- if (this._inputs.length === 1) {
- const resource = resources.read(this._name);
- if (resource) {
- const num_output = resource.slope.length;
- this._weight(resource, 'slope', [ num_output ]);
- }
- }
- break;
- }
- case 'HdrGuide': {
- const resource = resources.read(this._name);
- if (resource) {
- const weight_size = resource.ccm_weight.length;
- this._weight(resource, 'ccm_weight', [ weight_size ]);
- this._weight(resource, 'ccm_bias', [ weight_size ]);
- this._weight(resource, 'shifts', [ weight_size ]);
- this._weight(resource, 'slopes', [ weight_size ]);
- this._weight(resource, 'projection_weight', [ weight_size ]);
- this._weight(resource, 'projection_bias', [ weight_size ]);
- }
- break;
- }
- case 'BlobScale': {
- const resource = resources.read(this._name);
- if (resource) {
- const scale_data_size = resource.scale.length;
- this._weight(resource, 'scale', [ scale_data_size]);
- this._weight(resource, 'bias', [ scale_data_size ]);
- }
- break;
- }
- case 'Gather': {
- const resource = resources.read(this._name);
- if (resource) {
- if (resource.data) {
- this._weight(resource, 'data', [ resource.data.length ]);
- }
- if (resource.indices) {
- this._weight(resource, 'indices', [ resource.indices.length ]);
- }
- }
- break;
- }
- default: {
- break;
- }
- }
- }
- get type() {
- return this._type;
- }
- get name() {
- return this._name;
- }
- get attributes() {
- return this._attributes;
- }
- get inputs() {
- return this._inputs;
- }
- get outputs() {
- return this._outputs;
- }
- _weight(resource, name, shape) {
- const initializer = resource[name];
- if (!initializer) {
- throw new tnn.Error("Layer initializer'" + resource.type + "." + name + "' not found '");
- }
- const tensor = new tnn.Tensor(new tnn.TensorType(initializer.dataType, new tnn.TensorShape(shape)), initializer.value);
- this._inputs.push(new tnn.Parameter(name, [ new tnn.Argument('', null, tensor) ]));
- }
- };
- tnn.Attribute = class {
- constructor(schema, key, value) {
- this._type = '';
- this._name = key.toString();
- this._value = value;
- if (schema) {
- this._name = schema.name;
- if (schema.type) {
- this._type = schema.type;
- }
- switch (this._type) {
- case '':
- break;
- case 'int32':
- this._value = parseInt(this._value, 10);
- break;
- case 'float32':
- this._value = parseFloat(this._value);
- break;
- case 'int32[]':
- this._value = this._value.map((v) => parseInt(v, 10));
- break;
- case 'float32[]':
- this._value = this._value.map((v) => parseFloat(v));
- break;
- default:
- throw new tnn.Error("Unsupported attribute type '" + this._type + "'.");
- }
- if (Object.prototype.hasOwnProperty.call(schema, 'visible') && !schema.visible) {
- this._visible = false;
- }
- else if (Object.prototype.hasOwnProperty.call(schema, 'default')) {
- if (this._value == schema.default || (this._value && this._value.toString() == schema.default.toString())) {
- this._visible = false;
- }
- }
- }
- }
- get type() {
- return this._type;
- }
- get name() {
- return this._name;
- }
- get value() {
- return this._value;
- }
- get visible() {
- return this._visible == false ? false : true;
- }
- };
- tnn.Tensor = class {
- constructor(type, data) {
- this._type = type;
- this._data = data;
- }
- get category() {
- return 'Weight';
- }
- get type() {
- return this._type;
- }
- get values() {
- return this._data;
- }
- };
- tnn.TensorType = class {
- constructor(dataType, shape) {
- this._dataType = dataType || '?';
- this._shape = shape;
- }
- get dataType() {
- return this._dataType;
- }
- get shape() {
- return this._shape;
- }
- toString() {
- return this._dataType + this._shape.toString();
- }
- };
- tnn.TensorShape = class {
- constructor(dimensions) {
- this._dimensions = dimensions;
- }
- get dimensions() {
- return this._dimensions;
- }
- toString() {
- return this._dimensions ? ('[' + this._dimensions.map((dimension) => dimension ? dimension.toString() : '?').join(',') + ']') : '';
- }
- };
- tnn.TextProtoReader = class {
- constructor(buffer) {
- const reader = text.Reader.open(buffer);
- let lines = [];
- for (;;) {
- const line = reader.read();
- if (line === undefined) {
- break;
- }
- lines.push(line.replace(/\r|"/g, ''));
- }
- const split = (line, delimiter, trim, ignore_blank) => {
- return line.split(delimiter).map((v) => trim ? v.trim() : v).filter((v) => !ignore_blank || v);
- };
- lines = split(lines.join(''), ',', true, false);
- if (lines.length <= 5) {
- throw new tnn.Error('Invalid line count.');
- }
- const header = split(lines.shift(), ' ', true, false);
- if (header.length < 3) {
- throw new tnn.Error('Invalid header size.');
- }
- else if (header.length > 3 && (header[3] !== '4206624770' && header[3] !== '4206624772')) {
- throw new tnn.Error("Invalid signature '" + header[3] + "'.");
- }
- this._inputs = split(lines.shift(), ':', true, false).map((input) => {
- const array = split(input, ' ', true, false);
- const name = array.shift();
- if (header[3] === '4206624772') {
- const shape_size = parseInt(array.shift(), 10);
- const data_type_index = parseInt(array[shape_size], 10);
- return {
- name: name,
- data_type: [ 'float32', 'float16', 'int8', 'int32', 'bfloat16' ][data_type_index],
- shape: array.slice(0, -1).map((dim) => parseInt(dim, 10)),
- };
- }
- return {
- name: name,
- data_type: 'float32',
- shape: array.map((dim) => parseInt(dim, 10))
- };
- });
- lines.shift();
- this._outputs = split(lines.shift(), ' ', true, false).map((output) => {
- return { name: output };
- });
- lines.shift();
- this._layers = [];
- while (lines.length > 0) {
- const line = lines.shift().trim();
- if (line.length > 0) {
- const array = split(line, ' ', true, true);
- const layer = {};
- layer.type = array.shift();
- layer.name = array.shift();
- const inputCount = parseInt(array.shift(), 10);
- const outputCount = parseInt(array.shift(), 10);
- layer.inputs = array.splice(0, inputCount);
- layer.outputs = array.splice(0, outputCount);
- layer.attr = {};
- layer.attributes = [];
- let count = 0;
- for (const column of array) {
- const parts = column.split(' ');
- if (parts.length === 1) {
- let key = count;
- let value = parts.toString();
- const keyInt = parseInt(key, 10);
- if (keyInt < 0) {
- value = value.split(',').map((v) => v.trim());
- value.shift();
- key = (-(keyInt + 23300)).toString();
- }
- layer.attr[key] = value;
- layer.attributes.push({ key: key, value: value });
- count++;
- }
- }
- this._layers.push(layer);
- }
- }
- }
- get inputs() {
- return this._inputs;
- }
- get outputs() {
- return this._outputs;
- }
- get layers() {
- return this._layers;
- }
- };
- tnn.LayerResourceReader = class {
- constructor(buffer) {
- this._layerResources = [];
- if (buffer) {
- const reader = new base.BinaryReader(buffer);
- const magic_number = reader.uint32();
- if (magic_number !== 0xFABC0002 && magic_number !== 0xFABC0004) {
- throw new tnn.Error("Invalid blob header signature '" + magic_number.toString() + "'.");
- }
- this._layerResources = new Array(reader.int32() & 0x1FFFFFFF);
- const raw = (reader) => {
- const magic_number = reader.uint32();
- if (magic_number !== 0xFABC0002 && magic_number !== 0xFABC0004) {
- throw new tnn.Error("Invalid raw signature '" + magic_number.toString() + "'.");
- }
- const data_type = reader.int32();
- if (data_type > 4) {
- throw new tnn.Error("Unsupported data type '" + data_type + "'.");
- }
- const length = reader.int32();
- if (length <= 0) {
- return null;
- }
- let dims = null;
- if (magic_number === 0xFABC0004) {
- const dim_size = reader.int32();
- dims = reader.read(dim_size * 4);
- }
- return {
- dataType: [ 'float32', 'float16', 'int8', 'int32', 'bfloat16' ][data_type],
- length: length / [ 4, 2, 1, 4, 2 ][data_type],
- value: reader.read(length),
- shape: dims
- };
- };
- const expect = (reader, name) => {
- const content = reader.string();
- if (name !== content) {
- throw new tnn.Error("Invalid string '" + content + "' instead of '" + name + "'.");
- }
- };
- for (let i = 0; i < this._layerResources.length; i++) {
- const resource = {};
- resource.operator = reader.int32();
- resource.type = reader.string();
- resource.name = reader.string();
- switch (resource.type) {
- case 'Convolution':
- case 'ConvolutionDepthWise':
- case 'Deconvolution':
- case 'DeconvolutionDepthWise': {
- expect(reader, resource.name);
- const bias = reader.int32();
- resource.filter = raw(reader);
- if (bias) {
- resource.bias = raw(reader);
- }
- if (resource.filter.dataType === 'int8') {
- resource.quantized = raw(reader);
- }
- break;
- }
- case 'Conv3D': {
- expect(reader, resource.name);
- const bias = reader.int32();
- resource.filter = raw(reader);
- if (bias) {
- resource.bias = raw(reader);
- }
- break;
- }
- case 'InnerProduct': {
- expect(reader, resource.name);
- resource.weight = raw(reader);
- resource.bias = raw(reader);
- if (resource.weight.dataType === 'int8') {
- resource.scale = raw(reader);
- }
- break;
- }
- case 'PReLU': {
- expect(reader, resource.name);
- resource.slope = raw(reader);
- break;
- }
- case 'Add':
- case 'Div':
- case 'Mul':
- case 'Sub':
- case 'MatMul': {
- resource.slope = raw(reader);
- break;
- }
- case 'BatchNormCxx':
- case 'InstBatchNormCxx':
- resource.scale = raw(reader);
- resource.bias = raw(reader);
- break;
- case 'HdrGuide':
- resource.ccm_weight = raw(reader);
- resource.ccm_bias = raw(reader);
- resource.shifts = raw(reader);
- resource.slopes = raw(reader);
- resource.projection_weight = raw(reader);
- resource.projection_bias = raw(reader);
- break;
- case 'BlobScale':
- resource.scale = raw(reader);
- resource.bias = raw(reader);
- break;
- case 'Gather': {
- // reader.expect(resource.name);
- const has_data = reader.int32();
- if (has_data) {
- resource.data = raw(reader);
- }
- const has_indices = reader.int32();
- if (has_indices) {
- resource.indices = raw(reader);
- }
- break;
- }
- default: {
- throw new tnn.Error("Unsupported layer resource type '" + resource.type + "'.");
- }
- }
- this._layerResources[i] = resource;
- }
- if (reader.position !== reader.length) {
- throw new tnn.Error("Invalid blob size.");
- }
- }
- }
- read(name) {
- const resource = this._layerResources.shift();
- if (resource && resource.name !== name) {
- throw new tnn.Error("Invalid blob layer name '" + name + "'.");
- }
- return resource;
- }
- };
- tnn.Error = class extends Error {
- constructor(message) {
- super(message);
- this.name = 'Error loading TNN model.';
- }
- };
- if (typeof module !== 'undefined' && typeof module.exports === 'object') {
- module.exports.ModelFactory = tnn.ModelFactory;
- }
|