tengine.js 32 KB

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