tnn.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. var tnn = {};
  2. var text = require('./text');
  3. var base = require('./base');
  4. tnn.ModelFactory = class {
  5. match(context) {
  6. const identifier = context.identifier.toLowerCase();
  7. const stream = context.stream;
  8. if (stream && identifier.endsWith('.tnnproto')) {
  9. try {
  10. const buffer = stream.peek();
  11. const reader = text.Reader.open(buffer, 2048);
  12. const content = reader.read();
  13. if (content !== undefined) {
  14. const line = content.trim();
  15. if (line.startsWith('"') && line.endsWith('"')) {
  16. const header = line.replace(/(^")|("$)/g, '').split(',').shift().trim().split(' ');
  17. if (header.length === 3 || (header.length >= 4 && (header[3] === '4206624770' || header[3] == '4206624772'))) {
  18. return 'tnn.model';
  19. }
  20. }
  21. }
  22. }
  23. catch (err) {
  24. // continue regardless of error
  25. }
  26. }
  27. if (stream && identifier.endsWith('.tnnmodel')) {
  28. for (const signature of [ [ 0x02, 0x00, 0xbc, 0xfa ], [ 0x04, 0x00, 0xbc, 0xfa ] ]) {
  29. if (signature.length <= stream.length && stream.peek(signature.length).every((value, index) => value === signature[index])) {
  30. return 'tnn.params';
  31. }
  32. }
  33. }
  34. return '';
  35. }
  36. open(context, match) {
  37. return context.metadata('tnn-metadata.json').then((metadata) => {
  38. switch (match) {
  39. case 'tnn.model': {
  40. const tnnmodel = context.identifier.substring(0, context.identifier.length - 9) + '.tnnmodel';
  41. return context.request(tnnmodel, null).then((stream) => {
  42. const buffer = stream.peek();
  43. return new tnn.Model(metadata, context.stream.peek(), buffer);
  44. }).catch(() => {
  45. return new tnn.Model(metadata, context.stream.peek(), null);
  46. });
  47. }
  48. case 'tnn.params': {
  49. const tnnproto = context.identifier.substring(0, context.identifier.length - 9) + '.tnnproto';
  50. return context.request(tnnproto, null).then((stream) => {
  51. const buffer = stream.peek();
  52. return new tnn.Model(metadata, buffer, context.stream.peek());
  53. });
  54. }
  55. default: {
  56. throw new tnn.Error("Unsupported TNN format '" + match + "'.");
  57. }
  58. }
  59. });
  60. }
  61. };
  62. tnn.Model = class {
  63. constructor(metadata, tnnproto, tnnmodel) {
  64. this._graphs = [
  65. new tnn.Graph(metadata, tnnproto, tnnmodel)
  66. ];
  67. }
  68. get format() {
  69. return 'TNN';
  70. }
  71. get graphs() {
  72. return this._graphs;
  73. }
  74. };
  75. tnn.Graph = class {
  76. constructor(metadata, tnnproto, tnnmodel) {
  77. this._inputs = [];
  78. this._outputs = [];
  79. this._nodes = [];
  80. const resources = new tnn.LayerResourceReader(tnnmodel);
  81. const reader = new tnn.TextProtoReader(tnnproto);
  82. for (const input of reader.inputs) {
  83. const shape = new tnn.TensorShape(input.shape);
  84. const type = new tnn.TensorType(input.data_type, shape);
  85. this._inputs.push(new tnn.Parameter(input.name, [ new tnn.Argument(input.name, type, null) ]));
  86. }
  87. for (const output of reader.outputs) {
  88. this._outputs.push(new tnn.Parameter(output.name, [ new tnn.Argument(output.name, null, null) ]));
  89. }
  90. for (const layer of reader.layers) {
  91. this._nodes.push(new tnn.Node(metadata, resources, layer));
  92. }
  93. }
  94. get inputs() {
  95. return this._inputs;
  96. }
  97. get outputs() {
  98. return this._outputs;
  99. }
  100. get nodes() {
  101. return this._nodes;
  102. }
  103. };
  104. tnn.Parameter = class {
  105. constructor(name, args) {
  106. this._name = name;
  107. this._arguments = args;
  108. }
  109. get name() {
  110. return this._name;
  111. }
  112. get visible() {
  113. return true;
  114. }
  115. get arguments() {
  116. return this._arguments;
  117. }
  118. };
  119. tnn.Argument = class {
  120. constructor(name, type, initializer) {
  121. if (typeof name !== 'string') {
  122. throw new tnn.Error("Invalid argument identifier '" + JSON.stringify(name) + "'.");
  123. }
  124. this._name = name;
  125. this._type = type || null;
  126. this._initializer = initializer || null;
  127. }
  128. get name() {
  129. return this._name;
  130. }
  131. get type() {
  132. if (this._initializer) {
  133. return this._initializer.type;
  134. }
  135. return this._type;
  136. }
  137. get initializer() {
  138. return this._initializer;
  139. }
  140. };
  141. tnn.Node = class {
  142. constructor(metadata, resources, layer) {
  143. this._inputs = [];
  144. this._outputs = [];
  145. this._attributes = [];
  146. this._name = layer.name;
  147. this._type = metadata.type(layer.type);
  148. const attributeSchemas = this._type && this._type.attributes ? this._type && this._type.attributes.slice() : [];
  149. const attributes = layer.attributes.slice();
  150. while (attributes.length > 0) {
  151. const attributeSchema = attributeSchemas.shift();
  152. let value = null;
  153. let name = '';
  154. if (attributeSchema && attributeSchema.type === 'int32[]' && attributeSchema.size) {
  155. name = attributeSchema.name;
  156. value = attributes.splice(0, layer.attr[attributeSchema.size]).map((attribute) => parseInt(attribute.value, 10));
  157. }
  158. else {
  159. const attribute = attributes.shift();
  160. name = attribute.key;
  161. value = attribute.value;
  162. }
  163. this._attributes.push(new tnn.Attribute(attributeSchema, name, value));
  164. }
  165. const inputs = layer.inputs;
  166. let inputIndex = 0;
  167. if (this._type && this._type.inputs) {
  168. for (const inputDef of this._type.inputs) {
  169. if (inputIndex < inputs.length || inputDef.option != 'optional') {
  170. const inputCount = (inputDef.option == 'variadic') ? (inputs.length - inputIndex) : 1;
  171. const inputArguments = inputs.slice(inputIndex, inputIndex + inputCount).filter((id) => id != '' || inputDef.option != 'optional').map((id) => {
  172. return new tnn.Argument(id, null, null);
  173. });
  174. this._inputs.push(new tnn.Parameter(inputDef.name, inputArguments));
  175. inputIndex += inputCount;
  176. }
  177. }
  178. }
  179. else {
  180. this._inputs.push(...inputs.slice(inputIndex).map((input, index) => {
  181. const inputName = ((inputIndex + index) == 0) ? 'input' : (inputIndex + index).toString();
  182. return new tnn.Parameter(inputName, [ new tnn.Argument(input, null, null) ]);
  183. }));
  184. }
  185. const outputs = layer.outputs;
  186. let outputIndex = 0;
  187. if (this._type && this._type.outputs) {
  188. for (const outputDef of this._type.outputs) {
  189. if (outputIndex < outputs.length || outputDef.option != 'optional') {
  190. const outputCount = (outputDef.option == 'variadic') ? (outputs.length - outputIndex) : 1;
  191. const outputArguments = outputs.slice(outputIndex, outputIndex + outputCount).map((id) => {
  192. return new tnn.Argument(id, null, null);
  193. });
  194. this._outputs.push(new tnn.Parameter(outputDef.name, outputArguments));
  195. outputIndex += outputCount;
  196. }
  197. }
  198. }
  199. else {
  200. this._outputs.push(...outputs.slice(outputIndex).map((output, index) => {
  201. const outputName = ((outputIndex + index) == 0) ? 'output' : (outputIndex + index).toString();
  202. return new tnn.Parameter(outputName, [ new tnn.Argument(output, null, null) ]);
  203. }));
  204. }
  205. switch (this._type.name) {
  206. case 'Convolution':
  207. case 'ConvolutionDepthWise':
  208. case 'Deconvolution':
  209. case 'DeconvolutionDepthWise': {
  210. const resource = resources.read(this._name);
  211. if (resource) {
  212. const num_output = parseInt(layer.attr['2'] || 0, 10);
  213. const kernel_w = parseInt(layer.attr['3'] || 0, 10);
  214. const kernel_h = parseInt(layer.attr['4'] || kernel_w, 10);
  215. const weight_data_size = resource.filter.length;
  216. this._weight(resource, 'filter', [ num_output, weight_data_size / (num_output * kernel_w * kernel_h), kernel_w, kernel_h ]);
  217. if (resource.bias) {
  218. this._weight(resource, 'bias', [ num_output ]);
  219. }
  220. if (resource.quantized) {
  221. this._weight(resource, 'quantized', [ num_output ]);
  222. }
  223. }
  224. break;
  225. }
  226. case 'Conv3D':{
  227. const resource = resources.read(this._name);
  228. if (resource) {
  229. const num_output = parseInt(layer.attr['2'] || 0, 10);
  230. const kernel_w = parseInt(layer.attr['3'] || 0, 10);
  231. const kernel_h = parseInt(layer.attr['4'] || kernel_w, 10);
  232. const kernel_d = parseInt(layer.attr['5'] || kernel_w, 10);
  233. const weight_data_size = resource.filter.length;
  234. this._weight(resource, 'weight', [ num_output, weight_data_size / (num_output * kernel_w * kernel_h * kernel_d), kernel_w, kernel_h, kernel_d ]);
  235. if (resource.bias) {
  236. this._weight(resources, 'bias', [ num_output ]);
  237. }
  238. }
  239. break;
  240. }
  241. case 'InnerProduct': {
  242. const resource = resources.read(this._name);
  243. if (resource) {
  244. const num_output = parseInt(layer.attr['0'] || 0, 10);
  245. const weight_data_size = resource.weight.length;
  246. this._weight(resource, 'weight', [ num_output, weight_data_size / num_output ]);
  247. this._weight(resource, 'bias', [ num_output ]);
  248. if (resource.weight.dataType === 'int8') {
  249. this._weight(resource, 'scale', [ num_output ]);
  250. }
  251. }
  252. break;
  253. }
  254. case 'PReLU': {
  255. const resource = resources.read(this._name);
  256. if (resource) {
  257. this._weight(resource, 'slope', [ resource.slope.length ]);
  258. }
  259. break;
  260. }
  261. case 'BatchNormCxx':
  262. case 'InstBatchNormCxx': {
  263. const resource = resources.read(this._name);
  264. if (resource) {
  265. this._weight(resource, 'scale', [ resource.scale.length ]);
  266. this._weight(resource, 'bias', [ resource.bias.length ]);
  267. }
  268. break;
  269. }
  270. case 'Div':
  271. case 'Sub':
  272. case 'Add':
  273. case 'Mul':
  274. case 'MatMul': {
  275. if (this._inputs.length === 1) {
  276. const resource = resources.read(this._name);
  277. if (resource) {
  278. const num_output = resource.slope.length;
  279. this._weight(resource, 'slope', [ num_output ]);
  280. }
  281. }
  282. break;
  283. }
  284. case 'HdrGuide': {
  285. const resource = resources.read(this._name);
  286. if (resource) {
  287. const weight_size = resource.ccm_weight.length;
  288. this._weight(resource, 'ccm_weight', [ weight_size ]);
  289. this._weight(resource, 'ccm_bias', [ weight_size ]);
  290. this._weight(resource, 'shifts', [ weight_size ]);
  291. this._weight(resource, 'slopes', [ weight_size ]);
  292. this._weight(resource, 'projection_weight', [ weight_size ]);
  293. this._weight(resource, 'projection_bias', [ weight_size ]);
  294. }
  295. break;
  296. }
  297. case 'BlobScale': {
  298. const resource = resources.read(this._name);
  299. if (resource) {
  300. const scale_data_size = resource.scale.length;
  301. this._weight(resource, 'scale', [ scale_data_size]);
  302. this._weight(resource, 'bias', [ scale_data_size ]);
  303. }
  304. break;
  305. }
  306. case 'Gather': {
  307. const resource = resources.read(this._name);
  308. if (resource) {
  309. if (resource.data) {
  310. this._weight(resource, 'data', [ resource.data.length ]);
  311. }
  312. if (resource.indices) {
  313. this._weight(resource, 'indices', [ resource.indices.length ]);
  314. }
  315. }
  316. break;
  317. }
  318. default: {
  319. break;
  320. }
  321. }
  322. }
  323. get type() {
  324. return this._type;
  325. }
  326. get name() {
  327. return this._name;
  328. }
  329. get attributes() {
  330. return this._attributes;
  331. }
  332. get inputs() {
  333. return this._inputs;
  334. }
  335. get outputs() {
  336. return this._outputs;
  337. }
  338. _weight(resource, name, shape) {
  339. const initializer = resource[name];
  340. if (!initializer) {
  341. throw new tnn.Error("Layer initializer'" + resource.type + "." + name + "' not found '");
  342. }
  343. const tensor = new tnn.Tensor(new tnn.TensorType(initializer.dataType, new tnn.TensorShape(shape)), initializer.value);
  344. this._inputs.push(new tnn.Parameter(name, [ new tnn.Argument('', null, tensor) ]));
  345. }
  346. };
  347. tnn.Attribute = class {
  348. constructor(schema, key, value) {
  349. this._type = '';
  350. this._name = key.toString();
  351. this._value = value;
  352. if (schema) {
  353. this._name = schema.name;
  354. if (schema.type) {
  355. this._type = schema.type;
  356. }
  357. switch (this._type) {
  358. case '':
  359. break;
  360. case 'int32':
  361. this._value = parseInt(this._value, 10);
  362. break;
  363. case 'float32':
  364. this._value = parseFloat(this._value);
  365. break;
  366. case 'int32[]':
  367. this._value = this._value.map((v) => parseInt(v, 10));
  368. break;
  369. case 'float32[]':
  370. this._value = this._value.map((v) => parseFloat(v));
  371. break;
  372. default:
  373. throw new tnn.Error("Unsupported attribute type '" + this._type + "'.");
  374. }
  375. if (Object.prototype.hasOwnProperty.call(schema, 'visible') && !schema.visible) {
  376. this._visible = false;
  377. }
  378. else if (Object.prototype.hasOwnProperty.call(schema, 'default')) {
  379. if (this._value == schema.default || (this._value && this._value.toString() == schema.default.toString())) {
  380. this._visible = false;
  381. }
  382. }
  383. }
  384. }
  385. get type() {
  386. return this._type;
  387. }
  388. get name() {
  389. return this._name;
  390. }
  391. get value() {
  392. return this._value;
  393. }
  394. get visible() {
  395. return this._visible == false ? false : true;
  396. }
  397. };
  398. tnn.Tensor = class {
  399. constructor(type, data) {
  400. this._type = type;
  401. this._data = data;
  402. }
  403. get category() {
  404. return 'Weight';
  405. }
  406. get type() {
  407. return this._type;
  408. }
  409. get values() {
  410. return this._data;
  411. }
  412. };
  413. tnn.TensorType = class {
  414. constructor(dataType, shape) {
  415. this._dataType = dataType || '?';
  416. this._shape = shape;
  417. }
  418. get dataType() {
  419. return this._dataType;
  420. }
  421. get shape() {
  422. return this._shape;
  423. }
  424. toString() {
  425. return this._dataType + this._shape.toString();
  426. }
  427. };
  428. tnn.TensorShape = class {
  429. constructor(dimensions) {
  430. this._dimensions = dimensions;
  431. }
  432. get dimensions() {
  433. return this._dimensions;
  434. }
  435. toString() {
  436. return this._dimensions ? ('[' + this._dimensions.map((dimension) => dimension ? dimension.toString() : '?').join(',') + ']') : '';
  437. }
  438. };
  439. tnn.TextProtoReader = class {
  440. constructor(buffer) {
  441. const reader = text.Reader.open(buffer);
  442. let lines = [];
  443. for (;;) {
  444. const line = reader.read();
  445. if (line === undefined) {
  446. break;
  447. }
  448. lines.push(line.replace(/\r|"/g, ''));
  449. }
  450. const split = (line, delimiter, trim, ignore_blank) => {
  451. return line.split(delimiter).map((v) => trim ? v.trim() : v).filter((v) => !ignore_blank || v);
  452. };
  453. lines = split(lines.join(''), ',', true, false);
  454. if (lines.length <= 5) {
  455. throw new tnn.Error('Invalid line count.');
  456. }
  457. const header = split(lines.shift(), ' ', true, false);
  458. if (header.length < 3) {
  459. throw new tnn.Error('Invalid header size.');
  460. }
  461. else if (header.length > 3 && (header[3] !== '4206624770' && header[3] !== '4206624772')) {
  462. throw new tnn.Error("Invalid signature '" + header[3] + "'.");
  463. }
  464. this._inputs = split(lines.shift(), ':', true, false).map((input) => {
  465. const array = split(input, ' ', true, false);
  466. const name = array.shift();
  467. if (header[3] === '4206624772') {
  468. const shape_size = parseInt(array.shift(), 10);
  469. const data_type_index = parseInt(array[shape_size], 10);
  470. return {
  471. name: name,
  472. data_type: [ 'float32', 'float16', 'int8', 'int32', 'bfloat16' ][data_type_index],
  473. shape: array.slice(0, -1).map((dim) => parseInt(dim, 10)),
  474. };
  475. }
  476. return {
  477. name: name,
  478. data_type: 'float32',
  479. shape: array.map((dim) => parseInt(dim, 10))
  480. };
  481. });
  482. lines.shift();
  483. this._outputs = split(lines.shift(), ' ', true, false).map((output) => {
  484. return { name: output };
  485. });
  486. lines.shift();
  487. this._layers = [];
  488. while (lines.length > 0) {
  489. const line = lines.shift().trim();
  490. if (line.length > 0) {
  491. const array = split(line, ' ', true, true);
  492. const layer = {};
  493. layer.type = array.shift();
  494. layer.name = array.shift();
  495. const inputCount = parseInt(array.shift(), 10);
  496. const outputCount = parseInt(array.shift(), 10);
  497. layer.inputs = array.splice(0, inputCount);
  498. layer.outputs = array.splice(0, outputCount);
  499. layer.attr = {};
  500. layer.attributes = [];
  501. let count = 0;
  502. for (const column of array) {
  503. const parts = column.split(' ');
  504. if (parts.length === 1) {
  505. let key = count;
  506. let value = parts.toString();
  507. const keyInt = parseInt(key, 10);
  508. if (keyInt < 0) {
  509. value = value.split(',').map((v) => v.trim());
  510. value.shift();
  511. key = (-(keyInt + 23300)).toString();
  512. }
  513. layer.attr[key] = value;
  514. layer.attributes.push({ key: key, value: value });
  515. count++;
  516. }
  517. }
  518. this._layers.push(layer);
  519. }
  520. }
  521. }
  522. get inputs() {
  523. return this._inputs;
  524. }
  525. get outputs() {
  526. return this._outputs;
  527. }
  528. get layers() {
  529. return this._layers;
  530. }
  531. };
  532. tnn.LayerResourceReader = class {
  533. constructor(buffer) {
  534. this._layerResources = [];
  535. if (buffer) {
  536. const reader = new base.BinaryReader(buffer);
  537. const magic_number = reader.uint32();
  538. if (magic_number !== 0xFABC0002 && magic_number !== 0xFABC0004) {
  539. throw new tnn.Error("Invalid blob header signature '" + magic_number.toString() + "'.");
  540. }
  541. this._layerResources = new Array(reader.int32() & 0x1FFFFFFF);
  542. const raw = (reader) => {
  543. const magic_number = reader.uint32();
  544. if (magic_number !== 0xFABC0002 && magic_number !== 0xFABC0004) {
  545. throw new tnn.Error("Invalid raw signature '" + magic_number.toString() + "'.");
  546. }
  547. const data_type = reader.int32();
  548. if (data_type > 4) {
  549. throw new tnn.Error("Unsupported data type '" + data_type + "'.");
  550. }
  551. const length = reader.int32();
  552. if (length <= 0) {
  553. return null;
  554. }
  555. let dims = null;
  556. if (magic_number === 0xFABC0004) {
  557. const dim_size = reader.int32();
  558. dims = reader.read(dim_size * 4);
  559. }
  560. return {
  561. dataType: [ 'float32', 'float16', 'int8', 'int32', 'bfloat16' ][data_type],
  562. length: length / [ 4, 2, 1, 4, 2 ][data_type],
  563. value: reader.read(length),
  564. shape: dims
  565. };
  566. };
  567. const expect = (reader, name) => {
  568. const content = reader.string();
  569. if (name !== content) {
  570. throw new tnn.Error("Invalid string '" + content + "' instead of '" + name + "'.");
  571. }
  572. };
  573. for (let i = 0; i < this._layerResources.length; i++) {
  574. const resource = {};
  575. resource.operator = reader.int32();
  576. resource.type = reader.string();
  577. resource.name = reader.string();
  578. switch (resource.type) {
  579. case 'Convolution':
  580. case 'ConvolutionDepthWise':
  581. case 'Deconvolution':
  582. case 'DeconvolutionDepthWise': {
  583. expect(reader, resource.name);
  584. const bias = reader.int32();
  585. resource.filter = raw(reader);
  586. if (bias) {
  587. resource.bias = raw(reader);
  588. }
  589. if (resource.filter.dataType === 'int8') {
  590. resource.quantized = raw(reader);
  591. }
  592. break;
  593. }
  594. case 'Conv3D': {
  595. expect(reader, resource.name);
  596. const bias = reader.int32();
  597. resource.filter = raw(reader);
  598. if (bias) {
  599. resource.bias = raw(reader);
  600. }
  601. break;
  602. }
  603. case 'InnerProduct': {
  604. expect(reader, resource.name);
  605. resource.weight = raw(reader);
  606. resource.bias = raw(reader);
  607. if (resource.weight.dataType === 'int8') {
  608. resource.scale = raw(reader);
  609. }
  610. break;
  611. }
  612. case 'PReLU': {
  613. expect(reader, resource.name);
  614. resource.slope = raw(reader);
  615. break;
  616. }
  617. case 'Add':
  618. case 'Div':
  619. case 'Mul':
  620. case 'Sub':
  621. case 'MatMul': {
  622. resource.slope = raw(reader);
  623. break;
  624. }
  625. case 'BatchNormCxx':
  626. case 'InstBatchNormCxx':
  627. resource.scale = raw(reader);
  628. resource.bias = raw(reader);
  629. break;
  630. case 'HdrGuide':
  631. resource.ccm_weight = raw(reader);
  632. resource.ccm_bias = raw(reader);
  633. resource.shifts = raw(reader);
  634. resource.slopes = raw(reader);
  635. resource.projection_weight = raw(reader);
  636. resource.projection_bias = raw(reader);
  637. break;
  638. case 'BlobScale':
  639. resource.scale = raw(reader);
  640. resource.bias = raw(reader);
  641. break;
  642. case 'Gather': {
  643. // reader.expect(resource.name);
  644. const has_data = reader.int32();
  645. if (has_data) {
  646. resource.data = raw(reader);
  647. }
  648. const has_indices = reader.int32();
  649. if (has_indices) {
  650. resource.indices = raw(reader);
  651. }
  652. break;
  653. }
  654. default: {
  655. throw new tnn.Error("Unsupported layer resource type '" + resource.type + "'.");
  656. }
  657. }
  658. this._layerResources[i] = resource;
  659. }
  660. if (reader.position !== reader.length) {
  661. throw new tnn.Error("Invalid blob size.");
  662. }
  663. }
  664. }
  665. read(name) {
  666. const resource = this._layerResources.shift();
  667. if (resource && resource.name !== name) {
  668. throw new tnn.Error("Invalid blob layer name '" + name + "'.");
  669. }
  670. return resource;
  671. }
  672. };
  673. tnn.Error = class extends Error {
  674. constructor(message) {
  675. super(message);
  676. this.name = 'Error loading TNN model.';
  677. }
  678. };
  679. if (typeof module !== 'undefined' && typeof module.exports === 'object') {
  680. module.exports.ModelFactory = tnn.ModelFactory;
  681. }