kmodel.js 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249
  1. import * as base from './base.js';
  2. const kmodel = {};
  3. kmodel.ModelFactory = class {
  4. async match(context) {
  5. const reader = kmodel.Reader.open(context.stream);
  6. if (reader) {
  7. return context.set('kmodel', reader);
  8. }
  9. return null;
  10. }
  11. async open(context) {
  12. const target = context.value;
  13. target.read();
  14. return new kmodel.Model(target);
  15. }
  16. };
  17. kmodel.Model = class {
  18. constructor(model) {
  19. this.format = `kmodel v${model.version}`;
  20. this.modules = model.modules.map((module) => new kmodel.Graph(module));
  21. }
  22. };
  23. kmodel.Graph = class {
  24. constructor(module) {
  25. this.name = module.name || '';
  26. this.description = module.type || '';
  27. this.inputs = [];
  28. this.outputs = [];
  29. this.nodes = [];
  30. const scopes = new Map();
  31. let index = 0;
  32. const values = new Map();
  33. const value = (arg) => {
  34. const name = arg.name;
  35. const type = arg.shape ? new kmodel.TensorType(arg.datatype || '?', arg.shape) : null;
  36. if (arg.data) {
  37. const tensor = arg.data ? new kmodel.Tensor(type, arg.data) : null;
  38. return new kmodel.Value(name, type || null, tensor);
  39. }
  40. if (!values.has(name)) {
  41. values.set(name, new kmodel.Value(name, type || null, null));
  42. } if ((type && !type.equals(values.get(name).type))) {
  43. return new kmodel.Value(name, type);
  44. }
  45. return values.get(name);
  46. };
  47. for (const layer of module.layers) {
  48. for (const input of layer.inputs || []) {
  49. for (const arg of input.value) {
  50. arg.name = scopes.has(arg.name) ? scopes.get(arg.name) : arg.name;
  51. }
  52. }
  53. for (const output of layer.outputs || []) {
  54. for (const arg of output.value) {
  55. const name = scopes.has(arg.name) ? `${arg.name}#${index}` : arg.name;
  56. scopes.set(arg.name, name); // custom argument id
  57. arg.name = name;
  58. if (arg.name && arg.shape && !arg.data) {
  59. value(arg);
  60. }
  61. }
  62. }
  63. index++;
  64. }
  65. for (const layer of module.layers) {
  66. for (const output of layer.outputs || []) {
  67. for (const arg of output.value) {
  68. if (arg.name && arg.shape && !arg.data) {
  69. value(arg);
  70. }
  71. }
  72. }
  73. }
  74. for (const layer of module.layers) {
  75. for (const input of layer.inputs || []) {
  76. for (const arg of input.value) {
  77. if (arg.name && arg.shape && !arg.data) {
  78. value(arg);
  79. }
  80. }
  81. }
  82. }
  83. for (const layer of module.layers) {
  84. switch (layer.type.name) {
  85. case 'INPUT':
  86. case 'input': {
  87. for (const input of layer.outputs) {
  88. const values = input.value.map((arg) => value(arg));
  89. const argument = new kmodel.Argument('input', values);
  90. this.inputs.push(argument);
  91. }
  92. break;
  93. }
  94. case 'OUTPUT':
  95. case 'output': {
  96. for (const output of layer.inputs) {
  97. const values = output.value.map((arg) => value(arg));
  98. const argument = new kmodel.Argument(output.name, values);
  99. this.outputs.push(argument);
  100. }
  101. break;
  102. }
  103. default: {
  104. const node = new kmodel.Node(layer, value);
  105. this.nodes.push(node);
  106. break;
  107. }
  108. }
  109. }
  110. }
  111. };
  112. kmodel.Argument = class {
  113. constructor(name, value) {
  114. this.name = name;
  115. this.value = value;
  116. }
  117. };
  118. kmodel.Value = class {
  119. constructor(name, type, initializer) {
  120. if (typeof name !== 'string') {
  121. throw new kmodel.Error(`Invalid value identifier '${JSON.stringify(name)}'.`);
  122. }
  123. this.name = name;
  124. this.type = !type && initializer ? initializer.type : type;
  125. this.initializer = initializer;
  126. }
  127. };
  128. kmodel.TensorType = class {
  129. constructor(dataType, shape) {
  130. this.dataType = dataType;
  131. this.shape = new kmodel.TensorShape(shape);
  132. }
  133. equals(obj) {
  134. return obj && this.dataType === obj.dataType && this.shape && this.shape.equals(obj.shape);
  135. }
  136. toString() {
  137. return this.dataType + this.shape.toString();
  138. }
  139. };
  140. kmodel.TensorShape = class {
  141. constructor(dimensions) {
  142. this.dimensions = dimensions;
  143. }
  144. equals(obj) {
  145. if (obj && Array.isArray(obj.dimensions) && Array.isArray(this.dimensions)) {
  146. if (this.dimensions.length === obj.dimensions.length) {
  147. return obj.dimensions.every((value, index) => this.dimensions[index] === value);
  148. }
  149. if (obj.dimensions.every((dim) => Number.isInteger(dim)) && this.dimensions.every((dim) => Number.isInteger(dim))) {
  150. const a = obj.dimensions.reduce((a, b) => a * b, 1);
  151. const b = this.dimensions.reduce((a, b) => a * b, 1);
  152. return a === b;
  153. }
  154. }
  155. return false;
  156. }
  157. toString() {
  158. if (this.dimensions && Array.isArray(this.dimensions) && this.dimensions.length > 0) {
  159. return `[${this.dimensions.map((dim) => dim ? dim.toString() : '?').join(',')}]`;
  160. }
  161. return '';
  162. }
  163. };
  164. kmodel.Tensor = class {
  165. constructor(type, data) {
  166. this.type = type;
  167. this.values = data;
  168. }
  169. };
  170. kmodel.Node = class {
  171. constructor(layer, value) {
  172. this.identifier = layer.location === undefined ? layer.location : layer.location.toString();
  173. this.name = '';
  174. this.type = layer.type;
  175. this.inputs = [];
  176. this.outputs = [];
  177. this.chain = [];
  178. this.attributes = [];
  179. this.chain = [];
  180. for (const [name, value] of Object.entries(layer)) {
  181. if (name === 'type' || name === 'location' || name === 'inputs' || name === 'outputs' || name === 'chain') {
  182. continue;
  183. }
  184. const attribute = new kmodel.Argument(name, value);
  185. this.attributes.push(attribute);
  186. }
  187. for (const input of layer.inputs || []) {
  188. const values = input.value.map((arg) => value(arg));
  189. const argument = new kmodel.Argument(input.name, values);
  190. this.inputs.push(argument);
  191. }
  192. for (const output of layer.outputs || []) {
  193. const values = output.value.map((arg) => value(arg));
  194. const argument = new kmodel.Argument(output.name, values);
  195. this.outputs.push(argument);
  196. }
  197. for (const chain of layer.chain || []) {
  198. const node = new kmodel.Node(chain, value);
  199. this.chain.push(node);
  200. }
  201. }
  202. };
  203. kmodel.Reader = class {
  204. static open(stream) {
  205. if (stream && stream.length >= 4) {
  206. const length = Math.min(8, stream.length);
  207. const buffer = stream.peek(length);
  208. if ([0x03, 0x00, 0x00, 0x00].every((value, index) => value === buffer[index])) {
  209. return new kmodel.Reader(stream, 3);
  210. }
  211. if ([0x4C, 0x44, 0x4D, 0x4B].every((value, index) => value === buffer[index]) && buffer.length >= 8) {
  212. const reader = base.BinaryReader.open(buffer);
  213. reader.skip(4);
  214. const version = reader.uint32();
  215. return new kmodel.Reader(stream, version);
  216. }
  217. }
  218. return null;
  219. }
  220. constructor(stream, version) {
  221. this.stream = stream;
  222. this.version = version;
  223. this.modules = [];
  224. }
  225. read() {
  226. if (this.version < 3 || this.version > 7) {
  227. throw new kmodel.Error(`Unsupported model version '${this.version}'.`);
  228. }
  229. const types = new Map();
  230. const register = (type, name, category, callback) => {
  231. types.set(type, { type: { name, category: category || '' }, callback });
  232. };
  233. switch (this.version) {
  234. case 3: {
  235. const reader = new kmodel.BinaryReader.v3(this.stream);
  236. const model_header = reader.kpu_model_header_t();
  237. const layers = new Array(model_header.layers_length);
  238. const outputs = new Array(model_header.output_count);
  239. for (let i = 0; i < model_header.output_count; i++) {
  240. outputs[i] = reader.kpu_model_output_t(`output${i > 0 ? i.toString() : ''}`);
  241. }
  242. for (let i = 0; i < layers.length; i++) {
  243. layers[i] = reader.kpu_model_layer_header_t();
  244. layers[i].location = i;
  245. }
  246. let offset = reader.position;
  247. for (const layer of layers) {
  248. layer.offset = offset;
  249. offset += layer.body_size;
  250. }
  251. /* eslint-disable space-in-parens */
  252. register( -1, 'DUMMY');
  253. register( 0, 'INVALID');
  254. register( 1, 'ADD');
  255. register( 2, 'QUANTIZED_ADD');
  256. register( 3, 'GLOBAL_MAX_POOL2D', 'Pool');
  257. register( 4, 'QUANTIZED_GLOBAL_MAX_POOL2D', 'Pool');
  258. register( 5, 'GLOBAL_AVERAGE_POOL2D', 'Pool', (layer, reader) => {
  259. layer.flags = reader.uint32();
  260. layer.inputs = [reader.parameter('input')];
  261. layer.outputs = [reader.parameter('output')];
  262. layer.kernel_size = reader.uint32();
  263. layer.channels = reader.uint32();
  264. });
  265. register( 6, 'QUANTIZED_GLOBAL_AVERAGE_POOL2D', 'Pool');
  266. register( 7, 'MAX_POOL2D', 'Pool');
  267. register( 8, 'QUANTIZED_MAX_POOL2D', 'Pool', (layer, reader) => {
  268. layer.flags = reader.uint32();
  269. layer.inputs = [reader.parameter('input')];
  270. layer.outputs = [reader.parameter('output')];
  271. layer.inputs[0].value[0].shape = [reader.uint32(), reader.uint32(), reader.uint32()];
  272. layer.outputs[0].value[0].shape = [reader.uint32(), reader.uint32(), reader.uint32()];
  273. layer.kernel = [reader.uint32(), reader.uint32()];
  274. layer.stride = [reader.uint32(), reader.uint32()];
  275. layer.padding = [reader.uint32(), reader.uint32()];
  276. });
  277. register( 9, 'AVERAGE_POOL2D', 'Pool');
  278. register( 10, 'QUANTIZED_AVERAGE_POOL2D', 'Pool');
  279. register( 11, 'QUANTIZE', '', (layer, reader) => {
  280. layer.flags = reader.uint32();
  281. layer.inputs = [reader.parameter('input')];
  282. layer.outputs = [reader.parameter('output')];
  283. layer.count = reader.uint32();
  284. layer.scale = reader.float32();
  285. layer.bias = reader.float32();
  286. });
  287. register( 12, 'DEQUANTIZE', '', (layer, reader) => {
  288. layer.flags = reader.uint32();
  289. layer.inputs = [reader.parameter('input')];
  290. layer.outputs = [reader.parameter('output')];
  291. layer.count = reader.uint32();
  292. layer.scale = reader.float32();
  293. layer.bias = reader.float32();
  294. });
  295. register( 13, 'REQUANTIZE', '', (layer, reader) => {
  296. layer.flags = reader.uint32();
  297. layer.inputs = [reader.parameter('input')];
  298. layer.outputs = [reader.parameter('output')];
  299. layer.count = reader.uint32();
  300. layer.table = reader.read(256);
  301. });
  302. register( 14, 'L2_NORMALIZATION', 'Normalization');
  303. register( 15, 'SOFTMAX', 'Activation', (layer, reader) => {
  304. layer.flags = reader.uint32();
  305. layer.inputs = [reader.parameter('input')];
  306. layer.outputs = [reader.parameter('output')];
  307. layer.channels = reader.uint32();
  308. });
  309. register( 16, 'CONCAT', 'Tensor', (layer, reader) => {
  310. layer.flags = reader.uint32();
  311. layer.outputs = [reader.parameter('output')];
  312. layer.inputs_mem = new Array(reader.uint32());
  313. for (let i = 0; i < layer.inputs_mem.length; i++) {
  314. layer.inputs_mem[i] = {
  315. start: reader.uint32(),
  316. end: reader.uint32()
  317. };
  318. }
  319. });
  320. register( 17, 'QUANTIZED_CONCAT', 'Tensor', (layer, reader) => {
  321. layer.flags = reader.uint32();
  322. layer.outputs = [reader.parameter('output')];
  323. layer.inputs_mem = new Array(reader.uint32());
  324. for (let i = 0; i < layer.inputs_mem.length; i++) {
  325. layer.inputs_mem[i] = {
  326. start: reader.uint32(),
  327. end: reader.uint32()
  328. };
  329. }
  330. });
  331. register( 18, 'FULLY_CONNECTED', 'Layer', (layer, reader) => {
  332. layer.flags = reader.uint32();
  333. layer.inputs = [reader.parameter('input')];
  334. layer.outputs = [reader.parameter('output')];
  335. layer.in_channels = reader.uint32();
  336. layer.out_channels = reader.uint32();
  337. const act = reader.uint32();
  338. const activations = [
  339. { name: 'LINEAR', category: 'Activation' },
  340. { name: 'RELU', category: 'Activation' },
  341. { name: 'RELU6', category: 'Activation' },
  342. ];
  343. if (act !== 0) {
  344. if (act > activations.length) {
  345. throw new kmodel.Error(`Unsupported FULLY_CONNECTED activation '${act}'.`);
  346. }
  347. layer.chain = [{ type: activations[act] }];
  348. }
  349. layer.inputs.push({ name: 'weights', value: [{ name: '', datatype: 'float32', shape: [layer.in_channels, layer.out_channels], data: reader.read(4 * layer.in_channels * layer.out_channels) }] });
  350. layer.inputs.push({ name: 'bias', value: [{ name: '', datatype: 'float32', shape: [layer.out_channels], data: reader.read(4 * layer.out_channels) }] });
  351. });
  352. register( 19, 'QUANTIZED_FULLY_CONNECTED', 'Layer');
  353. register( 20, 'TENSORFLOW_FLATTEN', 'Shape', (layer, reader) => {
  354. layer.flags = reader.uint32();
  355. layer.inputs = [reader.parameter('input')];
  356. layer.outputs = [reader.parameter('output')];
  357. const shape = [reader.uint32(), reader.uint32(), reader.uint32()];
  358. layer.inputs[0].value[0].shape = shape;
  359. layer.outputs[0].value[0].shape = shape;
  360. });
  361. register( 21, 'QUANTIZED_TENSORFLOW_FLATTEN', 'Shape', (layer, reader) => {
  362. layer.flags = reader.uint32();
  363. layer.inputs = [reader.parameter('input')];
  364. layer.outputs = [reader.parameter('output')];
  365. const shape = [reader.uint32(), reader.uint32(), reader.uint32()];
  366. layer.inputs[0].value[0].shape = shape;
  367. layer.outputs[0].value[0].shape = shape;
  368. });
  369. register( 22, 'RESIZE_NEAREST_NEIGHBOR', '', (layer, reader) => {
  370. layer.flags = reader.uint32();
  371. layer.inputs = [reader.parameter('input')];
  372. layer.outputs = [reader.parameter('output')];
  373. layer.inputs[0].value[0].shape = [reader.uint32(), reader.uint32(), reader.uint32()];
  374. layer.out_width = reader.uint32();
  375. layer.out_height = reader.uint32();
  376. layer.align_corners = reader.uint32();
  377. });
  378. register( 23, 'QUANTIZED_RESIZE_NEAREST_NEIGHBOR', '', (layer, reader) => {
  379. layer.flags = reader.uint32();
  380. layer.inputs = [reader.parameter('input')];
  381. layer.outputs = [reader.parameter('output')];
  382. layer.inputs[0].value[0].shape = [reader.uint32(), reader.uint32(), reader.uint32()];
  383. layer.out_width = reader.uint32();
  384. layer.out_height = reader.uint32();
  385. layer.align_corners = reader.uint32();
  386. });
  387. register( 1000, 'CONV', 'Layer');
  388. register( 1001, 'DWCONV', 'Layer');
  389. register( 1002, 'QUANTIZED_RESHAPE', 'Shape');
  390. register( 1003, 'RESHAPE', 'Shape');
  391. register(10240, 'K210_CONV', 'Layer', (layer, reader) => {
  392. layer.flags = reader.uint32();
  393. layer.outputs = [reader.parameter('output')];
  394. const layer_offset = reader.uint32();
  395. const weights_offset = reader.uint32();
  396. const bn_offset = reader.uint32();
  397. const act_offset = reader.uint32();
  398. reader.seek(layer_offset);
  399. layer.interrupt_enabe = reader.uint64_bits({ int_en: 0, ram_flag: 1, full_add: 2, depth_wise_layer: 3 });
  400. layer.inputs = [reader.parameter('input', 'kpu')];
  401. const outputs = [reader.parameter('output', 'kpu')];
  402. layer.outputs[0].value.push(outputs[0].value[0]);
  403. // layer.outputs = layer.flags & 1 ? layer.outputs : outputs;
  404. layer.image_channel_num = reader.uint64_bits({ i_ch_num: 0, o_ch_num: 32, o_ch_num_coef: 48 });
  405. layer.image_size = reader.uint64_bits({ i_row_wid: 0, i_col_high: 10, o_row_wid: 32, o_col_high : 42 });
  406. layer.kernel_pool_type_cfg = reader.uint64_bits({ kernel_type: 0, pad_type: 3, pool_type: 4, first_stride: 8, bypass_conv: 9, load_para: 10, dma_burst_size: 16, pad_value: 24, bwsx_base_addr: 32 });
  407. layer.kernel_load_cfg = reader.uint64_bits({ load_coor: 0, load_time: 1, para_size: 15, para_start_addr: 32 });
  408. layer.kernel_offset = reader.uint64_bits({ coef_column_offset: 0, coef_row_offset: 4 });
  409. layer.kernel_calc_type_cfg = reader.uint64_bits({ channel_switch_addr: 0, row_switch_addr: 16, coef_size: 20, coef_group: 28, load_act: 31, active_addr: 32 });
  410. layer.write_back_cfg = reader.uint64_bits({ wb_channel_switch_addr: 0, wb_row_switch_addr: 16, wb_group: 20 });
  411. layer.conv_value = reader.uint64_bits({ shr_w: 0, shr_x: 4, arg_w: 8, arg_x: 32 });
  412. layer.conv_value2 = reader.uint64_bits({ arg_add: 0 });
  413. layer.dma_parameter = reader.uint64_bits({ send_data_out: 0, channel_byte_num: 16, dma_total_byte: 32 });
  414. layer.chain = [];
  415. const ic = layer.image_channel_num.i_ch_num + 1;
  416. const oc = layer.image_channel_num.o_ch_num + 1;
  417. layer.outputs[0].value[0].shape = [layer.image_size.o_row_wid + 1, layer.image_size.o_col_high + 1, oc];
  418. const filter = [1, 3][layer.kernel_pool_type_cfg.kernel_type];
  419. const weights_shape = layer.interrupt_enabe.depth_wise_layer ? [oc, filter, filter] : [ic, oc, filter, filter];
  420. const weights_size = weights_shape.reduce((a, b) => a * b);
  421. reader.seek(bn_offset);
  422. const batch_norm = {
  423. type: { name: 'BATCH_NORM', category: 'Normalization' },
  424. weights: []
  425. };
  426. batch_norm.weights = new Array(oc);
  427. for (let i = 0; i < oc; i++) {
  428. batch_norm.weights[i] = reader.uint64_bits({ norm_mul: 0, norm_add: 24, norm_shift: 56, reserved: 60 });
  429. delete batch_norm.weights[i].reserved;
  430. }
  431. layer.chain.push(batch_norm);
  432. reader.seek(act_offset);
  433. const activation = {};
  434. activation.type = { name: 'ACTIVATION', category: 'Activation' };
  435. activation.activate_para = new Array(16);
  436. for (let i = 0; i < 16; i++) {
  437. activation.activate_para[i] = reader.uint64_bits({ shift_number: 0, y_mul: 8, x_start: 24, reserved: 60 });
  438. delete activation.activate_para[i].reserved;
  439. }
  440. for (let i = 0; i < 16; i++) {
  441. activation.activate_para[i].bias = reader.int8();
  442. }
  443. layer.chain.push(activation);
  444. reader.seek(weights_offset);
  445. layer.inputs.push({
  446. name: 'weights',
  447. value: [{
  448. name: '',
  449. datatype: 'uint8',
  450. shape: weights_shape,
  451. data: reader.read(weights_size)
  452. }]
  453. });
  454. delete layer.kernel_pool_type_cfg.bwsx_base_addr;
  455. delete layer.kernel_calc_type_cfg.active_addr;
  456. delete layer.kernel_load_cfg.para_start_addr;
  457. });
  458. register(10241, 'K210_ADD_PADDING', '', (layer, reader) => {
  459. layer.flags = reader.uint32();
  460. layer.inputs = [reader.parameter('input')];
  461. layer.outputs = [reader.parameter('output', 'kpu')];
  462. layer.channels = reader.uint32();
  463. });
  464. register(10242, 'K210_REMOVE_PADDING', '', (layer, reader) => {
  465. layer.flags = reader.uint32();
  466. layer.inputs = [reader.parameter('input')];
  467. layer.outputs = [reader.parameter('output')];
  468. layer.channels = reader.uint32();
  469. });
  470. register(10243, 'K210_UPLOAD', '', (layer, reader) => {
  471. layer.flags = reader.uint32();
  472. layer.inputs = [reader.parameter('input')];
  473. layer.outputs = [reader.parameter('output', 'kpu')];
  474. const shape = [reader.uint32(), reader.uint32(), reader.uint32()];
  475. layer.inputs[0].value[0].shape = shape;
  476. layer.outputs[0].value[0].shape = shape;
  477. });
  478. /* eslint-enable space-in-parens */
  479. for (const layer of layers) {
  480. const type = types.get(layer.type);
  481. if (!type) {
  482. throw new kmodel.Error(`Unsupported version '${this.version}' layer type '${layer.type}'.`);
  483. }
  484. if (!type.callback) {
  485. throw new kmodel.Error(`Unsupported version '${this.version}' layer '${type.type.name}'.`);
  486. }
  487. layer.type = type.type;
  488. reader.seek(layer.offset);
  489. type.callback(layer, reader);
  490. delete layer.offset;
  491. delete layer.body_size;
  492. }
  493. if (layers.length > 0) {
  494. layers.unshift({
  495. type: { name: 'input' },
  496. outputs: [layers[0].inputs[0]]
  497. });
  498. }
  499. for (const output of outputs) {
  500. layers.push({
  501. type: { name: 'output' },
  502. inputs: output.address
  503. });
  504. }
  505. this.modules.push({
  506. name: '',
  507. layers
  508. });
  509. break;
  510. }
  511. case 4: {
  512. const reader = new kmodel.BinaryReader.v4(this.stream);
  513. const model_header = {
  514. flags: reader.uint32(),
  515. target: reader.uint32(), // 0=CPU, 1=K210
  516. constants: reader.uint32(),
  517. main_mem: reader.uint32(),
  518. nodes: reader.uint32(),
  519. inputs: reader.uint32(),
  520. outputs: reader.uint32(),
  521. reserved0: reader.uint32(),
  522. };
  523. const inputs = new Array(model_header.inputs);
  524. for (let i = 0; i < inputs.length; i++) {
  525. inputs[i] = reader.parameter(`input${i === 0 ? '' : (i + 1)}`);
  526. }
  527. for (let i = 0; i < inputs.length; i++) {
  528. inputs[i].value[0].shape = reader.runtime_shape_t();
  529. }
  530. const outputs = new Array(model_header.outputs);
  531. for (let i = 0; i < outputs.length; i++) {
  532. outputs[i] = reader.parameter(`output${i === 0 ? '' : (i + 1)}`);
  533. }
  534. reader.constants(model_header.constants);
  535. const layers = new Array(model_header.nodes);
  536. for (let i = 0; i < layers.length; i++) {
  537. layers[i] = {
  538. location: i,
  539. opcode: reader.uint32(),
  540. body_size: reader.uint32()
  541. };
  542. }
  543. let offset = reader.position;
  544. for (const layer of layers) {
  545. layer.offset = offset;
  546. offset += layer.body_size;
  547. }
  548. /* eslint-disable space-in-parens */
  549. register( 0x00, 'binary', '', (layer, reader) => {
  550. layer.inputs = [
  551. reader.parameter('a'),
  552. reader.parameter('b')
  553. ];
  554. layer.outputs = [reader.parameter('outputs')];
  555. layer.binary_op = reader.binary_op_t();
  556. layer.inputs[0].value[0].shape = reader.runtime_shape_t();
  557. layer.inputs[1].value[0].shape = reader.runtime_shape_t();
  558. layer.outputs[0].value[0].shape = reader.runtime_shape_t();
  559. layer.fused_activation = [reader.float32(), reader.float32()];
  560. });
  561. register( 0x01, 'concat', 'Tensor', (layer, reader) => {
  562. layer.outputs = [reader.parameter('output')];
  563. layer.inner_size = reader.uint32();
  564. layer.outer_size = reader.uint32();
  565. const inputs_count = reader.uint32();
  566. layer.inputs = [{ name: 'inputs', value: [] }];
  567. for (let i = 0; i < inputs_count; i++) {
  568. layer.inputs[0].value[i] = reader.argument();
  569. }
  570. layer.dims = new Array(inputs_count);
  571. for (let i = 0; i < inputs_count; i++) {
  572. layer.dims[i] = reader.int32();
  573. }
  574. });
  575. register( 0x02, 'conv2d', 'Layer', (layer, reader) => {
  576. layer.inputs = [reader.parameter('input')];
  577. layer.outputs = [reader.parameter('output')];
  578. layer.inputs[0].value[0].shape = reader.runtime_shape_t();
  579. layer.groups = reader.int32();
  580. layer.out_channels = reader.int32();
  581. layer.padding_h = reader.padding();
  582. layer.padding_w = reader.padding();
  583. layer.filter_h = reader.int32();
  584. layer.filter_w = reader.int32();
  585. layer.stride_h = reader.int32();
  586. layer.stride_w = reader.int32();
  587. layer.dilation_h = reader.int32();
  588. layer.dilation_w = reader.int32();
  589. layer.fused_activation = [reader.float32(), reader.float32()];
  590. const weights_shape = [layer.out_channels, layer.inputs[0].value[0].shape[1] / layer.groups, layer.filter_h, layer.filter_w];
  591. const weights_size = 4 * weights_shape.reduce((a, b) => a * b);
  592. layer.inputs.push({
  593. name: 'weights',
  594. value: [{
  595. name: '',
  596. datatype: 'float32',
  597. shape: weights_shape,
  598. data: reader.read(weights_size)
  599. }]
  600. });
  601. const bias_shape = [layer.out_channels];
  602. const bias_size = 4 * layer.out_channels;
  603. layer.inputs.push({
  604. name: 'bias',
  605. value: [{
  606. name: '',
  607. datatype: 'float32',
  608. shape: bias_shape,
  609. data: reader.read(bias_size)
  610. }]
  611. });
  612. });
  613. register( 0x03, 'dequantize', '', (layer, reader) => {
  614. layer.inputs = [reader.parameter('input')];
  615. layer.outputs = [reader.parameter('output')];
  616. layer.zero_point = reader.int32();
  617. layer.scale = reader.float32();
  618. });
  619. register( 0x04, 'matmul', '', (layer, reader) => {
  620. layer.inputs = [
  621. reader.parameter('a'),
  622. reader.parameter('b'),
  623. ];
  624. layer.outputs = [reader.parameter('output')];
  625. layer.a_rows = reader.int32();
  626. layer.a_cols = reader.int32();
  627. layer.b_cols = reader.int32();
  628. layer.inputs[1].value[0].shape = [layer.a_cols, layer.b_cols];
  629. layer.fused_activation = [reader.float32(), reader.float32()];
  630. const bias = reader.read(4 * layer.b_cols);
  631. if (!bias.every((value) => value === 0)) {
  632. layer.inputs.push({
  633. name: 'bias',
  634. value: [{ name: '', datatype: 'float32', shape: [layer.b_cols], data: bias }]
  635. });
  636. }
  637. });
  638. register( 0x05, 'pad', 'Shape', (layer, reader) => {
  639. layer.inputs = [reader.parameter('input')];
  640. layer.outputs = [reader.parameter('output')];
  641. layer.inputs[0].value[0].shape = reader.runtime_shape_t();
  642. layer.paddings = reader.runtime_paddings_t();
  643. layer.pad_value = reader.scalar();
  644. });
  645. register( 0x06, 'quantize', '', (layer, reader) => {
  646. layer.inputs = [reader.parameter('input')];
  647. layer.outputs = [reader.parameter('output')];
  648. layer.zero_point = reader.int32();
  649. layer.scale = reader.float32();
  650. });
  651. register( 0x07, 'reduce', '', (layer, reader) => {
  652. layer.inputs = [reader.parameter('input')];
  653. layer.outputs = [reader.parameter('output')];
  654. layer.reduce_op = reader.reduce_op_t();
  655. layer.inputs[0].value[0].shape = reader.runtime_shape_t();
  656. layer.outputs[0].value[0].shape = reader.runtime_shape_t();
  657. layer.init_value = reader.float32();
  658. });
  659. register( 0x08, 'reduce_window2d', '', (layer, reader) => {
  660. layer.inputs = [reader.parameter('input')];
  661. layer.outputs = [reader.parameter('output')];
  662. layer.reduce_op = reader.reduce_op_t();
  663. layer.inputs[0].value[0].shape = reader.runtime_shape_t();
  664. layer.padding_h = reader.padding();
  665. layer.padding_w = reader.padding();
  666. layer.filter_h = reader.int32();
  667. layer.filter_w = reader.int32();
  668. layer.stride_h = reader.int32();
  669. layer.stride_w = reader.int32();
  670. layer.dilation_h = reader.int32();
  671. layer.dilation_w = reader.int32();
  672. layer.init_value = reader.float32();
  673. layer.fused_activation = [reader.float32(), reader.float32()];
  674. });
  675. register( 0x09, 'memory_copy', '', (layer, reader) => {
  676. layer.inputs = [reader.parameter('input')];
  677. layer.outputs = [reader.parameter('output')];
  678. });
  679. register( 0x0A, 'resize_image', '', (layer, reader) => {
  680. layer.inputs = [reader.parameter('input')];
  681. layer.outputs = [reader.parameter('output')];
  682. layer.reduce_op = reader.reduce_op_t();
  683. layer.inputs[0].value[0].shape = reader.runtime_shape_t();
  684. layer.out_h = reader.int32();
  685. layer.out_w = reader.int32();
  686. layer.mode = reader.image_resize_mode_t();
  687. layer.align_corners = reader.boolean();
  688. });
  689. register( 0x0B, 'softmax', 'Activation');
  690. register( 0x0C, 'transpose', 'Transform', (layer, reader) => {
  691. layer.inputs = [reader.parameter('input')];
  692. layer.outputs = [reader.parameter('output')];
  693. layer.inputs[0].value[0].shape = reader.runtime_shape_t();
  694. layer.perm = reader.runtime_shape_t();
  695. });
  696. register( 0x0D, 'strided_slice', 'Tensor', (layer, reader) => {
  697. layer.inputs = [reader.parameter('input')];
  698. layer.outputs = [reader.parameter('output')];
  699. layer.inputs[0].value[0].shape = reader.runtime_shape_t();
  700. layer.begin = reader.runtime_shape_t();
  701. layer.end = reader.runtime_shape_t();
  702. layer.strides = reader.runtime_shape_t();
  703. });
  704. register( 0x0E, 'unary', '', (layer, reader) => {
  705. layer.inputs = [reader.parameter('input')];
  706. layer.outputs = [reader.parameter('output')];
  707. layer.unary_op = reader.unary_op_t();
  708. });
  709. register( 0x0F, 'quantized_conv2d', 'Layer', (layer, reader) => {
  710. layer.inputs = [reader.parameter('input')];
  711. layer.outputs = [reader.parameter('output')];
  712. layer.inputs[0].value[0].shape = reader.runtime_shape_t();
  713. layer.groups = reader.int32();
  714. layer.out_channels = reader.int32();
  715. layer.padding_h = reader.padding();
  716. layer.padding_w = reader.padding();
  717. layer.filter_h = reader.int32();
  718. layer.filter_w = reader.int32();
  719. layer.stride_h = reader.int32();
  720. layer.stride_w = reader.int32();
  721. layer.dilation_h = reader.int32();
  722. layer.dilation_w = reader.int32();
  723. layer.input_offset = reader.int32();
  724. layer.filter_offset = reader.int32();
  725. layer.output_mul = reader.int32();
  726. layer.output_shift = reader.int32();
  727. layer.output_offset = reader.int32();
  728. const bias = reader.span('int32', [layer.out_channels]);
  729. if (bias) {
  730. layer.inputs.push({ name: 'bias', value: [bias] });
  731. }
  732. const weights = reader.span('uint8', [layer.out_channels, layer.inputs[0].value[0].shape[1] / layer.groups, layer.filter_h, layer.filter_w]);
  733. if (weights) {
  734. layer.inputs.push({ name: 'weights', value: [weights] });
  735. }
  736. });
  737. register( 0x10, 'quantized_matmul', '', (layer, reader) => {
  738. layer.inputs = [
  739. reader.parameter('a'),
  740. reader.parameter('b'),
  741. ];
  742. layer.outputs = [reader.parameter('output')];
  743. layer.a_rows = reader.int32();
  744. layer.a_cols = reader.int32();
  745. layer.b_cols = reader.int32();
  746. layer.inputs[1].value[0].shape = [layer.a_cols, layer.b_cols];
  747. layer.input_a_offset = reader.int32();
  748. layer.input_b_offset = reader.int32();
  749. layer.output_mul = reader.int32();
  750. layer.output_shift = reader.int32();
  751. layer.output_offset = reader.int32();
  752. const bias = reader.span('int32', [layer.b_cols]);
  753. if (bias) {
  754. layer.inputs.push({ name: 'bias', value: [bias] });
  755. }
  756. });
  757. register( 0x11, 'quantized_binary', '', (layer, reader) => {
  758. layer.inputs = [
  759. reader.parameter('a'),
  760. reader.parameter('b')
  761. ];
  762. layer.outputs = [reader.parameter('outputs')];
  763. layer.binary_op = reader.binary_op_t();
  764. layer.inputs[0].value[0].shape = reader.runtime_shape_t();
  765. layer.inputs[1].value[0].shape = reader.runtime_shape_t();
  766. layer.outputs[0].value[0].shape = reader.runtime_shape_t();
  767. layer.input_a_offset = reader.int32();
  768. layer.input_a_mul = reader.int32();
  769. layer.input_a_shift = reader.int32();
  770. layer.input_b_offset = reader.int32();
  771. layer.input_b_mul = reader.int32();
  772. layer.input_b_shift = reader.int32();
  773. layer.output_offset = reader.int32();
  774. layer.output_mul = reader.int32();
  775. layer.output_shift = reader.int32();
  776. });
  777. register( 0x12, 'table_lookup1d', '', (layer, reader) => {
  778. layer.inputs = [reader.parameter('input'), reader.parameter('table')];
  779. layer.outputs = [reader.parameter('output')];
  780. });
  781. register( 0x13, 'conv2d_transpose', 'Layer', (layer, reader) => {
  782. layer.inputs = [reader.parameter('input')];
  783. layer.outputs = [reader.parameter('output')];
  784. layer.inputs[0].value[0].shape = reader.runtime_shape_t();
  785. layer.groups = reader.int32();
  786. layer.out_channels = reader.int32();
  787. layer.padding_h = reader.padding();
  788. layer.padding_w = reader.padding();
  789. layer.filter_h = reader.int32();
  790. layer.filter_w = reader.int32();
  791. layer.stride_h = reader.int32();
  792. layer.stride_w = reader.int32();
  793. layer.dilation_h = reader.int32();
  794. layer.dilation_w = reader.int32();
  795. layer.fused_activation = [reader.float32(), reader.float32()];
  796. const weights_shape = [layer.out_channels, layer.inputs[0].value[0].shape[1] / layer.groups, layer.filter_h, layer.filter_w];
  797. const weights_size = 4 * weights_shape.reduce((a, b) => a * b);
  798. layer.inputs.push({
  799. name: 'weights',
  800. value: [{
  801. name: '',
  802. datatype: 'float32',
  803. shape: weights_shape,
  804. data: reader.read(weights_size)
  805. }]
  806. });
  807. const bias_shape = [layer.out_channels];
  808. const bias_size = 4 * layer.out_channels;
  809. layer.inputs.push({
  810. name: 'bias',
  811. value: [{
  812. name: '',
  813. datatype: 'float32',
  814. shape: bias_shape,
  815. data: reader.read(bias_size)
  816. }]
  817. });
  818. });
  819. register( 0x14, 'nnil_unary_method', '', (layer, reader, size) => {
  820. const position = reader.position;
  821. layer.inputs = [reader.parameter('input')];
  822. layer.outputs = [reader.parameter('output')];
  823. layer.body = reader.read(size - (reader.position - position));
  824. });
  825. register(0x1001, 'cpu_conv2d', 'Layer', (layer, reader) => {
  826. layer.inputs = [reader.parameter('input')];
  827. layer.outputs = [reader.parameter('output')];
  828. layer.inputs[0].value[0].shape = reader.runtime_shape_t();
  829. layer.groups = reader.int32();
  830. layer.out_channels = reader.int32();
  831. layer.padding_h = reader.padding();
  832. layer.padding_w = reader.padding();
  833. layer.filter_h = reader.int32();
  834. layer.filter_w = reader.int32();
  835. layer.stride_h = reader.int32();
  836. layer.stride_w = reader.int32();
  837. layer.dilation_h = reader.int32();
  838. layer.dilation_w = reader.int32();
  839. layer.fused_activation = [reader.float32(), reader.float32()];
  840. });
  841. register(0x1002, 'cpu_depthwise_conv2d', 'Layer', (layer, reader) => {
  842. layer.inputs = [reader.parameter('input')];
  843. layer.outputs = [reader.parameter('output')];
  844. layer.inputs[0].value[0].shape = reader.runtime_shape_t();
  845. layer.out_channels = reader.int32();
  846. layer.padding_h = reader.padding();
  847. layer.padding_w = reader.padding();
  848. layer.filter_h = reader.int32();
  849. layer.filter_w = reader.int32();
  850. layer.stride_h = reader.int32();
  851. layer.stride_w = reader.int32();
  852. layer.dilation_h = reader.int32();
  853. layer.dilation_w = reader.int32();
  854. layer.fused_activation = [reader.float32(), reader.float32()];
  855. });
  856. register(0x1003, 'cpu_reduce_window2d', 'Pool', (layer, reader) => {
  857. layer.inputs = [reader.parameter('input')];
  858. layer.outputs = [reader.parameter('output')];
  859. layer.reduce_op = reader.reduce_op_t();
  860. layer.inputs[0].value[0].shape = reader.runtime_shape_t();
  861. layer.padding_h = reader.padding();
  862. layer.padding_w = reader.padding();
  863. layer.filter_h = reader.int32();
  864. layer.filter_w = reader.int32();
  865. layer.stride_h = reader.int32();
  866. layer.stride_w = reader.int32();
  867. layer.dilation_h = reader.int32();
  868. layer.dilation_w = reader.int32();
  869. layer.fused_activation = [reader.float32(), reader.float32()];
  870. });
  871. register(0x1004, 'cpu_quantized_conv2d', 'Layer', (layer, reader) => {
  872. layer.inputs = [reader.parameter('input')];
  873. layer.outputs = [reader.parameter('output')];
  874. layer.inputs[0].value[0].shape = reader.runtime_shape_t();
  875. layer.groups = reader.int32();
  876. layer.out_channels = reader.int32();
  877. layer.padding_h = reader.padding();
  878. layer.padding_w = reader.padding();
  879. layer.filter_h = reader.int32();
  880. layer.filter_w = reader.int32();
  881. layer.stride_h = reader.int32();
  882. layer.stride_w = reader.int32();
  883. layer.dilation_h = reader.int32();
  884. layer.dilation_w = reader.int32();
  885. layer.input_offset = reader.int32();
  886. layer.filter_offset = reader.int32();
  887. layer.output_mul = reader.int32();
  888. layer.output_shift = reader.int32();
  889. layer.output_offset = reader.int32();
  890. });
  891. register(0x1005, 'cpu_quantized_depthwise_conv2d', 'Layer', (layer, reader) => {
  892. layer.inputs = [reader.parameter('input')];
  893. layer.outputs = [reader.parameter('output')];
  894. layer.inputs[0].value[0].shape = reader.runtime_shape_t();
  895. layer.out_channels = reader.int32();
  896. layer.padding_h = reader.padding();
  897. layer.padding_w = reader.padding();
  898. layer.filter_h = reader.int32();
  899. layer.filter_w = reader.int32();
  900. layer.stride_h = reader.int32();
  901. layer.stride_w = reader.int32();
  902. layer.dilation_h = reader.int32();
  903. layer.dilation_w = reader.int32();
  904. layer.input_offset = reader.int32();
  905. layer.filter_offset = reader.int32();
  906. layer.output_mul = reader.int32();
  907. layer.output_shift = reader.int32();
  908. layer.output_offset = reader.int32();
  909. });
  910. register(0x2001, 'kpu_upload', '', (layer, reader) => {
  911. layer.inputs = [reader.parameter('input')];
  912. layer.outputs = [reader.parameter('output')];
  913. layer.inputs[0].value[0].shape = reader.runtime_shape_t();
  914. });
  915. register(0x2002, 'kpu_conv2d', 'Layer', (layer, reader) => {
  916. layer.outputs = [reader.parameter('output')];
  917. layer.batches = reader.int32();
  918. layer.reserved0 = reader.int32();
  919. layer.interrupt_enabe = reader.uint64_bits({ int_en: 0, ram_flag: 1, full_add: 2, depth_wise_layer: 3 });
  920. const image_src_addr = reader.uint32();
  921. const image_dst_addr = reader.uint32();
  922. layer.inputs = [{ name: 'input', value: [{ name: `kpu:${image_src_addr}` }] }];
  923. const outputs = [{ name: 'output', value: [{ name: `kpu:${image_dst_addr}` }] }];
  924. layer.outputs[0].value.push(outputs[0].value[0]);
  925. // layer.outputs = layer.flags & 1 ? layer.outputs : outputs;
  926. layer.image_channel_num = reader.uint64_bits({ i_ch_num: 0, o_ch_num: 32, o_ch_num_coef: 48 });
  927. layer.image_size = reader.uint64_bits({ i_row_wid: 0, i_col_high: 10, o_row_wid: 32, o_col_high : 42 });
  928. layer.kernel_pool_type_cfg = reader.uint64_bits({ kernel_type: 0, pad_type: 3, pool_type: 4, first_stride: 8, bypass_conv: 9, load_para: 10, dma_burst_size: 16, pad_value: 24, bwsx_base_addr: 32 });
  929. layer.kernel_load_cfg = reader.uint64_bits({ load_coor: 0, load_time: 1, para_size: 15, para_start_addr: 32 });
  930. layer.kernel_offset = reader.uint64_bits({ coef_column_offset: 0, coef_row_offset: 4 });
  931. layer.kernel_calc_type_cfg = reader.uint64_bits({ channel_switch_addr: 0, row_switch_addr: 16, coef_size: 20, coef_group: 28, load_act: 31, active_addr: 32 });
  932. layer.write_back_cfg = reader.uint64_bits({ wb_channel_switch_addr: 0, wb_row_switch_addr: 16, wb_group: 20 });
  933. layer.conv_value = reader.uint64_bits({ shr_w: 0, shr_x: 4, arg_w: 8, arg_x: 32 });
  934. layer.conv_value2 = reader.uint64_bits({ arg_add: 0 });
  935. layer.dma_parameter = reader.uint64_bits({ send_data_out: 0, reserved: 1, channel_byte_num: 16, dma_total_byte: 32 });
  936. layer.chain = [];
  937. const ic = layer.image_channel_num.i_ch_num + 1;
  938. const oc = layer.image_channel_num.o_ch_num + 1;
  939. layer.outputs[0].value[0].shape = [layer.image_size.o_row_wid + 1, layer.image_size.o_col_high + 1, oc];
  940. const filter = [1, 3][layer.kernel_pool_type_cfg.kernel_type];
  941. const weights_shape = layer.interrupt_enabe.depth_wise_layer ? [oc, filter, filter] : [ic, oc, filter, filter];
  942. reader.skip(layer.kernel_pool_type_cfg.bwsx_base_addr);
  943. delete layer.kernel_pool_type_cfg.bwsx_base_addr;
  944. const batch_norm = {
  945. type: { name: 'batch_norm', category: 'Normalization' },
  946. weights: []
  947. };
  948. batch_norm.weights = new Array(oc);
  949. for (let i = 0; i < oc; i++) {
  950. batch_norm.weights[i] = reader.uint64_bits({ norm_mul: 0, norm_add: 24, norm_shift: 56, reserved: 60 });
  951. delete batch_norm.weights[i].reserved;
  952. }
  953. layer.chain.push(batch_norm);
  954. reader.skip(layer.kernel_calc_type_cfg.active_addr);
  955. delete layer.kernel_calc_type_cfg.active_addr;
  956. const activation = reader.kpu_activate_table_t();
  957. activation.type = { name: 'activation', category: 'Activation' };
  958. layer.chain.push(activation);
  959. reader.skip(layer.kernel_load_cfg.para_start_addr);
  960. delete layer.kernel_load_cfg.para_start_addr;
  961. const weights = reader.span('uint8', weights_shape);
  962. if (weights) {
  963. layer.inputs.push({ name: 'weights', value: [weights] });
  964. }
  965. });
  966. /* eslint-enable space-in-parens */
  967. for (const layer of layers) {
  968. const type = types.get(layer.opcode);
  969. if (!type) {
  970. throw new kmodel.Error(`Unsupported version '${this.version}' layer type '${layer.type}'.`);
  971. }
  972. if (!type.callback) {
  973. throw new kmodel.Error(`Unsupported version '${this.version}' layer '${type.type.name}'.`);
  974. }
  975. layer.type = type.type;
  976. reader.seek(layer.offset);
  977. if (type.callback) {
  978. type.callback(layer, reader, layer.body_size);
  979. }
  980. delete layer.offset;
  981. delete layer.body_size;
  982. delete layer.opcode;
  983. }
  984. for (const input of inputs) {
  985. layers.unshift({
  986. type: { name: 'INPUT' },
  987. outputs: [input]
  988. });
  989. }
  990. for (const output of outputs) {
  991. layers.push({
  992. type: { name: 'OUTPUT' },
  993. inputs: [output]
  994. });
  995. }
  996. this.modules.push({
  997. name: '',
  998. layers
  999. });
  1000. break;
  1001. }
  1002. case 5:
  1003. case 6:
  1004. case 7: {
  1005. let reader = null;
  1006. switch (this.version) {
  1007. case 5: reader = new kmodel.BinaryReader.v5(this.stream); break;
  1008. case 6: reader = new kmodel.BinaryReader.v6(this.stream); break;
  1009. case 7: reader = new kmodel.BinaryReader.v7(this.stream); break;
  1010. default: throw new kmodel.Error(`Unsupported model version '${this.version}'.`);
  1011. }
  1012. const model_header = reader.model_header();
  1013. this.modules = new Array(model_header.modules);
  1014. for (let i = 0; i < this.modules.length; i++) {
  1015. const module_header = reader.module_header();
  1016. const mempools = new Array(module_header.mempools || 0);
  1017. for (let j = 0; j < mempools.length; j++) {
  1018. mempools[j] = reader.mempool_desc();
  1019. }
  1020. const shared_mempools = new Array(module_header.shared_mempools || 0);
  1021. for (let j = 0; j < shared_mempools.length; j++) {
  1022. shared_mempools[i] = reader.mempool_desc();
  1023. }
  1024. const function_headers = new Array(module_header.functions);
  1025. const functions = new Array(module_header.functions);
  1026. for (let j = 0; j < functions.length; j++) {
  1027. const position = reader.position;
  1028. let inputs = [];
  1029. let outputs = [];
  1030. if (this.version === 5) {
  1031. const function_header = reader.function_header();
  1032. inputs = new Array(function_header.inputs || 0);
  1033. for (let k = 0; k < inputs.length; k++) {
  1034. inputs[k] = reader.parameter(`input${k === 0 ? '' : (k + 1)}`);
  1035. }
  1036. for (let k = 0; k < inputs.length; k++ || 0) {
  1037. inputs[k].value[0].shape = reader.shape();
  1038. }
  1039. outputs = new Array(function_header.outputs || 0);
  1040. for (let k = 0; k < outputs.length; k++) {
  1041. outputs[k] = reader.parameter(`output${k === 0 ? '' : (k + 1)}`);
  1042. }
  1043. for (let k = 0; k < outputs.length; k++) {
  1044. outputs[k].value[0].shape = reader.shape();
  1045. }
  1046. reader.align(8);
  1047. const size = reader.position - position;
  1048. if (function_header.size > size) {
  1049. reader.skip(function_header.size - size);
  1050. }
  1051. function_headers[j] = function_header;
  1052. } else {
  1053. const func_start = reader.position;
  1054. const function_header = reader.function_header();
  1055. const header_size = reader.position - func_start;
  1056. const remaining_size = function_header.size - header_size;
  1057. if (remaining_size > 0) {
  1058. reader.skip(remaining_size);
  1059. }
  1060. function_headers[j] = function_header;
  1061. }
  1062. functions[j] = {
  1063. type: { name: 'Unknown' },
  1064. inputs,
  1065. outputs
  1066. };
  1067. }
  1068. const sections = new Map();
  1069. for (let j = 0; j < module_header.sections; j++) {
  1070. const section_header = reader.section_header();
  1071. reader.skip(section_header.body_start);
  1072. const body = reader.read(section_header.body_size);
  1073. const section = {
  1074. reader: base.BinaryReader.open(body),
  1075. flags: section_header.flags
  1076. };
  1077. reader.align(8);
  1078. sections.set(section_header.name, section);
  1079. }
  1080. for (let j = 0; j < function_headers.length; j++) {
  1081. const function_header = function_headers[j];
  1082. const reader = sections.get('.text').reader;
  1083. reader.seek(function_header.entrypoint);
  1084. const size = function_header.text_size;
  1085. function_header.text = reader.read(size);
  1086. const layer = functions[i];
  1087. switch (module_header.type) {
  1088. case 'stackvm': {
  1089. layer.type = { name: 'stackvm' };
  1090. let reader = null;
  1091. const buffer = function_header.text;
  1092. switch (this.version) {
  1093. case 5: reader = new kmodel.BytecodeReader.v5(buffer); break;
  1094. case 6: reader = new kmodel.BytecodeReader.v6(buffer); break;
  1095. case 7: reader = new kmodel.BytecodeReader.v6(buffer); break;
  1096. default: throw new kmodel.Error(`Unsupported model version '${this.version}'.`);
  1097. }
  1098. reader = null;
  1099. if (reader) {
  1100. layer.operations = reader.read();
  1101. layer.tensor_operations = layer.operations.filter((op) => op.name === 'tensor');
  1102. }
  1103. break;
  1104. }
  1105. case 'k210':
  1106. case 'k230':
  1107. case 'k510':
  1108. break;
  1109. default:
  1110. throw new kmodel.Error(`Unsupported module type '${module_header.type}'.`);
  1111. }
  1112. }
  1113. this.modules[i] = {
  1114. type: module_header.type,
  1115. name: this.modules.length > 1 ? i.toString() : '',
  1116. layers: functions
  1117. };
  1118. }
  1119. break;
  1120. }
  1121. default: {
  1122. throw new kmodel.Error(`Unsupported model version '${this.version}'.`);
  1123. }
  1124. }
  1125. delete this.stream;
  1126. }
  1127. };
  1128. kmodel.BinaryReader = class {
  1129. constructor(data) {
  1130. this._reader = base.BinaryReader.open(data);
  1131. }
  1132. get position() {
  1133. return this._reader.position;
  1134. }
  1135. seek(position) {
  1136. this._reader.seek(position);
  1137. }
  1138. skip(offset) {
  1139. this._reader.skip(offset);
  1140. }
  1141. align(size) {
  1142. this._reader.align(size);
  1143. }
  1144. read(length) {
  1145. return this._reader.read(length);
  1146. }
  1147. boolean() {
  1148. return this._reader.boolean();
  1149. }
  1150. byte() {
  1151. return this._reader.byte();
  1152. }
  1153. int8() {
  1154. return this._reader.int8();
  1155. }
  1156. int16() {
  1157. return this._reader.int16();
  1158. }
  1159. int32() {
  1160. return this._reader.int32();
  1161. }
  1162. uint16() {
  1163. return this._reader.uint16();
  1164. }
  1165. uint32() {
  1166. return this._reader.uint32();
  1167. }
  1168. uint64() {
  1169. return this._reader.uint64().toNumber();
  1170. }
  1171. float32() {
  1172. return this._reader.float32();
  1173. }
  1174. uint64_bits(fields) {
  1175. const buffer = this.read(8);
  1176. fields = Object.entries(fields);
  1177. fields.push([null, Math.min(64, fields[fields.length - 1][1] + 56)]);
  1178. const obj = {};
  1179. for (let i = 0; i < fields.length - 1; i++) {
  1180. const current = fields[i];
  1181. const next = fields[i + 1];
  1182. const [key, start] = current;
  1183. const [, end] = next;
  1184. let value = 0;
  1185. let position = start;
  1186. while (position < end) {
  1187. const offset = (position / 8) >> 0;
  1188. const start = (position & 7);
  1189. const count = Math.min((offset + 1) * 8, end) - position;
  1190. value |= ((buffer[offset] >>> start) & ((1 << count) - 1)) << (position - fields[i][1]);
  1191. position += count;
  1192. }
  1193. obj[key] = value;
  1194. }
  1195. return obj;
  1196. }
  1197. };
  1198. kmodel.BinaryReader.v3 = class extends kmodel.BinaryReader {
  1199. constructor(buffer) {
  1200. super(buffer);
  1201. this.skip(4);
  1202. }
  1203. kpu_model_header_t() {
  1204. return {
  1205. flags: this.uint32(),
  1206. arch: this.uint32(),
  1207. layers_length: this.uint32(),
  1208. max_start_address: this.uint32(),
  1209. main_mem_usage: this.uint32(),
  1210. output_count: this.uint32()
  1211. };
  1212. }
  1213. kpu_model_output_t(name) {
  1214. return {
  1215. address: [this.parameter(name)],
  1216. size: this.uint32()
  1217. };
  1218. }
  1219. kpu_model_layer_header_t() {
  1220. return {
  1221. type: this.uint32(),
  1222. body_size: this.uint32()
  1223. };
  1224. }
  1225. argument(memory_type) {
  1226. memory_type = memory_type || 'main';
  1227. const address = this.uint32();
  1228. return { name: `${memory_type}:${address}` };
  1229. }
  1230. parameter(name, memory_type) {
  1231. return { name, value: [this.argument(memory_type)] };
  1232. }
  1233. };
  1234. kmodel.BinaryReader.v4 = class extends kmodel.BinaryReader {
  1235. constructor(buffer) {
  1236. super(buffer);
  1237. this.skip(8);
  1238. this._memory_types = ['const', 'main', 'kpu'];
  1239. this._datatypes = ['float32', 'uint8'];
  1240. }
  1241. memory_type_t() {
  1242. const value = this.uint32();
  1243. return this._memory_types[value];
  1244. }
  1245. datatype_t() {
  1246. const value = this.uint32();
  1247. return this._datatypes[value];
  1248. }
  1249. memory_range() {
  1250. return {
  1251. memory_type: this.memory_type_t(),
  1252. datatype: this.datatype_t(),
  1253. start: this.uint32(),
  1254. size: this.uint32()
  1255. };
  1256. }
  1257. argument() {
  1258. const memory = this.memory_range();
  1259. const value = {
  1260. name: `${memory.memory_type}:${memory.start}`,
  1261. datatype: memory.datatype
  1262. };
  1263. if (memory.memory_type === 'const') {
  1264. value.data = this._constants.slice(memory.start, memory.start + memory.size);
  1265. switch (value.datatype) {
  1266. case 'uint8': value.shape = [value.data.length]; break;
  1267. case 'float32': value.shape = [value.data.length >> 2]; break;
  1268. default: break;
  1269. }
  1270. }
  1271. return value;
  1272. }
  1273. parameter(name) {
  1274. return { name, value: [this.argument()] };
  1275. }
  1276. runtime_shape_t() {
  1277. return [this.uint32(), this.uint32(), this.uint32(), this.uint32()];
  1278. }
  1279. padding() {
  1280. return { before: this.int32(), after: this.int32() };
  1281. }
  1282. runtime_paddings_t() {
  1283. return [this.padding(), this.padding(), this.padding(), this.padding()];
  1284. }
  1285. scalar() {
  1286. return {
  1287. datatype_t: this.uint32(),
  1288. storage: this.read(4)
  1289. };
  1290. }
  1291. kpu_activate_table_t() {
  1292. const value = {};
  1293. value.activate_para = new Array(16);
  1294. for (let i = 0; i < 16; i++) {
  1295. value.activate_para[i] = this.uint64_bits({ shift_number: 0, y_mul: 8, x_start: 24, reserved: 60 });
  1296. delete value.activate_para[i].reserved;
  1297. }
  1298. for (let i = 0; i < 16; i++) {
  1299. value.activate_para[i].bias = this.int8();
  1300. }
  1301. return value;
  1302. }
  1303. unary_op_t() {
  1304. const value = this.uint32();
  1305. return ['abs', 'ceil', 'cos', 'exp', 'floor', 'log', 'neg', 'rsqrt', 'sin', 'square'][value];
  1306. }
  1307. binary_op_t() {
  1308. const value = this.uint32();
  1309. return ['add', 'sub', 'mul', 'div', 'min', 'max'][value];
  1310. }
  1311. reduce_op_t() {
  1312. const value = this.uint32();
  1313. return ['mean', 'min', 'max', 'sum'][value];
  1314. }
  1315. image_resize_mode_t() {
  1316. const value = this.uint32();
  1317. return ['bilinear', 'nearest_neighbor'][value];
  1318. }
  1319. constants(size) {
  1320. this._constants = this.read(size);
  1321. }
  1322. span(datatype, shape) {
  1323. const size = shape.reduce((a, b) => a * b, 1);
  1324. const itemsize = { 'int32': 4, 'uint8': 1 };
  1325. const buffer = this.read(itemsize[datatype] * size);
  1326. if (!buffer.every((value) => value === 0)) {
  1327. const array = {};
  1328. array.name = '';
  1329. array.datatype = datatype;
  1330. array.shape = shape;
  1331. array.data = buffer;
  1332. return array;
  1333. }
  1334. return null;
  1335. }
  1336. };
  1337. kmodel.BinaryReader.v5 = class extends kmodel.BinaryReader {
  1338. constructor(buffer) {
  1339. super(buffer);
  1340. this.skip(8);
  1341. this._datatypes = ['int8', 'int16', 'int32', 'int64', 'uint8', 'uint16', 'uint32', 'uint64', 'float16', 'float32', 'float64', 'bfloat16'];
  1342. this._memory_locations = new Map([[0, 'input'], [1, 'output'], [2, 'rdata'], [3, 'data'], [4, 'shared_data'], [64, 'kpu']]);
  1343. }
  1344. model_header() {
  1345. const model_header = {
  1346. header_size: this.uint32(),
  1347. flags: this.uint32(),
  1348. alignment: this.uint32(),
  1349. modules: this.uint32(),
  1350. entry_module: this.uint32(),
  1351. entry_function: this.uint32()
  1352. };
  1353. if (model_header.header_size < 32) {
  1354. throw new kmodel.Error(`Invalid header size '${model_header.header_size}'.`);
  1355. }
  1356. if (model_header.header_size > this.position) {
  1357. this.skip(model_header.header_size - this.position);
  1358. }
  1359. delete model_header.header_size;
  1360. return model_header;
  1361. }
  1362. module_type_t() {
  1363. const buffer = this.read(16);
  1364. const decoder = new TextDecoder('ascii');
  1365. const text = decoder.decode(buffer);
  1366. return text.replace(/\0.*$/, '');
  1367. }
  1368. module_header() {
  1369. const start = this.position;
  1370. const module_header = {
  1371. type: this.module_type_t(),
  1372. version: this.uint32(),
  1373. header_size: this.uint32(),
  1374. size: this.uint32(),
  1375. mempools: this.uint32(),
  1376. shared_mempools: this.uint32(),
  1377. sections: this.uint32(),
  1378. functions: this.uint32(),
  1379. reserved0: this.uint32()
  1380. };
  1381. if (module_header.header_size > (this.position - start)) {
  1382. this.skip(module_header.header_size - (this.position - start));
  1383. }
  1384. return module_header;
  1385. }
  1386. mempool_desc() {
  1387. return {
  1388. location: this.byte(),
  1389. reserved0: this.read(3),
  1390. size: this.uint32()
  1391. };
  1392. }
  1393. section_header() {
  1394. const buffer = this.read(16);
  1395. const decoder = new TextDecoder('ascii');
  1396. const name = decoder.decode(buffer);
  1397. return {
  1398. name: name.replace(/\0.*$/, ''),
  1399. flags: this.uint32(),
  1400. body_start: this.uint32(),
  1401. body_size: this.uint32(),
  1402. reserved0: this.uint32()
  1403. };
  1404. }
  1405. function_header() {
  1406. const position = this.position;
  1407. const function_header = {
  1408. header_size: this.uint32(),
  1409. size: this.uint32(),
  1410. input_pool_size: this.uint32(),
  1411. output_pool_size: this.uint32(),
  1412. inputs: this.uint32(),
  1413. outputs: this.uint32(),
  1414. entrypoint: this.uint32(),
  1415. text_size: this.uint32()
  1416. };
  1417. const header_size = this.position - position;
  1418. if (function_header.header_size > header_size) {
  1419. this.skip(function_header.header_size - header_size);
  1420. }
  1421. return function_header;
  1422. }
  1423. memory_location_t() {
  1424. const value = this.byte();
  1425. if (!this._memory_locations.has(value)) {
  1426. throw new kmodel.Error(`Unsupported memory location '${value}'.`);
  1427. }
  1428. return this._memory_locations.get(value);
  1429. }
  1430. datatype_t() {
  1431. const value = this.byte();
  1432. return this._datatypes[value];
  1433. }
  1434. memory_range() {
  1435. return {
  1436. memory_location: this.memory_location_t(),
  1437. datatype: this.datatype_t(),
  1438. shared_module: this.uint16(),
  1439. start: this.uint32(),
  1440. size: this.uint32()
  1441. };
  1442. }
  1443. argument() {
  1444. const memory = this.memory_range();
  1445. const value = {
  1446. name: `${memory.memory_location}:${memory.start}`,
  1447. datatype: memory.datatype
  1448. };
  1449. /*
  1450. if (memory.memory_type === 'const') {
  1451. value.data = constants.slice(memory.start, memory.start + memory.size);
  1452. switch (value.datatype) {
  1453. case 'uint8': value.shape = [ value.data.length ]; break;
  1454. case 'float32': value.shape = [ value.data.length >> 2 ]; break;
  1455. default: break;
  1456. }
  1457. }
  1458. */
  1459. return value;
  1460. }
  1461. parameter(name) {
  1462. return { name, value: [this.argument()] };
  1463. }
  1464. shape() {
  1465. const array = new Array(this.uint32());
  1466. for (let i = 0; i < array.length; i++) {
  1467. array[i] = this.uint32();
  1468. }
  1469. return array;
  1470. }
  1471. };
  1472. kmodel.BinaryReader.v6 = class extends kmodel.BinaryReader.v5 {
  1473. model_header() {
  1474. return {
  1475. flags: this.uint32(),
  1476. alignment: this.uint32(),
  1477. modules: this.uint32(),
  1478. entry_module: this.uint32(),
  1479. entry_function: this.uint32(),
  1480. reserved0: this.uint32()
  1481. };
  1482. }
  1483. module_header() {
  1484. return {
  1485. type: this.module_type_t(),
  1486. version: this.uint32(),
  1487. size: this.uint32(),
  1488. sections: this.uint32(),
  1489. functions: this.uint32()
  1490. };
  1491. }
  1492. function_header() {
  1493. return {
  1494. parameters: this.uint32(),
  1495. entrypoint: this.uint32(),
  1496. text_size: this.uint32(),
  1497. size: this.uint32(),
  1498. sections: this.uint32(),
  1499. reserved0: this.uint32(),
  1500. };
  1501. }
  1502. section_header() {
  1503. const buffer = this.read(16);
  1504. const decoder = new TextDecoder('ascii');
  1505. const name = decoder.decode(buffer);
  1506. return {
  1507. name: name.replace(/\0.*$/, ''),
  1508. size: this.uint32(),
  1509. flags: this.uint32(),
  1510. body_start: this.uint32(),
  1511. body_size: this.uint32(),
  1512. memory_size: this.uint32(),
  1513. reserved0: this.uint32()
  1514. };
  1515. }
  1516. deserialize_datatype() {
  1517. const typecode = this.byte();
  1518. if (typecode === 100) { // dt_pointer
  1519. const elem_type = this.deserialize_datatype();
  1520. return { type: 'pointer', elem_type };
  1521. } else if (typecode === 101) { // dt_valuetype
  1522. const uuid = this.read(16);
  1523. const size_bytes = this.uint32();
  1524. return { type: 'valuetype', uuid, size_bytes };
  1525. } else if (typecode >= 0 && typecode <= 12) {
  1526. const types = ['bool', 'int8', 'int16', 'int32', 'int64', 'uint8', 'uint16', 'uint32', 'uint64', 'float16', 'float32', 'float64', 'bfloat16'];
  1527. return { type: 'prim', name: types[typecode] || `unknown_${typecode}` };
  1528. }
  1529. throw new kmodel.Error(`Unknown datatype typecode: ${typecode}`);
  1530. }
  1531. deserialize_type() {
  1532. const token = this.byte();
  1533. switch (token) {
  1534. case 0: // type_sig_invalid
  1535. return { type: 'invalid' };
  1536. case 1: // type_sig_any
  1537. return { type: 'any' };
  1538. case 2: { // type_sig_tensor
  1539. const elem_type = this.deserialize_datatype();
  1540. const is_scalar = this.byte() === 0;
  1541. const shape = [];
  1542. if (!is_scalar) {
  1543. let dim_token = 0;
  1544. while ((dim_token = this.byte()) !== 255) { // type_sig_end
  1545. if (dim_token === 1) { // dim_fixed
  1546. shape.push(this.uint32());
  1547. } else if (dim_token === 2) { // dim_unknown
  1548. shape.push(-1);
  1549. } else {
  1550. throw new kmodel.Error(`Invalid dim token: ${dim_token}`);
  1551. }
  1552. }
  1553. }
  1554. return { type: 'tensor', elem_type, shape, is_scalar };
  1555. }
  1556. case 3: { // type_sig_tuple
  1557. const fields = [];
  1558. while (this.peek() !== 255) { // type_sig_end
  1559. fields.push(this.deserialize_type());
  1560. }
  1561. this.skip(1); // skip end token
  1562. return { type: 'tuple', fields };
  1563. }
  1564. case 4: // type_sig_callable
  1565. throw new kmodel.Error('Callable types not supported');
  1566. default:
  1567. throw new kmodel.Error(`Unknown type signature token: ${token}`);
  1568. }
  1569. }
  1570. peek() {
  1571. const value = this.byte();
  1572. this.seek(this.position - 1);
  1573. return value;
  1574. }
  1575. };
  1576. kmodel.BinaryReader.v7 = class extends kmodel.BinaryReader.v6 {
  1577. module_header() {
  1578. return {
  1579. type: this.module_type_t(),
  1580. version: this.uint32(),
  1581. sections: this.uint32(),
  1582. functions: this.uint32(),
  1583. reserved0: this.uint32(),
  1584. size: this.uint64(),
  1585. };
  1586. }
  1587. function_header() {
  1588. return {
  1589. parameters: this.uint32(),
  1590. sections: this.uint32(),
  1591. entrypoint: this.uint64(),
  1592. text_size: this.uint64(),
  1593. size: this.uint64(),
  1594. };
  1595. }
  1596. section_header() {
  1597. const buffer = this.read(16);
  1598. const decoder = new TextDecoder('ascii');
  1599. const name = decoder.decode(buffer);
  1600. return {
  1601. name: name.replace(/\0.*$/, ''),
  1602. flags: this.uint32(),
  1603. reserved0: this.uint32(),
  1604. size: this.uint64(),
  1605. body_start: this.uint64(),
  1606. body_size: this.uint64(),
  1607. memory_size: this.uint64(),
  1608. };
  1609. }
  1610. };
  1611. kmodel.BytecodeReader = class {
  1612. constructor(buffer) {
  1613. this._reader = base.BinaryReader.open(buffer);
  1614. }
  1615. read() {
  1616. const operations = [];
  1617. while (this._reader.position < this._reader.length) {
  1618. const position = this._reader.position;
  1619. const opcode = this._reader.byte();
  1620. if (!this._opcodes.has(opcode)) {
  1621. throw new kmodel.Error(`Unknown opcode '${opcode}'.`);
  1622. }
  1623. const name = this._opcodes.get(opcode);
  1624. const operation = { name, position };
  1625. this.operation(operation);
  1626. // console.log(JSON.stringify(operation));
  1627. operations.push(operation);
  1628. if (name === 'ret') {
  1629. break;
  1630. }
  1631. }
  1632. return operations;
  1633. }
  1634. strings() {
  1635. // Read strings until we encounter a null byte as the first character
  1636. const array = [];
  1637. while (this._reader.position < this._reader.length) {
  1638. // Peek at next byte
  1639. const byte = this._reader.byte();
  1640. if (byte === 0) {
  1641. break; // End of array
  1642. }
  1643. // Put the byte back by moving position back
  1644. this._reader.seek(this._reader.position - 1);
  1645. array.push(this.string());
  1646. }
  1647. return array;
  1648. }
  1649. };
  1650. kmodel.BytecodeReader.v5 = class extends kmodel.BytecodeReader {
  1651. constructor(buffer) {
  1652. super(buffer);
  1653. this._opcodes = new Map([
  1654. [0, 'nop'], [1, 'ldnull'], [2, 'ldc_i4'], [3, 'ldc_i4_0'], [4, 'ldc_i4_1'],
  1655. [5, 'ldc_r4'], [6, 'ldind_i1'], [7, 'ldind_i2'], [8, 'ldind_i4'], [9, 'ldind_i'],
  1656. [10, 'ldind_u1'], [11, 'ldind_u2'], [12, 'ldind_u4'], [13, 'ldind_u'],
  1657. [14, 'ldind_br2'], [15, 'ldind_r4'], [16, 'stind_i1'], [17, 'stind_i2'],
  1658. [18, 'stind_i4'], [19, 'stind_i'], [20, 'stind_br2'], [21, 'stind_r4'],
  1659. [22, 'lea_gp'], [23, 'lea_buffer'], [24, 'ldelem_i1'], [25, 'ldelem_i2'],
  1660. [26, 'ldelem_i4'], [27, 'ldelem_i'], [28, 'ldelem_u1'], [29, 'ldelem_u2'],
  1661. [30, 'ldelem_u4'], [0x1F, 'ldelem_u'], [0x20, 'ldelem_br2'], [33, 'ldelem_r4'],
  1662. [34, 'stelem_i1'], [35, 'stelem_i2'], [36, 'stelem_i4'], [37, 'stelem_i'],
  1663. [38, 'stelem_br2'], [39, 'stelem_r4'], [40, 'ldarg'], [41, 'ldarg_0'],
  1664. [42, 'ldarg_1'], [43, 'ldarg_2'], [44, 'ldarg_3'], [45, 'ldarg_4'],
  1665. [46, 'ldarg_5'], [47, 'dup'], [48, 'pop'], [0x31, 'stshape'], [50, 'stpaddings'],
  1666. [51, 'neg'], [52, 'add'], [53, 'sub'], [54, 'mul'], [55, 'div'], [56, 'div_u'],
  1667. [57, 'rem'], [58, 'rem_u'], [59, 'and'], [60, 'or'], [61, 'xor'], [62, 'not'],
  1668. [63, 'shl'], [64, 'shr'], [65, 'shr_u'], [66, 'clt'], [67, 'clt_u'],
  1669. [68, 'cle'], [69, 'cle_u'], [70, 'ceq'], [71, 'cge'], [72, 'cge_u'],
  1670. [73, 'cgt'], [74, 'cgt_u'], [75, 'cne'], [76, 'conv_i1'], [77, 'conv_i2'],
  1671. [78, 'conv_i4'], [79, 'conv_i'], [80, 'conv_u1'], [81, 'conv_u2'],
  1672. [82, 'conv_u4'], [83, 'conv_u'], [84, 'conv_br2'], [85, 'conv_r4'],
  1673. [86, 'br'], [87, 'br_true'], [88, 'br_false'], [89, 'ret'], [90, 'call'],
  1674. [91, 'ecall'], [0x5C, 'throw'], [0x5D, 'break'], [0x5E, 'tensor']
  1675. ]);
  1676. this._tensorFunctions = new Map([
  1677. [0x0000, { name: 'batch_to_space', category: 'Transform' }],
  1678. [0x0001, { name: 'binary', category: '' }],
  1679. [0x0002, { name: 'broadcast', category: '' }],
  1680. [0x0003, { name: 'call', category: '' }],
  1681. [0x0004, { name: 'clamp', category: 'Activation' }],
  1682. [0x0005, { name: 'conv2d', category: 'Layer' }],
  1683. [0x0006, { name: 'conv2d_transpose', category: 'Layer' }],
  1684. [0x0007, { name: 'convert', category: '' }],
  1685. [0x0008, { name: 'copy', category: '' }],
  1686. [0x0009, { name: 'cumsum', category: '' }],
  1687. [0x000A, { name: 'dequantize', category: 'Quantization' }],
  1688. [0x000B, { name: 'equal', category: '' }],
  1689. [0x000C, { name: 'gather', category: 'Transform' }],
  1690. [0x000D, { name: 'gather_nd', category: 'Transform' }],
  1691. [0x000E, { name: 'hardmax', category: 'Activation' }],
  1692. [0x000F, { name: 'logistic', category: 'Activation' }],
  1693. [0x0010, { name: 'lut1d', category: '' }],
  1694. [0x0011, { name: 'matmul', category: 'Layer' }],
  1695. [0x0012, { name: 'onehot', category: '' }],
  1696. [0x0013, { name: 'pad', category: '' }],
  1697. [0x0014, { name: 'quantize', category: 'Quantization' }],
  1698. [0x0015, { name: 'random_normal', category: '' }],
  1699. [0x0016, { name: 'random_uniform', category: '' }],
  1700. [0x0017, { name: 'reduce', category: 'Reduce' }],
  1701. [0x0018, { name: 'reduce_arg', category: 'Reduce' }],
  1702. [0x0019, { name: 'reduce_prod', category: 'Reduce' }],
  1703. [0x001A, { name: 'reduce_window2d', category: 'Pool' }],
  1704. [0x001B, { name: 'resize_image', category: 'Transform' }],
  1705. [0x001C, { name: 'roi_align', category: '' }],
  1706. [0x001D, { name: 'sigmoid', category: 'Activation' }],
  1707. [0x001E, { name: 'slice', category: 'Tensor' }],
  1708. [0x001F, { name: 'softmax', category: 'Activation' }],
  1709. [0x0020, { name: 'space_to_batch', category: 'Transform' }],
  1710. [0x0021, { name: 'take', category: '' }],
  1711. [0x0022, { name: 'ternary', category: '' }],
  1712. [0x0023, { name: 'topk', category: '' }],
  1713. [0x0024, { name: 'transpose', category: 'Transform' }],
  1714. [0x0025, { name: 'trilu', category: '' }],
  1715. [0x0026, { name: 'unary', category: '' }]
  1716. ]);
  1717. }
  1718. operation(operation) {
  1719. switch (operation.name) {
  1720. case 'ldc_i4':
  1721. operation.value = this._reader.int32();
  1722. break;
  1723. case 'ldc_r4':
  1724. operation.imm = this._reader.float32();
  1725. break;
  1726. case 'lea_gp':
  1727. operation.gpid = this._reader.byte();
  1728. operation.offset = this._reader.int32();
  1729. break;
  1730. case 'lea_buffer':
  1731. operation.location = this._reader.byte();
  1732. operation.subres_id = this._reader.byte();
  1733. operation.offset = this._reader.int32();
  1734. break;
  1735. case 'ldarg':
  1736. operation.index = this._reader.uint32();
  1737. break;
  1738. case 'stpaddings':
  1739. operation.rpaddings = this._reader.byte();
  1740. operation.rank = this._reader.byte();
  1741. break;
  1742. case 'stshape':
  1743. operation.rshape = this._reader.byte();
  1744. operation.rank = this._reader.byte();
  1745. break;
  1746. case 'br':
  1747. case 'br_true':
  1748. case 'br_false':
  1749. operation.target = this._reader.int32();
  1750. break;
  1751. case 'call':
  1752. operation.args = this._reader.byte();
  1753. operation.target = this._reader.int32();
  1754. break;
  1755. case 'ecall':
  1756. operation.args = this._reader.byte();
  1757. break;
  1758. case 'extcall':
  1759. operation.args = this._reader.uint16();
  1760. operation.is_prim_func = this._reader.byte() !== 0;
  1761. break;
  1762. case 'cuscall':
  1763. operation.registered_name = this.string();
  1764. operation.fields_size = this._reader.uint32();
  1765. this._reader.skip(operation.fields_size);
  1766. operation.args = this._reader.uint16();
  1767. break;
  1768. case 'tensor': {
  1769. operation.tensor_function = this._reader.uint16();
  1770. const func = this._tensorFunctions.get(operation.tensor_function);
  1771. if (func) {
  1772. operation.tensor_name = func.name;
  1773. operation.tensor_category = func.category;
  1774. }
  1775. this.tensor(operation);
  1776. break;
  1777. }
  1778. default:
  1779. break;
  1780. }
  1781. }
  1782. string() {
  1783. // Read null-terminated string
  1784. const bytes = [];
  1785. let byte = this._reader.byte();
  1786. while (byte !== 0) {
  1787. bytes.push(byte);
  1788. byte = this._reader.byte();
  1789. }
  1790. return new TextDecoder('utf-8').decode(new Uint8Array(bytes));
  1791. }
  1792. tensor(operation) {
  1793. switch (operation.tensor_name) {
  1794. case 'binary':
  1795. operation.datatype = this._reader.byte();
  1796. operation.rshape_src1 = this._reader.byte();
  1797. operation.rstride_src1 = this._reader.byte();
  1798. operation.rshape_src2 = this._reader.byte();
  1799. operation.rstride_src2 = this._reader.byte();
  1800. operation.rstride_dest = this._reader.byte();
  1801. operation.binary_op = this._reader.byte();
  1802. operation.fused_clamp_low = this._reader.float32();
  1803. operation.fused_clamp_high = this._reader.float32();
  1804. break;
  1805. case 'bitcast':
  1806. operation.type = this._reader.byte();
  1807. operation.new_type = this._reader.byte();
  1808. break;
  1809. case 'call':
  1810. operation.function_id = this._reader.uint32();
  1811. operation.module_id = this._reader.uint16();
  1812. operation.num_src = this._reader.byte();
  1813. operation.num_dst = this._reader.byte();
  1814. break;
  1815. case 'cast':
  1816. operation.new_type = this._reader.byte();
  1817. operation.cast_mode = this._reader.uint32();
  1818. break;
  1819. case 'compare':
  1820. operation.compare_op = this._reader.byte();
  1821. break;
  1822. case 'concat':
  1823. operation.axis = this._reader.int32();
  1824. break;
  1825. case 'cumsum':
  1826. operation.datatype = this._reader.byte();
  1827. operation.rshape_src = this._reader.byte();
  1828. operation.axis = this._reader.int32();
  1829. operation.exclusive = this._reader.byte() !== 0;
  1830. operation.reverse = this._reader.byte() !== 0;
  1831. break;
  1832. case 'condition':
  1833. operation.can_fold_const_call = this._reader.byte() !== 0;
  1834. break;
  1835. case 'conv2d':
  1836. operation.datatype = this._reader.byte();
  1837. operation.rshape_src = this._reader.byte();
  1838. operation.rstride_src = this._reader.byte();
  1839. operation.rshape_kernel = this._reader.byte();
  1840. operation.rstride_kernel = this._reader.byte();
  1841. operation.rstride_bias = this._reader.byte();
  1842. operation.rstride_dest = this._reader.byte();
  1843. operation.groups = this._reader.uint16();
  1844. operation.stride_h = this._reader.uint16();
  1845. operation.stride_w = this._reader.uint16();
  1846. operation.dilation_h = this._reader.uint16();
  1847. operation.dilation_w = this._reader.uint16();
  1848. operation.fused_clamp_low = this._reader.float32();
  1849. operation.fused_clamp_high = this._reader.float32();
  1850. break;
  1851. case 'conv2d_transpose':
  1852. operation.pad_mode = this._reader.byte();
  1853. break;
  1854. case 'dequantize':
  1855. operation.target_type = this._reader.byte();
  1856. break;
  1857. case 'fake_dequantize':
  1858. operation.target_type = this._reader.byte();
  1859. break;
  1860. case 'fake_quantize':
  1861. operation.target_type = this._reader.byte();
  1862. break;
  1863. case 'gather':
  1864. operation.axis = this._reader.int32();
  1865. break;
  1866. case 'layer_norm':
  1867. operation.axis = this._reader.int32();
  1868. operation.epsilon = this._reader.float32();
  1869. operation.use_mean = this._reader.byte() !== 0;
  1870. break;
  1871. case 'lstm':
  1872. operation.direction = this._reader.uint32();
  1873. operation.layout = this._reader.uint32();
  1874. operation.activations = this.strings();
  1875. break;
  1876. case 'matmul':
  1877. operation.rshape_src1 = this._reader.byte();
  1878. operation.rshape_src2 = this._reader.byte();
  1879. operation.fused_clamp_low = this._reader.float32();
  1880. operation.fused_clamp_high = this._reader.float32();
  1881. break;
  1882. case 'normal':
  1883. operation.type = this._reader.byte();
  1884. break;
  1885. case 'random_normal':
  1886. operation.datatype_dest = this._reader.byte();
  1887. operation.rshape_dest = this._reader.byte();
  1888. operation.mean = this._reader.float32();
  1889. operation.std = this._reader.float32();
  1890. operation.seed = this._reader.float32();
  1891. break;
  1892. case 'normal_like':
  1893. operation.type = this._reader.byte();
  1894. break;
  1895. case 'one_hot':
  1896. operation.one_hot_mode = this._reader.byte();
  1897. break;
  1898. case 'pad':
  1899. operation.datatype = this._reader.byte();
  1900. operation.rshape_src = this._reader.byte();
  1901. operation.rstride_src = this._reader.byte();
  1902. operation.rstride_dest = this._reader.byte();
  1903. operation.rpaddings = this._reader.byte();
  1904. operation.pad_mode = this._reader.byte();
  1905. break;
  1906. case 'quantize':
  1907. operation.target_type = this._reader.byte();
  1908. break;
  1909. case 'quant_param_of':
  1910. operation.quant_mode = this._reader.uint32();
  1911. break;
  1912. case 'range_of':
  1913. operation.is_range_of_weight = this._reader.byte() !== 0;
  1914. break;
  1915. case 'reduce':
  1916. operation.reduce_op = this._reader.byte();
  1917. break;
  1918. case 'reduce_arg':
  1919. operation.reduce_arg_op = this._reader.byte();
  1920. operation.dest_type = this._reader.byte();
  1921. break;
  1922. case 'reduce_window2d':
  1923. operation.reduce_op = this._reader.byte();
  1924. break;
  1925. case 'require':
  1926. operation.message = this.string();
  1927. operation.can_fold_const_call = this._reader.byte() !== 0;
  1928. break;
  1929. case 'resize_image':
  1930. operation.resize_mode = this._reader.byte();
  1931. operation.transformation_mode = this._reader.uint32();
  1932. operation.nearest_mode = this._reader.uint32();
  1933. operation.is_tfresize = this._reader.byte() !== 0;
  1934. break;
  1935. case 'unary':
  1936. operation.unary_op = this._reader.byte();
  1937. break;
  1938. case 'uniform':
  1939. operation.type = this._reader.byte();
  1940. break;
  1941. case 'uniform_like':
  1942. operation.type = this._reader.byte();
  1943. break;
  1944. case 'where':
  1945. operation.is_tf_where = this._reader.byte() !== 0;
  1946. break;
  1947. default:
  1948. break;
  1949. }
  1950. }
  1951. };
  1952. kmodel.BytecodeReader.v6 = class extends kmodel.BytecodeReader.v5 {
  1953. constructor(reader) {
  1954. super(reader);
  1955. this._opcodes = new Map([
  1956. [0, 'nop'], [1, 'ldnull'], [2, 'ldc_i4'], [3, 'ldc_i4_0'], [4, 'ldc_i4_1'],
  1957. [5, 'ldc_r4'], [6, 'ldind_i1'], [7, 'ldind_i2'], [8, 'ldind_i4'], [9, 'ldind_i'],
  1958. [10, 'ldind_u1'], [11, 'ldind_u2'], [12, 'ldind_u4'], [13, 'ldind_u'],
  1959. [14, 'ldind_br2'], [15, 'ldind_r4'], [16, 'stind_i1'], [17, 'stind_i2'],
  1960. [18, 'stind_i4'], [19, 'stind_i'], [20, 'stind_br2'], [21, 'stind_r4'],
  1961. [22, 'lea_gp'], [23, 'ldelem_i1'], [24, 'ldelem_i2'], [25, 'ldelem_i4'],
  1962. [26, 'ldelem_i'], [27, 'ldelem_u1'], [28, 'ldelem_u2'], [29, 'ldelem_u4'],
  1963. [30, 'ldelem_u'], [31, 'ldelem_br2'], [32, 'ldelem_r4'], [33, 'stelem_i1'],
  1964. [34, 'stelem_i2'], [35, 'stelem_i4'], [36, 'stelem_i'], [37, 'stelem_br2'],
  1965. [38, 'stelem_r4'], [39, 'ldarg'], [40, 'ldarg_0'], [41, 'ldarg_1'],
  1966. [42, 'ldarg_2'], [43, 'ldarg_3'], [44, 'ldarg_4'], [45, 'ldarg_5'],
  1967. [46, 'dup'], [47, 'pop'], [48, 'ldlocal'], [49, 'stlocal'], [50, 'ldtuple_elem'],
  1968. [51, 'ldtuple'], [52, 'lddatatype'], [53, 'ldtensor'], [54, 'ldscalar'],
  1969. [55, 'neg'], [56, 'add'], [57, 'sub'], [58, 'mul'], [59, 'div'], [60, 'div_u'],
  1970. [61, 'rem'], [62, 'rem_u'], [63, 'and'], [64, 'or'], [65, 'xor'], [66, 'not'],
  1971. [67, 'shl'], [68, 'shr'], [69, 'shr_u'], [70, 'clt'], [71, 'clt_u'],
  1972. [72, 'cle'], [73, 'cle_u'], [74, 'ceq'], [75, 'cge'], [76, 'cge_u'],
  1973. [77, 'cgt'], [78, 'cgt_u'], [79, 'cne'], [80, 'conv_i1'], [81, 'conv_i2'],
  1974. [82, 'conv_i4'], [83, 'conv_i'], [84, 'conv_u1'], [85, 'conv_u2'],
  1975. [86, 'conv_u4'], [87, 'conv_u'], [88, 'conv_br2'], [89, 'conv_r4'],
  1976. [90, 'br'], [91, 'br_true'], [92, 'br_false'], [93, 'ret'], [94, 'call'],
  1977. [95, 'ecall'], [96, 'extcall'], [97, 'cuscall'], [98, 'throw'], [99, 'break'],
  1978. [100, 'tensor']
  1979. ]);
  1980. this._tensorFunctions = new Map([
  1981. [0, { name: 'batch_normalization', category: 'Normalization' }],
  1982. [1, { name: 'batch_to_space', category: 'Transform' }],
  1983. [2, { name: 'binary', category: '' }],
  1984. [3, { name: 'bitcast', category: '' }],
  1985. [4, { name: 'broadcast', category: '' }],
  1986. [5, { name: 'broadcast_shape', category: 'Shape' }],
  1987. [6, { name: 'bucket_pad', category: '' }],
  1988. [7, { name: 'cast', category: '' }],
  1989. [8, { name: 'celu', category: 'Activation' }],
  1990. [9, { name: 'clamp', category: 'Activation' }],
  1991. [10, { name: 'compare', category: '' }],
  1992. [11, { name: 'concat', category: 'Tensor' }],
  1993. [12, { name: 'condition', category: '' }],
  1994. [13, { name: 'constant_of_shape', category: '' }],
  1995. [14, { name: 'conv2d', category: 'Layer' }],
  1996. [15, { name: 'conv2d_shape', category: 'Shape' }],
  1997. [16, { name: 'conv2d_transpose', category: 'Layer' }],
  1998. [17, { name: 'conv2d_transpose_shape', category: 'Shape' }],
  1999. [18, { name: 'cum_sum', category: '' }],
  2000. [19, { name: 'dequantize', category: 'Quantization' }],
  2001. [20, { name: 'elu', category: 'Activation' }],
  2002. [21, { name: 'erf', category: 'Activation' }],
  2003. [22, { name: 'expand', category: '' }],
  2004. [23, { name: 'fake_dequantize', category: 'Quantization' }],
  2005. [24, { name: 'fake_quantize', category: 'Quantization' }],
  2006. [25, { name: 'fix_shape', category: 'Shape' }],
  2007. [26, { name: 'flatten', category: 'Shape' }],
  2008. [27, { name: 'gather', category: 'Transform' }],
  2009. [28, { name: 'gather_elements', category: 'Transform' }],
  2010. [29, { name: 'gather_nd', category: 'Transform' }],
  2011. [30, { name: 'gelu', category: 'Activation' }],
  2012. [31, { name: 'get_item', category: '' }],
  2013. [32, { name: 'get_paddings', category: '' }],
  2014. [33, { name: 'hardmax', category: 'Activation' }],
  2015. [34, { name: 'hard_sigmoid', category: 'Activation' }],
  2016. [35, { name: 'hard_swish', category: 'Activation' }],
  2017. [36, { name: 'index_of', category: '' }],
  2018. [37, { name: 'instance_normalization', category: 'Normalization' }],
  2019. [38, { name: 'l2_normalization', category: 'Normalization' }],
  2020. [39, { name: 'layer_norm', category: 'Normalization' }],
  2021. [40, { name: 'leaky_relu', category: 'Activation' }],
  2022. [41, { name: 'log_softmax', category: 'Activation' }],
  2023. [42, { name: 'lp_normalization', category: 'Normalization' }],
  2024. [43, { name: 'lrn', category: 'Normalization' }],
  2025. [44, { name: 'lstm', category: 'Layer' }],
  2026. [45, { name: 'mat_mul', category: 'Layer' }],
  2027. [46, { name: 'mat_mul_shape', category: 'Shape' }],
  2028. [47, { name: 'normal' }],
  2029. [48, { name: 'normal_like' }],
  2030. [49, { name: 'one_hot', category: '' }],
  2031. [50, { name: 'pad', category: '' }],
  2032. [51, { name: 'prelu', category: 'Activation' }],
  2033. [52, { name: 'prod', category: '' }],
  2034. [53, { name: 'quantize', category: 'Quantization' }],
  2035. [54, { name: 'quant_param_of', category: 'Quantization' }],
  2036. [55, { name: 'range', category: '' }],
  2037. [56, { name: 'range_of', category: '' }],
  2038. [57, { name: 'rank', category: 'Shape' }],
  2039. [58, { name: 'reduce', category: 'Reduce' }],
  2040. [59, { name: 'reduce_arg', category: 'Reduce' }],
  2041. [60, { name: 'reduce_window2d', category: 'Pool' }],
  2042. [61, { name: 'relu', category: 'Activation' }],
  2043. [62, { name: 'relu6', category: 'Activation' }],
  2044. [63, { name: 'require', category: '' }],
  2045. [64, { name: 'reshape', category: 'Shape' }],
  2046. [65, { name: 'reshape_shape', category: 'Shape' }],
  2047. [66, { name: 'resize_image', category: 'Transform' }],
  2048. [67, { name: 'reverse_sequence', category: '' }],
  2049. [68, { name: 'scatter_nd', category: 'Transform' }],
  2050. [69, { name: 'select', category: '' }],
  2051. [70, { name: 'selu', category: 'Activation' }],
  2052. [71, { name: 'shape_of', category: 'Shape' }],
  2053. [72, { name: 'sigmoid', category: 'Activation' }],
  2054. [73, { name: 'size_of', category: 'Shape' }],
  2055. [74, { name: 'slice', category: 'Tensor' }],
  2056. [75, { name: 'softmax', category: 'Activation' }],
  2057. [76, { name: 'softplus', category: 'Activation' }],
  2058. [77, { name: 'softsign', category: 'Activation' }],
  2059. [78, { name: 'space_to_batch', category: 'Transform' }],
  2060. [79, { name: 'split', category: 'Tensor' }],
  2061. [80, { name: 'squeeze', category: 'Shape' }],
  2062. [81, { name: 'squeeze_shape', category: 'Shape' }],
  2063. [82, { name: 'stack', category: 'Tensor' }],
  2064. [83, { name: 'swish', category: 'Activation' }],
  2065. [84, { name: 'tile', category: '' }],
  2066. [85, { name: 'top_k', category: '' }],
  2067. [86, { name: 'transpose', category: 'Transform' }],
  2068. [87, { name: 'transpose_shape', category: 'Shape' }],
  2069. [88, { name: 'trilu', category: '' }],
  2070. [89, { name: 'unary', category: '' }],
  2071. [90, { name: 'uniform' }],
  2072. [91, { name: 'uniform_like' }],
  2073. [92, { name: 'unsqueeze', category: 'Shape' }],
  2074. [93, { name: 'unsqueeze_shape', category: 'Shape' }],
  2075. [94, { name: 'where', category: '' }]
  2076. ]);
  2077. }
  2078. operation(operation) {
  2079. switch (operation.opcode) {
  2080. case 'ldarg':
  2081. operation.index = this._reader.uint16();
  2082. break;
  2083. case 'call':
  2084. operation.args = this._reader.uint16();
  2085. operation.target = this._reader.int32();
  2086. break;
  2087. case 'ecall':
  2088. operation.args = this._reader.uint16();
  2089. break;
  2090. case 'ldlocal':
  2091. case 'stlocal':
  2092. operation.index = this._reader.uint16();
  2093. break;
  2094. default:
  2095. super.operation(operation);
  2096. }
  2097. }
  2098. tensor(operation) {
  2099. switch (operation.tensor_name) {
  2100. case 'binary':
  2101. operation.binary_op = this._reader.byte();
  2102. break;
  2103. case 'matmul':
  2104. break;
  2105. case 'normal':
  2106. operation.type = this._reader.byte();
  2107. break;
  2108. case 'random_normal':
  2109. operation.type = this._reader.byte();
  2110. break;
  2111. case 'normal_like':
  2112. operation.type = this._reader.byte();
  2113. break;
  2114. case 'one_hot':
  2115. operation.one_hot_mode = this._reader.byte();
  2116. break;
  2117. case 'pad':
  2118. operation.pad_mode = this._reader.byte();
  2119. break;
  2120. case 'cumsum':
  2121. break;
  2122. case 'conv2d':
  2123. operation.pad_mode = this._reader.byte();
  2124. break;
  2125. default:
  2126. super.tensor(operation);
  2127. }
  2128. }
  2129. };
  2130. kmodel.Error = class extends Error {
  2131. constructor(message) {
  2132. super(message);
  2133. this.name = 'Error loading kmodel.';
  2134. }
  2135. };
  2136. export const ModelFactory = kmodel.ModelFactory;