tnn.js 30 KB

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