edgeflowjs 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/backends/index.d.ts +1 -1
- package/dist/backends/index.d.ts.map +1 -1
- package/dist/backends/index.js +1 -1
- package/dist/backends/index.js.map +1 -1
- package/dist/backends/onnx.d.ts +38 -0
- package/dist/backends/onnx.d.ts.map +1 -1
- package/dist/backends/onnx.js +79 -9
- package/dist/backends/onnx.js.map +1 -1
- package/dist/edgeflow.browser.js +28 -5
- package/dist/edgeflow.browser.js.map +2 -2
- package/dist/edgeflow.browser.min.js +18 -18
- package/dist/edgeflow.browser.min.js.map +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -2
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var Kt=Object.defineProperty;var zs=(o,t,e)=>t in o?Kt(o,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):o[t]=e;var ht=(o,t)=>()=>(o&&(t=o(o=0)),t);var Ss=(o,t)=>{for(var e in t)Kt(o,e,{get:t[e],enumerable:!0})};var f=(o,t,e)=>(zs(o,typeof t!="symbol"?t+"":t,e),e);var x,M,B=ht(()=>{"use strict";x=class extends Error{constructor(e,n,s){super(e);f(this,"code");f(this,"details");this.code=n,this.details=s,this.name="EdgeFlowError"}},M={RUNTIME_NOT_AVAILABLE:"RUNTIME_NOT_AVAILABLE",RUNTIME_INIT_FAILED:"RUNTIME_INIT_FAILED",RUNTIME_NOT_INITIALIZED:"RUNTIME_NOT_INITIALIZED",MODEL_NOT_FOUND:"MODEL_NOT_FOUND",MODEL_LOAD_FAILED:"MODEL_LOAD_FAILED",MODEL_INVALID_FORMAT:"MODEL_INVALID_FORMAT",MODEL_NOT_LOADED:"MODEL_NOT_LOADED",INFERENCE_FAILED:"INFERENCE_FAILED",INFERENCE_TIMEOUT:"INFERENCE_TIMEOUT",INFERENCE_CANCELLED:"INFERENCE_CANCELLED",OUT_OF_MEMORY:"OUT_OF_MEMORY",MEMORY_LEAK_DETECTED:"MEMORY_LEAK_DETECTED",TENSOR_SHAPE_MISMATCH:"TENSOR_SHAPE_MISMATCH",TENSOR_DTYPE_MISMATCH:"TENSOR_DTYPE_MISMATCH",TENSOR_DISPOSED:"TENSOR_DISPOSED",PIPELINE_NOT_SUPPORTED:"PIPELINE_NOT_SUPPORTED",PIPELINE_INPUT_INVALID:"PIPELINE_INPUT_INVALID",INVALID_ARGUMENT:"INVALID_ARGUMENT",NOT_IMPLEMENTED:"NOT_IMPLEMENTED",UNKNOWN_ERROR:"UNKNOWN_ERROR"}});function Es(){return`tensor_${++vs}_${Date.now().toString(36)}`}function Xe(o){switch(o){case"float32":return Float32Array;case"float16":return Float32Array;case"int32":return Int32Array;case"int64":return BigInt64Array;case"uint8":case"bool":return Uint8Array;case"int8":return Int8Array;default:throw new x(`Unsupported data type: ${o}`,M.INVALID_ARGUMENT,{dtype:o})}}function ae(o){return o.length===0?1:o.reduce((t,e)=>t*e,1)}function Ds(o){for(let t=0;t<o.length;t++){let e=o[t];if(e===void 0||!Number.isInteger(e)||e<0)throw new x(`Invalid shape dimension at index ${t}: ${e}`,M.INVALID_ARGUMENT,{shape:o,index:t,dimension:e})}}function Yt(o,t,e="float32"){if(Array.isArray(o)&&o.length>0&&Array.isArray(o[0])){let s=o.length,i=o[0].length,r=[];for(let a of o){if(a.length!==i)throw new x("Nested arrays must have consistent dimensions",M.INVALID_ARGUMENT);r.push(...a)}return new w(r,t??[s,i],e)}let n=t??[o.length];return new w(o,n,e)}function ft(o,t="float32"){let e=ae(o),n=Xe(t),s=new n(e);return new w(s,o,t)}function Jt(o,t="float32"){let e=ae(o),n=Xe(t),s=new n(e);return s.fill(1),new w(s,o,t)}function Zt(o,t,e="float32"){let n=ae(o),s=Xe(e),i=new s(n);return i.fill(t),new w(i,o,e)}function en(o,t="float32"){let e=ae(o),n=new Float32Array(e);for(let s=0;s<e;s++)n[s]=Math.random();return new w(n,o,t)}function tn(o,t="float32"){let e=ae(o),n=new Float32Array(e);for(let s=0;s<e;s+=2){let i=Math.random(),r=Math.random(),a=Math.sqrt(-2*Math.log(i)),c=2*Math.PI*r;n[s]=a*Math.cos(c),s+1<e&&(n[s+1]=a*Math.sin(c))}return new w(n,o,t)}function nn(o,t,e=1,n="float32"){t===void 0&&(t=o,o=0);let s=Math.ceil((t-o)/e),i=new Float32Array(s);for(let r=0;r<s;r++)i[r]=o+r*e;return new w(i,[s],n)}function sn(o,t,e=50,n="float32"){let s=new Float32Array(e),i=(t-o)/(e-1);for(let r=0;r<e;r++)s[r]=o+r*i;return new w(s,[e],n)}function rn(o,t="float32"){let e=new Float32Array(o*o);for(let n=0;n<o;n++)e[n*o+n]=1;return new w(e,[o,o],t)}function on(o,t){if(typeof t=="number"){let i=new Float32Array(o.size),r=o.toFloat32Array();for(let a=0;a<o.size;a++)i[a]=(r[a]??0)+t;return new w(i,o.shape,o.dtype)}if(o.size!==t.size)throw new x("Tensor sizes must match for element-wise operations",M.TENSOR_SHAPE_MISMATCH,{aShape:o.shape,bShape:t.shape});let e=new Float32Array(o.size),n=o.toFloat32Array(),s=t.toFloat32Array();for(let i=0;i<o.size;i++)e[i]=(n[i]??0)+(s[i]??0);return new w(e,o.shape,o.dtype)}function an(o,t){if(typeof t=="number"){let i=new Float32Array(o.size),r=o.toFloat32Array();for(let a=0;a<o.size;a++)i[a]=(r[a]??0)-t;return new w(i,o.shape,o.dtype)}if(o.size!==t.size)throw new x("Tensor sizes must match for element-wise operations",M.TENSOR_SHAPE_MISMATCH,{aShape:o.shape,bShape:t.shape});let e=new Float32Array(o.size),n=o.toFloat32Array(),s=t.toFloat32Array();for(let i=0;i<o.size;i++)e[i]=(n[i]??0)-(s[i]??0);return new w(e,o.shape,o.dtype)}function cn(o,t){if(typeof t=="number"){let i=new Float32Array(o.size),r=o.toFloat32Array();for(let a=0;a<o.size;a++)i[a]=(r[a]??0)*t;return new w(i,o.shape,o.dtype)}if(o.size!==t.size)throw new x("Tensor sizes must match for element-wise operations",M.TENSOR_SHAPE_MISMATCH,{aShape:o.shape,bShape:t.shape});let e=new Float32Array(o.size),n=o.toFloat32Array(),s=t.toFloat32Array();for(let i=0;i<o.size;i++)e[i]=(n[i]??0)*(s[i]??0);return new w(e,o.shape,o.dtype)}function pt(o,t){if(typeof t=="number"){let i=new Float32Array(o.size),r=o.toFloat32Array();for(let a=0;a<o.size;a++)i[a]=(r[a]??0)/t;return new w(i,o.shape,o.dtype)}if(o.size!==t.size)throw new x("Tensor sizes must match for element-wise operations",M.TENSOR_SHAPE_MISMATCH,{aShape:o.shape,bShape:t.shape});let e=new Float32Array(o.size),n=o.toFloat32Array(),s=t.toFloat32Array();for(let i=0;i<o.size;i++)e[i]=(n[i]??0)/(s[i]??0);return new w(e,o.shape,o.dtype)}function ln(o,t){if(o.shape.length!==2||t.shape.length!==2)throw new x("matmul requires 2D tensors",M.INVALID_ARGUMENT,{aShape:o.shape,bShape:t.shape});let[e,n]=o.shape,[s,i]=t.shape;if(n!==s)throw new x(`Matrix dimensions incompatible for multiplication: (${e}x${n}) @ (${s}x${i})`,M.TENSOR_SHAPE_MISMATCH,{aShape:o.shape,bShape:t.shape});let r=new Float32Array(e*i),a=o.toFloat32Array(),c=t.toFloat32Array();for(let l=0;l<e;l++)for(let d=0;d<i;d++){let u=0;for(let h=0;h<n;h++)u+=(a[l*n+h]??0)*(c[h*i+d]??0);r[l*i+d]=u}return new w(r,[e,i],o.dtype)}function O(o,t=-1){let e=o.toFloat32Array(),n=new Float32Array(o.size),s=t<0?o.shape.length+t:t;if(s<0||s>=o.shape.length)throw new x(`Invalid axis ${t} for tensor with ${o.shape.length} dimensions`,M.INVALID_ARGUMENT,{axis:t,shape:o.shape});if(o.shape.length===1){let i=-1/0;for(let a=0;a<o.size;a++)(e[a]??0)>i&&(i=e[a]??0);let r=0;for(let a=0;a<o.size;a++)n[a]=Math.exp((e[a]??0)-i),r+=n[a]??0;for(let a=0;a<o.size;a++)n[a]=(n[a]??0)/r;return new w(n,o.shape,o.dtype)}if(o.shape.length===2&&s===1){let[i,r]=o.shape;for(let a=0;a<i;a++){let c=-1/0;for(let d=0;d<r;d++)(e[a*r+d]??0)>c&&(c=e[a*r+d]??0);let l=0;for(let d=0;d<r;d++)n[a*r+d]=Math.exp((e[a*r+d]??0)-c),l+=n[a*r+d]??0;for(let d=0;d<r;d++)n[a*r+d]=(n[a*r+d]??0)/l}return new w(n,o.shape,o.dtype)}throw new x("Softmax currently only supports 1D tensors or 2D tensors along the last axis",M.NOT_IMPLEMENTED,{shape:o.shape,axis:t})}function Qe(o){let t=o.toFloat32Array(),e=new Float32Array(o.size);for(let n=0;n<o.size;n++)e[n]=Math.max(0,t[n]??0);return new w(e,o.shape,o.dtype)}function Ke(o){let t=o.toFloat32Array(),e=new Float32Array(o.size);for(let n=0;n<o.size;n++)e[n]=1/(1+Math.exp(-(t[n]??0)));return new w(e,o.shape,o.dtype)}function dn(o){let t=o.toFloat32Array(),e=new Float32Array(o.size);for(let n=0;n<o.size;n++)e[n]=Math.tanh(t[n]??0);return new w(e,o.shape,o.dtype)}function Ge(o,t){let e=o.toFloat32Array();if(t===void 0){let i=0;for(let r=0;r<o.size;r++)i+=e[r]??0;return i}let n=t<0?o.shape.length+t:t;if(n<0||n>=o.shape.length)throw new x(`Invalid axis ${t} for tensor with ${o.shape.length} dimensions`,M.INVALID_ARGUMENT,{axis:t,shape:o.shape});let s=[...o.shape];if(s.splice(n,1),s.length===0){let i=0;for(let r=0;r<o.size;r++)i+=e[r]??0;return i}if(o.shape.length===2){let[i,r]=o.shape;if(n===0){let a=new Float32Array(r);for(let c=0;c<r;c++)for(let l=0;l<i;l++)a[c]=(a[c]??0)+(e[l*r+c]??0);return new w(a,[r],o.dtype)}else{let a=new Float32Array(i);for(let c=0;c<i;c++)for(let l=0;l<r;l++)a[c]=(a[c]??0)+(e[c*r+l]??0);return new w(a,[i],o.dtype)}}throw new x("Sum along axis currently only supports up to 2D tensors",M.NOT_IMPLEMENTED,{shape:o.shape,axis:t})}function un(o,t){if(t===void 0)return Ge(o)/o.size;let e=Ge(o,t);if(typeof e=="number")return e/(o.shape[t]??1);let n=o.shape[t]??1;return pt(e,n)}function hn(o,t){let e=o.toFloat32Array();if(t===void 0){let s=0,i=e[0]??-1/0;for(let r=1;r<o.size;r++)(e[r]??-1/0)>i&&(i=e[r]??-1/0,s=r);return s}let n=t<0?o.shape.length+t:t;if(o.shape.length===2&&n===1){let[s,i]=o.shape,r=new Float32Array(s);for(let a=0;a<s;a++){let c=0,l=e[a*i]??-1/0;for(let d=1;d<i;d++)(e[a*i+d]??-1/0)>l&&(l=e[a*i+d]??-1/0,c=d);r[a]=c}return new w(r,[s],"int32")}throw new x("Argmax along axis currently only supports 2D tensors along the last axis",M.NOT_IMPLEMENTED,{shape:o.shape,axis:t})}function fn(o,t=0){if(o.length===0)throw new x("Cannot concatenate empty array of tensors",M.INVALID_ARGUMENT);if(o.length===1)return o[0]?.clone()??ft([0]);let e=o[0];if(!e)throw new x("First tensor is undefined",M.INVALID_ARGUMENT);let n=t<0?e.shape.length+t:t;for(let r=1;r<o.length;r++){let a=o[r];if(a){if(a.shape.length!==e.shape.length)throw new x("All tensors must have the same number of dimensions",M.TENSOR_SHAPE_MISMATCH);for(let c=0;c<e.shape.length;c++)if(c!==n&&e.shape[c]!==a.shape[c])throw new x(`Shape mismatch at dimension ${c}`,M.TENSOR_SHAPE_MISMATCH)}}let s=[...e.shape],i=0;for(let r of o)r&&(i+=r.shape[n]??0);if(s[n]=i,e.shape.length===1){let r=new Float32Array(i),a=0;for(let c of o)c&&(r.set(c.toFloat32Array(),a),a+=c.size);return new w(r,s,e.dtype)}throw new x("Concatenation currently only supports 1D tensors",M.NOT_IMPLEMENTED)}var vs,w,C=ht(()=>{"use strict";B();vs=0;w=class o{constructor(t,e,n="float32"){f(this,"id");f(this,"dtype");f(this,"shape");f(this,"size");f(this,"_data");f(this,"_isDisposed",!1);Ds(e),this.id=Es(),this.dtype=n,this.shape=Object.freeze([...e]),this.size=ae(this.shape);let s=this.size;if(t.length!==s)throw new x(`Data length (${t.length}) does not match shape ${JSON.stringify(e)} (expected ${s})`,M.TENSOR_SHAPE_MISMATCH,{dataLength:t.length,expectedSize:s,shape:e});if(t instanceof Array){let i=Xe(n);if(this._data=new i(t.length),n==="int64"){let r=this._data;for(let a=0;a<t.length;a++)r[a]=BigInt(Math.round(t[a]??0))}else for(let r=0;r<t.length;r++)this._data[r]=t[r]??0}else this._data=t}get data(){return this.checkDisposed(),this._data}get isDisposed(){return this._isDisposed}checkDisposed(){if(this._isDisposed)throw new x("Cannot access disposed tensor",M.TENSOR_DISPOSED,{tensorId:this.id})}toFloat32Array(){if(this.checkDisposed(),this._data instanceof Float32Array)return this._data;let t=new Float32Array(this.size);for(let e=0;e<this.size;e++)t[e]=Number(this._data[e]??0);return t}toArray(){if(this.checkDisposed(),this.dtype==="int64"){let t=this._data,e=[];for(let n=0;n<t.length;n++)e.push(Number(t[n]));return e}return Array.from(this._data)}clone(){this.checkDisposed();let t=this._data.constructor,e=new t(this._data);return new o(e,this.shape,this.dtype)}dispose(){this._isDisposed||(this._isDisposed=!0,Object.assign(this,{_data:null}))}get(...t){if(this.checkDisposed(),t.length!==this.shape.length)throw new x(`Expected ${this.shape.length} indices, got ${t.length}`,M.INVALID_ARGUMENT,{expectedIndices:this.shape.length,gotIndices:t.length});let e=0,n=1;for(let s=this.shape.length-1;s>=0;s--){let i=t[s]??0,r=this.shape[s]??1;if(i<0||i>=r)throw new x(`Index ${i} out of bounds for dimension ${s} with size ${r}`,M.INVALID_ARGUMENT,{index:i,dimension:s,size:r});e+=i*n,n*=r}return Number(this._data[e]??0)}set(t,...e){if(this.checkDisposed(),e.length!==this.shape.length)throw new x(`Expected ${this.shape.length} indices, got ${e.length}`,M.INVALID_ARGUMENT,{expectedIndices:this.shape.length,gotIndices:e.length});let n=0,s=1;for(let i=this.shape.length-1;i>=0;i--){let r=e[i]??0,a=this.shape[i]??1;if(r<0||r>=a)throw new x(`Index ${r} out of bounds for dimension ${i} with size ${a}`,M.INVALID_ARGUMENT,{index:r,dimension:i,size:a});n+=r*s,s*=a}this._data[n]=t}reshape(t){this.checkDisposed();let e=ae(t);if(e!==this.size)throw new x(`Cannot reshape tensor of size ${this.size} to shape ${JSON.stringify(t)} (size ${e})`,M.TENSOR_SHAPE_MISMATCH,{currentSize:this.size,newSize:e,newShape:t});let n=this._data.constructor,s=new n(this._data);return new o(s,t,this.dtype)}transpose(){if(this.checkDisposed(),this.shape.length!==2)throw new x("Transpose is currently only supported for 2D tensors",M.NOT_IMPLEMENTED,{shape:this.shape});let[t,e]=this.shape,n=new Float32Array(this.size);for(let s=0;s<t;s++)for(let i=0;i<e;i++)n[i*t+s]=Number(this._data[s*e+i]??0);return new o(n,[e,t],this.dtype)}toString(){return`Tensor(shape=[${this.shape.join(", ")}], dtype=${this.dtype})`}}});var yn={};Ss(yn,{cancelPreload:()=>zt,clearModelCache:()=>kt,deleteCachedModel:()=>bt,getCachedModel:()=>Tt,getModelCacheStats:()=>It,getPreloadStatus:()=>At,getPreloadedModel:()=>St,isModelCached:()=>ve,loadModelData:()=>v,preloadModel:()=>xt,preloadModels:()=>Mt});async function qs(o){try{let t=await fetch(o,{method:"HEAD"}),e=t.headers.get("Accept-Ranges"),n=t.headers.get("Content-Length"),s=t.headers.get("ETag")??void 0;return{supports:e==="bytes",size:n?parseInt(n,10):0,etag:s}}catch{return{supports:!1,size:0}}}async function js(o,t,e,n){let s=new AbortController,i=setTimeout(()=>s.abort(),n);try{let r=await fetch(o,{headers:{Range:`bytes=${t}-${e}`},signal:s.signal});if(r.status!==206&&r.status!==200)throw new Error(`HTTP ${r.status}: ${r.statusText}`);return await r.arrayBuffer()}finally{clearTimeout(i)}}async function Hs(o,t){let{chunkSize:e=5*1024*1024,parallelConnections:n=4,timeout:s=3e4,onProgress:i}=t,{supports:r,size:a,etag:c}=await qs(o);if(!r||a<e*2)return gn(o,s,i);let l=await $.getDownloadState(o);if(!l||c&&l.totalSize!==a){let I=Math.ceil(a/e),S=[];for(let D=0;D<I;D++){let _=D*e,A=Math.min(_+e-1,a-1);S.push({index:D,start:_,end:A,downloaded:!1})}l={url:o,totalSize:a,downloadedSize:0,chunks:S,startedAt:Date.now()},await $.deleteModel(o)}let d=l.chunks.filter(I=>!I.downloaded),u=l.downloadedSize,p=Date.now(),m=u,y=()=>{if(!i)return;let I=Date.now(),S=(I-p)/1e3,D=u-m,_=S>0?D/S:0,A=a-u,me=_>0?A/_*1e3:0;i({loaded:u,total:a,percent:u/a*100,speed:_,eta:me,currentChunk:l.chunks.filter(R=>R.downloaded).length,totalChunks:l.chunks.length}),p=I,m=u},g=[...d],T=new Map;for(;g.length>0||T.size>0;){for(;g.length>0&&T.size<n;){let I=g.shift(),S=(async()=>{try{let D=await js(o,I.start,I.end,s);await $.saveChunk(o,I.index,D),I.downloaded=!0,u+=D.byteLength,l.downloadedSize=u,await $.saveDownloadState(l),y()}finally{T.delete(I.index)}})();T.set(I.index,S)}T.size>0&&await Promise.race(T.values())}let k=await $.getChunks(o),b=new Uint8Array(a),z=0;for(let I of k)b.set(new Uint8Array(I),z),z+=I.byteLength;return await $.saveMeta({url:o,size:a,etag:c,cachedAt:Date.now(),chunks:k.length,complete:!0}),await $.deleteDownloadState(o),b.buffer}async function gn(o,t,e){let n=new AbortController,s=setTimeout(()=>n.abort(),t);try{let i=await fetch(o,{signal:n.signal});if(!i.ok)throw new Error(`HTTP ${i.status}: ${i.statusText}`);let r=i.headers.get("Content-Length"),a=r?parseInt(r,10):0;if(!i.body||!e||a===0)return await i.arrayBuffer();let c=i.body.getReader(),l=[],d=0,u=Date.now();for(;;){let{done:m,value:y}=await c.read();if(m)break;l.push(y),d+=y.length;let g=(Date.now()-u)/1e3,T=g>0?d/g:0,k=a-d,b=T>0?k/T*1e3:0;e({loaded:d,total:a,percent:d/a*100,speed:T,eta:b})}let h=new Uint8Array(d),p=0;for(let m of l)h.set(m,p),p+=m.length;return h.buffer}finally{clearTimeout(s)}}async function v(o,t={}){let{cache:e=!0,forceDownload:n=!1,resumable:s=!0}=t;if(e&&!n){let r=await $.getModel(o);if(r){let a=new Uint8Array(r)[0];if(a===60||a===123||r.byteLength<1024)console.warn(`[edgeFlow.js] Cached model for ${o} appears corrupt (${r.byteLength} bytes, first byte 0x${a?.toString(16)}). Evicting and re-downloading.`),await $.deleteModel(o);else return console.log(`\u2713 Model loaded from cache: ${o}`),t.onProgress?.({loaded:r.byteLength,total:r.byteLength,percent:100,speed:0,eta:0}),r}}let i;return s?i=await Hs(o,t):i=await gn(o,t.timeout??3e4,t.onProgress),e&&(s||(await $.saveChunk(o,0,i),await $.saveMeta({url:o,size:i.byteLength,cachedAt:Date.now(),chunks:1,complete:!0}))),i}function xt(o,t={}){return Se.preload(o,t)}function Mt(o,t={}){return Promise.all(o.map(({url:e,priority:n})=>Se.preload(e,{...t,priority:n})))}async function ve(o){return(await $.getMeta(o))?.complete??!1}async function Tt(o){return $.getModel(o)}async function bt(o){return $.deleteModel(o)}async function kt(){return $.clear()}async function It(){return $.getStats()}function At(o){return Se.getStatus(o)}function zt(o){Se.cancel(o)}async function St(o){return Se.get(o)}var Bs,j,Q,J,yt,$,wt,Se,G=ht(()=>{"use strict";Bs="edgeflow-model-cache",j="meta",Q="chunks",J="download-state",yt=class{constructor(){f(this,"db",null);f(this,"dbPromise",null)}async openDB(){return this.db?this.db:this.dbPromise?this.dbPromise:(this.dbPromise=new Promise((t,e)=>{let n=indexedDB.open(Bs,1);n.onupgradeneeded=s=>{let i=s.target.result;i.objectStoreNames.contains(j)||i.createObjectStore(j,{keyPath:"url"}),i.objectStoreNames.contains(Q)||i.createObjectStore(Q,{keyPath:["url","index"]}).createIndex("url","url",{unique:!1}),i.objectStoreNames.contains(J)||i.createObjectStore(J,{keyPath:"url"})},n.onsuccess=()=>{this.db=n.result,t(this.db)},n.onerror=()=>e(n.error)}),this.dbPromise)}async getMeta(t){let e=await this.openDB();return new Promise((n,s)=>{let a=e.transaction(j,"readonly").objectStore(j).get(t);a.onsuccess=()=>n(a.result??null),a.onerror=()=>s(a.error)})}async saveMeta(t){try{await this.putInStore(j,t)}catch(e){if(this.isQuotaError(e)){await this.evictOldest(t.size);try{await this.putInStore(j,t)}catch{console.warn("[edgeFlow.js] IndexedDB quota exceeded even after eviction; skipping cache.")}}else throw e}}async saveChunk(t,e,n){try{await this.putInStore(Q,{url:t,index:e,data:n})}catch(s){if(this.isQuotaError(s)){await this.evictOldest(n.byteLength);try{await this.putInStore(Q,{url:t,index:e,data:n})}catch{console.warn("[edgeFlow.js] IndexedDB quota exceeded even after eviction; skipping cache for chunk.")}}else throw s}}async putInStore(t,e){let n=await this.openDB();return new Promise((s,i)=>{let r=n.transaction(t,"readwrite");r.objectStore(t).put(e),r.oncomplete=()=>s(),r.onerror=()=>i(r.error)})}isQuotaError(t){return t instanceof DOMException?t.name==="QuotaExceededError"||t.code===22:!1}async evictOldest(t){let e=await this.openDB(),n=await new Promise((i,r)=>{let l=e.transaction(j,"readonly").objectStore(j).getAll();l.onsuccess=()=>i(l.result??[]),l.onerror=()=>r(l.error)});n.sort((i,r)=>i.cachedAt-r.cachedAt);let s=0;for(let i of n){if(s>=t)break;await this.deleteModel(i.url),s+=i.size}}async getChunks(t){let e=await this.openDB();return new Promise((n,s)=>{let c=e.transaction(Q,"readonly").objectStore(Q).index("url").getAll(t);c.onsuccess=()=>{let l=c.result;l.sort((d,u)=>d.index-u.index),n(l.map(d=>d.data))},c.onerror=()=>s(c.error)})}async getModel(t){let e=await this.getMeta(t);if(!e||!e.complete)return null;let n=await this.getChunks(t);if(n.length===0)return null;let s=n.reduce((a,c)=>a+c.byteLength,0),i=new Uint8Array(s),r=0;for(let a of n)i.set(new Uint8Array(a),r),r+=a.byteLength;return i.buffer}async saveDownloadState(t){try{await this.putInStore(J,t)}catch(e){if(this.isQuotaError(e))console.warn("[edgeFlow.js] IndexedDB quota exceeded saving download state; resume may not work.");else throw e}}async getDownloadState(t){let e=await this.openDB();return new Promise((n,s)=>{let a=e.transaction(J,"readonly").objectStore(J).get(t);a.onsuccess=()=>n(a.result??null),a.onerror=()=>s(a.error)})}async deleteDownloadState(t){let e=await this.openDB();return new Promise((n,s)=>{let i=e.transaction(J,"readwrite");i.objectStore(J).delete(t),i.oncomplete=()=>n(),i.onerror=()=>s(i.error)})}async deleteModel(t){let e=await this.openDB();await new Promise((s,i)=>{let r=e.transaction(j,"readwrite");r.objectStore(j).delete(t),r.oncomplete=()=>s(),r.onerror=()=>i(r.error)}),(await this.getChunks(t)).length>0&&await new Promise((s,i)=>{let r=e.transaction(Q,"readwrite"),l=r.objectStore(Q).index("url").openCursor(IDBKeyRange.only(t));l.onsuccess=d=>{let u=d.target.result;u&&(u.delete(),u.continue())},r.oncomplete=()=>s(),r.onerror=()=>i(r.error)}),await this.deleteDownloadState(t)}async clear(){let t=await this.openDB(),e=[j,Q,J];for(let n of e)await new Promise((s,i)=>{let r=t.transaction(n,"readwrite");r.objectStore(n).clear(),r.oncomplete=()=>s(),r.onerror=()=>i(r.error)})}async getStats(){let t=await this.openDB();return new Promise((e,n)=>{let r=t.transaction(j,"readonly").objectStore(j).getAll();r.onsuccess=()=>{let a=r.result;e({models:a.filter(c=>c.complete).length,totalSize:a.reduce((c,l)=>c+(l.complete?l.size:0),0)})},r.onerror=()=>n(r.error)})}},$=new yt;wt=class{constructor(){f(this,"tasks",new Map);f(this,"queue",[]);f(this,"maxConcurrent",2);f(this,"activeCount",0)}preload(t,e={}){let n=this.tasks.get(t);if(n)return n.promise;let s,i,r=new Promise((l,d)=>{s=l,i=d}),a={url:t,priority:e.priority??0,options:e,promise:r,resolve:s,reject:i,status:"pending"};this.tasks.set(t,a);let c=this.queue.findIndex(l=>{let d=this.tasks.get(l);return d&&d.priority<a.priority});return c===-1?this.queue.push(t):this.queue.splice(c,0,t),this.processQueue(),r}async processQueue(){for(;this.queue.length>0&&this.activeCount<this.maxConcurrent;){let t=this.queue.shift();if(!t)break;let e=this.tasks.get(t);!e||e.status!=="pending"||(this.activeCount++,e.status="loading",this.downloadTask(e).finally(()=>{this.activeCount--,this.processQueue()}))}}async downloadTask(t){try{let e=await v(t.url,t.options);t.status="complete",t.resolve(e)}catch(e){t.status="error",t.reject(e instanceof Error?e:new Error(String(e)))}}isPreloaded(t){return this.tasks.get(t)?.status==="complete"}getStatus(t){return this.tasks.get(t)?.status??"not_found"}async get(t){let e=this.tasks.get(t);return e&&(e.status==="complete"||e.status==="loading")?e.promise:null}cancel(t){let e=this.tasks.get(t);e&&e.status==="pending"&&(this.tasks.delete(t),this.queue=this.queue.filter(n=>n!==t),e.reject(new Error("Preload cancelled")))}clear(){for(let[,t]of this.tasks)t.status==="pending"&&t.reject(new Error("Preload cleared"));this.tasks.clear(),this.queue=[]}},Se=new wt});B();C();B();var mt=class{constructor(t,e,n,s){f(this,"id");f(this,"modelId");f(this,"priority");f(this,"createdAt");f(this,"_status","pending");f(this,"_startedAt");f(this,"_completedAt");f(this,"_result");f(this,"_error");f(this,"_executor");f(this,"_resolvers",[]);f(this,"_cancelled",!1);this.id=t,this.modelId=e,this.priority=n,this.createdAt=Date.now(),this._executor=s}get status(){return this._status}get startedAt(){return this._startedAt}get completedAt(){return this._completedAt}get result(){return this._result}get error(){return this._error}cancel(){if(this._status==="pending"){this._cancelled=!0,this._status="cancelled",this._completedAt=Date.now();let t=new x("Task was cancelled",M.INFERENCE_CANCELLED,{taskId:this.id});for(let{reject:e}of this._resolvers)e(t);this._resolvers=[]}}wait(){return this._status==="completed"?Promise.resolve(this._result):this._status==="failed"?Promise.reject(this._error):this._status==="cancelled"?Promise.reject(new x("Task was cancelled",M.INFERENCE_CANCELLED,{taskId:this.id})):new Promise((t,e)=>{this._resolvers.push({resolve:t,reject:e})})}async execute(){if(!this._cancelled){this._status="running",this._startedAt=Date.now();try{this._result=await this._executor(),this._status="completed",this._completedAt=Date.now();for(let{resolve:t}of this._resolvers)t(this._result)}catch(t){this._error=t instanceof Error?t:new Error(String(t)),this._status="failed",this._completedAt=Date.now();for(let{reject:e}of this._resolvers)e(this._error)}this._resolvers=[]}}},pn={critical:0,high:1,normal:2,low:3},gt=class{constructor(){f(this,"items",[])}get length(){return this.items.length}isEmpty(){return this.items.length===0}enqueue(t){let e=!1;for(let n=0;n<this.items.length;n++){let s=this.items[n];if(s&&pn[t.priority]<pn[s.priority]){this.items.splice(n,0,t),e=!0;break}}e||this.items.push(t)}dequeue(){return this.items.shift()}peek(){return this.items[0]}remove(t){let e=this.items.findIndex(n=>n.id===t);if(e!==-1){let[n]=this.items.splice(e,1);return n}}getAll(){return[...this.items]}clear(){this.items=[]}};var Fs=0;function Ps(){return`task_${++Fs}_${Date.now().toString(36)}`}var _s={maxConcurrentTasks:4,maxConcurrentPerModel:1,defaultTimeout:3e4,enableBatching:!1,maxBatchSize:32,batchTimeout:50,maxRetries:0,retryBaseDelay:1e3,circuitBreaker:!1,circuitBreakerThreshold:5,circuitBreakerResetTimeout:3e4},ke=class{constructor(t={}){f(this,"options");f(this,"queues",new Map);f(this,"runningTasks",new Map);f(this,"allTasks",new Map);f(this,"batchers",new Map);f(this,"listeners",new Map);f(this,"circuits",new Map);f(this,"globalRunningCount",0);f(this,"isProcessing",!1);f(this,"disposed",!1);this.options={..._s,...t}}getCircuit(t){let e=this.circuits.get(t);return e||(e={failures:0,state:"closed",lastFailure:0},this.circuits.set(t,e)),e}isCircuitOpen(t){if(!this.options.circuitBreaker)return!1;let e=this.getCircuit(t);return e.state==="closed"?!1:e.state==="open"?Date.now()-e.lastFailure>this.options.circuitBreakerResetTimeout?(e.state="half-open",!1):!0:!1}circuitSuccess(t){if(!this.options.circuitBreaker)return;let e=this.getCircuit(t);e.failures=0,e.state="closed"}circuitFailure(t){if(!this.options.circuitBreaker)return;let e=this.getCircuit(t);e.failures++,e.lastFailure=Date.now(),e.failures>=this.options.circuitBreakerThreshold&&(e.state="open",this.emit("inference:error",{modelId:t,error:new Error(`Circuit breaker opened after ${e.failures} consecutive failures`)}))}getQueue(t){let e=this.queues.get(t);return e||(e=new gt,this.queues.set(t,e)),e}getRunningSet(t){let e=this.runningTasks.get(t);return e||(e=new Set,this.runningTasks.set(t,e)),e}canStartTask(t){if(this.globalRunningCount>=this.options.maxConcurrentTasks)return!1;let e=this.runningTasks.get(t);return!(e&&e.size>=this.options.maxConcurrentPerModel)}async processQueue(){if(this.isProcessing||this.disposed)return;this.isProcessing=!0;try{let e=[];for(let[n,s]of this.queues)for(;!s.isEmpty()&&this.canStartTask(n);){let i=s.dequeue();i&&i.status==="pending"&&(e.push(i),this.getRunningSet(n).add(i.id),this.globalRunningCount++)}await Promise.all(e.map(async n=>{this.emit("inference:start",{taskId:n.id,modelId:n.modelId});try{await n.execute(),this.emit("inference:complete",{taskId:n.id,modelId:n.modelId,duration:(n.completedAt??0)-(n.startedAt??0)})}catch(s){this.emit("inference:error",{taskId:n.id,modelId:n.modelId,error:s})}finally{let s=this.runningTasks.get(n.modelId);s&&s.delete(n.id),this.globalRunningCount--}}))}finally{this.isProcessing=!1}let t=!1;for(let e of this.queues.values())if(!e.isEmpty()){t=!0;break}t&&setTimeout(()=>this.processQueue(),0)}schedule(t,e,n="normal"){if(this.disposed)throw new x("Scheduler has been disposed",M.RUNTIME_NOT_INITIALIZED);if(this.isCircuitOpen(t))throw new x(`Circuit breaker is open for model ${t} \u2014 too many consecutive failures. Retry after ${this.options.circuitBreakerResetTimeout}ms.`,M.INFERENCE_FAILED,{modelId:t});let s=this.options.maxRetries,i=this.options.retryBaseDelay,r=s>0?async()=>{let l;for(let d=0;d<=s;d++)try{let u=await e();return this.circuitSuccess(t),u}catch(u){if(l=u instanceof Error?u:new Error(String(u)),this.circuitFailure(t),d<s){let h=i*Math.pow(2,d);await new Promise(p=>setTimeout(p,h))}}throw l}:async()=>{try{let l=await e();return this.circuitSuccess(t),l}catch(l){throw this.circuitFailure(t),l}},a=new mt(Ps(),t,n,r);return this.allTasks.set(a.id,a),this.getQueue(t).enqueue(a),this.processQueue(),a}scheduleWithTimeout(t,e,n=this.options.defaultTimeout,s="normal"){let i=()=>new Promise((r,a)=>{let c=setTimeout(()=>{a(new x(`Task timed out after ${n}ms`,M.INFERENCE_TIMEOUT,{timeout:n}))},n);e().then(l=>{clearTimeout(c),r(l)}).catch(l=>{clearTimeout(c),a(l)})});return this.schedule(t,i,s)}async scheduleAll(t){let e=t.map(({modelId:n,executor:s,priority:i})=>this.schedule(n,s,i));return Promise.all(e.map(n=>n.wait()))}getTask(t){return this.allTasks.get(t)}cancelTask(t){let e=this.allTasks.get(t);if(e&&e.status==="pending"){e.cancel();for(let n of this.queues.values())n.remove(t);return!0}return!1}cancelAllForModel(t){let e=this.queues.get(t);if(!e)return 0;let n=0;for(let s of e.getAll())s.status==="pending"&&(s.cancel(),n++);return e.clear(),n}getStats(){let t={totalTasks:this.allTasks.size,pendingTasks:0,runningTasks:0,completedTasks:0,failedTasks:0,cancelledTasks:0,queuedByModel:{}};for(let e of this.allTasks.values())switch(e.status){case"pending":t.pendingTasks++;break;case"running":t.runningTasks++;break;case"completed":t.completedTasks++;break;case"failed":t.failedTasks++;break;case"cancelled":t.cancelledTasks++;break}for(let[e,n]of this.queues)t.queuedByModel[e]=n.length;return t}on(t,e){let n=this.listeners.get(t);n||(n=new Set,this.listeners.set(t,n)),n.add(e)}off(t,e){let n=this.listeners.get(t);n&&n.delete(e)}emit(t,e){let n={type:t,timestamp:Date.now(),data:e},s=this.listeners.get(t);if(s)for(let i of s)try{i(n)}catch(r){console.error("Error in event listener:",r)}}clearHistory(){for(let[t,e]of this.allTasks)(e.status==="completed"||e.status==="failed"||e.status==="cancelled")&&this.allTasks.delete(t)}dispose(){this.disposed=!0;for(let t of this.queues.values()){for(let e of t.getAll())e.cancel();t.clear()}for(let t of this.batchers.values())t.clear();this.queues.clear(),this.runningTasks.clear(),this.allTasks.clear(),this.batchers.clear(),this.listeners.clear()}},ye=null;function Ie(){return ye||(ye=new ke),ye}function mn(o){ye&&ye.dispose(),ye=o}function Ls(o){mn(new ke(o))}var Ns={initialSize:67108864,maxSize:536870912,growthFactor:1.5,autoGC:!0,gcThreshold:.8},W=class W{constructor(t={}){f(this,"config");f(this,"resources",new Map);f(this,"disposers",new Map);f(this,"listeners",new Map);f(this,"allocated",0);f(this,"peak",0);f(this,"gcScheduled",!1);f(this,"disposed",!1);this.config={...Ns,...t}}static getInstance(){return W.instance||(W.instance=new W),W.instance}static configure(t){W.instance&&console.warn("MemoryManager already initialized, configuration may not apply"),W.instance=new W(t)}track(t,e){if(this.disposed)return;let n=this.estimateTensorSize(t);this.resources.set(t.id,{id:t.id,type:"tensor",size:n,createdAt:Date.now(),stackTrace:this.captureStackTrace()}),e&&this.disposers.set(t.id,e),this.allocated+=n,this.peak=Math.max(this.peak,this.allocated),this.checkMemoryThreshold()}trackModel(t,e){if(this.disposed)return;let n=t.metadata.sizeBytes;this.resources.set(t.id,{id:t.id,type:"model",size:n,createdAt:Date.now(),stackTrace:this.captureStackTrace()}),e&&this.disposers.set(t.id,e),this.allocated+=n,this.peak=Math.max(this.peak,this.allocated),this.checkMemoryThreshold()}untrack(t){let e=this.resources.get(t);e&&(this.allocated-=e.size,this.resources.delete(t),this.disposers.delete(t))}release(t){let e=typeof t=="string"?t:t.id,n=this.disposers.get(e);if(n)try{n()}catch(s){console.error("Error disposing resource:",s)}this.untrack(e)}estimateTensorSize(t){let e=this.getBytesPerElement(t.dtype);return t.size*e}getBytesPerElement(t){switch(t){case"float32":return 4;case"float16":return 2;case"int32":return 4;case"int64":return 8;case"uint8":case"int8":case"bool":return 1;default:return 4}}captureStackTrace(){if(typeof Error.captureStackTrace=="function"){let t={};return Error.captureStackTrace(t,this.captureStackTrace),t.stack}return new Error().stack}checkMemoryThreshold(){if(!this.config.autoGC)return;let t=this.allocated/this.config.maxSize;t>=this.config.gcThreshold&&!this.gcScheduled&&(this.gcScheduled=!0,this.emit("memory:warning",{allocated:this.allocated,maxSize:this.config.maxSize,usage:t}),setTimeout(()=>{this.gc(),this.gcScheduled=!1},0))}gc(t=!1,e=5*60*1e3){this.emit("memory:gc",{before:this.allocated});let n=Date.now(),s=[];for(let[i,r]of this.resources)n-r.createdAt>e&&s.push(i);if(t)for(let i of s)this.release(i);this.emit("memory:gc",{after:this.allocated,evicted:t?s.length:0,potentialCleanup:s.length})}async measureBrowserMemory(){try{if(typeof performance<"u"&&"measureUserAgentSpecificMemory"in performance)return await performance.measureUserAgentSpecificMemory()}catch{}return null}getDeviceMemory(){try{if(typeof navigator<"u"&&"deviceMemory"in navigator)return navigator.deviceMemory??null}catch{}return null}getStats(){let t=0,e=0;for(let n of this.resources.values())n.type==="tensor"?t++:e++;return{allocated:this.allocated,used:this.allocated,peak:this.peak,tensorCount:t,modelCount:e}}getResourceDetails(){return Array.from(this.resources.values())}detectLeaks(t=10*60*1e3){let e=Date.now(),n=[];for(let s of this.resources.values())e-s.createdAt>t&&n.push(s);return n}on(t,e){let n=this.listeners.get(t);n||(n=new Set,this.listeners.set(t,n)),n.add(e)}off(t,e){let n=this.listeners.get(t);n&&n.delete(e)}emit(t,e){let n={type:t,timestamp:Date.now(),data:e},s=this.listeners.get(t);if(s)for(let i of s)try{i(n)}catch(r){console.error("Error in event listener:",r)}}resetStats(){this.peak=this.allocated}disposeAll(){for(let t of this.resources.keys())this.release(t)}dispose(){this.disposeAll(),this.disposed=!0,this.listeners.clear(),W.instance=null}};f(W,"instance",null);var ce=W,Ae=class o{constructor(t){f(this,"resources",[]);f(this,"children",[]);f(this,"parent",null);t&&(this.parent=t,t.children.push(this))}track(t){return this.resources.push(t),t}createChild(){return new o(this)}keep(t){let e=this.resources.indexOf(t);return e!==-1&&this.resources.splice(e,1),t}dispose(){for(let t of this.children)t.dispose();this.children=[];for(let t=this.resources.length-1;t>=0;t--)try{this.resources[t]?.dispose()}catch(e){console.error("Error disposing resource in scope:",e)}if(this.resources=[],this.parent){let t=this.parent.children.indexOf(this);t!==-1&&this.parent.children.splice(t,1),this.parent=null}}};async function Cs(o){let t=new Ae;try{return await o(t)}finally{t.dispose()}}function Os(o){let t=new Ae;try{return o(t)}finally{t.dispose()}}var ze=class{constructor(t={}){f(this,"maxSize");f(this,"maxModels");f(this,"cache",new Map);f(this,"currentSize",0);this.maxSize=t.maxSize??256*1024*1024,this.maxModels=t.maxModels??5}get(t){let e=this.cache.get(t);if(e)return e.lastAccess=Date.now(),e.model}set(t,e){let n=e.metadata.sizeBytes;for(;(this.currentSize+n>this.maxSize||this.cache.size>=this.maxModels)&&this.cache.size>0;)this.evictLRU();this.cache.set(t,{model:e,size:n,lastAccess:Date.now()}),this.currentSize+=n}delete(t){let e=this.cache.get(t);return e?(e.model.dispose(),this.currentSize-=e.size,this.cache.delete(t),!0):!1}has(t){return this.cache.has(t)}evictLRU(){let t=null,e=1/0;for(let[n,s]of this.cache)s.lastAccess<e&&(e=s.lastAccess,t=n);t&&this.delete(t)}clear(){for(let t of this.cache.values())t.model.dispose();this.cache.clear(),this.currentSize=0}getStats(){return{size:this.currentSize,count:this.cache.size,maxSize:this.maxSize,maxModels:this.maxModels}}};function q(){return ce.getInstance()}function $s(){return ce.getInstance().getStats()}function Rs(o){ce.getInstance().release(o)}function Us(){ce.getInstance().gc()}B();var Ye=new Map,Z=new Map,vt=["webgpu","webnn","wasm"],le=class le{constructor(){f(this,"listeners",new Map);f(this,"defaultRuntime","auto")}static getInstance(){return le.instance||(le.instance=new le),le.instance}register(t,e){Ye.set(t,e)}async getRuntime(t="auto"){if(t==="auto")return this.getBestRuntime();let e=Z.get(t);if(e)return e;let n=Ye.get(t);if(!n)throw new x(`Runtime '${t}' is not registered`,M.RUNTIME_NOT_AVAILABLE,{runtime:t});if(e=n(),!await e.isAvailable())throw new x(`Runtime '${t}' is not available in this environment`,M.RUNTIME_NOT_AVAILABLE,{runtime:t});try{await e.initialize()}catch(i){throw new x(`Failed to initialize runtime '${t}': ${i instanceof Error?i.message:String(i)}`,M.RUNTIME_INIT_FAILED,{runtime:t,error:i})}return Z.set(t,e),this.emit("runtime:ready",{runtime:t}),e}async getBestRuntime(){for(let t of vt)try{let e=Z.get(t);if(e)return e;let n=Ye.get(t);if(!n)continue;let s=n();if(await s.isAvailable())return await s.initialize(),Z.set(t,s),this.emit("runtime:ready",{runtime:t}),s}catch{continue}throw new x("No runtime available. Please ensure WebGPU, WebNN, or WASM is supported.",M.RUNTIME_NOT_AVAILABLE,{triedRuntimes:vt})}async detectAvailableRuntimes(){let t=new Map;for(let e of vt){let n=Ye.get(e);if(!n){t.set(e,!1);continue}try{let s=n();t.set(e,await s.isAvailable())}catch{t.set(e,!1)}}return t}async getCapabilities(t){return(await this.getRuntime(t)).capabilities}setDefaultRuntime(t){this.defaultRuntime=t}getDefaultRuntimeType(){return this.defaultRuntime}disposeRuntime(t){let e=Z.get(t);e&&(e.dispose(),Z.delete(t))}disposeAll(){for(let[t,e]of Z)e.dispose(),Z.delete(t)}on(t,e){let n=this.listeners.get(t);n||(n=new Set,this.listeners.set(t,n)),n.add(e)}off(t,e){let n=this.listeners.get(t);n&&n.delete(e)}emit(t,e){let n={type:t,timestamp:Date.now(),data:e},s=this.listeners.get(t);if(s)for(let i of s)try{i(n)}catch(r){console.error("Error in event listener:",r)}}};f(le,"instance",null);var V=le,Vs=0;function Ws(){return`model_${++Vs}_${Date.now().toString(36)}`}var H=class{constructor(t,e,n){f(this,"id");f(this,"metadata");f(this,"runtime");f(this,"_isLoaded",!0);f(this,"_dispose");this.id=Ws(),this.metadata=t,this.runtime=e,this._dispose=n}get isLoaded(){return this._isLoaded}dispose(){this._isLoaded&&(this._isLoaded=!1,this._dispose(),q().untrack(this.id))}};async function we(o,t={}){let n=await V.getInstance().getRuntime(t.runtime??"auto"),{loadModelData:s}=await Promise.resolve().then(()=>(G(),yn)),i=await s(o,{cache:t.cache??!0,resumable:t.resumable??!0,chunkSize:t.chunkSize,forceDownload:t.forceDownload,onProgress:t.onProgress?a=>{t.onProgress(a.percent/100)}:void 0});return await n.loadModel(i,t)}async function F(o,t={}){return(await V.getInstance().getRuntime(t.runtime??"auto")).loadModel(o,t)}async function X(o,t){if(!o.isLoaded)throw new x("Model has been disposed",M.MODEL_NOT_LOADED,{modelId:o.id});let n=await V.getInstance().getRuntime(o.runtime);return Ie().schedule(o.id,()=>n.run(o,t)).wait()}async function U(o,t){if(!o.isLoaded)throw new x("Model has been disposed",M.MODEL_NOT_LOADED,{modelId:o.id});let n=await V.getInstance().getRuntime(o.runtime);if(!("runNamed"in n))throw new x("Runtime does not support named inputs",M.INFERENCE_FAILED,{modelId:o.id});return Ie().schedule(o.id,()=>n.runNamed(o,t)).wait()}async function Gs(o,t){let e=Ie(),s=await V.getInstance().getRuntime(o.runtime),i=t.map(r=>e.schedule(o.id,()=>s.run(o,r)));return Promise.all(i.map(r=>r.wait()))}function Xs(){return V.getInstance()}function de(o,t){V.getInstance().register(o,t)}async function Qs(){return V.getInstance().getBestRuntime()}async function Ee(){return V.getInstance().detectAvailableRuntimes()}var De=new Map,Et=new Map,Je=[];async function Ks(o){if(De.has(o.name)){console.warn(`[edgeFlow.js] Plugin "${o.name}" is already registered \u2014 skipping.`);return}if(o.setup&&await o.setup(),o.pipelines)for(let[t,e]of Object.entries(o.pipelines))Et.set(t,e);if(o.backends)for(let[t,e]of Object.entries(o.backends))de(t,e.factory);o.middleware&&Je.push(...o.middleware),De.set(o.name,o)}function Dt(o){return Et.get(o)}function Ys(){return Je}function Js(){return Array.from(De.values()).map(o=>({name:o.name,version:o.version}))}function Zs(o){let t=De.get(o);if(!t)return!1;if(t.pipelines)for(let e of Object.keys(t.pipelines))Et.delete(e);if(t.middleware)for(let e of t.middleware){let n=Je.indexOf(e);n!==-1&&Je.splice(n,1)}return De.delete(o),!0}var Fe=null;async function wn(){if(Fe)return Fe;let o=typeof navigator<"u"?navigator.hardwareConcurrency??2:2,t=typeof navigator<"u"&&"deviceMemory"in navigator?navigator.deviceMemory??null:null,e=typeof navigator<"u"?/Android|iPhone|iPad|iPod|Mobile/i.test(navigator.userAgent):!1,n=!1,s;if(typeof navigator<"u"&&"gpu"in navigator)try{let l=await navigator.gpu.requestAdapter();if(n=l!=null,l&&typeof l=="object")try{let d=l.info;d&&(s=`${d.vendor??""} ${d.architecture??""}`.trim()||void 0)}catch{}}catch{}let i=!1;if(typeof navigator<"u"&&"ml"in navigator)try{let l=navigator.ml;l&&(i=await l.createContext()!=null)}catch{}let r;return n&&o>=8&&(t===null||t>=8)?r="high":o>=4&&(t===null||t>=4)?r="medium":r="low",e&&r==="high"&&(r="medium"),Fe={tier:r,cores:o,memoryGiB:t,webgpu:n,webnn:i,recommendedBatchSize:r==="high"?32:r==="medium"?8:1,recommendedConcurrency:r==="high"?4:r==="medium"?2:1,mobile:e,gpuInfo:s},Fe}function xn(o){return o.tier==="high"&&o.webgpu?"float16":(o.tier==="medium","int8")}async function er(){let o=await wn();return{quantization:xn(o),executionProvider:o.webgpu?"webgpu":"wasm",batchSize:o.recommendedBatchSize,useWorker:o.cores>=4}}function tr(){Fe=null}B();C();var xe={STORAGE:128,COPY_SRC:4,COPY_DST:8,MAP_READ:1},Mn={COMPUTE:4},Pe=class{constructor(){f(this,"name","webgpu");f(this,"adapter",null);f(this,"device",null);f(this,"models",new Map);f(this,"initialized",!1)}get capabilities(){return{concurrency:!0,quantization:!0,float16:!0,dynamicShapes:!1,maxBatchSize:64,availableMemory:this.device?.limits.maxBufferSize??256*1024*1024}}async isAvailable(){if(typeof navigator>"u"||!navigator.gpu)return!1;try{return await navigator.gpu.requestAdapter()!==null}catch{return!1}}async initialize(){if(!this.initialized){if(!navigator.gpu)throw new x("WebGPU is not supported in this browser",M.RUNTIME_NOT_AVAILABLE);if(this.adapter=await navigator.gpu.requestAdapter({powerPreference:"high-performance"}),!this.adapter)throw new x("Failed to get WebGPU adapter",M.RUNTIME_INIT_FAILED);this.device=await this.adapter.requestDevice({requiredFeatures:[],requiredLimits:{}}),this.device.lost.then(t=>{console.error("WebGPU device was lost:",t.message),this.initialized=!1,this.device=null}),this.initialized=!0}}async loadModel(t,e={}){this.ensureInitialized();let n=this.parseModelData(t),s={shaders:new Map,pipelines:new Map,weights:new Map,bindGroupLayouts:[],config:n};await this.uploadWeights(t,s),await this.createPipelines(s);let i=`webgpu_${Date.now().toString(36)}`;this.models.set(i,s);let r={name:n.name||e.metadata?.name||"unknown",version:n.version,inputs:n.inputs.map(c=>({name:c.name,dtype:c.dtype,shape:c.shape})),outputs:n.outputs.map(c=>({name:c.name,dtype:c.dtype,shape:c.shape})),sizeBytes:t.byteLength,quantization:e.quantization??"float32",format:"edgeflow"},a=new H(r,"webgpu",()=>this.unloadModel(i));return q().trackModel(a,()=>a.dispose()),a}async run(t,e){return this.ensureInitialized(),this.executeModel(e,t.metadata)}async executeModel(t,e){let n=this.device,s=[];for(let i of e.outputs){let r=i.shape.reduce((d,u)=>d*u,1),a=n.createBuffer({size:r*4,usage:xe.STORAGE|xe.COPY_SRC}),c=n.createBuffer({size:r*4,usage:xe.MAP_READ|xe.COPY_DST}),l=new Float32Array(r);if(t.length>0&&t[0]){let d=t[0].toFloat32Array();for(let u=0;u<Math.min(r,d.length);u++)l[u]=d[u]??0}s.push(new w(l,i.shape,"float32")),a.destroy(),c.destroy()}return s}parseModelData(t){try{let e=new TextDecoder,n=e.decode(new Uint8Array(t,0,Math.min(1024,t.byteLength)));if(n.trim().startsWith("{")){let s=n.indexOf(`
|
|
1
|
+
var en=Object.defineProperty;var Fs=(o,t,e)=>t in o?en(o,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):o[t]=e;var mt=(o,t)=>()=>(o&&(t=o(o=0)),t);var Ps=(o,t)=>{for(var e in t)en(o,e,{get:t[e],enumerable:!0})};var f=(o,t,e)=>(Fs(o,typeof t!="symbol"?t+"":t,e),e);var x,M,B=mt(()=>{"use strict";x=class extends Error{constructor(e,n,s){super(e);f(this,"code");f(this,"details");this.code=n,this.details=s,this.name="EdgeFlowError"}},M={RUNTIME_NOT_AVAILABLE:"RUNTIME_NOT_AVAILABLE",RUNTIME_INIT_FAILED:"RUNTIME_INIT_FAILED",RUNTIME_NOT_INITIALIZED:"RUNTIME_NOT_INITIALIZED",MODEL_NOT_FOUND:"MODEL_NOT_FOUND",MODEL_LOAD_FAILED:"MODEL_LOAD_FAILED",MODEL_INVALID_FORMAT:"MODEL_INVALID_FORMAT",MODEL_NOT_LOADED:"MODEL_NOT_LOADED",INFERENCE_FAILED:"INFERENCE_FAILED",INFERENCE_TIMEOUT:"INFERENCE_TIMEOUT",INFERENCE_CANCELLED:"INFERENCE_CANCELLED",OUT_OF_MEMORY:"OUT_OF_MEMORY",MEMORY_LEAK_DETECTED:"MEMORY_LEAK_DETECTED",TENSOR_SHAPE_MISMATCH:"TENSOR_SHAPE_MISMATCH",TENSOR_DTYPE_MISMATCH:"TENSOR_DTYPE_MISMATCH",TENSOR_DISPOSED:"TENSOR_DISPOSED",PIPELINE_NOT_SUPPORTED:"PIPELINE_NOT_SUPPORTED",PIPELINE_INPUT_INVALID:"PIPELINE_INPUT_INVALID",INVALID_ARGUMENT:"INVALID_ARGUMENT",NOT_IMPLEMENTED:"NOT_IMPLEMENTED",UNKNOWN_ERROR:"UNKNOWN_ERROR"}});function Ls(){return`tensor_${++_s}_${Date.now().toString(36)}`}function Ke(o){switch(o){case"float32":return Float32Array;case"float16":return Float32Array;case"int32":return Int32Array;case"int64":return BigInt64Array;case"uint8":case"bool":return Uint8Array;case"int8":return Int8Array;default:throw new x(`Unsupported data type: ${o}`,M.INVALID_ARGUMENT,{dtype:o})}}function ae(o){return o.length===0?1:o.reduce((t,e)=>t*e,1)}function Ns(o){for(let t=0;t<o.length;t++){let e=o[t];if(e===void 0||!Number.isInteger(e)||e<0)throw new x(`Invalid shape dimension at index ${t}: ${e}`,M.INVALID_ARGUMENT,{shape:o,index:t,dimension:e})}}function tn(o,t,e="float32"){if(Array.isArray(o)&&o.length>0&&Array.isArray(o[0])){let s=o.length,i=o[0].length,r=[];for(let a of o){if(a.length!==i)throw new x("Nested arrays must have consistent dimensions",M.INVALID_ARGUMENT);r.push(...a)}return new w(r,t??[s,i],e)}let n=t??[o.length];return new w(o,n,e)}function gt(o,t="float32"){let e=ae(o),n=Ke(t),s=new n(e);return new w(s,o,t)}function nn(o,t="float32"){let e=ae(o),n=Ke(t),s=new n(e);return s.fill(1),new w(s,o,t)}function sn(o,t,e="float32"){let n=ae(o),s=Ke(e),i=new s(n);return i.fill(t),new w(i,o,e)}function rn(o,t="float32"){let e=ae(o),n=new Float32Array(e);for(let s=0;s<e;s++)n[s]=Math.random();return new w(n,o,t)}function on(o,t="float32"){let e=ae(o),n=new Float32Array(e);for(let s=0;s<e;s+=2){let i=Math.random(),r=Math.random(),a=Math.sqrt(-2*Math.log(i)),c=2*Math.PI*r;n[s]=a*Math.cos(c),s+1<e&&(n[s+1]=a*Math.sin(c))}return new w(n,o,t)}function an(o,t,e=1,n="float32"){t===void 0&&(t=o,o=0);let s=Math.ceil((t-o)/e),i=new Float32Array(s);for(let r=0;r<s;r++)i[r]=o+r*e;return new w(i,[s],n)}function cn(o,t,e=50,n="float32"){let s=new Float32Array(e),i=(t-o)/(e-1);for(let r=0;r<e;r++)s[r]=o+r*i;return new w(s,[e],n)}function ln(o,t="float32"){let e=new Float32Array(o*o);for(let n=0;n<o;n++)e[n*o+n]=1;return new w(e,[o,o],t)}function dn(o,t){if(typeof t=="number"){let i=new Float32Array(o.size),r=o.toFloat32Array();for(let a=0;a<o.size;a++)i[a]=(r[a]??0)+t;return new w(i,o.shape,o.dtype)}if(o.size!==t.size)throw new x("Tensor sizes must match for element-wise operations",M.TENSOR_SHAPE_MISMATCH,{aShape:o.shape,bShape:t.shape});let e=new Float32Array(o.size),n=o.toFloat32Array(),s=t.toFloat32Array();for(let i=0;i<o.size;i++)e[i]=(n[i]??0)+(s[i]??0);return new w(e,o.shape,o.dtype)}function un(o,t){if(typeof t=="number"){let i=new Float32Array(o.size),r=o.toFloat32Array();for(let a=0;a<o.size;a++)i[a]=(r[a]??0)-t;return new w(i,o.shape,o.dtype)}if(o.size!==t.size)throw new x("Tensor sizes must match for element-wise operations",M.TENSOR_SHAPE_MISMATCH,{aShape:o.shape,bShape:t.shape});let e=new Float32Array(o.size),n=o.toFloat32Array(),s=t.toFloat32Array();for(let i=0;i<o.size;i++)e[i]=(n[i]??0)-(s[i]??0);return new w(e,o.shape,o.dtype)}function hn(o,t){if(typeof t=="number"){let i=new Float32Array(o.size),r=o.toFloat32Array();for(let a=0;a<o.size;a++)i[a]=(r[a]??0)*t;return new w(i,o.shape,o.dtype)}if(o.size!==t.size)throw new x("Tensor sizes must match for element-wise operations",M.TENSOR_SHAPE_MISMATCH,{aShape:o.shape,bShape:t.shape});let e=new Float32Array(o.size),n=o.toFloat32Array(),s=t.toFloat32Array();for(let i=0;i<o.size;i++)e[i]=(n[i]??0)*(s[i]??0);return new w(e,o.shape,o.dtype)}function yt(o,t){if(typeof t=="number"){let i=new Float32Array(o.size),r=o.toFloat32Array();for(let a=0;a<o.size;a++)i[a]=(r[a]??0)/t;return new w(i,o.shape,o.dtype)}if(o.size!==t.size)throw new x("Tensor sizes must match for element-wise operations",M.TENSOR_SHAPE_MISMATCH,{aShape:o.shape,bShape:t.shape});let e=new Float32Array(o.size),n=o.toFloat32Array(),s=t.toFloat32Array();for(let i=0;i<o.size;i++)e[i]=(n[i]??0)/(s[i]??0);return new w(e,o.shape,o.dtype)}function fn(o,t){if(o.shape.length!==2||t.shape.length!==2)throw new x("matmul requires 2D tensors",M.INVALID_ARGUMENT,{aShape:o.shape,bShape:t.shape});let[e,n]=o.shape,[s,i]=t.shape;if(n!==s)throw new x(`Matrix dimensions incompatible for multiplication: (${e}x${n}) @ (${s}x${i})`,M.TENSOR_SHAPE_MISMATCH,{aShape:o.shape,bShape:t.shape});let r=new Float32Array(e*i),a=o.toFloat32Array(),c=t.toFloat32Array();for(let l=0;l<e;l++)for(let d=0;d<i;d++){let u=0;for(let h=0;h<n;h++)u+=(a[l*n+h]??0)*(c[h*i+d]??0);r[l*i+d]=u}return new w(r,[e,i],o.dtype)}function O(o,t=-1){let e=o.toFloat32Array(),n=new Float32Array(o.size),s=t<0?o.shape.length+t:t;if(s<0||s>=o.shape.length)throw new x(`Invalid axis ${t} for tensor with ${o.shape.length} dimensions`,M.INVALID_ARGUMENT,{axis:t,shape:o.shape});if(o.shape.length===1){let i=-1/0;for(let a=0;a<o.size;a++)(e[a]??0)>i&&(i=e[a]??0);let r=0;for(let a=0;a<o.size;a++)n[a]=Math.exp((e[a]??0)-i),r+=n[a]??0;for(let a=0;a<o.size;a++)n[a]=(n[a]??0)/r;return new w(n,o.shape,o.dtype)}if(o.shape.length===2&&s===1){let[i,r]=o.shape;for(let a=0;a<i;a++){let c=-1/0;for(let d=0;d<r;d++)(e[a*r+d]??0)>c&&(c=e[a*r+d]??0);let l=0;for(let d=0;d<r;d++)n[a*r+d]=Math.exp((e[a*r+d]??0)-c),l+=n[a*r+d]??0;for(let d=0;d<r;d++)n[a*r+d]=(n[a*r+d]??0)/l}return new w(n,o.shape,o.dtype)}throw new x("Softmax currently only supports 1D tensors or 2D tensors along the last axis",M.NOT_IMPLEMENTED,{shape:o.shape,axis:t})}function Ye(o){let t=o.toFloat32Array(),e=new Float32Array(o.size);for(let n=0;n<o.size;n++)e[n]=Math.max(0,t[n]??0);return new w(e,o.shape,o.dtype)}function Je(o){let t=o.toFloat32Array(),e=new Float32Array(o.size);for(let n=0;n<o.size;n++)e[n]=1/(1+Math.exp(-(t[n]??0)));return new w(e,o.shape,o.dtype)}function pn(o){let t=o.toFloat32Array(),e=new Float32Array(o.size);for(let n=0;n<o.size;n++)e[n]=Math.tanh(t[n]??0);return new w(e,o.shape,o.dtype)}function Qe(o,t){let e=o.toFloat32Array();if(t===void 0){let i=0;for(let r=0;r<o.size;r++)i+=e[r]??0;return i}let n=t<0?o.shape.length+t:t;if(n<0||n>=o.shape.length)throw new x(`Invalid axis ${t} for tensor with ${o.shape.length} dimensions`,M.INVALID_ARGUMENT,{axis:t,shape:o.shape});let s=[...o.shape];if(s.splice(n,1),s.length===0){let i=0;for(let r=0;r<o.size;r++)i+=e[r]??0;return i}if(o.shape.length===2){let[i,r]=o.shape;if(n===0){let a=new Float32Array(r);for(let c=0;c<r;c++)for(let l=0;l<i;l++)a[c]=(a[c]??0)+(e[l*r+c]??0);return new w(a,[r],o.dtype)}else{let a=new Float32Array(i);for(let c=0;c<i;c++)for(let l=0;l<r;l++)a[c]=(a[c]??0)+(e[c*r+l]??0);return new w(a,[i],o.dtype)}}throw new x("Sum along axis currently only supports up to 2D tensors",M.NOT_IMPLEMENTED,{shape:o.shape,axis:t})}function mn(o,t){if(t===void 0)return Qe(o)/o.size;let e=Qe(o,t);if(typeof e=="number")return e/(o.shape[t]??1);let n=o.shape[t]??1;return yt(e,n)}function gn(o,t){let e=o.toFloat32Array();if(t===void 0){let s=0,i=e[0]??-1/0;for(let r=1;r<o.size;r++)(e[r]??-1/0)>i&&(i=e[r]??-1/0,s=r);return s}let n=t<0?o.shape.length+t:t;if(o.shape.length===2&&n===1){let[s,i]=o.shape,r=new Float32Array(s);for(let a=0;a<s;a++){let c=0,l=e[a*i]??-1/0;for(let d=1;d<i;d++)(e[a*i+d]??-1/0)>l&&(l=e[a*i+d]??-1/0,c=d);r[a]=c}return new w(r,[s],"int32")}throw new x("Argmax along axis currently only supports 2D tensors along the last axis",M.NOT_IMPLEMENTED,{shape:o.shape,axis:t})}function yn(o,t=0){if(o.length===0)throw new x("Cannot concatenate empty array of tensors",M.INVALID_ARGUMENT);if(o.length===1)return o[0]?.clone()??gt([0]);let e=o[0];if(!e)throw new x("First tensor is undefined",M.INVALID_ARGUMENT);let n=t<0?e.shape.length+t:t;for(let r=1;r<o.length;r++){let a=o[r];if(a){if(a.shape.length!==e.shape.length)throw new x("All tensors must have the same number of dimensions",M.TENSOR_SHAPE_MISMATCH);for(let c=0;c<e.shape.length;c++)if(c!==n&&e.shape[c]!==a.shape[c])throw new x(`Shape mismatch at dimension ${c}`,M.TENSOR_SHAPE_MISMATCH)}}let s=[...e.shape],i=0;for(let r of o)r&&(i+=r.shape[n]??0);if(s[n]=i,e.shape.length===1){let r=new Float32Array(i),a=0;for(let c of o)c&&(r.set(c.toFloat32Array(),a),a+=c.size);return new w(r,s,e.dtype)}throw new x("Concatenation currently only supports 1D tensors",M.NOT_IMPLEMENTED)}var _s,w,C=mt(()=>{"use strict";B();_s=0;w=class o{constructor(t,e,n="float32"){f(this,"id");f(this,"dtype");f(this,"shape");f(this,"size");f(this,"_data");f(this,"_isDisposed",!1);Ns(e),this.id=Ls(),this.dtype=n,this.shape=Object.freeze([...e]),this.size=ae(this.shape);let s=this.size;if(t.length!==s)throw new x(`Data length (${t.length}) does not match shape ${JSON.stringify(e)} (expected ${s})`,M.TENSOR_SHAPE_MISMATCH,{dataLength:t.length,expectedSize:s,shape:e});if(t instanceof Array){let i=Ke(n);if(this._data=new i(t.length),n==="int64"){let r=this._data;for(let a=0;a<t.length;a++)r[a]=BigInt(Math.round(t[a]??0))}else for(let r=0;r<t.length;r++)this._data[r]=t[r]??0}else this._data=t}get data(){return this.checkDisposed(),this._data}get isDisposed(){return this._isDisposed}checkDisposed(){if(this._isDisposed)throw new x("Cannot access disposed tensor",M.TENSOR_DISPOSED,{tensorId:this.id})}toFloat32Array(){if(this.checkDisposed(),this._data instanceof Float32Array)return this._data;let t=new Float32Array(this.size);for(let e=0;e<this.size;e++)t[e]=Number(this._data[e]??0);return t}toArray(){if(this.checkDisposed(),this.dtype==="int64"){let t=this._data,e=[];for(let n=0;n<t.length;n++)e.push(Number(t[n]));return e}return Array.from(this._data)}clone(){this.checkDisposed();let t=this._data.constructor,e=new t(this._data);return new o(e,this.shape,this.dtype)}dispose(){this._isDisposed||(this._isDisposed=!0,Object.assign(this,{_data:null}))}get(...t){if(this.checkDisposed(),t.length!==this.shape.length)throw new x(`Expected ${this.shape.length} indices, got ${t.length}`,M.INVALID_ARGUMENT,{expectedIndices:this.shape.length,gotIndices:t.length});let e=0,n=1;for(let s=this.shape.length-1;s>=0;s--){let i=t[s]??0,r=this.shape[s]??1;if(i<0||i>=r)throw new x(`Index ${i} out of bounds for dimension ${s} with size ${r}`,M.INVALID_ARGUMENT,{index:i,dimension:s,size:r});e+=i*n,n*=r}return Number(this._data[e]??0)}set(t,...e){if(this.checkDisposed(),e.length!==this.shape.length)throw new x(`Expected ${this.shape.length} indices, got ${e.length}`,M.INVALID_ARGUMENT,{expectedIndices:this.shape.length,gotIndices:e.length});let n=0,s=1;for(let i=this.shape.length-1;i>=0;i--){let r=e[i]??0,a=this.shape[i]??1;if(r<0||r>=a)throw new x(`Index ${r} out of bounds for dimension ${i} with size ${a}`,M.INVALID_ARGUMENT,{index:r,dimension:i,size:a});n+=r*s,s*=a}this._data[n]=t}reshape(t){this.checkDisposed();let e=ae(t);if(e!==this.size)throw new x(`Cannot reshape tensor of size ${this.size} to shape ${JSON.stringify(t)} (size ${e})`,M.TENSOR_SHAPE_MISMATCH,{currentSize:this.size,newSize:e,newShape:t});let n=this._data.constructor,s=new n(this._data);return new o(s,t,this.dtype)}transpose(){if(this.checkDisposed(),this.shape.length!==2)throw new x("Transpose is currently only supported for 2D tensors",M.NOT_IMPLEMENTED,{shape:this.shape});let[t,e]=this.shape,n=new Float32Array(this.size);for(let s=0;s<t;s++)for(let i=0;i<e;i++)n[i*t+s]=Number(this._data[s*e+i]??0);return new o(n,[e,t],this.dtype)}toString(){return`Tensor(shape=[${this.shape.join(", ")}], dtype=${this.dtype})`}}});var Tn={};Ps(Tn,{cancelPreload:()=>Et,clearModelCache:()=>zt,deleteCachedModel:()=>At,getCachedModel:()=>It,getModelCacheStats:()=>St,getPreloadStatus:()=>vt,getPreloadedModel:()=>Dt,isModelCached:()=>ve,loadModelData:()=>v,preloadModel:()=>bt,preloadModels:()=>kt});async function Gs(o){try{let t=await fetch(o,{method:"HEAD"}),e=t.headers.get("Accept-Ranges"),n=t.headers.get("Content-Length"),s=t.headers.get("ETag")??void 0;return{supports:e==="bytes",size:n?parseInt(n,10):0,etag:s}}catch{return{supports:!1,size:0}}}async function Xs(o,t,e,n){let s=new AbortController,i=setTimeout(()=>s.abort(),n);try{let r=await fetch(o,{headers:{Range:`bytes=${t}-${e}`},signal:s.signal});if(r.status!==206&&r.status!==200)throw new Error(`HTTP ${r.status}: ${r.statusText}`);return await r.arrayBuffer()}finally{clearTimeout(i)}}async function Qs(o,t){let{chunkSize:e=5*1024*1024,parallelConnections:n=4,timeout:s=3e4,onProgress:i}=t,{supports:r,size:a,etag:c}=await Gs(o);if(!r||a<e*2)return Mn(o,s,i);let l=await R.getDownloadState(o);if(!l||c&&l.totalSize!==a){let I=Math.ceil(a/e),S=[];for(let D=0;D<I;D++){let _=D*e,A=Math.min(_+e-1,a-1);S.push({index:D,start:_,end:A,downloaded:!1})}l={url:o,totalSize:a,downloadedSize:0,chunks:S,startedAt:Date.now()},await R.deleteModel(o)}let d=l.chunks.filter(I=>!I.downloaded),u=l.downloadedSize,p=Date.now(),m=u,y=()=>{if(!i)return;let I=Date.now(),S=(I-p)/1e3,D=u-m,_=S>0?D/S:0,A=a-u,me=_>0?A/_*1e3:0;i({loaded:u,total:a,percent:u/a*100,speed:_,eta:me,currentChunk:l.chunks.filter($=>$.downloaded).length,totalChunks:l.chunks.length}),p=I,m=u},g=[...d],T=new Map;for(;g.length>0||T.size>0;){for(;g.length>0&&T.size<n;){let I=g.shift(),S=(async()=>{try{let D=await Xs(o,I.start,I.end,s);await R.saveChunk(o,I.index,D),I.downloaded=!0,u+=D.byteLength,l.downloadedSize=u,await R.saveDownloadState(l),y()}finally{T.delete(I.index)}})();T.set(I.index,S)}T.size>0&&await Promise.race(T.values())}let k=await R.getChunks(o),b=new Uint8Array(a),z=0;for(let I of k)b.set(new Uint8Array(I),z),z+=I.byteLength;return await R.saveMeta({url:o,size:a,etag:c,cachedAt:Date.now(),chunks:k.length,complete:!0}),await R.deleteDownloadState(o),b.buffer}async function Mn(o,t,e){let n=new AbortController,s=setTimeout(()=>n.abort(),t);try{let i=await fetch(o,{signal:n.signal});if(!i.ok)throw new Error(`HTTP ${i.status}: ${i.statusText}`);let r=i.headers.get("Content-Length"),a=r?parseInt(r,10):0;if(!i.body||!e||a===0)return await i.arrayBuffer();let c=i.body.getReader(),l=[],d=0,u=Date.now();for(;;){let{done:m,value:y}=await c.read();if(m)break;l.push(y),d+=y.length;let g=(Date.now()-u)/1e3,T=g>0?d/g:0,k=a-d,b=T>0?k/T*1e3:0;e({loaded:d,total:a,percent:d/a*100,speed:T,eta:b})}let h=new Uint8Array(d),p=0;for(let m of l)h.set(m,p),p+=m.length;return h.buffer}finally{clearTimeout(s)}}async function v(o,t={}){let{cache:e=!0,forceDownload:n=!1,resumable:s=!0}=t;if(e&&!n){let r=await R.getModel(o);if(r){let a=new Uint8Array(r)[0];if(a===60||a===123||r.byteLength<1024)console.warn(`[edgeFlow.js] Cached model for ${o} appears corrupt (${r.byteLength} bytes, first byte 0x${a?.toString(16)}). Evicting and re-downloading.`),await R.deleteModel(o);else return console.log(`\u2713 Model loaded from cache: ${o}`),t.onProgress?.({loaded:r.byteLength,total:r.byteLength,percent:100,speed:0,eta:0}),r}}let i;return s?i=await Qs(o,t):i=await Mn(o,t.timeout??3e4,t.onProgress),e&&(s||(await R.saveChunk(o,0,i),await R.saveMeta({url:o,size:i.byteLength,cachedAt:Date.now(),chunks:1,complete:!0}))),i}function bt(o,t={}){return Se.preload(o,t)}function kt(o,t={}){return Promise.all(o.map(({url:e,priority:n})=>Se.preload(e,{...t,priority:n})))}async function ve(o){return(await R.getMeta(o))?.complete??!1}async function It(o){return R.getModel(o)}async function At(o){return R.deleteModel(o)}async function zt(){return R.clear()}async function St(){return R.getStats()}function vt(o){return Se.getStatus(o)}function Et(o){Se.cancel(o)}async function Dt(o){return Se.get(o)}var Vs,j,Q,J,Mt,R,Tt,Se,G=mt(()=>{"use strict";Vs="edgeflow-model-cache",j="meta",Q="chunks",J="download-state",Mt=class{constructor(){f(this,"db",null);f(this,"dbPromise",null)}async openDB(){return this.db?this.db:this.dbPromise?this.dbPromise:(this.dbPromise=new Promise((t,e)=>{let n=indexedDB.open(Vs,1);n.onupgradeneeded=s=>{let i=s.target.result;i.objectStoreNames.contains(j)||i.createObjectStore(j,{keyPath:"url"}),i.objectStoreNames.contains(Q)||i.createObjectStore(Q,{keyPath:["url","index"]}).createIndex("url","url",{unique:!1}),i.objectStoreNames.contains(J)||i.createObjectStore(J,{keyPath:"url"})},n.onsuccess=()=>{this.db=n.result,t(this.db)},n.onerror=()=>e(n.error)}),this.dbPromise)}async getMeta(t){let e=await this.openDB();return new Promise((n,s)=>{let a=e.transaction(j,"readonly").objectStore(j).get(t);a.onsuccess=()=>n(a.result??null),a.onerror=()=>s(a.error)})}async saveMeta(t){try{await this.putInStore(j,t)}catch(e){if(this.isQuotaError(e)){await this.evictOldest(t.size);try{await this.putInStore(j,t)}catch{console.warn("[edgeFlow.js] IndexedDB quota exceeded even after eviction; skipping cache.")}}else throw e}}async saveChunk(t,e,n){try{await this.putInStore(Q,{url:t,index:e,data:n})}catch(s){if(this.isQuotaError(s)){await this.evictOldest(n.byteLength);try{await this.putInStore(Q,{url:t,index:e,data:n})}catch{console.warn("[edgeFlow.js] IndexedDB quota exceeded even after eviction; skipping cache for chunk.")}}else throw s}}async putInStore(t,e){let n=await this.openDB();return new Promise((s,i)=>{let r=n.transaction(t,"readwrite");r.objectStore(t).put(e),r.oncomplete=()=>s(),r.onerror=()=>i(r.error)})}isQuotaError(t){return t instanceof DOMException?t.name==="QuotaExceededError"||t.code===22:!1}async evictOldest(t){let e=await this.openDB(),n=await new Promise((i,r)=>{let l=e.transaction(j,"readonly").objectStore(j).getAll();l.onsuccess=()=>i(l.result??[]),l.onerror=()=>r(l.error)});n.sort((i,r)=>i.cachedAt-r.cachedAt);let s=0;for(let i of n){if(s>=t)break;await this.deleteModel(i.url),s+=i.size}}async getChunks(t){let e=await this.openDB();return new Promise((n,s)=>{let c=e.transaction(Q,"readonly").objectStore(Q).index("url").getAll(t);c.onsuccess=()=>{let l=c.result;l.sort((d,u)=>d.index-u.index),n(l.map(d=>d.data))},c.onerror=()=>s(c.error)})}async getModel(t){let e=await this.getMeta(t);if(!e||!e.complete)return null;let n=await this.getChunks(t);if(n.length===0)return null;let s=n.reduce((a,c)=>a+c.byteLength,0),i=new Uint8Array(s),r=0;for(let a of n)i.set(new Uint8Array(a),r),r+=a.byteLength;return i.buffer}async saveDownloadState(t){try{await this.putInStore(J,t)}catch(e){if(this.isQuotaError(e))console.warn("[edgeFlow.js] IndexedDB quota exceeded saving download state; resume may not work.");else throw e}}async getDownloadState(t){let e=await this.openDB();return new Promise((n,s)=>{let a=e.transaction(J,"readonly").objectStore(J).get(t);a.onsuccess=()=>n(a.result??null),a.onerror=()=>s(a.error)})}async deleteDownloadState(t){let e=await this.openDB();return new Promise((n,s)=>{let i=e.transaction(J,"readwrite");i.objectStore(J).delete(t),i.oncomplete=()=>n(),i.onerror=()=>s(i.error)})}async deleteModel(t){let e=await this.openDB();await new Promise((s,i)=>{let r=e.transaction(j,"readwrite");r.objectStore(j).delete(t),r.oncomplete=()=>s(),r.onerror=()=>i(r.error)}),(await this.getChunks(t)).length>0&&await new Promise((s,i)=>{let r=e.transaction(Q,"readwrite"),l=r.objectStore(Q).index("url").openCursor(IDBKeyRange.only(t));l.onsuccess=d=>{let u=d.target.result;u&&(u.delete(),u.continue())},r.oncomplete=()=>s(),r.onerror=()=>i(r.error)}),await this.deleteDownloadState(t)}async clear(){let t=await this.openDB(),e=[j,Q,J];for(let n of e)await new Promise((s,i)=>{let r=t.transaction(n,"readwrite");r.objectStore(n).clear(),r.oncomplete=()=>s(),r.onerror=()=>i(r.error)})}async getStats(){let t=await this.openDB();return new Promise((e,n)=>{let r=t.transaction(j,"readonly").objectStore(j).getAll();r.onsuccess=()=>{let a=r.result;e({models:a.filter(c=>c.complete).length,totalSize:a.reduce((c,l)=>c+(l.complete?l.size:0),0)})},r.onerror=()=>n(r.error)})}},R=new Mt;Tt=class{constructor(){f(this,"tasks",new Map);f(this,"queue",[]);f(this,"maxConcurrent",2);f(this,"activeCount",0)}preload(t,e={}){let n=this.tasks.get(t);if(n)return n.promise;let s,i,r=new Promise((l,d)=>{s=l,i=d}),a={url:t,priority:e.priority??0,options:e,promise:r,resolve:s,reject:i,status:"pending"};this.tasks.set(t,a);let c=this.queue.findIndex(l=>{let d=this.tasks.get(l);return d&&d.priority<a.priority});return c===-1?this.queue.push(t):this.queue.splice(c,0,t),this.processQueue(),r}async processQueue(){for(;this.queue.length>0&&this.activeCount<this.maxConcurrent;){let t=this.queue.shift();if(!t)break;let e=this.tasks.get(t);!e||e.status!=="pending"||(this.activeCount++,e.status="loading",this.downloadTask(e).finally(()=>{this.activeCount--,this.processQueue()}))}}async downloadTask(t){try{let e=await v(t.url,t.options);t.status="complete",t.resolve(e)}catch(e){t.status="error",t.reject(e instanceof Error?e:new Error(String(e)))}}isPreloaded(t){return this.tasks.get(t)?.status==="complete"}getStatus(t){return this.tasks.get(t)?.status??"not_found"}async get(t){let e=this.tasks.get(t);return e&&(e.status==="complete"||e.status==="loading")?e.promise:null}cancel(t){let e=this.tasks.get(t);e&&e.status==="pending"&&(this.tasks.delete(t),this.queue=this.queue.filter(n=>n!==t),e.reject(new Error("Preload cancelled")))}clear(){for(let[,t]of this.tasks)t.status==="pending"&&t.reject(new Error("Preload cleared"));this.tasks.clear(),this.queue=[]}},Se=new Tt});B();C();B();var wt=class{constructor(t,e,n,s){f(this,"id");f(this,"modelId");f(this,"priority");f(this,"createdAt");f(this,"_status","pending");f(this,"_startedAt");f(this,"_completedAt");f(this,"_result");f(this,"_error");f(this,"_executor");f(this,"_resolvers",[]);f(this,"_cancelled",!1);this.id=t,this.modelId=e,this.priority=n,this.createdAt=Date.now(),this._executor=s}get status(){return this._status}get startedAt(){return this._startedAt}get completedAt(){return this._completedAt}get result(){return this._result}get error(){return this._error}cancel(){if(this._status==="pending"){this._cancelled=!0,this._status="cancelled",this._completedAt=Date.now();let t=new x("Task was cancelled",M.INFERENCE_CANCELLED,{taskId:this.id});for(let{reject:e}of this._resolvers)e(t);this._resolvers=[]}}wait(){return this._status==="completed"?Promise.resolve(this._result):this._status==="failed"?Promise.reject(this._error):this._status==="cancelled"?Promise.reject(new x("Task was cancelled",M.INFERENCE_CANCELLED,{taskId:this.id})):new Promise((t,e)=>{this._resolvers.push({resolve:t,reject:e})})}async execute(){if(!this._cancelled){this._status="running",this._startedAt=Date.now();try{this._result=await this._executor(),this._status="completed",this._completedAt=Date.now();for(let{resolve:t}of this._resolvers)t(this._result)}catch(t){this._error=t instanceof Error?t:new Error(String(t)),this._status="failed",this._completedAt=Date.now();for(let{reject:e}of this._resolvers)e(this._error)}this._resolvers=[]}}},wn={critical:0,high:1,normal:2,low:3},xt=class{constructor(){f(this,"items",[])}get length(){return this.items.length}isEmpty(){return this.items.length===0}enqueue(t){let e=!1;for(let n=0;n<this.items.length;n++){let s=this.items[n];if(s&&wn[t.priority]<wn[s.priority]){this.items.splice(n,0,t),e=!0;break}}e||this.items.push(t)}dequeue(){return this.items.shift()}peek(){return this.items[0]}remove(t){let e=this.items.findIndex(n=>n.id===t);if(e!==-1){let[n]=this.items.splice(e,1);return n}}getAll(){return[...this.items]}clear(){this.items=[]}};var Cs=0;function Os(){return`task_${++Cs}_${Date.now().toString(36)}`}var Rs={maxConcurrentTasks:4,maxConcurrentPerModel:1,defaultTimeout:3e4,enableBatching:!1,maxBatchSize:32,batchTimeout:50,maxRetries:0,retryBaseDelay:1e3,circuitBreaker:!1,circuitBreakerThreshold:5,circuitBreakerResetTimeout:3e4},ke=class{constructor(t={}){f(this,"options");f(this,"queues",new Map);f(this,"runningTasks",new Map);f(this,"allTasks",new Map);f(this,"batchers",new Map);f(this,"listeners",new Map);f(this,"circuits",new Map);f(this,"globalRunningCount",0);f(this,"isProcessing",!1);f(this,"disposed",!1);this.options={...Rs,...t}}getCircuit(t){let e=this.circuits.get(t);return e||(e={failures:0,state:"closed",lastFailure:0},this.circuits.set(t,e)),e}isCircuitOpen(t){if(!this.options.circuitBreaker)return!1;let e=this.getCircuit(t);return e.state==="closed"?!1:e.state==="open"?Date.now()-e.lastFailure>this.options.circuitBreakerResetTimeout?(e.state="half-open",!1):!0:!1}circuitSuccess(t){if(!this.options.circuitBreaker)return;let e=this.getCircuit(t);e.failures=0,e.state="closed"}circuitFailure(t){if(!this.options.circuitBreaker)return;let e=this.getCircuit(t);e.failures++,e.lastFailure=Date.now(),e.failures>=this.options.circuitBreakerThreshold&&(e.state="open",this.emit("inference:error",{modelId:t,error:new Error(`Circuit breaker opened after ${e.failures} consecutive failures`)}))}getQueue(t){let e=this.queues.get(t);return e||(e=new xt,this.queues.set(t,e)),e}getRunningSet(t){let e=this.runningTasks.get(t);return e||(e=new Set,this.runningTasks.set(t,e)),e}canStartTask(t){if(this.globalRunningCount>=this.options.maxConcurrentTasks)return!1;let e=this.runningTasks.get(t);return!(e&&e.size>=this.options.maxConcurrentPerModel)}async processQueue(){if(this.isProcessing||this.disposed)return;this.isProcessing=!0;try{let e=[];for(let[n,s]of this.queues)for(;!s.isEmpty()&&this.canStartTask(n);){let i=s.dequeue();i&&i.status==="pending"&&(e.push(i),this.getRunningSet(n).add(i.id),this.globalRunningCount++)}await Promise.all(e.map(async n=>{this.emit("inference:start",{taskId:n.id,modelId:n.modelId});try{await n.execute(),this.emit("inference:complete",{taskId:n.id,modelId:n.modelId,duration:(n.completedAt??0)-(n.startedAt??0)})}catch(s){this.emit("inference:error",{taskId:n.id,modelId:n.modelId,error:s})}finally{let s=this.runningTasks.get(n.modelId);s&&s.delete(n.id),this.globalRunningCount--}}))}finally{this.isProcessing=!1}let t=!1;for(let e of this.queues.values())if(!e.isEmpty()){t=!0;break}t&&setTimeout(()=>this.processQueue(),0)}schedule(t,e,n="normal"){if(this.disposed)throw new x("Scheduler has been disposed",M.RUNTIME_NOT_INITIALIZED);if(this.isCircuitOpen(t))throw new x(`Circuit breaker is open for model ${t} \u2014 too many consecutive failures. Retry after ${this.options.circuitBreakerResetTimeout}ms.`,M.INFERENCE_FAILED,{modelId:t});let s=this.options.maxRetries,i=this.options.retryBaseDelay,r=s>0?async()=>{let l;for(let d=0;d<=s;d++)try{let u=await e();return this.circuitSuccess(t),u}catch(u){if(l=u instanceof Error?u:new Error(String(u)),this.circuitFailure(t),d<s){let h=i*Math.pow(2,d);await new Promise(p=>setTimeout(p,h))}}throw l}:async()=>{try{let l=await e();return this.circuitSuccess(t),l}catch(l){throw this.circuitFailure(t),l}},a=new wt(Os(),t,n,r);return this.allTasks.set(a.id,a),this.getQueue(t).enqueue(a),this.processQueue(),a}scheduleWithTimeout(t,e,n=this.options.defaultTimeout,s="normal"){let i=()=>new Promise((r,a)=>{let c=setTimeout(()=>{a(new x(`Task timed out after ${n}ms`,M.INFERENCE_TIMEOUT,{timeout:n}))},n);e().then(l=>{clearTimeout(c),r(l)}).catch(l=>{clearTimeout(c),a(l)})});return this.schedule(t,i,s)}async scheduleAll(t){let e=t.map(({modelId:n,executor:s,priority:i})=>this.schedule(n,s,i));return Promise.all(e.map(n=>n.wait()))}getTask(t){return this.allTasks.get(t)}cancelTask(t){let e=this.allTasks.get(t);if(e&&e.status==="pending"){e.cancel();for(let n of this.queues.values())n.remove(t);return!0}return!1}cancelAllForModel(t){let e=this.queues.get(t);if(!e)return 0;let n=0;for(let s of e.getAll())s.status==="pending"&&(s.cancel(),n++);return e.clear(),n}getStats(){let t={totalTasks:this.allTasks.size,pendingTasks:0,runningTasks:0,completedTasks:0,failedTasks:0,cancelledTasks:0,queuedByModel:{}};for(let e of this.allTasks.values())switch(e.status){case"pending":t.pendingTasks++;break;case"running":t.runningTasks++;break;case"completed":t.completedTasks++;break;case"failed":t.failedTasks++;break;case"cancelled":t.cancelledTasks++;break}for(let[e,n]of this.queues)t.queuedByModel[e]=n.length;return t}on(t,e){let n=this.listeners.get(t);n||(n=new Set,this.listeners.set(t,n)),n.add(e)}off(t,e){let n=this.listeners.get(t);n&&n.delete(e)}emit(t,e){let n={type:t,timestamp:Date.now(),data:e},s=this.listeners.get(t);if(s)for(let i of s)try{i(n)}catch(r){console.error("Error in event listener:",r)}}clearHistory(){for(let[t,e]of this.allTasks)(e.status==="completed"||e.status==="failed"||e.status==="cancelled")&&this.allTasks.delete(t)}dispose(){this.disposed=!0;for(let t of this.queues.values()){for(let e of t.getAll())e.cancel();t.clear()}for(let t of this.batchers.values())t.clear();this.queues.clear(),this.runningTasks.clear(),this.allTasks.clear(),this.batchers.clear(),this.listeners.clear()}},ye=null;function Ie(){return ye||(ye=new ke),ye}function xn(o){ye&&ye.dispose(),ye=o}function $s(o){xn(new ke(o))}var Us={initialSize:67108864,maxSize:536870912,growthFactor:1.5,autoGC:!0,gcThreshold:.8},V=class V{constructor(t={}){f(this,"config");f(this,"resources",new Map);f(this,"disposers",new Map);f(this,"listeners",new Map);f(this,"allocated",0);f(this,"peak",0);f(this,"gcScheduled",!1);f(this,"disposed",!1);this.config={...Us,...t}}static getInstance(){return V.instance||(V.instance=new V),V.instance}static configure(t){V.instance&&console.warn("MemoryManager already initialized, configuration may not apply"),V.instance=new V(t)}track(t,e){if(this.disposed)return;let n=this.estimateTensorSize(t);this.resources.set(t.id,{id:t.id,type:"tensor",size:n,createdAt:Date.now(),stackTrace:this.captureStackTrace()}),e&&this.disposers.set(t.id,e),this.allocated+=n,this.peak=Math.max(this.peak,this.allocated),this.checkMemoryThreshold()}trackModel(t,e){if(this.disposed)return;let n=t.metadata.sizeBytes;this.resources.set(t.id,{id:t.id,type:"model",size:n,createdAt:Date.now(),stackTrace:this.captureStackTrace()}),e&&this.disposers.set(t.id,e),this.allocated+=n,this.peak=Math.max(this.peak,this.allocated),this.checkMemoryThreshold()}untrack(t){let e=this.resources.get(t);e&&(this.allocated-=e.size,this.resources.delete(t),this.disposers.delete(t))}release(t){let e=typeof t=="string"?t:t.id,n=this.disposers.get(e);if(n)try{n()}catch(s){console.error("Error disposing resource:",s)}this.untrack(e)}estimateTensorSize(t){let e=this.getBytesPerElement(t.dtype);return t.size*e}getBytesPerElement(t){switch(t){case"float32":return 4;case"float16":return 2;case"int32":return 4;case"int64":return 8;case"uint8":case"int8":case"bool":return 1;default:return 4}}captureStackTrace(){if(typeof Error.captureStackTrace=="function"){let t={};return Error.captureStackTrace(t,this.captureStackTrace),t.stack}return new Error().stack}checkMemoryThreshold(){if(!this.config.autoGC)return;let t=this.allocated/this.config.maxSize;t>=this.config.gcThreshold&&!this.gcScheduled&&(this.gcScheduled=!0,this.emit("memory:warning",{allocated:this.allocated,maxSize:this.config.maxSize,usage:t}),setTimeout(()=>{this.gc(),this.gcScheduled=!1},0))}gc(t=!1,e=5*60*1e3){this.emit("memory:gc",{before:this.allocated});let n=Date.now(),s=[];for(let[i,r]of this.resources)n-r.createdAt>e&&s.push(i);if(t)for(let i of s)this.release(i);this.emit("memory:gc",{after:this.allocated,evicted:t?s.length:0,potentialCleanup:s.length})}async measureBrowserMemory(){try{if(typeof performance<"u"&&"measureUserAgentSpecificMemory"in performance)return await performance.measureUserAgentSpecificMemory()}catch{}return null}getDeviceMemory(){try{if(typeof navigator<"u"&&"deviceMemory"in navigator)return navigator.deviceMemory??null}catch{}return null}getStats(){let t=0,e=0;for(let n of this.resources.values())n.type==="tensor"?t++:e++;return{allocated:this.allocated,used:this.allocated,peak:this.peak,tensorCount:t,modelCount:e}}getResourceDetails(){return Array.from(this.resources.values())}detectLeaks(t=10*60*1e3){let e=Date.now(),n=[];for(let s of this.resources.values())e-s.createdAt>t&&n.push(s);return n}on(t,e){let n=this.listeners.get(t);n||(n=new Set,this.listeners.set(t,n)),n.add(e)}off(t,e){let n=this.listeners.get(t);n&&n.delete(e)}emit(t,e){let n={type:t,timestamp:Date.now(),data:e},s=this.listeners.get(t);if(s)for(let i of s)try{i(n)}catch(r){console.error("Error in event listener:",r)}}resetStats(){this.peak=this.allocated}disposeAll(){for(let t of this.resources.keys())this.release(t)}dispose(){this.disposeAll(),this.disposed=!0,this.listeners.clear(),V.instance=null}};f(V,"instance",null);var ce=V,Ae=class o{constructor(t){f(this,"resources",[]);f(this,"children",[]);f(this,"parent",null);t&&(this.parent=t,t.children.push(this))}track(t){return this.resources.push(t),t}createChild(){return new o(this)}keep(t){let e=this.resources.indexOf(t);return e!==-1&&this.resources.splice(e,1),t}dispose(){for(let t of this.children)t.dispose();this.children=[];for(let t=this.resources.length-1;t>=0;t--)try{this.resources[t]?.dispose()}catch(e){console.error("Error disposing resource in scope:",e)}if(this.resources=[],this.parent){let t=this.parent.children.indexOf(this);t!==-1&&this.parent.children.splice(t,1),this.parent=null}}};async function Bs(o){let t=new Ae;try{return await o(t)}finally{t.dispose()}}function qs(o){let t=new Ae;try{return o(t)}finally{t.dispose()}}var ze=class{constructor(t={}){f(this,"maxSize");f(this,"maxModels");f(this,"cache",new Map);f(this,"currentSize",0);this.maxSize=t.maxSize??256*1024*1024,this.maxModels=t.maxModels??5}get(t){let e=this.cache.get(t);if(e)return e.lastAccess=Date.now(),e.model}set(t,e){let n=e.metadata.sizeBytes;for(;(this.currentSize+n>this.maxSize||this.cache.size>=this.maxModels)&&this.cache.size>0;)this.evictLRU();this.cache.set(t,{model:e,size:n,lastAccess:Date.now()}),this.currentSize+=n}delete(t){let e=this.cache.get(t);return e?(e.model.dispose(),this.currentSize-=e.size,this.cache.delete(t),!0):!1}has(t){return this.cache.has(t)}evictLRU(){let t=null,e=1/0;for(let[n,s]of this.cache)s.lastAccess<e&&(e=s.lastAccess,t=n);t&&this.delete(t)}clear(){for(let t of this.cache.values())t.model.dispose();this.cache.clear(),this.currentSize=0}getStats(){return{size:this.currentSize,count:this.cache.size,maxSize:this.maxSize,maxModels:this.maxModels}}};function q(){return ce.getInstance()}function js(){return ce.getInstance().getStats()}function Hs(o){ce.getInstance().release(o)}function Ws(){ce.getInstance().gc()}B();var Ze=new Map,Z=new Map,Ft=["webgpu","webnn","wasm"],le=class le{constructor(){f(this,"listeners",new Map);f(this,"defaultRuntime","auto")}static getInstance(){return le.instance||(le.instance=new le),le.instance}register(t,e){Ze.set(t,e)}async getRuntime(t="auto"){if(t==="auto")return this.getBestRuntime();let e=Z.get(t);if(e)return e;let n=Ze.get(t);if(!n)throw new x(`Runtime '${t}' is not registered`,M.RUNTIME_NOT_AVAILABLE,{runtime:t});if(e=n(),!await e.isAvailable())throw new x(`Runtime '${t}' is not available in this environment`,M.RUNTIME_NOT_AVAILABLE,{runtime:t});try{await e.initialize()}catch(i){throw new x(`Failed to initialize runtime '${t}': ${i instanceof Error?i.message:String(i)}`,M.RUNTIME_INIT_FAILED,{runtime:t,error:i})}return Z.set(t,e),this.emit("runtime:ready",{runtime:t}),e}async getBestRuntime(){for(let t of Ft)try{let e=Z.get(t);if(e)return e;let n=Ze.get(t);if(!n)continue;let s=n();if(await s.isAvailable())return await s.initialize(),Z.set(t,s),this.emit("runtime:ready",{runtime:t}),s}catch{continue}throw new x("No runtime available. Please ensure WebGPU, WebNN, or WASM is supported.",M.RUNTIME_NOT_AVAILABLE,{triedRuntimes:Ft})}async detectAvailableRuntimes(){let t=new Map;for(let e of Ft){let n=Ze.get(e);if(!n){t.set(e,!1);continue}try{let s=n();t.set(e,await s.isAvailable())}catch{t.set(e,!1)}}return t}async getCapabilities(t){return(await this.getRuntime(t)).capabilities}setDefaultRuntime(t){this.defaultRuntime=t}getDefaultRuntimeType(){return this.defaultRuntime}disposeRuntime(t){let e=Z.get(t);e&&(e.dispose(),Z.delete(t))}disposeAll(){for(let[t,e]of Z)e.dispose(),Z.delete(t)}on(t,e){let n=this.listeners.get(t);n||(n=new Set,this.listeners.set(t,n)),n.add(e)}off(t,e){let n=this.listeners.get(t);n&&n.delete(e)}emit(t,e){let n={type:t,timestamp:Date.now(),data:e},s=this.listeners.get(t);if(s)for(let i of s)try{i(n)}catch(r){console.error("Error in event listener:",r)}}};f(le,"instance",null);var W=le,Ks=0;function Ys(){return`model_${++Ks}_${Date.now().toString(36)}`}var H=class{constructor(t,e,n){f(this,"id");f(this,"metadata");f(this,"runtime");f(this,"_isLoaded",!0);f(this,"_dispose");this.id=Ys(),this.metadata=t,this.runtime=e,this._dispose=n}get isLoaded(){return this._isLoaded}dispose(){this._isLoaded&&(this._isLoaded=!1,this._dispose(),q().untrack(this.id))}};async function we(o,t={}){let n=await W.getInstance().getRuntime(t.runtime??"auto"),{loadModelData:s}=await Promise.resolve().then(()=>(G(),Tn)),i=await s(o,{cache:t.cache??!0,resumable:t.resumable??!0,chunkSize:t.chunkSize,forceDownload:t.forceDownload,onProgress:t.onProgress?a=>{t.onProgress(a.percent/100)}:void 0});return await n.loadModel(i,t)}async function F(o,t={}){return(await W.getInstance().getRuntime(t.runtime??"auto")).loadModel(o,t)}async function X(o,t){if(!o.isLoaded)throw new x("Model has been disposed",M.MODEL_NOT_LOADED,{modelId:o.id});let n=await W.getInstance().getRuntime(o.runtime);return Ie().schedule(o.id,()=>n.run(o,t)).wait()}async function U(o,t){if(!o.isLoaded)throw new x("Model has been disposed",M.MODEL_NOT_LOADED,{modelId:o.id});let n=await W.getInstance().getRuntime(o.runtime);if(!("runNamed"in n))throw new x("Runtime does not support named inputs",M.INFERENCE_FAILED,{modelId:o.id});return Ie().schedule(o.id,()=>n.runNamed(o,t)).wait()}async function Js(o,t){let e=Ie(),s=await W.getInstance().getRuntime(o.runtime),i=t.map(r=>e.schedule(o.id,()=>s.run(o,r)));return Promise.all(i.map(r=>r.wait()))}function Zs(){return W.getInstance()}function de(o,t){W.getInstance().register(o,t)}async function er(){return W.getInstance().getBestRuntime()}async function Ee(){return W.getInstance().detectAvailableRuntimes()}var De=new Map,Pt=new Map,et=[];async function tr(o){if(De.has(o.name)){console.warn(`[edgeFlow.js] Plugin "${o.name}" is already registered \u2014 skipping.`);return}if(o.setup&&await o.setup(),o.pipelines)for(let[t,e]of Object.entries(o.pipelines))Pt.set(t,e);if(o.backends)for(let[t,e]of Object.entries(o.backends))de(t,e.factory);o.middleware&&et.push(...o.middleware),De.set(o.name,o)}function _t(o){return Pt.get(o)}function nr(){return et}function sr(){return Array.from(De.values()).map(o=>({name:o.name,version:o.version}))}function rr(o){let t=De.get(o);if(!t)return!1;if(t.pipelines)for(let e of Object.keys(t.pipelines))Pt.delete(e);if(t.middleware)for(let e of t.middleware){let n=et.indexOf(e);n!==-1&&et.splice(n,1)}return De.delete(o),!0}var Fe=null;async function bn(){if(Fe)return Fe;let o=typeof navigator<"u"?navigator.hardwareConcurrency??2:2,t=typeof navigator<"u"&&"deviceMemory"in navigator?navigator.deviceMemory??null:null,e=typeof navigator<"u"?/Android|iPhone|iPad|iPod|Mobile/i.test(navigator.userAgent):!1,n=!1,s;if(typeof navigator<"u"&&"gpu"in navigator)try{let l=await navigator.gpu.requestAdapter();if(n=l!=null,l&&typeof l=="object")try{let d=l.info;d&&(s=`${d.vendor??""} ${d.architecture??""}`.trim()||void 0)}catch{}}catch{}let i=!1;if(typeof navigator<"u"&&"ml"in navigator)try{let l=navigator.ml;l&&(i=await l.createContext()!=null)}catch{}let r;return n&&o>=8&&(t===null||t>=8)?r="high":o>=4&&(t===null||t>=4)?r="medium":r="low",e&&r==="high"&&(r="medium"),Fe={tier:r,cores:o,memoryGiB:t,webgpu:n,webnn:i,recommendedBatchSize:r==="high"?32:r==="medium"?8:1,recommendedConcurrency:r==="high"?4:r==="medium"?2:1,mobile:e,gpuInfo:s},Fe}function kn(o){return o.tier==="high"&&o.webgpu?"float16":(o.tier==="medium","int8")}async function or(){let o=await bn();return{quantization:kn(o),executionProvider:o.webgpu?"webgpu":"wasm",batchSize:o.recommendedBatchSize,useWorker:o.cores>=4}}function ir(){Fe=null}B();C();var xe={STORAGE:128,COPY_SRC:4,COPY_DST:8,MAP_READ:1},In={COMPUTE:4},Pe=class{constructor(){f(this,"name","webgpu");f(this,"adapter",null);f(this,"device",null);f(this,"models",new Map);f(this,"initialized",!1)}get capabilities(){return{concurrency:!0,quantization:!0,float16:!0,dynamicShapes:!1,maxBatchSize:64,availableMemory:this.device?.limits.maxBufferSize??256*1024*1024}}async isAvailable(){if(typeof navigator>"u"||!navigator.gpu)return!1;try{return await navigator.gpu.requestAdapter()!==null}catch{return!1}}async initialize(){if(!this.initialized){if(!navigator.gpu)throw new x("WebGPU is not supported in this browser",M.RUNTIME_NOT_AVAILABLE);if(this.adapter=await navigator.gpu.requestAdapter({powerPreference:"high-performance"}),!this.adapter)throw new x("Failed to get WebGPU adapter",M.RUNTIME_INIT_FAILED);this.device=await this.adapter.requestDevice({requiredFeatures:[],requiredLimits:{}}),this.device.lost.then(t=>{console.error("WebGPU device was lost:",t.message),this.initialized=!1,this.device=null}),this.initialized=!0}}async loadModel(t,e={}){this.ensureInitialized();let n=this.parseModelData(t),s={shaders:new Map,pipelines:new Map,weights:new Map,bindGroupLayouts:[],config:n};await this.uploadWeights(t,s),await this.createPipelines(s);let i=`webgpu_${Date.now().toString(36)}`;this.models.set(i,s);let r={name:n.name||e.metadata?.name||"unknown",version:n.version,inputs:n.inputs.map(c=>({name:c.name,dtype:c.dtype,shape:c.shape})),outputs:n.outputs.map(c=>({name:c.name,dtype:c.dtype,shape:c.shape})),sizeBytes:t.byteLength,quantization:e.quantization??"float32",format:"edgeflow"},a=new H(r,"webgpu",()=>this.unloadModel(i));return q().trackModel(a,()=>a.dispose()),a}async run(t,e){return this.ensureInitialized(),this.executeModel(e,t.metadata)}async executeModel(t,e){let n=this.device,s=[];for(let i of e.outputs){let r=i.shape.reduce((d,u)=>d*u,1),a=n.createBuffer({size:r*4,usage:xe.STORAGE|xe.COPY_SRC}),c=n.createBuffer({size:r*4,usage:xe.MAP_READ|xe.COPY_DST}),l=new Float32Array(r);if(t.length>0&&t[0]){let d=t[0].toFloat32Array();for(let u=0;u<Math.min(r,d.length);u++)l[u]=d[u]??0}s.push(new w(l,i.shape,"float32")),a.destroy(),c.destroy()}return s}parseModelData(t){try{let e=new TextDecoder,n=e.decode(new Uint8Array(t,0,Math.min(1024,t.byteLength)));if(n.trim().startsWith("{")){let s=n.indexOf(`
|
|
2
2
|
---
|
|
3
3
|
`);s===-1&&(s=t.byteLength);let i=e.decode(new Uint8Array(t,0,s));return JSON.parse(i)}}catch{}return{name:"unknown",version:"1.0.0",layers:[],inputs:[{name:"input",shape:[-1,768],dtype:"float32"}],outputs:[{name:"output",shape:[-1,768],dtype:"float32"}]}}async uploadWeights(t,e){let s=this.device.createBuffer({size:1024,usage:xe.STORAGE|xe.COPY_DST});e.weights.set("default",s)}async createPipelines(t){let e=this.device,s=e.createShaderModule({code:`
|
|
4
4
|
@group(0) @binding(0) var<storage, read> input: array<f32>;
|
|
@@ -11,11 +11,11 @@ var Kt=Object.defineProperty;var zs=(o,t,e)=>t in o?Kt(o,t,{enumerable:!0,config
|
|
|
11
11
|
output[idx] = input[idx];
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
|
-
`});t.shaders.set("default",s);let i=e.createBindGroupLayout({entries:[{binding:0,visibility:
|
|
14
|
+
`});t.shaders.set("default",s);let i=e.createBindGroupLayout({entries:[{binding:0,visibility:In.COMPUTE,buffer:{type:"read-only-storage"}},{binding:1,visibility:In.COMPUTE,buffer:{type:"storage"}}]});t.bindGroupLayouts.push(i);let r=e.createPipelineLayout({bindGroupLayouts:[i]}),a=e.createComputePipeline({layout:r,compute:{module:s,entryPoint:"main"}});t.pipelines.set("default",a)}unloadModel(t){let e=this.models.get(t);if(e){for(let n of e.weights.values())n.destroy();this.models.delete(t)}}ensureInitialized(){if(!this.initialized||!this.device)throw new x("WebGPU runtime is not initialized",M.RUNTIME_NOT_INITIALIZED)}dispose(){for(let t of this.models.keys())this.unloadModel(t);this.device&&(this.device.destroy(),this.device=null),this.adapter=null,this.initialized=!1}};function An(){return new Pe}B();C();var _e=class{constructor(){f(this,"name","webnn");f(this,"context",null);f(this,"models",new Map);f(this,"initialized",!1);f(this,"deviceType","default")}get capabilities(){return{concurrency:!0,quantization:!0,float16:!0,dynamicShapes:!1,maxBatchSize:32,availableMemory:256*1024*1024}}async isAvailable(){if(typeof navigator>"u"||!navigator.ml)return!1;try{return await navigator.ml.createContext({deviceType:"default"})!==null}catch{return!1}}async initialize(){if(!this.initialized){if(!navigator.ml)throw new x("WebNN is not supported in this browser",M.RUNTIME_NOT_AVAILABLE);try{this.context=await navigator.ml.createContext({deviceType:"gpu",powerPreference:"high-performance"}),this.deviceType="gpu"}catch{try{this.context=await navigator.ml.createContext({deviceType:"cpu"}),this.deviceType="cpu"}catch(t){throw new x(`Failed to create WebNN context: ${t instanceof Error?t.message:String(t)}`,M.RUNTIME_INIT_FAILED)}}this.initialized=!0}}async loadModel(t,e={}){this.ensureInitialized();let n=this.parseModelConfig(t),s=`webnn_${Date.now().toString(36)}`,i={name:n.name||e.metadata?.name||"unknown",version:n.version||"1.0.0",inputs:n.inputs.map(a=>({name:a.name,dtype:a.dtype,shape:a.shape})),outputs:n.outputs.map(a=>({name:a.name,dtype:a.dtype,shape:a.shape})),sizeBytes:t.byteLength,quantization:e.quantization??"float32",format:"edgeflow"},r=new H(i,"webnn",()=>this.unloadModel(s));return q().trackModel(r,()=>r.dispose()),r}async run(t,e){return this.ensureInitialized(),this.executeModel(e,t.metadata)}async executeModel(t,e){let n=[];for(let s of e.outputs){let i=s.shape.reduce((a,c)=>a*c,1),r=new Float32Array(i);if(t.length>0&&t[0]){let a=t[0].toFloat32Array();for(let c=0;c<Math.min(i,a.length);c++)r[c]=a[c]??0}n.push(new w(r,s.shape,"float32"))}return n}parseModelConfig(t){try{let e=new TextDecoder,n=e.decode(new Uint8Array(t,0,Math.min(1024,t.byteLength)));if(n.trim().startsWith("{")){let s=n.indexOf(`
|
|
15
15
|
---
|
|
16
|
-
`);s===-1&&(s=t.byteLength);let i=e.decode(new Uint8Array(t,0,s));return JSON.parse(i)}}catch{}return{name:"unknown",version:"1.0.0",inputs:[{name:"input",shape:[-1,768],dtype:"float32"}],outputs:[{name:"output",shape:[-1,768],dtype:"float32"}]}}unloadModel(t){this.models.delete(t)}ensureInitialized(){if(!this.initialized||!this.context)throw new x("WebNN runtime is not initialized",M.RUNTIME_NOT_INITIALIZED)}getDeviceType(){return this.deviceType}dispose(){this.models.clear(),this.context=null,this.initialized=!1}};function
|
|
16
|
+
`);s===-1&&(s=t.byteLength);let i=e.decode(new Uint8Array(t,0,s));return JSON.parse(i)}}catch{}return{name:"unknown",version:"1.0.0",inputs:[{name:"input",shape:[-1,768],dtype:"float32"}],outputs:[{name:"output",shape:[-1,768],dtype:"float32"}]}}unloadModel(t){this.models.delete(t)}ensureInitialized(){if(!this.initialized||!this.context)throw new x("WebNN runtime is not initialized",M.RUNTIME_NOT_INITIALIZED)}getDeviceType(){return this.deviceType}dispose(){this.models.clear(),this.context=null,this.initialized=!1}};function zn(){return new _e}B();C();var Le=class{constructor(){f(this,"name","wasm");f(this,"module",null);f(this,"simdSupported",!1);f(this,"models",new Map);f(this,"initialized",!1)}get capabilities(){return{concurrency:!1,quantization:!0,float16:!1,dynamicShapes:!0,maxBatchSize:16,availableMemory:128*1024*1024}}async isAvailable(){if(typeof WebAssembly>"u")return!1;try{let t=new Uint8Array([0,97,115,109,1,0,0,0]);return await WebAssembly.instantiate(t),!0}catch{return!1}}async initialize(){if(this.initialized)return;this.simdSupported=await this.checkSIMDSupport();let t=new WebAssembly.Memory({initial:256,maximum:2048});this.module={memory:t,exports:this.createJSFallback(t)},this.initialized=!0}async checkSIMDSupport(){try{let t=new Uint8Array([0,97,115,109,1,0,0,0,1,5,1,96,0,1,123,3,2,1,0,10,10,1,8,0,253,12,0,0,0,0,11]);return await WebAssembly.instantiate(t),!0}catch{return!1}}createJSFallback(t){let e=0,n=new Map;return{malloc:s=>{let i=e;return e+=s,n.set(i,s),i},free:s=>{n.delete(s)},matmul_f32:(s,i,r,a,c,l,d)=>{let u=new Float32Array(t.buffer),h=s/4,p=a/4,m=d/4;for(let y=0;y<i;y++)for(let g=0;g<l;g++){let T=0;for(let k=0;k<r;k++)T+=(u[h+y*r+k]??0)*(u[p+k*l+g]??0);u[m+y*l+g]=T}},add_f32:(s,i,r,a)=>{let c=new Float32Array(t.buffer),l=s/4,d=i/4,u=r/4;for(let h=0;h<a;h++)c[u+h]=(c[l+h]??0)+(c[d+h]??0)},mul_f32:(s,i,r,a)=>{let c=new Float32Array(t.buffer),l=s/4,d=i/4,u=r/4;for(let h=0;h<a;h++)c[u+h]=(c[l+h]??0)*(c[d+h]??0)},relu_f32:(s,i,r)=>{let a=new Float32Array(t.buffer),c=s/4,l=i/4;for(let d=0;d<r;d++)a[l+d]=Math.max(0,a[c+d]??0)},sigmoid_f32:(s,i,r)=>{let a=new Float32Array(t.buffer),c=s/4,l=i/4;for(let d=0;d<r;d++)a[l+d]=1/(1+Math.exp(-(a[c+d]??0)))},softmax_f32:(s,i,r)=>{let a=new Float32Array(t.buffer),c=s/4,l=i/4,d=-1/0;for(let h=0;h<r;h++)(a[c+h]??0)>d&&(d=a[c+h]??0);let u=0;for(let h=0;h<r;h++)a[l+h]=Math.exp((a[c+h]??0)-d),u+=a[l+h]??0;for(let h=0;h<r;h++)a[l+h]=(a[l+h]??0)/u}}}async loadModel(t,e={}){this.ensureInitialized();let n=this.parseModelConfig(t),s={weights:new Map,config:n,executionOrder:n.layers.map(c=>c.name)};await this.loadWeights(t,s);let i=`wasm_${Date.now().toString(36)}`;this.models.set(i,s);let r={name:n.name||e.metadata?.name||"unknown",version:n.version||"1.0.0",inputs:n.inputs.map(c=>({name:c.name,dtype:c.dtype,shape:c.shape})),outputs:n.outputs.map(c=>({name:c.name,dtype:c.dtype,shape:c.shape})),sizeBytes:t.byteLength,quantization:e.quantization??"float32",format:"edgeflow"},a=new H(r,"wasm",()=>this.unloadModel(i));return q().trackModel(a,()=>a.dispose()),a}async run(t,e){return this.ensureInitialized(),this.executeModel(e,t.metadata)}async executeModel(t,e){let n=[];for(let s of e.outputs){let i=s.shape.reduce((a,c)=>a*c,1),r;if(t.length>0&&t[0]){let a=t[0];if(s.name.includes("logits")||s.name.includes("class"))r=O(a);else if(s.name.includes("relu"))r=Ye(a);else if(s.name.includes("sigmoid"))r=Je(a);else{let c=new Float32Array(i),l=a.toFloat32Array();for(let d=0;d<Math.min(i,l.length);d++)c[d]=l[d]??0;r=new w(c,s.shape,"float32")}}else r=new w(new Float32Array(i),s.shape,"float32");n.push(r)}return n}parseModelConfig(t){try{let e=new TextDecoder,n=e.decode(new Uint8Array(t,0,Math.min(2048,t.byteLength)));if(n.trim().startsWith("{")){let s=n.indexOf(`
|
|
17
17
|
---
|
|
18
|
-
`);if(s===-1)try{return JSON.parse(n)}catch{s=t.byteLength}let i=e.decode(new Uint8Array(t,0,s));return JSON.parse(i)}}catch{}return{name:"unknown",version:"1.0.0",layers:[],inputs:[{name:"input",shape:[-1,768],dtype:"float32"}],outputs:[{name:"output",shape:[-1,768],dtype:"float32"}]}}async loadWeights(t,e){}unloadModel(t){let e=this.models.get(t);if(e&&this.module)for(let n of e.weights.values())this.module.exports.free(n.ptr);this.models.delete(t)}ensureInitialized(){if(!this.initialized||!this.module)throw new x("WASM runtime is not initialized",M.RUNTIME_NOT_INITIALIZED)}hasSIMDSupport(){return this.simdSupported}dispose(){for(let t of this.models.keys())this.unloadModel(t);this.module=null,this.initialized=!1}};function kn(){return new Le}B();C();var Ze=null;async function Ne(){if(Ze)return Ze;try{return Ze=await import("onnxruntime-web/wasm"),Ze}catch{return null}}async function nr(){return await Ne()!=null}var Me=new Map,Ft=class{constructor(){f(this,"name","wasm");f(this,"initialized",!1);f(this,"executionProvider","wasm")}get capabilities(){return{concurrency:!0,quantization:!0,float16:this.executionProvider==="webgpu",dynamicShapes:!0,maxBatchSize:32,availableMemory:512*1024*1024}}async isAvailable(){return nr()}async initialize(){if(this.initialized)return;let t=await Ne();if(!t)throw new x("onnxruntime-web is not installed. Install it with: npm install onnxruntime-web",M.RUNTIME_NOT_AVAILABLE);typeof window<"u"&&t.env?.wasm&&(t.env.wasm.wasmPaths="/ort/",t.env.wasm.numThreads=1),this.initialized=!0}async loadModel(t,e={}){this.initialized||await this.initialize();try{let n=await Ne();if(!n)throw new Error("onnxruntime-web is not installed");let s={executionProviders:["wasm"],graphOptimizationLevel:"all"},i=new Uint8Array(t),r=await n.InferenceSession.create(i,s),a=r.inputNames,c=r.outputNames,l=`onnx_${Date.now().toString(36)}_${Math.random().toString(36).slice(2,8)}`;Me.set(l,{session:r,inputNames:[...a],outputNames:[...c]});let d={name:e.metadata?.name??"onnx-model",version:"1.0.0",inputs:a.map(h=>({name:h,dtype:"float32",shape:[-1]})),outputs:c.map(h=>({name:h,dtype:"float32",shape:[-1]})),sizeBytes:t.byteLength,quantization:e.quantization??"float32",format:"onnx"},u=new H(d,"wasm",()=>this.unloadModel(l));return Object.defineProperty(u,"id",{value:l,writable:!1}),q().trackModel(u,()=>u.dispose()),u}catch(n){throw new x(`Failed to load ONNX model: ${n instanceof Error?n.message:String(n)}`,M.MODEL_LOAD_FAILED,{error:n})}}async run(t,e){let n=Me.get(t.id);if(!n)throw new x(`ONNX session not found for model ${t.id}`,M.MODEL_NOT_LOADED,{modelId:t.id});let{session:s,inputNames:i,outputNames:r}=n;try{let a=await Ne(),c={};for(let u=0;u<Math.min(e.length,i.length);u++){let h=i[u],p=e[u];if(h&&p){let m=p.dtype,y;if(m==="int64"){let g=p.data;y=new a.Tensor("int64",g,p.shape)}else if(m==="int32"){let g=p.data;y=new a.Tensor("int32",g,p.shape)}else{let g=p.toFloat32Array();y=new a.Tensor("float32",g,p.shape)}c[h]=y}}let l=await s.run(c),d=[];for(let u of r){let h=l[u];if(h){let p=h.data,m=Array.from(h.dims).map(y=>Number(y));d.push(new w(new Float32Array(p),m,"float32"))}}return d}catch(a){throw new x(`ONNX inference failed: ${a instanceof Error?a.message:String(a)}`,M.INFERENCE_FAILED,{modelId:t.id,error:a})}}async runNamed(t,e){let n=Me.get(t.id);if(!n)throw new x(`ONNX session not found for model ${t.id}`,M.MODEL_NOT_LOADED,{modelId:t.id});let{session:s,inputNames:i,outputNames:r}=n;try{let a=await Ne(),c={};for(let[u,h]of e){let p=h,m=p.dtype,y;if(m==="int64"){let g=p.data;y=new a.Tensor("int64",g,p.shape)}else if(m==="int32"){let g=p.data;y=new a.Tensor("int32",g,p.shape)}else{let g=p.toFloat32Array();y=new a.Tensor("float32",g,p.shape)}c[u]=y}let l=await s.run(c),d=[];for(let u of r){let h=l[u];if(h){let p=h.data,m=Array.from(h.dims).map(y=>Number(y));d.push(new w(new Float32Array(p),m,"float32"))}}return d}catch(a){throw new x(`ONNX inference failed: ${a instanceof Error?a.message:String(a)}`,M.INFERENCE_FAILED,{modelId:t.id,expectedInputs:i,providedInputs:Array.from(e.keys()),error:a})}}async unloadModel(t){Me.get(t)&&Me.delete(t)}dispose(){Me.clear(),this.initialized=!1}};function In(){return new Ft}B();C();var ue=new Map,K=null,Ce=class{constructor(){f(this,"name","wasm")}get capabilities(){return{concurrency:!0,quantization:!0,float16:!0,dynamicShapes:!0,maxBatchSize:128,availableMemory:1024*1024*1024}}async isAvailable(){return K?.pipelineFactory!=null}async initialize(){if(!K?.pipelineFactory)throw new x("TransformersAdapterRuntime requires a pipelineFactory. Call useTransformersBackend({ pipelineFactory }) first.",M.RUNTIME_INIT_FAILED)}async loadModel(t,e={}){let s={name:e.metadata?.name??"default",version:"1.0.0",inputs:[{name:"input",dtype:"float32",shape:[-1]}],outputs:[{name:"output",dtype:"float32",shape:[-1]}],sizeBytes:t.byteLength||0,quantization:e.quantization??"float32",format:"onnx"},i=`tjs_${Date.now().toString(36)}_${Math.random().toString(36).slice(2,6)}`,r=new H(s,this.name,()=>{let a=ue.get(i);a?.instance.dispose&&a.instance.dispose(),ue.delete(i)});return q().trackModel(r,()=>r.dispose()),r}async loadPipeline(t,e,n){if(!K?.pipelineFactory)throw new x("Adapter not initialised",M.RUNTIME_NOT_INITIALIZED);let s={...n};K.device&&(s.device=K.device),K.dtype&&(s.dtype=K.dtype);let i=await K.pipelineFactory(t,e,s),r=`tjs_${Date.now().toString(36)}_${Math.random().toString(36).slice(2,6)}`;return ue.set(r,{instance:i,task:t,model:e}),r}async run(t,e){let n=ue.get(t.id);if(!n)throw new x(`No transformers.js session for model ${t.id}`,M.MODEL_NOT_LOADED);let s=e[0]?.toFloat32Array()??new Float32Array(0),i=await n.instance(s),r=Array.isArray(i)?new Float32Array(i.flat(1/0)):new Float32Array([0]);return[new w(r,[r.length],"float32")]}async runDirect(t,e,n){let s=ue.get(t);if(!s)throw new x(`No transformers.js session for model ${t}`,M.MODEL_NOT_LOADED);return s.instance(e,n)}dispose(){for(let[t,e]of ue)e.instance.dispose&&e.instance.dispose(),ue.delete(t)}},Pt=null;function An(o){K=o,Pt=new Ce,de("wasm",()=>Pt)}function zn(){return Pt}function et(){de("wasm",In)}et();var Te=class{constructor(t={}){f(this,"options");f(this,"cache",new Map);f(this,"currentSize",0);f(this,"hits",0);f(this,"misses",0);this.options={strategy:t.strategy??"lru",maxSize:t.maxSize??100*1024*1024,maxEntries:t.maxEntries??1e3,ttl:t.ttl??0,persistent:t.persistent??!1,name:t.name??"edgeflow-cache"},this.options.persistent&&this.loadFromStorage()}get(t){let e=this.cache.get(t);if(!e){this.misses++;return}if(e.ttl&&Date.now()-e.createdAt>e.ttl){this.delete(t),this.misses++;return}return e.accessedAt=Date.now(),e.accessCount++,this.hits++,e.value}set(t,e,n,s){for(this.cache.has(t)&&this.delete(t);(this.currentSize+n>this.options.maxSize||this.cache.size>=this.options.maxEntries)&&this.cache.size>0;)this.evict();let i=s!==void 0?s:this.options.ttl>0?this.options.ttl:void 0,r={value:e,size:n,createdAt:Date.now(),accessedAt:Date.now(),accessCount:1,ttl:i};this.cache.set(t,r),this.currentSize+=n,this.options.persistent&&this.saveToStorage()}has(t){let e=this.cache.get(t);return e?e.ttl&&Date.now()-e.createdAt>e.ttl?(this.delete(t),!1):!0:!1}delete(t){let e=this.cache.get(t);return e?(this.currentSize-=e.size,this.cache.delete(t),this.options.persistent&&this.saveToStorage(),!0):!1}clear(){this.cache.clear(),this.currentSize=0,this.hits=0,this.misses=0,this.options.persistent&&this.clearStorage()}getStats(){let t=this.hits+this.misses;return{entries:this.cache.size,size:this.currentSize,hits:this.hits,misses:this.misses,hitRate:t>0?this.hits/t:0}}evict(){let t=null;switch(this.options.strategy){case"lru":t=this.findLRU();break;case"lfu":t=this.findLFU();break;case"fifo":t=this.findOldest();break;case"ttl":t=this.findExpired()??this.findOldest();break}t&&this.delete(t)}findLRU(){let t=null,e=1/0;for(let[n,s]of this.cache)s.accessedAt<e&&(e=s.accessedAt,t=n);return t}findLFU(){let t=null,e=1/0;for(let[n,s]of this.cache)s.accessCount<e&&(e=s.accessCount,t=n);return t}findOldest(){let t=null,e=1/0;for(let[n,s]of this.cache)s.createdAt<e&&(e=s.createdAt,t=n);return t}findExpired(){let t=Date.now();for(let[e,n]of this.cache)if(n.ttl&&t-n.createdAt>n.ttl)return e;return null}async loadFromStorage(){if(!(typeof indexedDB>"u"))try{let s=(await this.openDB()).transaction("cache","readonly").objectStore("cache").getAll();return new Promise((i,r)=>{s.onsuccess=()=>{let a=s.result;for(let{key:c,entry:l}of a)this.cache.set(c,l),this.currentSize+=l.size;i()},s.onerror=()=>r(s.error)})}catch{}}async saveToStorage(){if(!(typeof indexedDB>"u"))try{let e=(await this.openDB()).transaction("cache","readwrite"),n=e.objectStore("cache");n.clear();for(let[s,i]of this.cache)n.put({key:s,entry:i});return new Promise((s,i)=>{e.oncomplete=()=>s(),e.onerror=()=>i(e.error)})}catch{}}async clearStorage(){if(!(typeof indexedDB>"u"))try{(await this.openDB()).transaction("cache","readwrite").objectStore("cache").clear()}catch{}}openDB(){return new Promise((t,e)=>{let n=indexedDB.open(this.options.name,1);n.onupgradeneeded=()=>{let s=n.result;s.objectStoreNames.contains("cache")||s.createObjectStore("cache",{keyPath:"key"})},n.onsuccess=()=>t(n.result),n.onerror=()=>e(n.error)})}},tt=class extends Te{generateKey(t,e){let n=Array.isArray(e)?e:Array.from(e),s=this.hashArray(n);return`${t}:${s}`}hashArray(t){let e=0,n=t.length>100?t.filter((s,i)=>i%Math.floor(t.length/100)===0):t;for(let s=0;s<n.length;s++){let i=n[s]??0;e=(e<<5)-e+(i*1e3|0),e|=0}return e.toString(36)}},ee=class{constructor(t="edgeflow-models"){f(this,"cacheName");f(this,"cache",null);this.cacheName=t}async ensureCache(){if(!this.cache){if(typeof caches>"u")throw new Error("Cache API is not available");this.cache=await caches.open(this.cacheName)}return this.cache}async get(t){try{return await(await this.ensureCache()).match(t)??void 0}catch{return}}async put(t,e){try{await(await this.ensureCache()).put(t,e.clone())}catch{}}async delete(t){try{return await(await this.ensureCache()).delete(t)}catch{return!1}}async clear(){try{await caches.delete(this.cacheName),this.cache=null}catch{}}async keys(){try{return(await(await this.ensureCache()).keys()).map(n=>n.url)}catch{return[]}}};function Sn(o="medium",t={}){let e={small:{maxSize:10485760,maxEntries:100},medium:{maxSize:104857600,maxEntries:500},large:{maxSize:524288e3,maxEntries:2e3},custom:{}};return new Te({...e[o],...t})}var E=class{constructor(t){f(this,"model",null);f(this,"config");f(this,"modelCache");f(this,"downloadCache");f(this,"isReady",!1);this.config=t,this.modelCache=new ze,this.downloadCache=new ee}async initialize(){if(this.isReady&&this.model)return;if(this.config.model==="default"){this.isReady=!0;return}let t=this.modelCache.get(this.config.model);if(t){this.model=t,this.isReady=!0;return}this.model=await this.loadModelWithCache(this.config.model),this.isReady=!0}async loadModelWithCache(t){let e=await this.downloadCache.get(t);try{let n=await fetch(t);n.ok&&await this.downloadCache.put(t,n.clone())}catch{}return we(t,{runtime:this.config.runtime,quantization:this.config.quantization,cache:this.config.cache})}async run(t,e){await this.initialize();let n=performance.now(),s=await this.preprocess(t),i=await X(this.model,s),r=await this.postprocess(i,e);return r&&typeof r=="object"&&"processingTime"in r&&(r.processingTime=performance.now()-n),r}async runBatch(t,e){return await this.initialize(),await Promise.all(t.map(s=>this.run(s,e)))}get task(){return this.config.task}get ready(){return this.isReady}dispose(){this.model&&(this.model.dispose(),this.model=null),this.isReady=!1}},vn=new Map;function N(o,t){vn.set(o,t)}function En(o){return vn.get(o)}var nt=["negative","positive"],Dn=["anger","disgust","fear","joy","sadness","surprise","neutral"],st=["tench","goldfish","great white shark","tiger shark","hammerhead","electric ray","stingray","cock","hen","ostrich"];C();B();var P=class o{constructor(){f(this,"vocab",new Map);f(this,"reverseVocab",new Map);f(this,"merges",new Map);f(this,"addedTokens",new Map);f(this,"specialTokens",new Set);f(this,"modelType","BPE");f(this,"unkToken","[UNK]");f(this,"continuingSubwordPrefix","##");f(this,"padTokenId",0);f(this,"unkTokenId",0);f(this,"clsTokenId");f(this,"sepTokenId");f(this,"maskTokenId");f(this,"bosTokenId");f(this,"eosTokenId");f(this,"maxLength",512);f(this,"doLowerCase",!1);f(this,"stripAccents",!1);f(this,"postProcessor");f(this,"byteEncoder",new Map);f(this,"byteDecoder",new Map);this.initByteEncoder()}initByteEncoder(){let t=[];for(let s=33;s<=126;s++)t.push(s);for(let s=161;s<=172;s++)t.push(s);for(let s=174;s<=255;s++)t.push(s);let e=[...t],n=0;for(let s=0;s<256;s++)t.includes(s)||(t.push(s),e.push(256+n),n++);for(let s=0;s<t.length;s++){let i=t[s],r=String.fromCharCode(e[s]);this.byteEncoder.set(i,r),this.byteDecoder.set(r,i)}}static async fromJSON(t){let e=new o,n=typeof t=="string"?JSON.parse(t):t;if(n.model){if(e.modelType=n.model.type,n.model.vocab)if(Array.isArray(n.model.vocab)){let s=n.model.vocab;for(let i=0;i<s.length;i++){let r=s[i],a=Array.isArray(r)?r[0]:r;e.vocab.set(a,i),e.reverseVocab.set(i,a)}}else for(let[s,i]of Object.entries(n.model.vocab))e.vocab.set(s,i),e.reverseVocab.set(i,s);if(n.model.merges)for(let s=0;s<n.model.merges.length;s++)e.merges.set(n.model.merges[s],s);e.unkToken=n.model.unk_token??"[UNK]",e.continuingSubwordPrefix=n.model.continuing_subword_prefix??"##"}if(n.added_tokens)for(let s of n.added_tokens){e.addedTokens.set(s.content,s.id),e.reverseVocab.set(s.id,s.content),s.special&&e.specialTokens.add(s.content);let i=s.content.toLowerCase();i.includes("pad")&&(e.padTokenId=s.id),i.includes("unk")&&(e.unkTokenId=s.id),(i.includes("cls")||i==="[cls]")&&(e.clsTokenId=s.id),(i.includes("sep")||i==="[sep]")&&(e.sepTokenId=s.id),i.includes("mask")&&(e.maskTokenId=s.id),(i.includes("bos")||i==="<s>")&&(e.bosTokenId=s.id),(i.includes("eos")||i==="</s>")&&(e.eosTokenId=s.id)}return n.normalizer&&(e.doLowerCase=n.normalizer.lowercase??!1,e.stripAccents=n.normalizer.strip_accents??!1),n.truncation&&(e.maxLength=n.truncation.max_length),n.post_processor&&(e.postProcessor=n.post_processor),e}static async fromUrl(t){let e=await fetch(t);if(!e.ok)throw new x(`Failed to load tokenizer from ${t}: ${e.status}`,M.MODEL_NOT_FOUND);let n=await e.json();return o.fromJSON(n)}static async fromHuggingFace(t,e){let n=e?.revision??"main",s=`https://huggingface.co/${t}/resolve/${n}/tokenizer.json`;return o.fromUrl(s)}normalize(t){let e=t;return this.doLowerCase&&(e=e.toLowerCase()),this.stripAccents&&(e=e.normalize("NFD").replace(/[\u0300-\u036f]/g,"")),e=e.replace(/\s+/g," ").trim(),e}preTokenize(t){let e=/'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+/gu;return t.match(e)??[t]}textToBytes(t){let n=new TextEncoder().encode(t);return Array.from(n).map(s=>this.byteEncoder.get(s)??"").join("")}bytesToText(t){let e=new Uint8Array(t.split("").map(s=>this.byteDecoder.get(s)??0));return new TextDecoder("utf-8",{fatal:!1}).decode(e)}getPairs(t){let e=new Set;for(let n=0;n<t.length-1;n++)e.add(`${t[n]} ${t[n+1]}`);return e}bpe(t){if(this.vocab.has(t))return[t];let e=t.split(""),n=this.getPairs(e);if(n.size===0)return[t];for(;;){let s=null,i=1/0;for(let u of n){let h=this.merges.get(u);h!==void 0&&h<i&&(i=h,s=u)}if(s===null)break;let r=s.split(" "),a=r[0],c=r[1];if(!a||!c)break;let l=[],d=0;for(;d<e.length;){let u=e.indexOf(a,d);if(u===-1){l.push(...e.slice(d));break}l.push(...e.slice(d,u)),e[u]===a&&u<e.length-1&&e[u+1]===c?(l.push(a+c),d=u+2):(l.push(e[u]),d=u+1)}if(e=l,e.length===1)break;n=this.getPairs(e)}return e}wordPiece(t){if(this.vocab.has(t))return[t];let e=[],n=0;for(;n<t.length;){let s=t.length,i=null;for(;n<s;){let r=t.slice(n,s);if(n>0&&(r=this.continuingSubwordPrefix+r),this.vocab.has(r)){i=r;break}s--}i===null?(e.push(this.unkToken),n++):(e.push(i),n=s)}return e}tokenizeWord(t){if(this.addedTokens.has(t))return[t];switch(this.modelType){case"BPE":{let e=this.textToBytes(t);return this.bpe(e)}case"WordPiece":return this.wordPiece(t);case"Unigram":return this.unigramTokenize(t);default:return this.vocab.has(t)?[t]:[this.unkToken]}}unigramTokenize(t){let e="\u2581"+t,n=[],s=0,i=e;for(;s<i.length;){let r=i.length,a=!1;for(;r>s;){let c=i.slice(s,r);if(this.vocab.has(c)){n.push(c),s=r,a=!0;break}r--}if(!a){let c=i[s];n.push(this.vocab.has(c)?c:this.unkToken),s++}}return n.length>0?n:[this.unkToken]}tokenize(t){let e=this.normalize(t),n=[],s=e,i=Array.from(this.addedTokens.keys()).sort((r,a)=>a.length-r.length);for(let r of i)if(s.includes(r)){let a=s.split(r),c=[];for(let l=0;l<a.length;l++)a[l]&&c.push(a[l]),l<a.length-1&&n.push(r);s=c.join(" ")}if(s.trim()){let r=this.preTokenize(s);for(let a of r){if(!a)continue;let c=this.tokenizeWord(a);n.push(...c)}}return n}convertTokensToIds(t){return t.map(e=>{let n=this.addedTokens.get(e);if(n!==void 0)return n;let s=this.vocab.get(e);return s!==void 0?s:this.unkTokenId})}convertIdsToTokens(t){return t.map(e=>this.reverseVocab.get(e)??this.unkToken)}postProcess(t,e){if(!this.postProcessor){let r=[],a=[];return this.clsTokenId!==void 0&&(r.push(this.clsTokenId),a.push(0)),r.push(...t),a.push(...t.map(()=>0)),this.sepTokenId!==void 0&&(r.push(this.sepTokenId),a.push(0)),e&&(r.push(...e),a.push(...e.map(()=>1)),this.sepTokenId!==void 0&&(r.push(this.sepTokenId),a.push(1))),{ids:r,typeIds:a}}let n=e?this.postProcessor.pair:this.postProcessor.single;if(!n)return{ids:t,typeIds:t.map(()=>0)};let s=[],i=[];for(let r of n)if("SpecialToken"in r){let a=this.postProcessor.special_tokens?.[r.SpecialToken.id];a&&(s.push(...a.ids),i.push(...a.ids.map(()=>r.SpecialToken.type_id)))}else if("Sequence"in r){let a=r.Sequence.id==="A"?t:e??[];s.push(...a),i.push(...a.map(()=>r.Sequence.type_id))}return{ids:s,typeIds:i}}encode(t,e={}){let{addSpecialTokens:n=!0,maxLength:s=this.maxLength,padding:i="max_length",truncation:r=!0,returnAttentionMask:a=!0,returnTokenTypeIds:c=!1,textPair:l}=e,d=this.tokenize(t),u=this.convertTokensToIds(d),h;if(l){let g=this.tokenize(l);h=this.convertTokensToIds(g)}let p;if(n){let g=this.postProcess(u,h);u=g.ids,c&&(p=g.typeIds)}else h&&(u=[...u,...h],c&&(p=[...u.map(()=>0),...h.map(()=>1)]));r&&u.length>s&&(u=u.slice(0,s),p&&(p=p.slice(0,s)));let m=[];if(a&&(m=u.map(()=>1)),i==="max_length"&&u.length<s){let g=s-u.length;u=[...u,...new Array(g).fill(this.padTokenId)],a&&(m=[...m,...new Array(g).fill(0)]),p&&(p=[...p,...new Array(g).fill(0)])}let y={inputIds:u,attentionMask:m};return c&&p&&(y.tokenTypeIds=p),y}encodeBatch(t,e={}){if(e.padding==="longest"){let n=t.map(i=>this.encode(i,{...e,padding:"do_not_pad"})),s=Math.max(...n.map(i=>i.inputIds.length));return t.map(i=>this.encode(i,{...e,maxLength:s,padding:"max_length"}))}return t.map(n=>this.encode(n,e))}decode(t,e=!0){let n=this.convertIdsToTokens(t);if(e&&(n=n.filter(s=>!this.specialTokens.has(s))),this.modelType==="BPE")return this.bytesToText(n.join("")).replace(/\s+/g," ").trim();if(this.modelType==="WordPiece"){let s=this.continuingSubwordPrefix,i=[];for(let r of n)r.startsWith(s)?i.length>0?i[i.length-1]+=r.slice(s.length):i.push(r.slice(s.length)):i.push(r);return i.join(" ").replace(/\s+/g," ").trim()}return this.modelType==="Unigram"?n.join("").replace(/\u2581/g," ").replace(/\s+/g," ").trim():n.join(" ").replace(/\s+/g," ").trim()}decodeBatch(t,e=!0){return t.map(n=>this.decode(n,e))}get vocabSize(){return this.vocab.size+this.addedTokens.size}getSpecialTokenIds(){return{padTokenId:this.padTokenId,unkTokenId:this.unkTokenId,clsTokenId:this.clsTokenId,sepTokenId:this.sepTokenId,maskTokenId:this.maskTokenId,bosTokenId:this.bosTokenId,eosTokenId:this.eosTokenId}}getConfig(){return{vocabSize:this.vocabSize,maxLength:this.maxLength,padTokenId:this.padTokenId,unkTokenId:this.unkTokenId,clsTokenId:this.clsTokenId,sepTokenId:this.sepTokenId,maskTokenId:this.maskTokenId,bosTokenId:this.bosTokenId,eosTokenId:this.eosTokenId}}isSpecialToken(t){return this.specialTokens.has(t)}getTokenId(t){return this.addedTokens.get(t)??this.vocab.get(t)}getToken(t){return this.reverseVocab.get(t)}};function Fn(){return new P}async function Pn(o){return P.fromUrl(o)}async function _n(o,t){return P.fromHuggingFace(o,t)}G();var Ln={model:"https://huggingface.co/Xenova/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/onnx/model_quantized.onnx",tokenizer:"https://huggingface.co/Xenova/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/tokenizer.json"},sr=["NEGATIVE","POSITIVE"],Y=class extends E{constructor(e,n){super(e);f(this,"tokenizer",null);f(this,"onnxModel",null);f(this,"labels");f(this,"modelUrl");f(this,"tokenizerUrl");this.labels=n??sr,this.modelUrl=e.model!=="default"?e.model:Ln.model,this.tokenizerUrl=Ln.tokenizer}async initialize(){if(await super.initialize(),this.tokenizer||(this.tokenizer=await P.fromUrl(this.tokenizerUrl)),!this.onnxModel){let e=await v(this.modelUrl,{cache:this.config.cache??!0});this.onnxModel=await F(e)}}setLabels(e){this.labels=e}async run(e,n){let s=Array.isArray(e),i=s?e:[e];await this.initialize();let r=performance.now(),a=[];for(let l of i){let d=await this.preprocess(l),u=await this.runInference(d),h=await this.postprocess(u,n);a.push(h)}let c=performance.now()-r;for(let l of a)l.processingTime=c/a.length;return s?a:a[0]}async preprocess(e){let n=Array.isArray(e)?e[0]:e,s=this.tokenizer.encode(n,{maxLength:128,padding:"max_length",truncation:!0}),i=new w(BigInt64Array.from(s.inputIds.map(a=>BigInt(a))),[1,s.inputIds.length],"int64"),r=new w(BigInt64Array.from(s.attentionMask.map(a=>BigInt(a))),[1,s.attentionMask.length],"int64");return[i,r]}async runInference(e){let n=new Map;return n.set("input_ids",e[0]),n.set("attention_mask",e[1]),await U(this.onnxModel,n)}async postprocess(e,n){let s=e[0];if(!s)return{label:"unknown",score:0};let r=O(s,-1).toFloat32Array(),a=0,c=r[0]??0;for(let d=1;d<r.length;d++)(r[d]??0)>c&&(c=r[d]??0,a=d);return{label:n?.labels?.[a]??this.labels[a]??`class_${a}`,score:c}}},te=class extends Y{constructor(t){super(t,nt)}async analyze(t,e){return this.run(t,e)}};function Nn(o={}){return new Y({task:"text-classification",model:o.model??"default",runtime:o.runtime,cache:o.cache??!0,quantization:o.quantization})}function Cn(o={}){return new te({task:"sentiment-analysis",model:o.model??"default",runtime:o.runtime,cache:o.cache??!0,quantization:o.quantization})}N("text-classification",o=>new Y(o));N("sentiment-analysis",o=>new te(o));C();G();var On={model:"https://huggingface.co/Xenova/all-MiniLM-L6-v2/resolve/main/onnx/model_quantized.onnx",tokenizer:"https://huggingface.co/Xenova/all-MiniLM-L6-v2/resolve/main/tokenizer.json"},rr=384,ne=class extends E{constructor(e,n=rr){super(e);f(this,"tokenizer",null);f(this,"onnxModel",null);f(this,"embeddingDim");f(this,"modelUrl");f(this,"tokenizerUrl");f(this,"modelInputNames",new Set);this.embeddingDim=n,this.modelUrl=e.model!=="default"?e.model:On.model,this.tokenizerUrl=e.tokenizerUrl??On.tokenizer}async initialize(){if(await super.initialize(),this.tokenizer||(this.tokenizer=await P.fromUrl(this.tokenizerUrl)),!this.onnxModel){let e=await v(this.modelUrl,{cache:this.config.cache??!0});this.onnxModel=await F(e),this.modelInputNames=new Set(this.onnxModel.metadata.inputs.map(n=>n.name))}}async run(e,n){let s=Array.isArray(e),i=s?e:[e];await this.initialize();let r=performance.now(),a=[];for(let l of i){let d=await this.preprocess(l),u=await this.runInference(d),h=await this.postprocess(u,n);a.push(h)}let c=performance.now()-r;for(let l of a)l.processingTime=c/a.length;return s?a:a[0]}async preprocess(e){let n=Array.isArray(e)?e[0]:e,s=this.tokenizer.encode(n,{maxLength:128,padding:"max_length",truncation:!0}),i=new w(BigInt64Array.from(s.inputIds.map(c=>BigInt(c))),[1,s.inputIds.length],"int64"),r=new w(BigInt64Array.from(s.attentionMask.map(c=>BigInt(c))),[1,s.attentionMask.length],"int64"),a=[i,r];if(this.modelInputNames.has("token_type_ids")){let c=new w(BigInt64Array.from(s.inputIds.map(()=>BigInt(0))),[1,s.inputIds.length],"int64");a.push(c)}return a}async runInference(e){let n=new Map;return n.set("input_ids",e[0]),n.set("attention_mask",e[1]),this.modelInputNames.has("token_type_ids")&&e[2]&&n.set("token_type_ids",e[2]),await U(this.onnxModel,n)}async postprocess(e,n){let s=e[0];if(!s)return{embeddings:[]};let i=n?.pooling??"mean",r=n?.normalize??!0,a;switch(i){case"cls":a=this.extractCLSEmbedding(s);break;case"max":a=this.maxPooling(s);break;case"none":a=s.toArray();break;case"mean":default:a=this.meanPooling(s);break}return r&&(a=this.normalizeVector(a)),n?.outputDim&&n.outputDim<a.length&&(a=a.slice(0,n.outputDim)),{embeddings:a}}extractCLSEmbedding(e){let n=e.toFloat32Array(),s=e.shape[2]??this.embeddingDim;return Array.from(n.slice(0,s))}meanPooling(e){let n=e.toFloat32Array(),s=e.shape[1]??1,i=e.shape[2]??this.embeddingDim,r=new Float32Array(i);for(let a=0;a<s;a++)for(let c=0;c<i;c++)r[c]=(r[c]??0)+(n[a*i+c]??0)/s;return Array.from(r)}maxPooling(e){let n=e.toFloat32Array(),s=e.shape[1]??1,i=e.shape[2]??this.embeddingDim,r=new Array(i).fill(-1/0);for(let a=0;a<s;a++)for(let c=0;c<i;c++){let l=n[a*i+c]??0;l>(r[c]??-1/0)&&(r[c]=l)}return r}normalizeVector(e){let n=0;for(let s of e)n+=s*s;return n=Math.sqrt(n),n===0?e:e.map(s=>s/n)}};function $n(o={}){return new ne({task:"feature-extraction",model:o.model??"default",runtime:o.runtime,cache:o.cache??!0,quantization:o.quantization,tokenizerUrl:o.tokenizerUrl})}N("feature-extraction",o=>new ne(o));C();C();var _t={width:224,height:224,resizeMode:"cover",mean:[.485,.456,.406],std:[.229,.224,.225],rescaleFactor:1/255,grayscale:!1,channelFormat:"CHW",dtype:"float32",doResize:!0,doRescale:!0,doNormalize:!0,doCenterCrop:!1,paddingColor:[0,0,0]},he=class o{constructor(t={}){f(this,"options");f(this,"canvas",null);f(this,"ctx",null);let e=t.size,n=t.width??e??_t.width,s=t.height??e??_t.height;this.options={..._t,...t,width:n,height:s,size:e??n,cropSize:t.cropSize??t.size??n}}static fromConfig(t){let e={},n=t.size;if(n!==void 0){if(typeof n=="number")e.size=n;else if(typeof n=="object"&&n!==null){let h=n;e.width=h.width??h.shortest_edge,e.height=h.height??h.shortest_edge}}let s=t.crop_size;if(s!==void 0){if(typeof s=="number")e.cropSize=s;else if(typeof s=="object"&&s!==null){let h=s;e.cropSize={width:h.width??224,height:h.height??224}}}let i=t.image_mean;Array.isArray(i)&&(e.mean=i);let r=t.image_std;Array.isArray(r)&&(e.std=r);let a=t.rescale_factor;typeof a=="number"&&(e.rescaleFactor=a);let c=t.do_resize;typeof c=="boolean"&&(e.doResize=c);let l=t.do_rescale;typeof l=="boolean"&&(e.doRescale=l);let d=t.do_normalize;typeof d=="boolean"&&(e.doNormalize=d);let u=t.do_center_crop;return typeof u=="boolean"&&(e.doCenterCrop=u),t.resample!==void 0&&(e.resizeMode="cover"),new o(e)}static async fromUrl(t){let e=await fetch(t);if(!e.ok)throw new Error(`Failed to load preprocessor config from ${t}`);let n=await e.json();return o.fromConfig(n)}static async fromHuggingFace(t,e){let n=e?.revision??"main",s=`https://huggingface.co/${t}/resolve/${n}/preprocessor_config.json`;return o.fromUrl(s)}ensureCanvas(){if(!this.canvas)if(typeof document<"u")this.canvas=document.createElement("canvas"),this.ctx=this.canvas.getContext("2d");else throw new Error("ImagePreprocessor requires a browser environment")}async process(t){let e;typeof t=="string"?e=await this.loadFromUrl(t):t instanceof Blob||t instanceof File?e=await this.loadFromBlob(t):t instanceof ImageData?e=t:e=this.toImageData(t);let n=e;return this.options.doResize&&(n=this.resize(n)),this.options.doCenterCrop&&(n=this.centerCrop(n)),this.toTensor(n)}async processBatch(t){let e=await Promise.all(t.map(l=>this.process(l))),n=e.length,s=e[0];if(!s)return new w(new Float32Array(0),[0],"float32");let i=s.shape[0]??3,r=s.shape[1]??this.options.height,a=s.shape[2]??this.options.width,c=new Float32Array(n*i*r*a);for(let l=0;l<e.length;l++){let d=e[l];d&&c.set(d.toFloat32Array(),l*i*r*a)}return new w(c,[n,i,r,a],"float32")}async loadFromUrl(t){return new Promise((e,n)=>{let s=new Image;s.crossOrigin="anonymous",s.onload=()=>{e(this.toImageData(s))},s.onerror=()=>{n(new Error(`Failed to load image from ${t}`))},s.src=t})}async loadFromBlob(t){let e=URL.createObjectURL(t);try{return await this.loadFromUrl(e)}finally{URL.revokeObjectURL(e)}}centerCrop(t){let e=this.options.cropSize,n,s;typeof e=="number"?(n=e,s=e):(n=e.width,s=e.height);let i=Math.max(0,Math.floor((t.width-n)/2)),r=Math.max(0,Math.floor((t.height-s)/2));this.ensureCanvas();let a=document.createElement("canvas");return a.width=t.width,a.height=t.height,a.getContext("2d").putImageData(t,0,0),this.canvas.width=n,this.canvas.height=s,this.ctx.drawImage(a,i,r,n,s,0,0,n,s),this.ctx.getImageData(0,0,n,s)}toImageData(t){this.ensureCanvas();let{width:e,height:n}=t;return this.canvas.width=e,this.canvas.height=n,this.ctx.drawImage(t,0,0),this.ctx.getImageData(0,0,e,n)}resize(t){let{width:e,height:n,resizeMode:s}=this.options;this.ensureCanvas();let i=0,r=0,a=t.width,c=t.height,l=0,d=0,u=e,h=n;if(s==="contain"){let y=Math.min(e/t.width,n/t.height);u=Math.round(t.width*y),h=Math.round(t.height*y),l=Math.round((e-u)/2),d=Math.round((n-h)/2)}else if(s==="cover"){let y=Math.max(e/t.width,n/t.height);a=Math.round(e/y),c=Math.round(n/y),i=Math.round((t.width-a)/2),r=Math.round((t.height-c)/2)}let p=document.createElement("canvas");return p.width=t.width,p.height=t.height,p.getContext("2d").putImageData(t,0,0),this.canvas.width=e,this.canvas.height=n,(s==="contain"||s==="pad")&&(this.ctx.fillStyle="black",this.ctx.fillRect(0,0,e,n)),this.ctx.drawImage(p,i,r,a,c,l,d,u,h),this.ctx.getImageData(0,0,e,n)}toTensor(t){let{mean:e,std:n,grayscale:s,channelFormat:i,dtype:r,doRescale:a,rescaleFactor:c,doNormalize:l}=this.options,d=t.height,u=t.width,h=s?1:3,p=new Float32Array(h*d*u),m=t.data;for(let g=0;g<d;g++)for(let T=0;T<u;T++){let k=(g*u+T)*4;if(s){let b=.299*(m[k]??0)+.587*(m[k+1]??0)+.114*(m[k+2]??0);a&&(b*=c),l&&(b=(b-(e[0]??0))/(n[0]??1));let z=g*u+T;p[z]=b}else if(i==="CHW")for(let b=0;b<3;b++){let z=m[k+b]??0;a&&(z*=c),l&&(z=(z-(e[b]??0))/(n[b]??1));let I=b*d*u+g*u+T;p[I]=z}else for(let b=0;b<3;b++){let z=m[k+b]??0;a&&(z*=c),l&&(z=(z-(e[b]??0))/(n[b]??1));let I=g*u*3+T*3+b;p[I]=z}}let y=i==="CHW"?[h,d,u]:[d,u,h];return new w(p,y,r)}getOptions(){return{...this.options}}},or={sampleRate:16e3,nMels:80,nFft:400,hopLength:160,normalize:!0,maxDuration:30},fe=class o{constructor(t={}){f(this,"options");f(this,"audioContext",null);this.options={...or,...t}}static fromConfig(t){let e={},n=t.sampling_rate;typeof n=="number"&&(e.sampleRate=n);let s=t.feature_size;typeof s=="number"&&(e.nMels=s);let i=t.n_fft;typeof i=="number"&&(e.nFft=i);let r=t.hop_length;return typeof r=="number"&&(e.hopLength=r),new o(e)}static async fromHuggingFace(t,e){let n=e?.revision??"main",s=`https://huggingface.co/${t}/resolve/${n}/preprocessor_config.json`,i=await fetch(s);if(!i.ok)throw new Error(`Failed to load audio config from ${s}`);let r=await i.json();return o.fromConfig(r)}ensureAudioContext(){if(!this.audioContext)if(typeof AudioContext<"u")this.audioContext=new AudioContext({sampleRate:this.options.sampleRate});else throw new Error("AudioPreprocessor requires Web Audio API support")}async process(t){let e;typeof t=="string"?e=await this.loadFromUrl(t):t instanceof Blob||t instanceof File?e=await this.loadFromBlob(t):t instanceof AudioBuffer?e=this.audioBufferToFloat32(t):t instanceof Float32Array?e=t:e=await this.decodeAudioData(t),this.options.normalize&&(e=this.normalizeAudio(e));let n=this.options.maxDuration*this.options.sampleRate;return e.length>n&&(e=e.slice(0,n)),this.computeMelSpectrogram(e)}async processRaw(t){let e;typeof t=="string"?e=await this.loadFromUrl(t):t instanceof Blob||t instanceof File?e=await this.loadFromBlob(t):t instanceof AudioBuffer?e=this.audioBufferToFloat32(t):t instanceof Float32Array?e=t:e=await this.decodeAudioData(t),this.options.normalize&&(e=this.normalizeAudio(e));let n=this.options.maxDuration*this.options.sampleRate;return e.length>n&&(e=e.slice(0,n)),new w(e,[1,e.length],"float32")}async loadFromUrl(t){let e=await fetch(t);if(!e.ok)throw new Error(`Failed to load audio from ${t}`);let n=await e.arrayBuffer();return this.decodeAudioData(n)}async loadFromBlob(t){let e=await t.arrayBuffer();return this.decodeAudioData(e)}async decodeAudioData(t){this.ensureAudioContext();let e=await this.audioContext.decodeAudioData(t.slice(0));return this.audioBufferToFloat32(e)}audioBufferToFloat32(t){let e=t.getChannelData(0);return new Float32Array(e)}normalizeAudio(t){let e=0;for(let n=0;n<t.length;n++){let s=Math.abs(t[n]??0);s>e&&(e=s)}if(e>0){let n=new Float32Array(t.length);for(let s=0;s<t.length;s++)n[s]=(t[s]??0)/e;return n}return t}computeMelSpectrogram(t){let{nMels:e,nFft:n,hopLength:s}=this.options,i=Math.floor((t.length-n)/s)+1;if(i<=0)return new w(new Float32Array(e),[1,e],"float32");let r=new Float32Array(i*e);for(let a=0;a<i;a++){let c=a*s;for(let l=0;l<e;l++){let d=0,u=Math.floor(l/e*(n/2)),h=Math.floor((l+1)/e*(n/2));for(let p=u;p<Math.min(h,n);p++){let m=t[c+p]??0;d+=m*m}r[a*e+l]=Math.log(d+1e-10)}}return new w(r,[i,e],"float32")}dispose(){this.audioContext&&(this.audioContext.close(),this.audioContext=null)}};function Rn(o,t={}){let{lowercase:e=!0,removePunctuation:n=!1,normalizeWhitespace:s=!0,maxLength:i}=t,r=o;return e&&(r=r.toLowerCase()),n&&(r=r.replace(/[^\w\s]/g,"")),s&&(r=r.replace(/\s+/g," ").trim()),i&&r.length>i&&(r=r.slice(0,i)),r}function rt(o="imagenet",t={}){let e={imagenet:{width:224,height:224,mean:[.485,.456,.406],std:[.229,.224,.225]},clip:{width:224,height:224,mean:[.48145466,.4578275,.40821073],std:[.26862954,.26130258,.27577711]},vit:{width:224,height:224,mean:[.5,.5,.5],std:[.5,.5,.5]},custom:{}};return new he({...e[o],...t})}function Un(o="whisper",t={}){let e={whisper:{sampleRate:16e3,nMels:80,nFft:400,hopLength:160},wav2vec:{sampleRate:16e3,normalize:!0},custom:{}};return new fe({...e[o],...t})}G();var ir={model:"https://huggingface.co/Xenova/mobilevit-small/resolve/main/onnx/model_quantized.onnx"},se=class extends E{constructor(e,n,s=1e3){super(e);f(this,"preprocessor",null);f(this,"onnxModel",null);f(this,"labels");f(this,"modelUrl");this.labels=n??st,this.modelUrl=e.model!=="default"?e.model:ir.model}async initialize(){if(await super.initialize(),this.preprocessor||(this.preprocessor=rt("imagenet")),!this.onnxModel){let e=await v(this.modelUrl,{cache:this.config.cache??!0});this.onnxModel=await F(e)}}setLabels(e){this.labels=e}async run(e,n){let s=Array.isArray(e),i=s?e:[e];await this.initialize();let r=performance.now(),a=[];for(let l of i){let d=await this.preprocess(l),u=await this.runModelInference(d),h=await this.postprocess(u,n);a.push(h)}let c=performance.now()-r;for(let l of a)l.processingTime=c/a.length;return s?a:a[0]}async preprocess(e){let n=Array.isArray(e)?e[0]:e,s=await this.preprocessor.process(n);return s.shape.length===3?[s.reshape([1,...s.shape])]:[s]}async runModelInference(e){return await X(this.onnxModel,e)}async postprocess(e,n){let s=e[0];if(!s)return{label:"unknown",score:0};let r=O(s,-1).toFloat32Array(),a=0,c=r[0]??0;for(let d=1;d<r.length;d++)(r[d]??0)>c&&(c=r[d]??0,a=d);return{label:n?.labels?.[a]??this.labels[a]??`class_${a}`,score:c}}};function Bn(o={},t){return new se({task:"image-classification",model:o.model??"default",runtime:o.runtime,cache:o.cache??!0,quantization:o.quantization},t)}N("image-classification",o=>new se(o));C();var qn={model:"https://huggingface.co/Xenova/TinyLlama-1.1B-Chat-v1.0/resolve/main/onnx/model_q4f16.onnx",tokenizer:"https://huggingface.co/Xenova/TinyLlama-1.1B-Chat-v1.0/resolve/main/tokenizer.json"},pe=class extends E{constructor(e){super(e??{task:"text-generation",model:"default"});f(this,"tokenizer",null);f(this,"eosTokenId",50256);f(this,"llmModel",null);f(this,"modelsLoaded",!1);f(this,"modelUrl");f(this,"tokenizerUrl");f(this,"conversationHistory",[]);f(this,"chatTemplateType","chatml");this.modelUrl=qn.model,this.tokenizerUrl=qn.tokenizer}get isModelLoaded(){return this.modelsLoaded}setModelUrls(e,n){this.modelUrl=e,this.tokenizerUrl=n}async loadModel(e){if(this.modelsLoaded)return;e?.({stage:"tokenizer",loaded:0,total:100,progress:0});try{let s=await fetch(this.tokenizerUrl);if(!s.ok)throw new Error(`Failed to fetch tokenizer: ${s.status}`);let i=await s.json();this.tokenizer=await P.fromJSON(i);let r=this.tokenizer.getSpecialTokenIds();this.eosTokenId=r.eosTokenId??r.sepTokenId??2,e?.({stage:"tokenizer",loaded:100,total:100,progress:100})}catch(s){throw new Error(`Failed to load tokenizer: ${s}`)}e?.({stage:"model",loaded:0,total:100,progress:0});let n=await this.fetchModelWithProgress(this.modelUrl,(s,i)=>{e?.({stage:"model",loaded:s,total:i,progress:Math.round(s/i*100)})});this.llmModel=await F(n,{runtime:"wasm"}),this.model=this.llmModel,this.modelsLoaded=!0}async fetchModelWithProgress(e,n){let s=await fetch(e);if(!s.ok)throw new Error(`Failed to fetch model: ${s.status} ${s.statusText}`);let i=s.headers.get("content-length"),r=i?parseInt(i,10):0;if(!s.body){let h=await s.arrayBuffer();return n(h.byteLength,h.byteLength),h}let a=s.body.getReader(),c=[],l=0;for(;;){let{done:h,value:p}=await a.read();if(h)break;c.push(p),l+=p.length,n(l,r||l)}let d=new Uint8Array(l),u=0;for(let h of c)d.set(h,u),u+=h.length;return d.buffer}async initialize(){this.isReady||(this.isReady=!0)}setTokenizer(e){this.tokenizer=e;let n=e.getSpecialTokenIds();this.eosTokenId=n.eosTokenId??n.sepTokenId??50256}async preprocess(e){let n=Array.isArray(e)?e[0]??"":e;if(!this.tokenizer)return[new w(new Float32Array([0]),[1],"float32")];let s=this.tokenizer.encode(n,{addSpecialTokens:!1,padding:"do_not_pad"});return[new w(BigInt64Array.from(s.inputIds.map(i=>BigInt(i))),[1,s.inputIds.length],"int64")]}async postprocess(e,n){return{generatedText:"",tokenIds:[],numTokens:0,processingTime:0}}async run(e,n){await this.initialize();let s=Array.isArray(e)?e:[e],i=await Promise.all(s.map(r=>this.generateSingle(r,n??{})));return Array.isArray(e)?i:i[0]}async*stream(e,n={}){let s=performance.now();if(!this.tokenizer)throw new Error("Tokenizer not set. Call setTokenizer() first.");let{maxNewTokens:i=50,maxLength:r=512,temperature:a=1,topK:c=0,topP:l=1,repetitionPenalty:d=1,stopSequences:u=[],doSample:h=!0}=n,m=[...this.tokenizer.encode(e,{addSpecialTokens:!1,padding:"do_not_pad",truncation:!1}).inputIds],y=[],g="";for(let k=0;k<i&&!(m.length>=r);k++){let b=await this.generateNextToken(m,a,c,l,d,h);if(b===this.eosTokenId){yield{token:"",tokenId:b,generatedText:g,done:!0};break}let z=this.tokenizer.decode([b],!0);y.push(b),m.push(b),g+=z,n.onToken&&n.onToken(z,b);let I=!1;for(let S of u)if(g.endsWith(S)){g=g.slice(0,-S.length),I=!0;break}if(yield{token:z,tokenId:b,generatedText:g,done:I},I)break}let T=performance.now();console.log(`Generation completed in ${(T-s).toFixed(2)}ms`)}async generateSingle(e,n){let s=performance.now();if(!this.tokenizer)throw new Error("Tokenizer not set. Call setTokenizer() first.");let{maxNewTokens:i=50,maxLength:r=512,temperature:a=1,topK:c=0,topP:l=1,repetitionPenalty:d=1,stopSequences:u=[],doSample:h=!0,returnFullText:p=!1}=n,y=[...this.tokenizer.encode(e,{addSpecialTokens:!1,padding:"do_not_pad",truncation:!1}).inputIds],g=[];for(let b=0;b<i&&!(y.length>=r);b++){let z=await this.generateNextToken(y,a,c,l,d,h);if(z===this.eosTokenId)break;if(g.push(z),y.push(z),n.onToken){let D=this.tokenizer.decode([z],!0);n.onToken(D,z)}let I=this.tokenizer.decode(g,!0),S=!1;for(let D of u)if(I.endsWith(D)){S=!0;break}if(S)break}let T=this.tokenizer.decode(g,!0),k=performance.now();return{generatedText:T,fullText:p?e+T:void 0,tokenIds:g,numTokens:g.length,processingTime:k-s}}async generateNextToken(e,n,s,i,r,a){if(!this.model)throw new Error("Model not loaded");let c=e.length,l=new Map;l.set("input_ids",new w(BigInt64Array.from(e.map(I=>BigInt(I))),[1,c],"int64")),l.set("attention_mask",new w(BigInt64Array.from(e.map(()=>BigInt(1))),[1,c],"int64")),l.set("position_ids",new w(BigInt64Array.from(Array.from({length:c},(I,S)=>BigInt(S))),[1,c],"int64"));let d=22,u=4,h=64;for(let I=0;I<d;I++)l.set(`past_key_values.${I}.key`,new w(new Float32Array(0),[1,u,0,h],"float32")),l.set(`past_key_values.${I}.value`,new w(new Float32Array(0),[1,u,0,h],"float32"));let p=await U(this.model,l);if(!p||p.length===0)throw new Error("Model returned no outputs");let m=p[0],y=m.toFloat32Array(),g=m.shape[m.shape.length-1]??50257,T=new Float32Array(g),k=(e.length-1)*g;for(let I=0;I<g;I++)T[I]=y[k+I]??0;if(r!==1){for(let I of e)if(I<g){let S=T[I]??0;T[I]=S>0?S/r:S*r}}if(n!==1)for(let I=0;I<g;I++)T[I]=(T[I]??0)/n;let b=new w(T,[g],"float32"),z=O(b).toFloat32Array();return a?this.sample(z,s,i):this.greedy(z)}greedy(e){let n=0,s=e[0]??0;for(let i=1;i<e.length;i++)(e[i]??0)>s&&(s=e[i]??0,n=i);return n}sample(e,n,s){let i=Array.from({length:e.length},(d,u)=>u);i.sort((d,u)=>(e[u]??0)-(e[d]??0));let r=i;if(n>0&&n<e.length&&(r=i.slice(0,n)),s<1){let d=0,u=[];for(let h of r)if(u.push(h),d+=e[h]??0,d>=s)break;r=u}let a=0;for(let d of r)a+=e[d]??0;let c=Math.random()*a,l=0;for(let d of r)if(l+=e[d]??0,l>=c)return d;return r[0]??0}setChatTemplate(e){this.chatTemplateType=e}applyChatTemplate(e,n){switch(n?.templateType??this.chatTemplateType){case"chatml":return this.applyChatMLTemplate(e);case"llama2":return this.applyLlama2Template(e);case"llama3":return this.applyLlama3Template(e);case"mistral":return this.applyMistralTemplate(e);case"phi3":return this.applyPhi3Template(e);case"alpaca":return this.applyAlpacaTemplate(e);case"vicuna":return this.applyVicunaTemplate(e);case"custom":return this.applyCustomTemplate(e,n?.customTemplate??{});default:return this.applyChatMLTemplate(e)}}applyChatMLTemplate(e){let n="";for(let s of e)n+=`<|im_start|>${s.role}
|
|
18
|
+
`);if(s===-1)try{return JSON.parse(n)}catch{s=t.byteLength}let i=e.decode(new Uint8Array(t,0,s));return JSON.parse(i)}}catch{}return{name:"unknown",version:"1.0.0",layers:[],inputs:[{name:"input",shape:[-1,768],dtype:"float32"}],outputs:[{name:"output",shape:[-1,768],dtype:"float32"}]}}async loadWeights(t,e){}unloadModel(t){let e=this.models.get(t);if(e&&this.module)for(let n of e.weights.values())this.module.exports.free(n.ptr);this.models.delete(t)}ensureInitialized(){if(!this.initialized||!this.module)throw new x("WASM runtime is not initialized",M.RUNTIME_NOT_INITIALIZED)}hasSIMDSupport(){return this.simdSupported}dispose(){for(let t of this.models.keys())this.unloadModel(t);this.module=null,this.initialized=!1}};function Sn(){return new Le}B();C();var tt=null,Ce=null,Lt=null;function vn(o){Ce=o}function En(o){Lt=o}async function Ne(){if(Ce)return Ce;if(tt)return tt;try{return tt=await import("onnxruntime-web/wasm"),tt}catch{return null}}async function Nt(){return await Ne()!=null}var Me=new Map,Oe=class{constructor(){f(this,"name","wasm");f(this,"initialized",!1);f(this,"executionProvider","wasm")}get capabilities(){return{concurrency:!0,quantization:!0,float16:this.executionProvider==="webgpu",dynamicShapes:!0,maxBatchSize:32,availableMemory:512*1024*1024}}async isAvailable(){return Nt()}async initialize(){if(this.initialized)return;let t=await Ne();if(!t)throw new x('onnxruntime-web could not be loaded.\n - If it is not installed: `npm install onnxruntime-web`.\n - If it IS installed (e.g. you see this inside a Web Worker, or with a bundler that drops the internal dynamic import): import it yourself and inject it before use \u2014 `import * as ort from "onnxruntime-web/wasm"; setOnnxModule(ort);`',M.RUNTIME_NOT_AVAILABLE);let e=t.env?.wasm;e&&(Lt?e.wasmPaths={...Lt}:!Ce&&e.wasmPaths===void 0&&(e.wasmPaths="/ort/"),!Ce&&e.numThreads===void 0&&(e.numThreads=1)),this.initialized=!0}async loadModel(t,e={}){this.initialized||await this.initialize();try{let n=await Ne();if(!n)throw new Error("onnxruntime-web is not installed");let s={executionProviders:["wasm"],graphOptimizationLevel:"all"},i=new Uint8Array(t),r=await n.InferenceSession.create(i,s),a=r.inputNames,c=r.outputNames,l=`onnx_${Date.now().toString(36)}_${Math.random().toString(36).slice(2,8)}`;Me.set(l,{session:r,inputNames:[...a],outputNames:[...c]});let d={name:e.metadata?.name??"onnx-model",version:"1.0.0",inputs:a.map(h=>({name:h,dtype:"float32",shape:[-1]})),outputs:c.map(h=>({name:h,dtype:"float32",shape:[-1]})),sizeBytes:t.byteLength,quantization:e.quantization??"float32",format:"onnx"},u=new H(d,"wasm",()=>this.unloadModel(l));return Object.defineProperty(u,"id",{value:l,writable:!1}),q().trackModel(u,()=>u.dispose()),u}catch(n){throw new x(`Failed to load ONNX model: ${n instanceof Error?n.message:String(n)}`,M.MODEL_LOAD_FAILED,{error:n})}}async run(t,e){let n=Me.get(t.id);if(!n)throw new x(`ONNX session not found for model ${t.id}`,M.MODEL_NOT_LOADED,{modelId:t.id});let{session:s,inputNames:i,outputNames:r}=n;try{let a=await Ne(),c={};for(let u=0;u<Math.min(e.length,i.length);u++){let h=i[u],p=e[u];if(h&&p){let m=p.dtype,y;if(m==="int64"){let g=p.data;y=new a.Tensor("int64",g,p.shape)}else if(m==="int32"){let g=p.data;y=new a.Tensor("int32",g,p.shape)}else{let g=p.toFloat32Array();y=new a.Tensor("float32",g,p.shape)}c[h]=y}}let l=await s.run(c),d=[];for(let u of r){let h=l[u];if(h){let p=h.data,m=Array.from(h.dims).map(y=>Number(y));d.push(new w(new Float32Array(p),m,"float32"))}}return d}catch(a){throw new x(`ONNX inference failed: ${a instanceof Error?a.message:String(a)}`,M.INFERENCE_FAILED,{modelId:t.id,error:a})}}async runNamed(t,e){let n=Me.get(t.id);if(!n)throw new x(`ONNX session not found for model ${t.id}`,M.MODEL_NOT_LOADED,{modelId:t.id});let{session:s,inputNames:i,outputNames:r}=n;try{let a=await Ne(),c={};for(let[u,h]of e){let p=h,m=p.dtype,y;if(m==="int64"){let g=p.data;y=new a.Tensor("int64",g,p.shape)}else if(m==="int32"){let g=p.data;y=new a.Tensor("int32",g,p.shape)}else{let g=p.toFloat32Array();y=new a.Tensor("float32",g,p.shape)}c[u]=y}let l=await s.run(c),d=[];for(let u of r){let h=l[u];if(h){let p=h.data,m=Array.from(h.dims).map(y=>Number(y));d.push(new w(new Float32Array(p),m,"float32"))}}return d}catch(a){throw new x(`ONNX inference failed: ${a instanceof Error?a.message:String(a)}`,M.INFERENCE_FAILED,{modelId:t.id,expectedInputs:i,providedInputs:Array.from(e.keys()),error:a})}}async unloadModel(t){Me.get(t)&&Me.delete(t)}dispose(){Me.clear(),this.initialized=!1}};function nt(){return new Oe}B();C();var ue=new Map,K=null,Re=class{constructor(){f(this,"name","wasm")}get capabilities(){return{concurrency:!0,quantization:!0,float16:!0,dynamicShapes:!0,maxBatchSize:128,availableMemory:1024*1024*1024}}async isAvailable(){return K?.pipelineFactory!=null}async initialize(){if(!K?.pipelineFactory)throw new x("TransformersAdapterRuntime requires a pipelineFactory. Call useTransformersBackend({ pipelineFactory }) first.",M.RUNTIME_INIT_FAILED)}async loadModel(t,e={}){let s={name:e.metadata?.name??"default",version:"1.0.0",inputs:[{name:"input",dtype:"float32",shape:[-1]}],outputs:[{name:"output",dtype:"float32",shape:[-1]}],sizeBytes:t.byteLength||0,quantization:e.quantization??"float32",format:"onnx"},i=`tjs_${Date.now().toString(36)}_${Math.random().toString(36).slice(2,6)}`,r=new H(s,this.name,()=>{let a=ue.get(i);a?.instance.dispose&&a.instance.dispose(),ue.delete(i)});return q().trackModel(r,()=>r.dispose()),r}async loadPipeline(t,e,n){if(!K?.pipelineFactory)throw new x("Adapter not initialised",M.RUNTIME_NOT_INITIALIZED);let s={...n};K.device&&(s.device=K.device),K.dtype&&(s.dtype=K.dtype);let i=await K.pipelineFactory(t,e,s),r=`tjs_${Date.now().toString(36)}_${Math.random().toString(36).slice(2,6)}`;return ue.set(r,{instance:i,task:t,model:e}),r}async run(t,e){let n=ue.get(t.id);if(!n)throw new x(`No transformers.js session for model ${t.id}`,M.MODEL_NOT_LOADED);let s=e[0]?.toFloat32Array()??new Float32Array(0),i=await n.instance(s),r=Array.isArray(i)?new Float32Array(i.flat(1/0)):new Float32Array([0]);return[new w(r,[r.length],"float32")]}async runDirect(t,e,n){let s=ue.get(t);if(!s)throw new x(`No transformers.js session for model ${t}`,M.MODEL_NOT_LOADED);return s.instance(e,n)}dispose(){for(let[t,e]of ue)e.instance.dispose&&e.instance.dispose(),ue.delete(t)}},Ct=null;function Dn(o){K=o,Ct=new Re,de("wasm",()=>Ct)}function Fn(){return Ct}function st(){de("wasm",nt)}st();var Te=class{constructor(t={}){f(this,"options");f(this,"cache",new Map);f(this,"currentSize",0);f(this,"hits",0);f(this,"misses",0);this.options={strategy:t.strategy??"lru",maxSize:t.maxSize??100*1024*1024,maxEntries:t.maxEntries??1e3,ttl:t.ttl??0,persistent:t.persistent??!1,name:t.name??"edgeflow-cache"},this.options.persistent&&this.loadFromStorage()}get(t){let e=this.cache.get(t);if(!e){this.misses++;return}if(e.ttl&&Date.now()-e.createdAt>e.ttl){this.delete(t),this.misses++;return}return e.accessedAt=Date.now(),e.accessCount++,this.hits++,e.value}set(t,e,n,s){for(this.cache.has(t)&&this.delete(t);(this.currentSize+n>this.options.maxSize||this.cache.size>=this.options.maxEntries)&&this.cache.size>0;)this.evict();let i=s!==void 0?s:this.options.ttl>0?this.options.ttl:void 0,r={value:e,size:n,createdAt:Date.now(),accessedAt:Date.now(),accessCount:1,ttl:i};this.cache.set(t,r),this.currentSize+=n,this.options.persistent&&this.saveToStorage()}has(t){let e=this.cache.get(t);return e?e.ttl&&Date.now()-e.createdAt>e.ttl?(this.delete(t),!1):!0:!1}delete(t){let e=this.cache.get(t);return e?(this.currentSize-=e.size,this.cache.delete(t),this.options.persistent&&this.saveToStorage(),!0):!1}clear(){this.cache.clear(),this.currentSize=0,this.hits=0,this.misses=0,this.options.persistent&&this.clearStorage()}getStats(){let t=this.hits+this.misses;return{entries:this.cache.size,size:this.currentSize,hits:this.hits,misses:this.misses,hitRate:t>0?this.hits/t:0}}evict(){let t=null;switch(this.options.strategy){case"lru":t=this.findLRU();break;case"lfu":t=this.findLFU();break;case"fifo":t=this.findOldest();break;case"ttl":t=this.findExpired()??this.findOldest();break}t&&this.delete(t)}findLRU(){let t=null,e=1/0;for(let[n,s]of this.cache)s.accessedAt<e&&(e=s.accessedAt,t=n);return t}findLFU(){let t=null,e=1/0;for(let[n,s]of this.cache)s.accessCount<e&&(e=s.accessCount,t=n);return t}findOldest(){let t=null,e=1/0;for(let[n,s]of this.cache)s.createdAt<e&&(e=s.createdAt,t=n);return t}findExpired(){let t=Date.now();for(let[e,n]of this.cache)if(n.ttl&&t-n.createdAt>n.ttl)return e;return null}async loadFromStorage(){if(!(typeof indexedDB>"u"))try{let s=(await this.openDB()).transaction("cache","readonly").objectStore("cache").getAll();return new Promise((i,r)=>{s.onsuccess=()=>{let a=s.result;for(let{key:c,entry:l}of a)this.cache.set(c,l),this.currentSize+=l.size;i()},s.onerror=()=>r(s.error)})}catch{}}async saveToStorage(){if(!(typeof indexedDB>"u"))try{let e=(await this.openDB()).transaction("cache","readwrite"),n=e.objectStore("cache");n.clear();for(let[s,i]of this.cache)n.put({key:s,entry:i});return new Promise((s,i)=>{e.oncomplete=()=>s(),e.onerror=()=>i(e.error)})}catch{}}async clearStorage(){if(!(typeof indexedDB>"u"))try{(await this.openDB()).transaction("cache","readwrite").objectStore("cache").clear()}catch{}}openDB(){return new Promise((t,e)=>{let n=indexedDB.open(this.options.name,1);n.onupgradeneeded=()=>{let s=n.result;s.objectStoreNames.contains("cache")||s.createObjectStore("cache",{keyPath:"key"})},n.onsuccess=()=>t(n.result),n.onerror=()=>e(n.error)})}},rt=class extends Te{generateKey(t,e){let n=Array.isArray(e)?e:Array.from(e),s=this.hashArray(n);return`${t}:${s}`}hashArray(t){let e=0,n=t.length>100?t.filter((s,i)=>i%Math.floor(t.length/100)===0):t;for(let s=0;s<n.length;s++){let i=n[s]??0;e=(e<<5)-e+(i*1e3|0),e|=0}return e.toString(36)}},ee=class{constructor(t="edgeflow-models"){f(this,"cacheName");f(this,"cache",null);this.cacheName=t}async ensureCache(){if(!this.cache){if(typeof caches>"u")throw new Error("Cache API is not available");this.cache=await caches.open(this.cacheName)}return this.cache}async get(t){try{return await(await this.ensureCache()).match(t)??void 0}catch{return}}async put(t,e){try{await(await this.ensureCache()).put(t,e.clone())}catch{}}async delete(t){try{return await(await this.ensureCache()).delete(t)}catch{return!1}}async clear(){try{await caches.delete(this.cacheName),this.cache=null}catch{}}async keys(){try{return(await(await this.ensureCache()).keys()).map(n=>n.url)}catch{return[]}}};function Pn(o="medium",t={}){let e={small:{maxSize:10485760,maxEntries:100},medium:{maxSize:104857600,maxEntries:500},large:{maxSize:524288e3,maxEntries:2e3},custom:{}};return new Te({...e[o],...t})}var E=class{constructor(t){f(this,"model",null);f(this,"config");f(this,"modelCache");f(this,"downloadCache");f(this,"isReady",!1);this.config=t,this.modelCache=new ze,this.downloadCache=new ee}async initialize(){if(this.isReady&&this.model)return;if(this.config.model==="default"){this.isReady=!0;return}let t=this.modelCache.get(this.config.model);if(t){this.model=t,this.isReady=!0;return}this.model=await this.loadModelWithCache(this.config.model),this.isReady=!0}async loadModelWithCache(t){let e=await this.downloadCache.get(t);try{let n=await fetch(t);n.ok&&await this.downloadCache.put(t,n.clone())}catch{}return we(t,{runtime:this.config.runtime,quantization:this.config.quantization,cache:this.config.cache})}async run(t,e){await this.initialize();let n=performance.now(),s=await this.preprocess(t),i=await X(this.model,s),r=await this.postprocess(i,e);return r&&typeof r=="object"&&"processingTime"in r&&(r.processingTime=performance.now()-n),r}async runBatch(t,e){return await this.initialize(),await Promise.all(t.map(s=>this.run(s,e)))}get task(){return this.config.task}get ready(){return this.isReady}dispose(){this.model&&(this.model.dispose(),this.model=null),this.isReady=!1}},_n=new Map;function N(o,t){_n.set(o,t)}function Ln(o){return _n.get(o)}var ot=["negative","positive"],Nn=["anger","disgust","fear","joy","sadness","surprise","neutral"],it=["tench","goldfish","great white shark","tiger shark","hammerhead","electric ray","stingray","cock","hen","ostrich"];C();B();var P=class o{constructor(){f(this,"vocab",new Map);f(this,"reverseVocab",new Map);f(this,"merges",new Map);f(this,"addedTokens",new Map);f(this,"specialTokens",new Set);f(this,"modelType","BPE");f(this,"unkToken","[UNK]");f(this,"continuingSubwordPrefix","##");f(this,"padTokenId",0);f(this,"unkTokenId",0);f(this,"clsTokenId");f(this,"sepTokenId");f(this,"maskTokenId");f(this,"bosTokenId");f(this,"eosTokenId");f(this,"maxLength",512);f(this,"doLowerCase",!1);f(this,"stripAccents",!1);f(this,"postProcessor");f(this,"byteEncoder",new Map);f(this,"byteDecoder",new Map);this.initByteEncoder()}initByteEncoder(){let t=[];for(let s=33;s<=126;s++)t.push(s);for(let s=161;s<=172;s++)t.push(s);for(let s=174;s<=255;s++)t.push(s);let e=[...t],n=0;for(let s=0;s<256;s++)t.includes(s)||(t.push(s),e.push(256+n),n++);for(let s=0;s<t.length;s++){let i=t[s],r=String.fromCharCode(e[s]);this.byteEncoder.set(i,r),this.byteDecoder.set(r,i)}}static async fromJSON(t){let e=new o,n=typeof t=="string"?JSON.parse(t):t;if(n.model){if(e.modelType=n.model.type,n.model.vocab)if(Array.isArray(n.model.vocab)){let s=n.model.vocab;for(let i=0;i<s.length;i++){let r=s[i],a=Array.isArray(r)?r[0]:r;e.vocab.set(a,i),e.reverseVocab.set(i,a)}}else for(let[s,i]of Object.entries(n.model.vocab))e.vocab.set(s,i),e.reverseVocab.set(i,s);if(n.model.merges)for(let s=0;s<n.model.merges.length;s++)e.merges.set(n.model.merges[s],s);e.unkToken=n.model.unk_token??"[UNK]",e.continuingSubwordPrefix=n.model.continuing_subword_prefix??"##"}if(n.added_tokens)for(let s of n.added_tokens){e.addedTokens.set(s.content,s.id),e.reverseVocab.set(s.id,s.content),s.special&&e.specialTokens.add(s.content);let i=s.content.toLowerCase();i.includes("pad")&&(e.padTokenId=s.id),i.includes("unk")&&(e.unkTokenId=s.id),(i.includes("cls")||i==="[cls]")&&(e.clsTokenId=s.id),(i.includes("sep")||i==="[sep]")&&(e.sepTokenId=s.id),i.includes("mask")&&(e.maskTokenId=s.id),(i.includes("bos")||i==="<s>")&&(e.bosTokenId=s.id),(i.includes("eos")||i==="</s>")&&(e.eosTokenId=s.id)}return n.normalizer&&(e.doLowerCase=n.normalizer.lowercase??!1,e.stripAccents=n.normalizer.strip_accents??!1),n.truncation&&(e.maxLength=n.truncation.max_length),n.post_processor&&(e.postProcessor=n.post_processor),e}static async fromUrl(t){let e=await fetch(t);if(!e.ok)throw new x(`Failed to load tokenizer from ${t}: ${e.status}`,M.MODEL_NOT_FOUND);let n=await e.json();return o.fromJSON(n)}static async fromHuggingFace(t,e){let n=e?.revision??"main",s=`https://huggingface.co/${t}/resolve/${n}/tokenizer.json`;return o.fromUrl(s)}normalize(t){let e=t;return this.doLowerCase&&(e=e.toLowerCase()),this.stripAccents&&(e=e.normalize("NFD").replace(/[\u0300-\u036f]/g,"")),e=e.replace(/\s+/g," ").trim(),e}preTokenize(t){let e=/'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+/gu;return t.match(e)??[t]}textToBytes(t){let n=new TextEncoder().encode(t);return Array.from(n).map(s=>this.byteEncoder.get(s)??"").join("")}bytesToText(t){let e=new Uint8Array(t.split("").map(s=>this.byteDecoder.get(s)??0));return new TextDecoder("utf-8",{fatal:!1}).decode(e)}getPairs(t){let e=new Set;for(let n=0;n<t.length-1;n++)e.add(`${t[n]} ${t[n+1]}`);return e}bpe(t){if(this.vocab.has(t))return[t];let e=t.split(""),n=this.getPairs(e);if(n.size===0)return[t];for(;;){let s=null,i=1/0;for(let u of n){let h=this.merges.get(u);h!==void 0&&h<i&&(i=h,s=u)}if(s===null)break;let r=s.split(" "),a=r[0],c=r[1];if(!a||!c)break;let l=[],d=0;for(;d<e.length;){let u=e.indexOf(a,d);if(u===-1){l.push(...e.slice(d));break}l.push(...e.slice(d,u)),e[u]===a&&u<e.length-1&&e[u+1]===c?(l.push(a+c),d=u+2):(l.push(e[u]),d=u+1)}if(e=l,e.length===1)break;n=this.getPairs(e)}return e}wordPiece(t){if(this.vocab.has(t))return[t];let e=[],n=0;for(;n<t.length;){let s=t.length,i=null;for(;n<s;){let r=t.slice(n,s);if(n>0&&(r=this.continuingSubwordPrefix+r),this.vocab.has(r)){i=r;break}s--}i===null?(e.push(this.unkToken),n++):(e.push(i),n=s)}return e}tokenizeWord(t){if(this.addedTokens.has(t))return[t];switch(this.modelType){case"BPE":{let e=this.textToBytes(t);return this.bpe(e)}case"WordPiece":return this.wordPiece(t);case"Unigram":return this.unigramTokenize(t);default:return this.vocab.has(t)?[t]:[this.unkToken]}}unigramTokenize(t){let e="\u2581"+t,n=[],s=0,i=e;for(;s<i.length;){let r=i.length,a=!1;for(;r>s;){let c=i.slice(s,r);if(this.vocab.has(c)){n.push(c),s=r,a=!0;break}r--}if(!a){let c=i[s];n.push(this.vocab.has(c)?c:this.unkToken),s++}}return n.length>0?n:[this.unkToken]}tokenize(t){let e=this.normalize(t),n=[],s=e,i=Array.from(this.addedTokens.keys()).sort((r,a)=>a.length-r.length);for(let r of i)if(s.includes(r)){let a=s.split(r),c=[];for(let l=0;l<a.length;l++)a[l]&&c.push(a[l]),l<a.length-1&&n.push(r);s=c.join(" ")}if(s.trim()){let r=this.preTokenize(s);for(let a of r){if(!a)continue;let c=this.tokenizeWord(a);n.push(...c)}}return n}convertTokensToIds(t){return t.map(e=>{let n=this.addedTokens.get(e);if(n!==void 0)return n;let s=this.vocab.get(e);return s!==void 0?s:this.unkTokenId})}convertIdsToTokens(t){return t.map(e=>this.reverseVocab.get(e)??this.unkToken)}postProcess(t,e){if(!this.postProcessor){let r=[],a=[];return this.clsTokenId!==void 0&&(r.push(this.clsTokenId),a.push(0)),r.push(...t),a.push(...t.map(()=>0)),this.sepTokenId!==void 0&&(r.push(this.sepTokenId),a.push(0)),e&&(r.push(...e),a.push(...e.map(()=>1)),this.sepTokenId!==void 0&&(r.push(this.sepTokenId),a.push(1))),{ids:r,typeIds:a}}let n=e?this.postProcessor.pair:this.postProcessor.single;if(!n)return{ids:t,typeIds:t.map(()=>0)};let s=[],i=[];for(let r of n)if("SpecialToken"in r){let a=this.postProcessor.special_tokens?.[r.SpecialToken.id];a&&(s.push(...a.ids),i.push(...a.ids.map(()=>r.SpecialToken.type_id)))}else if("Sequence"in r){let a=r.Sequence.id==="A"?t:e??[];s.push(...a),i.push(...a.map(()=>r.Sequence.type_id))}return{ids:s,typeIds:i}}encode(t,e={}){let{addSpecialTokens:n=!0,maxLength:s=this.maxLength,padding:i="max_length",truncation:r=!0,returnAttentionMask:a=!0,returnTokenTypeIds:c=!1,textPair:l}=e,d=this.tokenize(t),u=this.convertTokensToIds(d),h;if(l){let g=this.tokenize(l);h=this.convertTokensToIds(g)}let p;if(n){let g=this.postProcess(u,h);u=g.ids,c&&(p=g.typeIds)}else h&&(u=[...u,...h],c&&(p=[...u.map(()=>0),...h.map(()=>1)]));r&&u.length>s&&(u=u.slice(0,s),p&&(p=p.slice(0,s)));let m=[];if(a&&(m=u.map(()=>1)),i==="max_length"&&u.length<s){let g=s-u.length;u=[...u,...new Array(g).fill(this.padTokenId)],a&&(m=[...m,...new Array(g).fill(0)]),p&&(p=[...p,...new Array(g).fill(0)])}let y={inputIds:u,attentionMask:m};return c&&p&&(y.tokenTypeIds=p),y}encodeBatch(t,e={}){if(e.padding==="longest"){let n=t.map(i=>this.encode(i,{...e,padding:"do_not_pad"})),s=Math.max(...n.map(i=>i.inputIds.length));return t.map(i=>this.encode(i,{...e,maxLength:s,padding:"max_length"}))}return t.map(n=>this.encode(n,e))}decode(t,e=!0){let n=this.convertIdsToTokens(t);if(e&&(n=n.filter(s=>!this.specialTokens.has(s))),this.modelType==="BPE")return this.bytesToText(n.join("")).replace(/\s+/g," ").trim();if(this.modelType==="WordPiece"){let s=this.continuingSubwordPrefix,i=[];for(let r of n)r.startsWith(s)?i.length>0?i[i.length-1]+=r.slice(s.length):i.push(r.slice(s.length)):i.push(r);return i.join(" ").replace(/\s+/g," ").trim()}return this.modelType==="Unigram"?n.join("").replace(/\u2581/g," ").replace(/\s+/g," ").trim():n.join(" ").replace(/\s+/g," ").trim()}decodeBatch(t,e=!0){return t.map(n=>this.decode(n,e))}get vocabSize(){return this.vocab.size+this.addedTokens.size}getSpecialTokenIds(){return{padTokenId:this.padTokenId,unkTokenId:this.unkTokenId,clsTokenId:this.clsTokenId,sepTokenId:this.sepTokenId,maskTokenId:this.maskTokenId,bosTokenId:this.bosTokenId,eosTokenId:this.eosTokenId}}getConfig(){return{vocabSize:this.vocabSize,maxLength:this.maxLength,padTokenId:this.padTokenId,unkTokenId:this.unkTokenId,clsTokenId:this.clsTokenId,sepTokenId:this.sepTokenId,maskTokenId:this.maskTokenId,bosTokenId:this.bosTokenId,eosTokenId:this.eosTokenId}}isSpecialToken(t){return this.specialTokens.has(t)}getTokenId(t){return this.addedTokens.get(t)??this.vocab.get(t)}getToken(t){return this.reverseVocab.get(t)}};function Cn(){return new P}async function On(o){return P.fromUrl(o)}async function Rn(o,t){return P.fromHuggingFace(o,t)}G();var $n={model:"https://huggingface.co/Xenova/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/onnx/model_quantized.onnx",tokenizer:"https://huggingface.co/Xenova/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/tokenizer.json"},ar=["NEGATIVE","POSITIVE"],Y=class extends E{constructor(e,n){super(e);f(this,"tokenizer",null);f(this,"onnxModel",null);f(this,"labels");f(this,"modelUrl");f(this,"tokenizerUrl");this.labels=n??ar,this.modelUrl=e.model!=="default"?e.model:$n.model,this.tokenizerUrl=$n.tokenizer}async initialize(){if(await super.initialize(),this.tokenizer||(this.tokenizer=await P.fromUrl(this.tokenizerUrl)),!this.onnxModel){let e=await v(this.modelUrl,{cache:this.config.cache??!0});this.onnxModel=await F(e)}}setLabels(e){this.labels=e}async run(e,n){let s=Array.isArray(e),i=s?e:[e];await this.initialize();let r=performance.now(),a=[];for(let l of i){let d=await this.preprocess(l),u=await this.runInference(d),h=await this.postprocess(u,n);a.push(h)}let c=performance.now()-r;for(let l of a)l.processingTime=c/a.length;return s?a:a[0]}async preprocess(e){let n=Array.isArray(e)?e[0]:e,s=this.tokenizer.encode(n,{maxLength:128,padding:"max_length",truncation:!0}),i=new w(BigInt64Array.from(s.inputIds.map(a=>BigInt(a))),[1,s.inputIds.length],"int64"),r=new w(BigInt64Array.from(s.attentionMask.map(a=>BigInt(a))),[1,s.attentionMask.length],"int64");return[i,r]}async runInference(e){let n=new Map;return n.set("input_ids",e[0]),n.set("attention_mask",e[1]),await U(this.onnxModel,n)}async postprocess(e,n){let s=e[0];if(!s)return{label:"unknown",score:0};let r=O(s,-1).toFloat32Array(),a=0,c=r[0]??0;for(let d=1;d<r.length;d++)(r[d]??0)>c&&(c=r[d]??0,a=d);return{label:n?.labels?.[a]??this.labels[a]??`class_${a}`,score:c}}},te=class extends Y{constructor(t){super(t,ot)}async analyze(t,e){return this.run(t,e)}};function Un(o={}){return new Y({task:"text-classification",model:o.model??"default",runtime:o.runtime,cache:o.cache??!0,quantization:o.quantization})}function Bn(o={}){return new te({task:"sentiment-analysis",model:o.model??"default",runtime:o.runtime,cache:o.cache??!0,quantization:o.quantization})}N("text-classification",o=>new Y(o));N("sentiment-analysis",o=>new te(o));C();G();var qn={model:"https://huggingface.co/Xenova/all-MiniLM-L6-v2/resolve/main/onnx/model_quantized.onnx",tokenizer:"https://huggingface.co/Xenova/all-MiniLM-L6-v2/resolve/main/tokenizer.json"},cr=384,ne=class extends E{constructor(e,n=cr){super(e);f(this,"tokenizer",null);f(this,"onnxModel",null);f(this,"embeddingDim");f(this,"modelUrl");f(this,"tokenizerUrl");f(this,"modelInputNames",new Set);this.embeddingDim=n,this.modelUrl=e.model!=="default"?e.model:qn.model,this.tokenizerUrl=e.tokenizerUrl??qn.tokenizer}async initialize(){if(await super.initialize(),this.tokenizer||(this.tokenizer=await P.fromUrl(this.tokenizerUrl)),!this.onnxModel){let e=await v(this.modelUrl,{cache:this.config.cache??!0});this.onnxModel=await F(e),this.modelInputNames=new Set(this.onnxModel.metadata.inputs.map(n=>n.name))}}async run(e,n){let s=Array.isArray(e),i=s?e:[e];await this.initialize();let r=performance.now(),a=[];for(let l of i){let d=await this.preprocess(l),u=await this.runInference(d),h=await this.postprocess(u,n);a.push(h)}let c=performance.now()-r;for(let l of a)l.processingTime=c/a.length;return s?a:a[0]}async preprocess(e){let n=Array.isArray(e)?e[0]:e,s=this.tokenizer.encode(n,{maxLength:128,padding:"max_length",truncation:!0}),i=new w(BigInt64Array.from(s.inputIds.map(c=>BigInt(c))),[1,s.inputIds.length],"int64"),r=new w(BigInt64Array.from(s.attentionMask.map(c=>BigInt(c))),[1,s.attentionMask.length],"int64"),a=[i,r];if(this.modelInputNames.has("token_type_ids")){let c=new w(BigInt64Array.from(s.inputIds.map(()=>BigInt(0))),[1,s.inputIds.length],"int64");a.push(c)}return a}async runInference(e){let n=new Map;return n.set("input_ids",e[0]),n.set("attention_mask",e[1]),this.modelInputNames.has("token_type_ids")&&e[2]&&n.set("token_type_ids",e[2]),await U(this.onnxModel,n)}async postprocess(e,n){let s=e[0];if(!s)return{embeddings:[]};let i=n?.pooling??"mean",r=n?.normalize??!0,a;switch(i){case"cls":a=this.extractCLSEmbedding(s);break;case"max":a=this.maxPooling(s);break;case"none":a=s.toArray();break;case"mean":default:a=this.meanPooling(s);break}return r&&(a=this.normalizeVector(a)),n?.outputDim&&n.outputDim<a.length&&(a=a.slice(0,n.outputDim)),{embeddings:a}}extractCLSEmbedding(e){let n=e.toFloat32Array(),s=e.shape[2]??this.embeddingDim;return Array.from(n.slice(0,s))}meanPooling(e){let n=e.toFloat32Array(),s=e.shape[1]??1,i=e.shape[2]??this.embeddingDim,r=new Float32Array(i);for(let a=0;a<s;a++)for(let c=0;c<i;c++)r[c]=(r[c]??0)+(n[a*i+c]??0)/s;return Array.from(r)}maxPooling(e){let n=e.toFloat32Array(),s=e.shape[1]??1,i=e.shape[2]??this.embeddingDim,r=new Array(i).fill(-1/0);for(let a=0;a<s;a++)for(let c=0;c<i;c++){let l=n[a*i+c]??0;l>(r[c]??-1/0)&&(r[c]=l)}return r}normalizeVector(e){let n=0;for(let s of e)n+=s*s;return n=Math.sqrt(n),n===0?e:e.map(s=>s/n)}};function jn(o={}){return new ne({task:"feature-extraction",model:o.model??"default",runtime:o.runtime,cache:o.cache??!0,quantization:o.quantization,tokenizerUrl:o.tokenizerUrl})}N("feature-extraction",o=>new ne(o));C();C();var Ot={width:224,height:224,resizeMode:"cover",mean:[.485,.456,.406],std:[.229,.224,.225],rescaleFactor:1/255,grayscale:!1,channelFormat:"CHW",dtype:"float32",doResize:!0,doRescale:!0,doNormalize:!0,doCenterCrop:!1,paddingColor:[0,0,0]},he=class o{constructor(t={}){f(this,"options");f(this,"canvas",null);f(this,"ctx",null);let e=t.size,n=t.width??e??Ot.width,s=t.height??e??Ot.height;this.options={...Ot,...t,width:n,height:s,size:e??n,cropSize:t.cropSize??t.size??n}}static fromConfig(t){let e={},n=t.size;if(n!==void 0){if(typeof n=="number")e.size=n;else if(typeof n=="object"&&n!==null){let h=n;e.width=h.width??h.shortest_edge,e.height=h.height??h.shortest_edge}}let s=t.crop_size;if(s!==void 0){if(typeof s=="number")e.cropSize=s;else if(typeof s=="object"&&s!==null){let h=s;e.cropSize={width:h.width??224,height:h.height??224}}}let i=t.image_mean;Array.isArray(i)&&(e.mean=i);let r=t.image_std;Array.isArray(r)&&(e.std=r);let a=t.rescale_factor;typeof a=="number"&&(e.rescaleFactor=a);let c=t.do_resize;typeof c=="boolean"&&(e.doResize=c);let l=t.do_rescale;typeof l=="boolean"&&(e.doRescale=l);let d=t.do_normalize;typeof d=="boolean"&&(e.doNormalize=d);let u=t.do_center_crop;return typeof u=="boolean"&&(e.doCenterCrop=u),t.resample!==void 0&&(e.resizeMode="cover"),new o(e)}static async fromUrl(t){let e=await fetch(t);if(!e.ok)throw new Error(`Failed to load preprocessor config from ${t}`);let n=await e.json();return o.fromConfig(n)}static async fromHuggingFace(t,e){let n=e?.revision??"main",s=`https://huggingface.co/${t}/resolve/${n}/preprocessor_config.json`;return o.fromUrl(s)}ensureCanvas(){if(!this.canvas)if(typeof document<"u")this.canvas=document.createElement("canvas"),this.ctx=this.canvas.getContext("2d");else throw new Error("ImagePreprocessor requires a browser environment")}async process(t){let e;typeof t=="string"?e=await this.loadFromUrl(t):t instanceof Blob||t instanceof File?e=await this.loadFromBlob(t):t instanceof ImageData?e=t:e=this.toImageData(t);let n=e;return this.options.doResize&&(n=this.resize(n)),this.options.doCenterCrop&&(n=this.centerCrop(n)),this.toTensor(n)}async processBatch(t){let e=await Promise.all(t.map(l=>this.process(l))),n=e.length,s=e[0];if(!s)return new w(new Float32Array(0),[0],"float32");let i=s.shape[0]??3,r=s.shape[1]??this.options.height,a=s.shape[2]??this.options.width,c=new Float32Array(n*i*r*a);for(let l=0;l<e.length;l++){let d=e[l];d&&c.set(d.toFloat32Array(),l*i*r*a)}return new w(c,[n,i,r,a],"float32")}async loadFromUrl(t){return new Promise((e,n)=>{let s=new Image;s.crossOrigin="anonymous",s.onload=()=>{e(this.toImageData(s))},s.onerror=()=>{n(new Error(`Failed to load image from ${t}`))},s.src=t})}async loadFromBlob(t){let e=URL.createObjectURL(t);try{return await this.loadFromUrl(e)}finally{URL.revokeObjectURL(e)}}centerCrop(t){let e=this.options.cropSize,n,s;typeof e=="number"?(n=e,s=e):(n=e.width,s=e.height);let i=Math.max(0,Math.floor((t.width-n)/2)),r=Math.max(0,Math.floor((t.height-s)/2));this.ensureCanvas();let a=document.createElement("canvas");return a.width=t.width,a.height=t.height,a.getContext("2d").putImageData(t,0,0),this.canvas.width=n,this.canvas.height=s,this.ctx.drawImage(a,i,r,n,s,0,0,n,s),this.ctx.getImageData(0,0,n,s)}toImageData(t){this.ensureCanvas();let{width:e,height:n}=t;return this.canvas.width=e,this.canvas.height=n,this.ctx.drawImage(t,0,0),this.ctx.getImageData(0,0,e,n)}resize(t){let{width:e,height:n,resizeMode:s}=this.options;this.ensureCanvas();let i=0,r=0,a=t.width,c=t.height,l=0,d=0,u=e,h=n;if(s==="contain"){let y=Math.min(e/t.width,n/t.height);u=Math.round(t.width*y),h=Math.round(t.height*y),l=Math.round((e-u)/2),d=Math.round((n-h)/2)}else if(s==="cover"){let y=Math.max(e/t.width,n/t.height);a=Math.round(e/y),c=Math.round(n/y),i=Math.round((t.width-a)/2),r=Math.round((t.height-c)/2)}let p=document.createElement("canvas");return p.width=t.width,p.height=t.height,p.getContext("2d").putImageData(t,0,0),this.canvas.width=e,this.canvas.height=n,(s==="contain"||s==="pad")&&(this.ctx.fillStyle="black",this.ctx.fillRect(0,0,e,n)),this.ctx.drawImage(p,i,r,a,c,l,d,u,h),this.ctx.getImageData(0,0,e,n)}toTensor(t){let{mean:e,std:n,grayscale:s,channelFormat:i,dtype:r,doRescale:a,rescaleFactor:c,doNormalize:l}=this.options,d=t.height,u=t.width,h=s?1:3,p=new Float32Array(h*d*u),m=t.data;for(let g=0;g<d;g++)for(let T=0;T<u;T++){let k=(g*u+T)*4;if(s){let b=.299*(m[k]??0)+.587*(m[k+1]??0)+.114*(m[k+2]??0);a&&(b*=c),l&&(b=(b-(e[0]??0))/(n[0]??1));let z=g*u+T;p[z]=b}else if(i==="CHW")for(let b=0;b<3;b++){let z=m[k+b]??0;a&&(z*=c),l&&(z=(z-(e[b]??0))/(n[b]??1));let I=b*d*u+g*u+T;p[I]=z}else for(let b=0;b<3;b++){let z=m[k+b]??0;a&&(z*=c),l&&(z=(z-(e[b]??0))/(n[b]??1));let I=g*u*3+T*3+b;p[I]=z}}let y=i==="CHW"?[h,d,u]:[d,u,h];return new w(p,y,r)}getOptions(){return{...this.options}}},lr={sampleRate:16e3,nMels:80,nFft:400,hopLength:160,normalize:!0,maxDuration:30},fe=class o{constructor(t={}){f(this,"options");f(this,"audioContext",null);this.options={...lr,...t}}static fromConfig(t){let e={},n=t.sampling_rate;typeof n=="number"&&(e.sampleRate=n);let s=t.feature_size;typeof s=="number"&&(e.nMels=s);let i=t.n_fft;typeof i=="number"&&(e.nFft=i);let r=t.hop_length;return typeof r=="number"&&(e.hopLength=r),new o(e)}static async fromHuggingFace(t,e){let n=e?.revision??"main",s=`https://huggingface.co/${t}/resolve/${n}/preprocessor_config.json`,i=await fetch(s);if(!i.ok)throw new Error(`Failed to load audio config from ${s}`);let r=await i.json();return o.fromConfig(r)}ensureAudioContext(){if(!this.audioContext)if(typeof AudioContext<"u")this.audioContext=new AudioContext({sampleRate:this.options.sampleRate});else throw new Error("AudioPreprocessor requires Web Audio API support")}async process(t){let e;typeof t=="string"?e=await this.loadFromUrl(t):t instanceof Blob||t instanceof File?e=await this.loadFromBlob(t):t instanceof AudioBuffer?e=this.audioBufferToFloat32(t):t instanceof Float32Array?e=t:e=await this.decodeAudioData(t),this.options.normalize&&(e=this.normalizeAudio(e));let n=this.options.maxDuration*this.options.sampleRate;return e.length>n&&(e=e.slice(0,n)),this.computeMelSpectrogram(e)}async processRaw(t){let e;typeof t=="string"?e=await this.loadFromUrl(t):t instanceof Blob||t instanceof File?e=await this.loadFromBlob(t):t instanceof AudioBuffer?e=this.audioBufferToFloat32(t):t instanceof Float32Array?e=t:e=await this.decodeAudioData(t),this.options.normalize&&(e=this.normalizeAudio(e));let n=this.options.maxDuration*this.options.sampleRate;return e.length>n&&(e=e.slice(0,n)),new w(e,[1,e.length],"float32")}async loadFromUrl(t){let e=await fetch(t);if(!e.ok)throw new Error(`Failed to load audio from ${t}`);let n=await e.arrayBuffer();return this.decodeAudioData(n)}async loadFromBlob(t){let e=await t.arrayBuffer();return this.decodeAudioData(e)}async decodeAudioData(t){this.ensureAudioContext();let e=await this.audioContext.decodeAudioData(t.slice(0));return this.audioBufferToFloat32(e)}audioBufferToFloat32(t){let e=t.getChannelData(0);return new Float32Array(e)}normalizeAudio(t){let e=0;for(let n=0;n<t.length;n++){let s=Math.abs(t[n]??0);s>e&&(e=s)}if(e>0){let n=new Float32Array(t.length);for(let s=0;s<t.length;s++)n[s]=(t[s]??0)/e;return n}return t}computeMelSpectrogram(t){let{nMels:e,nFft:n,hopLength:s}=this.options,i=Math.floor((t.length-n)/s)+1;if(i<=0)return new w(new Float32Array(e),[1,e],"float32");let r=new Float32Array(i*e);for(let a=0;a<i;a++){let c=a*s;for(let l=0;l<e;l++){let d=0,u=Math.floor(l/e*(n/2)),h=Math.floor((l+1)/e*(n/2));for(let p=u;p<Math.min(h,n);p++){let m=t[c+p]??0;d+=m*m}r[a*e+l]=Math.log(d+1e-10)}}return new w(r,[i,e],"float32")}dispose(){this.audioContext&&(this.audioContext.close(),this.audioContext=null)}};function Hn(o,t={}){let{lowercase:e=!0,removePunctuation:n=!1,normalizeWhitespace:s=!0,maxLength:i}=t,r=o;return e&&(r=r.toLowerCase()),n&&(r=r.replace(/[^\w\s]/g,"")),s&&(r=r.replace(/\s+/g," ").trim()),i&&r.length>i&&(r=r.slice(0,i)),r}function at(o="imagenet",t={}){let e={imagenet:{width:224,height:224,mean:[.485,.456,.406],std:[.229,.224,.225]},clip:{width:224,height:224,mean:[.48145466,.4578275,.40821073],std:[.26862954,.26130258,.27577711]},vit:{width:224,height:224,mean:[.5,.5,.5],std:[.5,.5,.5]},custom:{}};return new he({...e[o],...t})}function Wn(o="whisper",t={}){let e={whisper:{sampleRate:16e3,nMels:80,nFft:400,hopLength:160},wav2vec:{sampleRate:16e3,normalize:!0},custom:{}};return new fe({...e[o],...t})}G();var dr={model:"https://huggingface.co/Xenova/mobilevit-small/resolve/main/onnx/model_quantized.onnx"},se=class extends E{constructor(e,n,s=1e3){super(e);f(this,"preprocessor",null);f(this,"onnxModel",null);f(this,"labels");f(this,"modelUrl");this.labels=n??it,this.modelUrl=e.model!=="default"?e.model:dr.model}async initialize(){if(await super.initialize(),this.preprocessor||(this.preprocessor=at("imagenet")),!this.onnxModel){let e=await v(this.modelUrl,{cache:this.config.cache??!0});this.onnxModel=await F(e)}}setLabels(e){this.labels=e}async run(e,n){let s=Array.isArray(e),i=s?e:[e];await this.initialize();let r=performance.now(),a=[];for(let l of i){let d=await this.preprocess(l),u=await this.runModelInference(d),h=await this.postprocess(u,n);a.push(h)}let c=performance.now()-r;for(let l of a)l.processingTime=c/a.length;return s?a:a[0]}async preprocess(e){let n=Array.isArray(e)?e[0]:e,s=await this.preprocessor.process(n);return s.shape.length===3?[s.reshape([1,...s.shape])]:[s]}async runModelInference(e){return await X(this.onnxModel,e)}async postprocess(e,n){let s=e[0];if(!s)return{label:"unknown",score:0};let r=O(s,-1).toFloat32Array(),a=0,c=r[0]??0;for(let d=1;d<r.length;d++)(r[d]??0)>c&&(c=r[d]??0,a=d);return{label:n?.labels?.[a]??this.labels[a]??`class_${a}`,score:c}}};function Vn(o={},t){return new se({task:"image-classification",model:o.model??"default",runtime:o.runtime,cache:o.cache??!0,quantization:o.quantization},t)}N("image-classification",o=>new se(o));C();var Gn={model:"https://huggingface.co/Xenova/TinyLlama-1.1B-Chat-v1.0/resolve/main/onnx/model_q4f16.onnx",tokenizer:"https://huggingface.co/Xenova/TinyLlama-1.1B-Chat-v1.0/resolve/main/tokenizer.json"},pe=class extends E{constructor(e){super(e??{task:"text-generation",model:"default"});f(this,"tokenizer",null);f(this,"eosTokenId",50256);f(this,"llmModel",null);f(this,"modelsLoaded",!1);f(this,"modelUrl");f(this,"tokenizerUrl");f(this,"conversationHistory",[]);f(this,"chatTemplateType","chatml");this.modelUrl=Gn.model,this.tokenizerUrl=Gn.tokenizer}get isModelLoaded(){return this.modelsLoaded}setModelUrls(e,n){this.modelUrl=e,this.tokenizerUrl=n}async loadModel(e){if(this.modelsLoaded)return;e?.({stage:"tokenizer",loaded:0,total:100,progress:0});try{let s=await fetch(this.tokenizerUrl);if(!s.ok)throw new Error(`Failed to fetch tokenizer: ${s.status}`);let i=await s.json();this.tokenizer=await P.fromJSON(i);let r=this.tokenizer.getSpecialTokenIds();this.eosTokenId=r.eosTokenId??r.sepTokenId??2,e?.({stage:"tokenizer",loaded:100,total:100,progress:100})}catch(s){throw new Error(`Failed to load tokenizer: ${s}`)}e?.({stage:"model",loaded:0,total:100,progress:0});let n=await this.fetchModelWithProgress(this.modelUrl,(s,i)=>{e?.({stage:"model",loaded:s,total:i,progress:Math.round(s/i*100)})});this.llmModel=await F(n,{runtime:"wasm"}),this.model=this.llmModel,this.modelsLoaded=!0}async fetchModelWithProgress(e,n){let s=await fetch(e);if(!s.ok)throw new Error(`Failed to fetch model: ${s.status} ${s.statusText}`);let i=s.headers.get("content-length"),r=i?parseInt(i,10):0;if(!s.body){let h=await s.arrayBuffer();return n(h.byteLength,h.byteLength),h}let a=s.body.getReader(),c=[],l=0;for(;;){let{done:h,value:p}=await a.read();if(h)break;c.push(p),l+=p.length,n(l,r||l)}let d=new Uint8Array(l),u=0;for(let h of c)d.set(h,u),u+=h.length;return d.buffer}async initialize(){this.isReady||(this.isReady=!0)}setTokenizer(e){this.tokenizer=e;let n=e.getSpecialTokenIds();this.eosTokenId=n.eosTokenId??n.sepTokenId??50256}async preprocess(e){let n=Array.isArray(e)?e[0]??"":e;if(!this.tokenizer)return[new w(new Float32Array([0]),[1],"float32")];let s=this.tokenizer.encode(n,{addSpecialTokens:!1,padding:"do_not_pad"});return[new w(BigInt64Array.from(s.inputIds.map(i=>BigInt(i))),[1,s.inputIds.length],"int64")]}async postprocess(e,n){return{generatedText:"",tokenIds:[],numTokens:0,processingTime:0}}async run(e,n){await this.initialize();let s=Array.isArray(e)?e:[e],i=await Promise.all(s.map(r=>this.generateSingle(r,n??{})));return Array.isArray(e)?i:i[0]}async*stream(e,n={}){let s=performance.now();if(!this.tokenizer)throw new Error("Tokenizer not set. Call setTokenizer() first.");let{maxNewTokens:i=50,maxLength:r=512,temperature:a=1,topK:c=0,topP:l=1,repetitionPenalty:d=1,stopSequences:u=[],doSample:h=!0}=n,m=[...this.tokenizer.encode(e,{addSpecialTokens:!1,padding:"do_not_pad",truncation:!1}).inputIds],y=[],g="";for(let k=0;k<i&&!(m.length>=r);k++){let b=await this.generateNextToken(m,a,c,l,d,h);if(b===this.eosTokenId){yield{token:"",tokenId:b,generatedText:g,done:!0};break}let z=this.tokenizer.decode([b],!0);y.push(b),m.push(b),g+=z,n.onToken&&n.onToken(z,b);let I=!1;for(let S of u)if(g.endsWith(S)){g=g.slice(0,-S.length),I=!0;break}if(yield{token:z,tokenId:b,generatedText:g,done:I},I)break}let T=performance.now();console.log(`Generation completed in ${(T-s).toFixed(2)}ms`)}async generateSingle(e,n){let s=performance.now();if(!this.tokenizer)throw new Error("Tokenizer not set. Call setTokenizer() first.");let{maxNewTokens:i=50,maxLength:r=512,temperature:a=1,topK:c=0,topP:l=1,repetitionPenalty:d=1,stopSequences:u=[],doSample:h=!0,returnFullText:p=!1}=n,y=[...this.tokenizer.encode(e,{addSpecialTokens:!1,padding:"do_not_pad",truncation:!1}).inputIds],g=[];for(let b=0;b<i&&!(y.length>=r);b++){let z=await this.generateNextToken(y,a,c,l,d,h);if(z===this.eosTokenId)break;if(g.push(z),y.push(z),n.onToken){let D=this.tokenizer.decode([z],!0);n.onToken(D,z)}let I=this.tokenizer.decode(g,!0),S=!1;for(let D of u)if(I.endsWith(D)){S=!0;break}if(S)break}let T=this.tokenizer.decode(g,!0),k=performance.now();return{generatedText:T,fullText:p?e+T:void 0,tokenIds:g,numTokens:g.length,processingTime:k-s}}async generateNextToken(e,n,s,i,r,a){if(!this.model)throw new Error("Model not loaded");let c=e.length,l=new Map;l.set("input_ids",new w(BigInt64Array.from(e.map(I=>BigInt(I))),[1,c],"int64")),l.set("attention_mask",new w(BigInt64Array.from(e.map(()=>BigInt(1))),[1,c],"int64")),l.set("position_ids",new w(BigInt64Array.from(Array.from({length:c},(I,S)=>BigInt(S))),[1,c],"int64"));let d=22,u=4,h=64;for(let I=0;I<d;I++)l.set(`past_key_values.${I}.key`,new w(new Float32Array(0),[1,u,0,h],"float32")),l.set(`past_key_values.${I}.value`,new w(new Float32Array(0),[1,u,0,h],"float32"));let p=await U(this.model,l);if(!p||p.length===0)throw new Error("Model returned no outputs");let m=p[0],y=m.toFloat32Array(),g=m.shape[m.shape.length-1]??50257,T=new Float32Array(g),k=(e.length-1)*g;for(let I=0;I<g;I++)T[I]=y[k+I]??0;if(r!==1){for(let I of e)if(I<g){let S=T[I]??0;T[I]=S>0?S/r:S*r}}if(n!==1)for(let I=0;I<g;I++)T[I]=(T[I]??0)/n;let b=new w(T,[g],"float32"),z=O(b).toFloat32Array();return a?this.sample(z,s,i):this.greedy(z)}greedy(e){let n=0,s=e[0]??0;for(let i=1;i<e.length;i++)(e[i]??0)>s&&(s=e[i]??0,n=i);return n}sample(e,n,s){let i=Array.from({length:e.length},(d,u)=>u);i.sort((d,u)=>(e[u]??0)-(e[d]??0));let r=i;if(n>0&&n<e.length&&(r=i.slice(0,n)),s<1){let d=0,u=[];for(let h of r)if(u.push(h),d+=e[h]??0,d>=s)break;r=u}let a=0;for(let d of r)a+=e[d]??0;let c=Math.random()*a,l=0;for(let d of r)if(l+=e[d]??0,l>=c)return d;return r[0]??0}setChatTemplate(e){this.chatTemplateType=e}applyChatTemplate(e,n){switch(n?.templateType??this.chatTemplateType){case"chatml":return this.applyChatMLTemplate(e);case"llama2":return this.applyLlama2Template(e);case"llama3":return this.applyLlama3Template(e);case"mistral":return this.applyMistralTemplate(e);case"phi3":return this.applyPhi3Template(e);case"alpaca":return this.applyAlpacaTemplate(e);case"vicuna":return this.applyVicunaTemplate(e);case"custom":return this.applyCustomTemplate(e,n?.customTemplate??{});default:return this.applyChatMLTemplate(e)}}applyChatMLTemplate(e){let n="";for(let s of e)n+=`<|im_start|>${s.role}
|
|
19
19
|
${s.content}<|im_end|>
|
|
20
20
|
`;return n+=`<|im_start|>assistant
|
|
21
21
|
`,n}applyLlama2Template(e){let n="",s="";for(let i of e)i.role==="system"?s=i.content:i.role==="user"?s?(n+=`<s>[INST] <<SYS>>
|
|
@@ -48,9 +48,9 @@ ${i}
|
|
|
48
48
|
|
|
49
49
|
User:`,`
|
|
50
50
|
|
|
51
|
-
Human:`]}),r=Array.isArray(i)?i[0]:i;return this.conversationHistory.push({role:"assistant",content:r.generatedText.trim()}),r}async*chatStream(e,n){n?.systemPrompt&&(this.conversationHistory.length===0||this.conversationHistory[0]?.role!=="system")&&this.conversationHistory.unshift({role:"system",content:n.systemPrompt}),this.conversationHistory.push({role:"user",content:e});let s=this.applyChatTemplate(this.conversationHistory,n),i="";for await(let r of this.stream(s,{...n,stopSequences:[...n?.stopSequences??[],"<|im_end|>","<|end|>","<|eot_id|>","</s>"]}))i=r.generatedText,yield r;this.conversationHistory.push({role:"assistant",content:i.trim()})}getConversationHistory(){return[...this.conversationHistory]}setConversationHistory(e){this.conversationHistory=[...e]}clearConversation(){this.conversationHistory=[]}undoLastExchange(){this.conversationHistory.length>0&&this.conversationHistory[this.conversationHistory.length-1]?.role==="assistant"&&this.conversationHistory.pop(),this.conversationHistory.length>0&&this.conversationHistory[this.conversationHistory.length-1]?.role==="user"&&this.conversationHistory.pop()}};function jn(o){return new pe(o)}C();G();var ar={model:"https://huggingface.co/Xenova/yolos-tiny/resolve/main/onnx/model_quantized.onnx"},cr=["person","bicycle","car","motorcycle","airplane","bus","train","truck","boat","traffic light","fire hydrant","stop sign","parking meter","bench","bird","cat","dog","horse","sheep","cow","elephant","bear","zebra","giraffe","backpack","umbrella","handbag","tie","suitcase","frisbee","skis","snowboard","sports ball","kite","baseball bat","baseball glove","skateboard","surfboard","tennis racket","bottle","wine glass","cup","fork","knife","spoon","bowl","banana","apple","sandwich","orange","broccoli","carrot","hot dog","pizza","donut","cake","chair","couch","potted plant","bed","dining table","toilet","tv","laptop","mouse","remote","keyboard","cell phone","microwave","oven","toaster","sink","refrigerator","book","clock","vase","scissors","teddy bear","hair drier","toothbrush"],Oe=class extends E{constructor(e,n){super(e??{task:"object-detection",model:"default"});f(this,"preprocessor");f(this,"onnxModel",null);f(this,"labels");f(this,"modelUrl");this.labels=n??cr,this.modelUrl=e?.model&&e.model!=="default"?e.model:ar.model,this.preprocessor=new he({width:640,height:640,mean:[.485,.456,.406],std:[.229,.224,.225],channelFormat:"CHW"})}async initialize(){if(await super.initialize(),!this.onnxModel){let e=await v(this.modelUrl,{cache:this.config.cache??!0});this.onnxModel=await F(e)}}setLabels(e){this.labels=e}async run(e,n){await this.initialize();let s=await this.preprocess(e),i=await this.runModelInference(s);return this.postprocess(i,n)}async preprocess(e){let n=Array.isArray(e)?e:[e];if(n.length===1){let s=await this.preprocessor.process(n[0]);return[new w(s.toFloat32Array(),[1,...s.shape],"float32")]}return[await this.preprocessor.processBatch(n)]}async runModelInference(e){return await X(this.onnxModel,e)}async postprocess(e,n){let s=n??{},i=s.threshold??.5,r=s.topK??100,a=s.nms??!0,c=s.iouThreshold??.5;if(!e[0])return[];let l=e[0].toFloat32Array(),d=[...e[0].shape],u=this.parseDetections(l,d,i),h=a?this.nonMaxSuppression(u,c):u;return h.sort((p,m)=>m.score-p.score),h=h.slice(0,r),h}parseDetections(e,n,s){let i=[],r=n[1]??0,a=n[2]??0;if(a>=5){let c=a-5;for(let l=0;l<r;l++){let d=l*a,u=e[d+4]??0;if(u<s)continue;let h=0,p=0;for(let b=0;b<c;b++){let z=e[d+5+b]??0;z>h&&(h=z,p=b)}let m=u*h;if(m<s)continue;let y=e[d]??0,g=e[d+1]??0,T=e[d+2]??0,k=e[d+3]??0;i.push({label:this.labels[p]??`class_${p}`,score:m,classId:p,box:{x:Math.max(0,y-T/2),y:Math.max(0,g-k/2),width:T,height:k},boxNormalized:{x:Math.max(0,y-T/2),y:Math.max(0,g-k/2),width:T,height:k}})}}else if(a===4)for(let c=0;c<r;c++){let l=c*a,d=e[l]??0,u=e[l+1]??0,h=e[l+2]??0,p=e[l+3]??0;i.push({label:this.labels[0]??"object",score:1,classId:0,box:{x:d,y:u,width:h-d,height:p-u},boxNormalized:{x:d,y:u,width:h-d,height:p-u}})}return i}nonMaxSuppression(e,n){if(e.length===0)return[];let s=[...e].sort((a,c)=>c.score-a.score),i=[],r=new Array(s.length).fill(!0);for(let a=0;a<s.length;a++){if(!r[a])continue;let c=s[a];i.push(c);for(let l=a+1;l<s.length;l++){if(!r[l])continue;let d=s[l];if(c.classId!==d.classId)continue;this.computeIoU(c.box,d.box)>n&&(r[l]=!1)}}return i}computeIoU(e,n){let s=Math.max(0,Math.min(e.x+e.width,n.x+n.width)-Math.max(e.x,n.x)),i=Math.max(0,Math.min(e.y+e.height,n.y+n.height)-Math.max(e.y,n.y)),r=s*i,a=e.width*e.height,c=n.width*n.height,l=a+c-r;return l>0?r/l:0}};N("object-detection",o=>new Oe(o));C();G();var Lt={encoder:"https://huggingface.co/Xenova/whisper-tiny/resolve/main/onnx/encoder_model_quantized.onnx",decoder:"https://huggingface.co/Xenova/whisper-tiny/resolve/main/onnx/decoder_model_merged_quantized.onnx",tokenizer:"https://huggingface.co/Xenova/whisper-tiny/resolve/main/tokenizer.json"},lr=50258,dr=50358,ur=50359,hr=50257,fr=50363,Hn=50259,pr=448,$e=class extends E{constructor(e){super(e??{task:"automatic-speech-recognition",model:"default"});f(this,"audioPreprocessor");f(this,"tokenizer",null);f(this,"encoderModel",null);f(this,"decoderModel",null);f(this,"encoderUrl");f(this,"decoderUrl");f(this,"tokenizerUrl");this.encoderUrl=Lt.encoder,this.decoderUrl=Lt.decoder,this.tokenizerUrl=Lt.tokenizer,this.audioPreprocessor=new fe({sampleRate:16e3,nMels:80,nFft:400,hopLength:160,maxDuration:30})}async initialize(){if(await super.initialize(),this.tokenizer||(this.tokenizer=await P.fromUrl(this.tokenizerUrl)),!this.encoderModel){let e=await v(this.encoderUrl,{cache:this.config.cache??!0});this.encoderModel=await F(e)}if(!this.decoderModel){let e=await v(this.decoderUrl,{cache:this.config.cache??!0});this.decoderModel=await F(e)}}setTokenizer(e){this.tokenizer=e}async run(e,n){await this.initialize();let s=Array.isArray(e),i=s?e:[e],r=n??{},a=[];for(let c of i){let l=await this.transcribeSingle(c,r);a.push(l)}return s?a:a[0]}async transcribeSingle(e,n){let s=performance.now(),i=await this.audioPreprocessor.process(e),r=new w(i.toFloat32Array(),[1,...i.shape],"float32"),c=(await X(this.encoderModel,[r]))[0],l=n.task??"transcribe",d=this.buildInitialTokens(l,n.language),u=await this.autoregressiveDecode(c,d),h=this.tokenizer.decode(u,!0),p={text:h.trim(),processingTime:performance.now()-s};return n.returnTimestamps&&(p.chunks=this.extractTimestamps(u,h)),p}buildInitialTokens(e,n){let s=[lr];return s.push(n?this.getLanguageToken(n):Hn),s.push(e==="translate"?dr:ur),s.push(fr),s}getLanguageToken(e){return{en:50259,zh:50260,de:50261,es:50262,ru:50263,ko:50264,fr:50265,ja:50266,pt:50267,tr:50268,pl:50269,ca:50270,nl:50271,ar:50272,sv:50273,it:50274,id:50275,hi:50276,fi:50277,vi:50278}[e.toLowerCase()]??Hn}async autoregressiveDecode(e,n){let s=[...n];for(let i=0;i<pr;i++){let r=new w(BigInt64Array.from(s.map(m=>BigInt(m))),[1,s.length],"int64"),a=new Map;a.set("input_ids",r),a.set("encoder_hidden_states",e);let l=(await U(this.decoderModel,a))[0].toFloat32Array(),d=l.length/s.length,u=l.slice((s.length-1)*d),h=0,p=u[0]??-1/0;for(let m=1;m<u.length;m++)(u[m]??-1/0)>p&&(p=u[m]??-1/0,h=m);if(h===hr)break;s.push(h)}return s.slice(n.length)}extractTimestamps(e,n){let s=n.split(/\s+/).filter(l=>l.length>0),i=[],r=2.5,a="",c=0;for(let l=0;l<s.length;l++)if(a+=(a?" ":"")+s[l],(l+1)%5===0||l===s.length-1){let d=a.split(/\s+/).length/r;i.push({text:a,start:c,end:c+d}),c=c+d,a=""}return i}async processLongAudio(e,n={}){let s=n.chunkDuration??30,i=n.chunkOverlap??5,a=(await this.audioPreprocessor.processRaw(e)).toFloat32Array(),c=16e3,l=s*c,d=i*c,u=l-d,h=[];for(let y=0;y<a.length;y+=u){let g=Math.min(y+l,a.length),T=a.slice(y,g),k=await this.run(new Float32Array(T),n);if(k.chunks){let b=y/c;k.chunks=k.chunks.map(z=>({...z,start:z.start+b,end:z.end+b}))}h.push(k)}let p=h.map(y=>y.text).join(" "),m=h.flatMap(y=>y.chunks??[]);return{text:p,chunks:m}}async preprocess(e){let n=Array.isArray(e)?e:[e],s=await Promise.all(n.map(i=>this.audioPreprocessor.process(i)));if(s.length===1){let i=s[0];return[new w(i.toFloat32Array(),[1,...i.shape],"float32")]}return s}async postprocess(e,n){let i=(n??{}).returnTimestamps??!1;if(!e[0])return{text:""};let r=e[0].toFloat32Array(),a=e[0].shape,c=this.decodeOutput(r,a),l={text:c};return i&&(l.chunks=this.extractTimestamps([],c)),l}decodeOutput(e,n){let s=n[1]??e.length,i=n[2]??1,r=[];if(i>1)for(let a=0;a<s;a++){let c=a*i,l=0,d=e[c]??-1/0;for(let u=1;u<i;u++)(e[c+u]??-1/0)>d&&(d=e[c+u]??-1/0,l=u);r.push(l)}else for(let a=0;a<e.length;a++)r.push(Math.round(e[a]??0));return this.tokenizer?this.tokenizer.decode(r,!0):r.join(" ")}};N("automatic-speech-recognition",o=>new $e(o));C();G();var Vn={model:"https://huggingface.co/Xenova/nli-deberta-v3-small/resolve/main/onnx/model_quantized.onnx",tokenizer:"https://huggingface.co/Xenova/nli-deberta-v3-small/resolve/main/tokenizer.json"},mr=2,Re=class extends E{constructor(e){super(e??{task:"zero-shot-classification",model:"default"});f(this,"tokenizer",null);f(this,"onnxModel",null);f(this,"hypothesisTemplate","This text is about {label}.");f(this,"modelUrl");f(this,"tokenizerUrl");this.modelUrl=e?.model&&e.model!=="default"?e.model:Vn.model,this.tokenizerUrl=Vn.tokenizer}async initialize(){if(await super.initialize(),this.tokenizer||(this.tokenizer=await P.fromUrl(this.tokenizerUrl)),!this.onnxModel){let e=await v(this.modelUrl,{cache:this.config.cache??!0});this.onnxModel=await F(e)}}setTokenizer(e){this.tokenizer=e}async classify(e,n,s){return this.run({text:e,candidateLabels:n},s)}async run(e,n){await this.initialize();let{text:s,candidateLabels:i}=e,r=n??{},a=Array.isArray(s)?s:[s],c=r.hypothesisTemplate??this.hypothesisTemplate,l=r.multiLabel??!1,d=await Promise.all(a.map(u=>this.classifySingle(u,i,c,l)));return Array.isArray(s)?d:d[0]}async classifySingle(e,n,s,i){let r=performance.now(),a=n.map(u=>s.replace("{label}",u)),c=[];for(let u of a){let h=await this.scoreHypothesis(e,u);c.push(h)}let l;if(i)l=c.map(u=>1/(1+Math.exp(-u)));else{let u=new w(new Float32Array(c),[c.length],"float32");l=Array.from(O(u).toFloat32Array())}let d=n.map((u,h)=>({label:u,score:l[h]??0}));return d.sort((u,h)=>h.score-u.score),{sequence:e,labels:d.map(u=>u.label),scores:d.map(u=>u.score),processingTime:performance.now()-r}}async scoreHypothesis(e,n){let s=this.tokenizer.encode(e,{textPair:n,addSpecialTokens:!0,maxLength:512,truncation:!0,returnAttentionMask:!0}),i=new w(BigInt64Array.from(s.inputIds.map(d=>BigInt(d))),[1,s.inputIds.length],"int64"),r=new w(BigInt64Array.from(s.attentionMask.map(d=>BigInt(d))),[1,s.attentionMask.length],"int64"),a=new Map;return a.set("input_ids",i),a.set("attention_mask",r),(await U(this.onnxModel,a))[0].toFloat32Array()[mr]??0}async preprocess(e){let{text:n,candidateLabels:s}=e,i=Array.isArray(n)?n[0]??"":n,r=s[0]??"",a=this.tokenizer.encode(i,{textPair:this.hypothesisTemplate.replace("{label}",r),addSpecialTokens:!0,maxLength:512});return[new w(BigInt64Array.from(a.inputIds.map(c=>BigInt(c))),[1,a.inputIds.length],"int64")]}async postprocess(e,n){return{sequence:"",labels:[],scores:[]}}};N("zero-shot-classification",o=>new Re(o));C();G();var Wn={model:"https://huggingface.co/Xenova/distilbert-base-cased-distilled-squad/resolve/main/onnx/model_quantized.onnx",tokenizer:"https://huggingface.co/Xenova/distilbert-base-cased-distilled-squad/resolve/main/tokenizer.json"},Ue=class extends E{constructor(e){super(e??{task:"question-answering",model:"default"});f(this,"tokenizer",null);f(this,"onnxModel",null);f(this,"modelUrl");f(this,"tokenizerUrl");this.modelUrl=e?.model&&e.model!=="default"?e.model:Wn.model,this.tokenizerUrl=Wn.tokenizer}async initialize(){if(await super.initialize(),this.tokenizer||(this.tokenizer=await P.fromUrl(this.tokenizerUrl)),!this.onnxModel){let e=await v(this.modelUrl,{cache:this.config.cache??!0});this.onnxModel=await F(e)}}setTokenizer(e){this.tokenizer=e}async run(e,n){await this.initialize();let s=Array.isArray(e)?e:[e],i=await Promise.all(s.map(r=>this.answerQuestion(r,n??{})));return Array.isArray(e)?i:i[0]}async answerQuestion(e,n){let s=performance.now(),{question:i,context:r}=e,a=n.maxAnswerLength??30,c=this.tokenizer.encode(i,{textPair:r,addSpecialTokens:!0,maxLength:512,truncation:!0,padding:"do_not_pad",returnAttentionMask:!0,returnTokenTypeIds:!0}),l=c.inputIds.length,d=new w(BigInt64Array.from(c.inputIds.map(R=>BigInt(R))),[1,l],"int64"),u=new w(BigInt64Array.from(c.attentionMask.map(R=>BigInt(R))),[1,l],"int64"),h=new Map;h.set("input_ids",d),h.set("attention_mask",u);let p=await U(this.onnxModel,h);if(p.length<2)return{answer:"",score:0,start:0,end:0,processingTime:performance.now()-s};let m=p[0].toFloat32Array(),y=p[1].toFloat32Array(),g=O(new w(new Float32Array(m),[l],"float32")).toFloat32Array(),T=O(new w(new Float32Array(y),[l],"float32")).toFloat32Array(),b=(c.tokenTypeIds??new Array(l).fill(1)).findIndex(R=>R===1),z=b>=0?b:0,I=l-1,S=z,D=z,_=-1/0;for(let R=z;R<=I;R++)for(let ge=R;ge<Math.min(R+a,I+1);ge++){let L=(g[R]??0)*(T[ge]??0);L>_&&(_=L,S=R,D=ge)}let A=c.inputIds.slice(S,D+1);return{answer:this.tokenizer.decode(A,!0)||"",score:Math.max(0,_),start:S,end:D,processingTime:performance.now()-s}}async preprocess(e){let n=Array.isArray(e)?e[0]:e,s=this.tokenizer.encode(n.question,{textPair:n.context,addSpecialTokens:!0,maxLength:512,truncation:!0,returnAttentionMask:!0,returnTokenTypeIds:!0});return[new w(BigInt64Array.from(s.inputIds.map(i=>BigInt(i))),[1,s.inputIds.length],"int64"),new w(BigInt64Array.from(s.attentionMask.map(i=>BigInt(i))),[1,s.attentionMask.length],"int64")]}async postprocess(e,n){if(e.length<2)return{answer:"",score:0,start:0,end:0};let s=e[0].toFloat32Array(),i=e[1].toFloat32Array(),r=s.length,a=O(new w(s,[r],"float32")).toFloat32Array(),c=O(new w(i,[r],"float32")).toFloat32Array(),l=0,d=0,u=0;for(let h=0;h<r;h++)for(let p=h;p<Math.min(h+30,r);p++){let m=(a[h]??0)*(c[p]??0);m>u&&(u=m,l=h,d=p)}return{answer:"",score:u,start:l,end:d}}};N("question-answering",o=>new Ue(o));C();var Gn={encoder:"https://huggingface.co/Xenova/slimsam-77-uniform/resolve/main/onnx/vision_encoder_quantized.onnx",decoder:"https://huggingface.co/Xenova/slimsam-77-uniform/resolve/main/onnx/prompt_encoder_mask_decoder_quantized.onnx"},re=class extends E{constructor(e){super(e);f(this,"encoderModel",null);f(this,"decoderModel",null);f(this,"imageEmbedding",null);f(this,"imagePositionalEmbedding",null);f(this,"currentImageSize",null);f(this,"resizedImageSize",null);f(this,"inputSize",1024);f(this,"modelsLoaded",!1);f(this,"encoderUrl");f(this,"decoderUrl");this.encoderUrl=Gn.encoder,this.decoderUrl=Gn.decoder}get isModelsLoaded(){return this.modelsLoaded}setModelUrls(e,n){this.encoderUrl=e,this.decoderUrl=n}async loadModels(e){if(this.modelsLoaded)return;e?.({model:"encoder",loaded:0,total:100,progress:0});let n=await this.fetchModelWithProgress(this.encoderUrl,(i,r)=>{e?.({model:"encoder",loaded:i,total:r,progress:Math.round(i/r*100)})});this.encoderModel=await F(n,{runtime:"wasm"}),e?.({model:"decoder",loaded:0,total:100,progress:0});let s=await this.fetchModelWithProgress(this.decoderUrl,(i,r)=>{e?.({model:"decoder",loaded:i,total:r,progress:Math.round(i/r*100)})});this.decoderModel=await F(s,{runtime:"wasm"}),this.modelsLoaded=!0}async fetchModelWithProgress(e,n){let s=await fetch(e);if(!s.ok)throw new Error(`Failed to fetch model: ${s.status} ${s.statusText}`);let i=s.headers.get("content-length"),r=i?parseInt(i,10):0;if(!s.body){let h=await s.arrayBuffer();return n(h.byteLength,h.byteLength),h}let a=s.body.getReader(),c=[],l=0;for(;;){let{done:h,value:p}=await a.read();if(h)break;c.push(p),l+=p.length,n(l,r||l)}let d=new Uint8Array(l),u=0;for(let h of c)d.set(h,u),u+=h.length;return d.buffer}async initialize(){this.isReady||(this.isReady=!0)}async loadEncoder(e){this.encoderModel=await we(e,{runtime:"wasm"})}async loadDecoder(e){this.decoderModel=await we(e,{runtime:"wasm"})}async setImage(e){if(!this.modelsLoaded)throw new Error("Models not loaded. Call loadModels() first.");let n=await this.loadImage(e);this.currentImageSize={width:n.width,height:n.height};let{tensor:s,resizedSize:i}=this.preprocessImage(n);if(this.resizedImageSize=i,this.encoderModel){let r=await X(this.encoderModel,[s]);this.imageEmbedding=r[0],this.imagePositionalEmbedding=r[1],console.log("[SAM] Encoder outputs:",r.length),console.log("[SAM] image_embeddings shape:",this.imageEmbedding.shape),this.imagePositionalEmbedding&&console.log("[SAM] image_positional_embeddings shape:",this.imagePositionalEmbedding.shape)}else throw new Error("Encoder model not loaded")}async segment(e={}){if(!this.imageEmbedding||!this.currentImageSize||!this.resizedImageSize)throw new Error("No image set. Call setImage() first.");if(!this.decoderModel)throw new Error("Decoder model not loaded");let n=performance.now(),{points:s=[],boxes:i=[],maskThreshold:r=0,returnAllMasks:a=!1}=e,c=this.prepareDecoderInputs(s,i);if(c.set("image_embeddings",this.imageEmbedding),this.imagePositionalEmbedding)c.set("image_positional_embeddings",this.imagePositionalEmbedding);else throw new Error("image_positional_embeddings not available from encoder");let l=await U(this.decoderModel,c),d=l[0],u=l[1],h=this.postprocessMasks(d,u,r,a);return h.processingTime=performance.now()-n,h}async run(e,n){return await this.setImage(e),this.segment(n)}async loadImage(e){if(typeof e=="string")return this.loadImageFromUrl(e);if(e instanceof HTMLImageElement)return this.imageElementToImageData(e);if(e instanceof HTMLCanvasElement)return this.canvasToImageData(e);if(e instanceof ImageData)return e;if(typeof ImageBitmap<"u"&&e instanceof ImageBitmap)return this.imageBitmapToImageData(e);throw new Error("Unsupported image input type")}async loadImageFromUrl(e){return new Promise((n,s)=>{let i=new Image;i.crossOrigin="anonymous",i.onload=()=>{let r=document.createElement("canvas");r.width=i.width,r.height=i.height;let a=r.getContext("2d");a.drawImage(i,0,0),n(a.getImageData(0,0,i.width,i.height))},i.onerror=s,i.src=e})}imageElementToImageData(e){let n=document.createElement("canvas");n.width=e.naturalWidth||e.width,n.height=e.naturalHeight||e.height;let s=n.getContext("2d");return s.drawImage(e,0,0),s.getImageData(0,0,n.width,n.height)}canvasToImageData(e){return e.getContext("2d").getImageData(0,0,e.width,e.height)}imageBitmapToImageData(e){let n=document.createElement("canvas");n.width=e.width,n.height=e.height;let s=n.getContext("2d");return s.drawImage(e,0,0),s.getImageData(0,0,n.width,n.height)}preprocessImage(e){let{width:n,height:s}=e,i=this.inputSize/Math.max(n,s),r=Math.round(n*i),a=Math.round(s*i),c=document.createElement("canvas");c.width=this.inputSize,c.height=this.inputSize;let l=c.getContext("2d");l.fillStyle="rgb(123.675, 116.28, 103.53)",l.fillRect(0,0,this.inputSize,this.inputSize);let d=document.createElement("canvas");d.width=n,d.height=s,d.getContext("2d").putImageData(e,0,0),l.drawImage(d,0,0,r,a);let h=l.getImageData(0,0,this.inputSize,this.inputSize),p=new Float32Array(3*this.inputSize*this.inputSize),m=[123.675,116.28,103.53],y=[58.395,57.12,57.375];for(let g=0;g<this.inputSize*this.inputSize;g++){let T=g*4;p[g]=(h.data[T]-m[0])/y[0],p[this.inputSize*this.inputSize+g]=(h.data[T+1]-m[1])/y[1],p[2*this.inputSize*this.inputSize+g]=(h.data[T+2]-m[2])/y[2]}return{tensor:new w(p,[1,3,this.inputSize,this.inputSize],"float32"),resizedSize:{width:r,height:a}}}prepareDecoderInputs(e,n){let{width:s,height:i}=this.resizedImageSize,r=s,a=i,c=[],l=[];for(let h of e)c.push(h.x*r,h.y*a),l.push(h.label);for(let h of n)c.push(h.x1*r,h.y1*a),l.push(2),c.push(h.x2*r,h.y2*a),l.push(3);c.length===0&&(c.push(s/2,i/2),l.push(1));let d=l.length,u=new Map;return u.set("input_points",new w(new Float32Array(c),[1,1,d,2],"float32")),u.set("input_labels",new w(BigInt64Array.from(l.map(h=>BigInt(h))),[1,1,d],"int64")),u}postprocessMasks(e,n,s,i){let{width:r,height:a}=this.currentImageSize,c=n.toFloat32Array(),l=e.toFloat32Array(),d=c.length,u=e.shape,h=u[2]??a,p=u[3]??r,m=0,y=c[0]??0;for(let k=1;k<d;k++)(c[k]??0)>y&&(y=c[k]??0,m=k);let T={mask:this.resizeMask(l,m,p,h,r,a,s),width:r,height:a,score:y};if(i&&d>1){T.allMasks=[];for(let k=0;k<d;k++){let b=this.resizeMask(l,k,p,h,r,a,s);T.allMasks.push({mask:b,score:c[k]??0})}}return T}resizeMask(e,n,s,i,r,a,c){let l=new Uint8Array(r*a),d=n*s*i;for(let u=0;u<a;u++)for(let h=0;h<r;h++){let p=h/r*s,m=u/a*i,y=Math.floor(p),g=Math.min(y+1,s-1),T=Math.floor(m),k=Math.min(T+1,i-1),b=p-y,z=m-T,I=e[d+T*s+y]??0,S=e[d+T*s+g]??0,D=e[d+k*s+y]??0,_=e[d+k*s+g]??0,A=I*(1-b)*(1-z)+S*b*(1-z)+D*(1-b)*z+_*b*z,me=1/(1+Math.exp(-A));l[u*r+h]=me>c?255:0}return l}clearImage(){this.imageEmbedding=null,this.imagePositionalEmbedding=null,this.currentImageSize=null,this.resizedImageSize=null}async preprocess(e){let n=await this.loadImage(e),{tensor:s}=this.preprocessImage(n);return[s]}async postprocess(e,n){return{mask:new Uint8Array(0),width:0,height:0,score:0}}dispose(){super.dispose(),this.encoderModel?.dispose(),this.decoderModel?.dispose(),this.imageEmbedding=null,this.imagePositionalEmbedding=null,this.currentImageSize=null,this.resizedImageSize=null,this.modelsLoaded=!1}};function Xn(o={}){return new re({task:"image-segmentation",model:o.model??"slimsam",runtime:o.runtime,cache:o.cache??!0,quantization:o.quantization})}N("image-segmentation",o=>new re(o));async function Be(o,t){et();let e={task:o,model:t?.model??"default",runtime:t?.runtime,cache:t?.cache??!0,quantization:t?.quantization},n;switch(o){case"text-classification":n=new Y(e,t?.labels);break;case"sentiment-analysis":n=new te(e);break;case"feature-extraction":n=new ne(e);break;case"image-classification":n=new se(e,t?.labels);break;case"text-generation":n=new pe(e);break;case"object-detection":n=new Oe(e,t?.labels);break;case"automatic-speech-recognition":n=new $e(e);break;case"zero-shot-classification":n=new Re(e);break;case"question-answering":n=new Ue(e);break;case"image-segmentation":n=new re(e);break;default:{let s=Dt(o);if(s){n=s.factory(e);break}throw new Error(`Unknown pipeline task: "${o}". Register a plugin with registerPlugin() to add custom pipeline tasks.`)}}return await n.initialize(),n}async function gr(o,t){let e=await Promise.all(o.map(s=>Be(s,t))),n={};for(let s=0;s<o.length;s++){let i=o[s];n[i]=e[s]}return n}function yr(o){if(o.length===0)throw new Error("[edgeFlow.js] compose() requires at least one stage");let t=null;async function e(){return t||(t=await Promise.all(o.map(n=>Be(n.task,{model:n.model,...n.options}))),t)}return{get length(){return o.length},async run(n){let s=await e(),i=[],r=[],a=n,c=performance.now();for(let l=0;l<o.length;l++){let d=o[l],u=s[l];d.transform&&(a=d.transform(a));let h=performance.now();a=await u.run(a,d.runOptions),r.push(performance.now()-h),i.push(a)}return{output:a,stages:i,totalTime:performance.now()-c,stageTimes:r}},dispose(){if(t){for(let n of t)n&&typeof n.dispose=="function"&&n.dispose();t=null}}}}function wr(o){if(o.length===0)throw new Error("[edgeFlow.js] parallel() requires at least one stage");let t=null;async function e(){return t||(t=await Promise.all(o.map(n=>Be(n.task,{model:n.model,...n.options}))),t)}return{async run(n){let s=await e(),i=performance.now();return{outputs:await Promise.all(o.map((a,c)=>{let l=a.transform?a.transform(n):n;return s[c].run(l,a.runOptions)})),totalTime:performance.now()-i}},dispose(){if(t){for(let n of t)n&&typeof n.dispose=="function"&&n.dispose();t=null}}}}G();G();B();var xr="https://huggingface.co",Mr="main",Qn=["model.onnx","model_quantized.onnx","model_int8.onnx","model_uint8.onnx","model_fp16.onnx","onnx/model.onnx","onnx/model_quantized.onnx"];function ot(o,t,e={}){let n=e.endpoint??xr,s=e.revision??Mr,i=e.subfolder?`${e.subfolder}/`:"";return`${n}/${o}/resolve/${s}/${i}${t}`}async function Kn(o,t){let e={};return t&&(e.Authorization=`Bearer ${t}`),await fetch(o,{headers:e})}async function Yn(o,t,e={}){let n=ot(o,t,e);try{let s=await Kn(n,e.token);return s.ok||s.status===302}catch{return!1}}async function Nt(o,t={}){for(let e of Qn)if(await Yn(o,e,t))return e;return null}async function Jn(o,t,e={}){let n=ot(o,t,e);return v(n,{cache:e.cache??!0,forceDownload:e.forceDownload??!1,onProgress:e.onProgress?s=>{e.onProgress({file:t,fileIndex:1,totalFiles:1,fileProgress:s,overallProgress:s.percent})}:void 0})}async function Tr(o,t,e={}){let n=ot(o,t,e);if(e.cache!==!1&&!e.forceDownload&&await ve(n)){let r=await v(n,{cache:!0}),a=new TextDecoder().decode(r);return JSON.parse(a)}let s=await Kn(n,e.token);if(!s.ok)throw new x(`Failed to download ${t} from ${o}: ${s.status}`,M.MODEL_NOT_FOUND);return s.json()}async function Ct(o,t={}){let e=ot(o,"tokenizer.json",t);return P.fromUrl(e)}async function it(o,t={}){return Tr(o,"config.json",t)}async function at(o,t={}){let e={},s=0,i=(d,u)=>{if(t.onProgress){let h=s/3*100,p=u.percent/3;t.onProgress({file:d,fileIndex:s+1,totalFiles:3,fileProgress:u,overallProgress:h+p})}};console.log(`\u{1F50D} Finding ONNX model in ${o}...`);let r=await Nt(o,t);if(!r)throw new x(`No ONNX model found in ${o}. Please ensure the model has an ONNX file.`,M.MODEL_NOT_FOUND,{modelId:o,triedFiles:Qn});e.model=r,console.log(`\u{1F4E6} Downloading model: ${r}`);let a=await Jn(o,r,{...t,onProgress:d=>i(r,d.fileProgress)});s=1;let c;try{console.log("\u{1F4DD} Downloading tokenizer..."),e.tokenizer="tokenizer.json",c=await Ct(o,t),console.log("\u2713 Tokenizer loaded")}catch{console.warn(`\u26A0\uFE0F No tokenizer found for ${o}`)}s=2;let l;try{console.log("\u2699\uFE0F Downloading config..."),e.config="config.json",l=await it(o,t),console.log("\u2713 Config loaded")}catch{console.warn(`\u26A0\uFE0F No config found for ${o}`)}return s=3,t.onProgress&&t.onProgress({file:"complete",fileIndex:3,totalFiles:3,fileProgress:{loaded:1,total:1,percent:100,speed:0,eta:0},overallProgress:100}),console.log(`\u2705 Model bundle downloaded: ${o}`),{modelId:o,modelData:a,tokenizer:c,config:l,files:e}}async function Zn(o,t={}){return at(o,t)}async function es(o,t={}){try{return await Nt(o,t)!==null}catch{return!1}}async function ts(o,t={}){let[e,n,s]=await Promise.all([Nt(o,t),Yn(o,"tokenizer.json",t),it(o,t).catch(()=>{})]);return{hasOnnx:e!==null,onnxFile:e??void 0,hasTokenizer:n,hasConfig:s!==void 0,config:s}}var Ot={"sentiment-analysis":"Xenova/distilbert-base-uncased-finetuned-sst-2-english","text-classification":"Xenova/distilbert-base-uncased-finetuned-sst-2-english","feature-extraction":"Xenova/all-MiniLM-L6-v2","sentence-similarity":"Xenova/all-MiniLM-L6-v2","question-answering":"Xenova/distilbert-base-cased-distilled-squad",ner:"Xenova/bert-base-NER","token-classification":"Xenova/bert-base-NER","text-generation":"Xenova/gpt2","translation-en-fr":"Xenova/t5-small","translation-en-de":"Xenova/t5-small",summarization:"Xenova/distilbart-cnn-6-6","fill-mask":"Xenova/bert-base-uncased","image-classification":"Xenova/vit-base-patch16-224","object-detection":"Xenova/detr-resnet-50","image-segmentation":"Xenova/segformer-b0-finetuned-ade-512-512","zero-shot-classification":"Xenova/mobilebert-uncased-mnli","automatic-speech-recognition":"Xenova/whisper-tiny.en","text-to-speech":"Xenova/speecht5_tts"};function $t(o){return Ot[o]}async function ns(o,t={}){let e=$t(o);return at(e,t)}async function qe(o,t={}){let{warmupRuns:e=3,runs:n=10,verbose:s=!1,timeout:i=3e4,name:r="benchmark"}=t,a=[],c=0;s&&console.log(`[${r}] Running ${e} warmup iterations...`);for(let y=0;y<e;y++)try{await Promise.race([Promise.resolve(o()),new Promise((g,T)=>setTimeout(()=>T(new Error("Timeout")),i))])}catch{}s&&console.log(`[${r}] Running ${n} measured iterations...`);for(let y=0;y<n;y++)try{let g=performance.now();await Promise.race([Promise.resolve(o()),new Promise((k,b)=>setTimeout(()=>b(new Error("Timeout")),i))]);let T=performance.now();a.push(T-g),s&&console.log(` Run ${y+1}: ${(T-g).toFixed(2)}ms`)}catch(g){c++,s&&console.log(` Run ${y+1}: FAILED - ${g}`)}if(a.length===0)throw new Error(`All ${n} runs failed`);let l=[...a].sort((y,g)=>y-g),u=a.reduce((y,g)=>y+g,0)/a.length,h=a.reduce((y,g)=>y+Math.pow(g-u,2),0)/a.length,p=Math.sqrt(h),m={name:r,avgTime:u,medianTime:l[Math.floor(l.length/2)]??0,minTime:l[0]??0,maxTime:l[l.length-1]??0,stdDev:p,p95:l[Math.floor(l.length*.95)]??l[l.length-1]??0,p99:l[Math.floor(l.length*.99)]??l[l.length-1]??0,throughput:1e3/u,times:a,totalRuns:n,failedRuns:c};return s&&(console.log(`
|
|
52
|
-
[${r}] Results:`),console.log(` Avg: ${m.avgTime.toFixed(2)}ms`),console.log(` Median: ${m.medianTime.toFixed(2)}ms`),console.log(` Min: ${m.minTime.toFixed(2)}ms`),console.log(` Max: ${m.maxTime.toFixed(2)}ms`),console.log(` Std Dev: ${m.stdDev.toFixed(2)}ms`),console.log(` P95: ${m.p95.toFixed(2)}ms`),console.log(` Throughput: ${m.throughput.toFixed(2)} ops/sec`)),m}async function
|
|
53
|
-
=== ${n} ===`),e[n]=await
|
|
51
|
+
Human:`]}),r=Array.isArray(i)?i[0]:i;return this.conversationHistory.push({role:"assistant",content:r.generatedText.trim()}),r}async*chatStream(e,n){n?.systemPrompt&&(this.conversationHistory.length===0||this.conversationHistory[0]?.role!=="system")&&this.conversationHistory.unshift({role:"system",content:n.systemPrompt}),this.conversationHistory.push({role:"user",content:e});let s=this.applyChatTemplate(this.conversationHistory,n),i="";for await(let r of this.stream(s,{...n,stopSequences:[...n?.stopSequences??[],"<|im_end|>","<|end|>","<|eot_id|>","</s>"]}))i=r.generatedText,yield r;this.conversationHistory.push({role:"assistant",content:i.trim()})}getConversationHistory(){return[...this.conversationHistory]}setConversationHistory(e){this.conversationHistory=[...e]}clearConversation(){this.conversationHistory=[]}undoLastExchange(){this.conversationHistory.length>0&&this.conversationHistory[this.conversationHistory.length-1]?.role==="assistant"&&this.conversationHistory.pop(),this.conversationHistory.length>0&&this.conversationHistory[this.conversationHistory.length-1]?.role==="user"&&this.conversationHistory.pop()}};function Xn(o){return new pe(o)}C();G();var ur={model:"https://huggingface.co/Xenova/yolos-tiny/resolve/main/onnx/model_quantized.onnx"},hr=["person","bicycle","car","motorcycle","airplane","bus","train","truck","boat","traffic light","fire hydrant","stop sign","parking meter","bench","bird","cat","dog","horse","sheep","cow","elephant","bear","zebra","giraffe","backpack","umbrella","handbag","tie","suitcase","frisbee","skis","snowboard","sports ball","kite","baseball bat","baseball glove","skateboard","surfboard","tennis racket","bottle","wine glass","cup","fork","knife","spoon","bowl","banana","apple","sandwich","orange","broccoli","carrot","hot dog","pizza","donut","cake","chair","couch","potted plant","bed","dining table","toilet","tv","laptop","mouse","remote","keyboard","cell phone","microwave","oven","toaster","sink","refrigerator","book","clock","vase","scissors","teddy bear","hair drier","toothbrush"],$e=class extends E{constructor(e,n){super(e??{task:"object-detection",model:"default"});f(this,"preprocessor");f(this,"onnxModel",null);f(this,"labels");f(this,"modelUrl");this.labels=n??hr,this.modelUrl=e?.model&&e.model!=="default"?e.model:ur.model,this.preprocessor=new he({width:640,height:640,mean:[.485,.456,.406],std:[.229,.224,.225],channelFormat:"CHW"})}async initialize(){if(await super.initialize(),!this.onnxModel){let e=await v(this.modelUrl,{cache:this.config.cache??!0});this.onnxModel=await F(e)}}setLabels(e){this.labels=e}async run(e,n){await this.initialize();let s=await this.preprocess(e),i=await this.runModelInference(s);return this.postprocess(i,n)}async preprocess(e){let n=Array.isArray(e)?e:[e];if(n.length===1){let s=await this.preprocessor.process(n[0]);return[new w(s.toFloat32Array(),[1,...s.shape],"float32")]}return[await this.preprocessor.processBatch(n)]}async runModelInference(e){return await X(this.onnxModel,e)}async postprocess(e,n){let s=n??{},i=s.threshold??.5,r=s.topK??100,a=s.nms??!0,c=s.iouThreshold??.5;if(!e[0])return[];let l=e[0].toFloat32Array(),d=[...e[0].shape],u=this.parseDetections(l,d,i),h=a?this.nonMaxSuppression(u,c):u;return h.sort((p,m)=>m.score-p.score),h=h.slice(0,r),h}parseDetections(e,n,s){let i=[],r=n[1]??0,a=n[2]??0;if(a>=5){let c=a-5;for(let l=0;l<r;l++){let d=l*a,u=e[d+4]??0;if(u<s)continue;let h=0,p=0;for(let b=0;b<c;b++){let z=e[d+5+b]??0;z>h&&(h=z,p=b)}let m=u*h;if(m<s)continue;let y=e[d]??0,g=e[d+1]??0,T=e[d+2]??0,k=e[d+3]??0;i.push({label:this.labels[p]??`class_${p}`,score:m,classId:p,box:{x:Math.max(0,y-T/2),y:Math.max(0,g-k/2),width:T,height:k},boxNormalized:{x:Math.max(0,y-T/2),y:Math.max(0,g-k/2),width:T,height:k}})}}else if(a===4)for(let c=0;c<r;c++){let l=c*a,d=e[l]??0,u=e[l+1]??0,h=e[l+2]??0,p=e[l+3]??0;i.push({label:this.labels[0]??"object",score:1,classId:0,box:{x:d,y:u,width:h-d,height:p-u},boxNormalized:{x:d,y:u,width:h-d,height:p-u}})}return i}nonMaxSuppression(e,n){if(e.length===0)return[];let s=[...e].sort((a,c)=>c.score-a.score),i=[],r=new Array(s.length).fill(!0);for(let a=0;a<s.length;a++){if(!r[a])continue;let c=s[a];i.push(c);for(let l=a+1;l<s.length;l++){if(!r[l])continue;let d=s[l];if(c.classId!==d.classId)continue;this.computeIoU(c.box,d.box)>n&&(r[l]=!1)}}return i}computeIoU(e,n){let s=Math.max(0,Math.min(e.x+e.width,n.x+n.width)-Math.max(e.x,n.x)),i=Math.max(0,Math.min(e.y+e.height,n.y+n.height)-Math.max(e.y,n.y)),r=s*i,a=e.width*e.height,c=n.width*n.height,l=a+c-r;return l>0?r/l:0}};N("object-detection",o=>new $e(o));C();G();var Rt={encoder:"https://huggingface.co/Xenova/whisper-tiny/resolve/main/onnx/encoder_model_quantized.onnx",decoder:"https://huggingface.co/Xenova/whisper-tiny/resolve/main/onnx/decoder_model_merged_quantized.onnx",tokenizer:"https://huggingface.co/Xenova/whisper-tiny/resolve/main/tokenizer.json"},fr=50258,pr=50358,mr=50359,gr=50257,yr=50363,Qn=50259,wr=448,Ue=class extends E{constructor(e){super(e??{task:"automatic-speech-recognition",model:"default"});f(this,"audioPreprocessor");f(this,"tokenizer",null);f(this,"encoderModel",null);f(this,"decoderModel",null);f(this,"encoderUrl");f(this,"decoderUrl");f(this,"tokenizerUrl");this.encoderUrl=Rt.encoder,this.decoderUrl=Rt.decoder,this.tokenizerUrl=Rt.tokenizer,this.audioPreprocessor=new fe({sampleRate:16e3,nMels:80,nFft:400,hopLength:160,maxDuration:30})}async initialize(){if(await super.initialize(),this.tokenizer||(this.tokenizer=await P.fromUrl(this.tokenizerUrl)),!this.encoderModel){let e=await v(this.encoderUrl,{cache:this.config.cache??!0});this.encoderModel=await F(e)}if(!this.decoderModel){let e=await v(this.decoderUrl,{cache:this.config.cache??!0});this.decoderModel=await F(e)}}setTokenizer(e){this.tokenizer=e}async run(e,n){await this.initialize();let s=Array.isArray(e),i=s?e:[e],r=n??{},a=[];for(let c of i){let l=await this.transcribeSingle(c,r);a.push(l)}return s?a:a[0]}async transcribeSingle(e,n){let s=performance.now(),i=await this.audioPreprocessor.process(e),r=new w(i.toFloat32Array(),[1,...i.shape],"float32"),c=(await X(this.encoderModel,[r]))[0],l=n.task??"transcribe",d=this.buildInitialTokens(l,n.language),u=await this.autoregressiveDecode(c,d),h=this.tokenizer.decode(u,!0),p={text:h.trim(),processingTime:performance.now()-s};return n.returnTimestamps&&(p.chunks=this.extractTimestamps(u,h)),p}buildInitialTokens(e,n){let s=[fr];return s.push(n?this.getLanguageToken(n):Qn),s.push(e==="translate"?pr:mr),s.push(yr),s}getLanguageToken(e){return{en:50259,zh:50260,de:50261,es:50262,ru:50263,ko:50264,fr:50265,ja:50266,pt:50267,tr:50268,pl:50269,ca:50270,nl:50271,ar:50272,sv:50273,it:50274,id:50275,hi:50276,fi:50277,vi:50278}[e.toLowerCase()]??Qn}async autoregressiveDecode(e,n){let s=[...n];for(let i=0;i<wr;i++){let r=new w(BigInt64Array.from(s.map(m=>BigInt(m))),[1,s.length],"int64"),a=new Map;a.set("input_ids",r),a.set("encoder_hidden_states",e);let l=(await U(this.decoderModel,a))[0].toFloat32Array(),d=l.length/s.length,u=l.slice((s.length-1)*d),h=0,p=u[0]??-1/0;for(let m=1;m<u.length;m++)(u[m]??-1/0)>p&&(p=u[m]??-1/0,h=m);if(h===gr)break;s.push(h)}return s.slice(n.length)}extractTimestamps(e,n){let s=n.split(/\s+/).filter(l=>l.length>0),i=[],r=2.5,a="",c=0;for(let l=0;l<s.length;l++)if(a+=(a?" ":"")+s[l],(l+1)%5===0||l===s.length-1){let d=a.split(/\s+/).length/r;i.push({text:a,start:c,end:c+d}),c=c+d,a=""}return i}async processLongAudio(e,n={}){let s=n.chunkDuration??30,i=n.chunkOverlap??5,a=(await this.audioPreprocessor.processRaw(e)).toFloat32Array(),c=16e3,l=s*c,d=i*c,u=l-d,h=[];for(let y=0;y<a.length;y+=u){let g=Math.min(y+l,a.length),T=a.slice(y,g),k=await this.run(new Float32Array(T),n);if(k.chunks){let b=y/c;k.chunks=k.chunks.map(z=>({...z,start:z.start+b,end:z.end+b}))}h.push(k)}let p=h.map(y=>y.text).join(" "),m=h.flatMap(y=>y.chunks??[]);return{text:p,chunks:m}}async preprocess(e){let n=Array.isArray(e)?e:[e],s=await Promise.all(n.map(i=>this.audioPreprocessor.process(i)));if(s.length===1){let i=s[0];return[new w(i.toFloat32Array(),[1,...i.shape],"float32")]}return s}async postprocess(e,n){let i=(n??{}).returnTimestamps??!1;if(!e[0])return{text:""};let r=e[0].toFloat32Array(),a=e[0].shape,c=this.decodeOutput(r,a),l={text:c};return i&&(l.chunks=this.extractTimestamps([],c)),l}decodeOutput(e,n){let s=n[1]??e.length,i=n[2]??1,r=[];if(i>1)for(let a=0;a<s;a++){let c=a*i,l=0,d=e[c]??-1/0;for(let u=1;u<i;u++)(e[c+u]??-1/0)>d&&(d=e[c+u]??-1/0,l=u);r.push(l)}else for(let a=0;a<e.length;a++)r.push(Math.round(e[a]??0));return this.tokenizer?this.tokenizer.decode(r,!0):r.join(" ")}};N("automatic-speech-recognition",o=>new Ue(o));C();G();var Kn={model:"https://huggingface.co/Xenova/nli-deberta-v3-small/resolve/main/onnx/model_quantized.onnx",tokenizer:"https://huggingface.co/Xenova/nli-deberta-v3-small/resolve/main/tokenizer.json"},xr=2,Be=class extends E{constructor(e){super(e??{task:"zero-shot-classification",model:"default"});f(this,"tokenizer",null);f(this,"onnxModel",null);f(this,"hypothesisTemplate","This text is about {label}.");f(this,"modelUrl");f(this,"tokenizerUrl");this.modelUrl=e?.model&&e.model!=="default"?e.model:Kn.model,this.tokenizerUrl=Kn.tokenizer}async initialize(){if(await super.initialize(),this.tokenizer||(this.tokenizer=await P.fromUrl(this.tokenizerUrl)),!this.onnxModel){let e=await v(this.modelUrl,{cache:this.config.cache??!0});this.onnxModel=await F(e)}}setTokenizer(e){this.tokenizer=e}async classify(e,n,s){return this.run({text:e,candidateLabels:n},s)}async run(e,n){await this.initialize();let{text:s,candidateLabels:i}=e,r=n??{},a=Array.isArray(s)?s:[s],c=r.hypothesisTemplate??this.hypothesisTemplate,l=r.multiLabel??!1,d=await Promise.all(a.map(u=>this.classifySingle(u,i,c,l)));return Array.isArray(s)?d:d[0]}async classifySingle(e,n,s,i){let r=performance.now(),a=n.map(u=>s.replace("{label}",u)),c=[];for(let u of a){let h=await this.scoreHypothesis(e,u);c.push(h)}let l;if(i)l=c.map(u=>1/(1+Math.exp(-u)));else{let u=new w(new Float32Array(c),[c.length],"float32");l=Array.from(O(u).toFloat32Array())}let d=n.map((u,h)=>({label:u,score:l[h]??0}));return d.sort((u,h)=>h.score-u.score),{sequence:e,labels:d.map(u=>u.label),scores:d.map(u=>u.score),processingTime:performance.now()-r}}async scoreHypothesis(e,n){let s=this.tokenizer.encode(e,{textPair:n,addSpecialTokens:!0,maxLength:512,truncation:!0,returnAttentionMask:!0}),i=new w(BigInt64Array.from(s.inputIds.map(d=>BigInt(d))),[1,s.inputIds.length],"int64"),r=new w(BigInt64Array.from(s.attentionMask.map(d=>BigInt(d))),[1,s.attentionMask.length],"int64"),a=new Map;return a.set("input_ids",i),a.set("attention_mask",r),(await U(this.onnxModel,a))[0].toFloat32Array()[xr]??0}async preprocess(e){let{text:n,candidateLabels:s}=e,i=Array.isArray(n)?n[0]??"":n,r=s[0]??"",a=this.tokenizer.encode(i,{textPair:this.hypothesisTemplate.replace("{label}",r),addSpecialTokens:!0,maxLength:512});return[new w(BigInt64Array.from(a.inputIds.map(c=>BigInt(c))),[1,a.inputIds.length],"int64")]}async postprocess(e,n){return{sequence:"",labels:[],scores:[]}}};N("zero-shot-classification",o=>new Be(o));C();G();var Yn={model:"https://huggingface.co/Xenova/distilbert-base-cased-distilled-squad/resolve/main/onnx/model_quantized.onnx",tokenizer:"https://huggingface.co/Xenova/distilbert-base-cased-distilled-squad/resolve/main/tokenizer.json"},qe=class extends E{constructor(e){super(e??{task:"question-answering",model:"default"});f(this,"tokenizer",null);f(this,"onnxModel",null);f(this,"modelUrl");f(this,"tokenizerUrl");this.modelUrl=e?.model&&e.model!=="default"?e.model:Yn.model,this.tokenizerUrl=Yn.tokenizer}async initialize(){if(await super.initialize(),this.tokenizer||(this.tokenizer=await P.fromUrl(this.tokenizerUrl)),!this.onnxModel){let e=await v(this.modelUrl,{cache:this.config.cache??!0});this.onnxModel=await F(e)}}setTokenizer(e){this.tokenizer=e}async run(e,n){await this.initialize();let s=Array.isArray(e)?e:[e],i=await Promise.all(s.map(r=>this.answerQuestion(r,n??{})));return Array.isArray(e)?i:i[0]}async answerQuestion(e,n){let s=performance.now(),{question:i,context:r}=e,a=n.maxAnswerLength??30,c=this.tokenizer.encode(i,{textPair:r,addSpecialTokens:!0,maxLength:512,truncation:!0,padding:"do_not_pad",returnAttentionMask:!0,returnTokenTypeIds:!0}),l=c.inputIds.length,d=new w(BigInt64Array.from(c.inputIds.map($=>BigInt($))),[1,l],"int64"),u=new w(BigInt64Array.from(c.attentionMask.map($=>BigInt($))),[1,l],"int64"),h=new Map;h.set("input_ids",d),h.set("attention_mask",u);let p=await U(this.onnxModel,h);if(p.length<2)return{answer:"",score:0,start:0,end:0,processingTime:performance.now()-s};let m=p[0].toFloat32Array(),y=p[1].toFloat32Array(),g=O(new w(new Float32Array(m),[l],"float32")).toFloat32Array(),T=O(new w(new Float32Array(y),[l],"float32")).toFloat32Array(),b=(c.tokenTypeIds??new Array(l).fill(1)).findIndex($=>$===1),z=b>=0?b:0,I=l-1,S=z,D=z,_=-1/0;for(let $=z;$<=I;$++)for(let ge=$;ge<Math.min($+a,I+1);ge++){let L=(g[$]??0)*(T[ge]??0);L>_&&(_=L,S=$,D=ge)}let A=c.inputIds.slice(S,D+1);return{answer:this.tokenizer.decode(A,!0)||"",score:Math.max(0,_),start:S,end:D,processingTime:performance.now()-s}}async preprocess(e){let n=Array.isArray(e)?e[0]:e,s=this.tokenizer.encode(n.question,{textPair:n.context,addSpecialTokens:!0,maxLength:512,truncation:!0,returnAttentionMask:!0,returnTokenTypeIds:!0});return[new w(BigInt64Array.from(s.inputIds.map(i=>BigInt(i))),[1,s.inputIds.length],"int64"),new w(BigInt64Array.from(s.attentionMask.map(i=>BigInt(i))),[1,s.attentionMask.length],"int64")]}async postprocess(e,n){if(e.length<2)return{answer:"",score:0,start:0,end:0};let s=e[0].toFloat32Array(),i=e[1].toFloat32Array(),r=s.length,a=O(new w(s,[r],"float32")).toFloat32Array(),c=O(new w(i,[r],"float32")).toFloat32Array(),l=0,d=0,u=0;for(let h=0;h<r;h++)for(let p=h;p<Math.min(h+30,r);p++){let m=(a[h]??0)*(c[p]??0);m>u&&(u=m,l=h,d=p)}return{answer:"",score:u,start:l,end:d}}};N("question-answering",o=>new qe(o));C();var Jn={encoder:"https://huggingface.co/Xenova/slimsam-77-uniform/resolve/main/onnx/vision_encoder_quantized.onnx",decoder:"https://huggingface.co/Xenova/slimsam-77-uniform/resolve/main/onnx/prompt_encoder_mask_decoder_quantized.onnx"},re=class extends E{constructor(e){super(e);f(this,"encoderModel",null);f(this,"decoderModel",null);f(this,"imageEmbedding",null);f(this,"imagePositionalEmbedding",null);f(this,"currentImageSize",null);f(this,"resizedImageSize",null);f(this,"inputSize",1024);f(this,"modelsLoaded",!1);f(this,"encoderUrl");f(this,"decoderUrl");this.encoderUrl=Jn.encoder,this.decoderUrl=Jn.decoder}get isModelsLoaded(){return this.modelsLoaded}setModelUrls(e,n){this.encoderUrl=e,this.decoderUrl=n}async loadModels(e){if(this.modelsLoaded)return;e?.({model:"encoder",loaded:0,total:100,progress:0});let n=await this.fetchModelWithProgress(this.encoderUrl,(i,r)=>{e?.({model:"encoder",loaded:i,total:r,progress:Math.round(i/r*100)})});this.encoderModel=await F(n,{runtime:"wasm"}),e?.({model:"decoder",loaded:0,total:100,progress:0});let s=await this.fetchModelWithProgress(this.decoderUrl,(i,r)=>{e?.({model:"decoder",loaded:i,total:r,progress:Math.round(i/r*100)})});this.decoderModel=await F(s,{runtime:"wasm"}),this.modelsLoaded=!0}async fetchModelWithProgress(e,n){let s=await fetch(e);if(!s.ok)throw new Error(`Failed to fetch model: ${s.status} ${s.statusText}`);let i=s.headers.get("content-length"),r=i?parseInt(i,10):0;if(!s.body){let h=await s.arrayBuffer();return n(h.byteLength,h.byteLength),h}let a=s.body.getReader(),c=[],l=0;for(;;){let{done:h,value:p}=await a.read();if(h)break;c.push(p),l+=p.length,n(l,r||l)}let d=new Uint8Array(l),u=0;for(let h of c)d.set(h,u),u+=h.length;return d.buffer}async initialize(){this.isReady||(this.isReady=!0)}async loadEncoder(e){this.encoderModel=await we(e,{runtime:"wasm"})}async loadDecoder(e){this.decoderModel=await we(e,{runtime:"wasm"})}async setImage(e){if(!this.modelsLoaded)throw new Error("Models not loaded. Call loadModels() first.");let n=await this.loadImage(e);this.currentImageSize={width:n.width,height:n.height};let{tensor:s,resizedSize:i}=this.preprocessImage(n);if(this.resizedImageSize=i,this.encoderModel){let r=await X(this.encoderModel,[s]);this.imageEmbedding=r[0],this.imagePositionalEmbedding=r[1],console.log("[SAM] Encoder outputs:",r.length),console.log("[SAM] image_embeddings shape:",this.imageEmbedding.shape),this.imagePositionalEmbedding&&console.log("[SAM] image_positional_embeddings shape:",this.imagePositionalEmbedding.shape)}else throw new Error("Encoder model not loaded")}async segment(e={}){if(!this.imageEmbedding||!this.currentImageSize||!this.resizedImageSize)throw new Error("No image set. Call setImage() first.");if(!this.decoderModel)throw new Error("Decoder model not loaded");let n=performance.now(),{points:s=[],boxes:i=[],maskThreshold:r=0,returnAllMasks:a=!1}=e,c=this.prepareDecoderInputs(s,i);if(c.set("image_embeddings",this.imageEmbedding),this.imagePositionalEmbedding)c.set("image_positional_embeddings",this.imagePositionalEmbedding);else throw new Error("image_positional_embeddings not available from encoder");let l=await U(this.decoderModel,c),d=l[0],u=l[1],h=this.postprocessMasks(d,u,r,a);return h.processingTime=performance.now()-n,h}async run(e,n){return await this.setImage(e),this.segment(n)}async loadImage(e){if(typeof e=="string")return this.loadImageFromUrl(e);if(e instanceof HTMLImageElement)return this.imageElementToImageData(e);if(e instanceof HTMLCanvasElement)return this.canvasToImageData(e);if(e instanceof ImageData)return e;if(typeof ImageBitmap<"u"&&e instanceof ImageBitmap)return this.imageBitmapToImageData(e);throw new Error("Unsupported image input type")}async loadImageFromUrl(e){return new Promise((n,s)=>{let i=new Image;i.crossOrigin="anonymous",i.onload=()=>{let r=document.createElement("canvas");r.width=i.width,r.height=i.height;let a=r.getContext("2d");a.drawImage(i,0,0),n(a.getImageData(0,0,i.width,i.height))},i.onerror=s,i.src=e})}imageElementToImageData(e){let n=document.createElement("canvas");n.width=e.naturalWidth||e.width,n.height=e.naturalHeight||e.height;let s=n.getContext("2d");return s.drawImage(e,0,0),s.getImageData(0,0,n.width,n.height)}canvasToImageData(e){return e.getContext("2d").getImageData(0,0,e.width,e.height)}imageBitmapToImageData(e){let n=document.createElement("canvas");n.width=e.width,n.height=e.height;let s=n.getContext("2d");return s.drawImage(e,0,0),s.getImageData(0,0,n.width,n.height)}preprocessImage(e){let{width:n,height:s}=e,i=this.inputSize/Math.max(n,s),r=Math.round(n*i),a=Math.round(s*i),c=document.createElement("canvas");c.width=this.inputSize,c.height=this.inputSize;let l=c.getContext("2d");l.fillStyle="rgb(123.675, 116.28, 103.53)",l.fillRect(0,0,this.inputSize,this.inputSize);let d=document.createElement("canvas");d.width=n,d.height=s,d.getContext("2d").putImageData(e,0,0),l.drawImage(d,0,0,r,a);let h=l.getImageData(0,0,this.inputSize,this.inputSize),p=new Float32Array(3*this.inputSize*this.inputSize),m=[123.675,116.28,103.53],y=[58.395,57.12,57.375];for(let g=0;g<this.inputSize*this.inputSize;g++){let T=g*4;p[g]=(h.data[T]-m[0])/y[0],p[this.inputSize*this.inputSize+g]=(h.data[T+1]-m[1])/y[1],p[2*this.inputSize*this.inputSize+g]=(h.data[T+2]-m[2])/y[2]}return{tensor:new w(p,[1,3,this.inputSize,this.inputSize],"float32"),resizedSize:{width:r,height:a}}}prepareDecoderInputs(e,n){let{width:s,height:i}=this.resizedImageSize,r=s,a=i,c=[],l=[];for(let h of e)c.push(h.x*r,h.y*a),l.push(h.label);for(let h of n)c.push(h.x1*r,h.y1*a),l.push(2),c.push(h.x2*r,h.y2*a),l.push(3);c.length===0&&(c.push(s/2,i/2),l.push(1));let d=l.length,u=new Map;return u.set("input_points",new w(new Float32Array(c),[1,1,d,2],"float32")),u.set("input_labels",new w(BigInt64Array.from(l.map(h=>BigInt(h))),[1,1,d],"int64")),u}postprocessMasks(e,n,s,i){let{width:r,height:a}=this.currentImageSize,c=n.toFloat32Array(),l=e.toFloat32Array(),d=c.length,u=e.shape,h=u[2]??a,p=u[3]??r,m=0,y=c[0]??0;for(let k=1;k<d;k++)(c[k]??0)>y&&(y=c[k]??0,m=k);let T={mask:this.resizeMask(l,m,p,h,r,a,s),width:r,height:a,score:y};if(i&&d>1){T.allMasks=[];for(let k=0;k<d;k++){let b=this.resizeMask(l,k,p,h,r,a,s);T.allMasks.push({mask:b,score:c[k]??0})}}return T}resizeMask(e,n,s,i,r,a,c){let l=new Uint8Array(r*a),d=n*s*i;for(let u=0;u<a;u++)for(let h=0;h<r;h++){let p=h/r*s,m=u/a*i,y=Math.floor(p),g=Math.min(y+1,s-1),T=Math.floor(m),k=Math.min(T+1,i-1),b=p-y,z=m-T,I=e[d+T*s+y]??0,S=e[d+T*s+g]??0,D=e[d+k*s+y]??0,_=e[d+k*s+g]??0,A=I*(1-b)*(1-z)+S*b*(1-z)+D*(1-b)*z+_*b*z,me=1/(1+Math.exp(-A));l[u*r+h]=me>c?255:0}return l}clearImage(){this.imageEmbedding=null,this.imagePositionalEmbedding=null,this.currentImageSize=null,this.resizedImageSize=null}async preprocess(e){let n=await this.loadImage(e),{tensor:s}=this.preprocessImage(n);return[s]}async postprocess(e,n){return{mask:new Uint8Array(0),width:0,height:0,score:0}}dispose(){super.dispose(),this.encoderModel?.dispose(),this.decoderModel?.dispose(),this.imageEmbedding=null,this.imagePositionalEmbedding=null,this.currentImageSize=null,this.resizedImageSize=null,this.modelsLoaded=!1}};function Zn(o={}){return new re({task:"image-segmentation",model:o.model??"slimsam",runtime:o.runtime,cache:o.cache??!0,quantization:o.quantization})}N("image-segmentation",o=>new re(o));async function je(o,t){st();let e={task:o,model:t?.model??"default",runtime:t?.runtime,cache:t?.cache??!0,quantization:t?.quantization},n;switch(o){case"text-classification":n=new Y(e,t?.labels);break;case"sentiment-analysis":n=new te(e);break;case"feature-extraction":n=new ne(e);break;case"image-classification":n=new se(e,t?.labels);break;case"text-generation":n=new pe(e);break;case"object-detection":n=new $e(e,t?.labels);break;case"automatic-speech-recognition":n=new Ue(e);break;case"zero-shot-classification":n=new Be(e);break;case"question-answering":n=new qe(e);break;case"image-segmentation":n=new re(e);break;default:{let s=_t(o);if(s){n=s.factory(e);break}throw new Error(`Unknown pipeline task: "${o}". Register a plugin with registerPlugin() to add custom pipeline tasks.`)}}return await n.initialize(),n}async function Mr(o,t){let e=await Promise.all(o.map(s=>je(s,t))),n={};for(let s=0;s<o.length;s++){let i=o[s];n[i]=e[s]}return n}function Tr(o){if(o.length===0)throw new Error("[edgeFlow.js] compose() requires at least one stage");let t=null;async function e(){return t||(t=await Promise.all(o.map(n=>je(n.task,{model:n.model,...n.options}))),t)}return{get length(){return o.length},async run(n){let s=await e(),i=[],r=[],a=n,c=performance.now();for(let l=0;l<o.length;l++){let d=o[l],u=s[l];d.transform&&(a=d.transform(a));let h=performance.now();a=await u.run(a,d.runOptions),r.push(performance.now()-h),i.push(a)}return{output:a,stages:i,totalTime:performance.now()-c,stageTimes:r}},dispose(){if(t){for(let n of t)n&&typeof n.dispose=="function"&&n.dispose();t=null}}}}function br(o){if(o.length===0)throw new Error("[edgeFlow.js] parallel() requires at least one stage");let t=null;async function e(){return t||(t=await Promise.all(o.map(n=>je(n.task,{model:n.model,...n.options}))),t)}return{async run(n){let s=await e(),i=performance.now();return{outputs:await Promise.all(o.map((a,c)=>{let l=a.transform?a.transform(n):n;return s[c].run(l,a.runOptions)})),totalTime:performance.now()-i}},dispose(){if(t){for(let n of t)n&&typeof n.dispose=="function"&&n.dispose();t=null}}}}G();G();B();var kr="https://huggingface.co",Ir="main",es=["model.onnx","model_quantized.onnx","model_int8.onnx","model_uint8.onnx","model_fp16.onnx","onnx/model.onnx","onnx/model_quantized.onnx"];function ct(o,t,e={}){let n=e.endpoint??kr,s=e.revision??Ir,i=e.subfolder?`${e.subfolder}/`:"";return`${n}/${o}/resolve/${s}/${i}${t}`}async function ts(o,t){let e={};return t&&(e.Authorization=`Bearer ${t}`),await fetch(o,{headers:e})}async function ns(o,t,e={}){let n=ct(o,t,e);try{let s=await ts(n,e.token);return s.ok||s.status===302}catch{return!1}}async function $t(o,t={}){for(let e of es)if(await ns(o,e,t))return e;return null}async function ss(o,t,e={}){let n=ct(o,t,e);return v(n,{cache:e.cache??!0,forceDownload:e.forceDownload??!1,onProgress:e.onProgress?s=>{e.onProgress({file:t,fileIndex:1,totalFiles:1,fileProgress:s,overallProgress:s.percent})}:void 0})}async function Ar(o,t,e={}){let n=ct(o,t,e);if(e.cache!==!1&&!e.forceDownload&&await ve(n)){let r=await v(n,{cache:!0}),a=new TextDecoder().decode(r);return JSON.parse(a)}let s=await ts(n,e.token);if(!s.ok)throw new x(`Failed to download ${t} from ${o}: ${s.status}`,M.MODEL_NOT_FOUND);return s.json()}async function Ut(o,t={}){let e=ct(o,"tokenizer.json",t);return P.fromUrl(e)}async function lt(o,t={}){return Ar(o,"config.json",t)}async function dt(o,t={}){let e={},s=0,i=(d,u)=>{if(t.onProgress){let h=s/3*100,p=u.percent/3;t.onProgress({file:d,fileIndex:s+1,totalFiles:3,fileProgress:u,overallProgress:h+p})}};console.log(`\u{1F50D} Finding ONNX model in ${o}...`);let r=await $t(o,t);if(!r)throw new x(`No ONNX model found in ${o}. Please ensure the model has an ONNX file.`,M.MODEL_NOT_FOUND,{modelId:o,triedFiles:es});e.model=r,console.log(`\u{1F4E6} Downloading model: ${r}`);let a=await ss(o,r,{...t,onProgress:d=>i(r,d.fileProgress)});s=1;let c;try{console.log("\u{1F4DD} Downloading tokenizer..."),e.tokenizer="tokenizer.json",c=await Ut(o,t),console.log("\u2713 Tokenizer loaded")}catch{console.warn(`\u26A0\uFE0F No tokenizer found for ${o}`)}s=2;let l;try{console.log("\u2699\uFE0F Downloading config..."),e.config="config.json",l=await lt(o,t),console.log("\u2713 Config loaded")}catch{console.warn(`\u26A0\uFE0F No config found for ${o}`)}return s=3,t.onProgress&&t.onProgress({file:"complete",fileIndex:3,totalFiles:3,fileProgress:{loaded:1,total:1,percent:100,speed:0,eta:0},overallProgress:100}),console.log(`\u2705 Model bundle downloaded: ${o}`),{modelId:o,modelData:a,tokenizer:c,config:l,files:e}}async function rs(o,t={}){return dt(o,t)}async function os(o,t={}){try{return await $t(o,t)!==null}catch{return!1}}async function is(o,t={}){let[e,n,s]=await Promise.all([$t(o,t),ns(o,"tokenizer.json",t),lt(o,t).catch(()=>{})]);return{hasOnnx:e!==null,onnxFile:e??void 0,hasTokenizer:n,hasConfig:s!==void 0,config:s}}var Bt={"sentiment-analysis":"Xenova/distilbert-base-uncased-finetuned-sst-2-english","text-classification":"Xenova/distilbert-base-uncased-finetuned-sst-2-english","feature-extraction":"Xenova/all-MiniLM-L6-v2","sentence-similarity":"Xenova/all-MiniLM-L6-v2","question-answering":"Xenova/distilbert-base-cased-distilled-squad",ner:"Xenova/bert-base-NER","token-classification":"Xenova/bert-base-NER","text-generation":"Xenova/gpt2","translation-en-fr":"Xenova/t5-small","translation-en-de":"Xenova/t5-small",summarization:"Xenova/distilbart-cnn-6-6","fill-mask":"Xenova/bert-base-uncased","image-classification":"Xenova/vit-base-patch16-224","object-detection":"Xenova/detr-resnet-50","image-segmentation":"Xenova/segformer-b0-finetuned-ade-512-512","zero-shot-classification":"Xenova/mobilebert-uncased-mnli","automatic-speech-recognition":"Xenova/whisper-tiny.en","text-to-speech":"Xenova/speecht5_tts"};function qt(o){return Bt[o]}async function as(o,t={}){let e=qt(o);return dt(e,t)}async function He(o,t={}){let{warmupRuns:e=3,runs:n=10,verbose:s=!1,timeout:i=3e4,name:r="benchmark"}=t,a=[],c=0;s&&console.log(`[${r}] Running ${e} warmup iterations...`);for(let y=0;y<e;y++)try{await Promise.race([Promise.resolve(o()),new Promise((g,T)=>setTimeout(()=>T(new Error("Timeout")),i))])}catch{}s&&console.log(`[${r}] Running ${n} measured iterations...`);for(let y=0;y<n;y++)try{let g=performance.now();await Promise.race([Promise.resolve(o()),new Promise((k,b)=>setTimeout(()=>b(new Error("Timeout")),i))]);let T=performance.now();a.push(T-g),s&&console.log(` Run ${y+1}: ${(T-g).toFixed(2)}ms`)}catch(g){c++,s&&console.log(` Run ${y+1}: FAILED - ${g}`)}if(a.length===0)throw new Error(`All ${n} runs failed`);let l=[...a].sort((y,g)=>y-g),u=a.reduce((y,g)=>y+g,0)/a.length,h=a.reduce((y,g)=>y+Math.pow(g-u,2),0)/a.length,p=Math.sqrt(h),m={name:r,avgTime:u,medianTime:l[Math.floor(l.length/2)]??0,minTime:l[0]??0,maxTime:l[l.length-1]??0,stdDev:p,p95:l[Math.floor(l.length*.95)]??l[l.length-1]??0,p99:l[Math.floor(l.length*.99)]??l[l.length-1]??0,throughput:1e3/u,times:a,totalRuns:n,failedRuns:c};return s&&(console.log(`
|
|
52
|
+
[${r}] Results:`),console.log(` Avg: ${m.avgTime.toFixed(2)}ms`),console.log(` Median: ${m.medianTime.toFixed(2)}ms`),console.log(` Min: ${m.minTime.toFixed(2)}ms`),console.log(` Max: ${m.maxTime.toFixed(2)}ms`),console.log(` Std Dev: ${m.stdDev.toFixed(2)}ms`),console.log(` P95: ${m.p95.toFixed(2)}ms`),console.log(` Throughput: ${m.throughput.toFixed(2)} ops/sec`)),m}async function cs(o,t,e={}){let n=await He(o,{...e,name:e.name?`${e.name} (baseline)`:"baseline"}),s=await He(t,{...e,name:e.name?`${e.name} (comparison)`:"comparison"}),i=n.avgTime/s.avgTime,r=(n.avgTime-s.avgTime)/n.avgTime*100,a;return Math.abs(r)<5?a="tie":r>0?a="comparison":a="baseline",{baseline:n,comparison:s,speedup:i,percentFaster:r,winner:a}}async function ls(o,t={}){let e={};for(let[n,s]of Object.entries(o))console.log(`
|
|
53
|
+
=== ${n} ===`),e[n]=await He(s,{...t,name:n,verbose:!0});return e}function ds(o){return`
|
|
54
54
|
\u250C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510
|
|
55
55
|
\u2502 ${o.name.padEnd(39)} \u2502
|
|
56
56
|
\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524
|
|
@@ -64,7 +64,7 @@ Human:`]}),r=Array.isArray(i)?i[0]:i;return this.conversationHistory.push({role:
|
|
|
64
64
|
\u2502 Throughput: ${o.throughput.toFixed(2).padStart(10)} ops/sec \u2502
|
|
65
65
|
\u2502 Runs: ${o.totalRuns.toString().padStart(10)} (${o.failedRuns} failed) \u2502
|
|
66
66
|
\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518
|
|
67
|
-
`.trim()}function
|
|
67
|
+
`.trim()}function us(o){let t=o.percentFaster>0?"\u2191":o.percentFaster<0?"\u2193":"=",e=o.winner==="comparison"?"Comparison is faster!":o.winner==="baseline"?"Baseline is faster!":"Results are similar";return`
|
|
68
68
|
\u250C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510
|
|
69
69
|
\u2502 BENCHMARK COMPARISON \u2502
|
|
70
70
|
\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524
|
|
@@ -76,12 +76,12 @@ Human:`]}),r=Array.isArray(i)?i[0]:i;return this.conversationHistory.push({role:
|
|
|
76
76
|
\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524
|
|
77
77
|
\u2502 Winner: ${e.padEnd(42)} \u2502
|
|
78
78
|
\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518
|
|
79
|
-
`.trim()}async function as(o,t={}){let{name:e="memory-benchmark",runs:n=5}=t,s=()=>typeof performance<"u"&&"memory"in performance?performance.memory.usedJSHeapSize:0,i=[],r=s();for(let d=0;d<n;d++)await o(),i.push(s());let a=Math.max(...i),c=i.reduce((d,u)=>d+u,0)/i.length,l=c-r;return{name:e,peakMemory:a,avgMemory:c,memoryDelta:l}}B();C();function cs(o,t,e,n,s=0,i=[]){let r=e?-(1<<t-1):0,a=e?(1<<t-1)-1:(1<<t)-1;if(n&&i.length>1){let c=i[s]??1,l=new Float32Array(c),d=new Int32Array(c),u=o.length/c,h=1/0,p=-1/0;for(let m=0;m<c;m++){let y=1/0,g=-1/0;for(let T=0;T<u;T++){let k=m*u+T,b=o[k]??0;y=Math.min(y,b),g=Math.max(g,b)}if(h=Math.min(h,y),p=Math.max(p,g),e){let T=Math.max(Math.abs(y),Math.abs(g));l[m]=T/a,d[m]=0}else l[m]=(g-y)/(a-r),d[m]=Math.round(r-y/(l[m]||1));l[m]===0&&(l[m]=1)}return{scale:l,zeroPoint:d,min:h,max:p}}else{let c=1/0,l=-1/0;for(let h=0;h<o.length;h++){let p=o[h]??0;c=Math.min(c,p),l=Math.max(l,p)}let d,u;return e?(d=Math.max(Math.abs(c),Math.abs(l))/a,u=0):(d=(l-c)/(a-r),u=Math.round(r-c/(d||1))),d===0&&(d=1),{scale:d,zeroPoint:u,min:c,max:l}}}function ct(o,t,e,n,s=o.length){let i=new Int8Array(o.length);if(n&&t instanceof Float32Array){let r=t.length;for(let a=0;a<r;a++){let c=t[a]??1,l=e[a]??0;for(let d=0;d<s;d++){let u=a*s+d,h=o[u]??0;i[u]=Math.max(-128,Math.min(127,Math.round(h/c+l)))}}}else{let r=t,a=e;for(let c=0;c<o.length;c++){let l=o[c]??0;i[c]=Math.max(-128,Math.min(127,Math.round(l/r+a)))}}return i}function ls(o,t,e,n,s=o.length){let i=new Uint8Array(o.length);if(n&&t instanceof Float32Array){let r=t.length;for(let a=0;a<r;a++){let c=t[a]??1,l=e[a]??0;for(let d=0;d<s;d++){let u=a*s+d,h=o[u]??0;i[u]=Math.max(0,Math.min(255,Math.round(h/c+l)))}}}else{let r=t,a=e;for(let c=0;c<o.length;c++){let l=o[c]??0;i[c]=Math.max(0,Math.min(255,Math.round(l/r+a)))}}return i}function br(o,t,e){let n=Math.ceil(o.length/2),s=new Uint8Array(n);for(let i=0;i<o.length;i+=2){let r=o[i]??0,a=o[i+1]??0,c=Math.max(0,Math.min(15,Math.round(r/t+e+8))),l=Math.max(0,Math.min(15,Math.round(a/t+e+8)));s[i>>1]=c<<4|l}return s}function ds(o){let t=new Uint16Array(o.length);for(let e=0;e<o.length;e++)t[e]=kr(o[e]??0);return t}function kr(o){let t=new Float32Array(1),e=new Int32Array(t.buffer);t[0]=o;let n=e[0],s=n>>16&32768,i=(n>>23&255)-127+15,r=n&8388607;if(i<=0){if(i<-10)return s;let a=(r|8388608)>>1-i;return s|a>>13}else if(i>=31)return s|31744;return s|i<<10|r>>13}function lt(o,t,e,n=!1,s=o.length){let i=new Float32Array(o.length);if(n&&t instanceof Float32Array){let r=t.length;for(let a=0;a<r;a++){let c=t[a]??1,l=e[a]??0;for(let d=0;d<s;d++){let u=a*s+d;i[u]=((o[u]??0)-l)*c}}}else{let r=t,a=e;for(let c=0;c<o.length;c++)i[c]=((o[c]??0)-a)*r}return i}function Rt(o,t,e,n=!1,s=o.length){let i=new Float32Array(o.length);if(n&&t instanceof Float32Array){let r=t.length;for(let a=0;a<r;a++){let c=t[a]??1,l=e[a]??0;for(let d=0;d<s;d++){let u=a*s+d;i[u]=((o[u]??0)-l)*c}}}else{let r=t,a=e;for(let c=0;c<o.length;c++)i[c]=((o[c]??0)-a)*r}return i}function Ut(o){let t=(o&32768)>>15,e=(o&31744)>>10,n=o&1023;return e===0?n===0?t===0?0:-0:(t===0?1:-1)*Math.pow(2,-14)*(n/1024):e===31?n===0?t===0?1/0:-1/0:NaN:(t===0?1:-1)*Math.pow(2,e-15)*(1+n/1024)}function Bt(o){let t=new Float32Array(o.length);for(let e=0;e<o.length;e++)t[e]=Ut(o[e]??0);return t}function qt(o){let t=[],e=new Float32Array(o);return t.push({name:"model_weights",data:e,shape:[e.length],dtype:"float32"}),t}function Ir(o){let t=new TextEncoder,e=20;for(let a of o.weights){let c=t.encode(a.name),l=t.encode(a.dtype),d=t.encode(a.originalDtype);e+=4+c.length,e+=4+a.shape.length*4,e+=4+l.length,e+=4+d.length,e+=1,a.scale!==void 0&&(e+=Array.isArray(a.scale)?4+a.scale.length*4:4),e+=1,a.zeroPoint!==void 0&&(e+=Array.isArray(a.zeroPoint)?4+a.zeroPoint.length*4:4),e+=8+a.data.byteLength}let n=new ArrayBuffer(e),s=new DataView(n),i=new Uint8Array(n),r=0;s.setUint32(r,o.version,!0),r+=4,s.setUint32(r,["int8","uint8","int4","float16","dynamic"].indexOf(o.quantizationType),!0),r+=4,s.setUint32(r,o.originalSize&4294967295,!0),r+=4,s.setUint32(r,o.originalSize/4294967296>>>0,!0),r+=4,s.setUint32(r,o.weights.length,!0),r+=4;for(let a of o.weights){let c=t.encode(a.name),l=t.encode(a.dtype),d=t.encode(a.originalDtype);s.setUint32(r,c.length,!0),r+=4,i.set(c,r),r+=c.length,s.setUint32(r,a.shape.length,!0),r+=4;for(let p of a.shape)s.setInt32(r,p,!0),r+=4;if(s.setUint32(r,l.length,!0),r+=4,i.set(l,r),r+=l.length,s.setUint32(r,d.length,!0),r+=4,i.set(d,r),r+=d.length,a.scale!==void 0)if(s.setUint8(r,1),r+=1,Array.isArray(a.scale)){s.setUint32(r,a.scale.length,!0),r+=4;for(let p of a.scale)s.setFloat32(r,p,!0),r+=4}else s.setUint32(r,1,!0),r+=4,s.setFloat32(r,a.scale,!0),r+=4;else s.setUint8(r,0),r+=1;if(a.zeroPoint!==void 0)if(s.setUint8(r,1),r+=1,Array.isArray(a.zeroPoint)){s.setUint32(r,a.zeroPoint.length,!0),r+=4;for(let p of a.zeroPoint)s.setInt32(r,p,!0),r+=4}else s.setUint32(r,1,!0),r+=4,s.setInt32(r,a.zeroPoint,!0),r+=4;else s.setUint8(r,0),r+=1;let u=a.data.byteLength&4294967295,h=a.data.byteLength/4294967296>>>0;s.setUint32(r,u,!0),r+=4,s.setUint32(r,h,!0),r+=4,i.set(new Uint8Array(a.data),r),r+=a.data.byteLength}return n}async function jt(o,t){let{type:e,skipPatterns:n=[],perChannel:s=!1,symmetric:i=!0,onProgress:r,minTensorSize:a=100}=t,c=o.byteLength,l=[],d=0,u=0;r?.({stage:"analyzing",current:0,total:1,percent:0});let h=qt(o),p=[],m=0,y=0,g=[];for(let _=0;_<h.length;_++){let A=h[_],me=(_+1)/h.length*100;if(r?.({stage:"quantizing",current:_+1,total:h.length,percent:me,layerName:A.name}),m+=A.data.length,A.data.length<a||n.some(We=>typeof We=="string"?A.name.includes(We):We.test(A.name))){u++,l.push({name:A.name,originalDtype:A.dtype,quantizedDtype:A.dtype,originalSize:A.data.byteLength,quantizedSize:A.data.byteLength,scale:1,zeroPoint:0,minValue:Math.min(...A.data),maxValue:Math.max(...A.data),skipped:!0,skipReason:A.data.length<a?"Tensor too small":"Matched skip pattern"}),p.push({name:A.name,data:A.data.buffer.slice(0),shape:A.shape,dtype:A.dtype,originalDtype:A.dtype});continue}let ge=e==="int4"?4:8,L=cs(A.data,ge,i,s,0,A.shape),oe,ie;switch(e){case"int8":oe=ct(A.data,L.scale,L.zeroPoint,s,s?A.data.length/(A.shape[0]??1):A.data.length).buffer.slice(0),ie="int8";break;case"uint8":oe=ls(A.data,L.scale,L.zeroPoint,s,s?A.data.length/(A.shape[0]??1):A.data.length).buffer.slice(0),ie="uint8";break;case"int4":oe=br(A.data,L.scale,L.zeroPoint).buffer.slice(0),ie="int4";break;case"float16":oe=ds(A.data).buffer.slice(0),ie="float16";break;case"dynamic":default:oe=ct(A.data,L.scale,L.zeroPoint,s,s?A.data.length/(A.shape[0]??1):A.data.length).buffer.slice(0),ie="int8";break}d++,y+=A.data.length;let be=L.scale instanceof Float32Array?Array.from(L.scale):L.scale,Qt=L.zeroPoint instanceof Int32Array?Array.from(L.zeroPoint):L.zeroPoint;typeof be=="number"?g.push(be):g.push(...be),l.push({name:A.name,originalDtype:A.dtype,quantizedDtype:ie,originalSize:A.data.byteLength,quantizedSize:oe.byteLength,scale:be,zeroPoint:Qt,minValue:L.min,maxValue:L.max,skipped:!1}),p.push({name:A.name,data:oe,shape:A.shape,dtype:ie,originalDtype:A.dtype,scale:be,zeroPoint:Qt})}r?.({stage:"packing",current:0,total:1,percent:0});let k=Ir({version:1,quantizationType:e,originalSize:c,weights:p});r?.({stage:"complete",current:1,total:1,percent:100});let b=g.length>0?g.reduce((_,A)=>_+A,0)/g.length:1,z=g.length>0?Math.min(...g):1,I=g.length>0?Math.max(...g):1,D=b/(e==="int4"?8:e==="float16"?2:4);return{data:k,originalSize:c,quantizedSize:k.byteLength,compressionRatio:c/k.byteLength,tensorsQuantized:d,tensorsSkipped:u,layerStats:l,stats:{totalParameters:m,quantizedParameters:y,averageScale:b,minScale:z,maxScale:I,errorEstimate:D}}}function us(o,t,e={}){let{symmetric:n=!0,perChannel:s=!1}=e,i=o.toFloat32Array(),r=o.shape,c=cs(i,t==="int4"?4:8,n,s,0,r),l,d;switch(t){case"int8":l=ct(i,c.scale,c.zeroPoint,s),d="int32";break;case"uint8":l=ls(i,c.scale,c.zeroPoint,s),d="int32";break;case"float16":l=ds(i),d="float32";break;default:l=ct(i,c.scale,c.zeroPoint,s),d="int32"}let u=c.scale instanceof Float32Array?Array.from(c.scale):c.scale,h=c.zeroPoint instanceof Int32Array?Array.from(c.zeroPoint):c.zeroPoint;return{tensor:new w(Array.from(l),r,d),scale:u,zeroPoint:h}}function hs(o,t,e,n){let s=o.toArray(),i=o.shape,r,a=Array.isArray(t)?new Float32Array(t):t,c=Array.isArray(e)?new Int32Array(e):e,l=Array.isArray(t);switch(n){case"int8":r=lt(new Int8Array(s.map(Number)),a,c,l);break;case"uint8":r=Rt(new Uint8Array(s.map(Number)),a,c,l);break;case"float16":r=Bt(new Uint16Array(s.map(Number)));break;default:r=lt(new Int8Array(s.map(Number)),a,c,l)}return new w(Array.from(r),i,"float32")}function Ht(o,t={}){let{ratio:e=.5,method:n="magnitude",threshold:s}=t,i=o.toFloat32Array(),r=o.shape,a=new Float32Array(i.length),c=new Float32Array(i.length),l=0;if(n==="magnitude"){let d=Array.from(i).map(Math.abs).sort((p,m)=>p-m),u=Math.floor(d.length*e),h=s??d[u]??0;for(let p=0;p<i.length;p++)Math.abs(i[p]??0)>h?(a[p]=1,c[p]=i[p]??0):(a[p]=0,c[p]=0,l++)}else if(n==="random")for(let d=0;d<i.length;d++)Math.random()>e?(a[d]=1,c[d]=i[d]??0):(a[d]=0,c[d]=0,l++);return{tensor:new w(Array.from(c),r,"float32"),mask:new w(Array.from(a),r,"float32"),sparsity:l/i.length}}async function fs(o,t={}){let{onProgress:e}=t;e?.({current:0,total:1,percent:0});let n=qt(o),s=0,i=0;for(let r of n){s+=r.data.length;let a=new w(Array.from(r.data),r.shape,"float32"),{sparsity:c}=Ht(a,t);i+=Math.floor(r.data.length*c)}return e?.({current:1,total:1,percent:100}),{data:o,originalSize:o.byteLength,prunedSize:o.byteLength,sparsity:i/s,parametersPruned:i,totalParameters:s}}async function ps(o){let t=qt(o),e=o.byteLength,n={},s=0,i=[];for(let l of t){s+=l.data.length;let d=l.dtype==="float32"?4:l.dtype==="float16"?2:l.dtype==="int8"?1:4,u=l.data.length*d;n[l.dtype]||(n[l.dtype]={count:0,size:0}),n[l.dtype].count++,n[l.dtype].size+=u,i.push({name:l.name,size:u,shape:l.shape})}i.sort((l,d)=>d.size-l.size);let r=i.slice(0,10),a={int8:Math.ceil(e/4),uint8:Math.ceil(e/4),int4:Math.ceil(e/8),float16:Math.ceil(e/2),dynamic:Math.ceil(e/4)},c="dynamic";return e>500*1024*1024?c="int4":e>100*1024*1024?c="int8":e>50*1024*1024&&(c="float16"),{totalSize:e,tensorCount:t.length,totalParameters:s,dtypeBreakdown:n,largestTensors:r,estimatedMemory:s*4,recommendedQuantization:c,estimatedQuantizedSizes:a}}async function ms(o,t){let{format:e,quantize:n}=t,s=o;switch(n&&(s=(await jt(o,{type:n})).data),e){case"edgeflow":return s;case"onnx":return s;case"tflite":return s;default:return s}}function Ar(o){let t=o instanceof Float32Array?o:new Float32Array(o),e=1/0,n=-1/0,s=0,i=0,r=0,a=0;for(let h=0;h<t.length;h++){let p=t[h]??0;if(isNaN(p)){r++;continue}if(!isFinite(p)){a++;continue}e=Math.min(e,p),n=Math.max(n,p),s+=p,p===0&&i++}let c=t.length-r-a,l=c>0?s/c:0,d=0;for(let h=0;h<t.length;h++){let p=t[h]??0;!isNaN(p)&&isFinite(p)&&(d+=Math.pow(p-l,2))}let u=c>0?Math.sqrt(d/c):0;return{min:e===1/0?0:e,max:n===-1/0?0:n,mean:l,std:u,zeros:i,nans:r,infinities:a,sparsity:i/t.length}}function zr(o,t=50){let e=o instanceof Float32Array?o:new Float32Array(o),n=1/0,s=-1/0;for(let c=0;c<e.length;c++){let l=e[c]??0;!isNaN(l)&&isFinite(l)&&(n=Math.min(n,l),s=Math.max(s,l))}if(n===1/0||s===-1/0||n===s)return{bins:[n||0],counts:[e.length],binEdges:[n||0,s||0]};let i=(s-n)/t,r=new Array(t).fill(0),a=new Array(t+1);for(let c=0;c<=t;c++)a[c]=n+c*i;for(let c=0;c<e.length;c++){let l=e[c]??0;if(!isNaN(l)&&isFinite(l)){let d=Math.min(Math.floor((l-n)/i),t-1);r[d]++}}return{bins:a.slice(0,-1).map((c,l)=>(c+a[l+1])/2),counts:r,binEdges:a}}function je(o,t="tensor",e={}){let{histogram:n=!0,maxSample:s=10}=e,i=o.toFloat32Array(),r=o.shape,a=o.size,c=[],l=Math.max(1,Math.floor(a/s));for(let p=0;p<a&&c.length<s;p+=l)c.push(p);let d=c.map(p=>i[p]??0),u=o.dtype==="float32"||o.dtype==="int32"?4:o.dtype==="int64"?8:4,h=a*u;return{name:t,shape:r,dtype:o.dtype,size:a,memoryBytes:h,stats:Ar(i),sample:d,histogram:n?zr(i):void 0}}function gs(o){let{name:t,shape:e,dtype:n,size:s,memoryBytes:i,stats:r,sample:a}=o,c=[`\u250C\u2500 Tensor: ${t} \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500`,`\u2502 Shape: [${e.join(", ")}]`,`\u2502 Dtype: ${n}`,`\u2502 Size: ${s.toLocaleString()} elements`,`\u2502 Memory: ${Vt(i)}`,"\u251C\u2500 Statistics \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500",`\u2502 Min: ${r.min.toFixed(6)}`,`\u2502 Max: ${r.max.toFixed(6)}`,`\u2502 Mean: ${r.mean.toFixed(6)}`,`\u2502 Std: ${r.std.toFixed(6)}`,`\u2502 Sparsity: ${(r.sparsity*100).toFixed(2)}%`];return r.nans>0&&c.push(`\u2502 \u26A0\uFE0F NaN values: ${r.nans}`),r.infinities>0&&c.push(`\u2502 \u26A0\uFE0F Infinity values: ${r.infinities}`),c.push("\u251C\u2500 Sample Values \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"),c.push(`\u2502 [${a.map(l=>l.toFixed(4)).join(", ")}]`),c.push("\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"),c.join(`
|
|
80
|
-
`)}function
|
|
81
|
-
`)}},
|
|
82
|
-
`)}function
|
|
83
|
-
`)}function
|
|
84
|
-
`)}var
|
|
79
|
+
`.trim()}async function hs(o,t={}){let{name:e="memory-benchmark",runs:n=5}=t,s=()=>typeof performance<"u"&&"memory"in performance?performance.memory.usedJSHeapSize:0,i=[],r=s();for(let d=0;d<n;d++)await o(),i.push(s());let a=Math.max(...i),c=i.reduce((d,u)=>d+u,0)/i.length,l=c-r;return{name:e,peakMemory:a,avgMemory:c,memoryDelta:l}}B();C();function fs(o,t,e,n,s=0,i=[]){let r=e?-(1<<t-1):0,a=e?(1<<t-1)-1:(1<<t)-1;if(n&&i.length>1){let c=i[s]??1,l=new Float32Array(c),d=new Int32Array(c),u=o.length/c,h=1/0,p=-1/0;for(let m=0;m<c;m++){let y=1/0,g=-1/0;for(let T=0;T<u;T++){let k=m*u+T,b=o[k]??0;y=Math.min(y,b),g=Math.max(g,b)}if(h=Math.min(h,y),p=Math.max(p,g),e){let T=Math.max(Math.abs(y),Math.abs(g));l[m]=T/a,d[m]=0}else l[m]=(g-y)/(a-r),d[m]=Math.round(r-y/(l[m]||1));l[m]===0&&(l[m]=1)}return{scale:l,zeroPoint:d,min:h,max:p}}else{let c=1/0,l=-1/0;for(let h=0;h<o.length;h++){let p=o[h]??0;c=Math.min(c,p),l=Math.max(l,p)}let d,u;return e?(d=Math.max(Math.abs(c),Math.abs(l))/a,u=0):(d=(l-c)/(a-r),u=Math.round(r-c/(d||1))),d===0&&(d=1),{scale:d,zeroPoint:u,min:c,max:l}}}function ut(o,t,e,n,s=o.length){let i=new Int8Array(o.length);if(n&&t instanceof Float32Array){let r=t.length;for(let a=0;a<r;a++){let c=t[a]??1,l=e[a]??0;for(let d=0;d<s;d++){let u=a*s+d,h=o[u]??0;i[u]=Math.max(-128,Math.min(127,Math.round(h/c+l)))}}}else{let r=t,a=e;for(let c=0;c<o.length;c++){let l=o[c]??0;i[c]=Math.max(-128,Math.min(127,Math.round(l/r+a)))}}return i}function ps(o,t,e,n,s=o.length){let i=new Uint8Array(o.length);if(n&&t instanceof Float32Array){let r=t.length;for(let a=0;a<r;a++){let c=t[a]??1,l=e[a]??0;for(let d=0;d<s;d++){let u=a*s+d,h=o[u]??0;i[u]=Math.max(0,Math.min(255,Math.round(h/c+l)))}}}else{let r=t,a=e;for(let c=0;c<o.length;c++){let l=o[c]??0;i[c]=Math.max(0,Math.min(255,Math.round(l/r+a)))}}return i}function zr(o,t,e){let n=Math.ceil(o.length/2),s=new Uint8Array(n);for(let i=0;i<o.length;i+=2){let r=o[i]??0,a=o[i+1]??0,c=Math.max(0,Math.min(15,Math.round(r/t+e+8))),l=Math.max(0,Math.min(15,Math.round(a/t+e+8)));s[i>>1]=c<<4|l}return s}function ms(o){let t=new Uint16Array(o.length);for(let e=0;e<o.length;e++)t[e]=Sr(o[e]??0);return t}function Sr(o){let t=new Float32Array(1),e=new Int32Array(t.buffer);t[0]=o;let n=e[0],s=n>>16&32768,i=(n>>23&255)-127+15,r=n&8388607;if(i<=0){if(i<-10)return s;let a=(r|8388608)>>1-i;return s|a>>13}else if(i>=31)return s|31744;return s|i<<10|r>>13}function ht(o,t,e,n=!1,s=o.length){let i=new Float32Array(o.length);if(n&&t instanceof Float32Array){let r=t.length;for(let a=0;a<r;a++){let c=t[a]??1,l=e[a]??0;for(let d=0;d<s;d++){let u=a*s+d;i[u]=((o[u]??0)-l)*c}}}else{let r=t,a=e;for(let c=0;c<o.length;c++)i[c]=((o[c]??0)-a)*r}return i}function jt(o,t,e,n=!1,s=o.length){let i=new Float32Array(o.length);if(n&&t instanceof Float32Array){let r=t.length;for(let a=0;a<r;a++){let c=t[a]??1,l=e[a]??0;for(let d=0;d<s;d++){let u=a*s+d;i[u]=((o[u]??0)-l)*c}}}else{let r=t,a=e;for(let c=0;c<o.length;c++)i[c]=((o[c]??0)-a)*r}return i}function Ht(o){let t=(o&32768)>>15,e=(o&31744)>>10,n=o&1023;return e===0?n===0?t===0?0:-0:(t===0?1:-1)*Math.pow(2,-14)*(n/1024):e===31?n===0?t===0?1/0:-1/0:NaN:(t===0?1:-1)*Math.pow(2,e-15)*(1+n/1024)}function Wt(o){let t=new Float32Array(o.length);for(let e=0;e<o.length;e++)t[e]=Ht(o[e]??0);return t}function Vt(o){let t=[],e=new Float32Array(o);return t.push({name:"model_weights",data:e,shape:[e.length],dtype:"float32"}),t}function vr(o){let t=new TextEncoder,e=20;for(let a of o.weights){let c=t.encode(a.name),l=t.encode(a.dtype),d=t.encode(a.originalDtype);e+=4+c.length,e+=4+a.shape.length*4,e+=4+l.length,e+=4+d.length,e+=1,a.scale!==void 0&&(e+=Array.isArray(a.scale)?4+a.scale.length*4:4),e+=1,a.zeroPoint!==void 0&&(e+=Array.isArray(a.zeroPoint)?4+a.zeroPoint.length*4:4),e+=8+a.data.byteLength}let n=new ArrayBuffer(e),s=new DataView(n),i=new Uint8Array(n),r=0;s.setUint32(r,o.version,!0),r+=4,s.setUint32(r,["int8","uint8","int4","float16","dynamic"].indexOf(o.quantizationType),!0),r+=4,s.setUint32(r,o.originalSize&4294967295,!0),r+=4,s.setUint32(r,o.originalSize/4294967296>>>0,!0),r+=4,s.setUint32(r,o.weights.length,!0),r+=4;for(let a of o.weights){let c=t.encode(a.name),l=t.encode(a.dtype),d=t.encode(a.originalDtype);s.setUint32(r,c.length,!0),r+=4,i.set(c,r),r+=c.length,s.setUint32(r,a.shape.length,!0),r+=4;for(let p of a.shape)s.setInt32(r,p,!0),r+=4;if(s.setUint32(r,l.length,!0),r+=4,i.set(l,r),r+=l.length,s.setUint32(r,d.length,!0),r+=4,i.set(d,r),r+=d.length,a.scale!==void 0)if(s.setUint8(r,1),r+=1,Array.isArray(a.scale)){s.setUint32(r,a.scale.length,!0),r+=4;for(let p of a.scale)s.setFloat32(r,p,!0),r+=4}else s.setUint32(r,1,!0),r+=4,s.setFloat32(r,a.scale,!0),r+=4;else s.setUint8(r,0),r+=1;if(a.zeroPoint!==void 0)if(s.setUint8(r,1),r+=1,Array.isArray(a.zeroPoint)){s.setUint32(r,a.zeroPoint.length,!0),r+=4;for(let p of a.zeroPoint)s.setInt32(r,p,!0),r+=4}else s.setUint32(r,1,!0),r+=4,s.setInt32(r,a.zeroPoint,!0),r+=4;else s.setUint8(r,0),r+=1;let u=a.data.byteLength&4294967295,h=a.data.byteLength/4294967296>>>0;s.setUint32(r,u,!0),r+=4,s.setUint32(r,h,!0),r+=4,i.set(new Uint8Array(a.data),r),r+=a.data.byteLength}return n}async function Gt(o,t){let{type:e,skipPatterns:n=[],perChannel:s=!1,symmetric:i=!0,onProgress:r,minTensorSize:a=100}=t,c=o.byteLength,l=[],d=0,u=0;r?.({stage:"analyzing",current:0,total:1,percent:0});let h=Vt(o),p=[],m=0,y=0,g=[];for(let _=0;_<h.length;_++){let A=h[_],me=(_+1)/h.length*100;if(r?.({stage:"quantizing",current:_+1,total:h.length,percent:me,layerName:A.name}),m+=A.data.length,A.data.length<a||n.some(Xe=>typeof Xe=="string"?A.name.includes(Xe):Xe.test(A.name))){u++,l.push({name:A.name,originalDtype:A.dtype,quantizedDtype:A.dtype,originalSize:A.data.byteLength,quantizedSize:A.data.byteLength,scale:1,zeroPoint:0,minValue:Math.min(...A.data),maxValue:Math.max(...A.data),skipped:!0,skipReason:A.data.length<a?"Tensor too small":"Matched skip pattern"}),p.push({name:A.name,data:A.data.buffer.slice(0),shape:A.shape,dtype:A.dtype,originalDtype:A.dtype});continue}let ge=e==="int4"?4:8,L=fs(A.data,ge,i,s,0,A.shape),oe,ie;switch(e){case"int8":oe=ut(A.data,L.scale,L.zeroPoint,s,s?A.data.length/(A.shape[0]??1):A.data.length).buffer.slice(0),ie="int8";break;case"uint8":oe=ps(A.data,L.scale,L.zeroPoint,s,s?A.data.length/(A.shape[0]??1):A.data.length).buffer.slice(0),ie="uint8";break;case"int4":oe=zr(A.data,L.scale,L.zeroPoint).buffer.slice(0),ie="int4";break;case"float16":oe=ms(A.data).buffer.slice(0),ie="float16";break;case"dynamic":default:oe=ut(A.data,L.scale,L.zeroPoint,s,s?A.data.length/(A.shape[0]??1):A.data.length).buffer.slice(0),ie="int8";break}d++,y+=A.data.length;let be=L.scale instanceof Float32Array?Array.from(L.scale):L.scale,Zt=L.zeroPoint instanceof Int32Array?Array.from(L.zeroPoint):L.zeroPoint;typeof be=="number"?g.push(be):g.push(...be),l.push({name:A.name,originalDtype:A.dtype,quantizedDtype:ie,originalSize:A.data.byteLength,quantizedSize:oe.byteLength,scale:be,zeroPoint:Zt,minValue:L.min,maxValue:L.max,skipped:!1}),p.push({name:A.name,data:oe,shape:A.shape,dtype:ie,originalDtype:A.dtype,scale:be,zeroPoint:Zt})}r?.({stage:"packing",current:0,total:1,percent:0});let k=vr({version:1,quantizationType:e,originalSize:c,weights:p});r?.({stage:"complete",current:1,total:1,percent:100});let b=g.length>0?g.reduce((_,A)=>_+A,0)/g.length:1,z=g.length>0?Math.min(...g):1,I=g.length>0?Math.max(...g):1,D=b/(e==="int4"?8:e==="float16"?2:4);return{data:k,originalSize:c,quantizedSize:k.byteLength,compressionRatio:c/k.byteLength,tensorsQuantized:d,tensorsSkipped:u,layerStats:l,stats:{totalParameters:m,quantizedParameters:y,averageScale:b,minScale:z,maxScale:I,errorEstimate:D}}}function gs(o,t,e={}){let{symmetric:n=!0,perChannel:s=!1}=e,i=o.toFloat32Array(),r=o.shape,c=fs(i,t==="int4"?4:8,n,s,0,r),l,d;switch(t){case"int8":l=ut(i,c.scale,c.zeroPoint,s),d="int32";break;case"uint8":l=ps(i,c.scale,c.zeroPoint,s),d="int32";break;case"float16":l=ms(i),d="float32";break;default:l=ut(i,c.scale,c.zeroPoint,s),d="int32"}let u=c.scale instanceof Float32Array?Array.from(c.scale):c.scale,h=c.zeroPoint instanceof Int32Array?Array.from(c.zeroPoint):c.zeroPoint;return{tensor:new w(Array.from(l),r,d),scale:u,zeroPoint:h}}function ys(o,t,e,n){let s=o.toArray(),i=o.shape,r,a=Array.isArray(t)?new Float32Array(t):t,c=Array.isArray(e)?new Int32Array(e):e,l=Array.isArray(t);switch(n){case"int8":r=ht(new Int8Array(s.map(Number)),a,c,l);break;case"uint8":r=jt(new Uint8Array(s.map(Number)),a,c,l);break;case"float16":r=Wt(new Uint16Array(s.map(Number)));break;default:r=ht(new Int8Array(s.map(Number)),a,c,l)}return new w(Array.from(r),i,"float32")}function Xt(o,t={}){let{ratio:e=.5,method:n="magnitude",threshold:s}=t,i=o.toFloat32Array(),r=o.shape,a=new Float32Array(i.length),c=new Float32Array(i.length),l=0;if(n==="magnitude"){let d=Array.from(i).map(Math.abs).sort((p,m)=>p-m),u=Math.floor(d.length*e),h=s??d[u]??0;for(let p=0;p<i.length;p++)Math.abs(i[p]??0)>h?(a[p]=1,c[p]=i[p]??0):(a[p]=0,c[p]=0,l++)}else if(n==="random")for(let d=0;d<i.length;d++)Math.random()>e?(a[d]=1,c[d]=i[d]??0):(a[d]=0,c[d]=0,l++);return{tensor:new w(Array.from(c),r,"float32"),mask:new w(Array.from(a),r,"float32"),sparsity:l/i.length}}async function ws(o,t={}){let{onProgress:e}=t;e?.({current:0,total:1,percent:0});let n=Vt(o),s=0,i=0;for(let r of n){s+=r.data.length;let a=new w(Array.from(r.data),r.shape,"float32"),{sparsity:c}=Xt(a,t);i+=Math.floor(r.data.length*c)}return e?.({current:1,total:1,percent:100}),{data:o,originalSize:o.byteLength,prunedSize:o.byteLength,sparsity:i/s,parametersPruned:i,totalParameters:s}}async function xs(o){let t=Vt(o),e=o.byteLength,n={},s=0,i=[];for(let l of t){s+=l.data.length;let d=l.dtype==="float32"?4:l.dtype==="float16"?2:l.dtype==="int8"?1:4,u=l.data.length*d;n[l.dtype]||(n[l.dtype]={count:0,size:0}),n[l.dtype].count++,n[l.dtype].size+=u,i.push({name:l.name,size:u,shape:l.shape})}i.sort((l,d)=>d.size-l.size);let r=i.slice(0,10),a={int8:Math.ceil(e/4),uint8:Math.ceil(e/4),int4:Math.ceil(e/8),float16:Math.ceil(e/2),dynamic:Math.ceil(e/4)},c="dynamic";return e>500*1024*1024?c="int4":e>100*1024*1024?c="int8":e>50*1024*1024&&(c="float16"),{totalSize:e,tensorCount:t.length,totalParameters:s,dtypeBreakdown:n,largestTensors:r,estimatedMemory:s*4,recommendedQuantization:c,estimatedQuantizedSizes:a}}async function Ms(o,t){let{format:e,quantize:n}=t,s=o;switch(n&&(s=(await Gt(o,{type:n})).data),e){case"edgeflow":return s;case"onnx":return s;case"tflite":return s;default:return s}}function Er(o){let t=o instanceof Float32Array?o:new Float32Array(o),e=1/0,n=-1/0,s=0,i=0,r=0,a=0;for(let h=0;h<t.length;h++){let p=t[h]??0;if(isNaN(p)){r++;continue}if(!isFinite(p)){a++;continue}e=Math.min(e,p),n=Math.max(n,p),s+=p,p===0&&i++}let c=t.length-r-a,l=c>0?s/c:0,d=0;for(let h=0;h<t.length;h++){let p=t[h]??0;!isNaN(p)&&isFinite(p)&&(d+=Math.pow(p-l,2))}let u=c>0?Math.sqrt(d/c):0;return{min:e===1/0?0:e,max:n===-1/0?0:n,mean:l,std:u,zeros:i,nans:r,infinities:a,sparsity:i/t.length}}function Dr(o,t=50){let e=o instanceof Float32Array?o:new Float32Array(o),n=1/0,s=-1/0;for(let c=0;c<e.length;c++){let l=e[c]??0;!isNaN(l)&&isFinite(l)&&(n=Math.min(n,l),s=Math.max(s,l))}if(n===1/0||s===-1/0||n===s)return{bins:[n||0],counts:[e.length],binEdges:[n||0,s||0]};let i=(s-n)/t,r=new Array(t).fill(0),a=new Array(t+1);for(let c=0;c<=t;c++)a[c]=n+c*i;for(let c=0;c<e.length;c++){let l=e[c]??0;if(!isNaN(l)&&isFinite(l)){let d=Math.min(Math.floor((l-n)/i),t-1);r[d]++}}return{bins:a.slice(0,-1).map((c,l)=>(c+a[l+1])/2),counts:r,binEdges:a}}function We(o,t="tensor",e={}){let{histogram:n=!0,maxSample:s=10}=e,i=o.toFloat32Array(),r=o.shape,a=o.size,c=[],l=Math.max(1,Math.floor(a/s));for(let p=0;p<a&&c.length<s;p+=l)c.push(p);let d=c.map(p=>i[p]??0),u=o.dtype==="float32"||o.dtype==="int32"?4:o.dtype==="int64"?8:4,h=a*u;return{name:t,shape:r,dtype:o.dtype,size:a,memoryBytes:h,stats:Er(i),sample:d,histogram:n?Dr(i):void 0}}function Ts(o){let{name:t,shape:e,dtype:n,size:s,memoryBytes:i,stats:r,sample:a}=o,c=[`\u250C\u2500 Tensor: ${t} \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500`,`\u2502 Shape: [${e.join(", ")}]`,`\u2502 Dtype: ${n}`,`\u2502 Size: ${s.toLocaleString()} elements`,`\u2502 Memory: ${Qt(i)}`,"\u251C\u2500 Statistics \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500",`\u2502 Min: ${r.min.toFixed(6)}`,`\u2502 Max: ${r.max.toFixed(6)}`,`\u2502 Mean: ${r.mean.toFixed(6)}`,`\u2502 Std: ${r.std.toFixed(6)}`,`\u2502 Sparsity: ${(r.sparsity*100).toFixed(2)}%`];return r.nans>0&&c.push(`\u2502 \u26A0\uFE0F NaN values: ${r.nans}`),r.infinities>0&&c.push(`\u2502 \u26A0\uFE0F Infinity values: ${r.infinities}`),c.push("\u251C\u2500 Sample Values \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"),c.push(`\u2502 [${a.map(l=>l.toFixed(4)).join(", ")}]`),c.push("\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"),c.join(`
|
|
80
|
+
`)}function Qt(o){return o<1024?`${o} B`:o<1024*1024?`${(o/1024).toFixed(2)} KB`:o<1024*1024*1024?`${(o/(1024*1024)).toFixed(2)} MB`:`${(o/(1024*1024*1024)).toFixed(2)} GB`}var Ve=class{constructor(t={}){f(this,"config");f(this,"events",[]);f(this,"traces",[]);f(this,"performanceMetrics");f(this,"listeners",new Map);f(this,"isEnabled",!0);this.config={logging:t.logging??!0,logLevel:t.logLevel??"info",inspectTensors:t.inspectTensors??!0,maxDisplayValues:t.maxDisplayValues??10,trackPerformance:t.trackPerformance??!0,logger:t.logger??this.defaultLogger.bind(this)},this.performanceMetrics={inferenceCount:0,totalInferenceTime:0,averageInferenceTime:0,minInferenceTime:1/0,maxInferenceTime:0,peakMemoryUsage:0,currentMemoryUsage:0,tensorAllocations:0,tensorDeallocations:0}}defaultLogger(t,e,n){let i=`[edgeFlow.js ${new Date().toISOString()}] [${t.toUpperCase()}]`;switch(t){case"debug":console.debug(i,e,n??"");break;case"info":console.info(i,e,n??"");break;case"warn":console.warn(i,e,n??"");break;case"error":console.error(i,e,n??"");break;default:console.log(i,e,n??"")}}log(t,e,n){if(!this.isEnabled||!this.config.logging)return;let s=["debug","info","warn","error"],i=s.indexOf(this.config.logLevel);s.indexOf(t)>=i&&this.config.logger(t,e,n)}addEvent(t){this.events.push(t);let e=this.listeners.get(t.type)??[];for(let n of e)n(t);this.events.length>1e3&&(this.events=this.events.slice(-1e3))}enable(){this.isEnabled=!0,this.log("info","Debugger enabled")}disable(){this.isEnabled=!1}on(t,e){let n=this.listeners.get(t)??[];return n.push(e),this.listeners.set(t,n),()=>{let s=n.indexOf(e);s!==-1&&n.splice(s,1)}}inspectTensor(t,e="tensor"){let n=We(t,e,{histogram:!0,maxSample:this.config.maxDisplayValues});return this.config.inspectTensors&&(this.log("debug",`Tensor: ${e}`,n),this.addEvent({type:"tensor",timestamp:Date.now(),message:`Inspected tensor: ${e}`,data:n}),n.stats.nans>0&&this.log("warn",`Tensor "${e}" contains ${n.stats.nans} NaN values`),n.stats.infinities>0&&this.log("warn",`Tensor "${e}" contains ${n.stats.infinities} Infinity values`)),n}startTrace(t){let e=`trace_${Date.now()}_${Math.random().toString(36).slice(2,8)}`,n={id:e,modelId:t,timestamp:Date.now(),inputs:[],outputs:[],duration:0,memoryUsed:0,operations:[]};return this.traces.push(n),this.log("debug",`Started trace: ${e} for model: ${t}`),e}traceInput(t,e,n){let s=this.traces.find(i=>i.id===t);s&&s.inputs.push(We(e,n))}traceOutput(t,e,n){let s=this.traces.find(i=>i.id===t);s&&s.outputs.push(We(e,n))}traceOperation(t,e){let n=this.traces.find(s=>s.id===t);n&&n.operations.push(e)}endTrace(t){let e=this.traces.find(n=>n.id===t);if(e)return e.duration=Date.now()-e.timestamp,this.performanceMetrics.inferenceCount++,this.performanceMetrics.totalInferenceTime+=e.duration,this.performanceMetrics.averageInferenceTime=this.performanceMetrics.totalInferenceTime/this.performanceMetrics.inferenceCount,this.performanceMetrics.minInferenceTime=Math.min(this.performanceMetrics.minInferenceTime,e.duration),this.performanceMetrics.maxInferenceTime=Math.max(this.performanceMetrics.maxInferenceTime,e.duration),this.log("info",`Trace completed: ${t}`,{duration:`${e.duration}ms`,inputs:e.inputs.length,outputs:e.outputs.length,operations:e.operations.length}),this.addEvent({type:"inference",timestamp:Date.now(),message:`Inference completed in ${e.duration}ms`,data:e}),e}recordAllocation(t){if(!this.config.trackPerformance)return;this.performanceMetrics.tensorAllocations++;let e=t.size*4;this.performanceMetrics.currentMemoryUsage+=e,this.performanceMetrics.peakMemoryUsage=Math.max(this.performanceMetrics.peakMemoryUsage,this.performanceMetrics.currentMemoryUsage)}recordDeallocation(t){if(!this.config.trackPerformance)return;this.performanceMetrics.tensorDeallocations++;let e=t.size*4;this.performanceMetrics.currentMemoryUsage-=e}getPerformanceMetrics(){return{...this.performanceMetrics}}getEvents(){return[...this.events]}getTraces(){return[...this.traces]}getTrace(t){return this.traces.find(e=>e.id===t)}clear(){this.events=[],this.traces=[],this.performanceMetrics={inferenceCount:0,totalInferenceTime:0,averageInferenceTime:0,minInferenceTime:1/0,maxInferenceTime:0,peakMemoryUsage:0,currentMemoryUsage:0,tensorAllocations:0,tensorDeallocations:0}}export(){return{events:this.getEvents(),traces:this.getTraces(),metrics:this.getPerformanceMetrics(),timestamp:Date.now()}}generateReport(){let t=this.getPerformanceMetrics(),e=this.getTraces(),n=["\u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557","\u2551 edgeFlow.js Debug Report \u2551","\u2560\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2563","\u2551 Performance Metrics \u2551","\u255F\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2562",`\u2551 Total Inferences: ${t.inferenceCount.toString().padStart(10)} \u2551`,`\u2551 Average Time: ${t.averageInferenceTime.toFixed(2).padStart(10)}ms \u2551`,`\u2551 Min Time: ${(t.minInferenceTime===1/0?0:t.minInferenceTime).toFixed(2).padStart(10)}ms \u2551`,`\u2551 Max Time: ${t.maxInferenceTime.toFixed(2).padStart(10)}ms \u2551`,`\u2551 Peak Memory: ${Qt(t.peakMemoryUsage).padStart(10)} \u2551`,`\u2551 Current Memory: ${Qt(t.currentMemoryUsage).padStart(10)} \u2551`,`\u2551 Tensor Allocations: ${t.tensorAllocations.toString().padStart(10)} \u2551`,`\u2551 Tensor Deallocations: ${t.tensorDeallocations.toString().padStart(10)} \u2551`,"\u255F\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2562","\u2551 Recent Traces \u2551","\u255F\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2562"],s=e.slice(-5);for(let i of s)n.push(`\u2551 ${i.id.slice(0,20).padEnd(20)} | ${i.duration.toFixed(2).padStart(8)}ms | ${i.modelId.slice(0,20).padEnd(20)} \u2551`);return s.length===0&&n.push("\u2551 No traces recorded \u2551"),n.push("\u255A\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u255D"),n.join(`
|
|
81
|
+
`)}},ft=null;function Kt(o){return(!ft||o)&&(ft=new Ve(o)),ft}function bs(o){let t=Kt(o);return t.enable(),t}function ks(){ft?.disable()}function Is(o,t=50,e=10){let{counts:n,binEdges:s}=o,i=Math.max(...n);if(i===0)return"No data to display";let r=[],a=n.map(d=>Math.round(d/i*e));for(let d=e;d>0;d--){let u=d===e?`${i.toString().padStart(6)} \u2502`:" \u2502";for(let h=0;h<t&&h<a.length;h++)u+=(a[h]??0)>=d?"\u2588":" ";r.push(u)}r.push(" \u2514"+"\u2500".repeat(Math.min(t,a.length)));let c=(s[0]??0).toFixed(2),l=(s[s.length-1]??0).toFixed(2);return r.push(` ${c}${" ".repeat(Math.max(0,Math.min(t,a.length)-c.length-l.length))}${l}`),r.join(`
|
|
82
|
+
`)}function As(o,t=40){let e=o.shape;if(e.length!==2)return"Heatmap only supports 2D tensors";let[n,s]=e;if(n===void 0||s===void 0)return"Invalid tensor shape";let i=o.toFloat32Array(),r=1/0,a=-1/0;for(let p=0;p<i.length;p++){let m=i[p]??0;!isNaN(m)&&isFinite(m)&&(r=Math.min(r,m),a=Math.max(a,m))}let c=a-r,l=[" ","\u2591","\u2592","\u2593","\u2588"],d=[],u=Math.max(1,Math.ceil(s/t)),h=Math.min(s,t);for(let p=0;p<n;p++){let m="";for(let y=0;y<h;y++){let g=p*s+y*u,T=i[g]??0,k=c>0?(T-r)/c:0,b=Math.floor(k*(l.length-1));m+=l[b]}d.push(m)}return d.join(`
|
|
83
|
+
`)}function zs(o){let t=[];t.push("\u250C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510"),t.push("\u2502 Model Architecture \u2502"),t.push("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524");for(let e=0;e<o.length;e++){let n=o[e],s=`[${n.inputShape.join("\xD7")}]`,i=`[${n.outputShape.join("\xD7")}]`;t.push(`\u2502 ${(e+1).toString().padStart(2)}. ${n.name.padEnd(20)} \u2502 ${n.type.padEnd(15)} \u2502`),t.push(`\u2502 ${s.padEnd(15)} \u2192 ${i.padEnd(15)} \u2502`),e<o.length-1&&t.push("\u2502 \u2193 \u2502")}return t.push("\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518"),t.join(`
|
|
84
|
+
`)}var Ge=class{constructor(t={}){f(this,"config");f(this,"samples",[]);f(this,"isRunning",!1);f(this,"intervalId",null);f(this,"alerts",[]);f(this,"alertListeners",[]);f(this,"sampleListeners",[]);f(this,"inferenceCount",0);f(this,"inferenceTimes",[]);f(this,"queueLength",0);f(this,"activeCount",0);f(this,"frameCount",0);f(this,"lastFrameTime",0);f(this,"fps",0);f(this,"rafId",null);f(this,"tensorMemory",0);f(this,"cacheMemory",0);this.config={enabled:t.enabled??!0,sampleInterval:t.sampleInterval??1e3,historySize:t.historySize??60,monitorMemory:t.monitorMemory??!0,monitorFPS:t.monitorFPS??!0,collectors:t.collectors??[]}}start(){this.isRunning||(this.isRunning=!0,this.intervalId=setInterval(()=>{this.collectSample()},this.config.sampleInterval),this.config.monitorFPS&&typeof requestAnimationFrame<"u"&&(this.lastFrameTime=performance.now(),this.frameCount=0,this.monitorFPS()))}stop(){this.isRunning=!1,this.intervalId&&(clearInterval(this.intervalId),this.intervalId=null),this.rafId&&(cancelAnimationFrame(this.rafId),this.rafId=null)}monitorFPS(){if(!this.isRunning)return;this.frameCount++;let t=performance.now(),e=t-this.lastFrameTime;e>=1e3&&(this.fps=Math.round(this.frameCount*1e3/e),this.frameCount=0,this.lastFrameTime=t),this.rafId=requestAnimationFrame(()=>this.monitorFPS())}collectSample(){let t=Date.now(),e=this.inferenceTimes.length>0?this.inferenceTimes.reduce((u,h)=>u+h,0)/this.inferenceTimes.length:0,n=this.inferenceTimes.length>0?Math.min(...this.inferenceTimes):0,s=this.inferenceTimes.length>0?Math.max(...this.inferenceTimes):0,i=this.inferenceCount/(this.config.sampleInterval/1e3),r={count:this.inferenceCount,avgTime:e,minTime:n,maxTime:s,throughput:i,queueLength:this.queueLength,activeCount:this.activeCount},a=this.collectMemoryMetrics(),c=this.collectSystemMetrics(),l={};for(let u of this.config.collectors)try{Object.assign(l,u())}catch{}let d={timestamp:t,inference:r,memory:a,system:c,custom:l};this.samples.push(d),this.samples.length>this.config.historySize&&this.samples.shift(),this.checkAlerts(d);for(let u of this.sampleListeners)u(d);this.inferenceCount=0,this.inferenceTimes=[]}collectMemoryMetrics(){let t=0,e=0,n=0;if(typeof performance<"u"&&"memory"in performance){let s=performance.memory;t=s.usedJSHeapSize,e=s.totalJSHeapSize,n=s.jsHeapSizeLimit}return{usedHeap:t,totalHeap:e,heapLimit:n,heapUsage:n>0?t/n:0,tensorMemory:this.tensorMemory,cacheMemory:this.cacheMemory}}collectSystemMetrics(){let t=this.samples[this.samples.length-1],e=t?Date.now()-t.timestamp:this.config.sampleInterval,n=!1;typeof navigator<"u"&&"gpu"in navigator&&(n=!0);let s=!1;return typeof navigator<"u"&&"ml"in navigator&&(s=!0),{fps:this.fps,cpuUsage:this.estimateCPUUsage(),deltaTime:e,userAgent:typeof navigator<"u"?navigator.userAgent:"unknown",webgpuAvailable:n,webnnAvailable:s}}estimateCPUUsage(){if(this.inferenceTimes.length===0)return 0;let t=this.inferenceTimes.reduce((e,n)=>e+n,0);return Math.min(1,t/this.config.sampleInterval)}checkAlerts(t){for(let e of this.alerts){let n=this.getMetricValue(t,e.metric);if(n===void 0)continue;let s=!1;switch(e.operator){case">":s=n>e.threshold;break;case"<":s=n<e.threshold;break;case">=":s=n>=e.threshold;break;case"<=":s=n<=e.threshold;break;case"==":s=n===e.threshold;break;case"!=":s=n!==e.threshold;break}if(s){let i={config:e,value:n,timestamp:t.timestamp};for(let r of this.alertListeners)r(i)}}}getMetricValue(t,e){let n=e.split("."),s=t;for(let i of n)if(s&&typeof s=="object"&&i in s)s=s[i];else return;return typeof s=="number"?s:void 0}recordInference(t){this.inferenceCount++,this.inferenceTimes.push(t)}updateQueueLength(t){this.queueLength=t}updateActiveCount(t){this.activeCount=t}updateTensorMemory(t){this.tensorMemory=t}updateCacheMemory(t){this.cacheMemory=t}addAlert(t){this.alerts.push(t)}removeAlert(t){this.alerts=this.alerts.filter(e=>e.metric!==t)}onAlert(t){return this.alertListeners.push(t),()=>{let e=this.alertListeners.indexOf(t);e!==-1&&this.alertListeners.splice(e,1)}}onSample(t){return this.sampleListeners.push(t),()=>{let e=this.sampleListeners.indexOf(t);e!==-1&&this.sampleListeners.splice(e,1)}}getCurrentSample(){return this.samples[this.samples.length-1]}getSamples(){return[...this.samples]}getSamplesInRange(t,e){return this.samples.filter(n=>n.timestamp>=t&&n.timestamp<=e)}getSummary(){if(this.samples.length===0)return{avgInferenceTime:0,avgThroughput:0,avgMemoryUsage:0,avgFPS:0,totalInferences:0,uptime:0};let t=this.samples.reduce((l,d)=>l+d.inference.avgTime,0)/this.samples.length,e=this.samples.reduce((l,d)=>l+d.inference.throughput,0)/this.samples.length,n=this.samples.reduce((l,d)=>l+d.memory.heapUsage,0)/this.samples.length,s=this.samples.reduce((l,d)=>l+d.system.fps,0)/this.samples.length,i=this.samples.reduce((l,d)=>l+d.inference.count,0),r=this.samples[0],c=this.samples[this.samples.length-1].timestamp-r.timestamp;return{avgInferenceTime:t,avgThroughput:e,avgMemoryUsage:n,avgFPS:s,totalInferences:i,uptime:c}}clear(){this.samples=[],this.inferenceCount=0,this.inferenceTimes=[],this.queueLength=0,this.activeCount=0,this.tensorMemory=0,this.cacheMemory=0}export(){return{samples:this.getSamples(),summary:this.getSummary(),config:this.config,timestamp:Date.now()}}};function Ss(o){let t=o.getSummary(),e=o.getSamples(),n=e[e.length-1],s=r=>r<1024?`${r} B`:r<1024*1024?`${(r/1024).toFixed(1)} KB`:r<1024*1024*1024?`${(r/(1024*1024)).toFixed(1)} MB`:`${(r/(1024*1024*1024)).toFixed(1)} GB`;return`
|
|
85
85
|
<!DOCTYPE html>
|
|
86
86
|
<html lang="en">
|
|
87
87
|
<head>
|
|
@@ -398,7 +398,7 @@ Human:`]}),r=Array.isArray(i)?i[0]:i;return this.conversationHistory.push({role:
|
|
|
398
398
|
<stop offset="100%" stop-color="var(--accent)" stop-opacity="0"/>
|
|
399
399
|
</linearGradient>
|
|
400
400
|
</defs>
|
|
401
|
-
${
|
|
401
|
+
${Fr(e)}
|
|
402
402
|
</svg>
|
|
403
403
|
</div>
|
|
404
404
|
</div>
|
|
@@ -439,11 +439,11 @@ Human:`]}),r=Array.isArray(i)?i[0]:i;return this.conversationHistory.push({role:
|
|
|
439
439
|
</div>
|
|
440
440
|
</body>
|
|
441
441
|
</html>
|
|
442
|
-
`.trim()}function
|
|
442
|
+
`.trim()}function Fr(o){if(o.length<2)return"";let t=600,e=180,n=10,s=o.map(d=>d.inference.avgTime),i=Math.max(...s,1),r=o.map((d,u)=>{let h=n+u/(o.length-1)*(t-2*n),p=e-n-d.inference.avgTime/i*(e-2*n);return`${h},${p}`}),a=`M ${r.join(" L ")}`,c=`M ${n},${e-n} L ${r.join(" L ")} L ${t-n},${e-n} Z`,l=[];for(let d=0;d<=4;d++){let u=n+d/4*(e-2*n);l.push(`<line class="chart-grid" x1="${n}" y1="${u}" x2="${t-n}" y2="${u}"/>`)}return`
|
|
443
443
|
${l.join(`
|
|
444
444
|
`)}
|
|
445
445
|
<path class="chart-area" d="${c}"/>
|
|
446
446
|
<path class="chart-line" d="${a}"/>
|
|
447
|
-
`}function
|
|
448
|
-
`)}var
|
|
447
|
+
`}function vs(o){let t=o.getSummary(),e=o.getSamples(),n=e[e.length-1],s=c=>c<1024?`${c} B`:c<1024*1024?`${(c/1024).toFixed(1)} KB`:c<1024*1024*1024?`${(c/(1024*1024)).toFixed(1)} MB`:`${(c/(1024*1024*1024)).toFixed(1)} GB`,i=(c,l,d=20)=>{let u=Math.round(c/l*d);return"\u2588".repeat(u)+"\u2591".repeat(d-u)},r=["\u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557","\u2551 edgeFlow.js Performance Monitor Dashboard \u2551","\u2560\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2563","\u2551 \u2551",`\u2551 Total Inferences: ${t.totalInferences.toString().padStart(10)} \u2551`,`\u2551 Avg Inference: ${t.avgInferenceTime.toFixed(2).padStart(10)}ms \u2551`,`\u2551 Throughput: ${t.avgThroughput.toFixed(2).padStart(10)} ops/s \u2551`,`\u2551 Avg FPS: ${Math.round(t.avgFPS).toString().padStart(10)} \u2551`,"\u2551 \u2551","\u255F\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2562","\u2551 Memory Usage \u2551",`\u2551 Heap: ${i(t.avgMemoryUsage,1)} ${(t.avgMemoryUsage*100).toFixed(0).padStart(3)}% \u2551`,`\u2551 Used: ${s(n?.memory.usedHeap??0).padStart(10)} \u2551`,`\u2551 Tensor: ${s(n?.memory.tensorMemory??0).padStart(10)} \u2551`,`\u2551 Cache: ${s(n?.memory.cacheMemory??0).padStart(10)} \u2551`,"\u2551 \u2551","\u255F\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2562","\u2551 Inference Time History (last 30 samples) \u2551","\u2551 \u2551"],a=e.slice(-30);if(a.length>0){let c=a.map(u=>u.inference.avgTime),l=Math.max(...c,1),d=5;for(let u=d;u>0;u--){let h="\u2551 ";for(let p of c){let m=Math.ceil(p/l*d);h+=m>=u?"\u2593":" "}r.push(h.padEnd(76)+"\u2551")}r.push("\u2551 "+"\u2500".repeat(30)+" \u2551")}return r.push("\u2551 \u2551"),r.push(`\u2551 Last updated: ${new Date().toLocaleString().padEnd(40)} \u2551`),r.push("\u255A\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u255D"),r.join(`
|
|
448
|
+
`)}var pt=null;function Yt(o){return(!pt||o)&&(pt=new Ge(o)),pt}function Es(o){let t=Yt(o);return t.start(),t}function Ds(){pt?.stop()}async function Pr(o,t){let e=o instanceof ArrayBuffer?o:await Jt(o),n=e.byteLength,s,i=0,r=0;switch(t.method){case"int8":({data:s,layersQuantized:i,layersSkipped:r}=_r(e,t));break;case"uint8":({data:s,layersQuantized:i,layersSkipped:r}=Lr(e,t));break;case"float16":({data:s,layersQuantized:i,layersSkipped:r}=Nr(e,t));break;case"int4":({data:s,layersQuantized:i,layersSkipped:r}=Cr(e,t));break;default:s=e}return{modelData:s,originalSize:n,quantizedSize:s.byteLength,compressionRatio:n/s.byteLength,stats:{layersQuantized:i,layersSkipped:r}}}async function Jt(o){return new ArrayBuffer(0)}function _r(o,t){let e=new Float32Array(o),n=new Int8Array(e.length),s=0;for(let r=0;r<e.length;r++){let a=Math.abs(e[r]??0);a>s&&(s=a)}let i=s/127;for(let r=0;r<e.length;r++)n[r]=Math.round((e[r]??0)/i);return{data:n.buffer,layersQuantized:1,layersSkipped:0}}function Lr(o,t){let e=new Float32Array(o),n=new Uint8Array(e.length),s=1/0,i=-1/0;for(let a=0;a<e.length;a++){let c=e[a]??0;c<s&&(s=c),c>i&&(i=c)}let r=(i-s)/255;for(let a=0;a<e.length;a++)n[a]=Math.round(((e[a]??0)-s)/r);return{data:n.buffer,layersQuantized:1,layersSkipped:0}}function Nr(o,t){let e=new Float32Array(o),n=new Uint16Array(e.length);for(let s=0;s<e.length;s++)n[s]=Or(e[s]??0);return{data:n.buffer,layersQuantized:1,layersSkipped:0}}function Cr(o,t){let e=new Float32Array(o),n=new Uint8Array(Math.ceil(e.length/2)),s=0;for(let r=0;r<e.length;r++){let a=Math.abs(e[r]??0);a>s&&(s=a)}let i=s/7;for(let r=0;r<e.length;r+=2){let a=Math.round((e[r]??0)/i)+8,c=Math.round((e[r+1]??0)/i)+8;n[r/2]=(a&15)<<4|c&15}return{data:n.buffer,layersQuantized:1,layersSkipped:0}}function Or(o){let t=new Float32Array(1),e=new Int32Array(t.buffer);t[0]=o;let n=e[0]??0,s=n>>16&32768,i=n>>12&2047,r=n>>23&255;return r<103?s:r>142?(s|=31744,s|=(r===255?0:1)&&n&8388607,s):r<113?(i|=2048,s|=(i>>114-r)+(i>>113-r&1),s):(s|=r-112<<10|i>>1,s+=i&1,s)}async function Rr(o,t){let e=o instanceof ArrayBuffer?o:await Jt(o),n=new Float32Array(e),s=n.length,r=[...n.map(Math.abs)].sort((d,u)=>d-u),a=Math.floor(t.sparsity*r.length),c=r[a]??0,l=0;for(let d=0;d<n.length;d++)Math.abs(n[d]??0)<c&&(n[d]=0,l++);return{modelData:n.buffer,actualSparsity:l/s,parametersPruned:l,totalParameters:s}}async function $r(o){let t=o instanceof ArrayBuffer?o.byteLength:o.metadata.sizeBytes,e=Math.floor(t/4);return{totalParameters:e,sizeBytes:t,layers:[],estimatedFlops:e*2,memoryRequirements:{weights:t,activations:t*.1,total:t*1.1}}}async function Ur(o,t={}){let{warmupRuns:e=3,runs:n=10}=t;for(let h=0;h<e;h++)await o();let s=[];for(let h=0;h<n;h++){let p=performance.now();await o(),s.push(performance.now()-p)}let r=s.reduce((h,p)=>h+p,0)/s.length,a=Math.min(...s),c=Math.max(...s),d=s.map(h=>Math.pow(h-r,2)).reduce((h,p)=>h+p,0)/s.length,u=Math.sqrt(d);return{avgTime:r,minTime:a,maxTime:c,stdDev:u,throughput:1e3/r,times:s}}async function Br(o,t){let e=o instanceof ArrayBuffer?o:await Jt(o);switch(t){case"json":let n=new Float32Array(e);return JSON.stringify(Array.from(n));case"binary":case"onnx":default:return e}}async function dc(){let o=await Ee();return Array.from(o.values()).some(t=>t)}async function uc(){let o=await Ee();return o.get("webgpu")?"webgpu":o.get("webnn")?"webnn":o.get("wasm")?"wasm":null}async function hc(o){let t=new ee;await Promise.all(o.map(async e=>{if(!await t.get(e)){let n=await fetch(e);n.ok&&await t.put(e,n)}}))}var qr="0.3.0";async function fc(){let o=await Ee();return{version:qr,runtimes:{webgpu:o.get("webgpu")??!1,webnn:o.get("webnn")??!1,wasm:o.get("wasm")??!1,auto:!0},features:["concurrent-execution","batch-processing","memory-management","model-caching","quantization"]}}export{fe as AudioPreprocessor,E as BasePipeline,Te as Cache,Nn as EMOTION_LABELS,Ve as EdgeFlowDebugger,x as EdgeFlowError,w as EdgeFlowTensor,M as ErrorCodes,ne as FeatureExtractionPipeline,it as IMAGENET_LABELS,se as ImageClassificationPipeline,he as ImagePreprocessor,re as ImageSegmentationPipeline,rt as InferenceCache,ke as InferenceScheduler,H as LoadedModelImpl,ce as MemoryManager,Ae as MemoryScope,ze as ModelCache,ee as ModelDownloadCache,Oe as ONNXRuntime,Bt as POPULAR_MODELS,Ge as PerformanceMonitor,W as RuntimeManager,ot as SENTIMENT_LABELS,te as SentimentAnalysisPipeline,Y as TextClassificationPipeline,pe as TextGenerationPipeline,P as Tokenizer,Re as TransformersAdapterRuntime,qr as VERSION,Le as WASMRuntime,Pe as WebGPURuntime,_e as WebNNRuntime,dn as add,$r as analyzeModel,xs as analyzeModelDetailed,an as arange,gn as argmax,Ur as benchmark,hs as benchmarkMemory,ls as benchmarkSuite,Et as cancelPreload,zt as clearModelCache,cs as compareBenchmarks,Tr as compose,yn as concat,En as configureOnnxAssets,$s as configureScheduler,Is as createAsciiHistogram,Wn as createAudioPreprocessor,Cn as createBasicTokenizer,Pn as createCache,jn as createFeatureExtractionPipeline,Vn as createImageClassificationPipeline,at as createImagePreprocessor,Zn as createImageSegmentationPipeline,nt as createONNXRuntime,Mr as createPipelines,Bn as createSentimentAnalysisPipeline,As as createTensorHeatmap,Un as createTextClassificationPipeline,Xn as createTextGenerationPipeline,Sn as createWASMRuntime,An as createWebGPURuntime,zn as createWebNNRuntime,At as deleteCachedModel,Wt as dequantizeFloat16,ht as dequantizeInt8,ys as dequantizeTensor,jt as dequantizeUint8,ks as disableDebugging,yt as div,lt as downloadConfig,dt as downloadModel,Ut as downloadTokenizer,bs as enableDebugging,Br as exportModel,Ms as exportModelAdvanced,ln as eye,Ht as float16ToFloat32,ds as formatBenchmarkResult,us as formatComparisonResult,Ts as formatTensorInspection,rs as fromHub,as as fromTask,sn as full,Ws as gc,vs as generateAsciiDashboard,Ss as generateDashboardHTML,Ee as getAvailableRuntimes,er as getBestRuntime,uc as getBestRuntimeType,It as getCachedModel,Kt as getDebugger,qt as getDefaultModel,bn as getDeviceProfile,fc as getInfo,q as getMemoryManager,js as getMemoryStats,St as getModelCacheStats,is as getModelInfo,Yt as getMonitor,Ln as getPipelineFactory,nr as getPluginMiddleware,_t as getPluginPipeline,vt as getPreloadStatus,Dt as getPreloadedModel,Zs as getRuntimeManager,Ie as getScheduler,Fn as getTransformersAdapter,We as inspectTensor,ve as isModelCached,Nt as isOnnxAvailable,dc as isSupported,cn as linspace,sr as listPlugins,we as loadModel,v as loadModelData,F as loadModelFromBuffer,On as loadTokenizer,Rn as loadTokenizerFromHub,fn as matmul,mn as mean,os as modelExists,hn as mul,nn as ones,br as parallel,je as pipeline,hc as preload,bt as preloadModel,kt as preloadModels,Hn as preprocessText,Rr as prune,ws as pruneModel,Xt as pruneTensor,Pr as quantize,Gt as quantizeModel,gs as quantizeTensor,on as randn,rn as random,or as recommendModelVariant,kn as recommendQuantization,st as registerAllBackends,N as registerPipeline,tr as registerPlugin,de as registerRuntime,Hs as release,Ye as relu,ir as resetDeviceProfile,Js as runBatchInference,He as runBenchmark,X as runInference,U as runInferenceNamed,vn as setOnnxModule,xn as setScheduler,Je as sigmoid,O as softmax,Es as startMonitoring,Ds as stopMonitoring,un as sub,Qe as sum,pn as tanh,tn as tensor,rr as unregisterPlugin,Dn as useTransformersBackend,zs as visualizeModelArchitecture,Bs as withMemoryScope,qs as withMemoryScopeSync,gt as zeros};
|
|
449
449
|
//# sourceMappingURL=edgeflow.browser.min.js.map
|