rknn.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. var rknn = rknn || {};
  2. var base = base || require('./base');
  3. var flatbuffers = flatbuffers || require('./flatbuffers');
  4. var json = json || require('./json');
  5. rknn.ModelFactory = class {
  6. match(context) {
  7. return rknn.Container.open(context);
  8. }
  9. open(context, match) {
  10. return context.require('./rknn-schema').then(() => {
  11. rknn.schema = flatbuffers.get('rknn').rknn;
  12. return context.metadata('rknn-metadata.json').then((metadata) => {
  13. const container = match;
  14. const type = container.type;
  15. switch (type) {
  16. case 'json': {
  17. const buffer = container.value;
  18. const reader = json.TextReader.open(buffer);
  19. const model = reader.read();
  20. return new rknn.Model(metadata, type, model, container.next);
  21. }
  22. case 'flatbuffers': {
  23. const buffer = container.value;
  24. const reader = flatbuffers.BinaryReader.open(buffer);
  25. const model = rknn.schema.Model.create(reader);
  26. return new rknn.Model(metadata, type, model, null);
  27. }
  28. case 'openvx': {
  29. const buffer = container.value;
  30. const model = new openvx.Model(buffer);
  31. return new rknn.Model(metadata, type, model, null);
  32. }
  33. default: {
  34. throw new rknn.Error("Unsupported RKNN format '" + container.type + "'.");
  35. }
  36. }
  37. });
  38. });
  39. }
  40. };
  41. rknn.Model = class {
  42. constructor(metadata, type, model, next) {
  43. switch (type) {
  44. case 'json': {
  45. this._format = 'RKNN v' + model.version.split('-').shift();
  46. this._name = model.name || '';
  47. this._producer = model.ori_network_platform || model.network_platform || '';
  48. this._runtime = model.target_platform ? model.target_platform.join(',') : '';
  49. this._graphs = [ new rknn.Graph(metadata, type, model.name || '', model, next) ];
  50. break;
  51. }
  52. case 'flatbuffers': {
  53. const version = model.compiler.split('-').shift();
  54. this._format = 'RKNN Lite' + (version ? ' v' + version : '');
  55. this._runtime = model.runtime;
  56. this._name = model.name || '';
  57. this._graphs = model.graphs.map((graph) => new rknn.Graph(metadata, type, '', graph, null));
  58. this._metadata = [];
  59. this._metadata.push({ name: 'source', value: model.source });
  60. break;
  61. }
  62. case 'openvx': {
  63. this._format = 'RKNN OpenVX';
  64. this._name = model.name || '';
  65. this._graphs = [ new rknn.Graph(metadata, type, '', model, next) ];
  66. break;
  67. }
  68. default: {
  69. throw new rknn.Error("Unsupported RKNN model type '" + type + "'.");
  70. }
  71. }
  72. }
  73. get format() {
  74. return this._format;
  75. }
  76. get name() {
  77. return this._name;
  78. }
  79. get producer() {
  80. return this._producer;
  81. }
  82. get runtime() {
  83. return this._runtime;
  84. }
  85. get metadata() {
  86. return this._metadata;
  87. }
  88. get graphs() {
  89. return this._graphs;
  90. }
  91. };
  92. rknn.Graph = class {
  93. constructor(metadata, type, name, obj, next) {
  94. this._name = name;
  95. this._inputs = [];
  96. this._outputs = [];
  97. this._nodes = [];
  98. switch (type) {
  99. case 'json': {
  100. const dataType = (value) => {
  101. const type = value.vx_type.startsWith('VSI_NN_TYPE_') ? value.vx_type.split('_').pop().toLowerCase() : value.vx_type;
  102. switch (type) {
  103. case 'uint8':
  104. case 'int8':
  105. case 'int16':
  106. case 'int32':
  107. case 'int64':
  108. case 'float16':
  109. case 'float32':
  110. case 'float64':
  111. case 'vdata':
  112. return type;
  113. default:
  114. if (value.vx_type !== '') {
  115. throw new rknn.Error("Invalid data type '" + JSON.stringify(dataType) + "'.");
  116. }
  117. return '?';
  118. }
  119. };
  120. const model = obj;
  121. const args = new Map();
  122. for (const const_tensor of model.const_tensor) {
  123. const name = 'const_tensor:' + const_tensor.tensor_id.toString();
  124. const shape = new rknn.TensorShape(const_tensor.size);
  125. const type = new rknn.TensorType(dataType(const_tensor.dtype), shape);
  126. const tensor = new rknn.Tensor(type, const_tensor.offset, next.value);
  127. const argument = new rknn.Argument(name, type, tensor);
  128. args.set(name, argument);
  129. }
  130. for (const virtual_tensor of model.virtual_tensor) {
  131. const name = virtual_tensor.node_id.toString() + ':' + virtual_tensor.output_port.toString();
  132. const argument = new rknn.Argument(name, null, null);
  133. args.set(name, argument);
  134. }
  135. for (const norm_tensor of model.norm_tensor) {
  136. const name = 'norm_tensor:' + norm_tensor.tensor_id.toString();
  137. const shape = new rknn.TensorShape(norm_tensor.size);
  138. const type = new rknn.TensorType(dataType(norm_tensor.dtype), shape);
  139. const argument = new rknn.Argument(name, type, null);
  140. args.set(name, argument);
  141. }
  142. const arg = (name) => {
  143. if (!args.has(name)) {
  144. const argument = new rknn.Argument(name, null, null);
  145. args.set(name, argument);
  146. }
  147. return args.get(name);
  148. };
  149. for (const node of model.nodes) {
  150. node.input = [];
  151. node.output = [];
  152. }
  153. for (const connection of model.connection) {
  154. switch (connection.left) {
  155. case 'input':
  156. model.nodes[connection.node_id].input.push(connection);
  157. if (connection.right_node) {
  158. model.nodes[connection.right_node.node_id].output[connection.right_node.tensor_id] = connection;
  159. }
  160. break;
  161. case 'output':
  162. model.nodes[connection.node_id].output.push(connection);
  163. break;
  164. default:
  165. throw new rknn.Error("Unsupported left connection '" + connection.left + "'.");
  166. }
  167. }
  168. for (const graph of model.graph) {
  169. const key = graph.right + ':' + graph.right_tensor_id.toString();
  170. const argument = arg(key);
  171. const name = graph.left + (graph.left_tensor_id === 0 ? '' : graph.left_tensor_id.toString());
  172. const parameter = new rknn.Parameter(name, [ argument ]);
  173. switch (graph.left) {
  174. case 'input':
  175. this._inputs.push(parameter);
  176. break;
  177. case 'output':
  178. this._outputs.push(parameter);
  179. break;
  180. default:
  181. throw new rknn.Error("Unsupported left graph connection '" + graph.left + "'.");
  182. }
  183. }
  184. this._nodes = model.nodes.map((node) => new rknn.Node(metadata, type, node, arg, next));
  185. break;
  186. }
  187. case 'flatbuffers': {
  188. const graph = obj;
  189. const dataTypes = [ 'unk0', '?', '?', 'int8', '?', 'int16', 'float32', 'int64', '?', '?', 'float16', '?', '?', 'unk13' ];
  190. const args = graph.tensors.map((tensor) => {
  191. const shape = new rknn.TensorShape(Array.from(tensor.shape));
  192. const dataType = tensor.data_type < dataTypes.length ? dataTypes[tensor.data_type] : '?';
  193. if (dataType === '?') {
  194. throw new rknn.Error("Unsupported tensor data type '" + tensor.data_type + "'.");
  195. }
  196. const type = new rknn.TensorType(dataType, shape);
  197. const initializer = tensor.kind !== 4 && tensor.kind !== 5 ? null : new rknn.Tensor(type, 0, null);
  198. return new rknn.Argument(tensor.name, type, initializer);
  199. });
  200. const arg = (index) => {
  201. if (index >= args.length) {
  202. throw new rknn.Error("Invalid tensor index '" + index.toString() + "'.");
  203. }
  204. return args[index];
  205. };
  206. this._nodes = graph.nodes.map((node) => new rknn.Node(metadata, type, node, arg, next));
  207. break;
  208. }
  209. case 'openvx': {
  210. const model = obj;
  211. this._nodes = model.nodes.map((node) => new rknn.Node(metadata, type, node, null, next));
  212. break;
  213. }
  214. default: {
  215. throw new rknn.Error("Unsupported RKNN graph type '" + type + "'.");
  216. }
  217. }
  218. }
  219. get name() {
  220. return this._name;
  221. }
  222. get inputs() {
  223. return this._inputs;
  224. }
  225. get outputs() {
  226. return this._outputs;
  227. }
  228. get nodes() {
  229. return this._nodes;
  230. }
  231. };
  232. rknn.Parameter = class {
  233. constructor(name, args) {
  234. this._name = name;
  235. this._arguments = args;
  236. }
  237. get name() {
  238. return this._name;
  239. }
  240. get visible() {
  241. return true;
  242. }
  243. get arguments() {
  244. return this._arguments;
  245. }
  246. };
  247. rknn.Argument = class {
  248. constructor(name, type, initializer) {
  249. if (typeof name !== 'string') {
  250. throw new rknn.Error("Invalid argument identifier '" + JSON.stringify(name) + "'.");
  251. }
  252. this._name = name;
  253. this._type = type || null;
  254. this._initializer = initializer || null;
  255. }
  256. get name() {
  257. return this._name;
  258. }
  259. get type() {
  260. return this._type;
  261. }
  262. get initializer() {
  263. return this._initializer;
  264. }
  265. };
  266. rknn.Node = class {
  267. constructor(metadata, type, node, arg, next) {
  268. this._inputs = [];
  269. this._outputs = [];
  270. this._attributes = [];
  271. switch (type) {
  272. case 'json': {
  273. this._name = node.name || '';
  274. if (node.op === 'VSI_NN_OP_NBG' && next && next.type === 'openvx') {
  275. const buffer = next.value;
  276. const model = new openvx.Model(buffer);
  277. this._type = new rknn.Graph(metadata, next.type, 'NBG', model, null);
  278. }
  279. else if (node.op === 'RKNN_OP_NNBG' && next && next.type === 'flatbuffers') {
  280. const buffer = next.value;
  281. const reader = flatbuffers.BinaryReader.open(buffer);
  282. const model = rknn.schema.Model.create(reader);
  283. this._type = new rknn.Graph(metadata, next.type, 'NNBG', model.graphs[0], null);
  284. }
  285. else {
  286. this._type = Object.assign({}, metadata.type(node.op) || { name: node.op });
  287. for (const prefix of [ 'VSI_NN_OP_', 'RKNN_OP_' ]) {
  288. this._type.name = this._type.name.startsWith(prefix) ? this._type.name.substring(prefix.length) : this._type.name;
  289. }
  290. }
  291. node.input = node.input || [];
  292. for (let i = 0; i < node.input.length; ) {
  293. const input = this._type && this._type.inputs && i < this._type.inputs.length ? this._type.inputs[i] : { name: i === 0 ? 'input' : i.toString() };
  294. const count = input.list ? node.input.length - i : 1;
  295. const list = node.input.slice(i, i + count).map((input) => {
  296. if (input.right_tensor) {
  297. return arg(input.right_tensor.type + ':' + input.right_tensor.tensor_id.toString());
  298. }
  299. if (input.right_node) {
  300. return arg(input.right_node.node_id.toString() + ':' + input.right_node.tensor_id.toString());
  301. }
  302. throw new rknn.Error('Invalid input argument.');
  303. });
  304. this._inputs.push(new rknn.Parameter(input.name, list));
  305. i += count;
  306. }
  307. node.output = node.output || [];
  308. for (let i = 0; i < node.output.length; ) {
  309. const output = this._metadata && this._metadata.outputs && i < this._metadata.outputs.length ? this._metadata.outputs[i] : { name: i === 0 ? 'output' : i.toString() };
  310. const count = output.list ? node.output.length - i : 1;
  311. const list = node.output.slice(i, i + count).map((output) => {
  312. if (output.right_tensor) {
  313. return arg(output.right_tensor.type + ':' + output.right_tensor.tensor_id.toString());
  314. }
  315. if (output.right_node) {
  316. return arg(output.right_node.node_id.toString() + ':' + output.right_node.tensor_id.toString());
  317. }
  318. throw new rknn.Error('Invalid output argument.');
  319. });
  320. this._outputs.push(new rknn.Parameter(output.name, list));
  321. i += count;
  322. }
  323. if (node.nn) {
  324. const nn = node.nn;
  325. for (const key of Object.keys(nn)) {
  326. const params = nn[key];
  327. for (const name of Object.keys(params)) {
  328. const value = params[name];
  329. this._attributes.push(new rknn.Attribute(name, value));
  330. }
  331. }
  332. }
  333. break;
  334. }
  335. case 'flatbuffers': {
  336. this._name = node.name;
  337. this._type = metadata.type(node.type);
  338. if (node.inputs.length > 0) {
  339. const inputs = this._type.inputs || (node.inputs.length === 1 ? [ { name: "input" } ] : [ { name: "inputs", list: true } ]);
  340. if (Array.isArray(inputs) && inputs.length > 0 && inputs[0].list === true) {
  341. this._inputs = [new rknn.Parameter(inputs[0].name, Array.from(node.inputs).map((input) => arg(input))) ];
  342. }
  343. else {
  344. this._inputs = Array.from(node.inputs).map((input, index) => {
  345. const argument = arg(input);
  346. return new rknn.Parameter(index < inputs.length ? inputs[index].name : index.toString(), [ argument ]);
  347. });
  348. }
  349. }
  350. if (node.outputs.length > 0) {
  351. const outputs = this._type.outputs || (node.outputs.length === 1 ? [ { name: "output" } ] : [ { name: "outputs", list: true } ]);
  352. if (Array.isArray(outputs) && outputs.length > 0 && outputs[0].list === true) {
  353. this._outputs = [ new rknn.Parameter(outputs[0].name, Array.from(node.outputs).map((output) => arg(output))) ];
  354. }
  355. else {
  356. this._outputs = Array.from(node.outputs).map((output, index) => {
  357. const argument = arg(output);
  358. return new rknn.Parameter(index < outputs.length ? outputs[index].name : index.toString(), [ argument ]);
  359. });
  360. }
  361. }
  362. break;
  363. }
  364. case 'openvx': {
  365. this._name = '';
  366. this._type = metadata.type(node.type);
  367. break;
  368. }
  369. default: {
  370. throw new rknn.Error("Unsupported RKNN node type '" + type + "'.");
  371. }
  372. }
  373. }
  374. get name() {
  375. return this._name;
  376. }
  377. get type() {
  378. return this._type;
  379. }
  380. get inputs() {
  381. return this._inputs;
  382. }
  383. get outputs() {
  384. return this._outputs;
  385. }
  386. get attributes() {
  387. return this._attributes;
  388. }
  389. };
  390. rknn.Attribute = class {
  391. constructor(name, value) {
  392. this._name = name;
  393. this._value = value;
  394. }
  395. get name() {
  396. return this._name;
  397. }
  398. get value() {
  399. return this._value;
  400. }
  401. };
  402. rknn.Tensor = class {
  403. constructor(type, offset, weights) {
  404. this._type = type;
  405. this._data = null;
  406. let size = 0;
  407. switch (this._type.dataType) {
  408. case 'uint8': size = 1; break;
  409. case 'int8': size = 1; break;
  410. case 'int16': size = 2; break;
  411. case 'int32': size = 4; break;
  412. case 'int64': size = 8; break;
  413. case 'float16': size = 2; break;
  414. case 'float32': size = 4; break;
  415. case 'float64': size = 8; break;
  416. case 'vdata': size = 1; break;
  417. default: throw new rknn.Error("Unsupported tensor data type '" + this._type.dataType + "'.");
  418. }
  419. if (weights) {
  420. const shape = type.shape.dimensions;
  421. size = size * shape.reduce((a, b) => a * b, 1);
  422. if (size > 0) {
  423. this._data = weights.slice(offset, offset + size);
  424. }
  425. }
  426. }
  427. get type() {
  428. return this._type;
  429. }
  430. get state() {
  431. return this._context().state || null;
  432. }
  433. get value() {
  434. const context = this._context();
  435. if (context.state) {
  436. return null;
  437. }
  438. context.limit = Number.MAX_SAFE_INTEGER;
  439. return this._decode(context, 0);
  440. }
  441. toString() {
  442. const context = this._context();
  443. if (context.state) {
  444. return '';
  445. }
  446. context.limit = 10000;
  447. const value = this._decode(context, 0);
  448. return JSON.stringify(value, '', ' ');
  449. }
  450. _context() {
  451. const context = {};
  452. if (!this._type.dataType) {
  453. context.state = 'Tensor data type is not implemented.';
  454. return context;
  455. }
  456. if (!this._data) {
  457. context.state = 'Tensor data is empty.';
  458. return context;
  459. }
  460. context.index = 0;
  461. context.count = 0;
  462. context.shape = this._type.shape.dimensions;
  463. context.dataType = this._type.dataType;
  464. context.view = new DataView(this._data.buffer, this._data.byteOffset, this._data.byteLength);
  465. return context;
  466. }
  467. _decode(context, dimension) {
  468. const shape = context.shape.length !== 0 ? context.shape : [ 1 ];
  469. const results = [];
  470. const size = shape[dimension];
  471. if (dimension == shape.length - 1) {
  472. for (let i = 0; i < size; i++) {
  473. if (context.count > context.limit) {
  474. results.push('...');
  475. return results;
  476. }
  477. switch (context.dataType) {
  478. case 'float16':
  479. results.push(context.view.getFloat16(context.index, true));
  480. context.index += 2;
  481. context.count++;
  482. break;
  483. case 'float32':
  484. results.push(context.view.getFloat32(context.index, true));
  485. context.index += 4;
  486. context.count++;
  487. break;
  488. case 'float64':
  489. results.push(context.view.getFloat64(context.index, true));
  490. context.index += 8;
  491. context.count++;
  492. break;
  493. case 'uint8':
  494. results.push(context.view.getUint8(context.index, true));
  495. context.index++;
  496. context.count++;
  497. break;
  498. case 'int8':
  499. results.push(context.view.getInt8(context.index, true));
  500. context.index += 1;
  501. context.count++;
  502. break;
  503. case 'int16':
  504. results.push(context.view.getInt16(context.index, true));
  505. context.index += 2;
  506. context.count++;
  507. break;
  508. case 'int32':
  509. results.push(context.view.getInt32(context.index, true));
  510. context.index += 4;
  511. context.count++;
  512. break;
  513. case 'int64':
  514. results.push(context.view.getInt64(context.index, true));
  515. context.index += 8;
  516. context.count++;
  517. break;
  518. default:
  519. throw new rknn.Error("Unsupported tensor data type '" + context.dataType + "'.");
  520. }
  521. }
  522. }
  523. else {
  524. for (let j = 0; j < size; j++) {
  525. if (context.count > context.limit) {
  526. results.push('...');
  527. return results;
  528. }
  529. results.push(this._decode(context, dimension + 1));
  530. }
  531. }
  532. if (context.shape.length == 0) {
  533. return results[0];
  534. }
  535. return results;
  536. }
  537. };
  538. rknn.TensorType = class {
  539. constructor(dataType, shape) {
  540. this._dataType = dataType;
  541. this._shape = shape;
  542. }
  543. get dataType() {
  544. return this._dataType;
  545. }
  546. get shape() {
  547. return this._shape;
  548. }
  549. toString() {
  550. return this.dataType + this._shape.toString();
  551. }
  552. };
  553. rknn.TensorShape = class {
  554. constructor(shape) {
  555. this._dimensions = shape;
  556. }
  557. get dimensions() {
  558. return this._dimensions;
  559. }
  560. toString() {
  561. if (!this._dimensions || this._dimensions.length == 0) {
  562. return '';
  563. }
  564. return '[' + this._dimensions.join(',') + ']';
  565. }
  566. };
  567. rknn.Container = class {
  568. static open(context) {
  569. const stream = context.stream;
  570. const container = new rknn.Container(stream, stream.length);
  571. return container.type ? container : null;
  572. }
  573. constructor(stream, length) {
  574. this._stream = stream;
  575. this._length = length;
  576. this._type = '';
  577. if ((stream.position + 16) <= this._length) {
  578. const signature = [ 0x52, 0x4B, 0x4E, 0x4E ]; // RKNN
  579. if (stream.peek(signature.length).every((value, index) => value === signature[index])) {
  580. this._type = 'json';
  581. }
  582. }
  583. if ((stream.position + 16) <= this._length) {
  584. const signature = [ 0x43, 0x59, 0x50, 0x54, 0x52, 0x4B, 0x4E, 0x4E ]; // RKNN
  585. if (stream.peek(signature.length).every((value, index) => value === signature[index])) {
  586. this._type = 'crypt';
  587. }
  588. }
  589. if ((stream.position + 8) <= this._length) {
  590. const signature = [ 0x52, 0x4B, 0x4E, 0x4E ]; // RKNN
  591. if (stream.peek(8).subarray(4, 8).every((value, index) => value === signature[index])) {
  592. this._type = 'flatbuffers';
  593. }
  594. }
  595. if ((stream.position + 8) <= this._length) {
  596. const signature = [ 0x56, 0x50, 0x4D, 0x4E ]; // VPMN
  597. if (stream.peek(signature.length).every((value, index) => value === signature[index])) {
  598. this._type = 'openvx';
  599. }
  600. }
  601. }
  602. get type() {
  603. return this._type;
  604. }
  605. get value() {
  606. this.read();
  607. return this._value;
  608. }
  609. get next() {
  610. this.read();
  611. return this._next;
  612. }
  613. read() {
  614. if (this._stream) {
  615. const stream = this._stream;
  616. delete this._stream;
  617. switch (this._type) {
  618. case 'crypt': {
  619. throw new rknn.Error('Invalid file content. File contains undocumented encrypted RKNN data.');
  620. }
  621. case 'json': {
  622. const uint64 = () => {
  623. const buffer = stream.read(8);
  624. const reader = new base.BinaryReader(buffer);
  625. return reader.uint64();
  626. };
  627. stream.skip(8);
  628. const version = uint64();
  629. const data_size = uint64();
  630. switch (version) {
  631. case 0x0001:
  632. case 0x1001:
  633. break;
  634. case 0x0002:
  635. if (data_size > 0) {
  636. stream.skip(40);
  637. }
  638. break;
  639. default:
  640. throw new rknn.Error("Unsupported container version '" + version + "'.");
  641. }
  642. this._next = new rknn.Container(stream, data_size);
  643. this._next.read();
  644. const value_size = uint64(stream);
  645. this._value = stream.read(value_size);
  646. break;
  647. }
  648. case 'flatbuffers': {
  649. this._value = stream.read(this._length);
  650. this._next = null;
  651. break;
  652. }
  653. case 'openvx': {
  654. this._value = stream.read(this._length);
  655. this._next = null;
  656. break;
  657. }
  658. case '': {
  659. this._value = stream.read(this._length);
  660. this._next = null;
  661. break;
  662. }
  663. default: {
  664. throw new rknn.Error("Unsupported container type '" + this._format + "'.");
  665. }
  666. }
  667. }
  668. }
  669. };
  670. var openvx = openvx || {};
  671. openvx.Model = class {
  672. constructor(buffer) {
  673. const reader = new openvx.BinaryReader(buffer);
  674. reader.skip(4); // signature
  675. const major = reader.uint16();
  676. /* const minor = */ reader.uint16();
  677. reader.skip(4);
  678. this._name = reader.string(64);
  679. this._nodes = new Array(reader.uint32());
  680. if (major > 3) {
  681. reader.skip(296);
  682. }
  683. else if (major > 1) {
  684. reader.skip(288);
  685. }
  686. else {
  687. reader.skip(32);
  688. }
  689. /* const inputOffset = */ reader.uint32();
  690. /* const inputSize = */ reader.uint32();
  691. /* const outputOffset = */ reader.uint32();
  692. /* const outputSize = */ reader.uint32();
  693. const nodeOffset = reader.uint32();
  694. /* const nodeSize = */ reader.uint32();
  695. reader.seek(nodeOffset);
  696. for (let i = 0; i < this._nodes.length; i++) {
  697. const type = reader.string(64);
  698. const node = { type: type };
  699. node.index = reader.uint32();
  700. node.c = reader.uint32();
  701. if (major > 3) {
  702. node.d = reader.uint32();
  703. }
  704. this._nodes[i] = node;
  705. }
  706. }
  707. get name() {
  708. return this._name;
  709. }
  710. get nodes() {
  711. return this._nodes;
  712. }
  713. };
  714. openvx.BinaryReader = class extends base.BinaryReader {
  715. string(length) {
  716. const buffer = this.read(length);
  717. const index = buffer.indexOf(0);
  718. const data = index === -1 ? buffer : buffer.subarray(0, index);
  719. this._decoder = this._decoder || new TextDecoder('ascii');
  720. return this._decoder.decode(data);
  721. }
  722. };
  723. rknn.Error = class extends Error {
  724. constructor(message) {
  725. super(message);
  726. this.name = 'Error loading RKNN model.';
  727. }
  728. };
  729. if (typeof module !== 'undefined' && typeof module.exports === 'object') {
  730. module.exports.ModelFactory = rknn.ModelFactory;
  731. }