import { o as g, n as w, v as A, ag as Te, d as ke, h as M, m as Q, E as q, z as ae, N as ue, _ as le, a2 as fe, bb as he, aD as pe, bc as Ie, t as S, J as $e, a_ as Ee } from "./index-DSGwv2Yx.js";
import { t as be } from "./tensor1d-BMl0eZYV.js";
import { r as Le } from "./random_normal-CIm8lk2-.js";
import { s as P } from "./slice-BvItlgXu.js";
import { r as c } from "./reshape-BIN71H3p.js";
import { g as Ne } from "./gather-DEyjXNb1.js";
import { e as Pe } from "./step-DQY6_ABw.js";
import { c as ve } from "./clip_by_value-CLAD4h_I.js";
import { t as Ce } from "./tile-CsWlVKKz.js";
import { s as Fe, b as Me, c as je, g as Ve } from "./selu_util-BXdhy_W6.js";
import { m as $ } from "./mat_mul-DP86qZtZ.js";
import { t as Ue } from "./transpose-Qxz-4os3.js";
import { c as j } from "./concat-Dqk7Xk7h.js";
import { g as xe, r as Be } from "./dropout_util-N0z8Os-K.js";
import { f as Ge } from "./floor-CymuCmTO.js";
function Je(e) {
  return j(
    e,
    0
    /* axis */
  );
}
const qe = /* @__PURE__ */ g({ concat1d_: Je });
function Ke(e, n) {
  return j(e, n);
}
const Re = /* @__PURE__ */ g({ concat2d_: Ke });
function Ze(e, n) {
  return j(e, n);
}
const We = /* @__PURE__ */ g({ concat3d_: Ze });
function Ye(e, n) {
  return j(e, n);
}
const He = /* @__PURE__ */ g({ concat4d_: Ye });
function Qe(e, n, t) {
  const s = w(e, "x", "slice1d");
  return A(s.rank === 1, () => `slice1d expects a rank-1 tensor, but got a rank-${s.rank} tensor`), P(s, [n], [t]);
}
const X = /* @__PURE__ */ g({ slice1d_: Qe });
function Xe(e, n, t) {
  const s = w(e, "x", "slice2d");
  return A(s.rank === 2, () => `slice2d expects a rank-2 tensor, but got a rank-${s.rank} tensor`), P(s, n, t);
}
const we = /* @__PURE__ */ g({ slice2d_: Xe });
function ze(e, n, t) {
  const s = w(e, "x", "slice3d");
  return A(s.rank === 3, () => `slice3d expects a rank-3 tensor, but got a rank-${s.rank} tensor`), P(s, n, t);
}
const z = /* @__PURE__ */ g({ slice3d_: ze });
function en(e, n, t) {
  const s = w(e, "x", "slice4d");
  return A(s.rank === 4, () => `slice4d expects a rank-4 tensor, but got a rank-${s.rank} tensor`), P(s, n, t);
}
const U = /* @__PURE__ */ g({ slice4d_: en });
function nn(e, n, t, s) {
  const r = w(e, "x", "dropout");
  if (A(r.dtype === "float32", () => `x has to be a floating point tensor since it's going to be scaled, but got a ${r.dtype} tensor instead.`), A(n >= 0 && n < 1, () => `rate must be a float in the range [0, 1), but got ${n}.`), n === 0)
    return e instanceof Te ? r.clone() : r;
  const o = xe(r, t), i = 1 - n, f = ke(Ge(M(Be(o, 0, 1, "float32", s), i)), i);
  return Q(r, f);
}
const tn = /* @__PURE__ */ g({ dropout_: nn });
function sn({ a: e, b: n, transposeA: t = !1, transposeB: s = !1, bias: r, activation: o = "linear", preluActivationWeights: i, leakyreluAlpha: f = 0.2 }) {
  if (Fe(q.state.gradientDepth, o) === !1) {
    let _ = $(e, n, t, s);
    return r != null && (_ = M(_, r)), Me(_, o, i, f);
  }
  let a = w(e, "a", "fused matMul"), u = w(n, "b", "fused matMul");
  [a, u] = ae(a, u);
  const m = t ? a.shape[a.rank - 2] : a.shape[a.rank - 1], d = s ? u.shape[u.rank - 1] : u.shape[u.rank - 2], T = t ? a.shape[a.rank - 1] : a.shape[a.rank - 2], h = s ? u.shape[u.rank - 2] : u.shape[u.rank - 1], ne = a.shape.slice(0, -2), I = u.shape.slice(0, -2), te = ue(ne), se = ue(I);
  A(m === d, () => `Error in fused matMul: inner shapes (${m}) and (${d}) of Tensors with shapes ${a.shape} and ${u.shape} and transposeA=${t} and transposeB=${s} must match.`);
  const B = le(a.shape.slice(0, -2), u.shape.slice(0, -2)).concat([T, h]), G = t ? c(a, [te, m, T]) : c(a, [te, T, m]), J = s ? c(u, [se, h, d]) : c(u, [se, d, h]);
  let E;
  r != null && (E = w(r, "bias", "fused matMul"), [E] = ae(E, a), le(B, E.shape));
  let re;
  i != null && (re = w(i, "prelu weights", "fused matMul"));
  const oe = (_, v) => {
    const [O, D, y, V] = v, k = je(c(_, y.shape), y, o);
    let b, L;
    if (!t && !s ? (b = $(k, D, !1, !0), L = $(O, k, !0, !1)) : !t && s ? (b = $(k, D, !1, !1), L = $(k, O, !0, !1)) : t && !s ? (b = $(D, k, !1, !0), L = $(O, k, !1, !1)) : (b = $(D, k, !0, !0), L = $(k, O, !0, !0)), r != null) {
      const ye = Ve(V, k);
      return [b, L, ye];
    } else
      return [b, L];
  }, ie = {
    a: G,
    b: J,
    bias: E,
    preluActivationWeights: re
  }, ce = { transposeA: t, transposeB: s, activation: o, leakyreluAlpha: f };
  return r == null ? fe((v, O, D) => {
    const y = (
      // tslint:disable-next-line: no-unnecessary-type-assertion
      q.runKernel(he, ie, ce)
    );
    return D([v, O, y]), { value: c(y, B), gradFunc: oe };
  })(G, J) : fe((v, O, D, y) => {
    const V = (
      // tslint:disable-next-line: no-unnecessary-type-assertion
      q.runKernel(he, ie, ce)
    );
    return y([v, O, V, D]), { value: c(V, B), gradFunc: oe };
  })(G, J, E);
}
const de = /* @__PURE__ */ g({ fusedMatMul_: sn });
class Ae extends Error {
  constructor(n) {
    super(n), Object.setPrototypeOf(this, Ae.prototype);
  }
}
class Se extends Error {
  constructor(n) {
    super(n), Object.setPrototypeOf(this, Se.prototype);
  }
}
class l extends Error {
  constructor(n) {
    super(n), Object.setPrototypeOf(this, l.prototype);
  }
}
class F extends Error {
  constructor(n) {
    super(n), Object.setPrototypeOf(this, F.prototype);
  }
}
class ee extends Error {
  constructor(n) {
    super(n), Object.setPrototypeOf(this, ee.prototype);
  }
}
function Pn(e, n) {
  if (Array.isArray(e)) {
    let t = [];
    for (let s = 0; s < n; s++)
      t = t.concat(e);
    return t;
  } else {
    const t = new Array(n);
    return t.fill(e), t;
  }
}
function me(e, n) {
  if (!e)
    throw new ee(n);
}
function vn(e, n) {
  let t = 0;
  for (const s of e)
    s === n && t++;
  return t;
}
function Cn(e) {
  return e.length === 1 ? e[0] : e;
}
function Fn(e) {
  return Array.isArray(e) ? e : [e];
}
function Mn(e) {
  const t = e.replace(/(.)([A-Z][a-z0-9]+)/g, "$1_$2").replace(/([a-z])([A-Z])/g, "$1_$2").toLowerCase();
  return t[0] !== "_" ? t : "private" + t;
}
function jn(e) {
  return e.length <= 1 || e.indexOf("_") === -1 ? e : e.replace(/[_]+(\w|$)/g, (n, t) => t.toUpperCase());
}
let p = {};
function Vn(e) {
  if (e == null)
    return null;
  const n = {};
  return n.className = e.getClassName(), n.config = e.getConfig(), n;
}
function W(e) {
  if (!(e == null || typeof e != "object"))
    if (Array.isArray(e))
      e.forEach((n) => W(n));
    else {
      const n = Object.keys(e);
      for (const t of n) {
        const s = e[t];
        s != null && typeof s == "object" && (!Array.isArray(s) && s.type === "ndarray" && typeof s.value == "number" ? e[t] = s.value : W(s));
      }
    }
}
function Un(e, n = {}, t = {}, s = "object", r = !1) {
  if (typeof e == "string") {
    const o = e;
    let i;
    if (o in t)
      i = t[o];
    else if (o in p)
      i = p[o];
    else if (i = n[o], i == null)
      throw new l(`Unknown ${s}: ${e}. This may be due to one of the following reasons:
1. The ${s} is defined in Python, in which case it needs to be ported to TensorFlow.js or your JavaScript code.
2. The custom ${s} is defined in JavaScript, but is not registered properly with tf.serialization.registerClass().`);
    return i;
  } else {
    const o = e;
    if (o.className == null || o.config == null)
      throw new l(`${s}: Improper config format: ${JSON.stringify(o)}.
'className' and 'config' must set.`);
    const i = o.className;
    let f, a;
    if (i in t ? [f, a] = t[i] : i in p ? [f, a] = p.className : i in n && ([f, a] = n[i]), f == null)
      throw new l(`Unknown ${s}: ${i}. This may be due to one of the following reasons:
1. The ${s} is defined in Python, in which case it needs to be ported to TensorFlow.js or your JavaScript code.
2. The custom ${s} is defined in JavaScript, but is not registered properly with tf.serialization.registerClass().`);
    if (a != null) {
      const u = {};
      for (const h of Object.keys(p))
        u[h] = p[h];
      for (const h of Object.keys(t))
        u[h] = t[h];
      const m = o.config;
      m.customObjects = u;
      const d = Object.assign({}, p);
      for (const h of Object.keys(t))
        p[h] = t[h];
      W(o.config);
      const T = a(f, o.config, t, r);
      return p = Object.assign({}, d), T;
    } else {
      const u = Object.assign({}, p);
      for (const d of Object.keys(t))
        p[d] = t[d];
      const m = new f(o.config);
      return p = Object.assign({}, u), m;
    }
  }
}
function rn(e, n) {
  return e < n ? -1 : e > n ? 1 : 0;
}
function xn(e, n) {
  return -1 * rn(e, n);
}
function Bn(e) {
  if (e == null)
    return e;
  const n = [];
  for (const t of e)
    n.indexOf(t) === -1 && n.push(t);
  return n;
}
function Gn(e) {
  if (e == null)
    throw new l(`Invalid value in obj: ${JSON.stringify(e)}`);
  for (const n in e)
    if (e.hasOwnProperty(n))
      return !1;
  return !0;
}
function x(e, n, t) {
  if (t != null && e.indexOf(t) < 0)
    throw new l(`${t} is not a valid ${n}.  Valid values are ${e} or null/undefined.`);
}
function Jn(e, n, t = 0, s = 1 / 0) {
  return me(t >= 0), me(s >= t), Array.isArray(e) && e.length >= t && e.length <= s && e.every((r) => typeof r === n);
}
function on(e, n) {
  Array.isArray(e) ? (A(e.length > 0, () => `${n} is unexpectedly an empty array.`), e.forEach((t, s) => on(t, `element ${s + 1} of ${n}`))) : A(Number.isInteger(e) && e > 0, () => `Expected ${n} to be a positive integer, but got ${Oe(e)}.`);
}
function Oe(e) {
  return e === null ? "null" : Array.isArray(e) ? "[" + e.map((n) => Oe(n)).join(",") + "]" : typeof e == "string" ? `"${e}"` : `${e}`;
}
function qn(e, n, t) {
  let s = t != null ? t() : pe(), r;
  return (...i) => {
    const f = t != null ? t() : pe();
    return f - s < n || (s = f, r = e(...i)), r;
  };
}
function Kn(e) {
  return e === "relu" ? "relu" : e === "linear" ? "linear" : e === "elu" ? "elu" : null;
}
const cn = ["channelsFirst", "channelsLast"], an = ["nearest", "bilinear"], un = ["valid", "same", "causal"], ln = ["max", "avg"], Rn = ["sum", "mul", "concat", "ave"];
const N = /* @__PURE__ */ new Map();
function fn(e) {
  x(cn, "DataFormat", e);
}
function Zn(e) {
  x(an, "InterpolationFormat", e);
}
function Wn(e) {
  x(un, "PaddingMode", e);
}
function Yn(e) {
  x(ln, "PoolMode", e);
}
const C = [], ge = "/";
function Hn(e, n) {
  C.push(e);
  try {
    const t = n();
    return C.pop(), t;
  } catch (t) {
    throw C.pop(), t;
  }
}
function hn() {
  return C.length === 0 ? "" : C.join(ge) + ge;
}
function Qn(e) {
  if (!De(e))
    throw new Error("Not a valid tensor name: '" + e + "'");
  return hn() + e;
}
function Xn(e) {
  if (!De(e))
    throw new Error("Not a valid tensor name: '" + e + "'");
  N.has(e) || N.set(e, 0);
  const n = N.get(e);
  if (N.set(e, N.get(e) + 1), n > 0) {
    const t = `${e}_${n}`;
    return N.set(t, 1), t;
  } else
    return e;
}
const pn = new RegExp(/^[A-Za-z0-9][-A-Za-z0-9\._\/]*$/);
function De(e) {
  return !!e.match(pn);
}
function zn(e) {
  return e === parseInt(e.toString(), 10);
}
function _e(e, n, t) {
  n == null && (n = 0), t == null && (t = e.length);
  let s = 1;
  for (let r = n; r < t; ++r)
    s *= e[r];
  return s;
}
function et(e) {
  if (e.length === 0)
    return Number.NaN;
  let n = Number.POSITIVE_INFINITY;
  for (let t = 0; t < e.length; t++) {
    const s = e[t];
    s < n && (n = s);
  }
  return n;
}
function nt(e) {
  if (e.length === 0)
    return Number.NaN;
  let n = Number.NEGATIVE_INFINITY;
  for (let t = 0; t < e.length; t++) {
    const s = e[t];
    s > n && (n = s);
  }
  return n;
}
function tt(e, n) {
  if (n < e)
    throw new l(`end (${n}) < begin (${e}) is forbidden.`);
  const t = [];
  for (let s = e; s < n; ++s)
    t.push(s);
  return t;
}
let K;
function st() {
  return K == null && (K = Ie().epsilon()), K;
}
function Y() {
  return "channelsLast";
}
function rt(e, n) {
  return $e(e, n);
}
function dn(e, n = -1) {
  const t = e.shape.slice();
  return n < 0 && (n = t.length + n + 1), t.splice(n, 0, 1), c(e, t);
}
function ot(e, n) {
  return S(() => {
    if (e.shape.length !== 2)
      throw new l(`repeat() expects a rank-2 tensor, but received a rank-${e.shape.length} tensor.`);
    const t = dn(e, 1);
    return mn(t, [1, n, 1]);
  });
}
function it(e) {
  const n = [_e(e.shape)];
  return c(e, n);
}
function ct(e) {
  if (e.rank <= 1)
    throw new l(`batchFlatten requires a minimum rank of 2. Got rank: ${e.rank}.`);
  const n = [e.shape[0], _e(e.shape, 1)];
  return c(e, n);
}
function R(e, n, t) {
  return S(() => {
    switch (e.rank) {
      case 1:
        return X(e, n, t);
      case 2:
        return we(e, [n, 0], [t, e.shape[1]]);
      case 3:
        return z(e, [n, 0, 0], [t, e.shape[1], e.shape[2]]);
      case 4:
        return U(e, [n, 0, 0, 0], [t, e.shape[1], e.shape[2], e.shape[3]]);
      case 5:
        return P(e, [n, 0, 0, 0, 0], [
          t,
          e.shape[1],
          e.shape[2],
          e.shape[3],
          e.shape[4]
        ]);
      case 6:
        return P(e, [n, 0, 0, 0, 0, 0], [
          t,
          e.shape[1],
          e.shape[2],
          e.shape[3],
          e.shape[4],
          e.shape[5]
        ]);
      default:
        throw new l(`sliceAlongFirstAxis() received an unsupported tensor rank: ${e.rank}`);
    }
  });
}
function Z(e, n, t) {
  return S(() => {
    switch (e.rank) {
      case 1:
        return X(e, n, t);
      case 2:
        return we(e, [0, n], [e.shape[0], t]);
      case 3:
        return z(e, [0, 0, n], [e.shape[0], e.shape[1], t]);
      case 4:
        return U(e, [0, 0, 0, n], [e.shape[0], e.shape[1], e.shape[2], t]);
      default:
        throw new l(`sliceAlongLastAxis() received an unsupported tensor rank: ${e.rank}`);
    }
  });
}
function at(e, n, t, s) {
  return S(() => {
    switch (e.rank) {
      case 1:
        return X(e, n, t);
      case 2:
        switch (s) {
          case 1:
            return R(e, n, t);
          case 2:
            return Z(e, n, t);
          default:
            throw new l(`The axis is not within the rank of the tensor ${s}`);
        }
      case 3:
        switch (s) {
          case 1:
            return R(e, n, t);
          case 2:
            return z(e, [0, n, 0], [e.shape[0], t, e.shape[2]]);
          case 3:
            return Z(e, n, t);
          default:
            throw new l(`The axis is not within the rank of the tensor ${s}`);
        }
      case 4:
        switch (s) {
          case 1:
            return R(e, n, t);
          case 2:
            return U(e, [0, n, 0, 0], [e.shape[0], t, e.shape[2], e.shape[3]]);
          case 3:
            return U(e, [0, 0, n, 0], [e.shape[0], e.shape[1], t, e.shape[3]]);
          case 4:
            return Z(e, n, t);
          default:
            throw new l(`The axis is not within the rank of the tensor ${s}`);
        }
      default:
        throw new l(`sliceAlongLastAxis() received an unsupported tensor rank: ${e.rank}`);
    }
  });
}
function ut(e, n = -1) {
  let t;
  return n < 0 && (t = e[0].rank, t !== 0 ? n = t : n = 0), n === e[0].rank && (n = -1), j(e, n);
}
function lt(e, n) {
  switch (e.rank) {
    case 1:
      return qe([e, n]);
    case 2:
      return Re([e, n], 0);
    case 3:
      return We([e, n], 0);
    case 4:
      return He([e, n], 0);
    default:
      throw new l(`concatAlongFirstAxis() received an unsupported tensor rank: ${e.rank}`);
  }
}
function mn(e, n) {
  if (Array.isArray(n) || (n = [n]), e.rank !== n.length)
    throw new l(`The length of input n (${n.length}) does not match the number of dimensions in input x (${e.rank})`);
  return Ce(e, n);
}
function ft(e, n = 0, t = 1, s, r) {
  return Le(e, n, t, s, r);
}
function ht(e, n, t, s) {
  if (e.rank < 2 || n.rank < 2)
    throw new F(`dot requires both inputs to be rank >= 2 but got x shape = ${e.shape} and y shape = ${n.shape}`);
  if (n.rank >= 3) {
    const r = e.shape.slice(-1)[0], o = n.shape.slice(-2)[0];
    if (r !== o)
      throw new F(`If rank y >= 3, then the second last dim of y must equal the last dim of x but got x shape = ${e.shape} and  y shape = ${n.shape}`);
  }
  if (e.rank === 2 && n.rank === 2)
    return de({
      a: e,
      b: n,
      transposeA: !1,
      transposeB: !1,
      bias: s ? H(e.rank, s, Y()) : null,
      activation: t
    });
  {
    const r = e.shape.slice(), o = r.pop();
    e = c(e, [-1, o]);
    const i = n.shape.slice(), f = i.pop(), a = i.pop(), u = [...i, f], m = Array.from({ length: n.rank }, (ne, I) => I === 0 ? n.rank - 2 : I <= n.rank - 2 ? I - 1 : I);
    n = c(Ue(n, m), [a, -1]);
    const d = [...r, ...u];
    return c(de({
      a: e,
      b: n,
      transposeA: !1,
      transposeB: !1,
      bias: s ? H(e.rank, s, Y()) : null,
      activation: t
    }), d);
  }
}
function pt(e, n, t) {
  return S(() => (Array.isArray(n) ? n = be(n, "int32") : n = $e(n, "int32"), Ne(e, n, t)));
}
function dt(e) {
  return Q(e, e);
}
function H(e, n, t) {
  const s = n.shape;
  if (n.rank !== 1 && n.rank !== e)
    throw new l(`Unexpected bias dimensions: ${n.rank}; expected it to be 1 or ${e}`);
  if (e === 5) {
    if (t === "channelsFirst")
      return s.length === 1 ? c(n, [1, s[0], 1, 1, 1]) : c(n, [1, s[3], s[0], s[1], s[2]]);
    if (t === "channelsLast")
      return s.length === 1 ? c(n, [1, 1, 1, 1, s[0]]) : c(n, [1].concat(s));
  } else if (e === 4) {
    if (t === "channelsFirst")
      return s.length === 1 ? c(n, [1, s[0], 1, 1]) : c(n, [1, s[2], s[0], s[1]]);
    if (t === "channelsLast")
      return s.length === 1 ? c(n, [1, 1, 1, s[0]]) : c(n, [1].concat(s));
  } else if (e === 3) {
    if (t === "channelsFirst")
      return s.length === 1 ? c(n, [1, s[0], 1]) : c(n, [1, s[1], s[0]]);
    if (t === "channelsLast")
      return s.length === 1 ? c(n, [1, 1, s[0]]) : c(n, [1].concat(s));
  } else if (e < 3)
    return n;
  throw new l(`Unsupported input rank by biasAdd: ${n.rank}`);
}
function mt(e, n, t) {
  return S(() => (t == null && (t = Y()), fn(t), M(e, H(e.rank, n, t))));
}
function gt(e, n = 1) {
  if (n !== 1)
    throw new F(`Support for alpha values other than 1 (${n}) is not implemented yet.`);
  return Pe(e);
}
function kt(e) {
  return S(() => ke(e, M(Ee(e), 1)));
}
function $t(e, n, t, s) {
  return S(() => tn(e, n, t, s));
}
function wt(e) {
  return S(() => {
    const n = M(0.5, Q(0.2, e));
    return ve(n, 0, 1);
  });
}
function At(e, n, t = !1) {
  return t ? e() : n();
}
export {
  ot as $,
  Ae as A,
  pt as B,
  tt as C,
  vn as D,
  gt as E,
  wt as F,
  kt as G,
  nt as H,
  zn as I,
  Jn as J,
  mt as K,
  at as L,
  Zn as M,
  F as N,
  on as O,
  Kn as P,
  Wn as Q,
  Se as R,
  Y as S,
  mn as T,
  et as U,
  l as V,
  lt as W,
  At as X,
  $t as Y,
  ut as Z,
  ct as _,
  fn as a,
  Yn as a0,
  Rn as a1,
  _e as b,
  x as c,
  ht as d,
  Un as e,
  Xn as f,
  Qn as g,
  Cn as h,
  Fn as i,
  st as j,
  qn as k,
  it as l,
  dt as m,
  Hn as n,
  me as o,
  rt as p,
  jn as q,
  ft as r,
  Vn as s,
  Mn as t,
  Bn as u,
  xn as v,
  Pn as w,
  Gn as x,
  R as y,
  dn as z
};
