vcruz305 commited on
Commit
90ed159
·
1 Parent(s): 8b40aa7

Upload llama.cpp/patches/patch_llamacpp_v41.py with huggingface_hub

Browse files
llama.cpp/patches/patch_llamacpp_v41.py CHANGED
@@ -79,6 +79,19 @@ CONST_TENSOR_NAME_NEW = """ MODEL_TENSOR.ENGRAM_EMBD: "blk.{bid
79
  # The list below is derived from the 39 tensor families the converter actually wrote,
80
  # reverse mapped through TENSOR_NAMES, rather than copied from DEEPSEEK4 and trimmed.
81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  CONST_ARCH_ENUM_ANCHOR = " DEEPSEEK4 = auto()\n"
83
  CONST_ARCH_ENUM_NEW = """ DEEPSEEK4 = auto()
84
  DEEPSEEK41 = auto()
@@ -231,10 +244,14 @@ class DeepseekV41Model(DeepseekV4Model):
231
  super().set_gguf_parameters()
232
  hparams = self.hparams
233
  if (engram_ids := hparams.get("engram_layer_ids")) is not None:
234
- self.gguf_writer.add_uint32("deepseek4.engram.head_count", hparams["engram_n_heads"])
235
- self.gguf_writer.add_uint32("deepseek4.engram.key_length", hparams["engram_head_dim"])
236
- self.gguf_writer.add_uint32("deepseek4.engram.max_ngram_size", hparams["engram_max_ngram_size"])
237
- self.gguf_writer.add_array("deepseek4.engram.layer_ids", engram_ids)
 
 
 
 
238
 
239
 
240
  # rows per block when rewriting an engram table; 1M rows is about 1 GB of float32 scratch
@@ -378,6 +395,7 @@ def patch_constants(path: pathlib.Path, revert: bool, check: bool) -> int:
378
  anchors = (
379
  CONST_TENSOR_ENUM_ANCHOR,
380
  CONST_TENSOR_NAME_ANCHOR,
 
381
  CONST_ARCH_ENUM_ANCHOR,
382
  CONST_ARCH_NAME_ANCHOR,
383
  CONST_ARCH_TENSORS_ANCHOR,
@@ -395,6 +413,8 @@ def patch_constants(path: pathlib.Path, revert: bool, check: bool) -> int:
395
  if "ENGRAM_EMBD" not in text:
396
  text = text.replace(CONST_TENSOR_ENUM_ANCHOR, CONST_TENSOR_ENUM_NEW + CONST_TENSOR_ENUM_ANCHOR, 1)
397
  text = text.replace(CONST_TENSOR_NAME_ANCHOR, CONST_TENSOR_NAME_NEW + CONST_TENSOR_NAME_ANCHOR, 1)
 
 
398
  text = text.replace(CONST_ARCH_ENUM_ANCHOR, CONST_ARCH_ENUM_NEW, 1)
399
  text = text.replace(CONST_ARCH_NAME_ANCHOR, CONST_ARCH_NAME_NEW, 1)
400
  text = text.replace(CONST_ARCH_TENSORS_ANCHOR, CONST_ARCH_TENSORS_NEW, 1)
 
79
  # The list below is derived from the 39 tensor families the converter actually wrote,
80
  # reverse mapped through TENSOR_NAMES, rather than copied from DEEPSEEK4 and trimmed.
81
 
82
+ # The four engram KV keys, declared the way every other arch-scoped key is: with an {arch}
83
+ # placeholder. PerLayerEmbedding directly below is the closest existing analogue, and llama.cpp's
84
+ # own lazy-read comment pairs PLE and engrams for the same reason.
85
+ CONST_KV_ANCHOR = " class PerLayerEmbedding:\n"
86
+ CONST_KV_NEW = ''' class Engram:
87
+ LAYER_IDS = "{arch}.engram.layer_ids"
88
+ HEAD_COUNT = "{arch}.engram.head_count"
89
+ KEY_LENGTH = "{arch}.engram.key_length"
90
+ MAX_NGRAM_SIZE = "{arch}.engram.max_ngram_size"
91
+
92
+ class PerLayerEmbedding:
93
+ '''
94
+
95
  CONST_ARCH_ENUM_ANCHOR = " DEEPSEEK4 = auto()\n"
96
  CONST_ARCH_ENUM_NEW = """ DEEPSEEK4 = auto()
97
  DEEPSEEK41 = auto()
 
244
  super().set_gguf_parameters()
245
  hparams = self.hparams
246
  if (engram_ids := hparams.get("engram_layer_ids")) is not None:
247
+ # These MUST carry the arch prefix, not a literal "deepseek4.". llama.cpp resolves
248
+ # every LLM_KV_* as "{arch}.{key}", so a hardcoded prefix means the runtime looks up
249
+ # deepseek41.engram.head_count and finds nothing, on a file that otherwise loads.
250
+ arch = self.gguf_writer.arch
251
+ self.gguf_writer.add_uint32(gguf.Keys.Engram.HEAD_COUNT.format(arch=arch), hparams["engram_n_heads"])
252
+ self.gguf_writer.add_uint32(gguf.Keys.Engram.KEY_LENGTH.format(arch=arch), hparams["engram_head_dim"])
253
+ self.gguf_writer.add_uint32(gguf.Keys.Engram.MAX_NGRAM_SIZE.format(arch=arch), hparams["engram_max_ngram_size"])
254
+ self.gguf_writer.add_array(gguf.Keys.Engram.LAYER_IDS.format(arch=arch), engram_ids)
255
 
256
 
257
  # rows per block when rewriting an engram table; 1M rows is about 1 GB of float32 scratch
 
395
  anchors = (
396
  CONST_TENSOR_ENUM_ANCHOR,
397
  CONST_TENSOR_NAME_ANCHOR,
398
+ CONST_KV_ANCHOR,
399
  CONST_ARCH_ENUM_ANCHOR,
400
  CONST_ARCH_NAME_ANCHOR,
401
  CONST_ARCH_TENSORS_ANCHOR,
 
413
  if "ENGRAM_EMBD" not in text:
414
  text = text.replace(CONST_TENSOR_ENUM_ANCHOR, CONST_TENSOR_ENUM_NEW + CONST_TENSOR_ENUM_ANCHOR, 1)
415
  text = text.replace(CONST_TENSOR_NAME_ANCHOR, CONST_TENSOR_NAME_NEW + CONST_TENSOR_NAME_ANCHOR, 1)
416
+ if "class Engram:" not in text:
417
+ text = text.replace(CONST_KV_ANCHOR, CONST_KV_NEW, 1)
418
  text = text.replace(CONST_ARCH_ENUM_ANCHOR, CONST_ARCH_ENUM_NEW, 1)
419
  text = text.replace(CONST_ARCH_NAME_ANCHOR, CONST_ARCH_NAME_NEW, 1)
420
  text = text.replace(CONST_ARCH_TENSORS_ANCHOR, CONST_ARCH_TENSORS_NEW, 1)