import { _ as B, N as G, aU as K, a7 as W, aH as z, aV as V, ab as N, aI as F, am as S } from "./index-DSGwv2Yx.js";
import { u as O, f as H } from "./gpgpu_math-3bCb5ooU.js";
import { f as v } from "./backend_util-qwSFfxYx.js";
function Y(t, e) {
  return ["x", "y", "z", "w", "u", "v"].slice(0, e).map((s) => `${t}.${s}`);
}
function Z(t, e) {
  return e === 1 ? [t] : Y(t, e);
}
function pe(t, e) {
  if (t === 1)
    return "rc";
  let s = "";
  for (let r = 0; r < t; r++)
    s += e[r], r < t - 1 && (s += ",");
  return s;
}
class q {
  constructor(e, s) {
    this.variableNames = ["A"], this.outputShape = e, this.enableShapeUniforms = O(this.outputShape.length), this.userCode = `
      float unaryOperation(float x) {
        ${s}
      }

      void main() {
        float x = getAAtOutCoords();
        float y = unaryOperation(x);

        setOutput(y);
      }
    `;
  }
}
const T = "if (isnan(x)) return x;", M = "return x;", de = "return abs(x);", j = "return (x >= 0.0) ? x : (exp(x) - 1.0);", J = T + `
  return (x < 0.0) ? 0.0 : x;
`, Q = T + `
  return (x < 0.0) ? 0.0 : min(6.0, x);
`, he = "return x;", X = "return 1.0 / (1.0 + exp(-1.0 * x));";
const ee = "return x;", te = `
  vec4 result;

  result.r = (x.r >= 0.0) ? x.r : (exp(x.r) - 1.0);
  result.g = (x.g >= 0.0) ? x.g : (exp(x.g) - 1.0);
  result.b = (x.b >= 0.0) ? x.b : (exp(x.b) - 1.0);
  result.a = (x.a >= 0.0) ? x.a : (exp(x.a) - 1.0);

  return result;
`, se = `
  vec4 result = x * vec4(greaterThanEqual(x, vec4(0.0)));
  bvec4 isNaN = isnan(x);

  result.r = isNaN.r ? x.r : result.r;
  result.g = isNaN.g ? x.g : result.g;
  result.b = isNaN.b ? x.b : result.b;
  result.a = isNaN.a ? x.a : result.a;

  return result;
`, ae = `
  vec4 result = min(x, vec4(6.)) * vec4(greaterThanEqual(x, vec4(0.0)));
  bvec4 isNaN = isnan(x);

  result.r = isNaN.r ? x.r : result.r;
  result.g = isNaN.g ? x.g : result.g;
  result.b = isNaN.b ? x.b : result.b;
  result.a = isNaN.a ? x.a : result.a;

  return result;
`, re = "return 1.0 / (1.0 + exp(-1.0 * x));";
class ne {
  constructor(e, s) {
    this.variableNames = ["A"], this.packedInputs = !0, this.packedOutput = !0, this.outputShape = e, this.enableShapeUniforms = O(this.outputShape.length), this.userCode = `
      vec4 unaryOperation(vec4 x) {
        ${s}
      }

      void main() {
        vec4 x = getAAtOutCoords();
        vec4 y = unaryOperation(x);

        setOutput(y);
      }
    `;
  }
}
const fe = `
  if (isnan(a)) return a;
  if (isnan(b)) return b;
`;
class b {
  constructor(e, s, r) {
    this.variableNames = ["A", "B"], this.outputShape = B(s, r), this.enableShapeUniforms = O(this.outputShape.length), this.userCode = `
      float binaryOperation(float a, float b) {
        ${e}
      }

      void main() {
        float a = getAAtOutCoords();
        float b = getBAtOutCoords();
        setOutput(binaryOperation(a, b));
      }
    `;
  }
}
const xe = `
  result.r = isNaN.r ? NAN : result.r;
  result.g = isNaN.g ? NAN : result.g;
  result.b = isNaN.b ? NAN : result.b;
  result.a = isNaN.a ? NAN : result.a;
`;
class E {
  constructor(e, s, r, u = !1) {
    this.variableNames = ["A", "B"], this.supportsBroadcasting = !0, this.packedInputs = !0, this.packedOutput = !0, this.outputShape = B(s, r);
    const n = this.outputShape.length;
    this.enableShapeUniforms = O(n);
    let o = "";
    if (u)
      if (n === 0 || G(this.outputShape) === 1)
        o = `
          result.y = 0.;
          result.z = 0.;
          result.w = 0.;
        `;
      else if (o = `
          ${H(n)} coords = getOutputCoords();
        `, n === 1)
        this.enableShapeUniforms ? o += `
            result.y = (coords + 1) >= outShape ? 0. : result.y;
            result.z = 0.;
            result.w = 0.;
          ` : o += `
            result.y = (coords + 1) >= ${this.outputShape[0]} ? 0. : result.y;
            result.z = 0.;
            result.w = 0.;
          `;
      else {
        const a = Z("coords", n);
        this.enableShapeUniforms ? o += `
            bool nextRowOutOfBounds =
              (${a[n - 2]} + 1) >= outShape[${n} - 2];
            bool nextColOutOfBounds =
              (${a[n - 1]} + 1) >= outShape[${n} - 1];
            result.y = nextColOutOfBounds ? 0. : result.y;
            result.z = nextRowOutOfBounds ? 0. : result.z;
            result.w = nextColOutOfBounds || nextRowOutOfBounds ? 0. : result.w;
          ` : o += `
            bool nextRowOutOfBounds =
              (${a[n - 2]} + 1) >= ${this.outputShape[n - 2]};
            bool nextColOutOfBounds =
              (${a[n - 1]} + 1) >= ${this.outputShape[n - 1]};
            result.y = nextColOutOfBounds ? 0. : result.y;
            result.z = nextRowOutOfBounds ? 0. : result.z;
            result.w = nextColOutOfBounds || nextRowOutOfBounds ? 0. : result.w;
          `;
      }
    this.userCode = `
      vec4 binaryOperation(vec4 a, vec4 b) {
        ${e}
      }

      void main() {
        vec4 a = getAAtOutCoords();
        vec4 b = getBAtOutCoords();

        vec4 result = binaryOperation(a, b);
        ${o}

        setOutput(result);
      }
    `;
  }
}
function P(t) {
  const { inputs: e, backend: s } = t, { x: r } = e;
  return s.incRef(r.dataId), { dataId: r.dataId, shape: r.shape, dtype: r.dtype };
}
const ge = {
  kernelName: K,
  backendName: "webgl",
  kernelFunc: P
};
function L(t) {
  const { inputs: e, backend: s } = t, { real: r, imag: u } = e, n = s.makeTensorInfo(r.shape, "complex64"), o = s.texData.get(n.dataId), i = P({ inputs: { x: r }, backend: s }), a = P({ inputs: { x: u }, backend: s });
  return o.complexTensorInfos = { real: i, imag: a }, n;
}
const me = {
  kernelName: W,
  backendName: "webgl",
  kernelFunc: L
};
const w = "return (a < 0.) ? b * a : a;", R = `
  vec4 aLessThanZero = vec4(lessThan(a, vec4(0.)));
  return (aLessThanZero * (b * a)) + ((vec4(1.0) - aLessThanZero) * a);
`;
function oe(t) {
  const { inputs: e, backend: s, attrs: r } = t, { x: u } = e, { alpha: n } = r, o = s.makeTensorInfo([], "float32", V(n, "float32")), i = N().getBool("WEBGL_PACK_BINARY_OPERATIONS") ? new E(R, u.shape, o.shape) : new b(w, u.shape, o.shape), a = s.runWebGLProgram(i, [u, o], "float32");
  return s.disposeIntermediateTensorInfo(o), a;
}
const be = {
  kernelName: z,
  backendName: "webgl",
  kernelFunc: oe
};
const k = "return (a < 0.) ? b * a : a;", U = `
  vec4 aLessThanZero = vec4(lessThan(a, vec4(0.)));
  return (aLessThanZero * (b * a)) + ((vec4(1.0) - aLessThanZero) * a);
`;
function ue(t) {
  const { inputs: e, backend: s } = t, { x: r, alpha: u } = e, n = N().getBool("WEBGL_PACK_BINARY_OPERATIONS") ? new E(U, r.shape, u.shape) : new b(k, r.shape, u.shape);
  return s.runWebGLProgram(n, [r, u], "float32");
}
const Ne = {
  kernelName: F,
  backendName: "webgl",
  kernelFunc: ue
};
const Oe = "if (isnan(x)) return x;";
function ye({ opSnippet: t, packedOpSnippet: e, cpuKernelImpl: s, dtype: r }) {
  return ({ inputs: u, backend: n }) => {
    const { x: o } = u, i = n, a = r || o.dtype;
    if (i.shouldExecuteOnCPU([o]) && s != null) {
      const d = i.texData.get(o.dataId), y = s(d.values, a);
      return i.makeTensorInfo(o.shape, a, y);
    }
    const c = N().getBool("WEBGL_PACK_UNARY_OPERATIONS") && e != null;
    let l;
    return c ? l = new ne(o.shape, e) : l = new q(o.shape, t), i.runWebGLProgram(l, [o], a);
  };
}
function Ie({ opSnippet: t, packedOpSnippet: e, checkOutOfBounds: s = !1, supportsComplex: r = !1, cpuKernelImpl: u, dtype: n }) {
  return ({ inputs: o, backend: i }) => {
    const { a, b: c } = o, l = i;
    if (r && a.dtype === "complex64") {
      const h = l.texData.get(a.dataId), f = l.texData.get(c.dataId), [g, m] = [
        [h.complexTensorInfos.real, f.complexTensorInfos.real],
        [h.complexTensorInfos.imag, f.complexTensorInfos.imag]
      ].map((C) => {
        const [p, x] = C, $ = {
          dataId: p.dataId,
          dtype: p.dtype,
          shape: a.shape
        }, _ = {
          dataId: x.dataId,
          dtype: x.dtype,
          shape: c.shape
        }, D = new b(t, a.shape, c.shape);
        return l.runWebGLProgram(D, [$, _], S(p.dtype, x.dtype));
      }), A = L({ inputs: { real: g, imag: m }, backend: l });
      return l.disposeIntermediateTensorInfo(g), l.disposeIntermediateTensorInfo(m), A;
    }
    const d = n || S(a.dtype, c.dtype);
    if ((a.dtype === "string" || c.dtype === "string" || l.shouldExecuteOnCPU([a, c])) && u != null) {
      const h = l.texData.get(a.dataId).values, f = l.texData.get(c.dataId).values, g = a.dtype === "string" ? (
        // tslint:disable-next-line: no-any
        v(h)
      ) : h, m = a.dtype === "string" ? (
        // tslint:disable-next-line: no-any
        v(f)
      ) : f, [A, C] = u(a.shape, c.shape, g, m, d), p = l.makeTensorInfo(C, d), x = l.texData.get(p.dataId);
      return x.values = A, p;
    }
    const y = N().getBool("WEBGL_PACK_BINARY_OPERATIONS") && e != null;
    let I;
    return y ? I = new E(e, a.shape, c.shape, s) : I = new b(t, a.shape, c.shape), l.runWebGLProgram(I, [a, c], d);
  };
}
function Ae(t, e = !1) {
  if (t === "linear")
    return e ? ee : M;
  if (t === "relu")
    return e ? se : J;
  if (t === "elu")
    return e ? te : j;
  if (t === "relu6")
    return e ? ae : Q;
  if (t === "prelu")
    return e ? U : k;
  if (t === "leakyrelu")
    return e ? R : w;
  if (t === "sigmoid")
    return e ? re : X;
  throw new Error(`Activation ${t} has not been implemented for the WebGL backend.`);
}
export {
  de as A,
  E as B,
  T as C,
  ne as U,
  Z as a,
  Ie as b,
  pe as c,
  he as d,
  q as e,
  L as f,
  Y as g,
  b as h,
  P as i,
  fe as j,
  xe as k,
  Oe as l,
  Ae as m,
  me as n,
  ge as o,
  be as p,
  Ne as q,
  ye as u
};
