samcheng0 commited on
Commit
bb8bb58
Β·
verified Β·
1 Parent(s): 5b969d4

Upload infer_gguf.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. infer_gguf.py +19 -17
infer_gguf.py CHANGED
@@ -216,13 +216,6 @@ def apply_qlora_v2(model, target_modules=None, r=8, alpha=16, dropout=0.0):
216
  print(f" QLoRA applied: {qlora_params:,} LoRA params | trainable: {n:,}")
217
 
218
 
219
- # ─── V3 model (from model_tiny) ─────────────────────────────────────────
220
-
221
- def _load_v3_model():
222
- from scripts.model_tiny import TinyModel as TinyModelV3
223
- return TinyModelV3
224
-
225
-
226
  # ─── Tokenizer ──────────────────────────────────────────────────────────
227
 
228
  def load_tokenizer(ckpt_type=None):
@@ -252,17 +245,21 @@ def build_chat(system, user, tokenizer):
252
 
253
  def _detect_ckpt_type(state):
254
  keys = list(state.keys())
255
- if any("rpw" in k or "vcr" in k for k in keys):
256
- return "v3"
257
- if any("qweight" in k for k in keys):
 
 
 
 
 
 
 
 
 
258
  return "v2_qlora"
259
  if any("gate" in k for k in keys) or any("mlp.gate" in k for k in keys):
260
  return "v2_fp32"
261
- if any(k.startswith("blocks.") for k in keys) and any("ln1.weight" in k for k in keys):
262
- blk_keys = [k.split(".")[1] for k in keys if k.startswith("blocks.")]
263
- max_blk = max(int(b) for b in blk_keys) if blk_keys else 0
264
- if max_blk >= 3:
265
- return "v3"
266
  return "v2_fp32"
267
 
268
 
@@ -333,9 +330,14 @@ def load_checkpoint(ckpt_path):
333
  model.eval()
334
  return model, ckpt_type
335
 
336
- TV3 = _load_v3_model()
 
 
337
  model = TV3()
338
- model.load_state_dict(state)
 
 
 
339
  model.eval()
340
  return model, ckpt_type
341
 
 
216
  print(f" QLoRA applied: {qlora_params:,} LoRA params | trainable: {n:,}")
217
 
218
 
 
 
 
 
 
 
 
219
  # ─── Tokenizer ──────────────────────────────────────────────────────────
220
 
221
  def load_tokenizer(ckpt_type=None):
 
245
 
246
  def _detect_ckpt_type(state):
247
  keys = list(state.keys())
248
+ has_v3 = any("rpw" in k or "vcr" in k for k in keys)
249
+ has_v3_blocks = False
250
+ if any(k.startswith("blocks.") for k in keys):
251
+ blk_keys = [k.split(".")[1] for k in keys if k.startswith("blocks.")]
252
+ max_blk = max(int(b) for b in blk_keys) if blk_keys else 0
253
+ has_v3_blocks = max_blk >= 3
254
+ has_qlora = any("qweight" in k for k in keys)
255
+ if has_v3:
256
+ return "v3_qlora" if has_qlora else "v3"
257
+ if has_v3_blocks:
258
+ return "v3_qlora" if has_qlora else "v3"
259
+ if has_qlora:
260
  return "v2_qlora"
261
  if any("gate" in k for k in keys) or any("mlp.gate" in k for k in keys):
262
  return "v2_fp32"
 
 
 
 
 
263
  return "v2_fp32"
264
 
265
 
 
330
  model.eval()
331
  return model, ckpt_type
332
 
333
+ from scripts.model_tiny import TinyModel as TV3
334
+ from scripts.model_tiny import apply_qlora as apply_qlora_v3
335
+ is_qlora = "qlora" in ckpt_type
336
  model = TV3()
337
+ model.reset_weights()
338
+ if is_qlora:
339
+ model = apply_qlora_v3(model, r=8, alpha=16)
340
+ model.load_state_dict(state, strict=False)
341
  model.eval()
342
  return model, ckpt_type
343