| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925 |
- import * as protobuf from './protobuf.js';
- const onnx = {};
- onnx.ModelFactory = class {
- async match(context) {
- const identifier = context.identifier;
- const extensions = [
- 'saved_model.pb', 'predict_net.pb', 'init_net.pb',
- 'predict_net.pbtxt', 'init_net.pbtxt', 'predict_net.prototxt', 'init_net.prototxt'
- ];
- if (!extensions.some((extension) => identifier.endsWith(extension))) {
- const entries = [
- onnx.OrtReader,
- onnx.ProtoReader,
- onnx.TextReader,
- onnx.JsonReader,
- onnx.PickleReader,
- onnx.DataReader,
- onnx.MetaReader
- ];
- for (const entry of entries) {
- /* eslint-disable no-await-in-loop */
- const reader = await entry.open(context);
- /* eslint-enable no-await-in-loop */
- if (reader) {
- return context.set(reader.name, reader);
- }
- }
- }
- return null;
- }
- async open(context) {
- const target = context.value;
- await target.read();
- const metadata = await onnx.Metadata.open(context);
- return new onnx.Model(metadata, target);
- }
- filter(context, match) {
- return context.type !== 'onnx.proto' || (match.type !== 'onnx.data' && match.type !== 'onnx.meta' && match.type !== 'dot');
- }
- };
- onnx.Model = class {
- constructor(metadata, target) {
- const model = target.model;
- this._modules = [];
- this._format = target.format;
- 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;
- this._domain = model.domain;
- this._version = typeof model.model_version === 'number' || typeof model.model_version === 'bigint' ? model.model_version.toString() : '';
- this._description = model.doc_string;
- this._metadata = [];
- this._imports = null;
- const imports = new Map();
- if (model.opset_import && model.opset_import.length > 0) {
- for (const opset_import of model.opset_import) {
- const domain = opset_import.domain || 'ai.onnx';
- const version = typeof opset_import.version === 'bigint' ? opset_import.version.toNumber() : opset_import.version;
- if (!imports.has(domain) || imports.get(domain) > version) {
- imports.set(domain, version);
- }
- }
- this._imports = Array.from(imports).map(([name, version]) => `${name} v${version}`);
- }
- if (imports.size === 0) {
- imports.set('ai.onnx', 1);
- imports.set('ai.onnx.ml', 1);
- }
- let imageFormat = '';
- const metadata_props = model.metadata_props;
- if (metadata_props) {
- const metadata = new Map(metadata_props.map((entry) => [entry.key, entry.value]));
- const converted_from = metadata.get('converted_from');
- if (converted_from) {
- this.source = converted_from;
- }
- const author = metadata.get('author');
- if (author) {
- this._metadata.push(new onnx.Argument('author', author));
- }
- const company = metadata.get('company');
- if (company) {
- this._metadata.push(new onnx.Argument('company', company));
- }
- let license = metadata.get('license');
- const license_url = metadata.get('license_url');
- if (license_url) {
- license = `<a href='${license_url}'>${license ? license : license_url}</a>`;
- }
- if (license) {
- this._metadata.push(new onnx.Argument('license', license));
- }
- metadata.delete('author');
- metadata.delete('company');
- metadata.delete('converted_from');
- metadata.delete('license');
- metadata.delete('license_url');
- const imageMetadata = {};
- for (const [name, value] of metadata) {
- switch (name) {
- case 'Image.BitmapPixelFormat':
- case 'Image.ColorSpaceGamma':
- case 'Image.NominalPixelRange':
- imageMetadata[name] = value;
- break;
- default:
- this._metadata.push(new onnx.Argument(name, value));
- break;
- }
- }
- imageFormat = [imageMetadata['Image.BitmapPixelFormat'], imageMetadata['Image.ColorSpaceGamma'], imageMetadata['Image.NominalPixelRange']].filter((item) => item);
- }
- const context = new onnx.Context.Model(metadata, target.locations, imageFormat, imports, model.graph, model.functions);
- const graph = context.graph(null);
- if (graph) {
- this._modules.push(graph);
- }
- this._functions = context.functions;
- }
- get format() {
- return this._format;
- }
- get version() {
- return this._version;
- }
- get imports() {
- return this._imports;
- }
- get producer() {
- return this._producer;
- }
- get source() {
- return this._source;
- }
- get domain() {
- return this._domain || null;
- }
- get description() {
- return this._description || null;
- }
- get metadata() {
- return this._metadata;
- }
- get modules() {
- return this._modules;
- }
- get functions() {
- return this._functions;
- }
- };
- onnx.Graph = class {
- constructor(context, graph) {
- this._description = '';
- this._nodes = [];
- this._inputs = [];
- this._outputs = [];
- this._name = graph && graph.name || null;
- this._description = graph.doc_string || '';
- context = new onnx.Context.Graph(context, graph);
- if (Array.isArray(graph.quantization_annotation)) {
- for (const tensor_annotation of graph.quantization_annotation) {
- const tensor = context.tensor(tensor_annotation.tensor_name);
- tensor.annotation = new Map();
- for (const entry of tensor_annotation.quant_parameter_tensor_names) {
- tensor.annotation.set(entry.key, entry.value);
- }
- }
- }
- graph.input = graph.input.map((value) => {
- const tensor = context.tensor(value.name);
- tensor.type = context.createType(value.type);
- tensor.description = value.doc_string;
- const metadata_props = value.metadata_props || [];
- tensor.metadata = metadata_props.map((metadata) => new onnx.Argument(metadata.key, metadata.value));
- return tensor;
- });
- graph.output = graph.output.map((value) => {
- const tensor = context.tensor(value.name);
- tensor.type = context.createType(value.type);
- tensor.description = value.doc_string;
- const metadata_props = value.metadata_props || [];
- tensor.metadata = metadata_props.map((metadata) => new onnx.Argument(metadata.key, metadata.value));
- return tensor;
- });
- const inference = new onnx.Inference(graph.node);
- for (const output of graph.output) {
- inference.infer(output.name);
- }
- context.push(graph.node, graph.input, graph.output);
- this._nodes = context.pop();
- for (const input of graph.input) {
- const value = context.value(input.name);
- value.metadata = input.metadata || [];
- if (!value.initializer) {
- this._inputs.push(new onnx.Argument(input.name, [value]));
- }
- }
- for (const output of graph.output) {
- const value = context.value(output.name);
- value.metadata = output.metadata || [];
- if (!value.initializer) {
- this._outputs.push(new onnx.Argument(output.name, [value]));
- }
- }
- const metadata_props = graph.metadata_props || [];
- this.metadata = metadata_props.map((metadata) => new onnx.Argument(metadata.key, metadata.value));
- }
- get name() {
- return this._name;
- }
- get description() {
- return this._description;
- }
- get inputs() {
- return this._inputs;
- }
- get outputs() {
- return this._outputs;
- }
- get nodes() {
- return this._nodes;
- }
- toString() {
- return `graph(${this.name})`;
- }
- };
- onnx.Argument = class {
- constructor(name, value, type = null, description = null, visible = true) {
- this.name = name;
- this.value = value;
- this.type = type;
- this.description = description;
- this.visible = visible;
- }
- };
- onnx.Value = class {
- constructor(name, type = null, initializer = null, annotation = null, description = '') {
- if (typeof name !== 'string') {
- throw new onnx.Error(`Invalid value identifier '${JSON.stringify(name)}'.`);
- }
- this._name = name;
- this._type = type;
- this._initializer = initializer;
- this._description = description;
- this._quantization = annotation ? { type: 'annotation', value: annotation } : null;
- }
- get name() {
- return this._name;
- }
- get type() {
- return this._type;
- }
- get description() {
- return this._description;
- }
- get quantization() {
- return this._quantization;
- }
- get initializer() {
- return this._initializer;
- }
- };
- onnx.Node = class {
- constructor(context, node) {
- const attributes = node.attribute || [];
- const metadata_props = node.metadata_props || [];
- const domain = node.domain || 'ai.onnx';
- let op_type = node.op_type;
- let overload = node.overload || '';
- if (domain === 'pkg.torch.ops') {
- const path = op_type.split('.');
- overload = path.pop();
- op_type = path.join('.');
- }
- this.type = context.type(domain, op_type, overload);
- if (!this.type || (this.type.module !== domain && !(this.type instanceof onnx.Function))) {
- this.type = { ...this.type };
- this.type.name = op_type;
- this.type.module = domain;
- this.type.overload = overload;
- this.type.identifier = overload ? `${op_type}.${overload}` : `${op_type}`;
- }
- this.metadata = [];
- for (const metadata of metadata_props) {
- const key = metadata.key;
- const value = metadata.value;
- if (key === 'input_names' && value.startsWith('[') && value.endsWith(']') && !Array.isArray(this.type.inputs)) {
- const input_names = value.slice(1, -1).split(', ');
- if (input_names.every((item) => /^'.*'$/.test(item))) {
- this.type.inputs = input_names.map((item) => ({ name: item.slice(1, -1) }));
- continue;
- }
- }
- const argument = new onnx.Argument(metadata.key, metadata.value);
- this.metadata.push(argument);
- }
- const inputs = [];
- node.input = node.input || [];
- for (let i = 0; i < node.input.length;) {
- const input = this.type && Array.isArray(this.type.inputs) && i < this.type.inputs.length ? this.type.inputs[i] : { name: i.toString() };
- const count = input.list ? node.input.length - i : 1;
- const list = node.input.slice(i, i + count).filter((value) => value.name !== '' || value.initializer);
- const values = list.map((input) => context.value(input.name));
- const argument = new onnx.Argument(input.name, values);
- inputs.push(argument);
- i += count;
- }
- const outputs = [];
- node.output = node.output || [];
- for (let i = 0; i < node.output.length;) {
- const output = this.type && Array.isArray(this.type.outputs) && i < this.type.outputs.length ? this.type.outputs[i] : { name: i.toString() };
- const count = output.list ? node.output.length - i : 1;
- const list = node.output.slice(i, i + count).filter((value) => value.name !== '' || value.initializer);
- const values = list.map((output) => context.value(output.name));
- const argument = new onnx.Argument(output.name, values);
- outputs.push(argument);
- i += count;
- }
- this.name = node.name || '';
- this.description = node.doc_string || '';
- this.inputs = inputs || [];
- this.outputs = outputs || [];
- this.attributes = attributes.map((attr) => {
- if (op_type === 'Int8GivenTensorFill' && attr.s && attr.s.length > 0) {
- return new onnx.Argument(attr.name, Array.from(attr.s), 'byte[]');
- }
- const metadata = context.attribute(domain, op_type, overload, attr.name);
- return context.createAttribute(attr, metadata);
- });
- this.chain = [];
- const identifier = domain ? `${domain}.${op_type}` : op_type;
- if (identifier === 'com.microsoft.FusedConv') {
- const activation = attributes.find((attribute) => attribute.name === 'activation');
- if (activation) {
- const type = context.decodeText(activation.s);
- const node = new onnx.Node(context, { op_type: type });
- this.chain.push(node);
- }
- }
- }
- };
- onnx.Group = class {
- constructor(name, groups) {
- this._type = { name: 'Scope' };
- this._name = name;
- this._nodes = [];
- for (const [key, value] of groups) {
- if (key === '') {
- for (const node of value) {
- this._nodes.push(node);
- }
- } else {
- this._nodes.push(new onnx.Group(name === '' ? key : `${name}/${key}`, value));
- }
- }
- const set = new Set();
- const inputs = [];
- const outputs = [];
- for (const node of this._nodes) {
- if (node instanceof onnx.Group) {
- node.freeze();
- }
- for (const parameter of node.outputs) {
- for (const value of parameter.value) {
- if (!value.initializer) {
- outputs.push(value);
- set.add(value.name);
- }
- }
- }
- }
- for (const node of this._nodes) {
- for (const parameter of node.inputs) {
- for (const value of parameter.value) {
- if (!set.has(value.name) && !value.initializer) {
- inputs.push(value);
- }
- }
- }
- }
- this._inputs = [new onnx.Argument('inputs', inputs)];
- this._outputs = [new onnx.Argument('outputs', outputs)];
- this._attributes = [];
- }
- get name() {
- return this._name;
- }
- get type() {
- return this._type;
- }
- get inputs() {
- return this._inputs;
- }
- get outputs() {
- return this._outputs;
- }
- get attributes() {
- return this._attributes;
- }
- get nodes() {
- return this._nodes;
- }
- };
- onnx.Tensor = class {
- constructor(context, tensor, category = null) {
- this._category = category;
- if (tensor.indices && tensor.values) {
- this._name = tensor.values.name || '';
- this._type = context.createTensorType(tensor.values.data_type, tensor.dims, 'sparse');
- this._location = context.createLocation(tensor.values.data_location);
- this._values = new onnx.Tensor(context, tensor.values);
- this._indices = new onnx.Tensor(context, tensor.indices);
- } else {
- this._name = tensor.name || '';
- this._type = context.createTensorType(tensor.data_type, tensor.dims);
- this._location = context.createLocation(tensor.data_location);
- switch (tensor.data_location) {
- case onnx.DataLocation.DEFAULT: {
- switch (tensor.data_type) {
- case onnx.DataType.UNDEFINED: {
- break;
- }
- case onnx.DataType.FLOAT:
- this._data = new Float32Array(tensor.float_data);
- this._encoding = '|';
- break;
- case onnx.DataType.DOUBLE:
- this._data = new Float64Array(tensor.double_data);
- this._encoding = '|';
- break;
- case onnx.DataType.BOOL:
- if (tensor.int32_data && tensor.int32_data.length > 0) {
- const array = tensor.int32_data;
- this._data = new Array(array.length);
- for (let i = 0; i < this._data.length; i++) {
- this._data[i] = array[i] === 0 ? false : true;
- }
- this._encoding = '|';
- }
- break;
- case onnx.DataType.INT8:
- this._data = new Int8Array(tensor.int32_data);
- this._encoding = '|';
- break;
- case onnx.DataType.UINT8:
- this._data = new Uint8Array(tensor.int32_data);
- this._encoding = '|';
- break;
- case onnx.DataType.INT16:
- this._data = new Int32Array(tensor.int32_data);
- this._encoding = '|';
- break;
- case onnx.DataType.UINT16:
- this._data = new Int32Array(tensor.int32_data);
- this._encoding = '|';
- break;
- case onnx.DataType.INT32:
- this._data = new Int32Array(tensor.int32_data);
- this._encoding = '|';
- break;
- case onnx.DataType.UINT32:
- case onnx.DataType.UINT64:
- this._data = tensor.uint64_data;
- this._encoding = '|';
- break;
- case onnx.DataType.INT64:
- this._data = tensor.int64_data;
- this._encoding = '|';
- break;
- case onnx.DataType.STRING:
- this._data = tensor.string_data;
- this._encoding = '|';
- break;
- case onnx.DataType.COMPLEX64:
- case onnx.DataType.COMPLEX128:
- break;
- case onnx.DataType.FLOAT16:
- case onnx.DataType.BFLOAT16:
- if (tensor.int32_data && tensor.int32_data.length > 0) {
- const array = tensor.int32_data;
- const buffer = new Uint8Array(array.length << 1);
- const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
- for (let i = 0; i < array.length; i++) {
- view.setUint16(i << 1, array[i], true);
- }
- this._data = buffer;
- this._encoding = '<';
- }
- break;
- case onnx.DataType.FLOAT8E8M0:
- case onnx.DataType.FLOAT4E2M1:
- case onnx.DataType.FLOAT8E4M3FN:
- case onnx.DataType.FLOAT8E4M3FNUZ:
- case onnx.DataType.FLOAT8E5M2:
- case onnx.DataType.FLOAT8E5M2FNUZ:
- if (tensor.int32_data && tensor.int32_data.length > 0) {
- this._data = new Uint8Array(Array.from(tensor.int32_data));
- this._encoding = '<';
- }
- break;
- case onnx.DataType.INT2:
- case onnx.DataType.INT4:
- case onnx.DataType.UINT2:
- case onnx.DataType.UINT4:
- if (tensor.int32_data && tensor.int32_data.length > 0) {
- this._data = new Uint8Array(Array.from(tensor.int32_data));
- this._encoding = '<';
- }
- break;
- default:
- throw new onnx.Error(`Unsupported tensor data type '${tensor.data_type}'.`);
- }
- if (this._data && (Array.isArray(this._data) || ArrayBuffer.isView(this._data)) && this._data.length === 0) {
- this._data = undefined;
- }
- if (!this._data && tensor.raw_data && tensor.raw_data.length > 0) {
- this._data = tensor.raw_data;
- this._encoding = '<';
- }
- break;
- }
- case onnx.DataLocation.EXTERNAL: {
- if (Array.isArray(tensor.external_data)) {
- const data = new Map();
- for (const entry of tensor.external_data) {
- data.set(entry.key, entry.value);
- }
- if (data.has('location')) {
- this._location = data.get('location').toString();
- const location = context.location(this._location);
- const offset = data.has('offset') ? parseInt(data.get('offset'), 10) : 0;
- const length = data.has('length') ? parseInt(data.get('length'), 10) : -1;
- this._request = { location, offset, length };
- this._encoding = '<';
- }
- }
- break;
- }
- default: {
- break;
- }
- }
- }
- }
- peek() {
- return !this._request;
- }
- async read() {
- if (this._request) {
- const location = this._request.location;
- const offset = this._request.offset;
- const length = this._request.length;
- this._data = await location.read(offset, length);
- delete this._request;
- }
- }
- get name() {
- return this._name;
- }
- get category() {
- return this._category;
- }
- get encoding() {
- return this._encoding;
- }
- get location() {
- return this._location;
- }
- get type() {
- return this._type;
- }
- get indices() {
- return this._indices;
- }
- get values() {
- if (this._request) {
- throw new onnx.Error('Tensor data not loaded.');
- }
- switch (this.type.layout) {
- case 'sparse': {
- return this._values;
- }
- default: {
- if (!this._data || this._data instanceof Uint8Array) {
- return this._data;
- }
- if (Array.isArray(this._data) || ArrayBuffer.isView(this._data)) {
- return this._data;
- }
- return this._data.peek();
- }
- }
- }
- };
- onnx.TensorType = class {
- constructor(dataType, shape, layout = null, denotation = null) {
- this._dataType = dataType;
- this._shape = shape;
- this._layout = layout;
- this._denotation = denotation;
- }
- get dataType() {
- return this._dataType;
- }
- get shape() {
- return this._shape;
- }
- get layout() {
- return this._layout;
- }
- get denotation() {
- return this._denotation;
- }
- toString() {
- return this.dataType + this._shape.toString();
- }
- };
- onnx.TensorShape = class {
- constructor(dimensions) {
- this._dimensions = dimensions.map((dim) => typeof dim === 'bigint' ? dim.toNumber() : dim);
- }
- get dimensions() {
- return this._dimensions;
- }
- toString() {
- if (!this._dimensions || this._dimensions.length === 0) {
- return '';
- }
- return `[${this._dimensions.map((dim) => dim || Number.isInteger(dim) ? dim.toString() : '?').join(',')}]`;
- }
- };
- onnx.SequenceType = class {
- constructor(elementType, denotation) {
- this._elementType = elementType;
- this._denotation = denotation;
- }
- get elementType() {
- return this._elementType;
- }
- get denotation() {
- return this._denotation;
- }
- toString() {
- const elementType = this._elementType ? this._elementType.toString() : '';
- return `sequence<${elementType}>`;
- }
- };
- onnx.MapType = class {
- constructor(keyType, valueType, denotation) {
- this._keyType = keyType;
- this._valueType = valueType;
- this._denotation = denotation;
- }
- get keyType() {
- return this._keyType;
- }
- get valueType() {
- return this._valueType;
- }
- get denotation() {
- return this._denotation;
- }
- toString() {
- return `map<${this._keyType},${this._valueType}>`;
- }
- };
- onnx.OpaqueType = class {
- constructor(domain, name) {
- this._domain = domain;
- this._name = name;
- }
- toString() {
- const name = (this._domain ? (`${this._domain}.`) : '') + this._name;
- return `opaque<${name}>`;
- }
- };
- onnx.OptionalType = class {
- constructor(type) {
- this._type = type;
- }
- get type() {
- return this._type;
- }
- toString() {
- return `optional<${this._type}>`;
- }
- };
- onnx.Function = class {
- constructor(context, func) {
- this.type = 'function';
- this.name = func.name;
- this.module = func.domain;
- this.overload = func.overload || '';
- this.identifier = this.overload ? `${this.name}:${this.overload}` : this.name;
- this.description = func.doc_string;
- this.inputs = [];
- this.outputs = [];
- this.attributes = [];
- for (let i = 0; i < func.attribute.length; i++) {
- const attribute = new onnx.Argument(i.toString(), func.attribute[i]);
- this.attributes.push(attribute);
- }
- for (const attr of func.attribute_proto) {
- const attribute = context.createAttribute(attr, func.domain, func.name, func.overload);
- this.attributes.push(attribute);
- }
- context = new onnx.Context.Graph(context, func);
- func.input = func.input.map((input) => context.tensor(input));
- func.output = func.output.map((output) => context.tensor(output));
- context.push(func.node, func.input, func.output);
- this.nodes = context.pop();
- for (const input of func.input) {
- const value = context.value(input.name);
- if (!value.initializer) {
- this.inputs.push(new onnx.Argument(input.name, [value]));
- }
- }
- for (const output of func.output) {
- const value = context.value(output.name);
- if (!value.initializer) {
- this.outputs.push(new onnx.Argument(output.name, [value]));
- }
- }
- }
- };
- onnx.Context = class {};
- onnx.Context.Model = class {
- constructor(metadata, locations, imageFormat, imports, graph, functions) {
- this._metadata = metadata;
- this._locations = locations;
- this._dataTypes = new Map(Object.entries(onnx.DataType).map(([name, value]) => [value, name.toLowerCase()]));
- this._dataTypes.set(onnx.DataType.UNDEFINED, 'undefined');
- this._dataTypes.set(onnx.DataType.BOOL, 'boolean');
- this._dataTypes.set(onnx.DataType.FLOAT, 'float32');
- this._dataTypes.set(onnx.DataType.DOUBLE, 'float64');
- this._imageFormat = imageFormat;
- this._imports = imports;
- this._types = new Map();
- this._attributes = new Map();
- this._graph = null;
- this._graphs = new Map();
- this._functions = new Map();
- for (const func of functions || []) {
- const key = func.overload ? `${func.domain}:${func.name}:${func.overload}` : `${func.domain}:${func.name}`;
- func.initializer = [];
- func.uses = [];
- func.callers = new Set();
- this._functions.set(key, func);
- }
- if (graph) {
- if (this._functions.size > 0) { // #1208
- const queue = [graph].concat(Array.from(this._functions.values()));
- for (const graph of queue) {
- const graphs = [graph];
- while (graphs.length > 0) {
- const graph = graphs.shift();
- for (const node of graph.node) {
- const key = node.overload ? `${node.domain}:${node.op_type}:${node.overload}` : `${node.domain}:${node.op_type}`;
- if (this._functions.has(key)) {
- this._functions.get(key).callers.add(graph);
- }
- for (const attribute of node.attribute) {
- if (attribute.g) {
- graphs.push(attribute.g);
- }
- if (Array.isArray(attribute.graphs) && attribute.graphs.length > 0) {
- graphs.push(...attribute.graphs);
- }
- }
- }
- }
- }
- const visited = new Set();
- const graphs = new Set([graph]);
- while (graphs.size > 0) {
- const graph = graphs.values().next().value;
- graphs.delete(graph);
- if (visited.has(graph)) {
- continue;
- }
- if (graph.callers && !Array.from(graph.callers).every((caller) => visited.has(caller))) {
- graphs.add(graph);
- continue;
- }
- visited.add(graph);
- graph.initializer = graph.initializer || [];
- const initializers = new Map();
- for (const initializer of graph.initializer) {
- initializers.set(initializer.name, { uses: [], initializer, visible: true });
- }
- for (const node of graph.node) {
- const key = node.overload ? `${node.domain}:${node.op_type}:${node.overload}` : `${node.domain}:${node.op_type}`;
- if (this._functions.has(key)) {
- this._functions.get(key).uses.push(node);
- }
- for (const input of node.input) {
- if (initializers.has(input)) {
- initializers.get(input).uses.push(node);
- }
- }
- for (const attribute of node.attribute) {
- if (attribute.g) {
- graphs.add(attribute.g);
- }
- if (Array.isArray(attribute.graphs) && attribute.graphs.length > 0) {
- for (const graph of attribute.graphs) {
- graphs.add(graph);
- }
- }
- }
- }
- const queue = [];
- for (const [name, entry] of initializers) {
- if (entry.uses.length === 1) {
- const [node] = entry.uses;
- const key = node.overload ? `${node.domain}:${node.op_type}:${node.overload}` : `${node.domain}:${node.op_type}`;
- if (this._functions.has(key)) {
- const func = this._functions.get(key);
- if (func.uses.length === 1 && func.callers.size === 1) {
- const index = node.input.indexOf(name);
- if (Array.isArray(func.input) && index < func.input.length && func.input[index] === name) {
- func.initializer.push(entry.initializer);
- graphs.add(func);
- queue.push([index, node]);
- }
- }
- }
- }
- }
- queue.sort((a, b) => b[0] - a[0]);
- for (const [index, node] of queue) {
- node.input.splice(index, 1);
- }
- }
- }
- this._graph = new onnx.Graph(this, graph);
- }
- }
- graph(value) {
- if (value === null) {
- return this._graph;
- }
- if (!this._graphs.has(value)) {
- this._graphs.set(value, new onnx.Graph(this, value));
- }
- return this._graphs.get(value);
- }
- get functions() {
- for (const [, func] of this._functions) {
- if (func instanceof onnx.Function === false) {
- this.type(func.domain, func.name, func.overload);
- }
- }
- return Array.from(this._functions.values());
- }
- location(name) {
- if (this._locations.has(name)) {
- return this._locations.get(name);
- }
- return null;
- }
- initializer(/* name */) {
- return null;
- }
- type(domain, name, overload) {
- const key = overload ? `${domain}:${name}:${overload}` : `${domain}:${name}`;
- if (!this._types.has(key)) {
- let value = null;
- if (this._functions.has(key)) {
- value = this._functions.get(key);
- if (value && value instanceof onnx.Function === false) {
- value = new onnx.Function(this, value);
- this._functions.set(key, value);
- }
- }
- if (!value) {
- value = this._metadata.type(domain, name, this._imports);
- }
- this._types.set(key, value);
- }
- return this._types.get(key);
- }
- attribute(domain, type, overload, name) {
- const key = overload ? `${domain}:${type}:${overload}::${name}` : `${domain}:${type}::${name}`;
- if (!this._attributes.has(key)) {
- this._attributes.set(key, null);
- const metadata = this.type(domain, type);
- if (metadata && Array.isArray(metadata.attributes) && metadata.attributes.length > 0) {
- for (const attribute of metadata.attributes) {
- const name = attribute.name;
- const key = overload ? `${domain}:${type}:${overload}::${name}` : `${domain}:${type}::${name}`;
- this._attributes.set(key, attribute);
- }
- }
- }
- return this._attributes.get(key);
- }
- decodeText(value) {
- if (typeof value === 'string') {
- return value;
- }
- this._decoder = this._decoder || new TextDecoder('utf-8');
- return this._decoder.decode(value);
- }
- createAttribute(attribute, metadata) {
- const name = attribute.name;
- let type = null;
- let value = null;
- let visible = true;
- if (attribute.ref_attr_name) {
- value = attribute.ref_attr_name;
- type = 'reference';
- } else {
- switch (attribute.type) {
- case onnx.AttributeType.UNDEFINED:
- break;
- case onnx.AttributeType.FLOAT:
- value = attribute.f;
- type = 'float32';
- break;
- case onnx.AttributeType.INT:
- value = BigInt(attribute.i);
- type = 'int64';
- break;
- case onnx.AttributeType.STRING:
- value = this.decodeText(attribute.s);
- type = 'string';
- break;
- case onnx.AttributeType.TENSOR:
- value = new onnx.Tensor(this, attribute.t);
- type = 'tensor';
- break;
- case onnx.AttributeType.GRAPH:
- value = this.graph(attribute.g);
- type = 'graph';
- break;
- case onnx.AttributeType.FLOATS:
- value = ArrayBuffer.isView(attribute.floats) ? Array.from(attribute.floats) : attribute.floats;
- type = 'float32[]';
- break;
- case onnx.AttributeType.INTS:
- value = ArrayBuffer.isView(attribute.ints) ? Array.from(attribute.ints) : attribute.ints.map((value) => BigInt(value));
- type = 'int64[]';
- break;
- case onnx.AttributeType.STRINGS:
- value = attribute.strings.map((s) => this.decodeText(s));
- type = 'string[]';
- break;
- case onnx.AttributeType.TENSORS:
- value = attribute.tensors.map((tensor) => new onnx.Tensor(this, tensor));
- type = 'tensor[]';
- break;
- case onnx.AttributeType.GRAPHS:
- value = attribute.graphs.map((graph) => this.graph(graph));
- type = 'graph[]';
- break;
- case onnx.AttributeType.SPARSE_TENSOR:
- value = new onnx.Tensor(this, attribute.sparse_tensor);
- type = 'tensor';
- break;
- case onnx.AttributeType.SPARSE_TENSORS:
- value = attribute.sparse_tensors.map((tensor) => new onnx.Tensor(this, tensor));
- type = 'tensor[]';
- break;
- case onnx.AttributeType.TYPE_PROTO:
- value = this.createType(attribute.tp);
- type = 'type';
- break;
- case onnx.AttributeType.TYPE_PROTOS:
- value = attribute.type_protos.map((type) => this.createType(type));
- type = 'type[]';
- break;
- default:
- throw new onnx.Error(`Unsupported attribute type '${attribute.type}'.`);
- }
- if (metadata) {
- if (metadata.default !== undefined) {
- const defaultValue = type === 'int64' ? BigInt(metadata.default) : metadata.default;
- if (value === defaultValue) {
- visible = false;
- }
- }
- if (metadata.type === 'DataType') {
- type = metadata.type;
- value = this.createDataType(value);
- }
- }
- }
- return new onnx.Argument(name, value, type, attribute.doc_string, visible);
- }
- createType(type) {
- if (!type) {
- return null;
- }
- let denotation = '';
- switch (type.denotation) {
- case undefined:
- case null:
- case '':
- break;
- case 'TENSOR':
- denotation = 'Tensor';
- break;
- case 'IMAGE':
- denotation = `Image${this._imageFormat ? `(${this._imageFormat.join(',')})` : ''}`;
- break;
- case 'AUDIO':
- denotation = 'Audio';
- break;
- case 'TEXT':
- denotation = 'Text';
- break;
- default:
- throw new onnx.Error(`Unsupported tensor type denotation '${type.denotation}'.`);
- }
- if (type.tensor_type) {
- const tensor_type = type.tensor_type;
- 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) : [];
- return this.createTensorType(tensor_type.elem_type, shape, null, denotation);
- } else if (type.sparse_tensor_type) {
- type = type.sparse_tensor_type;
- const shape = type.shape && type.shape.dim ? type.shape.dim.map((dim) => dim.dim_param ? dim.dim_param : dim.dim_value || null) : [];
- return this.createTensorType(type.elem_type, shape, 'sparse', denotation);
- } else if (type.map_type) {
- const keyType = this.createDataType(type.map_type.key_type);
- const valueType = this.createType(type.map_type.value_type);
- return new onnx.MapType(keyType, valueType, denotation);
- } else if (type.sequence_type) {
- return new onnx.SequenceType(this.createType(type.sequence_type.elem_type), denotation);
- } else if (type.opaque_type) {
- return new onnx.OpaqueType(type.opaque_type.domain, type.opaque_type.name);
- } else if (type.optional_type) {
- return new onnx.OptionalType(this.createType(type.optional_type.elem_type), denotation);
- } else if (Object.keys(type).length === 0) {
- return null;
- }
- throw new onnx.Error(`Unsupported tensor type '${JSON.stringify(type)}'.`);
- }
- createDataType(value) {
- if (!Number.isInteger(value)) {
- if (typeof value === 'bigint') {
- value = value.toNumber();
- } else if (value && typeof value === 'string' && onnx.DataType[value.toUpperCase()] !== undefined) {
- value = onnx.DataType[value.toUpperCase()];
- } else {
- throw new onnx.Error(`Unsupported data type '${JSON.stringify(value)}'.`);
- }
- }
- if (this._dataTypes.has(value)) {
- return this._dataTypes.get(value);
- }
- throw new onnx.Error(`Unsupported data type '${JSON.stringify(value)}'.`);
- }
- createLocation(value) {
- switch (value) {
- case undefined:
- case onnx.DataLocation.DEFAULT: return '';
- case onnx.DataLocation.EXTERNAL: return 'external';
- default: return 'undefined';
- }
- }
- createTensorType(dataType, shape, layout, denotation) {
- dataType = this.createDataType(dataType);
- return new onnx.TensorType(dataType, new onnx.TensorShape(shape), layout, denotation);
- }
- };
- onnx.Metadata = class {
- static async open(context) {
- if (!onnx.Metadata._metadata) {
- let data = null;
- try {
- data = await context.request('onnx-metadata.json');
- } catch {
- // continue regardless of error
- }
- onnx.Metadata._metadata = new onnx.Metadata(data);
- }
- return onnx.Metadata._metadata;
- }
- constructor(data) {
- this._types = new Map();
- if (data) {
- const types = JSON.parse(data);
- for (const type of types) {
- if (!this._types.has(type.module)) {
- this._types.set(type.module, new Map());
- }
- const types = this._types.get(type.module);
- if (!types.has(type.name)) {
- types.set(type.name, []);
- }
- types.get(type.name).push(type);
- }
- }
- }
- type(domain, name, imports) {
- domain = domain || 'ai.onnx';
- let current = null;
- if (this._types.has(domain)) {
- const types = this._types.get(domain);
- if (types.has(name)) {
- for (const type of types.get(name)) {
- const matchVersion = current ? current.version : -1;
- const importVersion = imports.get(type.module) || 0;
- if (importVersion >= type.version && matchVersion < type.version) {
- current = type;
- }
- }
- }
- }
- return current;
- }
- };
- onnx.Inference = class {
- constructor(nodes) {
- this._outputs = new Map();
- for (const node of nodes) {
- for (const output of node.output) {
- this._outputs.set(output.name, node);
- }
- }
- }
- infer(output) {
- if (this._outputs.has(output)) {
- let hasInputShapes = true;
- const node = this._outputs.get(output);
- for (const input of node.input) {
- if (!input.type) {
- this.infer(input);
- if (!input.type) {
- hasInputShapes = false;
- break;
- }
- }
- }
- if (hasInputShapes) {
- // continue
- }
- }
- }
- };
- onnx.DataLocation = {
- DEFAULT: 0,
- EXTERNAL: 1
- };
- onnx.DataType = {
- UNDEFINED: 0,
- FLOAT: 1,
- UINT8: 2,
- INT8: 3,
- UINT16: 4,
- INT16: 5,
- INT32: 6,
- INT64: 7,
- STRING: 8,
- BOOL: 9,
- FLOAT16: 10,
- DOUBLE: 11,
- UINT32: 12,
- UINT64: 13,
- COMPLEX64: 14,
- COMPLEX128: 15,
- BFLOAT16: 16,
- FLOAT8E4M3FN: 17,
- FLOAT8E4M3FNUZ: 18,
- FLOAT8E5M2: 19,
- FLOAT8E5M2FNUZ: 20,
- UINT4: 21,
- INT4: 22,
- FLOAT4E2M1: 23,
- FLOAT8E8M0: 24,
- UINT2: 25,
- INT2: 26
- };
- onnx.AttributeType = {
- UNDEFINED: 0,
- FLOAT: 1,
- INT: 2,
- STRING: 3,
- TENSOR: 4,
- GRAPH: 5,
- FLOATS: 6,
- INTS: 7,
- STRINGS: 8,
- TENSORS: 9,
- GRAPHS: 10,
- SPARSE_TENSOR: 11,
- SPARSE_TENSORS: 12,
- TYPE_PROTO: 13,
- TYPE_PROTOS: 14
- };
- onnx.Context.Graph = class {
- constructor(context, graph) {
- this._context = context;
- this._initializers = new Map();
- this._tensors = new Map();
- this._values = new Map();
- this._groups = new Map();
- this._nodes = [];
- if (Array.isArray(graph.initializer)) {
- for (const initializer of graph.initializer) {
- const tensor = new onnx.Tensor(this, initializer, 'Initializer');
- this._initializers.set(initializer.name, tensor);
- }
- }
- if (Array.isArray(graph.sparse_initializer)) {
- for (const sparse_initializer of graph.sparse_initializer) {
- const tensor = new onnx.Tensor(this, sparse_initializer, 'Initializer');
- this._initializers.set(sparse_initializer.values.name, tensor);
- }
- }
- if (Array.isArray(graph.value_info)) {
- for (const value of graph.value_info) {
- const tensor = this.tensor(value.name);
- tensor.type = this.createType(value.type);
- tensor.description = value.doc_string;
- }
- }
- for (const node of graph.node) {
- node.input = node.input.map((name) => this.tensor(name));
- node.output = node.output.map((name) => this.tensor(name));
- node.param = {};
- if (Array.isArray(node.attribute)) {
- for (const attribute of node.attribute) {
- if (attribute.type) {
- continue;
- }
- if (Array.isArray(attribute.ints) && attribute.ints.length > 0) {
- attribute.type = onnx.AttributeType.INTS;
- } else if (Array.isArray(attribute.floats) && attribute.floats.length > 0) {
- attribute.type = onnx.AttributeType.FLOATS;
- } else if (Array.isArray(attribute.strings) && attribute.strings.length > 0) {
- attribute.type = onnx.AttributeType.STRINGS;
- } else if (Array.isArray(attribute.graphs) && attribute.graphs.length > 0) {
- attribute.type = onnx.AttributeType.GRAPHS;
- } else if (Array.isArray(attribute.s) && attribute.s.length > 0) {
- attribute.type = onnx.AttributeType.STRING;
- } else if (attribute.f !== undefined) {
- attribute.type = onnx.AttributeType.FLOAT;
- } else if (attribute.i !== undefined) {
- attribute.type = onnx.AttributeType.INT;
- } else if (attribute.t !== undefined) {
- attribute.type = onnx.AttributeType.TENSOR;
- } else if (attribute.g !== undefined) {
- attribute.type = onnx.AttributeType.GRAPH;
- } else if (attribute.sparse_tensor) {
- attribute.type = onnx.AttributeType.SPARSE_TENSOR;
- } else {
- attribute.type = onnx.AttributeType.UNDEFINED;
- }
- }
- }
- }
- }
- type(domain, name, overload) {
- return this._context.type(domain, name, overload);
- }
- attribute(domain, type, overload, name) {
- return this._context.attribute(domain, type, overload, name);
- }
- initializer(name) {
- if (this._initializers.has(name)) {
- return this._initializers.get(name);
- }
- return this._context.initializer(name);
- }
- tensor(name) {
- if (!this._tensors.has(name)) {
- this._tensors.set(name, { name, initializer: this.initializer(name) });
- }
- return this._tensors.get(name);
- }
- location(name) {
- return this._context.location(name);
- }
- group(name) {
- if (!this._groups.has(name)) {
- const path = name.split('/');
- if (path.length > 1) {
- path.pop();
- return this.group(path.join('/'));
- }
- this._groups.set(name, new Map([['', []]]));
- }
- return this._groups.get(name);
- }
- value(name) {
- if (!this._values.has(name)) {
- const tensor = this.tensor(name);
- const type = tensor.initializer ? tensor.initializer.type : tensor.type || null;
- this._values.set(name, new onnx.Value(name, type, tensor.initializer, tensor.annotation, tensor.description));
- }
- return this._values.get(name);
- }
- createType(type) {
- return this._context.createType(type);
- }
- createTensorType(dataType, shape, layout, denotation) {
- return this._context.createTensorType(dataType, shape, layout, denotation);
- }
- createDataType(value) {
- return this._context.createDataType(value);
- }
- createLocation(value) {
- return this._context.createLocation(value);
- }
- createAttribute(attribute, metadata) {
- return this._context.createAttribute(attribute, metadata);
- }
- decodeText(value) {
- return this._context.decodeText(value);
- }
- push(nodes, inputs, outputs) {
- const inputMap = new Map();
- const outputMap = new Map();
- for (const node of nodes) {
- for (const input of node.input) {
- inputMap.set(input.name, (inputMap.get(input.name) || 0) + 1);
- }
- for (const output of node.output) {
- outputMap.set(output.name, (outputMap.get(output.name) || 0) + 1);
- }
- }
- inputs.every((input) => inputMap.delete(input.name));
- outputs.every((output) => outputMap.delete(output.name));
- nodes = nodes.filter((node) => {
- const constant = node &&
- node.op_type === 'Constant' &&
- node.attribute.length === 1 && node.attribute[0] &&
- node.input.length === 0 &&
- node.output.length === 1 && node.output[0] && inputMap.get(node.output[0].name) === 1 && outputMap.get(node.output[0].name) === 1;
- const attribute = constant ? node.attribute[0] : null;
- if (attribute && attribute.name === 'value' && attribute.type === onnx.AttributeType.TENSOR && attribute.t) {
- const tensor = this.tensor(node.output[0].name);
- tensor.initializer = new onnx.Tensor(this, attribute.t, 'Constant');
- return false;
- } else if (attribute && attribute.name === 'sparse_value' && attribute.type === onnx.AttributeType.SPARSE_TENSOR && attribute.sparse_tensor) {
- const tensor = this.tensor(node.output[0].name);
- tensor.initializer = new onnx.Tensor(this, attribute.sparse_tensor, 'Constant');
- return false;
- }
- return true;
- });
- for (let node of nodes) {
- node = new onnx.Node(this, node);
- this._nodes.push(node);
- // const path = (node.name || '').split('/');
- // path.pop();
- // this.group(path.join('/')).get('').push(node);
- }
- }
- pop() {
- /*
- const nodes = [];
- for (const [name, value] of this._groups) {
- if (name === '') {
- for (const node of value.get('')) {
- nodes.push(node);
- }
- continue;
- }
- nodes.push(new onnx.Group(name, value));
- }
- return nodes;
- */
- return this._nodes;
- }
- };
- onnx.ProtoReader = class {
- static async open(context) {
- const identifier = context.identifier;
- const stream = context.stream;
- let offset = 0;
- if (stream && stream.length > 5) {
- const buffer = stream.peek(Math.min(stream.length, 256));
- if (buffer[0] === 0x08 && buffer[1] < 0x0B && buffer[2] === 0x12 && buffer[3] < 64 && (buffer[3] + 4) <= stream.length) {
- if (buffer[3] === 0x00 && buffer[4] === 0x1A && buffer[5] === 0x00) {
- return new onnx.ProtoReader(context, 'binary', 'model');
- }
- const producer = String.fromCharCode.apply(null, buffer.subarray(4, 4 + buffer[3]));
- if (producer.match(/^[A-Za-z][A-Za-z0-9_+-. ]+$/)) {
- return new onnx.ProtoReader(context, 'binary', 'model');
- }
- }
- const length = buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24);
- if (length === stream.length - 4 && (buffer[4] === 0x08 || buffer[4] === 0x0A)) {
- offset = 4;
- }
- }
- stream.seek(offset);
- const binaryTags = await context.tags('pb');
- stream.seek(0);
- if (binaryTags.size > 0) {
- const tags = binaryTags;
- if (tags.size === 1 && tags.get(1) === 2) {
- stream.seek(offset);
- const tags = await context.tags('pb+');
- stream.seek(0);
- const match = (tags, schema) => {
- for (const [key, inner] of schema) {
- const value = tags[key];
- if (value === undefined) {
- continue;
- }
- if (inner === false) {
- return false;
- }
- if (Array.isArray(inner)) {
- if (typeof value !== 'object' || !match(value, inner)) {
- return false;
- }
- } else if (inner !== value) {
- if (inner === 2 && !Array.isArray(value) && Object(value) === (value) && Object.keys(value).length === 0) {
- return true;
- }
- return false;
- }
- }
- return true;
- };
- // mediapipe.BoxDetectorIndex
- 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]])) {
- return undefined;
- }
- // third_party.tensorflow.python.keras.protobuf.SavedMetadata
- if (match(tags, [[1,[[1,[[1,0],[2,0]]],[2,0],[3,2],[4,2],[5,2]]]])) {
- return undefined;
- }
- }
- if (Array.from(tags.keys()).every((tag) => tag <= 100) &&
- Array.from(tags.values()).every((type) => type < 5)) {
- // TensorProto
- if (tags.get(1) === 0 && tags.get(2) === 0 && [3, 4, 5, 6].filter((tag) => tags.get(tag)).length <= 1) {
- const schema = [[1,0],[2,0],[4,2],[5,2],[7,2],[8,2],[9,2]];
- if (schema.every(([key, value]) => !tags.has(key) || tags.get(key) === value)) {
- return new onnx.ProtoReader(context, 'binary', 'tensor', offset);
- }
- }
- // GraphProto
- if (tags.get(1) === 2 && (identifier !== 'preloaded_data.pb' || tags.size !== 1)) {
- 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]];
- if (schema.every(([key, value]) => !tags.has(key) || tags.get(key) === value)) {
- const decode = (buffer, value) => {
- const reader = protobuf.BinaryReader.open(buffer);
- const length = reader.length;
- while (reader.position < length) {
- const tag = reader.uint32();
- const number = tag >>> 3;
- const type = tag & 7;
- if (value === number) {
- return type === 2 ? reader.bytes() : null;
- }
- reader.skipType(type);
- }
- return null;
- };
- const stream = context.stream;
- const buffer = stream.peek();
- const nodeBuffer = decode(buffer, 1);
- if (nodeBuffer) {
- const nameBuffer = decode(nodeBuffer, 4);
- if (nameBuffer && nameBuffer.every((c) => c > 0x20 && c < 0x7f)) {
- return new onnx.ProtoReader(context, 'binary', 'graph', offset);
- }
- }
- }
- }
- // ModelProto
- if (tags.get(7) === 2) {
- const schema = [[1,0],[2,2],[3,2],[4,2],[5,0],[6,2],[7,2],[8,2],[14,2],[20,2]];
- if (schema.every(([key, value]) => !tags.has(key) || tags.get(key) === value)) {
- return new onnx.ProtoReader(context, 'binary', 'model', offset);
- }
- }
- }
- }
- const textTags = await context.tags('pbtxt');
- if (textTags.size > 0) {
- const tags = textTags;
- if (tags.has('ir_version')) {
- return new onnx.ProtoReader(context, 'text', 'model');
- }
- const identifier = context.identifier;
- const extension = identifier.lastIndexOf('.') > 0 ? identifier.split('.').pop().toLowerCase() : '';
- if (tags.has('graph') && extension !== 'model') {
- return new onnx.ProtoReader(context, 'text', 'model');
- }
- }
- const obj = await context.peek('json');
- if (obj && (obj.ir_version === undefined && obj.producer_name === undefined && !Array.isArray(obj.opset_import) && !Array.isArray(obj.metadata_props)) &&
- (obj.irVersion !== undefined || obj.producerName !== undefined || Array.isArray(obj.opsetImport) || Array.isArray(obj.metadataProps) || (Array.isArray(obj.graph) && Array.isArray(obj.graph.node)))) {
- return new onnx.ProtoReader(context, 'json', 'model');
- }
- return undefined;
- }
- constructor(context, encoding, type, offset = 0) {
- this.name = 'onnx.proto';
- this.context = context;
- this.encoding = encoding;
- this.type = type;
- this.offset = offset;
- this.locations = new Map();
- }
- async read() {
- onnx.proto = await this.context.require('./onnx-proto');
- onnx.proto = onnx.proto.onnx;
- const context = this.context;
- switch (this.encoding) {
- case 'text': {
- try {
- const reader = await context.read('protobuf.text');
- this.model = onnx.proto.ModelProto.decodeText(reader);
- this.format = `ONNX${this.model.ir_version ? ` v${this.model.ir_version}` : ''}`;
- } catch (error) {
- const message = error && error.message ? error.message : error.toString();
- throw new onnx.Error(`File text format is not onnx.ModelProto (${message.replace(/\.$/, '')}).`);
- }
- break;
- }
- case 'json': {
- try {
- const obj = await context.read('json');
- this.model = onnx.proto.ModelProto.decodeJson(obj);
- this.format = `ONNX${this.model.ir_version ? ` v${this.model.ir_version}` : ''}`;
- } catch (error) {
- const message = error && error.message ? error.message : error.toString();
- throw new onnx.Error(`File JSON format is not onnx.ModelProto (${message.replace(/\.$/, '')}).`);
- }
- break;
- }
- case 'binary': {
- const stream = context.stream;
- if (this.offset) {
- stream.seek(this.offset);
- }
- switch (this.type) {
- case 'tensor': {
- // TensorProto
- // input_0.pb, output_0.pb
- try {
- const reader = await context.read('protobuf.binary');
- const tensor = onnx.proto.TensorProto.decode(reader);
- tensor.name = tensor.name || this.context.identifier;
- const attribute = new onnx.proto.AttributeProto();
- attribute.name = 'value';
- attribute.type = onnx.AttributeType.TENSOR;
- attribute.t = tensor;
- const node = new onnx.proto.NodeProto();
- node.op_type = 'Constant';
- node.attribute = [attribute];
- const graph = new onnx.proto.GraphProto();
- graph.node = [node];
- this.model = new onnx.proto.ModelProto();
- this.model.graph = graph;
- this.format = 'ONNX Tensor';
- } catch (error) {
- const message = error && error.message ? error.message : error.toString();
- throw new onnx.Error(`File format is not onnx.TensorProto (${message.replace(/\.$/, '')}).`);
- }
- break;
- }
- case 'graph': {
- // GraphProto
- try {
- const reader = await context.read('protobuf.binary');
- if (this.offset) {
- stream.seek(0);
- }
- this.model = new onnx.proto.ModelProto();
- this.model.graph = onnx.proto.GraphProto.decode(reader);
- this.format = 'ONNX';
- } catch (error) {
- const message = error && error.message ? error.message : error.toString();
- throw new onnx.Error(`File format is not onnx.GraphProto (${message.replace(/\.$/, '')}).`);
- }
- break;
- }
- case 'model': {
- // ModelProto
- try {
- const reader = await context.read('protobuf.binary');
- this.model = onnx.proto.ModelProto.decode(reader);
- this.format = `ONNX${this.model.ir_version ? ` v${this.model.ir_version}` : ''}`;
- } catch (error) {
- const message = error && error.message ? error.message : error.toString();
- throw new onnx.Error(`File format is not onnx.ModelProto (${message.replace(/\.$/, '')}).`);
- }
- break;
- }
- default: {
- throw new onnx.Error('Unsupported ONNX format type.');
- }
- }
- if (this.offset) {
- stream.seek(0);
- }
- break;
- }
- default: {
- throw new onnx.Error('Unsupported ONNX format encoding.');
- }
- }
- const locations = new Set();
- const location = (tensor) => {
- if (onnx.proto && tensor instanceof onnx.proto.SparseTensorProto) {
- location(tensor.indices);
- location(tensor.values);
- } else if (tensor.data_location === onnx.DataLocation.EXTERNAL && Array.isArray(tensor.external_data)) {
- for (const entry of tensor.external_data) {
- if (entry.key === 'location') {
- locations.add(entry.value);
- }
- }
- }
- };
- const model = this.model;
- const queue = model.graph ? [model.graph] : [];
- while (queue.length > 0) {
- const graph = queue.shift();
- if (Array.isArray(graph.initializer)) {
- for (const initializer of graph.initializer) {
- location(initializer);
- }
- }
- if (Array.isArray(graph.sparse_initializer)) {
- for (const sparse_initializer of graph.sparse_initializer) {
- location(sparse_initializer);
- }
- }
- if (Array.isArray(graph.node)) {
- for (const node of graph.node) {
- if (Array.isArray(node.attribute)) {
- for (const attribute of node.attribute) {
- if (attribute.g) {
- queue.push(attribute.g);
- } else if (attribute.t) {
- location(attribute.t);
- } else if (attribute.sparse_tensor) {
- location(attribute.sparse_tensor);
- } else if (Array.isArray(attribute.graphs) && attribute.graphs.length > 0) {
- for (const graph of attribute.graphs) {
- queue.push(graph);
- }
- } else if (Array.isArray(attribute.tensors) && attribute.tensors.length > 0) {
- for (const tensor of attribute.tensors) {
- location(tensor);
- }
- } else if (Array.isArray(attribute.sparse_tensors) && attribute.sparse_tensors.length > 0) {
- for (const tensor of attribute.sparse_tensors) {
- location(tensor);
- }
- }
- }
- }
- }
- }
- }
- for (const name of this.locations.keys()) {
- locations.delete(name);
- }
- for (const name of locations) {
- const location = new onnx.Location(this.context, name);
- this.locations.set(name, location);
- }
- }
- };
- onnx.OrtReader = class {
- static async open(context) {
- const identifier = context.identifier;
- const extension = identifier.lastIndexOf('.') > 0 ? identifier.split('.').pop().toLowerCase() : '';
- const reader = await context.peek('flatbuffers.binary');
- if (reader && reader.identifier === 'ORTM') {
- return new onnx.OrtReader(context);
- }
- const stream = context.stream;
- if (stream && stream.length >= 8 && extension === 'ort') {
- const signature = [0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
- if (signature.length <= stream.length && stream.peek(signature.length).every((value, index) => value === signature[index])) {
- return new onnx.OrtReader(context);
- }
- }
- return null;
- }
- constructor(context) {
- this.name = 'onnx.ort';
- this.context = context;
- }
- async read() {
- onnx.schema = await this.context.require('./onnx-schema');
- onnx.schema = onnx.schema.onnxruntime.fbs;
- try {
- this.graphs = new Set();
- const reader = await this.context.read('flatbuffers.binary');
- const session = onnx.schema.InferenceSession.create(reader);
- this.model = session.model;
- } catch (error) {
- const message = error && error.message ? error.message : error.toString();
- throw new onnx.Error(`File format is not ort.Model (${message.replace(/\.$/, '')}).`);
- }
- const tensor_shape = (value) => {
- if (value && value.dim && Array.isArray(value.dim)) {
- for (const dimension of value.dim) {
- switch (dimension.value.dim_type) {
- case 0:
- return {};
- case 1:
- dimension.dim_value = dimension.value.dim_value;
- delete dimension.value;
- break;
- case 2:
- dimension.dim_param = dimension.value.dim_param;
- delete dimension.value.dim_param;
- break;
- default:
- throw new onnx.Error(`Unknown shape dimension '${JSON.stringify(dimension.value)}'.`);
- }
- }
- }
- return value;
- };
- /* eslint-disable no-use-before-define */
- const node = (value) => {
- value.input = value.inputs;
- value.output = value.outputs;
- value.attribute = value.attributes.map((attribute) => {
- const type = attribute.type;
- if (type === onnx.AttributeType.GRAPH) {
- graph(attribute.g);
- } else if (type === onnx.AttributeType.GRAPHS) {
- for (const graph of attribute.graphs) {
- graph(graph);
- }
- } else if (type === onnx.AttributeType.TYPE_PROTO) {
- attribute.tp = type(attribute.tp);
- } else if (type === onnx.AttributeType.TYPE_PROTOS) {
- attribute.type_protos = attribute.type_protos.map((type) => type(type));
- }
- return attribute;
- });
- delete value.inputs;
- delete value.outputs;
- delete value.attributes;
- return value;
- };
- const tensor_type = (value) => {
- value.shape = tensor_shape(value.shape);
- return value;
- };
- const sequence_type = (value) => {
- value.shape = type(value.elem_type);
- return value;
- };
- const map_type = (value) => {
- value.value_type = type(value.value_type);
- return value;
- };
- /* eslint-enable no-use-before-define */
- const type = (value) => {
- if (value) {
- const type = value.value;
- if (type && type instanceof onnx.schema.TensorTypeAndShape) {
- value.tensor_type = tensor_type(type);
- return value;
- }
- if (type && type instanceof onnx.schema.SequenceType) {
- value.sequence_type = sequence_type(type);
- return value;
- }
- if (type && type instanceof onnx.schema.MapType) {
- value.map_type = map_type(type);
- return value;
- }
- throw new onnx.Error(`Unsupported type value '${JSON.stringify(value.value)}`);
- }
- return null;
- };
- const graph = (value) => {
- if (this.graphs.has(value)) {
- return;
- }
- this.graphs.add(value);
- value.name = this.graphs.size.toString();
- value.node = value.nodes.map((value) => node(value));
- delete value.nodes;
- value.value_info = value.node_args.map((arg) => {
- return {
- name: arg.name,
- doc_string: arg.doc_string,
- type: type(arg.type)
- };
- });
- delete value.node_args;
- const value_info = new Map(value.value_info.map((entry) => [entry.name, entry]));
- value.input = value.inputs.map((input) => {
- return value_info.has(input) ? value_info.get(input) : { name: input };
- });
- delete value.inputs;
- value.output = value.outputs.map((output) => {
- return value_info.has(output) ? value_info.get(output) : { name: output };
- });
- delete value.outputs;
- value.initializer = value.initializers.map((tensor) => {
- tensor.data_location = onnx.DataLocation.DEFAULT;
- return tensor;
- });
- delete value.initializers;
- value.sparse_initializer = value.sparse_initializers.map((tensor) => {
- tensor.values.data_location = onnx.DataLocation.DEFAULT;
- tensor.indices.data_location = onnx.DataLocation.DEFAULT;
- return tensor;
- });
- delete value.sparse_initializers;
- };
- graph(this.model.graph);
- this.model.graph.doc_string = this.model.graph_doc_string;
- delete this.model.graph_doc_string;
- this.format = `ONNX Runtime${this.model.ir_version ? ` v${this.model.ir_version}` : ''}`;
- }
- };
- onnx.JsonReader = class {
- static async open(context) {
- const obj = await context.peek('json');
- if (obj && obj.framework === undefined && obj.graph &&
- (obj.ir_version !== undefined || obj.producer_name !== undefined || Array.isArray(obj.opset_import))) {
- return new onnx.JsonReader(obj);
- }
- return null;
- }
- constructor(obj) {
- this.name = 'onnx.json';
- this.model = obj;
- }
- async read() {
- this.model = this._model(this.model);
- this.format = `ONNX JSON${this.model.ir_version ? ` v${this.model.ir_version}` : ''}`;
- }
- _tensor(value) {
- value.dims = Array.isArray(value.dims) ? value.dims : [];
- if (value.raw_data !== undefined) {
- if (value.raw_data && value.raw_data instanceof Uint8Array === false && value.raw_data.type === 'Buffer' && Array.isArray(value.raw_data.data)) {
- value.data_location = onnx.DataLocation.DEFAULT;
- value.raw_data = new Uint8Array(value.raw_data.data);
- }
- } else if ((Array.isArray(value.float_data) && value.float_data.length > 0) ||
- (Array.isArray(value.int32_data) && value.int32_data.length > 0) ||
- (Array.isArray(value.int64_data) && value.int64_data.length > 0)) {
- value.data_location = onnx.DataLocation.DEFAULT;
- } else {
- throw new onnx.Error(`Unsupported ONNX JSON tensor data type '${JSON.stringify(value.data_type)}'.`);
- }
- return value;
- }
- _sparse_tensor(value) {
- value.indices = this._tensor(value.indices);
- value.values = this._tensor(value.values);
- return value;
- }
- _attribute(value) {
- if (value.ref_attr_name) {
- value.ref_attr_name = value.ref_attr_name.toString();
- } else if (value.type === onnx.AttributeType.FLOATS || (Array.isArray(value.floats) && value.floats.length > 0)) {
- value.floats = value.floats.map((value) => parseFloat(value));
- } else if (value.type === onnx.AttributeType.INTS || (Array.isArray(value.ints) && value.ints.length > 0)) {
- value.ints = value.ints.map((value) => parseInt(value, 10));
- } else if (value.type === onnx.AttributeType.STRINGS || (Array.isArray(value.strings) && value.strings.length > 0)) {
- value.strings = value.strings.map((value) => atob(value));
- } else if (value.type === onnx.AttributeType.TENSORS || (Array.isArray(value.tensors) && value.tensors.length > 0)) {
- value.tensors = value.tensors.map((value) => this._tensor(value));
- } else if (value.type === onnx.AttributeType.GRAPHS || (Array.isArray(value.graphs) && value.graphs.length > 0)) {
- value.graphs = value.graphs.map((value) => this._graph(value));
- } else if (value.type === onnx.AttributeType.SPARSE_TENSORS || (Array.isArray(value.sparse_tensors) && value.sparse_tensors.length > 0)) {
- value.sparse_tensors = value.sparse_tensors.map((item) => this._sparse_tensor(item));
- } else if (value.type === onnx.AttributeType.FLOAT || value.f !== undefined) {
- // continue
- } else if (value.type === onnx.AttributeType.INT || value.i !== undefined) {
- // continue
- } else if (value.type === onnx.AttributeType.STRING || value.s !== undefined) {
- value.s = atob(value.s);
- } else if (value.type === onnx.AttributeType.TENSOR || value.t !== undefined) {
- value.t = this._tensor(value.t);
- } else if (value.type === onnx.AttributeType.GRAPH || value.g !== undefined) {
- value.g = this._graph(value.g);
- } else if (value.type === onnx.AttributeType.SPARSE_TENSOR || value.sparse_tensor !== undefined) {
- value.sparse_tensor = this._sparse_tensor(value.sparse_tensor);
- } else {
- throw new onnx.Error(`Unsupported ONNX JSON attribute type '${JSON.stringify(value.type)}'.`);
- }
- return value;
- }
- _node(value) {
- value.input = Array.isArray(value.input) ? value.input : [];
- value.output = Array.isArray(value.output) ? value.output : [];
- value.attribute = Array.isArray(value.attribute) ? value.attribute.map((value) => this._attribute(value)) : [];
- return value;
- }
- _graph(value) {
- value.node = value.node.map((value) => this._node(value));
- value.initializer = Array.isArray(value.initializer) ? value.initializer.map((value) => this._tensor(value)) : [];
- value.sparse_initializer = Array.isArray(value.sparse_initializer) ? value.sparse_initializer.map((item) => this._sparse_tensor(item)) : [];
- value.input = Array.isArray(value.input) ? value.input : [];
- value.output = Array.isArray(value.output) ? value.output : [];
- return value;
- }
- _function(value) {
- value.node = value.node.map((value) => this._node(value));
- value.input = Array.isArray(value.input) ? value.input : [];
- value.output = Array.isArray(value.output) ? value.output : [];
- value.attribute = Array.isArray(value.attribute) ? value.attribute : [];
- value.attribute_proto = Array.isArray(value.attribute_proto) ? value.attribute_proto.map((value) => this._attribute(value)) : [];
- return value;
- }
- _model(value) {
- value.graph = this._graph(value.graph);
- value.functions = Array.isArray(value.functions) ? value.functions.map((item) => this._function(item)) : [];
- return value;
- }
- };
- onnx.TextReader = class {
- static async open(context) {
- const extension = context.identifier.split('.').pop().toLowerCase();
- const extensions = new Set(['onnx', 'bin', 'data', 'onnxmodel', 'pt', 'pth']);
- if (!extensions.has(extension)) {
- try {
- const stream = context.stream;
- if (stream && stream.length > 2) {
- const buffer = stream.peek(2);
- if (buffer[0] < 0x80 || buffer[0] >= 0xFE) {
- const reader = await context.read('text', 0x10000);
- const lines = [];
- for (let i = 0; i < 32; i++) {
- const line = reader.read('\n');
- if (line === undefined) {
- break;
- }
- lines.push(line);
- }
- const content = lines.join('\n');
- if (/^\s*<\s*ir_version\s*:/m.exec(content) ||
- /^\s*[a-zA-Z][a-zA-Z0-9]*\s*\(.*\)\s=>\s\(/m.exec(content)) {
- return new onnx.TextReader(context);
- }
- }
- }
- } catch {
- // continue regardless of error
- }
- }
- return null;
- }
- constructor(context) {
- this.name = 'onnx.text';
- this._context = context;
- this._dataTypes = new Map(Object.entries(onnx.DataType).map(([key, value]) => [key.toLowerCase(), value]));
- this._attributeTypes = new Map(Object.entries(onnx.AttributeType).map(([key, value]) => [key.toLowerCase(), value]));
- }
- async read() {
- onnx.proto = await this._context.require('./onnx-proto');
- onnx.proto = onnx.proto.onnx;
- try {
- this._decoder = await this._context.read('text.decoder');
- this._position = 0;
- this._char = this._decoder.decode();
- this.model = this._parseModel();
- this.format = 'ONNX Text';
- if (this.model.ir_version !== undefined) {
- const version = typeof this.model.ir_version === 'bigint' ? this.model.ir_version.toNumber() : this.model.ir_version;
- this.format += ` v${version}`;
- }
- delete this._decoder;
- delete this._position;
- delete this._char;
- } catch (error) {
- const message = error && error.message ? error.message : error.toString();
- throw new onnx.Error(`File format is not onnx.ModelProto (${message.replace(/\.$/, '')}).`);
- }
- }
- _seek(position) {
- this._decoder.position = position;
- this._char = '';
- this._next();
- }
- _parseModel() {
- this._skipWhitespace();
- const model = new onnx.proto.ModelProto();
- if (this._match('<')) {
- do {
- const keyword = this._parseIdentifier();
- this._expect(':');
- switch (keyword) {
- case 'ir_version':
- case 'model_version':
- model[keyword] = this._parseInteger();
- break;
- case 'opset_import':
- model[keyword] = this._parseOperatorSetId();
- break;
- case 'producer_name':
- case 'producer_version':
- case 'domain':
- case 'doc_string':
- model[keyword] = this._parseString();
- break;
- case 'metadata_props':
- this._expect('[');
- if (!this._match(']')) {
- do {
- const entry = new onnx.proto.StringStringEntryProto();
- entry.key = this._parseString();
- this._expect(':');
- entry.value = this._parseString();
- model.metadata_props.push(entry);
- } while (this._match(','));
- this._expect(']');
- }
- break;
- default:
- this._throw(`Unknown keyword '${keyword}'.`);
- break;
- }
- } while (this._match(','));
- this._expect('>');
- }
- model.graph = this._parseGraph();
- this._skipWhitespace();
- while (this._char !== undefined) {
- const func = this._parseFunction();
- if (func) {
- model.functions.push(func);
- }
- this._skipWhitespace();
- }
- return model;
- }
- _parseGraph() {
- const graph = new onnx.proto.GraphProto();
- graph.name = this._parseIdentifier();
- if (this._match('(')) {
- if (!this._match(')')) {
- do {
- const valueInfo = this._parseValueInfo();
- if (this._match('=')) {
- const tensor = this._parseTensor(valueInfo.type);
- tensor.name = valueInfo.name;
- graph.initializer.push(tensor);
- }
- graph.input.push(valueInfo);
- }
- while (this._match(','));
- this._expect(')');
- }
- }
- this._expect('=>');
- graph.output = this._parseValueInfoList();
- if (this._match('<')) {
- if (!this._match('>')) {
- do {
- const valueInfo = this._parseValueInfo();
- if (this._match('=')) {
- const tensor = this._parseTensor(valueInfo.type);
- tensor.name = valueInfo.name;
- graph.initializer.push(tensor);
- } else {
- graph.value_info.push(valueInfo);
- }
- }
- while (this._match(','));
- this._expect('>');
- }
- }
- graph.node = this._parseNodeList();
- return graph;
- }
- _parseNodeList() {
- const list = [];
- this._expect('{');
- while (!this._match('}')) {
- list.push(this._parseNode());
- }
- return list;
- }
- _parseNode() {
- const node = new onnx.proto.NodeProto();
- node.output = this._parseIdentifierList();
- this._expect('=');
- let identifier = this._parseIdentifier();
- let domain = '';
- while (this._match('.')) {
- if (domain) {
- domain += '.';
- }
- domain += identifier;
- identifier = this._parseIdentifier();
- }
- node.domain = domain;
- node.op_type = identifier;
- if (this._match(':')) {
- node.overload = this._parseIdentifier();
- }
- node.attribute = this._parseAttributeList();
- this._expect('(');
- node.input = this._parseIdentifierList();
- this._expect(')');
- if (!node.attribute || node.attribute.length === 0) {
- node.attribute = this._parseAttributeList();
- }
- return node;
- }
- _parseAttributeList() {
- const list = [];
- if (this._match('<')) {
- do {
- list.push(this._parseAttribute());
- }
- while (this._match(','));
- this._expect('>');
- }
- return list;
- }
- _parseAttribute() {
- const attribute = new onnx.proto.AttributeProto();
- attribute.name = this._parseIdentifier();
- if (this._match(':')) {
- const type = this._parseIdentifier();
- if (!this._attributeTypes.has(type)) {
- this._throw(`Unexpected attribute type '${type}'.`);
- }
- attribute.type = this._attributeTypes.get(type);
- }
- this._expect('=');
- if (this._match('[')) {
- if (this._match(']')) {
- if (attribute.type === onnx.AttributeType.UNDEFINED) {
- this._throw('Empty list attribute value requires type annotation.');
- }
- switch (attribute.type) {
- case onnx.AttributeType.FLOAT:
- case onnx.AttributeType.INT:
- case onnx.AttributeType.STRING:
- case onnx.AttributeType.TENSOR:
- case onnx.AttributeType.GRAPH:
- case onnx.AttributeType.SPARSE_TENSOR:
- case onnx.AttributeType.TYPE_PROTO:
- this._throw("Singleton attribute value cannot be specified as a list.");
- break;
- default:
- break;
- }
- } else {
- do {
- const value = new onnx.proto.AttributeProto();
- let type = onnx.AttributeType.UNDEFINED;
- switch (attribute.type) {
- case onnx.AttributeType.FLOATS: type = onnx.AttributeType.FLOAT; break;
- case onnx.AttributeType.INTS: type = onnx.AttributeType.INT; break;
- case onnx.AttributeType.STRINGS: type = onnx.AttributeType.STRING; break;
- case onnx.AttributeType.TENSORS: type = onnx.AttributeType.TENSOR; break;
- case onnx.AttributeType.GRAPHS: type = onnx.AttributeType.GRAPH; break;
- case onnx.AttributeType.SPARSE_TENSORS: type = onnx.AttributeType.SPARSE_TENSOR; break;
- case onnx.AttributeType.TYPE_PROTOS: type = onnx.AttributeType.TYPE_PROTO; break;
- default: type = attribute.type; break;
- }
- this._parseAttributeValue(value, type);
- switch (value.type) {
- case onnx.AttributeType.INT:
- attribute.type = onnx.AttributeType.INTS;
- attribute.ints.push(value.i);
- break;
- case onnx.AttributeType.FLOAT:
- attribute.type = onnx.AttributeType.FLOATS;
- attribute.floats.push(value.f);
- break;
- case onnx.AttributeType.STRING:
- attribute.type = onnx.AttributeType.STRINGS;
- attribute.strings.push(value.s);
- break;
- default:
- break;
- }
- }
- while (this._match(','));
- }
- this._expect(']');
- } else {
- this._parseAttributeValue(attribute, attribute.type);
- }
- return attribute;
- }
- _parseAttributeValue(attribute, type) {
- if (this._isAlpha(this._char) || this._char === '_') {
- const identifier = this._peekIdentifier();
- if (this._isType(identifier)) {
- const type = this._parseType(this._parseIdentifier());
- if (!type.tensor_type.elem_type) {
- this._throw('Expected tensor data type.');
- }
- if (!type.tensor_type.shape || !type.tensor_type.shape.dim) {
- this._throw('Expected tensor shape.');
- }
- this._skipWhitespace();
- if (this._char === '{' || this._char === '=' || this._peekIdentifier()) {
- attribute.type = onnx.AttributeType.TENSOR;
- const name = this._parseIdentifier(true);
- this._match('=');
- attribute.t = this._parseTensor(type);
- if (name) {
- attribute.t.name = name;
- }
- } else {
- attribute.type = onnx.AttributeType.TYPE_PROTO;
- attribute.tp = type;
- }
- } else {
- const value = this._peekIdentifier();
- if (value === 'inf' || value === 'infinity' || value === 'nan') {
- attribute.type = onnx.AttributeType.FLOAT;
- attribute.f = this._parseLiteral();
- } else {
- attribute.type = onnx.AttributeType.GRAPH;
- attribute.g = this._parseGraph();
- }
- }
- } else if (this._match('@')) {
- attribute.ref_attr_name = this._parseIdentifier();
- } else {
- const value = this._parseLiteral();
- switch (typeof value) {
- case 'number':
- if (Number.isInteger(value)) {
- attribute.type = onnx.AttributeType.INT;
- attribute.i = value;
- } else {
- attribute.type = onnx.AttributeType.FLOAT;
- attribute.f = value;
- }
- break;
- case 'string':
- attribute.type = onnx.AttributeType.STRING;
- attribute.s = value;
- break;
- default: {
- this._throw(`Unexpected value '${JSON.stringify(value)}'.`);
- }
- }
- }
- if (type !== onnx.AttributeType.UNDEFINED && type !== attribute.type) {
- if (type === onnx.AttributeType.FLOAT && attribute.type === onnx.AttributeType.INT) {
- attribute.type = onnx.AttributeType.FLOAT;
- attribute.f = attribute.i;
- delete attribute.i;
- } else {
- this._throw('Attribute type mismatch.');
- }
- }
- }
- _parseValueInfoList() {
- const list = [];
- this._expect('(');
- if (!this._match(')')) {
- do {
- const value = this._parseValueInfo();
- list.push(value);
- } while (this._match(','));
- this._expect(')');
- }
- return list;
- }
- _parseValueInfo() {
- const valueInfo = new onnx.proto.ValueInfoProto();
- let identifier = this._parseIdentifier();
- if (this._isType(identifier)) {
- valueInfo.type = this._parseType(identifier);
- identifier = this._parseIdentifier();
- }
- valueInfo.name = identifier;
- return valueInfo;
- }
- _parseType(elem_type) {
- const type = new onnx.proto.TypeProto();
- type.tensor_type = new onnx.proto.TypeProto.Tensor();
- type.tensor_type.elem_type = this._dataTypes.get(elem_type);
- if (this._match('[')) {
- if (!this._match(']')) {
- type.tensor_type.shape = this._parseTensorShape();
- this._expect(']');
- }
- } else {
- type.tensor_type.shape = new onnx.proto.TensorShapeProto();
- }
- return type;
- }
- _parseTensorShape() {
- const shape = new onnx.proto.TensorShapeProto();
- do {
- const dimension = new onnx.proto.TensorShapeProto.Dimension();
- if (!this._match('?')) {
- const identifier = this._parseIdentifier(true);
- if (identifier) {
- dimension.dim_param = identifier;
- } else {
- dimension.dim_value = this._parseInteger();
- }
- }
- shape.dim.push(dimension);
- }
- while (this._match(','));
- return shape;
- }
- _parseTensor(type) {
- const tensor = new onnx.proto.TensorProto();
- if (!type.tensor_type || !type.tensor_type.elem_type) {
- this._throw('Expected tensor type.');
- }
- if (!type.tensor_type.shape || !type.tensor_type.shape.dim || !type.tensor_type.shape.dim.every((dim) => dim.dim_value)) {
- this._throw('Expected numeric tensor shape.');
- }
- const elem_type = type.tensor_type.elem_type;
- tensor.data_type = elem_type;
- tensor.dims = type.tensor_type.shape.dim.map((dim) => dim.dim_value);
- this._match('=');
- this._expect('{');
- if (!this._match('}')) {
- do {
- switch (elem_type) {
- case onnx.DataType.INT8:
- case onnx.DataType.INT16:
- case onnx.DataType.INT32:
- case onnx.DataType.UINT8:
- case onnx.DataType.UINT16:
- case onnx.DataType.BOOL:
- tensor.int32_data.push(this._parseInteger());
- break;
- case onnx.DataType.INT64:
- tensor.int64_data.push(this._parseInteger());
- break;
- case onnx.DataType.UINT32:
- case onnx.DataType.UINT64:
- tensor.uint64_data.push(this._parseInteger());
- break;
- case onnx.DataType.FLOAT:
- tensor.float_data.push(this._parseFloat());
- break;
- case onnx.DataType.DOUBLE:
- tensor.double_data.push(this._parseFloat());
- break;
- case onnx.DataType.STRING:
- tensor.string_data.push(this.string());
- break;
- default:
- return this._throw(`Unsupported tensor element type '${elem_type}'.`);
- }
- } while (this._match(','));
- this._expect('}');
- }
- return tensor;
- }
- _parseFunction() {
- const func = new onnx.proto.FunctionProto();
- if (this._match('<')) {
- do {
- const keyword = this._parseIdentifier();
- this._expect(':');
- switch (keyword) {
- case 'opset_import':
- func[keyword] = this._parseOperatorSetId();
- break;
- case 'domain':
- case 'doc_string':
- func[keyword] = this._parseString();
- break;
- case 'overload':
- func[keyword] = this._parseString();
- break;
- default:
- this._throw(`Unknown keyword '${keyword}'.`);
- break;
- }
- }
- while (this._match(','));
- this._expect('>');
- }
- func.name = this._parseIdentifier();
- if (this._match('<')) {
- func.attribute = this._parseIdentifierList();
- this._expect('>');
- }
- if (this._match('(')) {
- func.input = this._parseIdentifierList();
- this._expect(')');
- }
- this._expect('=>');
- if (this._match('(')) {
- func.output = this._parseIdentifierList();
- this._expect(')');
- }
- func.node = this._parseNodeList();
- return func;
- }
- _parseIdentifierList() {
- const list = [];
- const identifier = this._parseIdentifier(true);
- if (identifier) {
- list.push(identifier);
- while (this._match(',')) {
- list.push(this._parseIdentifier());
- }
- }
- return list;
- }
- _peekIdentifier() {
- const index = this._decoder.position;
- const position = this._position;
- const char = this._char;
- const value = this._parseIdentifier(true);
- this._char = char;
- this._position = position;
- this._decoder.position = index;
- return value;
- }
- _parseIdentifier(optional) {
- this._skipWhitespace();
- const value = [];
- if (this._isAlpha(this._char) || this._char === '_') {
- value.push(this._char);
- this._next();
- while (this._isAlpha(this._char) || (this._char >= '0' && this._char <= '9') || this._char === '_') {
- value.push(this._char);
- this._next();
- }
- }
- if (!optional && value.length === 0) {
- this._throw('Identifier expected.');
- }
- return value.join('');
- }
- _parseLiteral() {
- this._skipWhitespace();
- let decimal_point = false;
- if (this._char === '"') {
- const value = [];
- this._next();
- while (this._char !== undefined && this._char !== '"') {
- value.push(this._char);
- this._next();
- }
- if (this._char !== undefined) {
- this._next();
- }
- return value.join('');
- } else if ((this._char >= '0' && this._char <= '9') || this._char === '-') {
- const value = [this._char];
- this._next();
- while ((this._char >= '0' && this._char <= '9') || this._char === '.') {
- if (this._char === '.') {
- if (decimal_point) {
- this._throw();
- }
- decimal_point = true;
- }
- value.push(this._char);
- this._next();
- }
- if (value.length === 0) {
- this._throw('Value expected.');
- }
- if (this._char === 'e' || this._char === 'E') {
- decimal_point = true;
- value.push(this._char);
- this._next();
- if (this._char === '+' || this._char === '-') {
- value.push(this._char);
- this._next();
- }
- while ((this._char >= '0' && this._char <= '9')) {
- value.push(this._char);
- this._next();
- }
- }
- return decimal_point ? Number.parseFloat(value.join('')) : Number.parseInt(value.join(''), 10);
- }
- return undefined;
- }
- _parseInteger() {
- const value = this._parseLiteral();
- if (!Number.isInteger(value)) {
- this._throw('Integer value expected.');
- }
- return value;
- }
- _parseFloat() {
- const value = this._parseLiteral();
- if (typeof value !== 'number') {
- this._throw('Float value expected.');
- }
- return value;
- }
- _parseString() {
- const value = this._parseLiteral();
- if (typeof value !== 'string') {
- this._throw('String value expected.');
- }
- return value;
- }
- _parseOperatorSetId() {
- const list = [];
- this._expect('[');
- if (!this._match(']')) {
- do {
- const value = new onnx.proto.OperatorSetIdProto();
- value.domain = this._parseString();
- this._expect(':');
- value.version = this._parseInteger();
- list.push(value);
- }
- while (this._match(','));
- this._expect(']');
- }
- return list;
- }
- _isAlpha(value) {
- return (value >= 'a' && value <= 'z') || (value >= 'A' && value <= 'Z');
- }
- _isType(identifier) {
- return this._dataTypes.has(identifier) ||
- identifier === 'seq' ||
- identifier === 'map' ||
- identifier === 'optional' ||
- identifier === 'sparse_tensor';
- }
- _match(value) {
- this._skipWhitespace();
- if (this._char !== value[0]) {
- return false;
- }
- if (value.length === 1) {
- this._next();
- return true;
- }
- const position = this._position;
- for (let i = 0; i < value.length; i++) {
- if (this._char !== value[i]) {
- this._seek(position);
- return false;
- }
- this._next();
- }
- return true;
- }
- _expect(value) {
- if (!this._match(value)) {
- this._unexpected();
- }
- return true;
- }
- _skipWhitespace() {
- for (;;) {
- while (this._char === ' ' || this._char === '\n' || this._char === '\r' || this._char === '\t') {
- this._next();
- }
- if (this._char === undefined || this._char !== '#') {
- break;
- }
- while (this._char !== undefined && this._char !== '\n') {
- this._next();
- }
- }
- }
- _next() {
- if (this._char === undefined) {
- this._unexpected();
- }
- this._position = this._decoder.position;
- this._char = this._decoder.decode();
- }
- _unexpected() {
- let c = this._char;
- if (c === undefined) {
- throw new onnx.Error('Unexpected end of input.');
- } else if (c === '"') {
- c = 'string';
- } else if ((c >= '0' && c <= '9') || c === '-') {
- c = 'number';
- } else {
- if (c < ' ' || c > '\x7F') {
- const name = Object.keys(this._escape).filter((key) => this._escape[key] === c);
- c = (name.length === 1) ? `\\${name}` : `\\u${(`000${c.charCodeAt(0).toString(16)}`).slice(-4)}`;
- }
- c = `token '${c}'`;
- }
- this._throw(`Unexpected ${c}`);
- }
- _throw(message) {
- message = message.replace(/\.$/, '');
- throw new onnx.Error(`${message} ${this._location()}`);
- }
- _location() {
- let line = 1;
- let column = 1;
- this._decoder.position = 0;
- let c = '';
- do {
- if (this._decoder.position === this._position) {
- return `at ${line}:${column}.`;
- }
- c = this._decoder.decode();
- if (c === '\n') {
- line++;
- column = 1;
- } else {
- column++;
- }
- }
- while (c !== undefined);
- return `at ${line}:${column}.`;
- }
- };
- onnx.PickleReader = class {
- static async open(context) {
- const identifier = context.identifier;
- const extension = identifier.lastIndexOf('.') > 0 ? identifier.split('.').pop().toLowerCase() : '';
- const stream = context.stream;
- if (extension === 'onnx' && stream && stream.length > 3) {
- const signature = stream.peek(2);
- if (signature[0] === 0x80 && signature[1] < 7) {
- return new onnx.PickleReader();
- }
- }
- return null;
- }
- constructor() {
- this.name = 'onnx.pickle';
- }
- async read() {
- throw new onnx.Error('Unsupported Pickle content.');
- }
- };
- onnx.DataReader = class {
- static async open(context) {
- const identifier = context.identifier.toLowerCase();
- if (identifier.endsWith('.onnx_data') || identifier.endsWith('.onnx.data')) {
- return new onnx.DataReader(context, identifier);
- }
- return null;
- }
- constructor(context, identifier) {
- this.name = 'onnx.data';
- this.context = context;
- this.identifier = identifier;
- this.locations = new Map();
- this.locations.set(identifier, context.stream);
- }
- async read() {
- const file = this.identifier.substring(0, this.identifier.length - 5);
- const context = await this.context.fetch(file);
- const reader = new onnx.ProtoReader(context, 'binary', 'model');
- reader.locations = this.locations;
- await reader.read();
- this.format = reader.format;
- this.model = reader.model;
- }
- };
- onnx.MetaReader = class {
- static async open(context) {
- const identifier = context.identifier.toLowerCase();
- if (identifier.endsWith('.onnx.meta')) {
- const stream = context.stream;
- const buffer = context.stream.peek(Math.min(stream.length, 32));
- const content = String.fromCharCode.apply(null, buffer);
- if (content.startsWith('fileFormatVersion:') || content.startsWith('- !<AssetImportMetadata/')) {
- return new onnx.MetaReader(context, identifier);
- }
- }
- return null;
- }
- constructor(context, identifier) {
- this.name = 'onnx.meta';
- this.context = context;
- this.identifier = identifier;
- this.locations = new Map();
- }
- async read() {
- const file = this.identifier.substring(0, this.identifier.length - 5);
- const context = await this.context.fetch(file);
- const reader = new onnx.ProtoReader(context, 'binary', 'model');
- reader.locations = this.locations;
- await reader.read();
- this.format = reader.format;
- this.model = reader.model;
- }
- };
- onnx.Location = class {
- constructor(context, name) {
- this.context = context;
- this.name = name;
- this.content = new Map();
- }
- async read(offset, length) {
- const key = `${offset}:${length}`;
- if (this.content.has(key)) {
- return this.content.get(key);
- }
- if (!this.promise) {
- this.promise = this.context.fetch(this.name);
- }
- return this.promise.then((content) => {
- const stream = content.stream;
- const position = stream.position;
- stream.seek(offset);
- length = length === -1 ? stream.length - offset : length;
- content = stream.stream(length);
- stream.seek(position);
- this.content.set(key, content);
- return content;
- }).catch(() => {
- return null;
- });
- }
- };
- onnx.Error = class extends Error {
- constructor(message) {
- super(message);
- this.name = 'Error loading ONNX model.';
- }
- };
- export const ModelFactory = onnx.ModelFactory;
|