tengine.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. // Experimental
  2. var tengine = tengine || {};
  3. var base = base || require('./base');
  4. tengine.ModelFactory = class {
  5. match(context) {
  6. return tengine.Reader.open(context.stream);
  7. }
  8. open(context, match) {
  9. return tengine.Metadata.open(context).then((metadata) => {
  10. return new tengine.Model(metadata, match);
  11. });
  12. }
  13. };
  14. tengine.Model = class {
  15. constructor(metadata, reader) {
  16. this._version = reader.version;
  17. this._metadata = [
  18. { name: 'source', value: reader.source }
  19. ];
  20. this._graphs = reader.graphs.map((graph) => new tengine.Graph(metadata, graph));
  21. }
  22. get format() {
  23. return "Tengine v" + this._version;
  24. }
  25. get metadata() {
  26. return this._metadata;
  27. }
  28. get graphs() {
  29. return this._graphs;
  30. }
  31. };
  32. tengine.Graph = class {
  33. constructor(metadata, graph) {
  34. this._name = graph.id.toString();
  35. this._inputs = [];
  36. this._outputs = [];
  37. this._nodes = [];
  38. const tensors = graph.tensors.map((tensor) => new tengine.Argument(tensor));
  39. for (const input of graph.inputs) {
  40. const node = graph.nodes[input];
  41. this._inputs.push(new tengine.Parameter(node.name, true, node.outputs.map((output) => tensors[output])));
  42. }
  43. for (const output of graph.outputs) {
  44. const node = graph.nodes[output];
  45. this._outputs.push(new tengine.Parameter(node.name, true, node.outputs.map((output) => tensors[output])));
  46. }
  47. for (const node of graph.nodes) {
  48. switch (node.type) {
  49. case 'INPUT':
  50. case 'Const':
  51. break;
  52. default:
  53. this._nodes.push(new tengine.Node(metadata, node, tensors));
  54. break;
  55. }
  56. }
  57. }
  58. get name() {
  59. return this._name;
  60. }
  61. get inputs() {
  62. return this._inputs;
  63. }
  64. get outputs() {
  65. return this._outputs;
  66. }
  67. get nodes() {
  68. return this._nodes;
  69. }
  70. };
  71. tengine.Parameter = class {
  72. constructor(name, visible, args) {
  73. this._name = name;
  74. this._visible = visible;
  75. this._arguments = args;
  76. }
  77. get name() {
  78. return this._name;
  79. }
  80. get visible() {
  81. return this._visible;
  82. }
  83. get arguments() {
  84. return this._arguments;
  85. }
  86. };
  87. tengine.Argument = class {
  88. constructor(tensor) {
  89. this._name = tensor.name;
  90. this._type = new tengine.TensorType(tensor.dataType, new tengine.TensorShape(tensor.dims));
  91. this._initializer = (tensor.type === 2) ? new tengine.Tensor(this._type, tensor.buffer) : null;
  92. }
  93. get name() {
  94. return this._name;
  95. }
  96. get type() {
  97. if (this._initializer) {
  98. return this._initializer.type;
  99. }
  100. return this._type;
  101. }
  102. get quantization() {
  103. return null;
  104. }
  105. get initializer() {
  106. return this._initializer;
  107. }
  108. };
  109. tengine.Node = class {
  110. constructor(metadata, node, tensors) {
  111. this._name = node.name;
  112. const type = node.type; + (node.version && node.version !== 1 ? ':' + node.version.toString() : '');
  113. const version = node.version;
  114. this._inputs = [];
  115. this._outputs = [];
  116. this._attributes = [];
  117. this._type = metadata.type(type, version) || { name: type };
  118. for (let i = 0; i < node.params.length; i++) {
  119. const attributeSchema = (this._type && this._type.attributes && i < this._type.attributes.length) ? this._type.attributes[i] : null;
  120. const attributeName = attributeSchema ? attributeSchema.name : i.toString();
  121. this._attributes.push(new tengine.Attribute(attributeSchema, attributeName, node.params[i]));
  122. }
  123. const inputs = node.inputs;
  124. let inputIndex = 0;
  125. if (this._type && this._type.inputs) {
  126. for (const inputDef of this._type.inputs) {
  127. if (inputIndex < inputs.length || inputDef.option != 'optional') {
  128. const inputCount = (inputDef.option == 'variadic') ? (inputs.length - inputIndex) : 1;
  129. const inputArguments = inputs.slice(inputIndex, inputIndex + inputCount).filter((id) => id != '' || inputDef.option != 'optional').map((id) => tensors[id]);
  130. this._inputs.push(new tengine.Parameter(inputDef.name, true, inputArguments));
  131. inputIndex += inputCount;
  132. }
  133. }
  134. }
  135. else {
  136. this._inputs.push(...inputs.slice(inputIndex).map((id, index) => {
  137. const inputName = ((inputIndex + index) == 0) ? 'input' : (inputIndex + index).toString();
  138. return new tengine.Parameter(inputName, true, [ tensors[id] ]);
  139. }));
  140. }
  141. const outputs = node.outputs;
  142. let outputIndex = 0;
  143. if (this._type && this._type.outputs) {
  144. for (const outputDef of this._type.outputs) {
  145. if (outputIndex < outputs.length || outputDef.option != 'optional') {
  146. const outputCount = (outputDef.option == 'variadic') ? (outputs.length - outputIndex) : 1;
  147. const outputArguments = outputs.slice(outputIndex, outputIndex + outputCount).map((id) => tensors[id]);
  148. this._outputs.push(new tengine.Parameter(outputDef.name, true, outputArguments));
  149. outputIndex += outputCount;
  150. }
  151. }
  152. }
  153. else {
  154. this._outputs.push(...outputs.slice(outputIndex).map((id, index) => {
  155. const outputName = ((outputIndex + index) == 0) ? 'output' : (outputIndex + index).toString();
  156. return new tengine.Parameter(outputName, true, [ tensors[id] ]);
  157. }));
  158. }
  159. }
  160. get type() {
  161. return this._type;
  162. }
  163. get name() {
  164. return this._name;
  165. }
  166. get attributes() {
  167. return this._attributes;
  168. }
  169. get inputs() {
  170. return this._inputs;
  171. }
  172. get outputs() {
  173. return this._outputs;
  174. }
  175. };
  176. tengine.Attribute = class {
  177. constructor(schema, key, value) {
  178. this._type = '';
  179. this._name = key;
  180. this._value = value;
  181. if (schema) {
  182. this._name = schema.name;
  183. if (schema.type) {
  184. this._type = schema.type;
  185. }
  186. if (Object.prototype.hasOwnProperty.call(schema, 'visible') && !schema.visible) {
  187. this._visible = false;
  188. }
  189. else if (Object.prototype.hasOwnProperty.call(schema, 'default')) {
  190. if (this._value == schema.default || (this._value && this._value.toString() == schema.default.toString())) {
  191. this._visible = false;
  192. }
  193. }
  194. }
  195. }
  196. get type() {
  197. return this._type;
  198. }
  199. get name() {
  200. return this._name;
  201. }
  202. get value() {
  203. return this._value;
  204. }
  205. get visible() {
  206. return this._visible == false ? false : true;
  207. }
  208. };
  209. tengine.Tensor = class {
  210. constructor(type, data, kind) {
  211. this._type = type;
  212. this._data = data;
  213. this._kind = kind;
  214. }
  215. get kind() {
  216. return this._kind;
  217. }
  218. get type() {
  219. return this._type;
  220. }
  221. get state() {
  222. return this._context().state || null;
  223. }
  224. get value() {
  225. const context = this._context();
  226. if (context.state) {
  227. return null;
  228. }
  229. context.limit = Number.MAX_SAFE_INTEGER;
  230. return this._decode(context, 0);
  231. }
  232. toString() {
  233. const context = this._context();
  234. if (context.state) {
  235. return '';
  236. }
  237. context.limit = 10000;
  238. const value = this._decode(context, 0);
  239. return JSON.stringify(value, null, 4);
  240. }
  241. _context() {
  242. const context = {};
  243. context.index = 0;
  244. context.count = 0;
  245. context.state = null;
  246. if (this._type.dataType == '?') {
  247. context.state = 'Tensor has unknown data type.';
  248. return context;
  249. }
  250. if (!this._type.shape || (this._type.shape.dimensions && this._type.shape.dimensions.length == 0)) {
  251. context.state = 'Tensor has no dimensions.';
  252. return context;
  253. }
  254. if (!this._data) {
  255. context.state = 'Tensor data is empty.';
  256. return context;
  257. }
  258. switch (this._type.dataType) {
  259. case 'int8':
  260. case 'uint8':
  261. case 'float16':
  262. case 'float32':
  263. case 'int32':
  264. case 'int16':
  265. context.data = new DataView(this._data.buffer, this._data.byteOffset, this._data.byteLength);
  266. break;
  267. default:
  268. context.state = 'Tensor data type is not implemented.';
  269. break;
  270. }
  271. context.dataType = this._type.dataType;
  272. context.shape = this._type.shape.dimensions;
  273. return context;
  274. }
  275. _decode(context, dimension) {
  276. const shape = context.shape.length == 0 ? [ 1 ] : context.shape;
  277. const results = [];
  278. const size = shape[dimension];
  279. if (dimension == shape.length - 1) {
  280. for (let i = 0; i < size; i++) {
  281. if (context.count > context.limit) {
  282. results.push('...');
  283. return results;
  284. }
  285. switch (this._type.dataType) {
  286. case 'float32':
  287. results.push(context.data.getFloat32(context.index, true));
  288. context.index += 4;
  289. context.count++;
  290. break;
  291. case 'float16':
  292. results.push(context.data.getFloat16(context.index, true));
  293. context.index += 2;
  294. context.count++;
  295. break;
  296. case 'int8':
  297. results.push(context.data.getInt8(context.index, true));
  298. context.index += 1;
  299. context.count++;
  300. break;
  301. case 'uint8':
  302. results.push(context.data.getUint8(context.index, true));
  303. context.index += 1;
  304. context.count++;
  305. break;
  306. case 'int32':
  307. results.push(context.data.getInt32(context.index, true));
  308. context.index += 4;
  309. context.count++;
  310. break;
  311. case 'int16':
  312. results.push(context.data.getInt16(context.index, true));
  313. context.index += 2;
  314. context.count++;
  315. break;
  316. default:
  317. throw new tengine.Error("Unsupported tensor data type '" + this._type.dataType + "'.");
  318. }
  319. }
  320. }
  321. else {
  322. for (let j = 0; j < size; j++) {
  323. if (context.count > context.limit) {
  324. results.push('...');
  325. return results;
  326. }
  327. results.push(this._decode(context, dimension + 1));
  328. }
  329. }
  330. if (context.shape.length == 0) {
  331. return results[0];
  332. }
  333. return results;
  334. }
  335. };
  336. tengine.TensorType = class {
  337. constructor(dataType, shape) {
  338. switch (dataType) {
  339. case 0: this._dataType = 'float32'; break;
  340. case 1: this._dataType = 'float16'; break;
  341. case 2: this._dataType = 'int8'; break;
  342. case 3: this._dataType = 'uint8'; break;
  343. case 4: this._dataType = 'int32'; break;
  344. case 5: this._dataType = 'int16'; break;
  345. default: throw new tengine.Error("Unsupported data type'" + dataType + "'.");
  346. }
  347. this._shape = shape;
  348. }
  349. get dataType() {
  350. return this._dataType;
  351. }
  352. get shape() {
  353. return this._shape;
  354. }
  355. toString() {
  356. return this._dataType + this._shape.toString();
  357. }
  358. };
  359. tengine.TensorShape = class {
  360. constructor(dimensions) {
  361. this._dimensions = dimensions;
  362. }
  363. get dimensions() {
  364. return this._dimensions;
  365. }
  366. toString() {
  367. return this._dimensions ? ('[' + this._dimensions.map((dimension) => dimension ? dimension.toString() : '?').join(',') + ']') : '';
  368. }
  369. };
  370. tengine.Metadata = class {
  371. static open(context) {
  372. if (tengine.Metadata._metadata) {
  373. return Promise.resolve(tengine.Metadata._metadata);
  374. }
  375. return context.request('tengine-metadata.json', 'utf-8', null).then((data) => {
  376. tengine.Metadata._metadata = new tengine.Metadata(data);
  377. return tengine.Metadata._metadata;
  378. }).catch(() => {
  379. tengine.Metadata._metadata = new tengine.Metadata(null);
  380. return tengine.Metadata._metadata;
  381. });
  382. }
  383. constructor(data) {
  384. this._map = new Map();
  385. if (data) {
  386. const metadata = JSON.parse(data);
  387. for (const item of metadata) {
  388. if (item.name) {
  389. const version = item.version || 0;
  390. const name = item.name + ':' + version.toString();
  391. this._map.set(name, item);
  392. }
  393. }
  394. }
  395. }
  396. type(name, version) {
  397. let current = version;
  398. while (current > 0) {
  399. if (this._map.has(name + ':' + current.toString())) {
  400. break;
  401. }
  402. current--;
  403. }
  404. if (current >= 0) {
  405. const schema = this._map.get(name + ':' + current.toString());
  406. if (current !== version) {
  407. this._map.set(name + ':' + version.toString(), schema);
  408. }
  409. return schema;
  410. }
  411. return null;
  412. }
  413. };
  414. tengine.Reader = class {
  415. static open(stream) {
  416. if (stream.length > 4) {
  417. const buffer = stream.peek(2);
  418. if (buffer[0] < 4 && buffer[1] === 0) {
  419. return new tengine.Reader(stream);
  420. }
  421. }
  422. return null;
  423. }
  424. constructor(stream) {
  425. this._stream = stream;
  426. // https://github.com/OAID/Tengine/blob/tengine-lite/src/serializer/tm/tm2_format.h
  427. // https://github.com/OAID/Tengine/wiki/The-format-of-tmfile
  428. }
  429. _read() {
  430. if (this._stream) {
  431. const types = new Map();
  432. const register = (index, version, name, params) => {
  433. types.set(index.toString() + ':' + version.toString(), { name: name, params: params });
  434. };
  435. const operator = (index, version) => {
  436. let current = version;
  437. while (current >= 0) {
  438. if (types.has(index.toString() + ':' + current.toString())) {
  439. break;
  440. }
  441. current--;
  442. }
  443. if (current >= 0) {
  444. const schema = types.get(index.toString() + ':' + current.toString());
  445. if (current !== version) {
  446. types.set(index.toString() + ':' + version.toString(), schema);
  447. }
  448. return schema;
  449. }
  450. return null;
  451. };
  452. register( 0, 0, 'Accuracy', []);
  453. register( 1, 0, 'BatchNormalization', [ 'f', 'f', 'i' ]);
  454. register( 2, 0, 'BilinearResize', [ 'f', 'f', 'i' ]);
  455. register( 3, 0, 'Concat', [ 'i' ]);
  456. register( 4, 0, 'Const', []);
  457. register( 5, 0, 'Convolution', [ 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i' ]);
  458. register( 6, 0, 'Deconvolution', [ 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i' ]);
  459. register( 7, 0, 'DetectionOutput', [ 'i', 'i', 'i', 'f', 'f' ]);
  460. register( 8, 0, 'DropOut', []);
  461. register( 9, 0, 'Eltwise', [ 'i', 'i' ]);
  462. register(10, 0, 'Flatten', [ 'i' ]);
  463. register(11, 0, 'FullyConnected', [ 'i' ]);
  464. register(12, 0, 'INPUT', []);
  465. register(13, 0, 'LRN', [ 'i', 'f', 'f', 'i', 'f' ]);
  466. register(14, 0, 'Normalize', [ 'i', 'i' ]);
  467. register(15, 0, 'Permute', [ 'i', 'i', 'i', 'i', 'i' ]);
  468. register(16, 0, 'Pooling', [ 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i' ]);
  469. register(17, 0, 'Prelu', []);
  470. register(18, 0, 'PriorBox', [ 'f[]', 'f[]', 'f[]', 'f[]', 'i', 'i', 'i', 'i', 'i', 'f', 'f', 'f', 'i', 'i' ]);
  471. register(19, 0, 'Region', [ 'i', 'i', 'i', 'i', 'f', 'f', 'f[]' ]);
  472. register(20, 0, 'ReLU', [ 'f' ]);
  473. register(21, 0, 'ReLU6', []);
  474. register(22, 0, 'Reorg', [ 'i' ]);
  475. register(23, 0, 'Reshape', [ 'i', 'i', 'i', 'i', 'i', 'i' ]);
  476. // register(23, 0, 'Reshape', [ 'i', 'i', 'i[]' ]);
  477. register(24, 0, 'RoiPooling', [ 'i', 'i', 'f' ]);
  478. register(25, 0, 'RPN', [ 'f[]', 'f[]', 'i', 'i', 'i', 'i', 'i', 'f', 'anchors' ]);
  479. register(26, 0, 'Scale', [ 'i', 'i', 'i' ]);
  480. register(27, 0, 'Slice', [ 'i', 'i[]', 'i[]', 'i[]', 'i', 'i', 'i', 'i', 'i' ]);
  481. register(28, 0, 'SoftMax', [ 'i' ]);
  482. register(29, 0, 'Split', [ 'i', 'i', 'boolean', 'boolean', 'i[]' ]);
  483. register(30, 0, 'DetectionPostProcess', [ 'i', 'i', 'f', 'f', 'i', 'f[]' ]);
  484. register(31, 0, 'Gemm', [ 'f', 'f', 'i', 'i' ]);
  485. register(32, 0, 'Generic', [ 'i', 'i', 'string' ]);
  486. register(33, 0, 'Logistic', []);
  487. register(34, 0, 'LSTM', [ 'f', 'f', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i' ]);
  488. register(35, 0, 'RNN', [ 'f', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i' ]);
  489. register(36, 0, 'TanH', []);
  490. register(37, 0, 'Sigmoid', []);
  491. register(38, 0, 'Squeeze', [ 'i', 'i', 'i', 'i' ]);
  492. register(39, 0, 'FusedbnScaleRelu', []);
  493. register(40, 0, 'Pad', [ 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'f' ]);
  494. register(41, 0, 'StridedSlice', [ 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i' ]);
  495. register(42, 0, 'ArgMax', [ 'i' ]);
  496. register(43, 0, 'ArgMin', [ 'i' ]);
  497. register(44, 0, 'TopKV2', [ 'i', 'i' ]);
  498. register(45, 0, 'Reduction', [ 'i', 'i', 'i', 'i', 'i', 'i' ]);
  499. register(46, 0, 'Max', []);
  500. register(47, 0, 'Min', []);
  501. register(48, 0, 'GRU', [ 'f', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i' ]);
  502. register(49, 0, 'Addn', 'i');
  503. register(50, 0, 'SwapAxis', [ 'i', 'i' ]);
  504. register(51, 0, 'Upsample', [ 'f' ]);
  505. register(52, 0, 'SpaceToBatchND', [ 'i', 'i', 'i', 'i', 'i', 'i' ]);
  506. register(53, 0, 'BatchToSpaceND', [ 'i', 'i', 'i', 'i', 'i', 'i' ]);
  507. register(54, 0, 'Resize', [ 'f', 'f', 'i' ]);
  508. register(55, 0, 'ShuffleChannel', [ 'i' ]);
  509. register(56, 0, 'Crop', [ 'i', 'i', 'i', 'i', 'i', 'i', 'boolean', 'i', 'i' ]);
  510. register(57, 0, 'ROIAlign', [ 'i', 'i', 'f' ]);
  511. register(58, 0, 'Psroipooling', [ 'i', 'i', 'f', 'i' ]);
  512. register(59, 0, 'Unary', [ 'i' ]);
  513. register(60, 0, 'Expanddims', [ 'i' ]);
  514. register(61, 0, 'Bias', [ 'i' ]);
  515. register(62, 0, 'Noop', []);
  516. register(63, 0, 'Threshold', [ 'f' ]);
  517. register(64, 0, 'Hardsigmoid', [ 'f', 'f' ]);
  518. register(65, 0, 'Embed', [ 'f', 'f', 'f', 'f' ]);
  519. register(66, 0, 'InstanceNorm', [ 'f' ]);
  520. register(67, 0, 'MVN', [ 'i', 'i', 'f' ]);
  521. register(68, 0, 'Absval', []);
  522. register(69, 0, 'Cast', [ 'i', 'i' ]);
  523. register(70, 0, 'HardSwish', [ 'f', 'f' ]);
  524. register(71, 0, 'Interp', [ 'i', 'i', 'f', 'f', 'i' ]);
  525. register(72, 0, 'SELU', [ 'f', 'f' ]);
  526. register(73, 0, 'ELU', [ 'f' ]);
  527. register(74, 0, 'BroadMul', []);
  528. register(75, 0, 'Logical', [ 'i' ]);
  529. register(76, 0, 'Gather', [ 'i', 'i' ]);
  530. register(77, 0, 'Transpose', [ 'i[]' ]);
  531. register(78, 0, 'Comparison', [ 'i' ]);
  532. register(79, 0, 'SpaceToDepth', [ 'i' ]);
  533. register(80, 0, 'DepthToSpace', [ 'i' ]);
  534. register(81, 0, 'Reverse', []);
  535. register(82, 0, 'SparseToDense', [ 'i','i','i' ]);
  536. register(83, 0, 'Ceil', []);
  537. register(84, 0, 'SquaredDifference', []);
  538. register(85, 0, 'Round', []);
  539. register(86, 0, 'ZerosLike', []);
  540. register(87, 0, 'Clip', [ 'f','f' ]);
  541. register(88, 0, 'Unsqueeze', [ 'i[]' ]);
  542. register(89, 0, 'ReduceL2', [ 'i','i' ]);
  543. register(90, 0, 'Mean', []);
  544. register(91, 0, 'MatMul', []);
  545. register(92, 0, 'Expand', ['i[]']);
  546. register(93, 0, 'Scatter', ['i','boolean']);
  547. register(94, 0, 'Shape', []);
  548. register(95, 0, 'Where', []);
  549. register(96, 0, 'Tile', ['i','i']);
  550. register(97, 0, 'Mish', []);
  551. register(98, 0, 'L2Pool', []);
  552. register(99, 0, 'LogSoftmax', []);
  553. register(100, 0, 'ReLU1', []);
  554. register(101, 0, 'L2Normalization', []);
  555. register(102, 0, 'PackModel', ['i','i']);
  556. register(103, 0, 'Num', []);
  557. const buffer = this._stream.peek();
  558. const reader = new tengine.BinaryReader(buffer);
  559. this._majorVersion = reader.uint16();
  560. this._minorVersion = reader.uint16();
  561. if (this._majorVersion !== 2) {
  562. throw new tengine.Error("Unsupported format version 'v" + this._majorVersion.toString() + "." + this._minorVersion.toString() + "'.");
  563. }
  564. this._compileVersion = reader.uint16();
  565. reader.skip(2); // struct align
  566. reader.seek(reader.uint32()); // root table
  567. this._originalFormat = reader.int32();
  568. this._subFormat = reader.int32();
  569. this._graphs = [];
  570. const subgraphOffsets = reader.uint32s();
  571. for (const subgraphOffset of subgraphOffsets) {
  572. reader.seek(subgraphOffset);
  573. const subgraph = {};
  574. subgraph.id = reader.int32();
  575. subgraph.graphLayout = reader.int32();
  576. /*
  577. if (graphLayout == 0) {
  578. return "NCHW";
  579. }
  580. if (graphLayout == 1) {
  581. return "NHWC";
  582. }
  583. */
  584. subgraph.originalLayout = reader.int32();
  585. subgraph.inputs = reader.uint32s();
  586. subgraph.outputs = reader.uint32s();
  587. const nodeOffsets = reader.uint32s();
  588. const tensorOffsets = reader.uint32s();
  589. const bufferOffsets = reader.uint32s();
  590. subgraph.name = reader.string();
  591. subgraph.nodes = [];
  592. subgraph.tensors = [];
  593. this._graphs.push(subgraph);
  594. // nodes
  595. for (const nodeOffset of nodeOffsets) {
  596. reader.seek(nodeOffset);
  597. const node = {};
  598. node.id = reader.int32();
  599. node.inputs = reader.uint32s();
  600. node.outputs = reader.uint32s();
  601. const typeOffset = reader.int32();
  602. node.name = reader.string();
  603. const attributeOffsets = reader.uint32s();
  604. node.dynamicShape = reader.boolean();
  605. reader.seek(typeOffset);
  606. node.version = reader.int32();
  607. const index = reader.int32();
  608. const paramsOffset = reader.uint32();
  609. const schema = operator(index, node.version);
  610. node.type = schema ? schema.name : index.toString();
  611. const paramTypes = schema ? schema.params : [];
  612. node.params = [];
  613. if (paramsOffset) {
  614. reader.seek(paramsOffset);
  615. for (const paramType of paramTypes) {
  616. if (paramType !== 'boolean') {
  617. reader.align(4);
  618. }
  619. switch (paramType) {
  620. case 'i':
  621. node.params.push(reader.int32());
  622. break;
  623. case 'f':
  624. node.params.push(reader.float32());
  625. break;
  626. case 'i[]':
  627. node.params.push(reader.int32s());
  628. break;
  629. case 'f[]':
  630. node.params.push(reader.float32s());
  631. break;
  632. case 'boolean':
  633. node.params.push(reader.boolean());
  634. break;
  635. case 'string':
  636. node.params.push(reader.string());
  637. break;
  638. case 'anchors':
  639. node.params.push(reader.anchors(4));
  640. break;
  641. default:
  642. throw new tengine.Error("Unsupported param type '" + paramType + "' in '" + node.type + "'.");
  643. }
  644. }
  645. }
  646. if (node.type === 'Slice') {
  647. node.params[6] = (this._originalFormat == 5) ? node.params[6] : 0;
  648. }
  649. node.attributes = attributeOffsets.map((attributeOffset) => {
  650. reader.seek(attributeOffset);
  651. const name = reader.string();
  652. const value = reader.string();
  653. const type = reader.int32();
  654. return { name: name, value: value, type: type };
  655. });
  656. subgraph.nodes.push(node);
  657. }
  658. // buffers
  659. const buffers = bufferOffsets.map((bufferOffset) => {
  660. reader.seek(bufferOffset);
  661. const size = reader.uint32();
  662. const offset = reader.int32();
  663. if (offset !== 0) {
  664. reader.seek(offset);
  665. return reader.read(size);
  666. }
  667. return null;
  668. });
  669. // tensors
  670. subgraph.tensors = tensorOffsets.map((tensorOffset) => {
  671. reader.seek(tensorOffset);
  672. const tensor = {};
  673. tensor.id = reader.int32();
  674. tensor.buffer = buffers[reader.int32()];
  675. tensor.dims = reader.int32s();
  676. tensor.name = reader.string();
  677. const quantparamsOffset = reader.int32();
  678. tensor.layout = reader.int32();
  679. tensor.type = reader.int32(); // ar = 1, const = 2, input = 3, vdep, unknown
  680. tensor.dataType = reader.int32();
  681. if (quantparamsOffset) {
  682. reader.seek(quantparamsOffset);
  683. tensor.quantparams = {
  684. zeroPoint: reader.int32(),
  685. scale: reader.float32(),
  686. width: reader.int32()
  687. };
  688. }
  689. return tensor;
  690. });
  691. for (const node of subgraph.nodes) {
  692. if (node.type === 'Convolution') {
  693. switch (subgraph.graphLayout) {
  694. case 0: // NCHW
  695. node.params[6] = subgraph.tensors[node.inputs[1]].dims[1];
  696. break;
  697. case 1: // NHWC
  698. node.params[6] = subgraph.tensors[node.inputs[1]].dims[3];
  699. break;
  700. default:
  701. throw new tengine.Error("Unsupported 'Convolution' layout '" + subgraph.graphLayout + "'.");
  702. }
  703. }
  704. }
  705. }
  706. delete this._stream;
  707. }
  708. }
  709. get version() {
  710. this._read();
  711. return this._majorVersion + '.' + this._minorVersion;
  712. }
  713. get source() {
  714. this._read();
  715. switch (this._originalFormat) {
  716. case 0: return '';
  717. case 1: return 'Tengine';
  718. case 2: return 'Caffe';
  719. case 3: return 'ONNX';
  720. case 4: return 'MXNet';
  721. case 5: return 'TensorFlow';
  722. case 6: return 'TensorFlow Lite';
  723. case 7: return 'Darknet';
  724. case 8: return 'DLA v' + this._subFormat;
  725. case 9: return 'ncnn';
  726. case 10: return 'MegEngine';
  727. case 11: return 'OneFlow';
  728. case 12: return 'Horizon';
  729. case 13: return 'Bitman';
  730. default: throw new tengine.Error("Unsupported source '" + this._originalFormat.toString() + "'.");
  731. }
  732. }
  733. get graphs() {
  734. this._read();
  735. return this._graphs;
  736. }
  737. };
  738. tengine.BinaryReader = class extends base.BinaryReader {
  739. string() {
  740. const position = this.uint32();
  741. let content = '';
  742. if (position) {
  743. const next = this._position;
  744. this.seek(position);
  745. const size = this.uint32();
  746. this.seek(this.uint32());
  747. for(let i = 0; i < size - 1; i++) {
  748. content += String.fromCharCode(this._buffer[this._position++]);
  749. }
  750. this.seek(next);
  751. }
  752. return content;
  753. }
  754. uint32s() {
  755. const values = [];
  756. const offset = this.uint32();
  757. if (offset) {
  758. const next = this.position;
  759. this.seek(offset);
  760. const count = this.uint32();
  761. for (let i = 0; i < count; i++) {
  762. values.push(this.uint32());
  763. }
  764. this.seek(next);
  765. }
  766. return values;
  767. }
  768. int32s() {
  769. const values = [];
  770. const offset = this.uint32();
  771. if (offset) {
  772. const next = this.position;
  773. this.seek(offset);
  774. const count = this.uint32();
  775. for (let i = 0; i < count; i++) {
  776. values.push(this.int32());
  777. }
  778. this.seek(next);
  779. }
  780. return values;
  781. }
  782. float32s() {
  783. const values = [];
  784. const offset = this.uint32();
  785. if (offset) {
  786. const next = this.position;
  787. this.seek(offset);
  788. const count = this.uint32();
  789. for (let i = 0; i < count; i++) {
  790. values.push(this.float32());
  791. }
  792. this.seek(next);
  793. }
  794. return values;
  795. }
  796. anchors(length) {
  797. const arrays = [];
  798. const offset = this.uint32();
  799. if (offset) {
  800. const next = this._position;
  801. this.seek(offset);
  802. const count = this.uint32();
  803. for (let i = 0; i < count; i++) {
  804. const array = [];
  805. for (let j = 0; j < length; j++) {
  806. array.push(this.float32());
  807. }
  808. arrays.push(array);
  809. }
  810. this.seek(next);
  811. }
  812. return arrays;
  813. }
  814. };
  815. tengine.Error = class extends Error {
  816. constructor(message) {
  817. super(message);
  818. this.name = 'Error loading Tengine model.';
  819. }
  820. };
  821. if (typeof module !== 'undefined' && typeof module.exports === 'object') {
  822. module.exports.ModelFactory = tengine.ModelFactory;
  823. }