tnn.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. var tnn = tnn || {};
  2. var text = text || require('./text');
  3. var base = 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 kind() {
  404. return 'Weight';
  405. }
  406. get type() {
  407. return this._type;
  408. }
  409. get state() {
  410. return this._context().state || null;
  411. }
  412. get value() {
  413. const context = this._context();
  414. if (context.state) {
  415. return null;
  416. }
  417. context.limit = Number.MAX_SAFE_INTEGER;
  418. return this._decode(context, 0);
  419. }
  420. toString() {
  421. const context = this._context();
  422. if (context.state) {
  423. return '';
  424. }
  425. context.limit = 10000;
  426. const value = this._decode(context, 0);
  427. return JSON.stringify(value, null, 4);
  428. }
  429. _context() {
  430. const context = {};
  431. context.index = 0;
  432. context.count = 0;
  433. context.state = null;
  434. if (this._type.dataType == '?') {
  435. context.state = 'Tensor has unknown data type.';
  436. return context;
  437. }
  438. if (!this._type.shape) {
  439. context.state = 'Tensor has no dimensions.';
  440. return context;
  441. }
  442. if (!this._data) {
  443. context.state = 'Tensor data is empty.';
  444. return context;
  445. }
  446. switch (this._type.dataType) {
  447. case 'int32':
  448. case 'float16':
  449. case 'float32':
  450. context.data = new DataView(this._data.buffer, this._data.byteOffset, this._data.byteLength);
  451. break;
  452. default:
  453. context.state = 'Tensor data type is not implemented.';
  454. break;
  455. }
  456. context.dataType = this._type.dataType;
  457. context.shape = this._type.shape.dimensions;
  458. return context;
  459. }
  460. _decode(context, dimension) {
  461. const shape = context.shape.length !== 0 ? context.shape : [ 1 ];
  462. const results = [];
  463. const size = shape[dimension];
  464. if (dimension == shape.length - 1) {
  465. for (let i = 0; i < size; i++) {
  466. if (context.count > context.limit) {
  467. results.push('...');
  468. return results;
  469. }
  470. switch (this._type.dataType) {
  471. case 'int32':
  472. results.push(context.data.getInt32(context.index, true));
  473. context.index += 4;
  474. context.count++;
  475. break;
  476. case 'float32':
  477. results.push(context.data.getFloat32(context.index, true));
  478. context.index += 4;
  479. context.count++;
  480. break;
  481. case 'float16':
  482. results.push(context.data.getFloat16(context.index, true));
  483. context.index += 2;
  484. context.count++;
  485. break;
  486. default:
  487. throw new tnn.Error("Unsupported tensor data type '" + this._type.dataType + "'.");
  488. }
  489. }
  490. }
  491. else {
  492. for (let j = 0; j < size; j++) {
  493. if (context.count > context.limit) {
  494. results.push('...');
  495. return results;
  496. }
  497. results.push(this._decode(context, dimension + 1));
  498. }
  499. }
  500. if (context.shape.length == 0) {
  501. return results[0];
  502. }
  503. return results;
  504. }
  505. };
  506. tnn.TensorType = class {
  507. constructor(dataType, shape) {
  508. this._dataType = dataType || '?';
  509. this._shape = shape;
  510. }
  511. get dataType() {
  512. return this._dataType;
  513. }
  514. get shape() {
  515. return this._shape;
  516. }
  517. toString() {
  518. return this._dataType + this._shape.toString();
  519. }
  520. };
  521. tnn.TensorShape = class {
  522. constructor(dimensions) {
  523. this._dimensions = dimensions;
  524. }
  525. get dimensions() {
  526. return this._dimensions;
  527. }
  528. toString() {
  529. return this._dimensions ? ('[' + this._dimensions.map((dimension) => dimension ? dimension.toString() : '?').join(',') + ']') : '';
  530. }
  531. };
  532. tnn.TextProtoReader = class {
  533. constructor(buffer) {
  534. const reader = text.Reader.open(buffer);
  535. let lines = [];
  536. for (;;) {
  537. const line = reader.read();
  538. if (line === undefined) {
  539. break;
  540. }
  541. lines.push(line.replace(/\r|"/g, ''));
  542. }
  543. const split = (line, delimiter, trim, ignore_blank) => {
  544. return line.split(delimiter).map((v) => trim ? v.trim() : v).filter((v) => !ignore_blank || v);
  545. };
  546. lines = split(lines.join(''), ',', true, false);
  547. if (lines.length <= 5) {
  548. throw new tnn.Error('Invalid line count.');
  549. }
  550. const header = split(lines.shift(), ' ', true, false);
  551. if (header.length < 3) {
  552. throw new tnn.Error('Invalid header size.');
  553. }
  554. else if (header.length > 3 && (header[3] !== '4206624770' && header[3] !== '4206624772')) {
  555. throw new tnn.Error("Invalid signature '" + header[3] + "'.");
  556. }
  557. this._inputs = split(lines.shift(), ':', true, false).map((input) => {
  558. const array = split(input, ' ', true, false);
  559. const name = array.shift();
  560. if (header[3] === '4206624772') {
  561. const shape_size = parseInt(array.shift(), 10);
  562. const data_type_index = parseInt(array[shape_size], 10);
  563. return {
  564. name: name,
  565. data_type: [ 'float32', 'float16', 'int8', 'int32', 'bfloat16' ][data_type_index],
  566. shape: array.slice(0, -1).map((dim) => parseInt(dim, 10)),
  567. };
  568. }
  569. return {
  570. name: name,
  571. data_type: 'float32',
  572. shape: array.map((dim) => parseInt(dim, 10))
  573. };
  574. });
  575. lines.shift();
  576. this._outputs = split(lines.shift(), ' ', true, false).map((output) => {
  577. return { name: output };
  578. });
  579. lines.shift();
  580. this._layers = [];
  581. while (lines.length > 0) {
  582. const line = lines.shift().trim();
  583. if (line.length > 0) {
  584. const array = split(line, ' ', true, true);
  585. const layer = {};
  586. layer.type = array.shift();
  587. layer.name = array.shift();
  588. const inputCount = parseInt(array.shift(), 10);
  589. const outputCount = parseInt(array.shift(), 10);
  590. layer.inputs = array.splice(0, inputCount);
  591. layer.outputs = array.splice(0, outputCount);
  592. layer.attr = {};
  593. layer.attributes = [];
  594. let count = 0;
  595. for (const column of array) {
  596. const parts = column.split(' ');
  597. if (parts.length === 1) {
  598. let key = count;
  599. let value = parts.toString();
  600. const keyInt = parseInt(key, 10);
  601. if (keyInt < 0) {
  602. value = value.split(',').map((v) => v.trim());
  603. value.shift();
  604. key = (-(keyInt + 23300)).toString();
  605. }
  606. layer.attr[key] = value;
  607. layer.attributes.push({ key: key, value: value });
  608. count++;
  609. }
  610. }
  611. this._layers.push(layer);
  612. }
  613. }
  614. }
  615. get inputs() {
  616. return this._inputs;
  617. }
  618. get outputs() {
  619. return this._outputs;
  620. }
  621. get layers() {
  622. return this._layers;
  623. }
  624. };
  625. tnn.LayerResourceReader = class {
  626. constructor(buffer) {
  627. this._layerResources = [];
  628. if (buffer) {
  629. const reader = new base.BinaryReader(buffer);
  630. const magic_number = reader.uint32();
  631. if (magic_number !== 0xFABC0002 && magic_number !== 0xFABC0004) {
  632. throw new tnn.Error("Invalid blob header signature '" + magic_number.toString() + "'.");
  633. }
  634. this._layerResources = new Array(reader.int32() & 0x1FFFFFFF);
  635. const raw = (reader) => {
  636. const magic_number = reader.uint32();
  637. if (magic_number !== 0xFABC0002 && magic_number !== 0xFABC0004) {
  638. throw new tnn.Error("Invalid raw signature '" + magic_number.toString() + "'.");
  639. }
  640. const data_type = reader.int32();
  641. if (data_type > 4) {
  642. throw new tnn.Error("Unsupported data type '" + data_type + "'.");
  643. }
  644. const length = reader.int32();
  645. if (length <= 0) {
  646. return null;
  647. }
  648. let dims = null;
  649. if (magic_number === 0xFABC0004) {
  650. const dim_size = reader.int32();
  651. dims = reader.read(dim_size * 4);
  652. }
  653. return {
  654. dataType: [ 'float32', 'float16', 'int8', 'int32', 'bfloat16' ][data_type],
  655. length: length / [ 4, 2, 1, 4, 2 ][data_type],
  656. value: reader.read(length),
  657. shape: dims
  658. };
  659. };
  660. const expect = (reader, name) => {
  661. const content = reader.string();
  662. if (name !== content) {
  663. throw new tnn.Error("Invalid string '" + content + "' instead of '" + name + "'.");
  664. }
  665. };
  666. for (let i = 0; i < this._layerResources.length; i++) {
  667. const resource = {};
  668. resource.operator = reader.int32();
  669. resource.type = reader.string();
  670. resource.name = reader.string();
  671. switch (resource.type) {
  672. case 'Convolution':
  673. case 'ConvolutionDepthWise':
  674. case 'Deconvolution':
  675. case 'DeconvolutionDepthWise': {
  676. expect(reader, resource.name);
  677. const bias = reader.int32();
  678. resource.filter = raw(reader);
  679. if (bias) {
  680. resource.bias = raw(reader);
  681. }
  682. if (resource.filter.dataType === 'int8') {
  683. resource.quantized = raw(reader);
  684. }
  685. break;
  686. }
  687. case 'Conv3D': {
  688. expect(reader, resource.name);
  689. const bias = reader.int32();
  690. resource.filter = raw(reader);
  691. if (bias) {
  692. resource.bias = raw(reader);
  693. }
  694. break;
  695. }
  696. case 'InnerProduct': {
  697. expect(reader, resource.name);
  698. resource.weight = raw(reader);
  699. resource.bias = raw(reader);
  700. if (resource.weight.dataType === 'int8') {
  701. resource.scale = raw(reader);
  702. }
  703. break;
  704. }
  705. case 'PReLU': {
  706. expect(reader, resource.name);
  707. resource.slope = raw(reader);
  708. break;
  709. }
  710. case 'Add':
  711. case 'Div':
  712. case 'Mul':
  713. case 'Sub':
  714. case 'MatMul': {
  715. resource.slope = raw(reader);
  716. break;
  717. }
  718. case 'BatchNormCxx':
  719. case 'InstBatchNormCxx':
  720. resource.scale = raw(reader);
  721. resource.bias = raw(reader);
  722. break;
  723. case 'HdrGuide':
  724. resource.ccm_weight = raw(reader);
  725. resource.ccm_bias = raw(reader);
  726. resource.shifts = raw(reader);
  727. resource.slopes = raw(reader);
  728. resource.projection_weight = raw(reader);
  729. resource.projection_bias = raw(reader);
  730. break;
  731. case 'BlobScale':
  732. resource.scale = raw(reader);
  733. resource.bias = raw(reader);
  734. break;
  735. case 'Gather': {
  736. // reader.expect(resource.name);
  737. const has_data = reader.int32();
  738. if (has_data) {
  739. resource.data = raw(reader);
  740. }
  741. const has_indices = reader.int32();
  742. if (has_indices) {
  743. resource.indices = raw(reader);
  744. }
  745. break;
  746. }
  747. default: {
  748. throw new tnn.Error("Unsupported layer resource type '" + resource.type + "'.");
  749. }
  750. }
  751. this._layerResources[i] = resource;
  752. }
  753. if (reader.position !== reader.length) {
  754. throw new tnn.Error("Invalid blob size.");
  755. }
  756. }
  757. }
  758. read(name) {
  759. const resource = this._layerResources.shift();
  760. if (resource && resource.name !== name) {
  761. throw new tnn.Error("Invalid blob layer name '" + name + "'.");
  762. }
  763. return resource;
  764. }
  765. };
  766. tnn.Error = class extends Error {
  767. constructor(message) {
  768. super(message);
  769. this.name = 'Error loading TNN model.';
  770. }
  771. };
  772. if (typeof module !== 'undefined' && typeof module.exports === 'object') {
  773. module.exports.ModelFactory = tnn.ModelFactory;
  774. }