onnx.js 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342
  1. var onnx = onnx || {};
  2. var protobuf = protobuf || require('./protobuf');
  3. var flatbuffers = flatbuffers || require('./flatbuffers');
  4. var text = text || require('./text');
  5. onnx.ModelFactory = class {
  6. match(context) {
  7. const identifier = context.identifier;
  8. const extension = identifier.split('.').pop().toLowerCase();
  9. if (identifier.endsWith('saved_model.pb') || identifier.endsWith('predict_net.pb') || identifier.endsWith('init_net.pb')) {
  10. return undefined;
  11. }
  12. if (identifier.endsWith('predict_net.pbtxt') || identifier.endsWith('predict_net.prototxt') ||
  13. identifier.endsWith('init_net.pbtxt') || identifier.endsWith('init_net.prototxt')) {
  14. return undefined;
  15. }
  16. let tags = context.tags('pb');
  17. if (tags.size > 0) {
  18. if (tags.size === 1 && tags.get(1) === 2) {
  19. const tags = context.tags('pb+');
  20. const match = (tags, schema) => {
  21. for (const pair of schema) {
  22. const key = pair[0];
  23. const inner = pair[1];
  24. const value = tags[key];
  25. if (value === undefined) {
  26. continue;
  27. }
  28. if (inner === false) {
  29. return false;
  30. }
  31. if (Array.isArray(inner)) {
  32. if (typeof value !== 'object' || !match(value, inner)) {
  33. return false;
  34. }
  35. }
  36. else if (inner !== value) {
  37. if (inner === 2 && !Array.isArray(value) && Object(value) === (value) && Object.keys(value).length === 0) {
  38. return true;
  39. }
  40. return false;
  41. }
  42. }
  43. return true;
  44. };
  45. // mediapipe.BoxDetectorIndex
  46. if (match(tags, [[1,[[1,[[1,[[1,5],[2,5],[3,5],[4,5],[6,0],[7,5],[8,5],[10,5],[11,0],[12,0]]],[2,5],[3,[]]]],[2,false],[3,false],[4,false],[5,false]]],[2,false],[3,false]] )) {
  47. return undefined;
  48. }
  49. // third_party.tensorflow.python.keras.protobuf.SavedMetadata
  50. if (match(tags, [[1,[[1,[[1,0],[2,0]]],[2,0],[3,2],[4,2],[5,2]]]])) {
  51. return undefined;
  52. }
  53. }
  54. if (Array.from(tags.keys()).every((tag) => tag <= 100) &&
  55. Array.from(tags.values()).every((type) => type < 5)) {
  56. // TensorProto
  57. if (tags.get(1) === 0 && tags.get(2) === 0) {
  58. const schema = [[1,0],[2,0],[4,2],[5,2],[7,2],[8,2],[9,2]];
  59. if (schema.every((pair) => !tags.has(pair[0]) || tags.get(pair[0]) === pair[1])) {
  60. return 'onnx.pb.TensorProto';
  61. }
  62. }
  63. // GraphProto
  64. if (tags.get(1) === 2) {
  65. const schema = [[1,2],[2,2],[3,2],[4,2],[5,2],[6,0],[7,0],[8,2],[9,2],[10,2],[11,2],[12,2],[13,2],[14,2]];
  66. if (schema.every((pair) => !tags.has(pair[0]) || tags.get(pair[0]) === pair[1])) {
  67. const decode = (buffer, value) => {
  68. const reader = protobuf.BinaryReader.open(buffer);
  69. const length = reader.length;
  70. while (reader.position < length) {
  71. const tag = reader.uint32();
  72. const number = tag >>> 3;
  73. const type = tag & 7;
  74. if (value === number) {
  75. return type === 2 ? reader.bytes() : null;
  76. }
  77. else {
  78. reader.skipType(type);
  79. }
  80. }
  81. return null;
  82. };
  83. const stream = context.stream;
  84. const buffer = stream.peek();
  85. const nodeBuffer = decode(buffer, 1);
  86. if (nodeBuffer) {
  87. const nameBuffer = decode(nodeBuffer, 4);
  88. if (nameBuffer && nameBuffer.every((c) => c > 0x20 && c < 0x7f)) {
  89. return 'onnx.pb.GraphProto';
  90. }
  91. }
  92. }
  93. }
  94. // ModelProto
  95. if (tags.get(7) === 2) {
  96. const schema = [[1,0],[2,2],[3,2],[4,2][5,0],[6,2],[7,2],[8,2],[14,2],[20,2]];
  97. if (schema.every((pair) => !tags.has(pair[0]) || tags.get(pair[0]) === pair[1])) {
  98. return 'onnx.pb.ModelProto';
  99. }
  100. }
  101. }
  102. }
  103. const stream = context.stream;
  104. if (stream.length > 5) {
  105. const buffer = stream.peek(Math.min(stream.length, 32));
  106. if (buffer[0] === 0x08 && buffer[1] < 0x0A && buffer[2] === 0x12) {
  107. const producers = [
  108. 'backend-test', 'BrainwaveCompiler',
  109. 'CNTK',
  110. 'keras2onnx', 'Kneron', 'kneron_formatter', 'kneron_kl530_test_case',
  111. 'darknet to ONNX example',
  112. 'htshinichi',
  113. 'MATLAB Deep Learning Toolbox Converter for ONNX Model Format', 'ML.NET', 'MVTec Software',
  114. 'onnx-caffe2', 'onnx-example', 'onnx.quantize', 'onnx.utils.extract_model', 'OnnxMLTools', 'onnx_test', 'onnxruntime-tools', 'onnxruntime.transformers',
  115. 'PaddlePaddle', 'pytorch',
  116. 'sclblonnx', 'skl2onnx',
  117. 'Tencent YouTu', 'tf2onnx', 'tflite2onnx',
  118. 'WinMLTools'
  119. ];
  120. if (producers.some((producer) => Array.from(producer).every((ch, index) => index + 4 < buffer.length && ch.charCodeAt(0) === buffer[index + 4]))) {
  121. return 'onnx.pb.ModelProto';
  122. }
  123. }
  124. }
  125. tags = context.tags('pbtxt');
  126. if (tags.has('ir_version')) {
  127. return 'onnx.pbtxt.ModelProto';
  128. }
  129. if (tags.has('graph') && extension !== 'model') {
  130. return 'onnx.pbtxt.ModelProto';
  131. }
  132. if (context.tags('flatbuffers').get('file_identifier') === 'ORTM') {
  133. return 'onnx.flatbuffers';
  134. }
  135. if (onnx.TextReader.open(stream)) {
  136. return 'onnx.text';
  137. }
  138. return undefined;
  139. }
  140. open(context, match) {
  141. const open = (model, format) => {
  142. return onnx.Metadata.open(context).then((metadata) => {
  143. return new onnx.Model(metadata, model, format);
  144. });
  145. };
  146. switch (match) {
  147. case 'onnx.pbtxt.ModelProto':
  148. return context.require('./onnx-proto').then(() => {
  149. try {
  150. onnx.proto = protobuf.get('onnx').onnx;
  151. const stream = context.stream;
  152. const reader = protobuf.TextReader.open(stream);
  153. const model = onnx.proto.ModelProto.decodeText(reader);
  154. const format = 'ONNX' + (model.ir_version ? ' v' + model.ir_version.toString() : '');
  155. return open(model, format);
  156. }
  157. catch (error) {
  158. const message = error && error.message ? error.message : error.toString();
  159. throw new onnx.Error('File text format is not onnx.ModelProto (' + message.replace(/\.$/, '') + ').');
  160. }
  161. });
  162. case 'onnx.pb.TensorProto':
  163. return context.require('./onnx-proto').then(() => {
  164. // TensorProto
  165. // input_0.pb, output_0.pb
  166. try {
  167. onnx.proto = protobuf.get('onnx').onnx;
  168. const stream = context.stream;
  169. const reader = protobuf.BinaryReader.open(stream);
  170. const tensor = onnx.proto.TensorProto.decode(reader);
  171. tensor.name = tensor.name || context.identifier;
  172. const model = new onnx.proto.ModelProto();
  173. model.graph = new onnx.proto.GraphProto();
  174. model.graph.initializer = [ tensor ];
  175. model.graph.value_info = [ new onnx.proto.ValueInfoProto() ];
  176. model.graph.value_info[0].name = tensor.name;
  177. model.graph.node = [ new onnx.proto.NodeProto() ];
  178. model.graph.node[0].op_type = 'Constant';
  179. model.graph.node[0].attribute = [ new onnx.proto.AttributeProto() ];
  180. model.graph.node[0].attribute[0].name = 'value';
  181. model.graph.node[0].attribute[0].type = onnx.AttributeType.TENSOR;
  182. model.graph.node[0].attribute[0].t = tensor;
  183. const format = 'ONNX Tensor';
  184. return open(model, format);
  185. }
  186. catch (error) {
  187. const message = error && error.message ? error.message : error.toString();
  188. throw new onnx.Error('File format is not onnx.TensorProto (' + message.replace(/\.$/, '') + ').');
  189. }
  190. });
  191. case 'onnx.pb.GraphProto':
  192. return context.require('./onnx-proto').then(() => {
  193. // GraphProto
  194. try {
  195. onnx.proto = protobuf.get('onnx').onnx;
  196. const stream = context.stream;
  197. const reader = protobuf.BinaryReader.open(stream);
  198. const model = new onnx.proto.ModelProto();
  199. model.graph = onnx.proto.GraphProto.decode(reader);
  200. const format = 'ONNX';
  201. return open(model, format);
  202. }
  203. catch (error) {
  204. const message = error && error.message ? error.message : error.toString();
  205. throw new onnx.Error('File format is not onnx.GraphProto (' + message.replace(/\.$/, '') + ').');
  206. }
  207. });
  208. case 'onnx.pb.ModelProto':
  209. return context.require('./onnx-proto').then(() => {
  210. // ModelProto
  211. try {
  212. onnx.proto = protobuf.get('onnx').onnx;
  213. const stream = context.stream;
  214. const reader = protobuf.BinaryReader.open(stream);
  215. const model = onnx.proto.ModelProto.decode(reader);
  216. const format = 'ONNX' + (model.ir_version ? ' v' + model.ir_version.toString() : '');
  217. return open(model, format);
  218. }
  219. catch (error) {
  220. const message = error && error.message ? error.message : error.toString();
  221. throw new onnx.Error('File format is not onnx.ModelProto (' + message.replace(/\.$/, '') + ').');
  222. }
  223. });
  224. case 'onnx.flatbuffers': {
  225. return context.require('./onnx-schema').then((/* schema */) => {
  226. try {
  227. onnx.schema = flatbuffers.get('ort').onnxruntime.fbs;
  228. const stream = context.stream;
  229. const reader = flatbuffers.BinaryReader.open(stream);
  230. const session = onnx.schema.InferenceSession.create(reader);
  231. const model = session.model;
  232. const graph = model.graph;
  233. graph.node = graph.nodes;
  234. graph.doc_string = model.graph_doc_string;
  235. graph.value_info = graph.node_args;
  236. graph.input = graph.inputs.map((input) => {
  237. return { name: input };
  238. });
  239. graph.output = graph.outputs.map((output) => {
  240. return { name: output };
  241. });
  242. graph.initializer = graph.initializers.map((tensor) => {
  243. tensor.data_location = onnx.DataLocation.DEFAULT;
  244. return tensor;
  245. });
  246. graph.sparse_initializer = graph.sparse_initializers.map((tensor) => {
  247. tensor.values.data_location = onnx.DataLocation.DEFAULT;
  248. tensor.indices.data_location = onnx.DataLocation.DEFAULT;
  249. return tensor;
  250. });
  251. delete graph.nodes;
  252. delete graph.node_args;
  253. delete graph.inputs;
  254. delete graph.outputs;
  255. delete graph.initializers;
  256. delete graph.sparse_initializers;
  257. delete model.graph_doc_string;
  258. for (const node of graph.node) {
  259. node.input = node.inputs;
  260. node.output = node.outputs;
  261. node.attribute = node.attributes;
  262. delete node.inputs;
  263. delete node.outputs;
  264. delete node.attributes;
  265. }
  266. const format = 'ONNX Runtime' + (model.ir_version ? ' v' + model.ir_version.toString() : '');
  267. return open(model, format);
  268. }
  269. catch (error) {
  270. const message = error && error.message ? error.message : error.toString();
  271. throw new onnx.Error('File format is not ort.Model (' + message.replace(/\.$/, '') + ').');
  272. }
  273. });
  274. }
  275. case 'onnx.text': {
  276. return context.require('./onnx-proto').then(() => {
  277. try {
  278. onnx.proto = protobuf.get('onnx').onnx;
  279. const stream = context.stream;
  280. const reader = onnx.TextReader.open(stream);
  281. const model = reader.read();
  282. const format = 'ONNX Text' + (model.ir_version ? ' v' + model.ir_version.toString() : '');
  283. return open(model, format);
  284. }
  285. catch (error) {
  286. const message = error && error.message ? error.message : error.toString();
  287. throw new onnx.Error('File format is not onnx.ModelProto (' + message.replace(/\.$/, '') + ').');
  288. }
  289. });
  290. }
  291. default: {
  292. throw new onnx.Error("Unknown ONNX format '" + match + "'.");
  293. }
  294. }
  295. }
  296. };
  297. onnx.Model = class {
  298. constructor(metadata, model, format) {
  299. this._graphs = [];
  300. this._format = format;
  301. this._producer = model.producer_name && model.producer_name.length > 0 ? model.producer_name + (model.producer_version && model.producer_version.length > 0 ? ' ' + model.producer_version : '') : null;
  302. this._domain = model.domain;
  303. this._modelVersion = model.model_version;
  304. this._description = model.doc_string;
  305. this._metadata = [];
  306. this._imports = null;
  307. const imports = new Map();
  308. if (model.opset_import && model.opset_import.length > 0) {
  309. for (const opset_import of model.opset_import) {
  310. const domain = opset_import.domain || 'ai.onnx';
  311. const version = opset_import.version ? typeof opset_import.version === 'number' ? opset_import.version: opset_import.version.toNumber() : 0;
  312. if (!imports.has(domain) || imports.get(domain) > version) {
  313. imports.set(domain, version);
  314. }
  315. }
  316. this._imports = Array.from(imports).map((pair) => pair[0] + ' v' + pair[1].toString());
  317. }
  318. if (imports.size == 0) {
  319. imports.set('ai.onnx', 1);
  320. imports.set('ai.onnx.ml', 1);
  321. }
  322. let imageFormat = '';
  323. if (model.metadata_props) {
  324. const imageMetadata = {};
  325. for (const metadata_prop of model.metadata_props) {
  326. switch (metadata_prop.key) {
  327. case 'author':
  328. this._author = metadata_prop.value;
  329. break;
  330. case 'company':
  331. this._company = metadata_prop.value;
  332. break;
  333. case 'converted_from':
  334. this._converted_from = metadata_prop.value;
  335. break;
  336. case 'license':
  337. this._license = metadata_prop.value;
  338. break;
  339. case 'license_url':
  340. this._licenseUrl = metadata_prop.value;
  341. break;
  342. case 'Image.BitmapPixelFormat':
  343. case 'Image.ColorSpaceGamma':
  344. case 'Image.NominalPixelRange':
  345. imageMetadata[metadata_prop.key] = metadata_prop.value;
  346. break;
  347. default:
  348. this._metadata.push({ name: metadata_prop.key, value: metadata_prop.value});
  349. break;
  350. }
  351. }
  352. imageFormat = [ imageMetadata['Image.BitmapPixelFormat'], imageMetadata['Image.ColorSpaceGamma'], imageMetadata['Image.NominalPixelRange'] ].filter((item) => item);
  353. }
  354. this._graphs = [];
  355. if (model && model.graph) {
  356. const graphMetadata = new onnx.GraphMetadata(metadata, imports);
  357. const context = new onnx.ModelContext(graphMetadata, imageFormat);
  358. for (const func of model.functions || []) {
  359. context.metadata.add(new onnx.Function(context, func));
  360. }
  361. const graphs = [ model.graph ];
  362. while (graphs.length > 0) {
  363. const graph = graphs.shift();
  364. this._graphs.push(context.graph(graph));
  365. for (const node of graph.node || []) {
  366. for (const attribute of node.attribute || []) {
  367. if (attribute.g) {
  368. graphs.push(attribute.g);
  369. }
  370. else if (attribute.graphs && attribute.graphs.length > 0) {
  371. graphs.push(...attribute.graphs);
  372. }
  373. }
  374. }
  375. }
  376. }
  377. }
  378. get format() {
  379. return this._format;
  380. }
  381. get imports() {
  382. return this._imports;
  383. }
  384. get producer() {
  385. return this._producer;
  386. }
  387. get domain() {
  388. return this._domain || null;
  389. }
  390. get description() {
  391. return this._description || null;
  392. }
  393. get author() {
  394. return this._author || null;
  395. }
  396. get company() {
  397. return this._company || null;
  398. }
  399. get source() {
  400. return this._converted_from || null;
  401. }
  402. get license() {
  403. const license = [];
  404. if (this._license && this._license.length > 0) {
  405. license.push(this._license);
  406. }
  407. if (this._licenseUrl && this._licenseUrl.length > 0) {
  408. license.push('<a href=\'' + this._licenseUrl + '\'>' + this._licenseUrl + '</a>');
  409. }
  410. if (license.length > 0) {
  411. return license;
  412. }
  413. return null;
  414. }
  415. get metadata() {
  416. return this._metadata;
  417. }
  418. get graphs() {
  419. return this._graphs;
  420. }
  421. };
  422. onnx.Graph = class {
  423. constructor(context, graph) {
  424. this._node = '';
  425. this._description = '';
  426. this._nodes = [];
  427. this._inputs = [];
  428. this._outputs = [];
  429. this._name = graph.name || null;
  430. this._description = graph.doc_string || '';
  431. context = new onnx.GraphContext(context, graph.node);
  432. for (const initializer of graph.initializer) {
  433. const tensor = context.tensor(initializer.name);
  434. tensor.initializer = new onnx.Tensor(context, initializer, 'Initializer');
  435. }
  436. for (const sparse_initializer of graph.sparse_initializer) {
  437. const tensor = context.tensor(sparse_initializer.values.name);
  438. tensor.initializer = new onnx.Tensor(context, sparse_initializer, 'Sparse Initializer');
  439. }
  440. for (const tensor_annotation of graph.quantization_annotation || []) {
  441. const tensor = context.tensor(tensor_annotation.tensor_name);
  442. const annotation = {};
  443. for (const pair of tensor_annotation.quant_parameter_tensor_names) {
  444. annotation[pair.key] = pair.value;
  445. }
  446. tensor.annotation = annotation;
  447. }
  448. for (const valueInfo of graph.value_info) {
  449. const tensor = context.tensor(valueInfo.name);
  450. tensor.type = context.createType(valueInfo.type);
  451. tensor.description = valueInfo.doc_string;
  452. }
  453. graph.input = graph.input.map((valueInfo) => {
  454. const tensor = context.tensor(valueInfo.name);
  455. tensor.type = context.createType(valueInfo.type);
  456. tensor.description = valueInfo.doc_string;
  457. return tensor;
  458. });
  459. graph.output = graph.output.map((valueInfo) => {
  460. const tensor = context.tensor(valueInfo.name);
  461. tensor.type = context.createType(valueInfo.type);
  462. tensor.description = valueInfo.doc_string;
  463. return tensor;
  464. });
  465. new onnx.Inference(graph.node, graph.output);
  466. context.push(graph.node, graph.input, graph.output);
  467. this._nodes = context.pop();
  468. for (const input of graph.input) {
  469. const argument = context.argument(input.name);
  470. if (!argument.initializer) {
  471. this._inputs.push(new onnx.Parameter(input.name, [ argument ]));
  472. }
  473. }
  474. for (const output of graph.output) {
  475. const argument = context.argument(output.name);
  476. if (!argument.initializer) {
  477. this._outputs.push(new onnx.Parameter(output.name, [ argument ]));
  478. }
  479. }
  480. }
  481. get name() {
  482. return this._name;
  483. }
  484. get description() {
  485. return this._description;
  486. }
  487. get inputs() {
  488. return this._inputs;
  489. }
  490. get outputs() {
  491. return this._outputs;
  492. }
  493. get nodes() {
  494. return this._nodes;
  495. }
  496. toString() {
  497. return 'graph(' + this.name + ')';
  498. }
  499. };
  500. onnx.Parameter = class {
  501. constructor(name, args) {
  502. this._name = name;
  503. this._arguments = args;
  504. }
  505. get name() {
  506. return this._name;
  507. }
  508. get visible() {
  509. return true;
  510. }
  511. get arguments() {
  512. return this._arguments;
  513. }
  514. };
  515. onnx.Argument = class {
  516. constructor(name, type, initializer, annotation, description) {
  517. if (typeof name !== 'string') {
  518. throw new onnx.Error("Invalid argument identifier '" + JSON.stringify(name) + "'.");
  519. }
  520. this._name = name;
  521. this._type = type || null;
  522. this._initializer = initializer || null;
  523. this._annotation = annotation;
  524. this._description = description || '';
  525. }
  526. get name() {
  527. return this._name;
  528. }
  529. get type() {
  530. return this._type;
  531. }
  532. get description() {
  533. return this._description;
  534. }
  535. get quantization() {
  536. if (this._annotation) {
  537. return Object.keys(this._annotation).map((key) => key + ': ' + this._annotation[key]).join(', ');
  538. }
  539. return null;
  540. }
  541. get initializer() {
  542. return this._initializer;
  543. }
  544. };
  545. onnx.Node = class {
  546. constructor(context, op_type, domain, name, description, attributes, inputs, outputs) {
  547. this._type = context.metadata.type(op_type, domain) || { name: op_type, module: domain };
  548. if (this.type.module !== domain && !(this._type instanceof onnx.Function)) {
  549. this._type = Object.assign({}, this.type);
  550. this._type.name = op_type;
  551. this._type.module = domain;
  552. }
  553. this._name = name || '';
  554. this._description = description || '';
  555. this._inputs = inputs;
  556. this._outputs = outputs;
  557. this._attributes = (attributes || []).map((attribute) => new onnx.Attribute(context, op_type, domain, attribute));
  558. }
  559. get type() {
  560. return this._type;
  561. }
  562. get name() {
  563. return this._name;
  564. }
  565. get description() {
  566. return this._description;
  567. }
  568. get attributes() {
  569. return this._attributes;
  570. }
  571. get inputs() {
  572. return this._inputs;
  573. }
  574. get outputs() {
  575. return this._outputs;
  576. }
  577. };
  578. onnx.Attribute = class {
  579. constructor(context, op_type, domain, attribute) {
  580. this._name = attribute.name;
  581. this._description = attribute.doc_string || '';
  582. this._type = null;
  583. this._value = null;
  584. switch (attribute.type) {
  585. case onnx.AttributeType.FLOAT:
  586. this._value = attribute.f;
  587. this._type = 'float32';
  588. break;
  589. case onnx.AttributeType.INT:
  590. this._value = attribute.i;
  591. this._type = 'int64';
  592. break;
  593. case onnx.AttributeType.STRING:
  594. switch (op_type) {
  595. case 'Int8GivenTensorFill':
  596. this._value = Array.from(attribute.s);
  597. break;
  598. default:
  599. this._value = context.decodeText(attribute.s);
  600. break;
  601. }
  602. this._type = 'string';
  603. break;
  604. case onnx.AttributeType.TENSOR:
  605. this._value = new onnx.Tensor(context, attribute.t);
  606. this._type = 'tensor';
  607. break;
  608. case onnx.AttributeType.GRAPH:
  609. this._value = context.graph(attribute.g);
  610. this._type = 'graph';
  611. break;
  612. case onnx.AttributeType.FLOATS:
  613. this._value = ArrayBuffer.isView(attribute.floats) ? Array.from(attribute.floats) : attribute.floats;
  614. this._type = 'float32[]';
  615. break;
  616. case onnx.AttributeType.INTS:
  617. this._value = ArrayBuffer.isView(attribute.ints) ? Array.from(attribute.ints) : attribute.ints;
  618. this._type = 'int64[]';
  619. break;
  620. case onnx.AttributeType.STRINGS:
  621. this._value = attribute.strings.map((s) => context.decodeText(s));
  622. this._type = 'string[]';
  623. break;
  624. case onnx.AttributeType.TENSORS:
  625. this._value = attribute.tensors.map((tensor) => new onnx.Tensor(context, tensor));
  626. this._type = 'tensor[]';
  627. break;
  628. case onnx.AttributeType.GRAPHS:
  629. this._value = attribute.graphs.map((graph) => context.graph(graph));
  630. this._type = 'graph[]';
  631. break;
  632. case onnx.AttributeType.SPARSE_TENSOR:
  633. this._value = new onnx.Tensor(context, attribute.sparse_tensor);
  634. this._type = 'tensor';
  635. break;
  636. case onnx.AttributeType.SPARSE_TENSORS:
  637. this._value = attribute.sparse_tensors.map((tensor) => new onnx.Tensor(context, tensor));
  638. this._type = 'tensor[]';
  639. break;
  640. case onnx.AttributeType.TYPE_PROTO:
  641. this._value = context.createType(attribute.tp);
  642. this._type = 'type';
  643. break;
  644. case onnx.AttributeType.TYPE_PROTOS:
  645. this._value = attribute.type_protos.map((type) => context.createType(type));
  646. this._type = 'type[]';
  647. break;
  648. default:
  649. throw new onnx.Error("Unknown attribute type '" + attribute.type + "'.");
  650. }
  651. const metadata = context.metadata.attribute(op_type, domain, attribute.name);
  652. if (metadata) {
  653. if (Object.prototype.hasOwnProperty.call(metadata, 'default') && this._value == metadata.default) {
  654. this._visible = false;
  655. }
  656. if (metadata.type === 'DataType') {
  657. this._type = metadata.type;
  658. const value = this._value ? parseInt(this._value.toString(), 10) : this._value;
  659. this._value = Number.isInteger(value) ? context.createDataType(value) : value;
  660. }
  661. }
  662. }
  663. get name() {
  664. return this._name;
  665. }
  666. get type() {
  667. return this._type;
  668. }
  669. get value() {
  670. return this._value;
  671. }
  672. get description() {
  673. return this._description;
  674. }
  675. get visible() {
  676. return this._visible == false ? false : true;
  677. }
  678. };
  679. onnx.Group = class {
  680. constructor(name, groups) {
  681. this._type = { name: 'Scope' };
  682. this._name = name;
  683. this._nodes = [];
  684. for (const entry of groups) {
  685. const key = entry[0];
  686. if (key === '') {
  687. for (const node of entry[1]) {
  688. this._nodes.push(node);
  689. }
  690. }
  691. else {
  692. this._nodes.push(new onnx.Group(name === '' ? key : name + '/' + key, entry[1]));
  693. }
  694. }
  695. const set = new Set();
  696. const inputs = new Array();
  697. const outputs = new Array();
  698. for (const node of this._nodes) {
  699. if (node instanceof onnx.Group) {
  700. node.freeze();
  701. }
  702. for (const parameter of node.outputs) {
  703. for (const argument of parameter.arguments) {
  704. if (!argument.initializer) {
  705. outputs.push(argument);
  706. set.add(argument.name);
  707. }
  708. }
  709. }
  710. }
  711. for (const node of this._nodes) {
  712. for (const parameter of node.inputs) {
  713. for (const argument of parameter.arguments) {
  714. if (!set.has(argument.name) && !argument.initializer) {
  715. inputs.push(argument);
  716. }
  717. }
  718. }
  719. }
  720. this._inputs = [ new onnx.Parameter('inputs', inputs) ];
  721. this._outputs = [ new onnx.Parameter('outputs', outputs) ];
  722. this._attributes = [];
  723. }
  724. get name() {
  725. return this._name;
  726. }
  727. get type() {
  728. return this._type;
  729. }
  730. get inputs() {
  731. return this._inputs;
  732. }
  733. get outputs() {
  734. return this._outputs;
  735. }
  736. get attributes() {
  737. return this._attributes;
  738. }
  739. get nodes() {
  740. return this._nodes;
  741. }
  742. };
  743. onnx.Tensor = class {
  744. constructor(context, tensor, kind) {
  745. this._kind = kind || null;
  746. const data = (tensor) => {
  747. let data = undefined;
  748. if (tensor.data_location === onnx.DataLocation.DEFAULT) {
  749. switch (tensor.data_type) {
  750. case onnx.DataType.FLOAT16:
  751. if (tensor.int32_data && tensor.int32_data.length > 0) {
  752. const buffer = new Uint8Array(tensor.int32_data.length << 1);
  753. const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
  754. const array = tensor.int32_data;
  755. for (let i = 0; i < array.length; i++) {
  756. view.setUint16(i << 1, array[i], true);
  757. }
  758. data = {
  759. type: tensor.data_type,
  760. buffer: buffer
  761. };
  762. }
  763. break;
  764. case onnx.DataType.FLOAT:
  765. data = new Float32Array(tensor.float_data);
  766. break;
  767. case onnx.DataType.DOUBLE:
  768. data = new Float64Array(tensor.double_data);
  769. break;
  770. case onnx.DataType.BOOL:
  771. if (tensor.int32_data && tensor.int32_data.length > 0) {
  772. const array = tensor.int32_data;
  773. data = new Array(array.length);
  774. for (let i = 0; i < data.length; i++) {
  775. data[i] = array[i] === 0 ? false : true;
  776. }
  777. }
  778. break;
  779. case onnx.DataType.INT8:
  780. data = new Int8Array(tensor.int32_data);
  781. break;
  782. case onnx.DataType.UINT8:
  783. data = new Uint8Array(tensor.int32_data);
  784. break;
  785. case onnx.DataType.INT16:
  786. data = new Int32Array(tensor.int32_data);
  787. break;
  788. case onnx.DataType.UINT16:
  789. data = new Int32Array(tensor.int32_data);
  790. break;
  791. case onnx.DataType.INT32:
  792. data = new Int32Array(tensor.int32_data);
  793. break;
  794. case onnx.DataType.UINT32:
  795. case onnx.DataType.UINT64:
  796. data = tensor.uint64_data;
  797. break;
  798. case onnx.DataType.INT64:
  799. data = tensor.int64_data;
  800. break;
  801. }
  802. if (data && (Array.isArray(data) || ArrayBuffer.isView(data)) && data.length === 0) {
  803. data = undefined;
  804. }
  805. if (!data && tensor.raw_data && tensor.raw_data.length > 0) {
  806. data = {
  807. type: tensor.data_type,
  808. buffer: tensor.raw_data
  809. };
  810. }
  811. }
  812. return data;
  813. };
  814. if ((onnx.proto && tensor instanceof onnx.proto.SparseTensorProto) ||
  815. (onnx.schema && tensor instanceof onnx.schema.SparseTensor)) {
  816. this._name = tensor.values.name || '';
  817. this._type = context.createTensorType(tensor.values.data_type, tensor.dims.map((dim) => dim), null);
  818. this._location = Array.from(new Set([ context.createLocation(tensor.values.data_location), context.createLocation(tensor.indices.data_location) ])).join(':');
  819. this._values = data(tensor.values);
  820. this._indices = data(tensor.indices);
  821. }
  822. else {
  823. this._name = tensor.name || '';
  824. this._type = context.createTensorType(tensor.data_type, tensor.dims.map((dim) => dim), null);
  825. this._location = context.createLocation(tensor.data_location);
  826. this._values = data(tensor);
  827. }
  828. }
  829. get name() {
  830. return this._name;
  831. }
  832. get kind() {
  833. return this._kind;
  834. }
  835. get type() {
  836. return this._type;
  837. }
  838. get state() {
  839. return this._context().state || null;
  840. }
  841. get value() {
  842. const context = this._context();
  843. if (context.state) {
  844. return null;
  845. }
  846. context.limit = Number.MAX_SAFE_INTEGER;
  847. return this._decode(context, 0);
  848. }
  849. toString() {
  850. const context = this._context();
  851. if (context.state) {
  852. return '';
  853. }
  854. context.limit = 10000;
  855. const value = this._decode(context, 0);
  856. return onnx.Tensor._stringify(value, '', ' ');
  857. }
  858. _context() {
  859. const context = {};
  860. context.state = null;
  861. if (this._sparse) {
  862. context.state = 'Sparse data not implemented.';
  863. return context;
  864. }
  865. if (this._location !== 'default') {
  866. context.state = "Data '" + this._location + "' location not implemented.";
  867. return context;
  868. }
  869. const decode = (data) => {
  870. if (!data || Array.isArray(data) || ArrayBuffer.isView(data)) {
  871. return data;
  872. }
  873. const buffer = data.buffer;
  874. const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
  875. const type = data.type;
  876. data = undefined;
  877. switch (type) {
  878. case onnx.DataType.BOOL:
  879. data = new Array(buffer.length);
  880. for (let i = 0; i < buffer.length; i++) {
  881. data[i] = view.getUint8(i) === 0 ? false : true;
  882. }
  883. break;
  884. case onnx.DataType.FLOAT16:
  885. data = new Float32Array(buffer.length >> 1);
  886. for (let i = 0; i < data.length; i++) {
  887. data[i] = view.getFloat16(i << 1, true);
  888. }
  889. break;
  890. case onnx.DataType.FLOAT:
  891. data = new Float32Array(buffer.length >> 2);
  892. for (let i = 0; i < data.length; i++) {
  893. data[i] = view.getFloat32(i << 2, true);
  894. }
  895. break;
  896. case onnx.DataType.DOUBLE:
  897. data = new Float64Array(buffer.length >> 3);
  898. for (let i = 0; i < data.length; i++) {
  899. data[i] = view.getFloat64(i << 3, true);
  900. }
  901. break;
  902. case onnx.DataType.INT8:
  903. data = new Int8Array(buffer.length);
  904. for (let i = 0; i < data.length; i++) {
  905. data[i] = view.getInt8(i, true);
  906. }
  907. break;
  908. case onnx.DataType.UINT8:
  909. data = new Uint8Array(buffer.length);
  910. for (let i = 0; i < data.length; i++) {
  911. data[i] = view.getUint8(i, true);
  912. }
  913. break;
  914. case onnx.DataType.INT16:
  915. data = new Int16Array(buffer.length >> 1);
  916. for (let i = 0; i < data.length; i++) {
  917. data[i] = view.getInt16(i << 1, true);
  918. }
  919. break;
  920. case onnx.DataType.UINT16:
  921. data = new Uint16Array(buffer.length >> 1);
  922. for (let i = 0; i < data.length; i++) {
  923. data[i] = view.getUint16(i << 1, true);
  924. }
  925. break;
  926. case onnx.DataType.INT32:
  927. data = new Int32Array(buffer.length >> 2);
  928. for (let i = 0; i < data.length; i++) {
  929. data[i] = view.getInt32(i << 2, true);
  930. }
  931. break;
  932. case onnx.DataType.UINT32:
  933. data = new Uint32Array(buffer.length >> 2);
  934. for (let i = 0; i < data.length; i++) {
  935. data[i] = view.getUint32(i << 2, true);
  936. }
  937. break;
  938. case onnx.DataType.INT64:
  939. data = new Array(buffer.length >> 3);
  940. for (let i = 0; i < data.length; i++) {
  941. data[i] = view.getInt64(i << 3, true);
  942. }
  943. break;
  944. case onnx.DataType.UINT64:
  945. data = new Array(buffer.length >> 3);
  946. for (let i = 0; i < data.length; i++) {
  947. data[i] = view.getUint64(i << 3, true);
  948. }
  949. break;
  950. }
  951. return data;
  952. };
  953. this._values = decode(this._values);
  954. if (!this._values) {
  955. context.state = 'Tensor data is empty.';
  956. return context;
  957. }
  958. this._indices = decode(this._indices);
  959. context.values = this._values;
  960. context.indices = this._indices;
  961. context.index = 0;
  962. context.dataType = this.type.dataType;
  963. context.shape = this.type.shape.dimensions;
  964. context.data = function() {
  965. if (!this._data) {
  966. if (this.indices && this.values && this.indices.length === this.values.length) {
  967. const size = context.shape.reduce((a, b) => a * b, 1);
  968. const indices = this.indices;
  969. const values = this.values;
  970. const array = new values.constructor(size);
  971. switch (this.dataType) {
  972. case 'boolean':
  973. array.fill(false);
  974. break;
  975. case 'int64':
  976. case 'uint64':
  977. break;
  978. }
  979. if (indices.length > 0) {
  980. if (Object.prototype.hasOwnProperty.call(indices[0], 'low')) {
  981. for (let i = 0; i < indices.length; i++) {
  982. const index = indices[i];
  983. array[index.high === 0 ? index.low : index.toNumber()] = values[i];
  984. }
  985. }
  986. else {
  987. for (let i = 0; i < indices.length; i++) {
  988. array[indices[i]] = values[i];
  989. }
  990. }
  991. }
  992. this._data = array;
  993. }
  994. else {
  995. this._data = this.values;
  996. }
  997. }
  998. return this._data;
  999. };
  1000. return context;
  1001. }
  1002. _decode(context, dimension) {
  1003. const shape = context.shape.length !== 0 ? context.shape : [ 1 ];
  1004. const results = [];
  1005. const size = shape[dimension];
  1006. const data = context.data();
  1007. if (dimension == shape.length - 1) {
  1008. for (let i = 0; i < size; i++) {
  1009. if (context.index > context.limit) {
  1010. results.push('...');
  1011. return results;
  1012. }
  1013. results.push(data[context.index++]);
  1014. }
  1015. }
  1016. else {
  1017. for (let j = 0; j < size; j++) {
  1018. if (context.index > context.limit) {
  1019. results.push('...');
  1020. return results;
  1021. }
  1022. results.push(this._decode(context, dimension + 1));
  1023. }
  1024. }
  1025. if (context.shape.length == 0) {
  1026. return results[0];
  1027. }
  1028. return results;
  1029. }
  1030. static _stringify(value, indentation, indent) {
  1031. if (Array.isArray(value)) {
  1032. const result = [];
  1033. result.push(indentation + '[');
  1034. const items = value.map((item) => onnx.Tensor._stringify(item, indentation + indent, indent));
  1035. if (items.length > 0) {
  1036. result.push(items.join(',\n'));
  1037. }
  1038. result.push(indentation + ']');
  1039. return result.join('\n');
  1040. }
  1041. if (typeof value == 'string') {
  1042. return indentation + value;
  1043. }
  1044. if (value == Infinity) {
  1045. return indentation + 'Infinity';
  1046. }
  1047. if (value == -Infinity) {
  1048. return indentation + '-Infinity';
  1049. }
  1050. if (isNaN(value)) {
  1051. return indentation + 'NaN';
  1052. }
  1053. return indentation + value.toString();
  1054. }
  1055. };
  1056. onnx.TensorType = class {
  1057. constructor(dataType, shape, denotation) {
  1058. this._dataType = dataType;
  1059. this._shape = shape;
  1060. this._denotation = denotation || null;
  1061. }
  1062. get dataType() {
  1063. return this._dataType;
  1064. }
  1065. get shape() {
  1066. return this._shape;
  1067. }
  1068. get denotation() {
  1069. return this._denotation;
  1070. }
  1071. toString() {
  1072. return this.dataType + this._shape.toString();
  1073. }
  1074. };
  1075. onnx.TensorShape = class {
  1076. constructor(dimensions) {
  1077. this._dimensions = dimensions;
  1078. }
  1079. get dimensions() {
  1080. return this._dimensions;
  1081. }
  1082. toString() {
  1083. if (!this._dimensions || this._dimensions.length == 0) {
  1084. return '';
  1085. }
  1086. return '[' + this._dimensions.map((dim) => dim ? dim.toString() : '?').join(',') + ']';
  1087. }
  1088. };
  1089. onnx.SequenceType = class {
  1090. constructor(elementType, denotation) {
  1091. this._elementType = elementType;
  1092. this._denotation = denotation;
  1093. }
  1094. get elementType() {
  1095. return this._elementType;
  1096. }
  1097. get dennotation() {
  1098. return this._dennotation;
  1099. }
  1100. toString() {
  1101. return 'sequence<' + this._elementType.toString() + '>';
  1102. }
  1103. };
  1104. onnx.MapType = class {
  1105. constructor(keyType, valueType, denotation) {
  1106. this._keyType = keyType;
  1107. this._valueType = valueType;
  1108. this._denotation = denotation;
  1109. }
  1110. get keyType() {
  1111. return this._keyType;
  1112. }
  1113. get valueType() {
  1114. return this._valueType;
  1115. }
  1116. get denotation() {
  1117. return this._denotation;
  1118. }
  1119. toString() {
  1120. return 'map<' + this._keyType + ',' + this._valueType.toString() + '>';
  1121. }
  1122. };
  1123. onnx.OpaqueType = class {
  1124. constructor(domain, name) {
  1125. this._domain = domain;
  1126. this._name = name;
  1127. }
  1128. toString() {
  1129. const name = (this._domain ? (this._domain + '.') : '') + this._name;
  1130. return 'opaque<' + name + '>';
  1131. }
  1132. };
  1133. onnx.Function = class {
  1134. constructor(context, func) {
  1135. this._name = func.name;
  1136. this._domain = func.domain;
  1137. this._description = func.doc_string;
  1138. this._inputs = [];
  1139. this._outputs = [];
  1140. this._attributes = func.attribute.map((attribtue) => { return { name: attribtue }; });
  1141. context = new onnx.GraphContext(context, func.node);
  1142. func.input = func.input.map((input) => context.tensor(input));
  1143. func.output = func.output.map((output) => context.tensor(output));
  1144. context.push(func.node, func.input, func.output);
  1145. this._nodes = context.pop();
  1146. for (const input of func.input) {
  1147. const argument = context.argument(input.name);
  1148. if (!argument.initializer) {
  1149. this._inputs.push(new onnx.Parameter(input.name, [ argument ]));
  1150. }
  1151. }
  1152. for (const output of func.output) {
  1153. const argument = context.argument(output.name);
  1154. if (!argument.initializer) {
  1155. this._outputs.push(new onnx.Parameter(output.name, [ argument ]));
  1156. }
  1157. }
  1158. }
  1159. get type() {
  1160. return 'function';
  1161. }
  1162. get name() {
  1163. return this._name;
  1164. }
  1165. get module() {
  1166. return this._domain;
  1167. }
  1168. get description() {
  1169. return this._description;
  1170. }
  1171. get inputs() {
  1172. return this._inputs;
  1173. }
  1174. get outputs() {
  1175. return this._outputs;
  1176. }
  1177. get attributes() {
  1178. return this._attributes;
  1179. }
  1180. get nodes() {
  1181. return this._nodes;
  1182. }
  1183. };
  1184. onnx.GraphMetadata = class {
  1185. constructor(metadata, imports) {
  1186. this._metadata = metadata;
  1187. this._imports = imports;
  1188. this._cache = new Map();
  1189. this._attributes = new Map();
  1190. this._functions = new Map();
  1191. }
  1192. add(func) {
  1193. if (!this._functions.has(func.module)) {
  1194. this._functions.set(func.module, new Map());
  1195. }
  1196. const map = this._functions.get(func.module);
  1197. if (map.has(func.name)) {
  1198. throw new onnx.Error("Duplicate function identifier '" + func.module + '.' + func.name + "'.");
  1199. }
  1200. map.set(func.name, func);
  1201. }
  1202. type(name, domain) {
  1203. domain = domain || 'ai.onnx';
  1204. const key = domain + ':' + name;
  1205. if (!this._cache.has(key)) {
  1206. let value = this._metadata.type(name, domain, this._imports);
  1207. if (!value) {
  1208. if (this._functions.has(domain)) {
  1209. const map = this._functions.get(domain);
  1210. if (map.has(name)) {
  1211. value = map.get(name);
  1212. }
  1213. }
  1214. }
  1215. this._cache.set(key, value);
  1216. }
  1217. return this._cache.get(key);
  1218. }
  1219. attribute(type, domain, name) {
  1220. const key = domain + ':' + type + ':' + name;
  1221. if (!this._attributes.has(key)) {
  1222. const schema = this.type(type, domain);
  1223. if (schema && schema.attributes && schema.attributes.length > 0) {
  1224. for (const attribute of schema.attributes) {
  1225. this._attributes.set(key, attribute);
  1226. }
  1227. }
  1228. if (!this._attributes.has(key)) {
  1229. this._attributes.set(key, null);
  1230. }
  1231. }
  1232. return this._attributes.get(key);
  1233. }
  1234. };
  1235. onnx.Metadata = class {
  1236. static open(context) {
  1237. if (onnx.Metadata._metadata) {
  1238. return Promise.resolve(onnx.Metadata._metadata);
  1239. }
  1240. return context.request('onnx-metadata.json', 'utf-8', null).then((data) => {
  1241. onnx.Metadata._metadata = new onnx.Metadata(data);
  1242. return onnx.Metadata._metadata;
  1243. }).catch(() => {
  1244. onnx.Metadata._metadata = new onnx.Metadata(null);
  1245. return onnx.Metadata._metadata;
  1246. });
  1247. }
  1248. constructor(data) {
  1249. this._map = new Map();
  1250. if (data) {
  1251. const metadata = JSON.parse(data);
  1252. for (const item of metadata) {
  1253. if (!this._map.has(item.module)) {
  1254. this._map.set(item.module, new Map());
  1255. }
  1256. const map = this._map.get(item.module);
  1257. if (!map.has(item.name)) {
  1258. map.set(item.name, []);
  1259. }
  1260. map.get(item.name).push(item);
  1261. }
  1262. }
  1263. }
  1264. type(name, domain, imports) {
  1265. domain = domain || 'ai.onnx';
  1266. let current = null;
  1267. if (this._map.has(domain)) {
  1268. const map = this._map.get(domain);
  1269. if (map.has(name)) {
  1270. for (const metadata of map.get(name)) {
  1271. const matchVersion = current ? current.version : -1;
  1272. const importVersion = imports.get(metadata.module) || 0;
  1273. if (importVersion >= metadata.version && matchVersion < metadata.version) {
  1274. current = metadata;
  1275. }
  1276. }
  1277. }
  1278. }
  1279. return current;
  1280. }
  1281. };
  1282. onnx.Inference = class {
  1283. constructor(nodes, outputs) {
  1284. this._outputs = new Map();
  1285. for (const node of nodes) {
  1286. for (const output of node.output) {
  1287. this._outputs.set(output.name, node);
  1288. }
  1289. }
  1290. for (const output of outputs) {
  1291. this._infer(output.name);
  1292. }
  1293. }
  1294. _infer(output) {
  1295. if (this._outputs.has(output)) {
  1296. let hasInputShapes = true;
  1297. const node = this._outputs.get(output);
  1298. for (const input of node.input) {
  1299. if (!input.type) {
  1300. this._infer(input);
  1301. if (!input.type) {
  1302. hasInputShapes = false;
  1303. break;
  1304. }
  1305. }
  1306. }
  1307. if (hasInputShapes) {
  1308. // continue
  1309. }
  1310. }
  1311. }
  1312. };
  1313. onnx.DataLocation = {
  1314. DEFAULT: 0,
  1315. EXTERNAL: 1
  1316. };
  1317. onnx.DataType = {
  1318. UNDEFINED: 0,
  1319. FLOAT: 1,
  1320. UINT8: 2,
  1321. INT8: 3,
  1322. UINT16: 4,
  1323. INT16: 5,
  1324. INT32: 6,
  1325. INT64: 7,
  1326. STRING: 8,
  1327. BOOL: 9,
  1328. FLOAT16: 10,
  1329. DOUBLE: 11,
  1330. UINT32: 12,
  1331. UINT64: 13,
  1332. COMPLEX64: 14,
  1333. COMPLEX128: 15,
  1334. BFLOAT16: 16
  1335. };
  1336. onnx.AttributeType = {
  1337. UNDEFINED: 0,
  1338. FLOAT: 1,
  1339. INT: 2,
  1340. STRING: 3,
  1341. TENSOR: 4,
  1342. GRAPH: 5,
  1343. FLOATS: 6,
  1344. INTS: 7,
  1345. STRINGS: 8,
  1346. TENSORS: 9,
  1347. GRAPHS: 10,
  1348. SPARSE_TENSOR: 11,
  1349. SPARSE_TENSORS: 12,
  1350. TYPE_PROTO: 13,
  1351. TYPE_PROTOS: 14
  1352. };
  1353. onnx.ModelContext = class {
  1354. constructor(metadata, imageFormat) {
  1355. this._metadata = metadata;
  1356. this._imageFormat = imageFormat;
  1357. this._graphs = new Map();
  1358. }
  1359. get metadata() {
  1360. return this._metadata;
  1361. }
  1362. get imageFormat() {
  1363. return this._imageFormat;
  1364. }
  1365. graph(value) {
  1366. if (!this._graphs.has(value)) {
  1367. this._graphs.set(value, new onnx.Graph(this, value));
  1368. }
  1369. return this._graphs.get(value);
  1370. }
  1371. };
  1372. onnx.GraphContext = class {
  1373. constructor(context, nodes) {
  1374. this._context = context;
  1375. this._decoder = new TextDecoder('utf-8');
  1376. this._dataTypes = new Map(Object.entries(onnx.DataType).map((entry) => [ entry[1], entry[0].toLowerCase() ]));
  1377. this._dataTypes.set(onnx.DataType.UNDEFINED, 'UNDEFINED');
  1378. this._dataTypes.set(onnx.DataType.BOOL, 'boolean');
  1379. this._dataTypes.set(onnx.DataType.FLOAT, 'float32');
  1380. this._dataTypes.set(onnx.DataType.DOUBLE, 'float64');
  1381. this._tensors = new Map();
  1382. this._arguments = new Map();
  1383. this._groups = new Map();
  1384. this._nodes = [];
  1385. for (const node of nodes) {
  1386. node.input = node.input.map((name) => this.tensor(name));
  1387. node.output = node.output.map((name) => this.tensor(name));
  1388. node.param = {};
  1389. for (const attribute of node.attribute) {
  1390. if (attribute.type) {
  1391. continue;
  1392. }
  1393. if (attribute.ints && attribute.ints.length > 0) {
  1394. attribute.type = onnx.AttributeType.INTS;
  1395. }
  1396. else if (attribute.floats && attribute.floats.length > 0) {
  1397. attribute.type = onnx.AttributeType.FLOATS;
  1398. }
  1399. else if (attribute.strings && attribute.strings.length > 0) {
  1400. attribute.type = onnx.AttributeType.STRINGS;
  1401. }
  1402. else if (attribute.graphs && attribute.graphs.length > 0) {
  1403. attribute.type = onnx.AttributeType.GRAPHS;
  1404. }
  1405. else if (attribute.s && attribute.s.length > 0) {
  1406. attribute.type = onnx.AttributeType.STRING;
  1407. }
  1408. else if (Object.prototype.hasOwnProperty.call(attribute, 'f')) {
  1409. attribute.type = onnx.AttributeType.FLOAT;
  1410. }
  1411. else if (Object.prototype.hasOwnProperty.call(attribute, 'i')) {
  1412. attribute.type = onnx.AttributeType.INT;
  1413. }
  1414. else if (Object.prototype.hasOwnProperty.call(attribute, 't')) {
  1415. attribute.type = onnx.AttributeType.TENSOR;
  1416. }
  1417. else if (Object.prototype.hasOwnProperty.call(attribute, 'g')) {
  1418. attribute.type = onnx.AttributeType.GRAPH;
  1419. }
  1420. else if (Object.prototype.hasOwnProperty.call(attribute, 'sparse_tensor')) {
  1421. attribute.type =onnx.AttributeType.SPARSE_TENSOR;
  1422. }
  1423. else {
  1424. attribute.type = onnx.AttributeType.UNDEFINED;
  1425. }
  1426. }
  1427. }
  1428. }
  1429. get metadata() {
  1430. return this._context.metadata;
  1431. }
  1432. graph(name) {
  1433. return this._context.graph(name);
  1434. }
  1435. tensor(name) {
  1436. if (!this._tensors.has(name)) {
  1437. this._tensors.set(name, { name: name });
  1438. }
  1439. return this._tensors.get(name);
  1440. }
  1441. group(name) {
  1442. if (!this._groups.has(name)) {
  1443. const path = name.split('/');
  1444. if (path.length > 1) {
  1445. path.pop();
  1446. return this.group(path.join('/'));
  1447. }
  1448. this._groups.set(name, new Map([ [ '', [] ]]));
  1449. }
  1450. return this._groups.get(name);
  1451. }
  1452. argument(name) {
  1453. if (!this._arguments.has(name)) {
  1454. const tensor = this.tensor(name);
  1455. const type = tensor.initializer ? tensor.initializer.type : tensor.type || null;
  1456. this._arguments.set(name, new onnx.Argument(name, type, tensor.initializer, tensor.annotation, tensor.description));
  1457. }
  1458. return this._arguments.get(name);
  1459. }
  1460. createType(type) {
  1461. if (!type) {
  1462. return null;
  1463. }
  1464. let denotation = '';
  1465. switch (type.denotation) {
  1466. case 'TENSOR':
  1467. denotation = 'Tensor';
  1468. break;
  1469. case 'IMAGE':
  1470. denotation = 'Image' + (this._context.imageFormat ? '(' + this._context.imageFormat.join(',') + ')' : '');
  1471. break;
  1472. case 'AUDIO':
  1473. denotation = 'Audio';
  1474. break;
  1475. case 'TEXT':
  1476. denotation = 'Text';
  1477. break;
  1478. }
  1479. switch (type.value) {
  1480. case 'tensor_type': {
  1481. const tensor_type = type.tensor_type;
  1482. let shape = [];
  1483. if (tensor_type.shape && tensor_type.shape.dim) {
  1484. shape = tensor_type.shape.dim.map((dim) => dim.dim_param ? dim.dim_param : dim.dim_value ? dim.dim_value : null);
  1485. }
  1486. return this.createTensorType(tensor_type.elem_type, shape, denotation);
  1487. }
  1488. case 'sparse_tensor_type': {
  1489. const tensor_type = type.sparse_tensor_type;
  1490. let shape = [];
  1491. if (tensor_type.shape && tensor_type.shape.dim) {
  1492. shape = tensor_type.shape.dim.map((dim) => dim.dim_param ? dim.dim_param : dim.dim_value);
  1493. }
  1494. return this.createTensorType(tensor_type.elem_type, shape, denotation);
  1495. }
  1496. case 'map_type': {
  1497. return this.createMapType(type.map_type.key_type, this.createType(type.map_type.value_type), denotation);
  1498. }
  1499. case 'sequence_type': {
  1500. return new onnx.SequenceType(this.createType(type.sequence_type.elem_type), denotation);
  1501. }
  1502. case 'opaque_type': {
  1503. return new onnx.OpaqueType(type.opaque_type.domain, type.opaque_type.name);
  1504. }
  1505. }
  1506. return null;
  1507. }
  1508. createTensorType(dataType, shape, denotation) {
  1509. dataType = this.createDataType(dataType);
  1510. return new onnx.TensorType(dataType, new onnx.TensorShape(shape), denotation);
  1511. }
  1512. createMapType(keyType, valueType, denotation) {
  1513. keyType = this.createDataType(keyType);
  1514. return new onnx.MapType(keyType, valueType, denotation);
  1515. }
  1516. createDataType(value) {
  1517. return this._dataTypes.has(value) ? this._dataTypes.get(value) : this._dataTypes.get(onnx.DataType.UNDEFINED);
  1518. }
  1519. createLocation(value) {
  1520. switch (value) {
  1521. case onnx.DataLocation.DEFAULT: return 'default';
  1522. case onnx.DataLocation.EXTERNAL: return 'external';
  1523. }
  1524. return 'UNDEFINED';
  1525. }
  1526. decodeText(value) {
  1527. if (typeof value === 'string') {
  1528. return value;
  1529. }
  1530. return this._decoder.decode(value);
  1531. }
  1532. push(nodes, inputs, outputs) {
  1533. const inputMap = new Map();
  1534. const outputMap = new Map();
  1535. for (const node of nodes) {
  1536. node.input.every((input) => inputMap.set(input.name, (inputMap.get(input) || 0) + 1));
  1537. node.output.every((output) => outputMap.set(output.name, (outputMap.get(output) || 0) + 1));
  1538. }
  1539. inputs.every((input) => inputMap.delete(input.name));
  1540. outputs.every((output) => outputMap.delete(output.name));
  1541. nodes = nodes.filter((node) => {
  1542. const constant = node &&
  1543. node.op_type === 'Constant' &&
  1544. node.attribute.length === 1 && node.attribute[0] &&
  1545. node.input.length === 0 &&
  1546. node.output.length === 1 && node.output[0] && inputMap.get(node.output[0].name) === 1 && outputMap.get(node.output[0].name) === 1;
  1547. const attribute = constant ? node.attribute[0] : null;
  1548. if (attribute && attribute.name === 'value' && attribute.type === onnx.AttributeType.TENSOR && attribute.t) {
  1549. const tensor = this.tensor(node.output[0].name);
  1550. tensor.initializer = new onnx.Tensor(this, attribute.t, 'Constant');
  1551. return false;
  1552. }
  1553. else if (attribute && attribute.name === 'sparse_value' && attribute.type === onnx.AttributeType.SPARSE_TENSOR && attribute.sparse_tensor) {
  1554. const tensor = this.tensor(node.output[0].name);
  1555. tensor.initializer = new onnx.Tensor(this, attribute.sparse_tensor, 'Sparse Constant');
  1556. return false;
  1557. }
  1558. return true;
  1559. });
  1560. for (let node of nodes) {
  1561. const schema = this._context.metadata.type(node.op_type, node.domain);
  1562. const inputs = [];
  1563. node.input = node.input || [];
  1564. for (let i = 0; i < node.input.length; ) {
  1565. const input = schema && schema.inputs && i < schema.inputs.length ? schema.inputs[i] : { name: i.toString() };
  1566. const count = input.list ? node.input.length - i : 1;
  1567. const list = node.input.slice(i, i + count).map((input) => this.argument(input.name));
  1568. inputs.push(new onnx.Parameter(input.name, list));
  1569. i += count;
  1570. }
  1571. const outputs = [];
  1572. node.output = node.output || [];
  1573. for (let i = 0; i < node.output.length; ) {
  1574. const output = schema && schema.outputs && i < schema.outputs.length ? schema.outputs[i] : { name: i.toString() };
  1575. const count = output.list ? node.output.length - i : 1;
  1576. const list = node.output.slice(i, i + count).map((output) => this.argument(output.name));
  1577. outputs.push(new onnx.Parameter(output.name, list));
  1578. i += count;
  1579. }
  1580. node = new onnx.Node(this, node.op_type, node.domain, node.name, node.doc_string, node.attribute, inputs, outputs);
  1581. this._nodes.push(node);
  1582. // const path = (node.name || '').split('/');
  1583. // path.pop();
  1584. // this.group(path.join('/')).get('').push(node);
  1585. }
  1586. }
  1587. pop() {
  1588. /*
  1589. const nodes = [];
  1590. for (const entry of this._groups) {
  1591. if (entry[0] === '') {
  1592. for (const node of entry[1].get('')) {
  1593. nodes.push(node);
  1594. }
  1595. continue;
  1596. }
  1597. nodes.push(new onnx.Group(entry[0], entry[1]));
  1598. }
  1599. return nodes;
  1600. */
  1601. return this._nodes;
  1602. }
  1603. };
  1604. onnx.TextReader = class {
  1605. static open(data) {
  1606. try {
  1607. const reader = text.Reader.open(data);
  1608. const lines = [];
  1609. for (let i = 0; i < 32; i++) {
  1610. const line = reader.read();
  1611. if (line === undefined) {
  1612. break;
  1613. }
  1614. lines.push(line);
  1615. }
  1616. const content = lines.join('\n');
  1617. if (/^\s*<\s*ir_version\s*:/m.exec(content)) {
  1618. return new onnx.TextReader(data);
  1619. }
  1620. }
  1621. catch (err) {
  1622. // continue regardless of error
  1623. }
  1624. return null;
  1625. }
  1626. constructor(data) {
  1627. this._data = data;
  1628. this._dataTypes = new Map([
  1629. [ 'float', 1 ], [ 'uint8', 2 ], [ 'int8', 3 ], [ 'uint16', 4 ],
  1630. [ 'int16', 5 ], [ 'int32', 6 ], [ 'int64', 7 ], [ 'string', 8 ],
  1631. [ 'bool', 9 ], [ 'float16', 10 ], [ 'double', 11 ], [ 'uint32', 12 ],
  1632. [ 'uint64', 13 ], [ 'complex64', 14 ], [ 'complex128', 15 ], [ 'bfloat16', 16 ]
  1633. ]);
  1634. this._attributeTypes = new Map([
  1635. [ 'float', 1 ], [ 'int', 2 ], [ 'string', 3 ],
  1636. [ 'tensor', 4 ], [ 'graph', 5 ], [ 'sparse_tensor', 11 ], [ 'type_proto', 13 ],
  1637. [ 'floats', 6 ], [ 'ints', 7 ], [ 'strings', 8 ],
  1638. [ 'tensors', 9 ], [ 'graphs', 10 ], [ 'sparse_tensors', 12 ], [ 'type_protos', 14 ]
  1639. ]);
  1640. }
  1641. read() {
  1642. const decoder = text.Decoder.open(this._data);
  1643. this._decoder = decoder;
  1644. this._position = 0;
  1645. this._char = decoder.decode();
  1646. return this._model();
  1647. }
  1648. _seek(position) {
  1649. this._decoder.position = position;
  1650. this._char = '';
  1651. this._next();
  1652. }
  1653. _model() {
  1654. this._whitespace();
  1655. const model = new onnx.proto.ModelProto();
  1656. if (this._match('<')) {
  1657. do {
  1658. const keyword = this._identifier();
  1659. this._expect(':');
  1660. switch (keyword) {
  1661. case 'ir_version':
  1662. case 'model_version':
  1663. model[keyword] = this._int64();
  1664. break;
  1665. case 'opset_import':
  1666. model[keyword] = this._operatorSetId();
  1667. break;
  1668. case 'producer_name':
  1669. case 'producer_version':
  1670. case 'domain':
  1671. case 'doc_string':
  1672. model[keyword] = this._string();
  1673. break;
  1674. case 'metadata_props':
  1675. this._expect('[');
  1676. if (!this._match(']')) {
  1677. do {
  1678. const entry = new onnx.proto.StringStringEntryProto();
  1679. entry.key = this._string();
  1680. this._expect(':');
  1681. entry.value = this._string();
  1682. model.metadata_props.push(entry);
  1683. } while (this._match(','));
  1684. this._expect(']');
  1685. }
  1686. break;
  1687. default:
  1688. this._throw("Unknown keyword '" + keyword + "'.");
  1689. break;
  1690. }
  1691. } while (this._match(','));
  1692. this._expect('>');
  1693. }
  1694. model.graph = this._graph();
  1695. this._whitespace();
  1696. while (this._char !== undefined) {
  1697. const func = this._function();
  1698. if (func) {
  1699. model.functions.push(func);
  1700. }
  1701. this._whitespace();
  1702. }
  1703. return model;
  1704. }
  1705. _graph() {
  1706. const graph = new onnx.proto.GraphProto();
  1707. graph.name = this._identifier();
  1708. if (this._match('(')) {
  1709. if (!this._match(')')) {
  1710. do {
  1711. const valueInfo = this._valueInfo();
  1712. if (this._match('=')) {
  1713. this._throw('Initializer not implemented.');
  1714. }
  1715. graph.input.push(valueInfo);
  1716. }
  1717. while (this._match(','));
  1718. this._expect(')');
  1719. }
  1720. }
  1721. this._expect('=>');
  1722. graph.output = this._valueInfoList();
  1723. if (this._match('<')) {
  1724. if (!this._match('>')) {
  1725. do {
  1726. const valueInfo = this._valueInfo();
  1727. if (this._match('=')) {
  1728. this._throw('Initializer not implemented.');
  1729. }
  1730. else {
  1731. graph.value_info.push(valueInfo);
  1732. }
  1733. }
  1734. while (this._match(','));
  1735. this._expect('>');
  1736. }
  1737. }
  1738. graph.node = this._nodeList();
  1739. return graph;
  1740. }
  1741. _nodeList() {
  1742. const list = [];
  1743. this._expect('{');
  1744. while (!this._match('}')) {
  1745. list.push(this._node());
  1746. }
  1747. return list;
  1748. }
  1749. _node() {
  1750. const node = new onnx.proto.NodeProto();
  1751. node.output = this._identifierList();
  1752. this._expect('=');
  1753. let identifier = this._identifier();
  1754. let domain = '';
  1755. while (this._match('.')) {
  1756. if (domain) {
  1757. domain += '.';
  1758. }
  1759. domain += identifier;
  1760. identifier = this._identifier();
  1761. }
  1762. node.domain = domain;
  1763. node.op_type = identifier;
  1764. node.attribute = this._attributeList();
  1765. this._expect('(');
  1766. node.input = this._identifierList();
  1767. this._expect(')');
  1768. if (!node.attribute || node.attribute.length === 0) {
  1769. node.attribute = this._attributeList();
  1770. }
  1771. return node;
  1772. }
  1773. _attributeList() {
  1774. const list = [];
  1775. if (this._match('<')) {
  1776. do {
  1777. list.push(this._attribute());
  1778. }
  1779. while (this._match(','));
  1780. this._expect('>');
  1781. }
  1782. return list;
  1783. }
  1784. _attribute() {
  1785. const attribute = new onnx.proto.AttributeProto();
  1786. attribute.name = this._identifier();
  1787. if (this._match(':')) {
  1788. const type = this._identifier();
  1789. if (!this._attributeTypes.has(type)) {
  1790. this._throw("Unexpected attribute type '" + type + "'.");
  1791. }
  1792. attribute.type = this._attributeTypes.get(type);
  1793. }
  1794. this._expect('=');
  1795. if (this._match('[')) {
  1796. const list = [];
  1797. do {
  1798. list.push(this._literal());
  1799. }
  1800. while (this._match(','));
  1801. this._expect(']');
  1802. if (list.every((value) => typeof value === 'string')) {
  1803. attribute.type = onnx.AttributeType.STRINGS;
  1804. attribute.strings = list;
  1805. }
  1806. else if (list.every((value) => typeof value === 'number' && Number.isInteger(value))) {
  1807. attribute.type = onnx.AttributeType.INTS;
  1808. attribute.ints = list;
  1809. }
  1810. else if (list.every((value) => typeof value === 'number')) {
  1811. attribute.type = onnx.AttributeType.FLOATS;
  1812. attribute.floats = list;
  1813. }
  1814. else {
  1815. this._throw("Unexpected value '" + JSON.stringify(list) + "'.");
  1816. }
  1817. }
  1818. else {
  1819. if ((this._char >= 'a' && this._char <= 'z') || (this._char >= 'A' && this._char <= 'Z') || this._char === '_') {
  1820. const identifier = this._identifier();
  1821. if (this._dataTypes.has(identifier)) {
  1822. attribute.type = onnx.AttributeType.TENSOR;
  1823. attribute.t = this._tensor(identifier);
  1824. }
  1825. else {
  1826. attribute.type = onnx.AttributeType.GRAPH;
  1827. attribute.g = this._graph();
  1828. }
  1829. }
  1830. else if (this._match('@')) {
  1831. attribute.ref_attr_name = this._identifier();
  1832. }
  1833. else {
  1834. const value = this._literal();
  1835. switch (typeof value) {
  1836. case 'number':
  1837. if (Number.isInteger(value)) {
  1838. attribute.type = onnx.AttributeType.INT;
  1839. attribute.i = value;
  1840. }
  1841. else {
  1842. attribute.type = onnx.AttributeType.FLOAT;
  1843. attribute.f = value;
  1844. }
  1845. break;
  1846. case 'string':
  1847. attribute.type = onnx.AttributeType.STRING;
  1848. attribute.s = value;
  1849. break;
  1850. default: {
  1851. this._throw("Unexpected value '" + JSON.stringify(value) + "'.");
  1852. }
  1853. }
  1854. }
  1855. }
  1856. return attribute;
  1857. }
  1858. _valueInfoList() {
  1859. const list = [];
  1860. this._expect('(');
  1861. if (!this._match(')')) {
  1862. do {
  1863. list.push(this._valueInfo());
  1864. } while (this._match(','));
  1865. this._expect(')');
  1866. }
  1867. return list;
  1868. }
  1869. _valueInfo() {
  1870. const valueInfo = new onnx.proto.ValueInfoProto();
  1871. let identifier = this._identifier();
  1872. if (this._dataTypes.has(identifier)) {
  1873. valueInfo.type = this._type(this._dataTypes.get(identifier));
  1874. identifier = this._identifier();
  1875. }
  1876. valueInfo.name = identifier;
  1877. return valueInfo;
  1878. }
  1879. _type(elem_type) {
  1880. const type = new onnx.proto.TypeProto();
  1881. type.elem_type = elem_type;
  1882. if (this._match('[')) {
  1883. if (!this._match(']')) {
  1884. type.shape = this._shape();
  1885. this._expect(']');
  1886. }
  1887. }
  1888. return type;
  1889. }
  1890. _shape() {
  1891. const shape = new onnx.proto.TensorShapeProto();
  1892. do {
  1893. const dimension = new onnx.proto.TensorShapeProto.Dimension();
  1894. if (!this._match('?')) {
  1895. const identifier = this._identifier(true);
  1896. if (identifier) {
  1897. dimension.dim_param = identifier;
  1898. }
  1899. else {
  1900. dimension.dim_value = this._int64();
  1901. }
  1902. }
  1903. shape.dim.push(dimension);
  1904. }
  1905. while (this._match(','));
  1906. return shape;
  1907. }
  1908. _tensor() {
  1909. const tensor = new onnx.proto.TensorProto();
  1910. this._expect('=');
  1911. this._expect('{');
  1912. if (!this._match('}')) {
  1913. do {
  1914. this._next();
  1915. } while (!this._match('}'));
  1916. }
  1917. return tensor;
  1918. }
  1919. _function() {
  1920. const func = new onnx.proto.FunctionProto();
  1921. if (this._match('<')) {
  1922. do {
  1923. const keyword = this._identifier();
  1924. this._expect(':');
  1925. switch (keyword) {
  1926. case 'opset_import':
  1927. func[keyword] = this._operatorSetId();
  1928. break;
  1929. case 'domain':
  1930. case 'doc_string':
  1931. func[keyword] = this._string();
  1932. break;
  1933. default:
  1934. this._throw("Unknown keyword '" + keyword + "'.");
  1935. break;
  1936. }
  1937. }
  1938. while (this._match(','));
  1939. this._expect('>');
  1940. }
  1941. func.name = this._identifier();
  1942. if (this._match('<')) {
  1943. func.attribute = this._identifierList();
  1944. this._expect('>');
  1945. }
  1946. if (this._match('(')) {
  1947. func.input = this._identifierList();
  1948. this._expect(')');
  1949. }
  1950. this._expect('=>');
  1951. if (this._match('(')) {
  1952. func.output = this._identifierList();
  1953. this._expect(')');
  1954. }
  1955. func.node = this._nodeList();
  1956. return func;
  1957. }
  1958. _identifierList() {
  1959. const list = [];
  1960. const identifier = this._identifier(true);
  1961. if (identifier) {
  1962. list.push(identifier);
  1963. while (this._match(',')) {
  1964. list.push(this._identifier());
  1965. }
  1966. }
  1967. return list;
  1968. }
  1969. _identifier(optional) {
  1970. this._whitespace();
  1971. const value = [];
  1972. if ((this._char >= 'a' && this._char <= 'z') || (this._char >= 'A' && this._char <= 'Z')) {
  1973. value.push(this._char);
  1974. this._next();
  1975. while ((this._char >= 'a' && this._char <= 'z') || (this._char >= 'A' && this._char <= 'Z') || (this._char >= '0' && this._char <= '9') || this._char === '_') {
  1976. value.push(this._char);
  1977. this._next();
  1978. }
  1979. }
  1980. if (optional !== true && value.length == 0) {
  1981. this._throw('Identifier expected.');
  1982. }
  1983. return value.join('');
  1984. }
  1985. _literal() {
  1986. this._whitespace();
  1987. let decimal_point = false;
  1988. if (this._char === '"') {
  1989. const value = [];
  1990. this._next();
  1991. while (this._char !== undefined && this._char !== '"') {
  1992. value.push(this._char);
  1993. this._next();
  1994. }
  1995. if (this._char !== undefined) {
  1996. this._next();
  1997. }
  1998. return value.join('');
  1999. }
  2000. else if ((this._char >= '0' && this._char <= '9') || this._char === '-') {
  2001. const value = [ this._char ];
  2002. this._next();
  2003. while ((this._char >= '0' && this._char <= '9') || this._char === '.') {
  2004. if (this._char === '.') {
  2005. if (decimal_point) {
  2006. this._throw();
  2007. }
  2008. decimal_point = true;
  2009. }
  2010. value.push(this._char);
  2011. this._next();
  2012. }
  2013. if (value.length === 0) {
  2014. this._throw('Value expected.');
  2015. }
  2016. if (this._char === 'e' || this._char === 'E') {
  2017. decimal_point = true;
  2018. value.push(this._char);
  2019. this._next();
  2020. if (this._char === '+' || this._char === '-') {
  2021. value.push(this._char);
  2022. this._next();
  2023. }
  2024. while ((this._char >= '0' && this._char <= '9')) {
  2025. value.push(this._char);
  2026. this._next();
  2027. }
  2028. }
  2029. return decimal_point ? Number.parseFloat(value.join('')) : Number.parseInt(value.join(''), 10);
  2030. }
  2031. return undefined;
  2032. }
  2033. _int64() {
  2034. const value = this._literal();
  2035. if (!Number.isInteger(value)) {
  2036. this._throw('Integer value expected.');
  2037. }
  2038. return value;
  2039. }
  2040. _string() {
  2041. const value = this._literal();
  2042. if (typeof value !== 'string') {
  2043. this._throw('String value expected.');
  2044. }
  2045. return value;
  2046. }
  2047. _operatorSetId() {
  2048. const list = [];
  2049. this._expect('[');
  2050. if (!this._match(']')) {
  2051. do {
  2052. const value = new onnx.proto.OperatorSetIdProto();
  2053. value.domain = this._string();
  2054. this._expect(':');
  2055. value.version = this._int64();
  2056. list.push(value);
  2057. }
  2058. while (this._match(','));
  2059. this._expect(']');
  2060. }
  2061. return list;
  2062. }
  2063. _match(value) {
  2064. this._whitespace();
  2065. if (this._char !== value[0]) {
  2066. return false;
  2067. }
  2068. if (value.length === 1) {
  2069. this._next();
  2070. return true;
  2071. }
  2072. const position = this._position;
  2073. for (let i = 0; i < value.length; i++) {
  2074. if (this._char !== value[i]) {
  2075. this._seek(position);
  2076. return false;
  2077. }
  2078. this._next();
  2079. }
  2080. return true;
  2081. }
  2082. _expect(value) {
  2083. if (!this._match(value)) {
  2084. this._unexpected();
  2085. }
  2086. return true;
  2087. }
  2088. _whitespace() {
  2089. for (;;) {
  2090. while (this._char === ' ' || this._char === '\n' || this._char === '\r' || this._char === '\t') {
  2091. this._next();
  2092. }
  2093. if (this._char === undefined || this._char !== '#') {
  2094. break;
  2095. }
  2096. while (this._char !== undefined && this._char !== '\n') {
  2097. this._next();
  2098. }
  2099. }
  2100. }
  2101. _next() {
  2102. if (this._char === undefined) {
  2103. this._unexpected();
  2104. }
  2105. this._position = this._decoder.position;
  2106. this._char = this._decoder.decode();
  2107. }
  2108. _unexpected() {
  2109. let c = this._char;
  2110. if (c === undefined) {
  2111. throw new onnx.Error('Unexpected end of input.');
  2112. }
  2113. else if (c === '"') {
  2114. c = 'string';
  2115. }
  2116. else if ((c >= '0' && c <= '9') || c === '-') {
  2117. c = 'number';
  2118. }
  2119. else {
  2120. if (c < ' ' || c > '\x7F') {
  2121. const name = Object.keys(this._escape).filter((key) => this._escape[key] === c);
  2122. c = (name.length === 1) ? '\\' + name : '\\u' + ('000' + c.charCodeAt(0).toString(16)).slice(-4);
  2123. }
  2124. c = "token '" + c + "'";
  2125. }
  2126. this._throw('Unexpected ' + c);
  2127. }
  2128. _throw(message) {
  2129. throw new onnx.Error(message.replace(/\.$/, '') + this._location());
  2130. }
  2131. _location() {
  2132. let line = 1;
  2133. let column = 1;
  2134. this._decoder.position = 0;
  2135. let c;
  2136. do {
  2137. if (this._decoder.position === this._position) {
  2138. return ' at ' + line.toString() + ':' + column.toString() + '.';
  2139. }
  2140. c = this._decoder.decode();
  2141. if (c === '\n') {
  2142. line++;
  2143. column = 1;
  2144. }
  2145. else {
  2146. column++;
  2147. }
  2148. }
  2149. while (c !== undefined);
  2150. return ' at ' + line.toString() + ':' + column.toString() + '.';
  2151. }
  2152. };
  2153. onnx.Error = class extends Error {
  2154. constructor(message) {
  2155. super(message);
  2156. this.name = 'Error loading ONNX model.';
  2157. }
  2158. };
  2159. if (typeof module !== 'undefined' && typeof module.exports === 'object') {
  2160. module.exports.ModelFactory = onnx.ModelFactory;
  2161. }