PageSourceSearch

https://www.farminsights.ai/assets/index-Bz0yw_kb.js

js farminsights.ai collected 2026-10-05 15:21:44 UTC 27,319 bytes, 1 lines download raw bytes

1var oe=Object.defineProperty;var se=(e,t,r)=>t in e?oe(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r;var y=(e,t,r)=>se(e,typeof t!="symbol"?t+"":t,r);async function ie(e){if(typeof importScripts=="function")importScripts(e.toString());else{const t=document.createElement("script");return t.src=e.toString(),t.crossOrigin="anonymous",new Promise((r,n)=>{t.addEventListener("load",()=>{r()},!1),t.addEventListener("error",o=>{n(o)},!1),document.body.appendChild(t)})}}var ae=async(e,t,r,n,o)=>{if(t&&await ie(t),!self.ModuleFactory)throw new Error("ModuleFactory not set.");const s=await self.ModuleFactory(self.Module||o);return self.ModuleFactory=self.Module=void 0,new e(s,n)},c={NONE:0,FLOAT32:1,INT32:2,UINT8:3,INT64:4,STRING:5,BOOL:6,INT16:7,COMPLEX64:8,INT8:9,FLOAT16:10,FLOAT64:11,COMPLEX128:12,UINT64:13,RESOURCE:14,VARIANT:15,UINT32:16,UINT16:17,INT4:18,BFLOAT16:19},V={[c.NONE]:"NONE",[c.FLOAT32]:"FLOAT32",[c.INT32]:"INT32",[c.UINT8]:"UINT8",[c.INT64]:"INT64",[c.STRING]:"STRING",[c.BOOL]:"BOOL",[c.INT16]:"INT16",[c.COMPLEX64]:"COMPLEX64",[c.INT8]:"INT8",[c.FLOAT16]:"FLOAT16",[c.FLOAT64]:"FLOAT64",[c.COMPLEX128]:"COMPLEX128",[c.UINT64]:"UINT64",[c.RESOURCE]:"RESOURCE",[c.VARIANT]:"VARIANT",[c.UINT32]:"UINT32",[c.UINT16]:"UINT16",[c.INT4]:"INT4",[c.BFLOAT16]:"BFLOAT16"},g={HOST_MEMORY:1,WEB_GPU_BUFFER:20,WEB_GPU_BUFFER_FP16:21,WEB_GPU_BUFFER_PACKED:26},U={[g.HOST_MEMORY]:"HOST_MEMORY",[g.WEB_GPU_BUFFER]:"WEB_GPU_BUFFER",[g.WEB_GPU_BUFFER_FP16]:"WEB_GPU_BUFFER_FP16",[g.WEB_GPU_BUFFER_PACKED]:"WEB_GPU_BUFFER_PACKED"},ue=Object.freeze([{dtype:"float32",typedArrayConstructor:Float32Array,elementType:c.FLOAT32},{dtype:"int32",typedArrayConstructor:Int32Array,elementType:c.INT32},{dtype:"uint8",typedArrayConstructor:Uint8Array,elementType:c.UINT8}]);function P(e){for(const t of ue)if(t.dtype===e||t.typedArrayConstructor===e||e instanceof t.typedArrayConstructor||t.elementType===e)return t;throw typeof e=="string"?new Error(`DType ${e} is not supported.`):e instanceof Object?new Error(`Typed array ${"name"in e?e.name:e.constructor.name} is not supported.`):new Error(`Element type ${V[e]??e} is not supported.`)}var le=class extends Error{constructor(){super("LiteRT is not initialized yet. Please call loadLiteRt() and wait for its promise to resolve to load the LiteRT WASM module.")}},L=void 0,O=void 0;function T(){if(!L)throw new le;return L}function $(){return!!L}function K(e){L=e}function fe(){return O}function X(){return!!O}function G(e){O=e}var ce={webgpu:g.WEB_GPU_BUFFER_PACKED,wasm:g.HOST_MEMORY},de={[g.HOST_MEMORY]:"wasm",[g.WEB_GPU_BUFFER]:"webgpu",[g.WEB_GPU_BUFFER_FP16]:"webgpu",[g.WEB_GPU_BUFFER_PACKED]:"webgpu"},pe=["shader-f16","subgroups"],N=class Z{constructor(t){y(this,"liteRtEnvironment");this.options=t,this.liteRtEnvironment=T().liteRtWasm.LiteRtEnvironment.create(t.webGpuDevice)}static async create(t={}){let r=null;if("webGpuDevice"in t)t.webGpuDevice&&(r=t.webGpuDevice);else try{r=await he()}catch(n){console.warn("Failed to create default WebGPU device:",n)}return new Z({...t,webGpuDevice:r})}get webGpuDevice(){return this.options.webGpuDevice}delete(){this.liteRtEnvironment.delete()}};async function he(){const e={powerPreference:"high-performance"},t=await navigator.gpu.requestAdapter(e);if(!t)throw new Error("No GPU adapter found.");const r={maxBufferSize:t.limits.maxBufferSize,maxStorageBufferBindingSize:t.limits.maxStorageBufferBindingSize,maxStorageBuffersPerShaderStage:t.limits.maxStorageBuffersPerShaderStage,maxTextureDimension2D:t.limits.maxTextureDimension2D},n=[];for(const o of pe)t.features.has(o)&&n.push(o);return await t.requestDevice({requiredFeatures:n,requiredLimits:r})}function _(e){const t=new Array(e.size());for(let r=0;r<e.size();++r)t[r]=e.get(r);return e.delete(),t}function Q(e,t){for(const r of e)t.push_back(r)}function ye(e){const t=e.shift(),r=T().liteRtWasm;if(t instanceof r.LiteRtTensorBuffer)return{liteRtTensorBuffer:t};if(ArrayBuffer.isView(t))return{typedArray:t};if(t instanceof GPUBuffer)return{gpuBuffer:t};throw new Error(`Unknown type (${(t==null?void 0:t.constructor.name)??t}) provided to create a Tensor`)}function Te(e){return Array.isArray(e[0])||e[0]instanceof Int32Array?{shape:e.shift()}:{}}function I(e){for(;e.length>0&&e[0]===void 0;)e.shift()}function me(e){if(I(e),typeof e[0]=="string"){const t=e.shift();return{dataType:P(t).dtype}}else return{}}function we(e){return I(e),e[0]instanceof N?{environment:e.shift()}:{}}function ge(e){return I(e),e[0]instanceof Function?{onDelete:e.shift()}:{}}function Re(e){return{...ye(e),...Te(e),...me(e),...we(e),...ge(e)}}var A,S=(A=class{constructor(t,r,n,o,s){y(this,"liteRtTensorBuffer");y(this,"type");y(this,"environment");y(this,"deletedInternal",!1);y(this,"onDelete");const{typedArray:a,gpuBuffer:i,liteRtTensorBuffer:u,shape:l,dataType:p,environment:f,onDelete:h}=Re([t,r,n,o,s]);if(this.onDelete=h,this.environment=f??T().getDefaultEnvironment(),u){if(l)throw new Error("A LiteRtTensorBuffer cannot be provided with a shape.");if(p)throw new Error("A LiteRtTensorBuffer cannot be provided with a data type.");this.liteRtTensorBuffer=u}else if(i){if(!l)throw new Error("A GPUBuffer must be provided with a shape.");if(!p)throw new Error("A GPUBuffer must be provided with a data type.");const[d,R]=be(i,l,p,this.environment);this.liteRtTensorBuffer=d;const m=this.onDelete;this.onDelete=()=>{T().liteRtWasm.wgpuBufferRelease(R),m==null||m()}}else if(a)this.liteRtTensorBuffer=Be(a,l,f);else throw new Error("No data provided to create a Tensor.");this.type=Ee(this.liteRtTensorBuffer)}static fromTypedArray(t,r,n){return new A(t,r,n)}ensureNotDeleted(){if(this.deleted)throw new Error("Tensor is deleted and cannot be used.")}async data(){if(this.ensureNotDeleted(),this.liteRtTensorBuffer.bufferType().value===g.HOST_MEMORY)return this.toTypedArray();const t=await this.copyTo("wasm"),r=await t.data();return t.delete(),r}toTypedArray(){this.ensureNotDeleted();const t=T().liteRtWasm;if(this.liteRtTensorBuffer.isWebGpuMemory())throw new Error("Cannot convert a Tensor with WebGPU memory to a TypedArray.");if(this.liteRtTensorBuffer.bufferType().value!==t.LiteRtTensorBufferType.HOST_MEMORY.value)throw new Error("Cannot convert a Tensor with non-host memory to a TypedArray.");if(this.liteRtTensorBuffer.size()!==this.liteRtTensorBuffer.packedSize()||this.liteRtTensorBuffer.offset()!==0)throw new Error("Tensors with strides or padding are not yet supported.");const r=this.liteRtTensorBuffer.tensorType(),n=r.elementType(),o=t.liteRtGetByteWidth(n);r.delete();const s=P(n.value).typedArrayConstructor;if(s.BYTES_PER_ELEMENT!==o)throw new Error(`Byte width ${o} of the tensor's element type ${V[n.value]} does not match the expected byte width ${s.BYTES_PER_ELEMENT} of the ${s.name}.`);const a=this.liteRtTensorBuffer.lock(T().liteRtWasm.LiteRtTensorBufferLockMode.READ);try{const i=t.HEAPU8.slice(a,a+this.liteRtTensorBuffer.packedSize());return new s(i.buffer,i.byteOffset,i.byteLength/o)}finally{this.liteRtTensorBuffer.unlock()}}getBufferType(){return this.ensureNotDeleted(),this.liteRtTensorBuffer.bufferType().value}toGpuBuffer(){this.ensureNotDeleted();const t=T().liteRtWasm;if(!this.liteRtTensorBuffer.isWebGpuMemory())throw new Error("Cannot convert a Tensor with non-WebGPU memory to a GPUBuffer.");const r=this.liteRtTensorBuffer.bufferType().value;if(r!==t.LiteRtTensorBufferType.WEB_GPU_BUFFER.value&&r!==t.LiteRtTensorBufferType.WEB_GPU_BUFFER_FP16.value&&r!==t.LiteRtTensorBufferType.WEB_GPU_BUFFER_PACKED.value)throw new Error("Cannot convert a Tensor with host memory to a GPUBuffer.");if(this.liteRtTensorBuffer.size()!==this.liteRtTensorBuffer.packedSize()||this.liteRtTensorBuffer.offset()!==0)throw new Error("Tensors with strides or padding are not yet supported.");const n=this.liteRtTensorBuffer.getWebGpuBuffer();return t.WebGPU.getJsObject(n)}getCopyFunctionSet(t){this.ensureNotDeleted();const r=this.getBufferType(),n=A.copyFunctions.get(r);if(!n)throw new Error(`TensorBufferType ${U[r]??r} does not support copying or moving`);const o=typeof t=="string"?ce[t]:t;
1if(o==null)throw new Error(`Unknown destination '${t}' for copying or moving.`);const s=n.get(o);if(!s){const a=[...n].map(([i])=>U[i]??i);throw new Error(`TensorBufferType ${U[r]} does not support copying or moving to ${U[o]}. It supports the following TensorBufferTypes: [${a.join(", ")}].`)}return[s,o]}async copyTo(t,r){const[n,o]=this.getCopyFunctionSet(t);if(!n.copyTo)throw new Error(`Copying to ${U[o]} is not supported by this tensor.`);return n.copyTo(this,r)}async moveTo(t,r){const[n,o]=this.getCopyFunctionSet(t);if(!n.moveTo)throw new Error(`Moving to ${U[o]} is not supported by this tensor.`);return n.moveTo(this,r)}get bufferType(){return this.liteRtTensorBuffer.bufferType().value}get accelerator(){const t=de[this.bufferType];if(t===void 0)throw new Error(`TensorBufferType ${U[this.bufferType]} has an unknown accelerator type.`);return t}get deleted(){return this.deletedInternal}delete(){var t;this.deletedInternal||(this.deletedInternal=!0,this.liteRtTensorBuffer.delete(),(t=this.onDelete)==null||t.call(this))}},y(A,"copyFunctions",new Map),A);function Ee(e){const t=e.tensorType(),r=t.elementType(),n=t.layout(),o=n.dimensions();return n.delete(),t.delete(),{dtype:P(r.value).dtype,layout:{dimensions:_(o)}}}function be(e,t,r,n){const s=T().liteRtWasm,a=new s.VectorInt32;Q(t,a);const i=s.LiteRtLayout.create(a);a.delete();const u=s.LiteRtRankedTensorType.create({value:P(r).elementType},i);i.delete();const l=s.WebGPU.importJsBuffer(e),p=s.LiteRtTensorBuffer.createFromWebGpuBuffer(n.liteRtEnvironment,u,s.LiteRtTensorBufferType.WEB_GPU_BUFFER_PACKED,l,e.size);return u.delete(),[p,l]}function Be(e,t,r){const n=T(),o=n.liteRtWasm;r=r??n.getDefaultEnvironment();const s=P(e).elementType,a=new o.VectorInt32;Q(t??[e.length],a);const i=o.LiteRtLayout.create(a);a.delete();const u=i.numElements();if(e.length!==u)throw i.delete(),new Error(`Number of elements ${e.length} of the provided TypedArray does not match the expected number of elements ${u}.`);const l=o.LiteRtRankedTensorType.create({value:s},i);i.delete();const f=e.constructor.BYTES_PER_ELEMENT*e.length,h=l.bytes();if(f!==h)throw l.delete(),new Error(`Byte length ${f} of the provided TypedArray does not match the expected buffer size ${h}.`);const d=o.LiteRtTensorBuffer.createManaged(r.liteRtEnvironment,o.LiteRtTensorBufferType.HOST_MEMORY,l,f);l.delete();const R=d.lock(o.LiteRtTensorBufferLockMode.WRITE);try{const m=new Uint8Array(e.buffer,e.byteOffset,e.byteLength);o.HEAPU8.set(m,R)}finally{d.unlock()}return d}var ve=class{constructor(e,t,r,n){y(this,"inputDetails");y(this,"outputDetails");y(this,"liteRtSimpleSignature");y(this,"deletedInternal",!1);this.signatureIndex=e,this.liteRtModel=t,this.liteRtCompiledModel=r,this.options=n,this.liteRtSimpleSignature=t.getSignature(e);const o=_(this.liteRtSimpleSignature.inputNames()),s=[];for(let u=0;u<o.length;u++){const l=o[u],p=t.getInputTensorType(e,u),f=r.getInputBufferRequirements(e,u);s.push(x(l,u,p,f))}this.inputDetails=Object.freeze(s);const a=_(this.liteRtSimpleSignature.outputNames()),i=[];for(let u=0;u<a.length;u++){const l=a[u],p=t.getOutputTensorType(e,u),f=r.getOutputBufferRequirements(e,u);i.push(x(l,u,p,f))}this.outputDetails=Object.freeze(i)}get key(){return this.ensureNotDeleted(),this.liteRtSimpleSignature.key()}getInputDetails(){return this.ensureNotDeleted(),this.inputDetails}getOutputDetails(){return this.ensureNotDeleted(),this.outputDetails}async run(e){this.ensureNotDeleted();const t=this.inputsToArray(e),{inputsOnAccelerator:r,cleanup:n}=await this.ensureInputsOnAccelerator(t);let o;try{o=await this.runWithArray(r)}finally{n()}return Array.isArray(e)||e instanceof S?o:this.outputsToRecord(o)}inputsToArray(e){if(Array.isArray(e)){if(e.length!==this.inputDetails.length)throw new Error(`run() called with ${e.length} inputs, but signature expects ${this.inputDetails.length} inputs`);return e}if(e instanceof S){if(this.inputDetails.length!==1)throw new Error(`run() called with a single tensor, but signature expects ${this.inputDetails.length} inputs`);return[e]}const t=[];for(const r of this.inputDetails){if(!(r.name in e))throw new Error(`run() called with input record that is missing input ${r.name} with index ${r.index}`);t.push(e[r.name])}return t}outputsToRecord(e){const t={};for(let r=0;r<this.outputDetails.length;r++)t[this.outputDetails[r].name]=e[r];return t}async ensureInputsOnAccelerator(e){const t=[],r=[],n=this.getInputDetails();if(e.length!==n.length)throw new Error(`ensureInputsOnAccelerator() called with ${e.length}
1 inputs, but signature expects ${n.length} inputs`);for(let o=0;o<e.length;o++){const s=e[o],a=s.getBufferType(),i=n[o].supportedBufferTypes;if(i.size===0)throw new Error(`Tensor ${n[o].name} with index ${n[o].index} has no supported buffer types.`);if(i.has(a))r.push(s);else{const u=i.values().next().value,l=await s.copyTo(u);t.push(l),r.push(l)}}return{inputsOnAccelerator:r,cleanup:()=>{for(const o of t)o.delete()}}}async runWithArray(e){for(let r=0;r<e.length;r++){const n=e[r],o=this.liteRtModel.getInputTensorType(this.signatureIndex,r),s=this.liteRtCompiledModel.getInputBufferRequirements(this.signatureIndex,r);T().liteRtWasm.checkTensorBufferCompatible(n.liteRtTensorBuffer,o,s),o.delete(),s.delete()}return(await this.liteRtCompiledModel.run(this.signatureIndex,e.map(r=>r.liteRtTensorBuffer))).map(r=>new S(r,this.options.environment))}get deleted(){return this.deletedInternal}ensureNotDeleted(){if(this.deleted)throw new Error("CompiledModelSignatureRunner is deleted and cannot be used.")}delete(){this.deletedInternal||(this.deletedInternal=!0,this.liteRtSimpleSignature.delete())}};function x(e,t,r,n){const o=r.layout(),s=_(o.dimensions());o.delete();const a=new Set(_(n.supportedTypes()).map(({value:u})=>u)),i={name:e,index:t,dtype:P(r.elementType().value).dtype,shape:new Int32Array(s),supportedBufferTypes:a};return r.delete(),n.delete(),i}var ee=class{constructor(e,t,r,n){y(this,"defaultSignature");y(this,"compiledModelSignatureRunners");y(this,"key");y(this,"deletedInternal",!1);this.model=e,this.liteRtCompiledModel=t,this.options=r,this.onDelete=n;const o=e.liteRtModel.getNumSignatures(),s={};for(let a=0;a<o;a++){const i=new ve(a,e.liteRtModel,t,r);s[i.key]=i}this.compiledModelSignatureRunners=Object.freeze(s),this.defaultSignature=Object.values(this.signatures)[0],this.key=this.defaultSignature.key}get signatures(){return this.ensureNotDeleted(),this.compiledModelSignatureRunners}getInputDetails(){return this.ensureNotDeleted(),this.defaultSignature.getInputDetails()}getOutputDetails(){return this.ensureNotDeleted(),this.defaultSignature.getOutputDetails()}async run(e,t){this.ensureNotDeleted();const[r,n]=this.parseRunInputs(e,t);return await r.run(n)}parseRunInputs(e,t){let r,n;if(typeof e=="string"){if(r=this.signatures[e],!r)throw new Error(`No signature named ${e} found in model.`);if(!t)throw new Error(`No input provided for signature ${e}`);n=t}else r=this.defaultSignature,n=e;return[r,n]}get deleted(){return this.deletedInternal}ensureNotDeleted(){if(this.deleted)throw new Error("CompiledModel is deleted and cannot be used.")}get isFullyAccelerated(){return this.ensureNotDeleted(),this.liteRtCompiledModel.isFullyAccelerated()}delete(){if(!this.deletedInternal){this.deletedInternal=!0,this.liteRtCompiledModel.delete(),this.model.delete();for(const e of Object.values(this.compiledModelSignatureRunners))e.delete();this.onDelete()}}};async function De(e){const t=await fetch(e);return new Uint8Array(await t.arrayBuffer())}async function Ae(e){let t=0,r=new Uint8Array(1024);const n=2e9;for(;;){const{done:o,value:s}=await e.read();if(s){if(r.byteLength<t+s.byteLength){if(t+s.byteLength>n)throw new Error(`Model is too large (> ${n} bytes).`);const a=new Uint8Array(Math.min(n,Math.max(r.byteLength,s.byteLength)*2));a.set(r),r=a}r.set(s,t),t+=s.byteLength}if(o)break}return r.slice(0,t)}var te=class{constructor(e,t){this.liteRtModel=e,this.onDelete=t}delete(){this.liteRtModel.delete(),this.onDelete()}};function re(e={},t,r){return{environment:t,accelerator:e.accelerator??(t.webGpuDevice?"webgpu":"wasm"),cpuOptions:e.cpuOptions??{numThreads:r},gpuOptions:e.gpuOptions??{},webNNOptions:e.webNNOptions??{}}}var Se=new Uint8Array([0,97,115,109,1,0,0,0,1,5,1,96,0,1,123,3,2,1,0,10,15,1,13,0,65,1,253,15,65,2,253,15,253,128,2,11]),Ue=new Uint8Array([0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,5,4,1,3,1,1,10,11,1,9,0,65,0,254,16,2,0,26,11]),E={relaxedSimd:void 0,threads:void 0,jspi:void 0,webnn:void 0};function ne(){return"Suspending"in WebAssembly}function Pe(){return typeof navigator<"u"&&!!navigator.ml}async function j(e){try{return await WebAssembly.instantiate(e),{supported:!0}
1}catch(t){return{supported:!1,error:t}}}var M={relaxedSimd:()=>(E.relaxedSimd===void 0&&(E.relaxedSimd=j(Se)),E.relaxedSimd),threads:()=>{if(E.threads===void 0)try{typeof MessageChannel<"u"&&new MessageChannel().port1.postMessage(new SharedArrayBuffer(1)),E.threads=j(Ue)}catch(e){E.threads=Promise.resolve({supported:!1,error:e})}return E.threads},jspi:()=>{if(E.jspi===void 0){const e=ne();E.jspi=Promise.resolve({supported:e,error:e?void 0:new Error("JSPI is not supported")})}return E.jspi},webnn:()=>{if(E.webnn===void 0){const e=Pe();E.webnn=Promise.resolve({supported:e,error:e?void 0:new Error("WebNN is not supported")})}return E.webnn}};async function Me(e){var r;const t=(r=M[e])==null?void 0:r.call(M);if(!t)throw new Error(`Unknown feature: ${e}`);return(await t).supported}async function z(e){var n;const t=(n=M[e])==null?void 0:n.call(M);if(!t)throw new Error(`Unknown feature: ${e}`);const r=await t;if(!r.supported)throw r.error}function ke(){return!!(typeof globalThis<"u"&&globalThis.navigator&&globalThis.navigator.gpu)}function $e(){return T().getDefaultEnvironment()}function xe(e,t){return T().loadAndCompile(e,t)}function je(){return T().getWebGpuDevice()}function ze(e){T().setWebGpuDevice(e)}var We=class{constructor(e){y(this,"liteRtWasm");y(this,"defaultEnvironment");y(this,"objectsToDelete",new Set);this.liteRtWasm=e,this.liteRtWasm.setupLogging()}setDefaultEnvironment(e){this.defaultEnvironment=e}getDefaultEnvironment(){if(!this.defaultEnvironment)throw new Error("Default environment is not set.");return this.defaultEnvironment}setWebGpuDevice(e){const t=this.getDefaultEnvironment();this.setDefaultEnvironment(new N({...t.options,webGpuDevice:e}))}getWebGpuDevice(){return this.getDefaultEnvironment().webGpuDevice}_registerObjectForDeletion(e){this.objectsToDelete.add(e)}_unregisterObjectForDeletion(e){this.objectsToDelete.delete(e)}async loadAndCompile(e,t={}){let r;if(typeof e=="string"||e instanceof URL)r=await De(e);else if(e instanceof Uint8Array)r=e;else if(e instanceof ReadableStreamDefaultReader)r=await Ae(e);else throw new Error("Unsupported model type.");const n=t.environment??this.getDefaultEnvironment(),o=t.accelerator??(n.webGpuDevice?"webgpu":"wasm"),s=o==="webgpu";if(s&&!n.webGpuDevice)throw new Error("WebGPU was requested but no WebGPU device is set in the environment.");const a=re(t,n,this.liteRtWasm.getThreadCount()),i=this.liteRtWasm._malloc(r.byteLength);this.liteRtWasm.HEAPU8.set(r,i);const u=this.liteRtWasm.loadModel(a.environment.liteRtEnvironment,i,r.byteLength),l=await this.liteRtWasm.compileModel(a.environment.liteRtEnvironment,u,a),p=new te(u,()=>{this.liteRtWasm._free(i)}),f=new ee(p,l,a,()=>{this.objectsToDelete.delete(f)});if(this.objectsToDelete.add(f),(s||o==="webnn")&&!f.isFullyAccelerated)if(ne())console.warn(`%c[LiteRT]%c Model not fully compiled for ${o}. Partially delegating to WASM execution.`,"background: #FFA000; color: black; font-weight: bold; padding: 2px 5px; border-radius: 3px;","font-weight: bold;");else{console.warn(`%c[LiteRT]%c Model not fully compiled for ${o} on non-JSPI browser. Falling back to WASM execution.`,"background: #D32F2F; color: white; font-weight: bold; padding: 2px 5px; border-radius: 3px;","color: #D32F2F; font-weight: bold;"),f.delete();const R={...t,accelerator:"wasm"};return this.loadAndCompile(r,R)}return f}delete(){for(const e of this.objectsToDelete)e.delete()}};function _e(e,t){if(!e)return t;if(!t)return e;const r=e.endsWith("/")?e:e+"/",n=t.startsWith("/")?t.substring(1):t;return r+n}var Le="litert_wasm_internal.js",Fe="litert_wasm_compat_internal.js",Ge="litert_wasm_threaded_internal.js",Oe="litert_wasm_jspi_internal.js";async function Ne(e,t){const r=e,n=r.endsWith(".wasm")||r.endsWith(".js"),o=await Me("relaxedSimd");if(t!=null&&t.threads){if(t!=null&&t.jspi)throw new Error("The `threads` and `jspi` options are mutually exclusive.");if(n&&console.warn(`The \`threads\` option was specified, but the wasm path ${r} is a full file path. Whether threads are available or not will depend on the loaded file. To allow LiteRT.js to load the threaded wasm file, use a directory path instead of a full file path.`),!o)throw new Error("Threads are only supported with relaxed SIMD, and the current browser does not support relaxed SIMD.");await z("threads")}t!=null&&t.jspi&&(n&&console.warn(`The \`jspi\` option was specified, but the wasm path ${r} is a full file path. Whether JSPI is available or not will depend on the loaded file. To allow LiteRT.js to load the JSPI wasm file, use a directory path instead of a full file path.`),await z("jspi"));let s=Fe;o&&(t!=null&&t.threads?s=Ge:t!=null&&t.jspi?s=Oe:s=Le);let a=e;if(r.endsWith(".wasm"))throw new Error("Please load the `.js` file corresponding to the `.wasm` file, or load the directory containing it.");return r.endsWith(".js")||(a=_e(e,s)),ae(We,a)}function He(e,t){if(X())throw new Error("LiteRT is already loading / loaded.");return G(Ne(e,t).then(async r=>(K(r),r.setDefaultEnvironment(await N.create()),r)).catch(r=>{throw G(void 0),r})),fe()}function Ye(){if(X()&&!$())throw new Error("LiteRT is loading and can not be unloaded or canceled until it is finished loading.");$()&&(T().delete(),K(void 0)),G(void 0)}var H=Promise.resolve();async function qe(e,t,r={}){const n=T(),o=n.liteRtWasm,s=r.environment??n.getDefaultEnvironment();if(!s.webGpuDevice)throw new Error("WebGPU device is required for streamed loading in this implementation.");const a=H;let i;H=new Promise(u=>{i=u}),await a;try{o.registerStreamWeightsCallback(async(u,l,p,f)=>{const h=[];if(u.length!==l.length)throw new Error(`Stream weights callback received arrays of different lengths: tflIds=${u.length}, wgpuBufferIds=${l.length}, `);for(let w=0;w<u.length;w++)h.push({id:u[w],wgpuBufferId:l[w],offset:p[w],length:f[w]});h.sort((w,D)=>w.offset-D.offset),console.log(`[StreamWeights] Starting loading session for ${h.length} tensors:`,JSON.stringify(h));const d=t.getReader();let R=0,m=new Uint8Array(0),v=0;try{for(;v<h.length;){const w=h[v],D=w.offset-R,C=D+w.length;if(C<=m.length){if(D<0)throw new Error(`Stream logic error: weight starts before current buffer (req.offset=${w.offset}, streamOffset=${R}).`);let b=m.subarray(D,C);if(b.byteLength%4!==0){const W=b.byteLength+3&-4,k=new Uint8Array(W);k.set(b),b=k}const B=o.WebGPU.getJsObject(w.wgpuBufferId);if(!B)throw new Error(`Failed to find GPUBuffer for ID: ${w.wgpuBufferId}`);console.log(`[StreamWeights] Writing ${b.byteLength} bytes to GPUBuffer ${w.wgpuBufferId} (tflId=${w.id}, first 5 bytes=${Array.from(b.slice(0,5))})`),s.webGpuDevice.queue.writeBuffer(B,0,b),v++}
1else{const{done:b,value:B}=await d.read();if(b)throw new Error("Stream ended before all weights were loaded.");const W=new Uint8Array(m.length+B.length);W.set(m),W.set(B,m.length),m=W}if(v<h.length){const b=h[v].offset-R,B=Math.min(b,m.length);(B>1024*1024||B===m.length)&&(m=m.slice(B),R+=B)}}}finally{d.releaseLock()}});try{const u=o._malloc(e.byteLength);o.HEAPU8.set(e,u);const l=o.loadModel(s.liteRtEnvironment,u,e.byteLength),p=new te(l,()=>{o._free(u)}),f=re(r,s,o.getThreadCount()),h=await o.compileModel(s.liteRtEnvironment,l,f),d=new ee(p,h,f,()=>{n._unregisterObjectForDeletion(d)});return n._registerObjectForDeletion(d),d}finally{o.registerStreamWeightsCallback(void 0)}}finally{i()}}async function Y(e,t={}){const r=t.environment??e.environment,n=T().liteRtWasm,o=e.liteRtTensorBuffer;if(o.bufferType().value!==g.HOST_MEMORY)throw new Error("Source tensor is not in host memory. Cannot copy to host memory.");const a=o.lock(n.LiteRtTensorBufferLockMode.READ);let i;try{i=n.LiteRtTensorBuffer.createManaged(r.liteRtEnvironment,n.LiteRtTensorBufferType.HOST_MEMORY,o.tensorType(),o.size());const u=i.lock(n.LiteRtTensorBufferLockMode.WRITE);try{const l=new Uint8Array(n.HEAPU8.buffer,a,o.size());n.HEAPU8.set(l,u)}finally{i.unlock()}}finally{o.unlock()}if(!i)throw new Error("Failed to create destination tensor buffer.");return new S(i,r)}async function q(e,t={}){const r=t.environment??e.environment,n=r.webGpuDevice;if(!n)throw new Error("No WebGPU device is available. Did you forget to pass a destination environment that has a WebGPU device?");const o=T().liteRtWasm,a=e.liteRtTensorBuffer.size()+3&-4,i=n.createBuffer({size:a,usage:GPUBufferUsage.MAP_WRITE|GPUBufferUsage.COPY_SRC,mappedAtCreation:!0}),u=await i.getMappedRange(),l=new Uint8Array(u),p=e.liteRtTensorBuffer.lock(o.LiteRtTensorBufferLockMode.READ);try{const d=new Uint8Array(o.HEAPU8.buffer,p,e.liteRtTensorBuffer.size());l.set(d)}finally{e.liteRtTensorBuffer.unlock()}i.unmap();const f=n.createBuffer({size:a,usage:GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST|GPUBufferUsage.STORAGE}),h=n.createCommandEncoder();return h.copyBufferToBuffer(i,0,f,0,a),n.queue.submit([h.finish()]),i.destroy(),new S(f,e.type.layout.dimensions,e.type.dtype,r,()=>{f.destroy()})}async function J(e,t={}){const r=t.environment??e.environment,n=e.environment.webGpuDevice;if(!n)throw new Error("No WebGPU device is available. Does the source tensor have a WebGPU device?");const o=T().liteRtWasm,s=e.liteRtTensorBuffer,a=s.bufferType();if(a!==o.LiteRtTensorBufferType.WEB_GPU_BUFFER_PACKED)throw new Error(`Cannot convert a tensor with a non-WebGPU buffer type ${a} to a CPU tensor.`);const i=o.WebGPU.getJsObject(s.getWebGpuBuffer()),u=s.offset(),l=s.tensorType(),p=l.layout(),f=p.numElements(),h=P(l.elementType().value).typedArrayConstructor;p.delete(),l.delete();let d=i,R=()=>{};if(!(i.usage&GPUBufferUsage.MAP_READ)){d=n.createBuffer({size:i.size,usage:GPUBufferUsage.COPY_DST|GPUBufferUsage.MAP_READ}),R=()=>{d.destroy()};const D=n.createCommandEncoder();D.copyBufferToBuffer(i,0,d,0,i.size),n.queue.submit([D.finish()])}await d.mapAsync(GPUMapMode.READ);const m=d.getMappedRange(),v=new h(m,u,f),w=new S(v,e.type.layout.dimensions,r);return d.unmap(),R(),w}function F(e){return async(t,r)=>{const n=await e(t,r);return t.delete(),n}}function Ie(){S.copyFunctions.set(g.HOST_MEMORY,new Map([[g.HOST_MEMORY,{copyTo:Y,moveTo:F(Y)}],[g.WEB_GPU_BUFFER_PACKED,{copyTo:q,moveTo:F(q)}]])),S.copyFunctions.set(g.WEB_GPU_BUFFER_PACKED,new Map([[g.HOST_MEMORY,{copyTo:J,moveTo:F(J)}]]))}Ie();export{ee as CompiledModel,N as Environment,We as LiteRt,le as LiteRtNotLoadedError,S as Tensor,g as TensorBufferType,$e as getDefaultEnvironment,T as getGlobalLiteRt,fe as getGlobalLiteRtPromise,je as getWebGpuDevice,ke as isWebGPUSupported,xe as loadAndCompile,He as loadLiteRt,qe as loadModelAndWeights,ze as setWebGpuDevice,Me as supportsFeature,Ye as unloadLiteRt};

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.