import { c as y, e as h } from "./index-DSGwv2Yx.js";
import "./ops/webgl/matMul16.js";
import "./ops/cpu/matMul16.js";
import { isPackedTensor as v } from "./utilities/packed.js";
import { p as g } from "./pack16-WlOSOuZA.js";
import { d as k } from "./gelu-DpTCC3eB.js";
import { transpose16 as S } from "./ops/transpose16.js";
import { reshape16 as w } from "./ops/reshape16.js";
import { mul16 as D } from "./ops/mul16.js";
const G = {
  kernelName: "MatMul16",
  inputsToSave: ["A", "B"],
  outputsToSave: [],
  gradFunc: (r, o, l) => {
    const [s, t] = o;
    if (Array.isArray(r))
      throw new Error("Expected dy to be a single Tensor");
    let e = r;
    const { transposeA: u, transposeB: i, scale: a, activation: p, originalShape: d, perm: m } = l;
    if (m && d) {
      const f = new Array(m.length);
      for (let M = 0; M < m.length; ++M)
        f[m[M]] = M;
      const c = e;
      e = S(e, f), c.dispose();
    }
    if (d) {
      const f = e;
      e = w(e, d), f.dispose();
    }
    if (p === "gelu") {
      const f = e, c = n(s, t, u, i);
      e = k(f, c), f.dispose(), c.dispose();
    } else if (p === "relu2") {
      const f = e, c = n(s, t, u, i, { activation: "relu", scale: 2 });
      e = D(f, c), f.dispose(), c.dispose();
    }
    if (!u && !i)
      return {
        A: () => a !== void 0 ? B(e, t, a, !1, !0) : n(e, t, !1, !0),
        B: () => a !== void 0 ? A(s, e, a, !0, !1) : n(s, e, !0, !1)
      };
    if (!u && i)
      return {
        A: () => a !== void 0 ? B(e, t, a, !1, !1) : n(e, t, !1, !1),
        B: () => a !== void 0 ? A(s, e, a, !0, !1) : n(s, e, !0, !1)
      };
    if (u && !i)
      return {
        A: () => a !== void 0 ? A(t, e, a, !1, !0) : n(t, e, !1, !0),
        B: () => a !== void 0 ? A(s, e, a, !1, !1) : n(s, e, !1, !1)
      };
    throw new Error("Gradient for transposeA=true and transposeB=true is not supported yet.");
  }
};
y(G);
function n(r, o, l = !1, s = !1, t = {}) {
  const e = v(r), u = v(o), i = e || u, a = !i || e ? r : g(r), p = !i || u ? o : g(o), d = h().runKernel("MatMul16", { A: a, B: p }, { transposeA: l, transposeB: s, ...t });
  return i && !e && a.dispose(), i && !u && p.dispose(), d;
}
function j(r, o, l, s = !1, t = !1) {
  return n(r, o, s, t, { scale: l });
}
function B(r, o, l, s = !1, t = !1) {
  return n(r, o, s, t, { scaleA: l });
}
function A(r, o, l, s = !1, t = !1) {
  return n(r, o, s, t, { scaleB: l });
}
function q(r, o, l = !1, s = !1) {
  return n(r, o, l, s, { activation: "gelu" });
}
export {
  G as a,
  n as b,
  q as c,
  B as d,
  A as e,
  j as m
};
