onnx.js 91 KB

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