# own tokenizer logic, not the Qwen default).
from . import get_model_class
with open(self.target_model_dir / "config.json", "r", encoding="utf-8") as f:
- target_arch = json.load(f)["architectures"][0]
+ target_hparams = json.load(f)
+ target_arch = target_hparams["architectures"][0]
target_cls = get_model_class(target_arch)
if target_cls is not type(self):
+ if target_arch == "NemotronHForCausalLM":
+ setattr(self, "is_moe", "num_experts_per_tok" in target_hparams)
target_cls.set_vocab(self) # ty: ignore[unresolved-attribute]
else:
super().set_vocab()
name = "model." + name
return super().filter_tensors((name, gen))
+ def modify_tensors(self, data_torch: Tensor, name: str, bid: int | None) -> Iterable[tuple[str, Tensor]]:
+ if name == "model.embed_tokens.weight" and not self.hparams.get("has_embed_tokens", True):
+ return
+
+ yield from super().modify_tensors(data_torch, name, bid)
+
@ModelBase.register("Qwen3DSparkModel")
class DSparkModel(DFlashModel):
const int64_t n_embd_inp = hparams.n_embd_inp_enc();
+ tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, TENSOR_NOT_REQUIRED);
// DSpark = DFlash + a semi-autoregressive Markov head and Confidence head
//
// TODO: only Qwen3-style backbones are supported for now; other backbones (e.g. Gemma4)
}
fc = create_tensor(tn(LLM_TENSOR_FC, "weight"), { n_embd_inp, n_embd }, 0);
+ fc_s = create_tensor(tn(LLM_TENSOR_FC, "scale"), { 1 }, TENSOR_NOT_REQUIRED);
output_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_OUTPUT_NORM, "weight"), { n_embd }, 0); // encoder hidden_norm (after fc)
output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0); // decoder final norm
llama_model_dflash::graph<true>::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
ggml_tensor * cur = build_inp_embd_enc();
- cur = build_lora_mm(model.fc, cur);
+ cur = build_lora_mm(model.fc, cur, model.fc_s);
cb(cur, "fc_out", -1);
cur = build_norm(cur, model.output_norm_enc, NULL, LLM_NORM_RMS, -1);
cb(cur, "ffn_norm", il);
cur = build_ffn(cur,
- layer.ffn_up, NULL, NULL,
- layer.ffn_gate, NULL, NULL,
- layer.ffn_down, NULL, NULL,
+ layer.ffn_up, NULL, layer.ffn_up_s,
+ layer.ffn_gate, NULL, layer.ffn_gate_s,
+ layer.ffn_down, NULL, layer.ffn_down_s,
NULL,
LLM_FFN_SILU, LLM_FFN_PAR, il);
cb(cur, "ffn_out", il);
res->t_embd = cur;
// lm_head from the target model (shared via ctx_other)
- auto * output = model.output;
+ auto * output = model.output;
+ auto * output_s = model.output_s;
if (output == nullptr) {
GGML_ASSERT(cparams.ctx_other != nullptr);
const auto * model_other = llama_get_model(cparams.ctx_other);
GGML_ASSERT(model_other->output != nullptr && "DFlash decoder requires the target model's output projection");
- output = model_other->output;
+ output = model_other->output;
+ output_s = model_other->output_s;
}
- cur = build_lora_mm(output, cur);
+ cur = build_lora_mm(output, cur, output_s);
cb(cur, "result_output", -1);
res->t_logits = cur;
cb(cur, "result_norm", -1);
// lm_head from the target model (shared via ctx_other)
- auto * output = model.output;
+ auto * output = model.output;
+ auto * output_s = model.output_s;
if (output == nullptr) {
GGML_ASSERT(cparams.ctx_other != nullptr);
const auto * model_other = llama_get_model(cparams.ctx_other);
GGML_ASSERT(model_other->output != nullptr && "DSpark decoder requires the target model's output projection");
- output = model_other->output;
+ output = model_other->output;
+ output_s = model_other->output_s;
}
- cur = build_lora_mm(output, cur);
+ cur = build_lora_mm(output, cur, output_s);
cb(cur, "result_output", -1);
res->t_logits = cur;
auto * inp = build_inp_mem_hybrid();
ggml_tensor * inp_out_ids = build_inp_out_ids();
+ const bool extract_final_inp = (size_t) n_layer < cparams.embeddings_layer_inp.size() && cparams.embeddings_layer_inp[n_layer];
for (int il = 0; il < n_layer; ++il) {
+ res->t_layer_inp[il] = inpL;
+
struct ggml_tensor * inpSA = inpL;
// norm
cur = build_ffn_layer(cur, model, il);
}
- if (il == n_layer - 1 && inp_out_ids && cparams.embeddings_nextn_masked) {
+ if (il == n_layer - 1 && inp_out_ids && cparams.embeddings_nextn_masked && !extract_final_inp) {
cur = ggml_get_rows(ctx0, cur, inp_out_ids);
inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
}
}
cur = inpL;
+ if (extract_final_inp) {
+ res->t_layer_inp[n_layer] = cur;
+
+ if (inp_out_ids && cparams.embeddings_nextn_masked) {
+ cur = ggml_get_rows(ctx0, cur, inp_out_ids);
+ }
+ }
cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);