import { a as p } from "../../index-DSGwv2Yx.js";
import { createReduceInfo as m, reduce as l, ReduceProgram as i } from "./utils/reductions.js";
import { isPackedTensor as n } from "../../utilities/packed.js";
import k from "./utils/deviceInfo.js";
class x extends i {
  constructor(e, t) {
    super(e, t, { reductionOp: "sum", elementwise: !0 }, !0), this.shaderKey = "SoftmaxGrad16", this.variableNames = ["dy", "softmaxOutput"], this.variableComponents = [1, 1];
  }
  getReadSnippet() {
    return `
            let d: vec2<f32> = unpack2x16float(u32(dy[index]));
            let l: vec2<f32> = unpack2x16float(u32(softmaxOutput[index]));
            return d * l;
        `;
  }
  getWriteSnippet() {
    return `
            let d: vec2<f32> = unpack2x16float(u32(dy[offset + k]));
            let l: vec2<f32> = unpack2x16float(u32(softmaxOutput[offset + k]));
            let outVal = l * (d - bestValue);
            result[offset + k] = i32(pack2x16float(outVal));
        `;
  }
}
function b(o) {
  const { dy: e, softmaxOutput: t } = o.inputs, a = o.backend, s = k(a), u = n(e), c = n(t);
  if (!(u && c))
    throw new Error("softmaxGradGPU: dy and softmaxOutput must be packed tensors");
  const r = [e, t], f = m(r, -1), d = new x(s, f);
  return l(d, r, a);
}
const v = {
  kernelName: "Softmax16Grad",
  backendName: "webgpu",
  kernelFunc: b
};
p(v);
