import i from "../layers/BaseLayer.js";
import "../index-DSGwv2Yx.js";
import "../random_width-B_fVXhGx.js";
import "../zeros_like-rOHr54NY.js";
import "../Generator.js";
import "../index-Cp39cXWe.js";
import "../dataset-DlqAN81i.js";
import "../ops/cpu/attentionMask.js";
import "../ops/webgl/attentionMask.js";
import "../ops/grads/attentionMask.js";
import "../ops/cpu/rope.js";
import "../ops/webgl/rope.js";
import "../rope-CC5RjmKU.js";
import "../ops/cpu/appendCache.js";
import "../ops/webgl/appendCache.js";
import "../ops/grads/softmax16.js";
import "../matMul16-BIT70Vya.js";
import "../ops/webgl/matMul16.js";
import "../ops/cpu/matMul16.js";
import "../pack16-WlOSOuZA.js";
import "../ops/transpose16.js";
import "../ops/reshape16.js";
import "../ops/cpu/qkv.js";
import "../ops/webgl/qkv.js";
import "../ops/grads/qkv.js";
import "../ops/cpu/normRMS.js";
import "../ops/webgl/normRMS.js";
import "../ops/grads/normRMS.js";
import "../ops/dropout16.js";
import "../ops/webgl/dropout16.js";
import "../ops/grads/add16.js";
import "../jszip.min-BZhlzntC.js";
import "../index-DvYrXKkX.js";
import "../ops/cpu/adamAdjust.js";
import "../ops/webgl/adamAdjust.js";
import "../ops/cpu/adamMoments.js";
import "../ops/webgl/adamMoments.js";
import "../ops/cpu/gatherSub.js";
import "../ops/webgl/gatherSub.js";
import "../ops/cpu/scatterSub.js";
import "../ops/webgl/scatterSub.js";
import "../papaparse.min-C0cScC2i.js";
import { estimateParameterCount as a } from "../utilities/parameters.js";
import "../ops/cpu/matMulGelu.js";
import "../matMulGelu-CsZnh18H.js";
import "../ops/grads/matMulGelu.js";
import "../ops/cpu/gelu.js";
import "../ops/webgl/gelu.js";
import "../gelu-DpTCC3eB.js";
import "../ops/webgl/log.js";
import "../checks/normRMS.js";
import "../checks/normRMSGrad.js";
import e from "../layers/LoRA.js";
class st extends i {
  lossScaling = 128;
  trainingState = null;
  metaData = { version: 2, application: "@genai-fi/nanogpt" };
  loraLayer;
  loraMap = /* @__PURE__ */ new Map();
  constructor(t) {
    super(t), t.loraConfig && t.loraConfig.forEach((r, o) => {
      this.createLoRA(o, r);
    });
  }
  createLoRA(t, r) {
    if (this.loraMap.has(t))
      return;
    const o = new e(t, this.weightStore, r.alpha, r.rank, r.variables);
    this.loraMap.set(t, o), this.config.loraConfig = this.config.loraConfig || /* @__PURE__ */ new Map(), this.config.loraConfig.set(t, r);
  }
  deleteLoRA(t) {
    const r = this.loraMap.get(t);
    if (!r)
      throw new Error(`No LoRA with name ${t} exists.`);
    this.loraLayer === r && this.detachLoRA(), r.dispose(), this.loraMap.delete(t), this.config.loraConfig && this.config.loraConfig.delete(t);
  }
  renameLoRA(t, r) {
    if (!this.loraMap.has(t))
      throw new Error(`No LoRA with name ${t} exists.`);
    if (this.loraMap.has(r))
      throw new Error(`LoRA with name ${r} already exists.`);
    const o = this.loraMap.get(t);
    this.loraMap.set(r, o), this.loraMap.delete(t), this.config.loraConfig && (this.config.loraConfig.delete(t), this.config.loraConfig.set(r, {
      rank: o.rank,
      alpha: o.alpha,
      variables: Array.from(o.variables)
    }));
  }
  mergeLoRA(t) {
    const r = this.loraMap.get(t);
    if (!r)
      throw new Error(`No LoRA with name ${t} exists.`);
    r.merge(), this.deleteLoRA(t);
  }
  attachLoRA(t) {
    if (this.loraLayer) {
      if (this.loraLayer.name === t)
        return;
      this.detachLoRA();
    }
    const r = this.loraMap.get(t);
    if (!r)
      throw new Error(`No LoRA with name ${t} exists.`);
    r.attach(), this.loraLayer = r, this.config.loraName = t;
  }
  detachLoRA() {
    if (!this.loraLayer)
      throw new Error("No LoRA layer is attached to this model.");
    this.loraLayer.detach(), this.loraLayer = void 0, this.config.loraName = void 0;
  }
  hasLoRA(t) {
    return t ? this.loraMap.has(t) : !!this.loraLayer;
  }
  listLoRAs() {
    return Array.from(this.loraMap.keys());
  }
  get lora() {
    return this.loraLayer || null;
  }
  getNumParams() {
    return a(this.config);
  }
  validateInput(t) {
    if (t.shape.length !== 2)
      throw new Error(`Invalid input shape: expected [batch_size, sequence_length], got ${t.shape}`);
    if (t.shape[1] > this.config.blockSize)
      throw new Error(`Input sequence length ${t.shape[1]} isn't block size ${this.config.blockSize}`);
    if (t.dtype !== "int32")
      throw new Error(`Input tensor must be of type int32, got ${t.dtype}`);
  }
}
export {
  st as default
};
