import { o as h, n as c, E as d, bm as T, bn as q, bo as H, v as l, bp as P, J as _, bq as y, br as I, bs as W, bt as B, bu as A, bv as G, bw as L, bx as O, by as z, bz as F, z as M, _ as j, bA as J, bB as U, bC as V, a2 as Q, a1 as N, m as X, bD as Y, bE as Z, bF as R, bG as nn, bH as tn, bI as sn, bJ as en, bK as rn, bL as on, bM as an, bN as un, aE as cn, bO as ln } from "./index-DSGwv2Yx.js";
import { k as C, c as g, m as D } from "./step-DQY6_ABw.js";
import { r as b } from "./reshape-BIN71H3p.js";
import { m as pn, b as hn, e as w } from "./not_equal-CkQKkKZy.js";
import { s as K } from "./sum-D39FeU5h.js";
function fn(s, n = null, t = !1) {
  const i = { x: c(s, "x", "all", "bool") }, o = { axis: n, keepDims: t };
  return d.runKernel(T, i, o);
}
const nt = /* @__PURE__ */ h({ all_: fn });
function dn(s, n = null, t = !1) {
  const i = { x: c(s, "x", "any", "bool") }, o = { axis: n, keepDims: t };
  return d.runKernel(q, i, o);
}
const tt = /* @__PURE__ */ h({ any_: dn });
function mn(s, n = 0) {
  const e = { x: c(s, "x", "argMax") }, i = { axis: n };
  return d.runKernel(H, e, i);
}
const st = /* @__PURE__ */ h({ argMax_: mn });
function $n(s, n, t, e, i) {
  const o = c(s, "x", "avgPool", "float32"), p = 1;
  l(C(t, p), () => `Error in avgPool: Either strides or dilations must be 1. Got strides ${t} and dilations '${p}'`);
  let r = o, a = !1;
  o.rank === 3 && (a = !0, r = b(o, [1, o.shape[0], o.shape[1], o.shape[2]])), l(r.rank === 4, () => `Error in avgPool: x must be rank 4 but got rank ${r.rank}.`), g("avgPool", e, i);
  const u = { x: r }, m = { filterSize: n, strides: t, pad: e, dimRoundingMode: i };
  let f = d.runKernel(P, u, m);
  return f = _(f, o.dtype), a ? b(f, [f.shape[1], f.shape[2], f.shape[3]]) : f;
}
const et = /* @__PURE__ */ h({ avgPool_: $n });
function bn(s) {
  const t = { x: c(s, "x", "tanh", "float32") };
  return d.runKernel(y, t);
}
const rt = /* @__PURE__ */ h({ tanh_: bn });
function xn(s, n, t) {
  const e = c(s, "x", "batchToSpaceND"), i = n.reduce((r, a) => r * a);
  l(e.rank >= 1 + n.length, () => `input rank is ${e.rank} but should be > than blockShape.length ${n.length}`), l(t.length === n.length, () => `crops.length is ${t.length} but should be equal to blockShape.length  ${n.length}`), l(e.shape[0] % i === 0, () => `input tensor batch is ${e.shape[0]} but is not divisible by the product of the elements of blockShape ${n.join(" * ")} === ${i}`);
  const o = { x: e }, p = { blockShape: n, crops: t };
  return d.runKernel(I, o, p);
}
const ot = /* @__PURE__ */ h({ batchToSpaceND_: xn });
function kn(s) {
  let n;
  return s.rank === 0 || s.rank === 1 ? n = b(s, [1, 1, 1, s.size]) : s.rank === 2 ? n = b(s, [1, 1, s.shape[0], s.shape[1]]) : s.rank === 3 ? n = b(s, [1, s.shape[0], s.shape[1], s.shape[2]]) : n = s, n;
}
function vn(s, n, t, e, i, o) {
  o == null && (o = 1e-3);
  const p = c(s, "x", "batchNorm"), r = c(n, "mean", "batchNorm"), a = c(t, "variance", "batchNorm");
  let u;
  i != null && (u = c(i, "scale", "batchNorm"));
  let m;
  e != null && (m = c(e, "offset", "batchNorm")), l(r.rank === a.rank, () => "Batch normalization gradient requires mean and variance to have equal ranks."), l(m == null || r.rank === m.rank, () => "Batch normalization gradient requires mean and offset to have equal ranks."), l(u == null || r.rank === u.rank, () => "Batch normalization gradient requires mean and scale to have equal ranks.");
  const x = {
    x: kn(p),
    scale: u,
    offset: m,
    mean: r,
    variance: a
  }, k = { varianceEpsilon: o }, $ = d.runKernel(W, x, k);
  return b($, p.shape);
}
const at = /* @__PURE__ */ h({ batchNorm_: vn });
function gn(s, n, t, e, i = "NHWC", o = [1, 1], p) {
  const r = c(s, "x", "conv2d", "float32"), a = c(n, "filter", "conv2d", "float32");
  let u = r, m = !1;
  r.rank === 3 && (m = !0, u = b(r, [1, r.shape[0], r.shape[1], r.shape[2]])), l(u.rank === 4, () => `Error in conv2d: input must be rank 4, but got rank ${u.rank}.`), l(a.rank === 4, () => `Error in conv2d: filter must be rank 4, but got rank ${a.rank}.`), g("conv2d", e, p);
  const f = i === "NHWC" ? u.shape[3] : u.shape[1];
  l(f === a.shape[2], () => `Error in conv2d: depth of input (${f}) must match input depth for filter ${a.shape[2]}.`), l(C(t, o), () => `Error in conv2D: Either strides or dilations must be 1. Got strides ${t} and dilations '${o}'`), l(D(o), () => "Error in conv2D: Dilated rates should be larger than 0."), l(D(t), () => "Error in conv2D: Strides should be larger than 0.");
  const x = { x: u, filter: a }, k = { strides: t, pad: e, dataFormat: i, dilations: o, dimRoundingMode: p }, $ = d.runKernel(B, x, k);
  return m ? b($, [$.shape[1], $.shape[2], $.shape[3]]) : $;
}
const S = /* @__PURE__ */ h({ conv2d_: gn });
function Dn(s, n, t, e, i = "NWC", o = 1, p) {
  const r = c(s, "x", "conv1d"), a = c(n, "filter", "conv1d");
  let u = r, m = !1;
  r.rank === 2 && (m = !0, u = b(r, [1, r.shape[0], r.shape[1]])), l(u.rank === 3, () => `Error in conv1d: input must be rank 3, but got rank ${u.rank}.`), l(a.rank === 3, () => `Error in conv1d: filter must be rank 3, but got rank ${a.rank}.`), g("conv1d", e, p), l(u.shape[2] === a.shape[1], () => `Error in conv1d: depth of input (${u.shape[2]}) must match input depth for filter ${a.shape[1]}.`), l(C(t, o), () => `Error in conv1D: Either stride or dilation must be 1. Got stride ${t} and dilation '${o}'`), l(D(o), () => "Error in conv1D: Dilated rates should be larger than 0."), l(D(t), () => "Error in conv1D: Stride should be larger than 0."), l(i === "NWC", () => `Error in conv1d: got dataFormat of ${i} but only NWC is currently supported.`);
  const f = b(a, [1, a.shape[0], a.shape[1], a.shape[2]]), x = b(u, [u.shape[0], 1, u.shape[1], u.shape[2]]), v = S(x, f, [1, t], e, "NHWC", [1, o], p);
  return m ? b(v, [v.shape[2], v.shape[3]]) : b(v, [v.shape[0], v.shape[2], v.shape[3]]);
}
const it = /* @__PURE__ */ h({ conv1d_: Dn });
function Cn(s, n, t, e, i, o = "NHWC", p) {
  l(s.length === n.rank, () => `Length of inShape (${s.length}) and rank of dy (${n.rank}) must match`);
  let r = s, a = n, u = !1;
  n.rank === 3 && (u = !0, a = b(n, [1, n.shape[0], n.shape[1], n.shape[2]]), r = [1, s[0], s[1], s[2]]), l(r.length === 4, () => `Error in conv2dDerInput: inShape must be length 4, but got length ${r.length}.`), l(a.rank === 4, () => `Error in conv2dDerInput: dy must be rank 4, but got rank ${a.rank}`), l(t.rank === 4, () => `Error in conv2dDerInput: filter must be rank 4, but got rank ${t.rank}`);
  const m = o === "NHWC" ? r[3] : r[1], f = o === "NHWC" ? a.shape[3] : a.shape[1];
  l(m === t.shape[2], () => `Error in conv2dDerInput: depth of input (${m}) must match input depth for filter ${t.shape[2]}.`), l(f === t.shape[3], () => `Error in conv2dDerInput: depth of output (${f}) must match output depth for filter ${t.shape[3]}.`), g("conv2dDerInput", i, p);
  const x = { dy: a, filter: t }, k = { strides: e, pad: i, dataFormat: o, dimRoundingMode: p, inputShape: r }, $ = d.runKernel(A, x, k);
  return u ? b($, [$.shape[1], $.shape[2], $.shape[3]]) : $;
}
const En = /* @__PURE__ */ h({ conv2DBackpropInput_: Cn });
function Nn(s, n, t, e, i, o) {
  const p = c(s, "x", "conv2dTranspose"), r = c(n, "filter", "conv2dTranspose");
  return En(t, p, r, e, i, "NHWC", o);
}
const ut = /* @__PURE__ */ h({ conv2dTranspose_: Nn });
function _n(s) {
  const t = { x: c(s, "x", "cos", "float32") };
  return d.runKernel(G, t);
}
const ct = /* @__PURE__ */ h({ cos_: _n });
function wn(s) {
  const t = { x: c(s, "x", "cosh", "float32") };
  return d.runKernel(L, t);
}
const lt = /* @__PURE__ */ h({ cosh_: wn });
function Kn(s, n = 0, t = !1, e = !1) {
  const o = { x: c(s, "x", "cumprod") }, p = { axis: n, exclusive: t, reverse: e };
  return d.runKernel(O, o, p);
}
const pt = /* @__PURE__ */ h({ cumprod_: Kn });
function Sn(s, n = 0, t = !1, e = !1) {
  const o = { x: c(s, "x", "cumsum") }, p = { axis: n, exclusive: t, reverse: e };
  return d.runKernel(z, o, p);
}
const ht = /* @__PURE__ */ h({ cumsum_: Sn });
function Tn(s, n, t, e, i = "NHWC", o = [1, 1], p) {
  const r = c(s, "x", "depthwiseConv2d", "float32"), a = c(n, "filter", "depthwiseConv2d", "float32");
  let u = r, m = !1;
  r.rank === 3 && (m = !0, u = b(r, [1, r.shape[0], r.shape[1], r.shape[2]])), l(u.rank === 4, () => `Error in depthwiseConv2d: input must be rank 4, but got rank ${u.rank}.`), l(a.rank === 4, () => `Error in depthwiseConv2d: filter must be rank 4, but got rank ${a.rank}.`);
  const f = i === "NHWC" ? u.shape[3] : u.shape[1];
  l(f === a.shape[2], () => `Error in depthwiseConv2d: number of input channels (${f}) must match the inChannels dimension in filter ${a.shape[2]}.`), g("depthwiseConv2d", e, p);
  const x = { x: u, filter: a }, k = { strides: t, pad: e, dataFormat: i, dilations: o, dimRoundingMode: p }, $ = d.runKernel(F, x, k);
  return m ? b($, [$.shape[1], $.shape[2], $.shape[3]]) : $;
}
const qn = /* @__PURE__ */ h({ depthwiseConv2d_: Tn });
function Hn(s, n) {
  let t = c(s, "a", "equal", "string_or_numeric"), e = c(n, "b", "equal", "string_or_numeric");
  [t, e] = M(t, e), j(t.shape, e.shape);
  const i = { a: t, b: e };
  return d.runKernel(J, i);
}
const ft = /* @__PURE__ */ h({ equal_: Hn });
function Pn(s) {
  let n = c(s, "x", "erf");
  l(n.dtype === "int32" || n.dtype === "float32", () => "Input dtype must be `int32` or `float32`."), n.dtype === "int32" && (n = _(n, "float32"));
  const t = { x: n };
  return d.runKernel(U, t);
}
const dt = /* @__PURE__ */ h({ erf_: Pn });
function yn(s) {
  const t = { x: c(s, "x", "softplus") };
  return d.runKernel(V, t);
}
const mt = /* @__PURE__ */ h({ softplus_: yn });
function In(s, n = -1) {
  const t = c(s, "logits", "logSoftmax");
  if (n === -1 && (n = t.rank - 1), n !== t.rank - 1)
    throw Error(`Log Softmax along a non-last dimension is not yet supported. Logits was rank ${t.rank} and axis was ${n}`);
  return Q((i, o) => {
    const r = pn(i, n, !0), a = N(i, r), u = N(_(a, "float32"), hn(K(w(a), n, !0)));
    return o([u]), { value: u, gradFunc: (f, x) => {
      const [k] = x, $ = !0, E = w(k);
      return N(f, X(K(f, n, $), E));
    } };
  })(t);
}
const $t = /* @__PURE__ */ h({ logSoftmax_: In });
function Wn(s) {
  const t = { x: c(s, "x", "logicalNot", "bool") };
  return d.runKernel(Y, t);
}
const bt = /* @__PURE__ */ h({ logicalNot_: Wn });
function Bn(s, n, t, e, i) {
  const o = c(s, "x", "maxPool"), p = 1;
  let r = o, a = !1;
  o.rank === 3 && (a = !0, r = b(o, [1, o.shape[0], o.shape[1], o.shape[2]])), l(r.rank === 4, () => `Error in maxPool: input must be rank 4 but got rank ${r.rank}.`), l(C(t, p), () => `Error in maxPool: Either strides or dilations must be 1. Got strides ${t} and dilations '${p}'`), g("maxPool", e, i);
  const u = { x: r }, m = { filterSize: n, strides: t, pad: e, dimRoundingMode: i }, f = d.runKernel(Z, u, m);
  return a ? b(f, [f.shape[1], f.shape[2], f.shape[3]]) : f;
}
const xt = /* @__PURE__ */ h({ maxPool_: Bn });
function An(s, n, t = 1, e = 0, i = "int32") {
  if (n < 2)
    throw new Error(`Error in oneHot: depth must be >=2, but it is ${n}`);
  const p = { indices: c(s, "indices", "oneHot", "int32") }, r = { dtype: i, depth: n, onValue: t, offValue: e };
  return d.runKernel(R, p, r);
}
const kt = /* @__PURE__ */ h({ oneHot_: An });
function Gn(s) {
  const t = { x: c(s, "x", "onesLike") };
  return d.runKernel(nn, t);
}
const vt = /* @__PURE__ */ h({ onesLike_: Gn });
function Ln(s, n, t = 0) {
  const e = c(s, "x", "pad");
  if (e.rank === 0)
    throw new Error("pad(scalar) is not defined. Pass non-scalar to pad");
  const i = { paddings: n, constantValue: t }, o = { x: e };
  return d.runKernel(tn, o, i);
}
const gt = /* @__PURE__ */ h({ pad_: Ln });
function On(s, n, t) {
  const e = c(s, "x", "spaceToBatchND");
  l(e.rank >= 1 + n.length, () => `input rank ${e.rank} should be > than [blockShape] ${n.length}`), l(t.length === n.length, () => `paddings.shape[0] ${t.length} must be equal to [blockShape] ${n.length}`), l(e.shape.reduce((p, r, a) => a > 0 && a <= n.length ? p && (r + t[a - 1][0] + t[a - 1][1]) % n[a - 1] === 0 : p, !0), () => `input spatial dimensions ${e.shape.slice(1)} with paddings ${t.toString()} must be divisible by blockShapes ${n.toString()}`);
  const i = { x: e }, o = { blockShape: n, paddings: t };
  return d.runKernel(sn, i, o);
}
const Dt = /* @__PURE__ */ h({ spaceToBatchND_: On });
function zn(s, n) {
  const e = { x: c(s, "x", "reverse") }, i = { dims: n };
  return d.runKernel(en, e, i);
}
const Ct = /* @__PURE__ */ h({ reverse_: zn });
function Fn(s) {
  const t = { x: c(s, "x", "rsqrt", "float32") };
  return d.runKernel(rn, t);
}
const Et = /* @__PURE__ */ h({ rsqrt_: Fn });
function Mn(s) {
  const t = { x: c(s, "x", "selu") };
  return d.runKernel(on, t);
}
const Nt = /* @__PURE__ */ h({ selu_: Mn });
function jn(s, n, t, e, i, o = [1, 1], p = "NHWC") {
  const r = c(s, "x", "separableConv2d"), a = c(n, "depthwiseFilter", "separableConv2d"), u = c(t, "pointwiseFilter", "separableConv2d");
  let m = r, f = !1;
  if (r.rank === 3 && (f = !0, m = b(r, [1, r.shape[0], r.shape[1], r.shape[2]])), p === "NCHW")
    throw new Error("separableConv2d currently does not support dataFormat NCHW; only NHWC is supported");
  l(m.rank === 4, () => `Error in separableConv2d: input must be rank 4, but got rank ${m.rank}.`), l(a.rank === 4, () => `Error in separableConv2d: depthwise filter must be rank 4, but got rank ${a.rank}.`), l(u.rank === 4, () => `Error in separableConv2d: pointwise filter must be rank 4, but got rank ${a.rank}.`), l(u.shape[0] === 1, () => `Error in separableConv2d: the first dimension of pointwise filter  must be 1, but got ${u.shape[0]}.`), l(u.shape[1] === 1, () => `Error in separableConv2d: the second dimension of pointwise filter must be 1, but got ${u.shape[1]}.`);
  const x = a.shape[2], k = a.shape[3];
  l(u.shape[2] === x * k, () => `Error in separableConv2d: the third dimension of pointwise filter must be ${x * k}, but got ${u.shape[2]}.`);
  const $ = qn(m, a, e, i, p, o), v = S($, u, 1, "valid", p);
  return f ? b(v, [v.shape[1], v.shape[2], v.shape[3]]) : v;
}
const _t = /* @__PURE__ */ h({ separableConv2d_: jn });
function Jn(s) {
  const t = { x: c(s, "x", "sin", "float32") };
  return d.runKernel(an, t);
}
const wt = /* @__PURE__ */ h({ sin_: Jn });
function Un(s) {
  const t = { x: c(s, "x", "sinh") };
  return d.runKernel(un, t);
}
const Kt = /* @__PURE__ */ h({ sinh_: Un });
function Vn(s, n, t) {
  const e = c(s, "x", "unsortedSegmentSum"), i = c(n, "segmentIds", "unsortedSegmentSum", "int32");
  l(cn(t), () => "numSegments must be of dtype int");
  const o = { x: e, segmentIds: i }, p = { numSegments: t };
  return d.runKernel(ln, o, p);
}
const St = /* @__PURE__ */ h({ unsortedSegmentSum_: Vn });
export {
  Et as A,
  Nt as B,
  _t as C,
  Kt as D,
  rt as E,
  St as F,
  En as G,
  mt as a,
  Dt as b,
  ct as c,
  ot as d,
  ft as e,
  et as f,
  nt as g,
  tt as h,
  st as i,
  at as j,
  it as k,
  bt as l,
  xt as m,
  ut as n,
  S as o,
  lt as p,
  pt as q,
  Ct as r,
  wt as s,
  ht as t,
  qn as u,
  dt as v,
  $t as w,
  kt as x,
  vt as y,
  gt as z
};
