edgeflowjs 0.1.0 → 0.2.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/README.md +200 -66
- package/dist/backends/index.d.ts +9 -2
- package/dist/backends/index.d.ts.map +1 -1
- package/dist/backends/index.js +13 -13
- package/dist/backends/index.js.map +1 -1
- package/dist/backends/onnx.d.ts +11 -4
- package/dist/backends/onnx.d.ts.map +1 -1
- package/dist/backends/onnx.js +97 -78
- package/dist/backends/onnx.js.map +1 -1
- package/dist/backends/transformers-adapter.d.ts +99 -0
- package/dist/backends/transformers-adapter.d.ts.map +1 -0
- package/dist/backends/transformers-adapter.js +171 -0
- package/dist/backends/transformers-adapter.js.map +1 -0
- package/dist/backends/webgpu.d.ts +7 -5
- package/dist/backends/webgpu.d.ts.map +1 -1
- package/dist/backends/webgpu.js +7 -5
- package/dist/backends/webgpu.js.map +1 -1
- package/dist/backends/webnn.d.ts +6 -5
- package/dist/backends/webnn.d.ts.map +1 -1
- package/dist/backends/webnn.js +6 -5
- package/dist/backends/webnn.js.map +1 -1
- package/dist/core/composer.d.ts +118 -0
- package/dist/core/composer.d.ts.map +1 -0
- package/dist/core/composer.js +163 -0
- package/dist/core/composer.js.map +1 -0
- package/dist/core/device-profiler.d.ts +75 -0
- package/dist/core/device-profiler.d.ts.map +1 -0
- package/dist/core/device-profiler.js +131 -0
- package/dist/core/device-profiler.js.map +1 -0
- package/dist/core/index.d.ts +4 -0
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +8 -0
- package/dist/core/index.js.map +1 -1
- package/dist/core/memory.d.ts +22 -2
- package/dist/core/memory.d.ts.map +1 -1
- package/dist/core/memory.js +49 -13
- package/dist/core/memory.js.map +1 -1
- package/dist/core/plugin.d.ts +100 -0
- package/dist/core/plugin.d.ts.map +1 -0
- package/dist/core/plugin.js +106 -0
- package/dist/core/plugin.js.map +1 -0
- package/dist/core/runtime.d.ts +4 -0
- package/dist/core/runtime.d.ts.map +1 -1
- package/dist/core/runtime.js +18 -0
- package/dist/core/runtime.js.map +1 -1
- package/dist/core/scheduler.d.ts +17 -0
- package/dist/core/scheduler.d.ts.map +1 -1
- package/dist/core/scheduler.js +101 -3
- package/dist/core/scheduler.js.map +1 -1
- package/dist/core/types.d.ts +14 -0
- package/dist/core/types.d.ts.map +1 -1
- package/dist/core/types.js.map +1 -1
- package/dist/core/worker.d.ts +202 -0
- package/dist/core/worker.d.ts.map +1 -0
- package/dist/core/worker.js +477 -0
- package/dist/core/worker.js.map +1 -0
- package/dist/edgeflow.browser.js +9770 -4383
- package/dist/edgeflow.browser.js.map +4 -4
- package/dist/edgeflow.browser.min.js +435 -5
- package/dist/edgeflow.browser.min.js.map +4 -4
- package/dist/index.d.ts +7 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +28 -10
- package/dist/index.js.map +1 -1
- package/dist/pipelines/automatic-speech-recognition.d.ts +63 -0
- package/dist/pipelines/automatic-speech-recognition.d.ts.map +1 -0
- package/dist/pipelines/automatic-speech-recognition.js +269 -0
- package/dist/pipelines/automatic-speech-recognition.js.map +1 -0
- package/dist/pipelines/base.d.ts +6 -1
- package/dist/pipelines/base.d.ts.map +1 -1
- package/dist/pipelines/base.js +12 -2
- package/dist/pipelines/base.js.map +1 -1
- package/dist/pipelines/feature-extraction.d.ts +5 -40
- package/dist/pipelines/feature-extraction.d.ts.map +1 -1
- package/dist/pipelines/feature-extraction.js +44 -63
- package/dist/pipelines/feature-extraction.js.map +1 -1
- package/dist/pipelines/image-classification.d.ts +4 -36
- package/dist/pipelines/image-classification.d.ts.map +1 -1
- package/dist/pipelines/image-classification.js +22 -60
- package/dist/pipelines/image-classification.js.map +1 -1
- package/dist/pipelines/image-segmentation.d.ts +221 -0
- package/dist/pipelines/image-segmentation.d.ts.map +1 -0
- package/dist/pipelines/image-segmentation.js +535 -0
- package/dist/pipelines/image-segmentation.js.map +1 -0
- package/dist/pipelines/index.d.ts +18 -0
- package/dist/pipelines/index.d.ts.map +1 -1
- package/dist/pipelines/index.js +51 -2
- package/dist/pipelines/index.js.map +1 -1
- package/dist/pipelines/object-detection.d.ts +44 -0
- package/dist/pipelines/object-detection.d.ts.map +1 -0
- package/dist/pipelines/object-detection.js +218 -0
- package/dist/pipelines/object-detection.js.map +1 -0
- package/dist/pipelines/question-answering.d.ts +41 -0
- package/dist/pipelines/question-answering.d.ts.map +1 -0
- package/dist/pipelines/question-answering.js +164 -0
- package/dist/pipelines/question-answering.js.map +1 -0
- package/dist/pipelines/text-classification.d.ts +3 -39
- package/dist/pipelines/text-classification.d.ts.map +1 -1
- package/dist/pipelines/text-classification.js +29 -67
- package/dist/pipelines/text-classification.js.map +1 -1
- package/dist/pipelines/text-generation.d.ts +281 -0
- package/dist/pipelines/text-generation.d.ts.map +1 -0
- package/dist/pipelines/text-generation.js +766 -0
- package/dist/pipelines/text-generation.js.map +1 -0
- package/dist/pipelines/zero-shot-classification.d.ts +45 -0
- package/dist/pipelines/zero-shot-classification.d.ts.map +1 -0
- package/dist/pipelines/zero-shot-classification.js +140 -0
- package/dist/pipelines/zero-shot-classification.js.map +1 -0
- package/dist/tools/benchmark.d.ts +92 -0
- package/dist/tools/benchmark.d.ts.map +1 -0
- package/dist/tools/benchmark.js +213 -0
- package/dist/tools/benchmark.js.map +1 -0
- package/dist/tools/debugger.d.ts +258 -0
- package/dist/tools/debugger.d.ts.map +1 -0
- package/dist/tools/debugger.js +624 -0
- package/dist/tools/debugger.js.map +1 -0
- package/dist/tools/index.d.ts +8 -0
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js +16 -0
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/monitor.d.ts +284 -0
- package/dist/tools/monitor.d.ts.map +1 -0
- package/dist/tools/monitor.js +921 -0
- package/dist/tools/monitor.js.map +1 -0
- package/dist/tools/quantization.d.ts +235 -0
- package/dist/tools/quantization.d.ts.map +1 -0
- package/dist/tools/quantization.js +830 -0
- package/dist/tools/quantization.js.map +1 -0
- package/dist/utils/hub.d.ts +162 -0
- package/dist/utils/hub.d.ts.map +1 -0
- package/dist/utils/hub.js +311 -0
- package/dist/utils/hub.js.map +1 -0
- package/dist/utils/index.d.ts +3 -1
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +5 -1
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/model-loader.d.ts.map +1 -1
- package/dist/utils/model-loader.js +106 -30
- package/dist/utils/model-loader.js.map +1 -1
- package/dist/utils/offline.d.ts +147 -0
- package/dist/utils/offline.d.ts.map +1 -0
- package/dist/utils/offline.js +405 -0
- package/dist/utils/offline.js.map +1 -0
- package/dist/utils/preprocessor.d.ts +82 -6
- package/dist/utils/preprocessor.d.ts.map +1 -1
- package/dist/utils/preprocessor.js +278 -21
- package/dist/utils/preprocessor.js.map +1 -1
- package/dist/utils/tokenizer.d.ts +197 -72
- package/dist/utils/tokenizer.d.ts.map +1 -1
- package/dist/utils/tokenizer.js +558 -274
- package/dist/utils/tokenizer.js.map +1 -1
- package/package.json +26 -11
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
var Ye=Object.defineProperty;var Mt=(r,e,t)=>e in r?Ye(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t;var St=(r,e)=>()=>(r&&(e=r(r=0)),e);var bt=(r,e)=>{for(var t in e)Ye(r,t,{get:e[t],enumerable:!0})};var h=(r,e,t)=>(Mt(r,typeof e!="symbol"?e+"":e,t),t);var st={};bt(st,{cancelPreload:()=>Ue,clearModelCache:()=>Re,deleteCachedModel:()=>Fe,getCachedModel:()=>Le,getModelCacheStats:()=>Oe,getPreloadStatus:()=>Be,getPreloadedModel:()=>qe,isModelCached:()=>Ce,loadModelData:()=>me,preloadModel:()=>Pe,preloadModels:()=>ve});async function ss(r){try{let e=await fetch(r,{method:"HEAD"}),t=e.headers.get("Accept-Ranges"),s=e.headers.get("Content-Length"),n=e.headers.get("ETag")??void 0;return{supports:t==="bytes",size:s?parseInt(s,10):0,etag:n}}catch{return{supports:!1,size:0}}}async function ns(r,e,t,s){let n=new AbortController,o=setTimeout(()=>n.abort(),s);try{let i=await fetch(r,{headers:{Range:`bytes=${e}-${t}`},signal:n.signal});if(i.status!==206&&i.status!==200)throw new Error(`HTTP ${i.status}: ${i.statusText}`);return await i.arrayBuffer()}finally{clearTimeout(o)}}async function rs(r,e){let{chunkSize:t=5*1024*1024,parallelConnections:s=4,timeout:n=3e4,onProgress:o}=e,{supports:i,size:a,etag:c}=await ss(r);if(!i||a<t*2)return tt(r,n,o);let l=await M.getDownloadState(r);if(!l||c&&l.totalSize!==a){let I=Math.ceil(a/t),j=[];for(let P=0;P<I;P++){let K=P*t,Se=Math.min(K+t-1,a-1);j.push({index:P,start:K,end:Se,downloaded:!1})}l={url:r,totalSize:a,downloadedSize:0,chunks:j,startedAt:Date.now()},await M.deleteModel(r)}let u=l.chunks.filter(I=>!I.downloaded),f=l.downloadedSize,w=Date.now(),A=f,x=()=>{if(!o)return;let I=Date.now(),j=(I-w)/1e3,P=f-A,K=j>0?P/j:0,Se=a-f,Tt=K>0?Se/K*1e3:0;o({loaded:f,total:a,percent:f/a*100,speed:K,eta:Tt,currentChunk:l.chunks.filter(It=>It.downloaded).length,totalChunks:l.chunks.length}),w=I,A=f},y=[...u],T=new Map;for(;y.length>0||T.size>0;){for(;y.length>0&&T.size<s;){let I=y.shift(),j=(async()=>{try{let P=await ns(r,I.start,I.end,n);await M.saveChunk(r,I.index,P),I.downloaded=!0,f+=P.byteLength,l.downloadedSize=f,await M.saveDownloadState(l),x()}finally{T.delete(I.index)}})();T.set(I.index,j)}T.size>0&&await Promise.race(T.values())}let S=await M.getChunks(r),R=new Uint8Array(a),Xe=0;for(let I of S)R.set(new Uint8Array(I),Xe),Xe+=I.byteLength;return await M.saveMeta({url:r,size:a,etag:c,cachedAt:Date.now(),chunks:S.length,complete:!0}),await M.deleteDownloadState(r),R.buffer}async function tt(r,e,t){let s=new AbortController,n=setTimeout(()=>s.abort(),e);try{let o=await fetch(r,{signal:s.signal});if(!o.ok)throw new Error(`HTTP ${o.status}: ${o.statusText}`);let i=o.headers.get("Content-Length"),a=i?parseInt(i,10):0;if(!o.body||!t||a===0)return await o.arrayBuffer();let c=o.body.getReader(),l=[],u=0,f=Date.now();for(;;){let{done:A,value:x}=await c.read();if(A)break;l.push(x),u+=x.length;let y=(Date.now()-f)/1e3,T=y>0?u/y:0,S=a-u,R=T>0?S/T*1e3:0;t({loaded:u,total:a,percent:u/a*100,speed:T,eta:R})}let d=new Uint8Array(u),w=0;for(let A of l)d.set(A,w),w+=A.length;return d.buffer}finally{clearTimeout(n)}}async function me(r,e={}){let{cache:t=!0,forceDownload:s=!1,resumable:n=!0}=e;if(t&&!s){let i=await M.getModel(r);if(i)return console.log(`\u2713 Model loaded from cache: ${r}`),e.onProgress?.({loaded:i.byteLength,total:i.byteLength,percent:100,speed:0,eta:0}),i}let o;return n?o=await rs(r,e):o=await tt(r,e.timeout??3e4,e.onProgress),t&&(n||(await M.saveChunk(r,0,o),await M.saveMeta({url:r,size:o.byteLength,cachedAt:Date.now(),chunks:1,complete:!0}))),o}function Pe(r,e={}){return ne.preload(r,e)}function ve(r,e={}){return Promise.all(r.map(({url:t,priority:s})=>ne.preload(t,{...e,priority:s})))}async function Ce(r){return(await M.getMeta(r))?.complete??!1}async function Le(r){return M.getModel(r)}async function Fe(r){return M.deleteModel(r)}async function Re(){return M.clear()}async function Oe(){return M.getStats()}function Be(r){return ne.getStatus(r)}function Ue(r){ne.cancel(r)}async function qe(r){return ne.get(r)}var ts,k,v,C,_e,M,Ne,ne,$e=St(()=>{"use strict";ts="edgeflow-model-cache",k="meta",v="chunks",C="download-state",_e=class{constructor(){h(this,"db",null);h(this,"dbPromise",null)}async openDB(){return this.db?this.db:this.dbPromise?this.dbPromise:(this.dbPromise=new Promise((e,t)=>{let s=indexedDB.open(ts,1);s.onupgradeneeded=n=>{let o=n.target.result;o.objectStoreNames.contains(k)||o.createObjectStore(k,{keyPath:"url"}),o.objectStoreNames.contains(v)||o.createObjectStore(v,{keyPath:["url","index"]}).createIndex("url","url",{unique:!1}),o.objectStoreNames.contains(C)||o.createObjectStore(C,{keyPath:"url"})},s.onsuccess=()=>{this.db=s.result,e(this.db)},s.onerror=()=>t(s.error)}),this.dbPromise)}async getMeta(e){let t=await this.openDB();return new Promise((s,n)=>{let a=t.transaction(k,"readonly").objectStore(k).get(e);a.onsuccess=()=>s(a.result??null),a.onerror=()=>n(a.error)})}async saveMeta(e){let t=await this.openDB();return new Promise((s,n)=>{let o=t.transaction(k,"readwrite");o.objectStore(k).put(e),o.oncomplete=()=>s(),o.onerror=()=>n(o.error)})}async saveChunk(e,t,s){let n=await this.openDB();return new Promise((o,i)=>{let a=n.transaction(v,"readwrite");a.objectStore(v).put({url:e,index:t,data:s}),a.oncomplete=()=>o(),a.onerror=()=>i(a.error)})}async getChunks(e){let t=await this.openDB();return new Promise((s,n)=>{let c=t.transaction(v,"readonly").objectStore(v).index("url").getAll(e);c.onsuccess=()=>{let l=c.result;l.sort((u,f)=>u.index-f.index),s(l.map(u=>u.data))},c.onerror=()=>n(c.error)})}async getModel(e){let t=await this.getMeta(e);if(!t||!t.complete)return null;let s=await this.getChunks(e);if(s.length===0)return null;let n=s.reduce((a,c)=>a+c.byteLength,0),o=new Uint8Array(n),i=0;for(let a of s)o.set(new Uint8Array(a),i),i+=a.byteLength;return o.buffer}async saveDownloadState(e){let t=await this.openDB();return new Promise((s,n)=>{let o=t.transaction(C,"readwrite");o.objectStore(C).put(e),o.oncomplete=()=>s(),o.onerror=()=>n(o.error)})}async getDownloadState(e){let t=await this.openDB();return new Promise((s,n)=>{let a=t.transaction(C,"readonly").objectStore(C).get(e);a.onsuccess=()=>s(a.result??null),a.onerror=()=>n(a.error)})}async deleteDownloadState(e){let t=await this.openDB();return new Promise((s,n)=>{let o=t.transaction(C,"readwrite");o.objectStore(C).delete(e),o.oncomplete=()=>s(),o.onerror=()=>n(o.error)})}async deleteModel(e){let t=await this.openDB();await new Promise((n,o)=>{let i=t.transaction(k,"readwrite");i.objectStore(k).delete(e),i.oncomplete=()=>n(),i.onerror=()=>o(i.error)}),(await this.getChunks(e)).length>0&&await new Promise((n,o)=>{let i=t.transaction(v,"readwrite"),l=i.objectStore(v).index("url").openCursor(IDBKeyRange.only(e));l.onsuccess=u=>{let f=u.target.result;f&&(f.delete(),f.continue())},i.oncomplete=()=>n(),i.onerror=()=>o(i.error)}),await this.deleteDownloadState(e)}async clear(){let e=await this.openDB(),t=[k,v,C];for(let s of t)await new Promise((n,o)=>{let i=e.transaction(s,"readwrite");i.objectStore(s).clear(),i.oncomplete=()=>n(),i.onerror=()=>o(i.error)})}async getStats(){let e=await this.openDB();return new Promise((t,s)=>{let i=e.transaction(k,"readonly").objectStore(k).getAll();i.onsuccess=()=>{let a=i.result;t({models:a.filter(c=>c.complete).length,totalSize:a.reduce((c,l)=>c+(l.complete?l.size:0),0)})},i.onerror=()=>s(i.error)})}},M=new _e;Ne=class{constructor(){h(this,"tasks",new Map);h(this,"queue",[]);h(this,"maxConcurrent",2);h(this,"activeCount",0)}preload(e,t={}){let s=this.tasks.get(e);if(s)return s.promise;let n,o,i=new Promise((l,u)=>{n=l,o=u}),a={url:e,priority:t.priority??0,options:t,promise:i,resolve:n,reject:o,status:"pending"};this.tasks.set(e,a);let c=this.queue.findIndex(l=>{let u=this.tasks.get(l);return u&&u.priority<a.priority});return c===-1?this.queue.push(e):this.queue.splice(c,0,e),this.processQueue(),i}async processQueue(){for(;this.queue.length>0&&this.activeCount<this.maxConcurrent;){let e=this.queue.shift();if(!e)break;let t=this.tasks.get(e);!t||t.status!=="pending"||(this.activeCount++,t.status="loading",this.downloadTask(t).finally(()=>{this.activeCount--,this.processQueue()}))}}async downloadTask(e){try{let t=await me(e.url,e.options);e.status="complete",e.resolve(t)}catch(t){e.status="error",e.reject(t instanceof Error?t:new Error(String(t)))}}isPreloaded(e){return this.tasks.get(e)?.status==="complete"}getStatus(e){return this.tasks.get(e)?.status??"not_found"}async get(e){let t=this.tasks.get(e);return t&&(t.status==="complete"||t.status==="loading")?t.promise:null}cancel(e){let t=this.tasks.get(e);t&&t.status==="pending"&&(this.tasks.delete(e),this.queue=this.queue.filter(s=>s!==e),t.reject(new Error("Preload cancelled")))}clear(){for(let[,e]of this.tasks)e.status==="pending"&&e.reject(new Error("Preload cleared"));this.tasks.clear(),this.queue=[]}},ne=new Ne});var p=class extends Error{constructor(t,s,n){super(t);h(this,"code");h(this,"details");this.code=s,this.details=n,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"};var Et=0;function kt(){return`tensor_${++Et}_${Date.now().toString(36)}`}function fe(r){switch(r){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 p(`Unsupported data type: ${r}`,m.INVALID_ARGUMENT,{dtype:r})}}function W(r){return r.length===0?1:r.reduce((e,t)=>e*t,1)}function zt(r){for(let e=0;e<r.length;e++){let t=r[e];if(t===void 0||!Number.isInteger(t)||t<0)throw new p(`Invalid shape dimension at index ${e}: ${t}`,m.INVALID_ARGUMENT,{shape:r,index:e,dimension:t})}}var g=class r{constructor(e,t,s="float32"){h(this,"id");h(this,"dtype");h(this,"shape");h(this,"size");h(this,"_data");h(this,"_isDisposed",!1);zt(t),this.id=kt(),this.dtype=s,this.shape=Object.freeze([...t]),this.size=W(this.shape);let n=this.size;if(e.length!==n)throw new p(`Data length (${e.length}) does not match shape ${JSON.stringify(t)} (expected ${n})`,m.TENSOR_SHAPE_MISMATCH,{dataLength:e.length,expectedSize:n,shape:t});if(e instanceof Array){let o=fe(s);if(this._data=new o(e.length),s==="int64"){let i=this._data;for(let a=0;a<e.length;a++)i[a]=BigInt(Math.round(e[a]??0))}else for(let i=0;i<e.length;i++)this._data[i]=e[i]??0}else this._data=e}get data(){return this.checkDisposed(),this._data}get isDisposed(){return this._isDisposed}checkDisposed(){if(this._isDisposed)throw new p("Cannot access disposed tensor",m.TENSOR_DISPOSED,{tensorId:this.id})}toFloat32Array(){if(this.checkDisposed(),this._data instanceof Float32Array)return this._data;let e=new Float32Array(this.size);for(let t=0;t<this.size;t++)e[t]=Number(this._data[t]??0);return e}toArray(){if(this.checkDisposed(),this.dtype==="int64"){let e=this._data,t=[];for(let s=0;s<e.length;s++)t.push(Number(e[s]));return t}return Array.from(this._data)}clone(){this.checkDisposed();let e=this._data.constructor,t=new e(this._data);return new r(t,this.shape,this.dtype)}dispose(){this._isDisposed||(this._isDisposed=!0,Object.assign(this,{_data:null}))}get(...e){if(this.checkDisposed(),e.length!==this.shape.length)throw new p(`Expected ${this.shape.length} indices, got ${e.length}`,m.INVALID_ARGUMENT,{expectedIndices:this.shape.length,gotIndices:e.length});let t=0,s=1;for(let n=this.shape.length-1;n>=0;n--){let o=e[n]??0,i=this.shape[n]??1;if(o<0||o>=i)throw new p(`Index ${o} out of bounds for dimension ${n} with size ${i}`,m.INVALID_ARGUMENT,{index:o,dimension:n,size:i});t+=o*s,s*=i}return Number(this._data[t]??0)}set(e,...t){if(this.checkDisposed(),t.length!==this.shape.length)throw new p(`Expected ${this.shape.length} indices, got ${t.length}`,m.INVALID_ARGUMENT,{expectedIndices:this.shape.length,gotIndices:t.length});let s=0,n=1;for(let o=this.shape.length-1;o>=0;o--){let i=t[o]??0,a=this.shape[o]??1;if(i<0||i>=a)throw new p(`Index ${i} out of bounds for dimension ${o} with size ${a}`,m.INVALID_ARGUMENT,{index:i,dimension:o,size:a});s+=i*n,n*=a}this._data[s]=e}reshape(e){this.checkDisposed();let t=W(e);if(t!==this.size)throw new p(`Cannot reshape tensor of size ${this.size} to shape ${JSON.stringify(e)} (size ${t})`,m.TENSOR_SHAPE_MISMATCH,{currentSize:this.size,newSize:t,newShape:e});let s=this._data.constructor,n=new s(this._data);return new r(n,e,this.dtype)}transpose(){if(this.checkDisposed(),this.shape.length!==2)throw new p("Transpose is currently only supported for 2D tensors",m.NOT_IMPLEMENTED,{shape:this.shape});let[e,t]=this.shape,s=new Float32Array(this.size);for(let n=0;n<e;n++)for(let o=0;o<t;o++)s[o*e+n]=Number(this._data[n*t+o]??0);return new r(s,[t,e],this.dtype)}toString(){return`Tensor(shape=[${this.shape.join(", ")}], dtype=${this.dtype})`}};function Dt(r,e,t="float32"){if(Array.isArray(r)&&r.length>0&&Array.isArray(r[0])){let n=r.length,o=r[0].length,i=[];for(let a of r){if(a.length!==o)throw new p("Nested arrays must have consistent dimensions",m.INVALID_ARGUMENT);i.push(...a)}return new g(i,e??[n,o],t)}let s=e??[r.length];return new g(r,s,t)}function Qe(r,e="float32"){let t=W(r),s=fe(e),n=new s(t);return new g(n,r,e)}function _t(r,e="float32"){let t=W(r),s=fe(e),n=new s(t);return n.fill(1),new g(n,r,e)}function Nt(r,e,t="float32"){let s=W(r),n=fe(t),o=new n(s);return o.fill(e),new g(o,r,t)}function Pt(r,e="float32"){let t=W(r),s=new Float32Array(t);for(let n=0;n<t;n++)s[n]=Math.random();return new g(s,r,e)}function vt(r,e="float32"){let t=W(r),s=new Float32Array(t);for(let n=0;n<t;n+=2){let o=Math.random(),i=Math.random(),a=Math.sqrt(-2*Math.log(o)),c=2*Math.PI*i;s[n]=a*Math.cos(c),n+1<t&&(s[n+1]=a*Math.sin(c))}return new g(s,r,e)}function Ct(r,e,t=1,s="float32"){e===void 0&&(e=r,r=0);let n=Math.ceil((e-r)/t),o=new Float32Array(n);for(let i=0;i<n;i++)o[i]=r+i*t;return new g(o,[n],s)}function Lt(r,e,t=50,s="float32"){let n=new Float32Array(t),o=(e-r)/(t-1);for(let i=0;i<t;i++)n[i]=r+i*o;return new g(n,[t],s)}function Ft(r,e="float32"){let t=new Float32Array(r*r);for(let s=0;s<r;s++)t[s*r+s]=1;return new g(t,[r,r],e)}function Rt(r,e){if(typeof e=="number"){let o=new Float32Array(r.size),i=r.toFloat32Array();for(let a=0;a<r.size;a++)o[a]=(i[a]??0)+e;return new g(o,r.shape,r.dtype)}if(r.size!==e.size)throw new p("Tensor sizes must match for element-wise operations",m.TENSOR_SHAPE_MISMATCH,{aShape:r.shape,bShape:e.shape});let t=new Float32Array(r.size),s=r.toFloat32Array(),n=e.toFloat32Array();for(let o=0;o<r.size;o++)t[o]=(s[o]??0)+(n[o]??0);return new g(t,r.shape,r.dtype)}function Ot(r,e){if(typeof e=="number"){let o=new Float32Array(r.size),i=r.toFloat32Array();for(let a=0;a<r.size;a++)o[a]=(i[a]??0)-e;return new g(o,r.shape,r.dtype)}if(r.size!==e.size)throw new p("Tensor sizes must match for element-wise operations",m.TENSOR_SHAPE_MISMATCH,{aShape:r.shape,bShape:e.shape});let t=new Float32Array(r.size),s=r.toFloat32Array(),n=e.toFloat32Array();for(let o=0;o<r.size;o++)t[o]=(s[o]??0)-(n[o]??0);return new g(t,r.shape,r.dtype)}function Bt(r,e){if(typeof e=="number"){let o=new Float32Array(r.size),i=r.toFloat32Array();for(let a=0;a<r.size;a++)o[a]=(i[a]??0)*e;return new g(o,r.shape,r.dtype)}if(r.size!==e.size)throw new p("Tensor sizes must match for element-wise operations",m.TENSOR_SHAPE_MISMATCH,{aShape:r.shape,bShape:e.shape});let t=new Float32Array(r.size),s=r.toFloat32Array(),n=e.toFloat32Array();for(let o=0;o<r.size;o++)t[o]=(s[o]??0)*(n[o]??0);return new g(t,r.shape,r.dtype)}function Je(r,e){if(typeof e=="number"){let o=new Float32Array(r.size),i=r.toFloat32Array();for(let a=0;a<r.size;a++)o[a]=(i[a]??0)/e;return new g(o,r.shape,r.dtype)}if(r.size!==e.size)throw new p("Tensor sizes must match for element-wise operations",m.TENSOR_SHAPE_MISMATCH,{aShape:r.shape,bShape:e.shape});let t=new Float32Array(r.size),s=r.toFloat32Array(),n=e.toFloat32Array();for(let o=0;o<r.size;o++)t[o]=(s[o]??0)/(n[o]??0);return new g(t,r.shape,r.dtype)}function Ut(r,e){if(r.shape.length!==2||e.shape.length!==2)throw new p("matmul requires 2D tensors",m.INVALID_ARGUMENT,{aShape:r.shape,bShape:e.shape});let[t,s]=r.shape,[n,o]=e.shape;if(s!==n)throw new p(`Matrix dimensions incompatible for multiplication: (${t}x${s}) @ (${n}x${o})`,m.TENSOR_SHAPE_MISMATCH,{aShape:r.shape,bShape:e.shape});let i=new Float32Array(t*o),a=r.toFloat32Array(),c=e.toFloat32Array();for(let l=0;l<t;l++)for(let u=0;u<o;u++){let f=0;for(let d=0;d<s;d++)f+=(a[l*s+d]??0)*(c[d*o+u]??0);i[l*o+u]=f}return new g(i,[t,o],r.dtype)}function G(r,e=-1){let t=r.toFloat32Array(),s=new Float32Array(r.size),n=e<0?r.shape.length+e:e;if(n<0||n>=r.shape.length)throw new p(`Invalid axis ${e} for tensor with ${r.shape.length} dimensions`,m.INVALID_ARGUMENT,{axis:e,shape:r.shape});if(r.shape.length===1){let o=-1/0;for(let a=0;a<r.size;a++)(t[a]??0)>o&&(o=t[a]??0);let i=0;for(let a=0;a<r.size;a++)s[a]=Math.exp((t[a]??0)-o),i+=s[a]??0;for(let a=0;a<r.size;a++)s[a]=(s[a]??0)/i;return new g(s,r.shape,r.dtype)}if(r.shape.length===2&&n===1){let[o,i]=r.shape;for(let a=0;a<o;a++){let c=-1/0;for(let u=0;u<i;u++)(t[a*i+u]??0)>c&&(c=t[a*i+u]??0);let l=0;for(let u=0;u<i;u++)s[a*i+u]=Math.exp((t[a*i+u]??0)-c),l+=s[a*i+u]??0;for(let u=0;u<i;u++)s[a*i+u]=(s[a*i+u]??0)/l}return new g(s,r.shape,r.dtype)}throw new p("Softmax currently only supports 1D tensors or 2D tensors along the last axis",m.NOT_IMPLEMENTED,{shape:r.shape,axis:e})}function Ee(r){let e=r.toFloat32Array(),t=new Float32Array(r.size);for(let s=0;s<r.size;s++)t[s]=Math.max(0,e[s]??0);return new g(t,r.shape,r.dtype)}function ke(r){let e=r.toFloat32Array(),t=new Float32Array(r.size);for(let s=0;s<r.size;s++)t[s]=1/(1+Math.exp(-(e[s]??0)));return new g(t,r.shape,r.dtype)}function qt(r){let e=r.toFloat32Array(),t=new Float32Array(r.size);for(let s=0;s<r.size;s++)t[s]=Math.tanh(e[s]??0);return new g(t,r.shape,r.dtype)}function be(r,e){let t=r.toFloat32Array();if(e===void 0){let o=0;for(let i=0;i<r.size;i++)o+=t[i]??0;return o}let s=e<0?r.shape.length+e:e;if(s<0||s>=r.shape.length)throw new p(`Invalid axis ${e} for tensor with ${r.shape.length} dimensions`,m.INVALID_ARGUMENT,{axis:e,shape:r.shape});let n=[...r.shape];if(n.splice(s,1),n.length===0){let o=0;for(let i=0;i<r.size;i++)o+=t[i]??0;return o}if(r.shape.length===2){let[o,i]=r.shape;if(s===0){let a=new Float32Array(i);for(let c=0;c<i;c++)for(let l=0;l<o;l++)a[c]=(a[c]??0)+(t[l*i+c]??0);return new g(a,[i],r.dtype)}else{let a=new Float32Array(o);for(let c=0;c<o;c++)for(let l=0;l<i;l++)a[c]=(a[c]??0)+(t[c*i+l]??0);return new g(a,[o],r.dtype)}}throw new p("Sum along axis currently only supports up to 2D tensors",m.NOT_IMPLEMENTED,{shape:r.shape,axis:e})}function $t(r,e){if(e===void 0)return be(r)/r.size;let t=be(r,e);if(typeof t=="number")return t/(r.shape[e]??1);let s=r.shape[e]??1;return Je(t,s)}function jt(r,e){let t=r.toFloat32Array();if(e===void 0){let n=0,o=t[0]??-1/0;for(let i=1;i<r.size;i++)(t[i]??-1/0)>o&&(o=t[i]??-1/0,n=i);return n}let s=e<0?r.shape.length+e:e;if(r.shape.length===2&&s===1){let[n,o]=r.shape,i=new Float32Array(n);for(let a=0;a<n;a++){let c=0,l=t[a*o]??-1/0;for(let u=1;u<o;u++)(t[a*o+u]??-1/0)>l&&(l=t[a*o+u]??-1/0,c=u);i[a]=c}return new g(i,[n],"int32")}throw new p("Argmax along axis currently only supports 2D tensors along the last axis",m.NOT_IMPLEMENTED,{shape:r.shape,axis:e})}function Wt(r,e=0){if(r.length===0)throw new p("Cannot concatenate empty array of tensors",m.INVALID_ARGUMENT);if(r.length===1)return r[0]?.clone()??Qe([0]);let t=r[0];if(!t)throw new p("First tensor is undefined",m.INVALID_ARGUMENT);let s=e<0?t.shape.length+e:e;for(let i=1;i<r.length;i++){let a=r[i];if(a){if(a.shape.length!==t.shape.length)throw new p("All tensors must have the same number of dimensions",m.TENSOR_SHAPE_MISMATCH);for(let c=0;c<t.shape.length;c++)if(c!==s&&t.shape[c]!==a.shape[c])throw new p(`Shape mismatch at dimension ${c}`,m.TENSOR_SHAPE_MISMATCH)}}let n=[...t.shape],o=0;for(let i of r)i&&(o+=i.shape[s]??0);if(n[s]=o,t.shape.length===1){let i=new Float32Array(o),a=0;for(let c of r)c&&(i.set(c.toFloat32Array(),a),a+=c.size);return new g(i,n,t.dtype)}throw new p("Concatenation currently only supports 1D tensors",m.NOT_IMPLEMENTED)}var ze=class{constructor(e,t,s,n){h(this,"id");h(this,"modelId");h(this,"priority");h(this,"createdAt");h(this,"_status","pending");h(this,"_startedAt");h(this,"_completedAt");h(this,"_result");h(this,"_error");h(this,"_executor");h(this,"_resolvers",[]);h(this,"_cancelled",!1);this.id=e,this.modelId=t,this.priority=s,this.createdAt=Date.now(),this._executor=n}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 e=new p("Task was cancelled",m.INFERENCE_CANCELLED,{taskId:this.id});for(let{reject:t}of this._resolvers)t(e);this._resolvers=[]}}wait(){return this._status==="completed"?Promise.resolve(this._result):this._status==="failed"?Promise.reject(this._error):this._status==="cancelled"?Promise.reject(new p("Task was cancelled",m.INFERENCE_CANCELLED,{taskId:this.id})):new Promise((e,t)=>{this._resolvers.push({resolve:e,reject:t})})}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:e}of this._resolvers)e(this._result)}catch(e){this._error=e instanceof Error?e:new Error(String(e)),this._status="failed",this._completedAt=Date.now();for(let{reject:t}of this._resolvers)t(this._error)}this._resolvers=[]}}},Ze={critical:0,high:1,normal:2,low:3},De=class{constructor(){h(this,"items",[])}get length(){return this.items.length}isEmpty(){return this.items.length===0}enqueue(e){let t=!1;for(let s=0;s<this.items.length;s++){let n=this.items[s];if(n&&Ze[e.priority]<Ze[n.priority]){this.items.splice(s,0,e),t=!0;break}}t||this.items.push(e)}dequeue(){return this.items.shift()}peek(){return this.items[0]}remove(e){let t=this.items.findIndex(s=>s.id===e);if(t!==-1){let[s]=this.items.splice(t,1);return s}}getAll(){return[...this.items]}clear(){this.items=[]}};var Gt=0;function Vt(){return`task_${++Gt}_${Date.now().toString(36)}`}var Ht={maxConcurrentTasks:4,maxConcurrentPerModel:1,defaultTimeout:3e4,enableBatching:!1,maxBatchSize:32,batchTimeout:50},ee=class{constructor(e={}){h(this,"options");h(this,"queues",new Map);h(this,"runningTasks",new Map);h(this,"allTasks",new Map);h(this,"batchers",new Map);h(this,"listeners",new Map);h(this,"globalRunningCount",0);h(this,"isProcessing",!1);h(this,"disposed",!1);this.options={...Ht,...e}}getQueue(e){let t=this.queues.get(e);return t||(t=new De,this.queues.set(e,t)),t}getRunningSet(e){let t=this.runningTasks.get(e);return t||(t=new Set,this.runningTasks.set(e,t)),t}canStartTask(e){if(this.globalRunningCount>=this.options.maxConcurrentTasks)return!1;let t=this.runningTasks.get(e);return!(t&&t.size>=this.options.maxConcurrentPerModel)}async processQueue(){if(this.isProcessing||this.disposed)return;this.isProcessing=!0;try{let t=[];for(let[s,n]of this.queues)for(;!n.isEmpty()&&this.canStartTask(s);){let o=n.dequeue();o&&o.status==="pending"&&(t.push(o),this.getRunningSet(s).add(o.id),this.globalRunningCount++)}await Promise.all(t.map(async s=>{this.emit("inference:start",{taskId:s.id,modelId:s.modelId});try{await s.execute(),this.emit("inference:complete",{taskId:s.id,modelId:s.modelId,duration:(s.completedAt??0)-(s.startedAt??0)})}catch(n){this.emit("inference:error",{taskId:s.id,modelId:s.modelId,error:n})}finally{let n=this.runningTasks.get(s.modelId);n&&n.delete(s.id),this.globalRunningCount--}}))}finally{this.isProcessing=!1}let e=!1;for(let t of this.queues.values())if(!t.isEmpty()){e=!0;break}e&&setTimeout(()=>this.processQueue(),0)}schedule(e,t,s="normal"){if(this.disposed)throw new p("Scheduler has been disposed",m.RUNTIME_NOT_INITIALIZED);let n=new ze(Vt(),e,s,t);return this.allTasks.set(n.id,n),this.getQueue(e).enqueue(n),this.processQueue(),n}scheduleWithTimeout(e,t,s=this.options.defaultTimeout,n="normal"){let o=()=>new Promise((i,a)=>{let c=setTimeout(()=>{a(new p(`Task timed out after ${s}ms`,m.INFERENCE_TIMEOUT,{timeout:s}))},s);t().then(l=>{clearTimeout(c),i(l)}).catch(l=>{clearTimeout(c),a(l)})});return this.schedule(e,o,n)}async scheduleAll(e){let t=e.map(({modelId:s,executor:n,priority:o})=>this.schedule(s,n,o));return Promise.all(t.map(s=>s.wait()))}getTask(e){return this.allTasks.get(e)}cancelTask(e){let t=this.allTasks.get(e);if(t&&t.status==="pending"){t.cancel();for(let s of this.queues.values())s.remove(e);return!0}return!1}cancelAllForModel(e){let t=this.queues.get(e);if(!t)return 0;let s=0;for(let n of t.getAll())n.status==="pending"&&(n.cancel(),s++);return t.clear(),s}getStats(){let e={totalTasks:this.allTasks.size,pendingTasks:0,runningTasks:0,completedTasks:0,failedTasks:0,cancelledTasks:0,queuedByModel:{}};for(let t of this.allTasks.values())switch(t.status){case"pending":e.pendingTasks++;break;case"running":e.runningTasks++;break;case"completed":e.completedTasks++;break;case"failed":e.failedTasks++;break;case"cancelled":e.cancelledTasks++;break}for(let[t,s]of this.queues)e.queuedByModel[t]=s.length;return e}on(e,t){let s=this.listeners.get(e);s||(s=new Set,this.listeners.set(e,s)),s.add(t)}off(e,t){let s=this.listeners.get(e);s&&s.delete(t)}emit(e,t){let s={type:e,timestamp:Date.now(),data:t},n=this.listeners.get(e);if(n)for(let o of n)try{o(s)}catch(i){console.error("Error in event listener:",i)}}clearHistory(){for(let[e,t]of this.allTasks)(t.status==="completed"||t.status==="failed"||t.status==="cancelled")&&this.allTasks.delete(e)}dispose(){this.disposed=!0;for(let e of this.queues.values()){for(let t of e.getAll())t.cancel();e.clear()}for(let e of this.batchers.values())e.clear();this.queues.clear(),this.runningTasks.clear(),this.allTasks.clear(),this.batchers.clear(),this.listeners.clear()}},X=null;function pe(){return X||(X=new ee),X}function et(r){X&&X.dispose(),X=r}function Kt(r){et(new ee(r))}var Xt={initialSize:67108864,maxSize:536870912,growthFactor:1.5,autoGC:!0,gcThreshold:.8},b=class b{constructor(e={}){h(this,"config");h(this,"resources",new Map);h(this,"disposers",new Map);h(this,"listeners",new Map);h(this,"allocated",0);h(this,"peak",0);h(this,"gcScheduled",!1);h(this,"disposed",!1);this.config={...Xt,...e}}static getInstance(){return b.instance||(b.instance=new b),b.instance}static configure(e){b.instance&&console.warn("MemoryManager already initialized, configuration may not apply"),b.instance=new b(e)}track(e,t){if(this.disposed)return;let s=this.estimateTensorSize(e);this.resources.set(e.id,{id:e.id,type:"tensor",size:s,createdAt:Date.now(),stackTrace:this.captureStackTrace()}),t&&this.disposers.set(e.id,t),this.allocated+=s,this.peak=Math.max(this.peak,this.allocated),this.checkMemoryThreshold()}trackModel(e,t){if(this.disposed)return;let s=e.metadata.sizeBytes;this.resources.set(e.id,{id:e.id,type:"model",size:s,createdAt:Date.now(),stackTrace:this.captureStackTrace()}),t&&this.disposers.set(e.id,t),this.allocated+=s,this.peak=Math.max(this.peak,this.allocated),this.checkMemoryThreshold()}untrack(e){let t=this.resources.get(e);t&&(this.allocated-=t.size,this.resources.delete(e),this.disposers.delete(e))}release(e){let t=typeof e=="string"?e:e.id,s=this.disposers.get(t);if(s)try{s()}catch(n){console.error("Error disposing resource:",n)}this.untrack(t)}estimateTensorSize(e){let t=this.getBytesPerElement(e.dtype);return e.size*t}getBytesPerElement(e){switch(e){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 e={};return Error.captureStackTrace(e,this.captureStackTrace),e.stack}return new Error().stack}checkMemoryThreshold(){if(!this.config.autoGC)return;let e=this.allocated/this.config.maxSize;e>=this.config.gcThreshold&&!this.gcScheduled&&(this.gcScheduled=!0,this.emit("memory:warning",{allocated:this.allocated,maxSize:this.config.maxSize,usage:e}),setTimeout(()=>{this.gc(),this.gcScheduled=!1},0))}gc(){this.emit("memory:gc",{before:this.allocated});let e=Date.now(),t=[];for(let[s,n]of this.resources)e-n.createdAt>5*60*1e3&&t.push(s);this.emit("memory:gc",{after:this.allocated,potentialCleanup:t.length})}getStats(){let e=0,t=0;for(let s of this.resources.values())s.type==="tensor"?e++:t++;return{allocated:this.allocated,used:this.allocated,peak:this.peak,tensorCount:e,modelCount:t}}getResourceDetails(){return Array.from(this.resources.values())}detectLeaks(e=10*60*1e3){let t=Date.now(),s=[];for(let n of this.resources.values())t-n.createdAt>e&&s.push(n);return s}on(e,t){let s=this.listeners.get(e);s||(s=new Set,this.listeners.set(e,s)),s.add(t)}off(e,t){let s=this.listeners.get(e);s&&s.delete(t)}emit(e,t){let s={type:e,timestamp:Date.now(),data:t},n=this.listeners.get(e);if(n)for(let o of n)try{o(s)}catch(i){console.error("Error in event listener:",i)}}resetStats(){this.peak=this.allocated}disposeAll(){for(let e of this.resources.keys())this.release(e)}dispose(){this.disposeAll(),this.disposed=!0,this.listeners.clear(),b.instance=null}};h(b,"instance",null);var V=b,te=class r{constructor(e){h(this,"resources",[]);h(this,"children",[]);h(this,"parent",null);e&&(this.parent=e,e.children.push(this))}track(e){return this.resources.push(e),e}createChild(){return new r(this)}keep(e){let t=this.resources.indexOf(e);return t!==-1&&this.resources.splice(t,1),e}dispose(){for(let e of this.children)e.dispose();this.children=[];for(let e=this.resources.length-1;e>=0;e--)try{this.resources[e]?.dispose()}catch(t){console.error("Error disposing resource in scope:",t)}if(this.resources=[],this.parent){let e=this.parent.children.indexOf(this);e!==-1&&this.parent.children.splice(e,1),this.parent=null}}};async function Yt(r){let e=new te;try{return await r(e)}finally{e.dispose()}}function Qt(r){let e=new te;try{return r(e)}finally{e.dispose()}}var se=class{constructor(e={}){h(this,"maxSize");h(this,"maxModels");h(this,"cache",new Map);h(this,"currentSize",0);this.maxSize=e.maxSize??256*1024*1024,this.maxModels=e.maxModels??5}get(e){let t=this.cache.get(e);if(t)return t.lastAccess=Date.now(),t.model}set(e,t){let s=t.metadata.sizeBytes;for(;(this.currentSize+s>this.maxSize||this.cache.size>=this.maxModels)&&this.cache.size>0;)this.evictLRU();this.cache.set(e,{model:t,size:s,lastAccess:Date.now()}),this.currentSize+=s}delete(e){let t=this.cache.get(e);return t?(t.model.dispose(),this.currentSize-=t.size,this.cache.delete(e),!0):!1}has(e){return this.cache.has(e)}evictLRU(){let e=null,t=1/0;for(let[s,n]of this.cache)n.lastAccess<t&&(t=n.lastAccess,e=s);e&&this.delete(e)}clear(){for(let e of this.cache.values())e.model.dispose();this.cache.clear(),this.currentSize=0}getStats(){return{size:this.currentSize,count:this.cache.size,maxSize:this.maxSize,maxModels:this.maxModels}}};function E(){return V.getInstance()}function Jt(){return V.getInstance().getStats()}function Zt(r){V.getInstance().release(r)}function es(){V.getInstance().gc()}var ge=new Map,O=new Map,je=["webgpu","webnn","wasm"],H=class H{constructor(){h(this,"listeners",new Map);h(this,"defaultRuntime","auto")}static getInstance(){return H.instance||(H.instance=new H),H.instance}register(e,t){ge.set(e,t)}async getRuntime(e="auto"){if(e==="auto")return this.getBestRuntime();let t=O.get(e);if(t)return t;let s=ge.get(e);if(!s)throw new p(`Runtime '${e}' is not registered`,m.RUNTIME_NOT_AVAILABLE,{runtime:e});if(t=s(),!await t.isAvailable())throw new p(`Runtime '${e}' is not available in this environment`,m.RUNTIME_NOT_AVAILABLE,{runtime:e});try{await t.initialize()}catch(o){throw new p(`Failed to initialize runtime '${e}': ${o instanceof Error?o.message:String(o)}`,m.RUNTIME_INIT_FAILED,{runtime:e,error:o})}return O.set(e,t),this.emit("runtime:ready",{runtime:e}),t}async getBestRuntime(){for(let e of je)try{let t=O.get(e);if(t)return t;let s=ge.get(e);if(!s)continue;let n=s();if(await n.isAvailable())return await n.initialize(),O.set(e,n),this.emit("runtime:ready",{runtime:e}),n}catch{continue}throw new p("No runtime available. Please ensure WebGPU, WebNN, or WASM is supported.",m.RUNTIME_NOT_AVAILABLE,{triedRuntimes:je})}async detectAvailableRuntimes(){let e=new Map;for(let t of je){let s=ge.get(t);if(!s){e.set(t,!1);continue}try{let n=s();e.set(t,await n.isAvailable())}catch{e.set(t,!1)}}return e}async getCapabilities(e){return(await this.getRuntime(e)).capabilities}setDefaultRuntime(e){this.defaultRuntime=e}getDefaultRuntimeType(){return this.defaultRuntime}disposeRuntime(e){let t=O.get(e);t&&(t.dispose(),O.delete(e))}disposeAll(){for(let[e,t]of O)t.dispose(),O.delete(e)}on(e,t){let s=this.listeners.get(e);s||(s=new Set,this.listeners.set(e,s)),s.add(t)}off(e,t){let s=this.listeners.get(e);s&&s.delete(t)}emit(e,t){let s={type:e,timestamp:Date.now(),data:t},n=this.listeners.get(e);if(n)for(let o of n)try{o(s)}catch(i){console.error("Error in event listener:",i)}}};h(H,"instance",null);var z=H,os=0;function is(){return`model_${++os}_${Date.now().toString(36)}`}var D=class{constructor(e,t,s){h(this,"id");h(this,"metadata");h(this,"runtime");h(this,"_isLoaded",!0);h(this,"_dispose");this.id=is(),this.metadata=e,this.runtime=t,this._dispose=s}get isLoaded(){return this._isLoaded}dispose(){this._isLoaded&&(this._isLoaded=!1,this._dispose(),E().untrack(this.id))}};async function We(r,e={}){let s=await z.getInstance().getRuntime(e.runtime??"auto"),{loadModelData:n}=await Promise.resolve().then(()=>($e(),st)),o=await n(r,{cache:e.cache??!0,resumable:e.resumable??!0,chunkSize:e.chunkSize,forceDownload:e.forceDownload,onProgress:e.onProgress?a=>{e.onProgress(a.percent/100)}:void 0});return await s.loadModel(o,e)}async function as(r,e={}){return(await z.getInstance().getRuntime(e.runtime??"auto")).loadModel(r,e)}async function Ge(r,e){if(!r.isLoaded)throw new p("Model has been disposed",m.MODEL_NOT_LOADED,{modelId:r.id});let s=await z.getInstance().getRuntime(r.runtime);return pe().schedule(r.id,()=>s.run(r,e)).wait()}async function cs(r,e){let t=pe(),n=await z.getInstance().getRuntime(r.runtime),o=e.map(i=>t.schedule(r.id,()=>n.run(r,i)));return Promise.all(o.map(i=>i.wait()))}function ls(){return z.getInstance()}function re(r,e){z.getInstance().register(r,e)}async function us(){return z.getInstance().getBestRuntime()}async function oe(){return z.getInstance().detectAvailableRuntimes()}var Y={STORAGE:128,COPY_SRC:4,COPY_DST:8,MAP_READ:1},nt={COMPUTE:4},ie=class{constructor(){h(this,"name","webgpu");h(this,"adapter",null);h(this,"device",null);h(this,"models",new Map);h(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 p("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 p("Failed to get WebGPU adapter",m.RUNTIME_INIT_FAILED);this.device=await this.adapter.requestDevice({requiredFeatures:[],requiredLimits:{}}),this.device.lost.then(e=>{console.error("WebGPU device was lost:",e.message),this.initialized=!1,this.device=null}),this.initialized=!0}}async loadModel(e,t={}){this.ensureInitialized();let s=this.parseModelData(e),n={shaders:new Map,pipelines:new Map,weights:new Map,bindGroupLayouts:[],config:s};await this.uploadWeights(e,n),await this.createPipelines(n);let o=`webgpu_${Date.now().toString(36)}`;this.models.set(o,n);let i={name:s.name||t.metadata?.name||"unknown",version:s.version,inputs:s.inputs.map(c=>({name:c.name,dtype:c.dtype,shape:c.shape})),outputs:s.outputs.map(c=>({name:c.name,dtype:c.dtype,shape:c.shape})),sizeBytes:e.byteLength,quantization:t.quantization??"float32",format:"edgeflow"},a=new D(i,"webgpu",()=>this.unloadModel(o));return E().trackModel(a,()=>a.dispose()),a}async run(e,t){return this.ensureInitialized(),this.executeModel(t,e.metadata)}async executeModel(e,t){let s=this.device,n=[];for(let o of t.outputs){let i=o.shape.reduce((u,f)=>u*f,1),a=s.createBuffer({size:i*4,usage:Y.STORAGE|Y.COPY_SRC}),c=s.createBuffer({size:i*4,usage:Y.MAP_READ|Y.COPY_DST}),l=new Float32Array(i);if(e.length>0&&e[0]){let u=e[0].toFloat32Array();for(let f=0;f<Math.min(i,u.length);f++)l[f]=u[f]??0}n.push(new g(l,o.shape,"float32")),a.destroy(),c.destroy()}return n}parseModelData(e){try{let t=new TextDecoder,s=t.decode(new Uint8Array(e,0,Math.min(1024,e.byteLength)));if(s.trim().startsWith("{")){let n=s.indexOf(`
|
|
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(`
|
|
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>;
|
|
5
5
|
@group(0) @binding(1) var<storage, read_write> output: array<f32>;
|
|
6
6
|
|
|
@@ -11,9 +11,439 @@ var Ye=Object.defineProperty;var Mt=(r,e,t)=>e in r?Ye(r,e,{enumerable:!0,config
|
|
|
11
11
|
output[idx] = input[idx];
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
|
-
`});
|
|
14
|
+
`});t.shaders.set("default",s);let i=e.createBindGroupLayout({entries:[{binding:0,visibility:Mn.COMPUTE,buffer:{type:"read-only-storage"}},{binding:1,visibility:Mn.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 Tn(){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
|
-
`);
|
|
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 bn(){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=Qe(a);else if(s.name.includes("sigmoid"))r=Ke(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(n===-1)try{return JSON.parse(s)}catch{n=e.byteLength}let o=t.decode(new Uint8Array(e,0,n));return JSON.parse(o)}}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(e,t){}unloadModel(e){let t=this.models.get(e);if(t&&this.module)for(let s of t.weights.values())this.module.exports.free(s.ptr);this.models.delete(e)}ensureInitialized(){if(!this.initialized||!this.module)throw new p("WASM runtime is not initialized",m.RUNTIME_NOT_INITIALIZED)}hasSIMDSupport(){return this.simdSupported}dispose(){for(let e of this.models.keys())this.unloadModel(e);this.module=null,this.initialized=!1}};function rt(){return new ce}var it="1.17.0",Ae=`https://cdn.jsdelivr.net/npm/onnxruntime-web@${it}/dist/`,ot=`${Ae}ort.min.js`,_=null,xe=null;async function hs(){return _||xe||(xe=new Promise((r,e)=>{if(typeof window<"u"&&window.ort){_=window.ort,_.env.wasm.wasmPaths=Ae,r(_);return}let t=document.createElement("script");t.src=ot,t.async=!0,t.onload=()=>{window.ort?(_=window.ort,_.env.wasm.wasmPaths=Ae,console.log(`\u2713 ONNX Runtime v${it} loaded from CDN`),r(_)):e(new Error("ONNX Runtime loaded but ort global not found"))},t.onerror=()=>{e(new Error(`Failed to load ONNX Runtime from ${ot}`))},document.head.appendChild(t)}),xe)}async function Ve(){return _||(_=await hs()),_}var le=new Map,He=class{constructor(){h(this,"name","wasm");h(this,"initialized",!1);h(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!0}async initialize(){if(this.initialized)return;let e=await Ve();e.env.wasm.wasmPaths=Ae,this.executionProvider="wasm",this.initialized=!0}async loadModel(e,t={}){this.initialized||await this.initialize();let s=await Ve();try{let n={executionProviders:[this.executionProvider],graphOptimizationLevel:"all"},o=new Uint8Array(e),i=await s.InferenceSession.create(o,n),a=i.inputNames,c=i.outputNames,l=`onnx_${Date.now().toString(36)}_${Math.random().toString(36).slice(2,8)}`;le.set(l,{session:i,inputNames:[...a],outputNames:[...c]});let u={name:t.metadata?.name??"onnx-model",version:"1.0.0",inputs:a.map(d=>({name:d,dtype:"float32",shape:[-1]})),outputs:c.map(d=>({name:d,dtype:"float32",shape:[-1]})),sizeBytes:e.byteLength,quantization:t.quantization??"float32",format:"onnx"},f=new D(u,"wasm",()=>this.unloadModel(l));return Object.defineProperty(f,"id",{value:l,writable:!1}),E().trackModel(f,()=>f.dispose()),f}catch(n){throw new p(`Failed to load ONNX model: ${n instanceof Error?n.message:String(n)}`,m.MODEL_LOAD_FAILED,{error:n})}}async run(e,t){let s=le.get(e.id);if(!s)throw new p(`ONNX session not found for model ${e.id}`,m.MODEL_NOT_LOADED,{modelId:e.id});let n=await Ve(),{session:o,inputNames:i,outputNames:a}=s;try{let c={};for(let f=0;f<Math.min(t.length,i.length);f++){let d=i[f],w=t[f];if(d&&w){let A=w.dtype,x;if(A==="int64"){let y=w.data;x=new n.Tensor("int64",y,w.shape)}else if(A==="int32"){let y=w.data;x=new n.Tensor("int32",y,w.shape)}else{let y=w.toFloat32Array();x=new n.Tensor("float32",y,w.shape)}c[d]=x}}let l=await o.run(c),u=[];for(let f of a){let d=l[f];if(d){let w=d.data,A=Array.from(d.dims).map(x=>Number(x));u.push(new g(new Float32Array(w),A,"float32"))}}return u}catch(c){throw new p(`ONNX inference failed: ${c instanceof Error?c.message:String(c)}`,m.INFERENCE_FAILED,{modelId:e.id,error:c})}}async unloadModel(e){le.get(e)&&le.delete(e)}dispose(){le.clear(),this.initialized=!1}};function at(){return new He}function ct(){re("webgpu",we),re("webnn",ye),re("wasm",at)}ct();var Q=class{constructor(e={}){h(this,"options");h(this,"cache",new Map);h(this,"currentSize",0);h(this,"hits",0);h(this,"misses",0);this.options={strategy:e.strategy??"lru",maxSize:e.maxSize??100*1024*1024,maxEntries:e.maxEntries??1e3,ttl:e.ttl??0,persistent:e.persistent??!1,name:e.name??"edgeflow-cache"},this.options.persistent&&this.loadFromStorage()}get(e){let t=this.cache.get(e);if(!t){this.misses++;return}if(t.ttl&&Date.now()-t.createdAt>t.ttl){this.delete(e),this.misses++;return}return t.accessedAt=Date.now(),t.accessCount++,this.hits++,t.value}set(e,t,s,n){for(this.cache.has(e)&&this.delete(e);(this.currentSize+s>this.options.maxSize||this.cache.size>=this.options.maxEntries)&&this.cache.size>0;)this.evict();let o=n!==void 0?n:this.options.ttl>0?this.options.ttl:void 0,i={value:t,size:s,createdAt:Date.now(),accessedAt:Date.now(),accessCount:1,ttl:o};this.cache.set(e,i),this.currentSize+=s,this.options.persistent&&this.saveToStorage()}has(e){let t=this.cache.get(e);return t?t.ttl&&Date.now()-t.createdAt>t.ttl?(this.delete(e),!1):!0:!1}delete(e){let t=this.cache.get(e);return t?(this.currentSize-=t.size,this.cache.delete(e),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 e=this.hits+this.misses;return{entries:this.cache.size,size:this.currentSize,hits:this.hits,misses:this.misses,hitRate:e>0?this.hits/e:0}}evict(){let e=null;switch(this.options.strategy){case"lru":e=this.findLRU();break;case"lfu":e=this.findLFU();break;case"fifo":e=this.findOldest();break;case"ttl":e=this.findExpired()??this.findOldest();break}e&&this.delete(e)}findLRU(){let e=null,t=1/0;for(let[s,n]of this.cache)n.accessedAt<t&&(t=n.accessedAt,e=s);return e}findLFU(){let e=null,t=1/0;for(let[s,n]of this.cache)n.accessCount<t&&(t=n.accessCount,e=s);return e}findOldest(){let e=null,t=1/0;for(let[s,n]of this.cache)n.createdAt<t&&(t=n.createdAt,e=s);return e}findExpired(){let e=Date.now();for(let[t,s]of this.cache)if(s.ttl&&e-s.createdAt>s.ttl)return t;return null}async loadFromStorage(){if(!(typeof indexedDB>"u"))try{let n=(await this.openDB()).transaction("cache","readonly").objectStore("cache").getAll();return new Promise((o,i)=>{n.onsuccess=()=>{let a=n.result;for(let{key:c,entry:l}of a)this.cache.set(c,l),this.currentSize+=l.size;o()},n.onerror=()=>i(n.error)})}catch{}}async saveToStorage(){if(!(typeof indexedDB>"u"))try{let t=(await this.openDB()).transaction("cache","readwrite"),s=t.objectStore("cache");s.clear();for(let[n,o]of this.cache)s.put({key:n,entry:o});return new Promise((n,o)=>{t.oncomplete=()=>n(),t.onerror=()=>o(t.error)})}catch{}}async clearStorage(){if(!(typeof indexedDB>"u"))try{(await this.openDB()).transaction("cache","readwrite").objectStore("cache").clear()}catch{}}openDB(){return new Promise((e,t)=>{let s=indexedDB.open(this.options.name,1);s.onupgradeneeded=()=>{let n=s.result;n.objectStoreNames.contains("cache")||n.createObjectStore("cache",{keyPath:"key"})},s.onsuccess=()=>e(s.result),s.onerror=()=>t(s.error)})}},Te=class extends Q{generateKey(e,t){let s=Array.isArray(t)?t:Array.from(t),n=this.hashArray(s);return`${e}:${n}`}hashArray(e){let t=0,s=e.length>100?e.filter((n,o)=>o%Math.floor(e.length/100)===0):e;for(let n=0;n<s.length;n++){let o=s[n]??0;t=(t<<5)-t+(o*1e3|0),t|=0}return t.toString(36)}},B=class{constructor(e="edgeflow-models"){h(this,"cacheName");h(this,"cache",null);this.cacheName=e}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(e){try{return await(await this.ensureCache()).match(e)??void 0}catch{return}}async put(e,t){try{await(await this.ensureCache()).put(e,t.clone())}catch{}}async delete(e){try{return await(await this.ensureCache()).delete(e)}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(s=>s.url)}catch{return[]}}};function lt(r="medium",e={}){let t={small:{maxSize:10485760,maxEntries:100},medium:{maxSize:104857600,maxEntries:500},large:{maxSize:524288e3,maxEntries:2e3},custom:{}};return new Q({...t[r],...e})}var N=class{constructor(e){h(this,"model",null);h(this,"config");h(this,"modelCache");h(this,"downloadCache");h(this,"isReady",!1);this.config=e,this.modelCache=new se,this.downloadCache=new B}async initialize(){if(this.isReady&&this.model)return;let e=this.modelCache.get(this.config.model);if(e){this.model=e,this.isReady=!0;return}this.model=await this.loadModelWithCache(this.config.model),this.isReady=!0}async loadModelWithCache(e){let t=await this.downloadCache.get(e);try{let s=await fetch(e);s.ok&&await this.downloadCache.put(e,s.clone())}catch{}return We(e,{runtime:this.config.runtime,quantization:this.config.quantization,cache:this.config.cache})}async run(e,t){await this.initialize();let s=performance.now(),n=await this.preprocess(e),o=await Ge(this.model,n),i=await this.postprocess(o,t);return i&&typeof i=="object"&&"processingTime"in i&&(i.processingTime=performance.now()-s),i}async runBatch(e,t){return await this.initialize(),await Promise.all(e.map(n=>this.run(n,t)))}get task(){return this.config.task}get ready(){return this.isReady}dispose(){this.model&&(this.model.dispose(),this.model=null),this.isReady=!1}},ut=new Map;function L(r,e){ut.set(r,e)}function ht(r){return ut.get(r)}var ue=["negative","positive"],dt=["anger","disgust","fear","joy","sadness","surprise","neutral"],Ie=["tench","goldfish","great white shark","tiger shark","hammerhead","electric ray","stingray","cock","hen","ostrich"];var J=class{constructor(e,t={}){h(this,"vocab");h(this,"reverseVocab");h(this,"config");h(this,"model");h(this,"merges",new Map);this.config={vocabSize:e.vocabSize??30522,maxLength:e.maxLength??512,padTokenId:e.padTokenId??0,unkTokenId:e.unkTokenId??100,bosTokenId:e.bosTokenId,eosTokenId:e.eosTokenId,sepTokenId:e.sepTokenId??102,clsTokenId:e.clsTokenId??101,maskTokenId:e.maskTokenId??103},this.model=t.model??"basic",this.vocab=new Map,this.reverseVocab=new Map,t.vocab&&this.loadVocab(t.vocab),t.merges&&this.loadMerges(t.merges)}loadVocab(e){e instanceof Map?this.vocab=new Map(e):this.vocab=new Map(Object.entries(e));for(let[t,s]of this.vocab)this.reverseVocab.set(s,t)}loadMerges(e){for(let t of e){let[s,n]=t.split(" ");s&&n&&this.merges.set(`${s} ${n}`,`${s}${n}`)}}encode(e,t={}){let{addSpecialTokens:s=!0,maxLength:n=this.config.maxLength,padding:o="max_length",truncation:i=!0,returnAttentionMask:a=!0,returnTokenTypeIds:c=!1}=t,l=this.tokenize(e);s&&(l=this.addSpecialTokens(l));let u=this.convertTokensToIds(l);i&&u.length>n&&(u=u.slice(0,n),s&&this.config.sepTokenId!==void 0&&(u[u.length-1]=this.config.sepTokenId));let f=a?u.map(()=>1):[];if(o==="max_length"&&u.length<n){let w=n-u.length;u=[...u,...new Array(w).fill(this.config.padTokenId)],a&&f.push(...new Array(w).fill(0))}let d={inputIds:u,attentionMask:f};return c&&(d.tokenTypeIds=u.map(()=>0)),d}encodeBatch(e,t={}){let s=t.maxLength??this.config.maxLength;if(t.padding==="longest"){let n=e.map(o=>this.encode(o,{...t,padding:"do_not_pad"}));s=Math.max(...n.map(o=>o.inputIds.length))}return e.map(n=>this.encode(n,{...t,maxLength:s}))}decode(e,t=!0){let s=this.convertIdsToTokens(e),n=t?s.filter(o=>!this.isSpecialToken(o)):s;return this.detokenize(n)}tokenize(e){let t=this.normalize(e);switch(this.model){case"bpe":return this.tokenizeBPE(t);case"wordpiece":return this.tokenizeWordPiece(t);default:return this.tokenizeBasic(t)}}normalize(e){return e.toLowerCase().replace(/[^\w\s'-]/g," $& ").replace(/\s+/g," ").trim()}tokenizeBasic(e){return e.split(/\s+/).filter(t=>t.length>0)}tokenizeWordPiece(e){let t=e.split(/\s+/).filter(n=>n.length>0),s=[];for(let n of t){let o=this.tokenizeWord(n);s.push(...o)}return s}tokenizeWord(e){if(this.vocab.has(e))return[e];let t=[],s=0;for(;s<e.length;){let n=e.length,o=!1;for(;s<n;){let i=s===0?e.slice(s,n):`##${e.slice(s,n)}`;if(this.vocab.has(i)){t.push(i),o=!0;break}n--}o?s=n:(t.push("[UNK]"),s++)}return t}tokenizeBPE(e){let t=e.split(/\s+/).filter(n=>n.length>0),s=[];for(let n of t){let o=n.split("").map((i,a)=>a===n.length-1?i+"</w>":i);for(;o.length>1;){let i=null,a=1/0;for(let f=0;f<o.length-1;f++){let d=`${o[f]} ${o[f+1]}`;if(this.merges.has(d)){let w=Array.from(this.merges.keys()).indexOf(d);w<a&&(a=w,i=[f,d])}}if(!i)break;let[c,l]=i,u=this.merges.get(l);o=[...o.slice(0,c),u,...o.slice(c+2)]}s.push(...o)}return s}addSpecialTokens(e){let t=[];return this.config.clsTokenId!==void 0&&t.push("[CLS]"),t.push(...e),this.config.sepTokenId!==void 0&&t.push("[SEP]"),t}convertTokensToIds(e){return e.map(t=>{let s=this.vocab.get(t);return s!==void 0?s:t==="[CLS]"?this.config.clsTokenId??this.config.unkTokenId:t==="[SEP]"?this.config.sepTokenId??this.config.unkTokenId:t==="[PAD]"?this.config.padTokenId:t==="[MASK]"?this.config.maskTokenId??this.config.unkTokenId:t==="[UNK]"?this.config.unkTokenId:this.config.unkTokenId})}convertIdsToTokens(e){return e.map(t=>{let s=this.reverseVocab.get(t);return s!==void 0?s:t===this.config.clsTokenId?"[CLS]":t===this.config.sepTokenId?"[SEP]":t===this.config.padTokenId?"[PAD]":t===this.config.maskTokenId?"[MASK]":(t===this.config.unkTokenId,"[UNK]")})}isSpecialToken(e){return["[CLS]","[SEP]","[PAD]","[MASK]","[UNK]"].includes(e)}detokenize(e){return e.join(" ").replace(/ ##/g,"").replace(/<\/w>/g," ").trim()}get vocabSize(){return this.vocab.size}getConfig(){return{...this.config}}};function Z(){let r={"[PAD]":0,"[UNK]":1,"[CLS]":2,"[SEP]":3,"[MASK]":4},e=["the","a","an","is","are","was","were","be","been","being","have","has","had","do","does","did","will","would","could","should","may","might","must","shall","can","need","dare","ought","used","i","you","he","she","it","we","they","me","him","her","us","them","my","your","his","its","our","their","mine","yours","hers","ours","theirs","this","that","these","those","what","which","who","whom","whose","and","but","or","nor","for","yet","so","as","if","when","while","not","no","yes","all","any","both","each","every","few","more","most","other","some","such","only","own","same","than","too","very","good","bad","great","new","old","high","low","big","small","long","short","love","like","hate","want","need","think","know","feel","see","hear"],t=5;for(let s of e)r[s]=t++;return new J({vocabSize:t,maxLength:128,padTokenId:0,unkTokenId:1,clsTokenId:2,sepTokenId:3,maskTokenId:4},{vocab:r,model:"basic"})}async function ft(r){let e=await fetch(r);if(!e.ok)throw new p(`Failed to load tokenizer from ${r}`,m.MODEL_NOT_FOUND);let t=await e.json();return new J(t.config??{},{vocab:t.vocab,merges:t.merges,model:t.model})}var F=class extends N{constructor(t,s){super(t);h(this,"tokenizer",null);h(this,"labels");this.labels=s??ue}async initialize(){await super.initialize(),this.tokenizer||(this.tokenizer=Z())}setLabels(t){this.labels=t}async run(t,s){let n=Array.isArray(t),o=n?t:[t];await this.initialize();let i=performance.now(),a=[];for(let l of o){let u=await this.preprocess(l),f=await this.runInference(u),d=await this.postprocess(f,s);a.push(d)}let c=performance.now()-i;for(let l of a)l.processingTime=c/a.length;return n?a:a[0]}async preprocess(t){let s=Array.isArray(t)?t[0]:t,n=this.tokenizer.encode(s,{maxLength:128,padding:"max_length",truncation:!0}),o=new g(new Float32Array(n.inputIds),[1,n.inputIds.length],"float32"),i=new g(new Float32Array(n.attentionMask),[1,n.attentionMask.length],"float32");return[o,i]}async runInference(t){let s=this.labels.length,n=new Float32Array(s),i=(t[0]?.toFloat32Array()??new Float32Array(0)).reduce((a,c)=>a+c,0);for(let a=0;a<s;a++)n[a]=Math.sin(i*(a+1))*2;return[new g(n,[1,s],"float32")]}async postprocess(t,s){let n=t[0];if(!n)return{label:"unknown",score:0};let i=G(n,-1).toFloat32Array(),a=s?.topK??1;(s?.returnAllScores??!1)||a>1;let l=0,u=i[0]??0;for(let d=1;d<i.length;d++)(i[d]??0)>u&&(u=i[d]??0,l=d);return{label:s?.labels?.[l]??this.labels[l]??`class_${l}`,score:u}}},U=class extends F{constructor(e){super(e,ue)}async analyze(e,t){return this.run(e,t)}};function pt(r={}){return new F({task:"text-classification",model:r.model??"default",runtime:r.runtime,cache:r.cache??!0,quantization:r.quantization})}function mt(r={}){return new U({task:"sentiment-analysis",model:r.model??"default",runtime:r.runtime,cache:r.cache??!0,quantization:r.quantization})}L("text-classification",r=>new F(r));L("sentiment-analysis",r=>new U(r));var q=class extends N{constructor(t,s=768){super(t);h(this,"tokenizer",null);h(this,"embeddingDim");this.embeddingDim=s}async initialize(){await super.initialize(),this.tokenizer||(this.tokenizer=Z())}async run(t,s){let n=Array.isArray(t),o=n?t:[t];await this.initialize();let i=performance.now(),a=[];for(let l of o){let u=await this.preprocess(l),f=await this.runInference(u),d=await this.postprocess(f,s);a.push(d)}let c=performance.now()-i;for(let l of a)l.processingTime=c/a.length;return n?a:a[0]}async preprocess(t){let s=Array.isArray(t)?t[0]:t,n=this.tokenizer.encode(s,{maxLength:128,padding:"max_length",truncation:!0}),o=new g(new Float32Array(n.inputIds),[1,n.inputIds.length],"float32"),i=new g(new Float32Array(n.attentionMask),[1,n.attentionMask.length],"float32");return[o,i]}async runInference(t){let s=t[0]?.shape[1]??128,n=new Float32Array(s*this.embeddingDim),o=t[0]?.toFloat32Array()??new Float32Array(0);for(let i=0;i<s;i++)for(let a=0;a<this.embeddingDim;a++){let c=o[i]??0;n[i*this.embeddingDim+a]=Math.sin(c*(a+1)*.01)*.1}return[new g(n,[1,s,this.embeddingDim],"float32")]}async postprocess(t,s){let n=t[0];if(!n)return{embeddings:[]};let o=s?.pooling??"mean",i=s?.normalize??!0,a;switch(o){case"cls":a=this.extractCLSEmbedding(n);break;case"max":a=this.maxPooling(n);break;case"none":a=n.toArray();break;case"mean":default:a=this.meanPooling(n);break}return i&&(a=this.normalizeVector(a)),s?.outputDim&&s.outputDim<a.length&&(a=a.slice(0,s.outputDim)),{embeddings:a}}extractCLSEmbedding(t){let s=t.toFloat32Array(),n=t.shape[2]??this.embeddingDim;return Array.from(s.slice(0,n))}meanPooling(t){let s=t.toFloat32Array(),n=t.shape[1]??1,o=t.shape[2]??this.embeddingDim,i=new Float32Array(o);for(let a=0;a<n;a++)for(let c=0;c<o;c++)i[c]=(i[c]??0)+(s[a*o+c]??0)/n;return Array.from(i)}maxPooling(t){let s=t.toFloat32Array(),n=t.shape[1]??1,o=t.shape[2]??this.embeddingDim,i=new Array(o).fill(-1/0);for(let a=0;a<n;a++)for(let c=0;c<o;c++){let l=s[a*o+c]??0;l>(i[c]??-1/0)&&(i[c]=l)}return i}normalizeVector(t){let s=0;for(let n of t)s+=n*n;return s=Math.sqrt(s),s===0?t:t.map(n=>n/s)}};function gt(r={}){return new q({task:"feature-extraction",model:r.model??"default",runtime:r.runtime,cache:r.cache??!0,quantization:r.quantization})}L("feature-extraction",r=>new q(r));var ds={width:224,height:224,resizeMode:"cover",mean:[.485,.456,.406],std:[.229,.224,.225],grayscale:!1,channelFormat:"CHW",dtype:"float32"},he=class{constructor(e={}){h(this,"options");h(this,"canvas",null);h(this,"ctx",null);this.options={...ds,...e}}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(e){let t;typeof e=="string"?t=await this.loadFromUrl(e):e instanceof ImageData?t=e:t=this.toImageData(e);let s=this.resize(t);return this.toTensor(s)}async processBatch(e){let t=await Promise.all(e.map(l=>this.process(l))),s=t.length,n=t[0];if(!n)return new g(new Float32Array(0),[0],"float32");let o=n.shape[0]??3,i=n.shape[1]??this.options.height,a=n.shape[2]??this.options.width,c=new Float32Array(s*o*i*a);for(let l=0;l<t.length;l++){let u=t[l];u&&c.set(u.toFloat32Array(),l*o*i*a)}return new g(c,[s,o,i,a],"float32")}async loadFromUrl(e){return new Promise((t,s)=>{let n=new Image;n.crossOrigin="anonymous",n.onload=()=>{t(this.toImageData(n))},n.onerror=()=>{s(new Error(`Failed to load image from ${e}`))},n.src=e})}toImageData(e){this.ensureCanvas();let{width:t,height:s}=e;return this.canvas.width=t,this.canvas.height=s,this.ctx.drawImage(e,0,0),this.ctx.getImageData(0,0,t,s)}resize(e){let{width:t,height:s,resizeMode:n}=this.options;this.ensureCanvas();let o=0,i=0,a=e.width,c=e.height,l=0,u=0,f=t,d=s;if(n==="contain"){let x=Math.min(t/e.width,s/e.height);f=Math.round(e.width*x),d=Math.round(e.height*x),l=Math.round((t-f)/2),u=Math.round((s-d)/2)}else if(n==="cover"){let x=Math.max(t/e.width,s/e.height);a=Math.round(t/x),c=Math.round(s/x),o=Math.round((e.width-a)/2),i=Math.round((e.height-c)/2)}let w=document.createElement("canvas");return w.width=e.width,w.height=e.height,w.getContext("2d").putImageData(e,0,0),this.canvas.width=t,this.canvas.height=s,(n==="contain"||n==="pad")&&(this.ctx.fillStyle="black",this.ctx.fillRect(0,0,t,s)),this.ctx.drawImage(w,o,i,a,c,l,u,f,d),this.ctx.getImageData(0,0,t,s)}toTensor(e){let{width:t,height:s,mean:n,std:o,grayscale:i,channelFormat:a,dtype:c}=this.options,l=i?1:3,u=new Float32Array(l*s*t),f=e.data;for(let w=0;w<s;w++)for(let A=0;A<t;A++){let x=(w*t+A)*4;if(i){let y=(.299*(f[x]??0)+.587*(f[x+1]??0)+.114*(f[x+2]??0))/255,T=w*t+A;u[T]=(y-(n[0]??0))/(o[0]??1)}else if(a==="CHW")for(let y=0;y<3;y++){let S=((f[x+y]??0)/255-(n[y]??0))/(o[y]??1),R=y*s*t+w*t+A;u[R]=S}else for(let y=0;y<3;y++){let S=((f[x+y]??0)/255-(n[y]??0))/(o[y]??1),R=w*t*3+A*3+y;u[R]=S}}let d=a==="CHW"?[l,s,t]:[s,t,l];return new g(u,d,c)}},fs={sampleRate:16e3,nMels:80,nFft:400,hopLength:160,normalize:!0,maxDuration:30},de=class{constructor(e={}){h(this,"options");h(this,"audioContext",null);this.options={...fs,...e}}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(e){let t;typeof e=="string"?t=await this.loadFromUrl(e):e instanceof AudioBuffer?t=this.audioBufferToFloat32(e):e instanceof Float32Array?t=e:t=await this.decodeAudioData(e),this.options.normalize&&(t=this.normalizeAudio(t));let s=this.options.maxDuration*this.options.sampleRate;return t.length>s&&(t=t.slice(0,s)),this.computeMelSpectrogram(t)}async loadFromUrl(e){let t=await fetch(e);if(!t.ok)throw new Error(`Failed to load audio from ${e}`);let s=await t.arrayBuffer();return this.decodeAudioData(s)}async decodeAudioData(e){this.ensureAudioContext();let t=await this.audioContext.decodeAudioData(e);return this.audioBufferToFloat32(t)}audioBufferToFloat32(e){let t=e.getChannelData(0);return new Float32Array(t)}normalizeAudio(e){let t=0;for(let s=0;s<e.length;s++){let n=Math.abs(e[s]??0);n>t&&(t=n)}if(t>0){let s=new Float32Array(e.length);for(let n=0;n<e.length;n++)s[n]=(e[n]??0)/t;return s}return e}computeMelSpectrogram(e){let{nMels:t,nFft:s,hopLength:n}=this.options,o=Math.floor((e.length-s)/n)+1;if(o<=0)return new g(new Float32Array(t),[1,t],"float32");let i=new Float32Array(o*t);for(let a=0;a<o;a++){let c=a*n;for(let l=0;l<t;l++){let u=0,f=Math.floor(l/t*(s/2)),d=Math.floor((l+1)/t*(s/2));for(let w=f;w<Math.min(d,s);w++){let A=e[c+w]??0;u+=A*A}i[a*t+l]=Math.log(u+1e-10)}}return new g(i,[o,t],"float32")}dispose(){this.audioContext&&(this.audioContext.close(),this.audioContext=null)}};function wt(r,e={}){let{lowercase:t=!0,removePunctuation:s=!1,normalizeWhitespace:n=!0,maxLength:o}=e,i=r;return t&&(i=i.toLowerCase()),s&&(i=i.replace(/[^\w\s]/g,"")),n&&(i=i.replace(/\s+/g," ").trim()),o&&i.length>o&&(i=i.slice(0,o)),i}function Me(r="imagenet",e={}){let t={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({...t[r],...e})}function yt(r="whisper",e={}){let t={whisper:{sampleRate:16e3,nMels:80,nFft:400,hopLength:160},wav2vec:{sampleRate:16e3,normalize:!0},custom:{}};return new de({...t[r],...e})}var $=class extends N{constructor(t,s,n=1e3){super(t);h(this,"preprocessor",null);h(this,"labels");h(this,"numClasses");this.labels=s??Ie,this.numClasses=n}async initialize(){await super.initialize(),this.preprocessor||(this.preprocessor=Me("imagenet"))}setLabels(t){this.labels=t,this.numClasses=t.length}async run(t,s){let n=Array.isArray(t),o=n?t:[t];await this.initialize();let i=performance.now(),a=[];for(let l of o){let u=await this.preprocess(l),f=await this.runInference(u),d=await this.postprocess(f,s);a.push(d)}let c=performance.now()-i;for(let l of a)l.processingTime=c/a.length;return n?a:a[0]}async preprocess(t){let s=Array.isArray(t)?t[0]:t,n=await this.preprocessor.process(s);return n.shape.length===3?[n.reshape([1,...n.shape])]:[n]}async runInference(t){let s=new Float32Array(this.numClasses),n=t[0]?.toFloat32Array()??new Float32Array(0),o=0;for(let i=0;i<Math.min(1e3,n.length);i++)o+=n[i]??0;for(let i=0;i<this.numClasses;i++)s[i]=Math.sin(o*(i+1)*.1)*3;return[new g(s,[1,this.numClasses],"float32")]}async postprocess(t,s){let n=t[0];if(!n)return{label:"unknown",score:0};let i=G(n,-1).toFloat32Array();(s?.topK??1)>1||s?.returnAllScores;let c=0,l=i[0]??0;for(let f=1;f<i.length;f++)(i[f]??0)>l&&(l=i[f]??0,c=f);return{label:s?.labels?.[c]??this.labels[c]??`class_${c}`,score:l}}};function xt(r={},e){return new $({task:"image-classification",model:r.model??"default",runtime:r.runtime,cache:r.cache??!0,quantization:r.quantization},e)}L("image-classification",r=>new $(r));async function At(r,e){let t={task:r,model:e?.model??"default",runtime:e?.runtime,cache:e?.cache??!0,quantization:e?.quantization},s;switch(r){case"text-classification":s=new F(t,e?.labels);break;case"sentiment-analysis":s=new U(t);break;case"feature-extraction":s=new q(t);break;case"image-classification":s=new $(t,e?.labels);break;default:throw new Error(`Unknown pipeline task: ${r}`)}return await s.initialize(),s}async function ps(r,e){let t=await Promise.all(r.map(n=>At(n,e))),s={};for(let n=0;n<r.length;n++){let o=r[n];s[o]=t[n]}return s}$e();async function ms(r,e){let t=r instanceof ArrayBuffer?r:await Ke(r),s=t.byteLength,n,o=0,i=0;switch(e.method){case"int8":({data:n,layersQuantized:o,layersSkipped:i}=gs(t,e));break;case"uint8":({data:n,layersQuantized:o,layersSkipped:i}=ws(t,e));break;case"float16":({data:n,layersQuantized:o,layersSkipped:i}=ys(t,e));break;case"int4":({data:n,layersQuantized:o,layersSkipped:i}=xs(t,e));break;default:n=t}return{modelData:n,originalSize:s,quantizedSize:n.byteLength,compressionRatio:s/n.byteLength,stats:{layersQuantized:o,layersSkipped:i}}}async function Ke(r){return new ArrayBuffer(0)}function gs(r,e){let t=new Float32Array(r),s=new Int8Array(t.length),n=0;for(let i=0;i<t.length;i++){let a=Math.abs(t[i]??0);a>n&&(n=a)}let o=n/127;for(let i=0;i<t.length;i++)s[i]=Math.round((t[i]??0)/o);return{data:s.buffer,layersQuantized:1,layersSkipped:0}}function ws(r,e){let t=new Float32Array(r),s=new Uint8Array(t.length),n=1/0,o=-1/0;for(let a=0;a<t.length;a++){let c=t[a]??0;c<n&&(n=c),c>o&&(o=c)}let i=(o-n)/255;for(let a=0;a<t.length;a++)s[a]=Math.round(((t[a]??0)-n)/i);return{data:s.buffer,layersQuantized:1,layersSkipped:0}}function ys(r,e){let t=new Float32Array(r),s=new Uint16Array(t.length);for(let n=0;n<t.length;n++)s[n]=As(t[n]??0);return{data:s.buffer,layersQuantized:1,layersSkipped:0}}function xs(r,e){let t=new Float32Array(r),s=new Uint8Array(Math.ceil(t.length/2)),n=0;for(let i=0;i<t.length;i++){let a=Math.abs(t[i]??0);a>n&&(n=a)}let o=n/7;for(let i=0;i<t.length;i+=2){let a=Math.round((t[i]??0)/o)+8,c=Math.round((t[i+1]??0)/o)+8;s[i/2]=(a&15)<<4|c&15}return{data:s.buffer,layersQuantized:1,layersSkipped:0}}function As(r){let e=new Float32Array(1),t=new Int32Array(e.buffer);e[0]=r;let s=t[0]??0,n=s>>16&32768,o=s>>12&2047,i=s>>23&255;return i<103?n:i>142?(n|=31744,n|=(i===255?0:1)&&s&8388607,n):i<113?(o|=2048,n|=(o>>114-i)+(o>>113-i&1),n):(n|=i-112<<10|o>>1,n+=o&1,n)}async function Ts(r,e){let t=r instanceof ArrayBuffer?r:await Ke(r),s=new Float32Array(t),n=s.length,i=[...s.map(Math.abs)].sort((u,f)=>u-f),a=Math.floor(e.sparsity*i.length),c=i[a]??0,l=0;for(let u=0;u<s.length;u++)Math.abs(s[u]??0)<c&&(s[u]=0,l++);return{modelData:s.buffer,actualSparsity:l/n,parametersPruned:l,totalParameters:n}}async function Is(r){let e=r instanceof ArrayBuffer?r.byteLength:r.metadata.sizeBytes,t=Math.floor(e/4);return{totalParameters:t,sizeBytes:e,layers:[],estimatedFlops:t*2,memoryRequirements:{weights:e,activations:e*.1,total:e*1.1}}}async function Ms(r,e={}){let{warmupRuns:t=3,runs:s=10}=e;for(let d=0;d<t;d++)await r();let n=[];for(let d=0;d<s;d++){let w=performance.now();await r(),n.push(performance.now()-w)}let i=n.reduce((d,w)=>d+w,0)/n.length,a=Math.min(...n),c=Math.max(...n),u=n.map(d=>Math.pow(d-i,2)).reduce((d,w)=>d+w,0)/n.length,f=Math.sqrt(u);return{avgTime:i,minTime:a,maxTime:c,stdDev:f,throughput:1e3/i,times:n}}async function Ss(r,e){let t=r instanceof ArrayBuffer?r:await Ke(r);switch(e){case"json":let s=new Float32Array(t);return JSON.stringify(Array.from(s));case"binary":case"onnx":default:return t}}async function ur(){let r=await oe();return Array.from(r.values()).some(e=>e)}async function hr(){let r=await oe();return r.get("webgpu")?"webgpu":r.get("webnn")?"webnn":r.get("wasm")?"wasm":null}async function dr(r){let e=new B;await Promise.all(r.map(async t=>{if(!await e.get(t)){let s=await fetch(t);s.ok&&await e.put(t,s)}}))}var bs="0.1.0";async function fr(){let r=await oe();return{version:bs,runtimes:{webgpu:r.get("webgpu")??!1,webnn:r.get("webnn")??!1,wasm:r.get("wasm")??!1,auto:!0},features:["concurrent-execution","batch-processing","memory-management","model-caching","quantization"]}}export{de as AudioPreprocessor,N as BasePipeline,Q as Cache,dt as EMOTION_LABELS,p as EdgeFlowError,g as EdgeFlowTensor,m as ErrorCodes,q as FeatureExtractionPipeline,Ie as IMAGENET_LABELS,$ as ImageClassificationPipeline,he as ImagePreprocessor,Te as InferenceCache,ee as InferenceScheduler,D as LoadedModelImpl,V as MemoryManager,te as MemoryScope,se as ModelCache,B as ModelDownloadCache,z as RuntimeManager,ue as SENTIMENT_LABELS,U as SentimentAnalysisPipeline,F as TextClassificationPipeline,J as Tokenizer,bs as VERSION,ce as WASMRuntime,ie as WebGPURuntime,ae as WebNNRuntime,Rt as add,Is as analyzeModel,Ct as arange,jt as argmax,Ms as benchmark,Ue as cancelPreload,Re as clearModelCache,Wt as concat,Kt as configureScheduler,yt as createAudioPreprocessor,Z as createBasicTokenizer,lt as createCache,gt as createFeatureExtractionPipeline,xt as createImageClassificationPipeline,Me as createImagePreprocessor,ps as createPipelines,mt as createSentimentAnalysisPipeline,pt as createTextClassificationPipeline,rt as createWASMRuntime,we as createWebGPURuntime,ye as createWebNNRuntime,Fe as deleteCachedModel,Je as div,Ss as exportModel,Ft as eye,Nt as full,es as gc,oe as getAvailableRuntimes,us as getBestRuntime,hr as getBestRuntimeType,Le as getCachedModel,fr as getInfo,E as getMemoryManager,Jt as getMemoryStats,Oe as getModelCacheStats,ht as getPipelineFactory,Be as getPreloadStatus,qe as getPreloadedModel,ls as getRuntimeManager,pe as getScheduler,Ce as isModelCached,ur as isSupported,Lt as linspace,We as loadModel,me as loadModelData,as as loadModelFromBuffer,ft as loadTokenizer,Ut as matmul,$t as mean,Bt as mul,_t as ones,At as pipeline,dr as preload,Pe as preloadModel,ve as preloadModels,wt as preprocessText,Ts as prune,ms as quantize,vt as randn,Pt as random,ct as registerAllBackends,L as registerPipeline,re as registerRuntime,Zt as release,Ee as relu,cs as runBatchInference,Ge as runInference,et as setScheduler,ke as sigmoid,G as softmax,Ot as sub,be as sum,qt as tanh,Dt as tensor,Yt as withMemoryScope,Qt as withMemoryScopeSync,Qe as zeros};
|
|
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}
|
|
19
|
+
${s.content}<|im_end|>
|
|
20
|
+
`;return n+=`<|im_start|>assistant
|
|
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>>
|
|
22
|
+
${s}
|
|
23
|
+
<</SYS>>
|
|
24
|
+
|
|
25
|
+
${i.content} [/INST]`,s=""):n+=`<s>[INST] ${i.content} [/INST]`:i.role==="assistant"&&(n+=` ${i.content} </s>`);return n}applyLlama3Template(e){let n="<|begin_of_text|>";for(let s of e)n+=`<|start_header_id|>${s.role}<|end_header_id|>
|
|
26
|
+
|
|
27
|
+
${s.content}<|eot_id|>`;return n+=`<|start_header_id|>assistant<|end_header_id|>
|
|
28
|
+
|
|
29
|
+
`,n}applyMistralTemplate(e){let n="<s>";for(let s of e)s.role==="user"?n+=`[INST] ${s.content} [/INST]`:s.role==="assistant"?n+=` ${s.content}</s>`:s.role==="system"&&(n+=`[INST] ${s.content}
|
|
30
|
+
`);return n}applyPhi3Template(e){let n="";for(let s of e)n+=`<|${s.role}|>
|
|
31
|
+
${s.content}<|end|>
|
|
32
|
+
`;return n+=`<|assistant|>
|
|
33
|
+
`,n}applyAlpacaTemplate(e){let n="",s="",i="";for(let r of e)r.role==="system"?s=r.content:r.role==="user"&&(i=r.content);return s&&(n=`### Instruction:
|
|
34
|
+
${s}
|
|
35
|
+
|
|
36
|
+
`),i&&(n+=`### Input:
|
|
37
|
+
${i}
|
|
38
|
+
|
|
39
|
+
`),n+=`### Response:
|
|
40
|
+
`,n}applyVicunaTemplate(e){let n="";for(let s of e)s.role==="system"?n+=`${s.content}
|
|
41
|
+
|
|
42
|
+
`:s.role==="user"?n+=`USER: ${s.content}
|
|
43
|
+
`:s.role==="assistant"&&(n+=`ASSISTANT: ${s.content}
|
|
44
|
+
`);return n+="ASSISTANT:",n}applyCustomTemplate(e,n){let{systemPrefix:s="",systemSuffix:i=`
|
|
45
|
+
`,userPrefix:r="User: ",userSuffix:a=`
|
|
46
|
+
`,assistantPrefix:c="Assistant: ",assistantSuffix:l=`
|
|
47
|
+
`,separator:d=""}=n,u="";for(let h=0;h<e.length;h++){let p=e[h];switch(h>0&&(u+=d),p.role){case"system":u+=`${s}${p.content}${i}`;break;case"user":u+=`${r}${p.content}${a}`;break;case"assistant":u+=`${c}${p.content}${l}`;break}}return u+=c,u}async chat(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=await this.run(s,{...n,stopSequences:[...n?.stopSequences??[],"<|im_end|>","<|end|>","<|eot_id|>","</s>",`
|
|
48
|
+
|
|
49
|
+
User:`,`
|
|
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 ss(o,t,e={}){let n=await qe(o,{...e,name:e.name?`${e.name} (baseline)`:"baseline"}),s=await qe(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 rs(o,t={}){let e={};for(let[n,s]of Object.entries(o))console.log(`
|
|
53
|
+
=== ${n} ===`),e[n]=await qe(s,{...t,name:n,verbose:!0});return e}function os(o){return`
|
|
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
|
+
\u2502 ${o.name.padEnd(39)} \u2502
|
|
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
|
|
57
|
+
\u2502 Avg Time: ${o.avgTime.toFixed(2).padStart(10)}ms \u2502
|
|
58
|
+
\u2502 Median: ${o.medianTime.toFixed(2).padStart(10)}ms \u2502
|
|
59
|
+
\u2502 Min Time: ${o.minTime.toFixed(2).padStart(10)}ms \u2502
|
|
60
|
+
\u2502 Max Time: ${o.maxTime.toFixed(2).padStart(10)}ms \u2502
|
|
61
|
+
\u2502 Std Dev: ${o.stdDev.toFixed(2).padStart(10)}ms \u2502
|
|
62
|
+
\u2502 P95: ${o.p95.toFixed(2).padStart(10)}ms \u2502
|
|
63
|
+
\u2502 P99: ${o.p99.toFixed(2).padStart(10)}ms \u2502
|
|
64
|
+
\u2502 Throughput: ${o.throughput.toFixed(2).padStart(10)} ops/sec \u2502
|
|
65
|
+
\u2502 Runs: ${o.totalRuns.toString().padStart(10)} (${o.failedRuns} failed) \u2502
|
|
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 is(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
|
+
\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
|
+
\u2502 BENCHMARK COMPARISON \u2502
|
|
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
|
|
71
|
+
\u2502 Baseline: ${o.baseline.avgTime.toFixed(2).padStart(10)}ms \u2502
|
|
72
|
+
\u2502 Comparison: ${o.comparison.avgTime.toFixed(2).padStart(10)}ms \u2502
|
|
73
|
+
\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
|
|
74
|
+
\u2502 Speedup: ${o.speedup.toFixed(2).padStart(10)}x \u2502
|
|
75
|
+
\u2502 Difference: ${t} ${Math.abs(o.percentFaster).toFixed(1).padStart(8)}% \u2502
|
|
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
|
+
\u2502 Winner: ${e.padEnd(42)} \u2502
|
|
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 Vt(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 He=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=je(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(je(e,n))}traceOutput(t,e,n){let s=this.traces.find(i=>i.id===t);s&&s.outputs.push(je(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: ${Vt(t.peakMemoryUsage).padStart(10)} \u2551`,`\u2551 Current Memory: ${Vt(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
|
+
`)}},dt=null;function Wt(o){return(!dt||o)&&(dt=new He(o)),dt}function ys(o){let t=Wt(o);return t.enable(),t}function ws(){dt?.disable()}function xs(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 Ms(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 Ts(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 Ve=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 bs(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
|
+
<!DOCTYPE html>
|
|
86
|
+
<html lang="en">
|
|
87
|
+
<head>
|
|
88
|
+
<meta charset="UTF-8">
|
|
89
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
90
|
+
<title>edgeFlow.js Performance Dashboard</title>
|
|
91
|
+
<style>
|
|
92
|
+
:root {
|
|
93
|
+
--bg-primary: #0d1117;
|
|
94
|
+
--bg-secondary: #161b22;
|
|
95
|
+
--bg-tertiary: #21262d;
|
|
96
|
+
--text-primary: #f0f6fc;
|
|
97
|
+
--text-secondary: #8b949e;
|
|
98
|
+
--accent: #58a6ff;
|
|
99
|
+
--success: #3fb950;
|
|
100
|
+
--warning: #d29922;
|
|
101
|
+
--error: #f85149;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
* {
|
|
105
|
+
margin: 0;
|
|
106
|
+
padding: 0;
|
|
107
|
+
box-sizing: border-box;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
body {
|
|
111
|
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
|
|
112
|
+
background: var(--bg-primary);
|
|
113
|
+
color: var(--text-primary);
|
|
114
|
+
line-height: 1.6;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
.dashboard {
|
|
118
|
+
max-width: 1400px;
|
|
119
|
+
margin: 0 auto;
|
|
120
|
+
padding: 24px;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
header {
|
|
124
|
+
display: flex;
|
|
125
|
+
justify-content: space-between;
|
|
126
|
+
align-items: center;
|
|
127
|
+
margin-bottom: 32px;
|
|
128
|
+
padding-bottom: 16px;
|
|
129
|
+
border-bottom: 1px solid var(--bg-tertiary);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
h1 {
|
|
133
|
+
font-size: 24px;
|
|
134
|
+
font-weight: 600;
|
|
135
|
+
display: flex;
|
|
136
|
+
align-items: center;
|
|
137
|
+
gap: 12px;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
.status {
|
|
141
|
+
display: flex;
|
|
142
|
+
align-items: center;
|
|
143
|
+
gap: 8px;
|
|
144
|
+
font-size: 14px;
|
|
145
|
+
color: var(--text-secondary);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
.status-dot {
|
|
149
|
+
width: 8px;
|
|
150
|
+
height: 8px;
|
|
151
|
+
border-radius: 50%;
|
|
152
|
+
background: var(--success);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
.grid {
|
|
156
|
+
display: grid;
|
|
157
|
+
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
|
|
158
|
+
gap: 20px;
|
|
159
|
+
margin-bottom: 32px;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
.card {
|
|
163
|
+
background: var(--bg-secondary);
|
|
164
|
+
border: 1px solid var(--bg-tertiary);
|
|
165
|
+
border-radius: 12px;
|
|
166
|
+
padding: 20px;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
.card-header {
|
|
170
|
+
display: flex;
|
|
171
|
+
justify-content: space-between;
|
|
172
|
+
align-items: center;
|
|
173
|
+
margin-bottom: 16px;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
.card-title {
|
|
177
|
+
font-size: 14px;
|
|
178
|
+
font-weight: 500;
|
|
179
|
+
color: var(--text-secondary);
|
|
180
|
+
text-transform: uppercase;
|
|
181
|
+
letter-spacing: 0.5px;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
.card-value {
|
|
185
|
+
font-size: 36px;
|
|
186
|
+
font-weight: 700;
|
|
187
|
+
font-variant-numeric: tabular-nums;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
.card-value.small {
|
|
191
|
+
font-size: 24px;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
.card-unit {
|
|
195
|
+
font-size: 14px;
|
|
196
|
+
color: var(--text-secondary);
|
|
197
|
+
margin-left: 4px;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
.card-change {
|
|
201
|
+
font-size: 12px;
|
|
202
|
+
padding: 4px 8px;
|
|
203
|
+
border-radius: 4px;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
.card-change.up {
|
|
207
|
+
background: rgba(63, 185, 80, 0.2);
|
|
208
|
+
color: var(--success);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
.card-change.down {
|
|
212
|
+
background: rgba(248, 81, 73, 0.2);
|
|
213
|
+
color: var(--error);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
.progress-bar {
|
|
217
|
+
height: 8px;
|
|
218
|
+
background: var(--bg-tertiary);
|
|
219
|
+
border-radius: 4px;
|
|
220
|
+
overflow: hidden;
|
|
221
|
+
margin-top: 12px;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
.progress-fill {
|
|
225
|
+
height: 100%;
|
|
226
|
+
border-radius: 4px;
|
|
227
|
+
transition: width 0.3s ease;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
.progress-fill.blue { background: var(--accent); }
|
|
231
|
+
.progress-fill.green { background: var(--success); }
|
|
232
|
+
.progress-fill.yellow { background: var(--warning); }
|
|
233
|
+
.progress-fill.red { background: var(--error); }
|
|
234
|
+
|
|
235
|
+
.chart-container {
|
|
236
|
+
background: var(--bg-secondary);
|
|
237
|
+
border: 1px solid var(--bg-tertiary);
|
|
238
|
+
border-radius: 12px;
|
|
239
|
+
padding: 20px;
|
|
240
|
+
margin-bottom: 20px;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
.chart-header {
|
|
244
|
+
display: flex;
|
|
245
|
+
justify-content: space-between;
|
|
246
|
+
align-items: center;
|
|
247
|
+
margin-bottom: 16px;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
.chart-title {
|
|
251
|
+
font-size: 16px;
|
|
252
|
+
font-weight: 600;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
.chart {
|
|
256
|
+
height: 200px;
|
|
257
|
+
position: relative;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
.chart-line {
|
|
261
|
+
stroke: var(--accent);
|
|
262
|
+
stroke-width: 2;
|
|
263
|
+
fill: none;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
.chart-area {
|
|
267
|
+
fill: url(#chartGradient);
|
|
268
|
+
opacity: 0.3;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
.chart-grid {
|
|
272
|
+
stroke: var(--bg-tertiary);
|
|
273
|
+
stroke-width: 1;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
.table {
|
|
277
|
+
width: 100%;
|
|
278
|
+
border-collapse: collapse;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
.table th,
|
|
282
|
+
.table td {
|
|
283
|
+
padding: 12px 16px;
|
|
284
|
+
text-align: left;
|
|
285
|
+
border-bottom: 1px solid var(--bg-tertiary);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
.table th {
|
|
289
|
+
font-size: 12px;
|
|
290
|
+
font-weight: 500;
|
|
291
|
+
color: var(--text-secondary);
|
|
292
|
+
text-transform: uppercase;
|
|
293
|
+
letter-spacing: 0.5px;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
.table td {
|
|
297
|
+
font-variant-numeric: tabular-nums;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
footer {
|
|
301
|
+
text-align: center;
|
|
302
|
+
padding: 24px;
|
|
303
|
+
color: var(--text-secondary);
|
|
304
|
+
font-size: 14px;
|
|
305
|
+
}
|
|
306
|
+
</style>
|
|
307
|
+
</head>
|
|
308
|
+
<body>
|
|
309
|
+
<div class="dashboard">
|
|
310
|
+
<header>
|
|
311
|
+
<h1>
|
|
312
|
+
<svg width="32" height="32" viewBox="0 0 32 32" fill="none">
|
|
313
|
+
<rect width="32" height="32" rx="8" fill="var(--accent)"/>
|
|
314
|
+
<path d="M8 16L14 10L20 16L26 10" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
|
|
315
|
+
<path d="M8 22L14 16L20 22L26 16" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" opacity="0.5"/>
|
|
316
|
+
</svg>
|
|
317
|
+
edgeFlow.js Performance Dashboard
|
|
318
|
+
</h1>
|
|
319
|
+
<div class="status">
|
|
320
|
+
<div class="status-dot"></div>
|
|
321
|
+
Running for ${(r=>r<1e3?`${r.toFixed(0)}ms`:r<6e4?`${(r/1e3).toFixed(1)}s`:`${(r/6e4).toFixed(1)}m`)(t.uptime)}
|
|
322
|
+
</div>
|
|
323
|
+
</header>
|
|
324
|
+
|
|
325
|
+
<div class="grid">
|
|
326
|
+
<div class="card">
|
|
327
|
+
<div class="card-header">
|
|
328
|
+
<span class="card-title">Total Inferences</span>
|
|
329
|
+
</div>
|
|
330
|
+
<div class="card-value">${t.totalInferences.toLocaleString()}</div>
|
|
331
|
+
</div>
|
|
332
|
+
|
|
333
|
+
<div class="card">
|
|
334
|
+
<div class="card-header">
|
|
335
|
+
<span class="card-title">Avg Inference Time</span>
|
|
336
|
+
</div>
|
|
337
|
+
<div class="card-value">${t.avgInferenceTime.toFixed(1)}<span class="card-unit">ms</span></div>
|
|
338
|
+
</div>
|
|
339
|
+
|
|
340
|
+
<div class="card">
|
|
341
|
+
<div class="card-header">
|
|
342
|
+
<span class="card-title">Throughput</span>
|
|
343
|
+
</div>
|
|
344
|
+
<div class="card-value">${t.avgThroughput.toFixed(1)}<span class="card-unit">ops/s</span></div>
|
|
345
|
+
</div>
|
|
346
|
+
|
|
347
|
+
<div class="card">
|
|
348
|
+
<div class="card-header">
|
|
349
|
+
<span class="card-title">Avg FPS</span>
|
|
350
|
+
</div>
|
|
351
|
+
<div class="card-value">${Math.round(t.avgFPS)}</div>
|
|
352
|
+
</div>
|
|
353
|
+
</div>
|
|
354
|
+
|
|
355
|
+
<div class="grid">
|
|
356
|
+
<div class="card">
|
|
357
|
+
<div class="card-header">
|
|
358
|
+
<span class="card-title">Memory Usage</span>
|
|
359
|
+
</div>
|
|
360
|
+
<div class="card-value small">${s(n?.memory.usedHeap??0)}</div>
|
|
361
|
+
<div class="progress-bar">
|
|
362
|
+
<div class="progress-fill ${t.avgMemoryUsage>.8?"red":t.avgMemoryUsage>.6?"yellow":"green"}"
|
|
363
|
+
style="width: ${(t.avgMemoryUsage*100).toFixed(0)}%"></div>
|
|
364
|
+
</div>
|
|
365
|
+
</div>
|
|
366
|
+
|
|
367
|
+
<div class="card">
|
|
368
|
+
<div class="card-header">
|
|
369
|
+
<span class="card-title">Tensor Memory</span>
|
|
370
|
+
</div>
|
|
371
|
+
<div class="card-value small">${s(n?.memory.tensorMemory??0)}</div>
|
|
372
|
+
</div>
|
|
373
|
+
|
|
374
|
+
<div class="card">
|
|
375
|
+
<div class="card-header">
|
|
376
|
+
<span class="card-title">Cache Memory</span>
|
|
377
|
+
</div>
|
|
378
|
+
<div class="card-value small">${s(n?.memory.cacheMemory??0)}</div>
|
|
379
|
+
</div>
|
|
380
|
+
|
|
381
|
+
<div class="card">
|
|
382
|
+
<div class="card-header">
|
|
383
|
+
<span class="card-title">Queue Length</span>
|
|
384
|
+
</div>
|
|
385
|
+
<div class="card-value small">${n?.inference.queueLength??0}</div>
|
|
386
|
+
</div>
|
|
387
|
+
</div>
|
|
388
|
+
|
|
389
|
+
<div class="chart-container">
|
|
390
|
+
<div class="chart-header">
|
|
391
|
+
<span class="chart-title">Inference Time History</span>
|
|
392
|
+
</div>
|
|
393
|
+
<div class="chart">
|
|
394
|
+
<svg width="100%" height="100%" viewBox="0 0 600 200" preserveAspectRatio="none">
|
|
395
|
+
<defs>
|
|
396
|
+
<linearGradient id="chartGradient" x1="0" y1="0" x2="0" y2="1">
|
|
397
|
+
<stop offset="0%" stop-color="var(--accent)" stop-opacity="0.5"/>
|
|
398
|
+
<stop offset="100%" stop-color="var(--accent)" stop-opacity="0"/>
|
|
399
|
+
</linearGradient>
|
|
400
|
+
</defs>
|
|
401
|
+
${Sr(e)}
|
|
402
|
+
</svg>
|
|
403
|
+
</div>
|
|
404
|
+
</div>
|
|
405
|
+
|
|
406
|
+
<div class="chart-container">
|
|
407
|
+
<div class="chart-header">
|
|
408
|
+
<span class="chart-title">Recent Samples</span>
|
|
409
|
+
</div>
|
|
410
|
+
<table class="table">
|
|
411
|
+
<thead>
|
|
412
|
+
<tr>
|
|
413
|
+
<th>Time</th>
|
|
414
|
+
<th>Inferences</th>
|
|
415
|
+
<th>Avg Time</th>
|
|
416
|
+
<th>Throughput</th>
|
|
417
|
+
<th>Memory</th>
|
|
418
|
+
<th>FPS</th>
|
|
419
|
+
</tr>
|
|
420
|
+
</thead>
|
|
421
|
+
<tbody>
|
|
422
|
+
${e.slice(-10).reverse().map(r=>`
|
|
423
|
+
<tr>
|
|
424
|
+
<td>${new Date(r.timestamp).toLocaleTimeString()}</td>
|
|
425
|
+
<td>${r.inference.count}</td>
|
|
426
|
+
<td>${r.inference.avgTime.toFixed(2)}ms</td>
|
|
427
|
+
<td>${r.inference.throughput.toFixed(1)}/s</td>
|
|
428
|
+
<td>${s(r.memory.usedHeap)}</td>
|
|
429
|
+
<td>${r.system.fps}</td>
|
|
430
|
+
</tr>
|
|
431
|
+
`).join("")}
|
|
432
|
+
</tbody>
|
|
433
|
+
</table>
|
|
434
|
+
</div>
|
|
435
|
+
|
|
436
|
+
<footer>
|
|
437
|
+
Generated at ${new Date().toLocaleString()} | edgeFlow.js Performance Monitor
|
|
438
|
+
</footer>
|
|
439
|
+
</div>
|
|
440
|
+
</body>
|
|
441
|
+
</html>
|
|
442
|
+
`.trim()}function Sr(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
|
+
${l.join(`
|
|
444
|
+
`)}
|
|
445
|
+
<path class="chart-area" d="${c}"/>
|
|
446
|
+
<path class="chart-line" d="${a}"/>
|
|
447
|
+
`}function ks(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 ut=null;function Gt(o){return(!ut||o)&&(ut=new Ve(o)),ut}function Is(o){let t=Gt(o);return t.start(),t}function As(){ut?.stop()}async function vr(o,t){let e=o instanceof ArrayBuffer?o:await Xt(o),n=e.byteLength,s,i=0,r=0;switch(t.method){case"int8":({data:s,layersQuantized:i,layersSkipped:r}=Er(e,t));break;case"uint8":({data:s,layersQuantized:i,layersSkipped:r}=Dr(e,t));break;case"float16":({data:s,layersQuantized:i,layersSkipped:r}=Fr(e,t));break;case"int4":({data:s,layersQuantized:i,layersSkipped:r}=Pr(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 Xt(o){return new ArrayBuffer(0)}function Er(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 Dr(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 Fr(o,t){let e=new Float32Array(o),n=new Uint16Array(e.length);for(let s=0;s<e.length;s++)n[s]=_r(e[s]??0);return{data:n.buffer,layersQuantized:1,layersSkipped:0}}function Pr(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 _r(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 Lr(o,t){let e=o instanceof ArrayBuffer?o:await Xt(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 Nr(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 Cr(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 Or(o,t){let e=o instanceof ArrayBuffer?o:await Xt(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 oc(){let o=await Ee();return Array.from(o.values()).some(t=>t)}async function ic(){let o=await Ee();return o.get("webgpu")?"webgpu":o.get("webnn")?"webnn":o.get("wasm")?"wasm":null}async function ac(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 $r="0.1.0";async function cc(){let o=await Ee();return{version:$r,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,Dn as EMOTION_LABELS,He as EdgeFlowDebugger,x as EdgeFlowError,w as EdgeFlowTensor,M as ErrorCodes,ne as FeatureExtractionPipeline,st as IMAGENET_LABELS,se as ImageClassificationPipeline,he as ImagePreprocessor,re as ImageSegmentationPipeline,tt as InferenceCache,ke as InferenceScheduler,H as LoadedModelImpl,ce as MemoryManager,Ae as MemoryScope,ze as ModelCache,ee as ModelDownloadCache,Ot as POPULAR_MODELS,Ve as PerformanceMonitor,V as RuntimeManager,nt as SENTIMENT_LABELS,te as SentimentAnalysisPipeline,Y as TextClassificationPipeline,pe as TextGenerationPipeline,P as Tokenizer,Ce as TransformersAdapterRuntime,$r as VERSION,Le as WASMRuntime,Pe as WebGPURuntime,_e as WebNNRuntime,on as add,Nr as analyzeModel,ps as analyzeModelDetailed,nn as arange,hn as argmax,Cr as benchmark,as as benchmarkMemory,rs as benchmarkSuite,zt as cancelPreload,kt as clearModelCache,ss as compareBenchmarks,yr as compose,fn as concat,Ls as configureScheduler,xs as createAsciiHistogram,Un as createAudioPreprocessor,Fn as createBasicTokenizer,Sn as createCache,$n as createFeatureExtractionPipeline,Bn as createImageClassificationPipeline,rt as createImagePreprocessor,Xn as createImageSegmentationPipeline,gr as createPipelines,Cn as createSentimentAnalysisPipeline,Ms as createTensorHeatmap,Nn as createTextClassificationPipeline,jn as createTextGenerationPipeline,kn as createWASMRuntime,Tn as createWebGPURuntime,bn as createWebNNRuntime,bt as deleteCachedModel,Bt as dequantizeFloat16,lt as dequantizeInt8,hs as dequantizeTensor,Rt as dequantizeUint8,ws as disableDebugging,pt as div,it as downloadConfig,at as downloadModel,Ct as downloadTokenizer,ys as enableDebugging,Or as exportModel,ms as exportModelAdvanced,rn as eye,Ut as float16ToFloat32,os as formatBenchmarkResult,is as formatComparisonResult,gs as formatTensorInspection,Zn as fromHub,ns as fromTask,Zt as full,Us as gc,ks as generateAsciiDashboard,bs as generateDashboardHTML,Ee as getAvailableRuntimes,Qs as getBestRuntime,ic as getBestRuntimeType,Tt as getCachedModel,Wt as getDebugger,$t as getDefaultModel,wn as getDeviceProfile,cc as getInfo,q as getMemoryManager,$s as getMemoryStats,It as getModelCacheStats,ts as getModelInfo,Gt as getMonitor,En as getPipelineFactory,Ys as getPluginMiddleware,Dt as getPluginPipeline,At as getPreloadStatus,St as getPreloadedModel,Xs as getRuntimeManager,Ie as getScheduler,zn as getTransformersAdapter,je as inspectTensor,ve as isModelCached,oc as isSupported,sn as linspace,Js as listPlugins,we as loadModel,v as loadModelData,F as loadModelFromBuffer,Pn as loadTokenizer,_n as loadTokenizerFromHub,ln as matmul,un as mean,es as modelExists,cn as mul,Jt as ones,wr as parallel,Be as pipeline,ac as preload,xt as preloadModel,Mt as preloadModels,Rn as preprocessText,Lr as prune,fs as pruneModel,Ht as pruneTensor,vr as quantize,jt as quantizeModel,us as quantizeTensor,tn as randn,en as random,er as recommendModelVariant,xn as recommendQuantization,et as registerAllBackends,N as registerPipeline,Ks as registerPlugin,de as registerRuntime,Rs as release,Qe as relu,tr as resetDeviceProfile,Gs as runBatchInference,qe as runBenchmark,X as runInference,mn as setScheduler,Ke as sigmoid,O as softmax,Is as startMonitoring,As as stopMonitoring,an as sub,Ge as sum,dn as tanh,Yt as tensor,Zs as unregisterPlugin,An as useTransformersBackend,Ts as visualizeModelArchitecture,Cs as withMemoryScope,Os as withMemoryScopeSync,ft as zeros};
|
|
19
449
|
//# sourceMappingURL=edgeflow.browser.min.js.map
|