| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599 |
- /* jshint esversion: 6 */
- var rknn = rknn || {};
- var json = json || require('./json');
- rknn.ModelFactory = class {
- match(context) {
- const stream = context.stream;
- const signature = [ 0x52, 0x4B, 0x4E, 0x4E, 0x00, 0x00, 0x00, 0x00 ];
- if (signature.length <= stream.length && stream.peek(signature.length).every((value, index) => value === signature[index])) {
- return true;
- }
- return false;
- }
- open(context) {
- return rknn.Metadata.open(context).then((metadata) => {
- const stream = context.stream;
- const container = rknn.Container.open(stream);
- return new rknn.Model(metadata, container.model, container.weights);
- });
- }
- };
- rknn.Model = class {
- constructor(metadata, model, weights) {
- this._version = model.version;
- this._producer = model.ori_network_platform || model.network_platform || '';
- this._runtime = model.target_platform ? model.target_platform.join(',') : '';
- this._graphs = [ new rknn.Graph(metadata, model, weights) ];
- }
- get format() {
- return 'RKNN v' + this._version;
- }
- get producer() {
- return this._producer;
- }
- get runtime() {
- return this._runtime;
- }
- get graphs() {
- return this._graphs;
- }
- };
- rknn.Graph = class {
- constructor(metadata, model, weights) {
- this._name = model.name || '';
- this._inputs = [];
- this._outputs = [];
- this._nodes = [];
- const args = new Map();
- for (const const_tensor of model.const_tensor) {
- const name = 'const_tensor:' + const_tensor.tensor_id.toString();
- const shape = new rknn.TensorShape(const_tensor.size);
- const type = new rknn.TensorType(const_tensor.dtype, shape);
- const tensor = new rknn.Tensor(type, const_tensor.offset, weights);
- const argument = new rknn.Argument(name, type, tensor);
- args.set(name, argument);
- }
- for (const virtual_tensor of model.virtual_tensor) {
- const name = virtual_tensor.node_id.toString() + ':' + virtual_tensor.output_port.toString();
- const argument = new rknn.Argument(name, null, null);
- args.set(name, argument);
- }
- for (const norm_tensor of model.norm_tensor) {
- const name = 'norm_tensor:' + norm_tensor.tensor_id.toString();
- const shape = new rknn.TensorShape(norm_tensor.size);
- const type = new rknn.TensorType(norm_tensor.dtype, shape);
- const argument = new rknn.Argument(name, type, null);
- args.set(name, argument);
- }
- for (const node of model.nodes) {
- node.input = [];
- node.output = [];
- }
- for (const connection of model.connection) {
- switch (connection.left) {
- case 'input':
- model.nodes[connection.node_id].input.push(connection);
- if (connection.right_node) {
- model.nodes[connection.right_node.node_id].output[connection.right_node.tensor_id] = connection;
- }
- break;
- case 'output':
- model.nodes[connection.node_id].output.push(connection);
- break;
- }
- }
- for (const graph of model.graph) {
- const key = graph.right + ':' + graph.right_tensor_id.toString();
- const argument = args.get(key);
- if (!argument) {
- throw new rknn.Error("Invalid argument '" + key + "'.");
- }
- const name = graph.left + ((graph.left_tensor_id === 0) ? '' : graph.left_tensor_id.toString());
- const parameter = new rknn.Parameter(name, [ argument ]);
- switch (graph.left) {
- case 'input': {
- this._inputs.push(parameter);
- break;
- }
- case 'output': {
- this._outputs.push(parameter);
- break;
- }
- }
- }
- for (const node of model.nodes) {
- this._nodes.push(new rknn.Node(metadata, node, args));
- }
- }
- get name() {
- return this._name;
- }
- get inputs() {
- return this._inputs;
- }
- get outputs() {
- return this._outputs;
- }
- get nodes() {
- return this._nodes;
- }
- };
- rknn.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;
- }
- };
- rknn.Argument = class {
- constructor(name, type, initializer) {
- if (typeof name !== 'string') {
- throw new rknn.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() {
- return this._type;
- }
- get initializer() {
- return this._initializer;
- }
- };
- rknn.Node = class {
- constructor(metadata, node, args) {
- this._metadata = metadata;
- this._name = node.name || '';
- this._type = node.op;
- this._inputs = [];
- this._outputs = [];
- this._attributes = [];
- const schema = this._metadata.type(this._type);
- node.input = node.input || [];
- for (let i = 0; i < node.input.length; ) {
- const input = schema && schema.inputs && i < schema.inputs.length ? schema.inputs[i] : { name: i === 0 ? 'input' : i.toString() };
- const count = input.list ? node.input.length - i : 1;
- const list = node.input.slice(i, i + count).map((input) => {
- if (input.right_tensor) {
- const key = input.right_tensor.type + ':' + input.right_tensor.tensor_id.toString();
- const argument = args.get(key);
- if (!argument) {
- throw new rknn.Error("Invalid input argument '" + key + "'.");
- }
- return argument;
- }
- if (input.right_node) {
- const key = input.right_node.node_id.toString() + ':' + input.right_node.tensor_id.toString();
- const argument = args.get(key);
- if (!argument) {
- throw new rknn.Error("Invalid input argument '" + key + "'.");
- }
- return argument;
- }
- throw new rknn.Error('Invalid input argument.');
- });
- this._inputs.push(new rknn.Parameter(input.name, list));
- i += count;
- }
- node.output = node.output || [];
- for (let i = 0; i < node.output.length; ) {
- const output = schema && schema.outputs && i < schema.outputs.length ? schema.outputs[i] : { name: i === 0 ? 'output' : i.toString() };
- const count = output.list ? node.output.length - i : 1;
- const list = node.output.slice(i, i + count).map((output) => {
- if (output.right_tensor) {
- const key = output.right_tensor.type + ':' + output.right_tensor.tensor_id.toString();
- const argument = args.get(key);
- if (!argument) {
- throw new rknn.Error("Invalid output argument '" + key + "'.");
- }
- return argument;
- }
- if (output.right_node) {
- const key = output.right_node.node_id.toString() + ':' + output.right_node.tensor_id.toString();
- const argument = args.get(key);
- if (!argument) {
- throw new rknn.Error("Invalid output argument '" + key + "'.");
- }
- return argument;
- }
- throw new rknn.Error('Invalid output argument.');
- });
- this._outputs.push(new rknn.Parameter(output.name, list));
- i += count;
- }
- if (node.nn) {
- const nn = node.nn;
- for (const key of Object.keys(nn)) {
- const params = nn[key];
- for (const name of Object.keys(params)) {
- const value = params[name];
- this._attributes.push(new rknn.Attribute(name, value));
- }
- }
- }
- }
- get name() {
- return this._name;
- }
- get type() {
- const prefix = 'VSI_NN_OP_';
- return this._type.startsWith(prefix) ? this._type.substring(prefix.length) : this.type;
- }
- get metadata() {
- return this._metadata.type(this._type);
- }
- get inputs() {
- return this._inputs;
- }
- get outputs() {
- return this._outputs;
- }
- get attributes() {
- return this._attributes;
- }
- };
- rknn.Attribute = class {
- constructor(name, value) {
- this._name = name;
- this._value = value;
- }
- get name() {
- return this._name;
- }
- get value() {
- return this._value;
- }
- };
- rknn.Tensor = class {
- constructor(type, offset, weights) {
- this._type = type;
- let size = 0;
- switch (this._type.dataType) {
- case 'uint8': size = 1; break;
- case 'int8': size = 1; break;
- case 'int32': size = 4; break;
- case 'float16': size = 2; break;
- case 'float32': size = 4; break;
- }
- const shape = type.shape.dimensions;
- size = size * (shape.length === 0 ? 1 : shape.reduce((a, b) => a * b));
- if (size > 0) {
- this._data = weights.slice(offset, offset + size);
- }
- }
- get type() {
- return this._type;
- }
- get state() {
- return this._context().state || null;
- }
- get value() {
- const context = this._context();
- if (context.state) {
- return null;
- }
- context.limit = Number.MAX_SAFE_INTEGER;
- return this._decode(context, 0);
- }
- toString() {
- const context = this._context();
- if (context.state) {
- return '';
- }
- context.limit = 10000;
- const value = this._decode(context, 0);
- return JSON.stringify(value, '', ' ');
- }
- _context() {
- const context = {};
- if (!this._type.dataType) {
- context.state = 'Tensor data type is not implemented.';
- return context;
- }
- if (!this._data) {
- context.state = 'Tensor data is empty.';
- return context;
- }
- context.index = 0;
- context.count = 0;
- context.shape = this._type.shape.dimensions;
- context.dataType = this._type.dataType;
- context.view = new DataView(this._data.buffer, this._data.byteOffset, this._data.byteLength);
- return context;
- }
- _decode(context, dimension) {
- const shape = context.shape.length !== 0 ? context.shape : [ 1 ];
- const results = [];
- const size = shape[dimension];
- if (dimension == shape.length - 1) {
- for (let i = 0; i < size; i++) {
- if (context.count > context.limit) {
- results.push('...');
- return results;
- }
- switch (context.dataType) {
- case 'float16':
- results.push(context.view.getFloat16(context.index, true));
- context.index += 2;
- context.count++;
- break;
- case 'float32':
- results.push(context.view.getFloat32(context.index, true));
- context.index += 4;
- context.count++;
- break;
- case 'uint8':
- results.push(context.view.getUint8(context.index, true));
- context.index++;
- context.count++;
- break;
- case 'int8':
- results.push(context.view.getInt8(context.index, true));
- context.index += 1;
- context.count++;
- break;
- case 'int32':
- results.push(context.view.getInt32(context.index, true));
- context.index += 4;
- context.count++;
- break;
- }
- }
- }
- else {
- for (let j = 0; j < size; j++) {
- if (context.count > context.limit) {
- results.push('...');
- return results;
- }
- results.push(this._decode(context, dimension + 1));
- }
- }
- if (context.shape.length == 0) {
- return results[0];
- }
- return results;
- }
- };
- rknn.TensorType = class {
- constructor(dataType, shape) {
- switch (dataType.vx_type) {
- case 'VSI_NN_TYPE_UINT8': this._dataType = 'uint8'; break;
- case 'VSI_NN_TYPE_INT8': this._dataType = 'int8'; break;
- case 'VSI_NN_TYPE_INT16': this._dataType = 'int16'; break;
- case 'VSI_NN_TYPE_INT32': this._dataType = 'int32'; break;
- case 'VSI_NN_TYPE_INT64': this._dataType = 'int64'; break;
- case 'VSI_NN_TYPE_FLOAT16': this._dataType = 'float16'; break;
- case 'VSI_NN_TYPE_FLOAT32': this._dataType = 'float32'; break;
- default:
- throw new rknn.Error("Invalid data type '" + JSON.stringify(dataType) + "'.");
- }
- this._shape = shape;
- }
- get dataType() {
- return this._dataType;
- }
- get shape() {
- return this._shape;
- }
- toString() {
- return this.dataType + this._shape.toString();
- }
- };
- rknn.TensorShape = class {
- constructor(shape) {
- this._dimensions = shape;
- }
- get dimensions() {
- return this._dimensions;
- }
- toString() {
- if (!this._dimensions || this._dimensions.length == 0) {
- return '';
- }
- return '[' + this._dimensions.join(',') + ']';
- }
- };
- rknn.Container = class {
- static open(stream) {
- const signature = [ 0x52, 0x4B, 0x4E, 0x4E, 0x00, 0x00, 0x00, 0x00 ];
- if (signature.length <= stream.length && stream.peek(signature.length).every((value, index) => value === signature[index])) {
- return new rknn.Container(stream);
- }
- return null;
- }
- constructor(stream) {
- this._reader = new rknn.Container.StreamReader(stream);
- }
- get version() {
- this._read();
- return this._version;
- }
- get weights() {
- this._read();
- return this._weights;
- }
- get model() {
- this._read();
- return this._model;
- }
- _read() {
- if (this._reader) {
- this._reader.uint64();
- this._version = this._reader.uint64();
- this._weights = this._reader.read();
- const buffer = this._reader.read();
- const reader = json.TextReader.create(buffer);
- this._model = reader.read();
- delete this._reader;
- }
- }
- };
- rknn.Container.StreamReader = class {
- constructor(stream) {
- this._stream = stream;
- this._length = stream.length;
- this._position = 0;
- }
- skip(offset) {
- this._position += offset;
- if (this._position > this._length) {
- throw new rknn.Error('Expected ' + (this._position - this._length) + ' more bytes. The file might be corrupted. Unexpected end of file.');
- }
- }
- uint64() {
- this.skip(8);
- const buffer = this._stream.read(8);
- const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
- return view.getUint64(0, true).toNumber();
- }
- read() {
- const size = this.uint64();
- this.skip(size);
- return this._stream.read(size);
- }
- };
- rknn.Metadata = class {
- static open(context) {
- if (rknn.Metadata._metadata) {
- return Promise.resolve(rknn.Metadata._metadata);
- }
- return context.request('rknn-metadata.json', 'utf-8', null).then((data) => {
- rknn.Metadata._metadata = new rknn.Metadata(data);
- return rknn.Metadata._metadata;
- }).catch(() => {
- rknn.Metadata._metadata = new rknn.Metadata(null);
- return rknn.Metadata._metadata;
- });
- }
- constructor(data) {
- this._map = new Map();
- if (data) {
- const metadata = JSON.parse(data);
- this._map = new Map(metadata.map((item) => [ item.name, item ]));
- }
- }
- type(name) {
- return this._map.has(name) ? this._map.get(name) : null;
- }
- attribute(type, name) {
- const schema = this.type(type);
- if (schema) {
- let attributeMap = schema.attributeMap;
- if (!attributeMap) {
- attributeMap = {};
- if (schema.attributes) {
- for (const attribute of schema.attributes) {
- attributeMap[attribute.name] = attribute;
- }
- }
- schema.attributeMap = attributeMap;
- }
- const attributeSchema = attributeMap[name];
- if (attributeSchema) {
- return attributeSchema;
- }
- }
- return null;
- }
- };
- rknn.Error = class extends Error {
- constructor(message) {
- super(message);
- this.name = 'Error loading RKNN model.';
- }
- };
- if (typeof module !== 'undefined' && typeof module.exports === 'object') {
- module.exports.ModelFactory = rknn.ModelFactory;
- }
|