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A,EACgC,iBAAzB,GACT,EAAY,EACZ,EAAa,GAEb,EAAa,EAEf,MAAM,EAAqB,EAAM,kBAG3B,EAAS,UAAU,GAAY,CAAC,EAAY,KAChD,IACE,IAAI,EACsB,iBAAf,EACT,EAAe,EAAmB,GACH,iBAAf,IAChB,EACI,EAAmB,MAAK,GAAW,EAAQ,OAAS,KAO1D,MAAM,EACF,GAAc,qBAAqB,SAAS,OAAO,MAEvD,OAAO,aACH,EAAY,EAAM,QAAQ,YAAa,EAC7C,OAAS,GAIP,MAHI,aAAa,4BAAwC,IAAf,GACxC,EAAE,UAAU,GAER,CACR,KAIF,IAAI,EACJ,GAAI,EAAW,CACb,KAAM,aAAiB,eACrB,MAAM,IAAI,MACN,2EAGN,QAAgB,EAAM,IAAI,EAAW,EACvC,MACE,QAAgB,EAAM,IAAI,GAS5B,OALA,YAAY,GAAQ,IAClB,EAAO,QAAO,IAIT,YAAY,GAAS,CAAC,EAAQ,KACnC,IAAI,EACJ,IACE,EAAa,aAAa,EAC5B,OAAS,GAIP,MAHI,aAAa,4BAAwC,IAAf,GACxC,EAAE,UAAU,GAER,CACR,SAGE,EAAO,QACT,CACA,OAAO,CAAA,GAEX,Q","file":"/npm/@litertjs/tfjs-interop@2.0.0/dist/index.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/@litertjs/tfjs-interop@2.0.0/dist/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","/**\n * Copyright 2025 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {CompiledModel, type DType, getDefaultEnvironment, SignatureRunner, Tensor, TensorBufferType, TensorDetails, type TypedArray} from '@litertjs/core';\nimport {type WebGPUBackend} from '@tensorflow/tfjs-backend-webgpu';\nimport * as tf from '@tensorflow/tfjs-core';\n\nconst WEBGPU_DEVICE_MISMATCH_ERROR_MESSAGE =\n    'To convert from TFJS to LiteRT, you must use an environment ' +\n    'that has the same WebGPU device as the TFJS backend.\\n\\n' +\n    'Since LiteRT.js sets some WebGPU options by default, the recommended ' +\n    'way to share a WebGPU device is to let LiteRT.js create the device when ' +\n    'it sets up the default environment ' +\n    'and then create a new TFJS WebGPU backnend using that device:\\n\\n' +\n    '```javascript\\n' +\n    '    const device = await getWebGpuDevice(); // from LiteRT.js\\n' +\n    '    const adapterInfo = device.adapterInfo;\\n' +\n    '    tf.removeBackend(\\'webgpu\\');\\n' +\n    '    tf.registerBackend(\\'webgpu\\', () => new WebGPUBackend(device, adapterInfo));\\n\\n' +\n    '    await tf.setBackend(\\'webgpu\\');\\n' +\n    '    ```';\n\nfunction isWebGpuBackend(): boolean {\n  // We can't use `instanceof WebGPUBackend` because then we would have to\n  // import the actual WebGPU backend rather than just its type.\n  return tf.getBackend().includes('webgpu');\n}\n\n/**\n * Error thrown when a tensor can not be converted between TFJS and LiteRT.\n */\nexport class TensorConversionError extends Error {\n  tensor?: string|number;\n  private originalMessage: string;\n\n  constructor(message: string) {\n    super(message);\n    this.originalMessage = message;\n  }\n\n  setTensor(tensor: string|number) {\n    this.message = `For tensor ${tensor}: ${this.originalMessage}`;\n    this.tensor = tensor;\n  }\n}\n\nfunction tfjsDtypeToLiteRt(tfjsDtype: tf.DataType): DType {\n  switch (tfjsDtype) {\n    case 'float32':\n      return 'float32';\n    case 'int32':\n      return 'int32';\n    default:\n      throw new Error(\n          `Unsupported type: ${tfjsDtype}. You may need to cast ` +\n          'to int32 or float32.');\n  }\n}\n\nfunction liteRtDtypeToTfjs(liteRtDtype: DType): tf.DataType {\n  switch (liteRtDtype) {\n    case 'float32':\n      return 'float32';\n    case 'int32':\n      return 'int32';\n    default:\n      throw new Error(`Unsupported type: ${liteRtDtype}.`);\n  }\n}\n\n/**\n * Convert a TFJS tensor to a LiteRT tensor.\n *\n * The TFJS tensor is copied to whichever LiteRT buffer type is most efficient\n * to copy to.\n * - For TFJS tensors on WebGPU, TensorBufferType.WEB_GPU_BUFFER_PACKED is\n *   used (if the TFJS tensor has been uploaded to WebGPU).\n * - Otherwise, TensorBufferType.HOST_MEMORY is used.\n *\n * The user can specify a preferred buffer type for the LiteRT tensor to be\n * created with, but this does not guarantee that the tensor will be created\n * with that buffer type. For example, if the TFJS tensor is on WebGPU and not\n * cached in CPU memory, the WebGPU buffer type will be used even if\n * TensorBufferType.HOST_MEMORY is requested.\n */\nexport function tfjsToLitert(\n    tfjsTensor: tf.Tensor, environment = getDefaultEnvironment(),\n    preferredBufferType?: TensorBufferType): Tensor {\n  if (isWebGpuBackend()) {\n    const backend = tf.backend() as WebGPUBackend;\n    const tensorMapValue = backend.tensorMap.get(tfjsTensor.dataId);\n\n    // Sometimes, the user does not want a WebGPU tensor even when the data is\n    // on WebGPU (e.g., they're running a CPU model). In this case, we should\n    // return a CPU tensor if we can do so efficiently, i.e., if TFJS has the\n    // tensor cached in CPU memory.\n    const shouldReturnCpuTensor = tensorMapValue?.values &&\n        preferredBufferType === TensorBufferType.HOST_MEMORY;\n    if (tensorMapValue?.resource && !shouldReturnCpuTensor) {\n      // Then the data is on WebGPU, so create a new LiteRT WebGPU /\n      // WEB_GPU_BUFFER_PACKED tensor.\n\n      if (backend.device !== environment.webGpuDevice) {\n        throw new TensorConversionError(WEBGPU_DEVICE_MISMATCH_ERROR_MESSAGE);\n      }\n\n      const gpuData = tfjsTensor.dataToGPU();\n      const buffer = gpuData.buffer;\n      if (!buffer) {\n        throw new TensorConversionError(\n            'TFJS tensor did not have a GPU buffer.');\n      }\n\n      const dtype = tfjsDtypeToLiteRt(tfjsTensor.dtype);\n      return new Tensor(buffer, tfjsTensor.shape, dtype, environment, () => {\n        // Return the tfjs GPU data back to the pool.\n        gpuData.tensorRef.dispose();\n      });\n    }\n  }\n\n  // Otherwise, copy the data to a CPU / HOST_MEMORY tensor.\n  const tfjsData = tfjsTensor.dataSync();\n  // dtype is handled implicitly by the TypeArray type returned by\n  // dataSync.\n  return new Tensor(tfjsData as TypedArray, tfjsTensor.shape, environment);\n}\n\n/**\n * Convert a LiteRT tensor to a TFJS tensor.\n *\n * The tensor is copied to the respective TFJS backend.\n * - Wasm CPU tensors will be copied to the TFJS tensor's CPU cache. This\n * is supported on all TFJS backends.\n * - WebGPU tensors will be copied to the TFJS WebGPU backend. This is\n * only supported on the TFJS WebGPU backend.\n */\nexport function litertToTfjs(tensor: Tensor): tf.Tensor {\n  switch (tensor.bufferType) {\n    case TensorBufferType.HOST_MEMORY:\n      return litertToTfjsCpu(tensor);\n    case TensorBufferType.WEB_GPU_BUFFER_PACKED:\n      return litertToTfjsWebGpu(tensor);\n    default:\n      throw new Error('Unsupported accelerator: ' + tensor.accelerator);\n  }\n}\n\nfunction litertToTfjsCpu(tensor: Tensor): tf.Tensor {\n  const typedArray = tensor.toTypedArray();\n  const tfjsDataType = liteRtDtypeToTfjs(tensor.type.dtype);\n\n  return tf.tensor(\n      typedArray, [...tensor.type.layout.dimensions], tfjsDataType);\n}\n\nfunction litertToTfjsWebGpu(tensor: Tensor): tf.Tensor {\n  if (!isWebGpuBackend()) {\n    throw new TensorConversionError(\n        'LiteRT WebGPU tensors can only be converted to TFJS WebGPU tensors, ' +\n        `but the TFJS backend is ${\n            tf.getBackend()}. If you want to convert to a CPU ` +\n        'TFJS tensor, please first move (or copy) the LiteRT tensor to CPU ' +\n        'with `tensor.moveTo(\\'wasm\\')` (or `tensor.copyTo(\\'wasm\\')`).');\n  }\n\n  const backend = tf.backend() as WebGPUBackend;\n  const device = backend.device;\n  if (device !== tensor.environment.webGpuDevice) {\n    throw new TensorConversionError(WEBGPU_DEVICE_MISMATCH_ERROR_MESSAGE);\n  }\n\n  const litertBuffer = tensor.toGpuBuffer();\n  const requiredSizeInBytes = tensor.liteRtTensorBuffer.size();\n\n  let buffer: GPUBuffer;\n  if (litertBuffer.size === requiredSizeInBytes) {\n    buffer = litertBuffer;\n  } else {\n    // If the LiteRT buffer is larger than needed, we need to copy it to a\n    // new buffer with the correct size, because TFJS does not support\n    // buffer views.\n    buffer = device.createBuffer({\n      size: requiredSizeInBytes,\n      usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC |\n          GPUBufferUsage.COPY_DST,\n    });\n    const commandEncoder = device.createCommandEncoder();\n    commandEncoder.copyBufferToBuffer(\n        litertBuffer, 0, buffer, 0, requiredSizeInBytes);\n    device.queue.submit([commandEncoder.finish()]);\n  }\n\n  // We do not need to manually make a copy of the WebGPU buffer from\n  // LiteRT. TFJS will automatically copy the buffer when creating the\n  // tensor (unless we set `zeroCopy: true`, which we don't).\n  // https://github.com/tensorflow/tfjs/blob/tfjs-v4.22.0/tfjs-backend-webgpu/src/backend_webgpu.ts#L584-L586\n\n  const tfjsDataType = liteRtDtypeToTfjs(tensor.type.dtype);\n  return tf.tensor({buffer}, [...tensor.type.layout.dimensions], tfjsDataType);\n}\n\n/** Container types for tensors passed to / from the model. */\ntype Container<T> = T|Record<string, T>|T[];\n\n// clang-format off\n/**\n * Maps the type for a Container of one value type to a type for a Container of\n * another value type.\n */\ntype MapContainer<T extends Container<unknown>, NewVal> = T extends Array<infer _> ? NewVal[]\n    : T extends Record<string, infer _> ? Record<string, NewVal>\n    : NewVal;\n// clang-format on\n\n/**\n * Map a Container of one value type to the same Container type of another\n * value type.\n *\n * This is similar to a functor.\n *\n * @param t The Container to map.\n * @param f The function to map each value in the Container.\n * @param isA The function to check if the value is of type A.\n */\nfunction mapOnContainer<A, T extends Container<A>, B>(\n    t: T, f: (a: A, keyOrIndex?: string|number) => B,\n    isA: (val: unknown) => val is A): MapContainer<T, B> {\n  if (isA(t)) {\n    return f(t) as MapContainer<T, B>;\n  } else if (Array.isArray(t)) {\n    return t.map(f) as MapContainer<T, B>;\n  } else {\n    return Object.fromEntries(Object.entries(t as Record<string, A>)\n                                  .map(([key, a]) => [key, f(a, key)])) as\n        MapContainer<T, B>;\n  }\n}\n\n/**\n * Map a Container of TFJS tensors to the same Container type but with new\n * values from f.\n *\n * @param t The Container to map.\n * @param f The function to map each value in the Container.\n */\nfunction mapOnTfjs<T extends Container<tf.Tensor>, NewVal>(\n    t: T, f: (tfjsTensor: tf.Tensor, keyOrIndex?: string|number) => NewVal):\n    MapContainer<T, NewVal> {\n  return mapOnContainer(t, f, (val) => val instanceof tf.Tensor);\n}\n\n/**\n * Map a Container of LiteRT tensors to the same Container type but with\n * new values from f.\n *\n * @param t The Container to map.\n * @param f The function to map each value in the Container.\n */\nfunction mapOnLiteRt<T extends Container<Tensor>, NewVal>(\n    t: T, f: (liteRtTensor: Tensor, keyOrIndex?: string|number) => NewVal):\n    MapContainer<T, NewVal> {\n  return mapOnContainer(t, f, (val) => val instanceof Tensor);\n}\n\n/**\n * Run a LiteRT CompiledModel with TFJS inputs and outputs.\n *\n * If a signature name is not provided, the default signature will be\n * used.\n *\n * When calling a Wasm CPU LiteRT model with WebGPU TFJS tensors, please\n * first call `await tensor.data()` on each tensor to more efficiently\n * copy the tensors to CPU.\n */\n// Without signature name\nexport function runWithTfjsTensors(\n    model: CompiledModel|SignatureRunner,\n    input: tf.Tensor|tf.Tensor[]): Promise<tf.Tensor[]>;\nexport function runWithTfjsTensors(\n    model: CompiledModel|SignatureRunner,\n    input: Record<string, tf.Tensor>): Promise<Record<string, tf.Tensor>>;\n// With signature name\nexport function runWithTfjsTensors(\n    model: CompiledModel, signature: string,\n    input: tf.Tensor|tf.Tensor[]): Promise<tf.Tensor[]>;\nexport function runWithTfjsTensors(\n    model: CompiledModel, signature: string,\n    input: Record<string, tf.Tensor>): Promise<Record<string, tf.Tensor>>;\n// The following signatures are needed since TS won't automatically\n// distribute a union type across the above function signatures.\n// https://github.com/microsoft/TypeScript/issues/14107\nexport function runWithTfjsTensors(\n    model: CompiledModel|SignatureRunner,\n    input: tf.Tensor|tf.Tensor[]|\n    Record<string, tf.Tensor>): Promise<tf.Tensor[]|Record<string, tf.Tensor>>;\nexport function runWithTfjsTensors(\n    model: CompiledModel, signature: string,\n    input: tf.Tensor|tf.Tensor[]|\n    Record<string, tf.Tensor>): Promise<tf.Tensor[]|Record<string, tf.Tensor>>;\nexport async function runWithTfjsTensors(\n    model: CompiledModel|SignatureRunner,\n    inputOrSignatureName: string|tf.Tensor|tf.Tensor[]|\n    Record<string, tf.Tensor>,\n    maybeInputOrLiteRt?: tf.Tensor|tf.Tensor[]|\n    Record<string, tf.Tensor>): Promise<tf.Tensor[]|Record<string, tf.Tensor>> {\n  let signature: string|undefined;\n  let tfjsInputs: Record<string, tf.Tensor>|tf.Tensor[]|tf.Tensor;\n  if (typeof inputOrSignatureName === 'string') {\n    signature = inputOrSignatureName;\n    tfjsInputs = maybeInputOrLiteRt as Container<tf.Tensor>;\n  } else {\n    tfjsInputs = inputOrSignatureName;\n  }\n  const litertInputDetails = model.getInputDetails();\n\n  // Convert TFJS inputs to LiteRT tensors.\n  const inputs = mapOnTfjs(tfjsInputs, (tfjsTensor, keyOrIndex) => {\n    try {\n      let inputDetails: TensorDetails|undefined;\n      if (typeof keyOrIndex === 'number') {\n        inputDetails = litertInputDetails[keyOrIndex];\n      } else if (typeof keyOrIndex === 'string') {\n        inputDetails =\n            litertInputDetails.find(details => details.name === keyOrIndex);\n      }\n\n      // If the user runs a CPU model with a WebGPU TFJS tensor that is already\n      // cached in TFJS CPU memory, we should create a CPU tensor from that\n      // cache rather than a WebGPU tensor (which `model.run` would then\n      // have to copy back to CPU).\n      const preferredBufferType =\n          inputDetails?.supportedBufferTypes.values().next().value;\n\n      return tfjsToLitert(\n          tfjsTensor, model.options.environment, preferredBufferType);\n    } catch (e) {\n      if (e instanceof TensorConversionError && keyOrIndex !== undefined) {\n        e.setTensor(keyOrIndex);\n      }\n      throw e;\n    }\n  });\n\n  // Run the model.\n  let outputs: Record<string, Tensor>|Tensor[];\n  if (signature) {\n    if (!(model instanceof CompiledModel)) {\n      throw new Error(\n          'Signature specified but a SignatureRunner was passed instead of a' +\n          ' model');\n    }\n    outputs = await model.run(signature, inputs);\n  } else {\n    outputs = await model.run(inputs);\n  }\n\n  // Delete all the LiteRT inputs that we created to pass to the model.\n  mapOnLiteRt(inputs, tensor => {\n    tensor.delete();\n  });\n\n  // Convert LiteRT outputs to TFJS tensors.\n  return mapOnLiteRt(outputs, (tensor, keyOrIndex) => {\n    let tfjsTensor: tf.Tensor;\n    try {\n      tfjsTensor = litertToTfjs(tensor);\n    } catch (e) {\n      if (e instanceof TensorConversionError && keyOrIndex !== undefined) {\n        e.setTensor(keyOrIndex);\n      }\n      throw e;\n    } finally {\n      // It is safe to delete the LiteRT tensor because TFJS will have already\n      // enqueued the WebGPU buffer copy.\n      tensor.delete();\n    }\n    return tfjsTensor;\n  });\n}\n"]}