import { N as c, af as l, ab as m, v as n } from "../index-DSGwv2Yx.js";
import "../webgpu_util-DuofJBMo.js";
import { W as P } from "../backend_webgpu-DI2wXEC2.js";
import { compileProgram as y } from "./webgpu_program.js";
import { m as M } from "../webgpu_program-CbjdYLYk.js";
const b = (o, s) => {
  const i = o.limits.maxComputeWorkgroupsPerDimension, t = s.dispatchLayout, a = s.dispatch;
  if (a.every((r) => r <= i))
    return a;
  n(
    a[0] > i && t.y === void 0 && t.z === void 0,
    () => "Dispatch size exceeds WebGPU limits in Y or Z dimension."
  );
  let e = Math.ceil(Math.sqrt(a[0]));
  return e > i ? (e = Math.ceil(Math.cbrt(a[0])), n(
    e <= i,
    () => "Total dispatch size exceeds WebGPU maximum."
  ), [e, e, e]) : [e, e, 1];
};
class v extends P {
  subgroupMaxSize;
  subgroupMinSize;
  constructor(s, i) {
    super(s, i), this.subgroupMaxSize = i?.subgroupMaxSize ?? 0, this.subgroupMinSize = i?.subgroupMinSize ?? 0;
  }
  runWebGPUProgram(s, i, t, a, e) {
    if (e || (e = this.makeTensorInfo(s.outputShape, t)), c(e.shape) === 0)
      return this.tensorMap.get(e.dataId).values = l(e.dtype, 0), e;
    this.uploadToGPU(e.dataId), s.dispatch = b(this.device, s);
    const r = i.map((h, p) => {
      if (h.dtype === "complex64")
        throw new Error(
          "GPGPUProgram does not support complex64 input. For complex64 dtypes, please separate the program into real and imaginary parts."
        );
      return this.uploadToGPU(h.dataId), {
        // Returning dtype from tensorMap because it reflects dtype
        // of underlying buffer, rather than abstract dtype.
        dtype: this.tensorMap.get(h.dataId).dtype,
        shape: h.shape,
        name: s.variableNames[p]
      };
    });
    s.shaderKey = M(s, r, e);
    const d = m().getBool("WEBGPU_ENGINE_COMPILE_ONLY");
    return s.shaderKey in this.pipelineCache || (this.pipelineCache[s.shaderKey] = y(
      this.device,
      s,
      r,
      e,
      d
    )), s.pipeline = this.pipelineCache[s.shaderKey], d || this.recordAndSubmit(s, e, i, a), e;
  }
}
export {
  v as default
};
