onnx.js 110 KB

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