import { a as I } from "../../index-DSGwv2Yx.js";
import { r as y } from "../../range-BDxO73mk.js";
import { g as F } from "../../gather-DEyjXNb1.js";
import { s as E } from "../../stack-z6QE7kmP.js";
import { c as T } from "../../concat-Dqk7Xk7h.js";
function U(c, r, p, e, n) {
  const t = e.shape[3], s = p;
  if (s > t) return e;
  const o = e.shape[2], i = s / 2, a = r.slice([n, 0, 0], [o, i, 1]).reshape([1, 1, o, i]), d = c.slice([n, 0, 0], [o, i, 1]).reshape([1, 1, o, i]), l = e.shape[0], m = e.shape[1], h = y(0, s, 2, "int32"), g = y(1, s, 2, "int32"), D = ((k) => {
    const C = k.slice([0, 0, 0, 0], [l, m, o, s]), R = s < t ? k.slice([0, 0, 0, s], [l, m, o, t - s]) : null, u = F(C, h, 3), f = F(C, g, 3), v = u.mul(a), N = f.mul(d), S = v.sub(N), P = f.mul(a), b = u.mul(d), x = P.add(b);
    u.dispose(), f.dispose(), a.dispose(), d.dispose(), v.dispose(), N.dispose(), P.dispose(), b.dispose();
    const K = E([S, x], -1);
    S.dispose(), x.dispose();
    const w = K.reshape([l, m, o, s]);
    return K.dispose(), R ? T([w, R], 3) : w;
  })(e);
  return h.dispose(), g.dispose(), D;
}
function B(c) {
  const { x: r } = c.inputs, { pastLen: p, negSin: e, ropeCache: n } = c.attrs, t = r.shape[3], s = e ? n.getNegSin() : n.getSin(), o = n.getCos();
  return U(s, o, t, r, p);
}
const j = {
  kernelName: "Rope",
  backendName: "cpu",
  kernelFunc: B
};
I(j);
const z = {
  kernelName: "Rope",
  backendName: "tensorflow",
  kernelFunc: B
};
I(z);
export {
  U as applyRoPE,
  B as ropeCPU
};
