onnx.js 110 KB

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