CompressedGemma commited on
Commit
f4c62e9
·
verified ·
1 Parent(s): 8668eaf

Upload slimshard.py

Browse files
Files changed (1) hide show
  1. slimshard.py +37 -5
slimshard.py CHANGED
@@ -1461,18 +1461,26 @@ def _write_lm_head_calibrated_chunked(
1461
  and "model.layers.0.input_layernorm.weight" in writer._entries
1462
  and "model.layers.0.self_attn_raon_mhc.phi_pre" in writer._entries
1463
  )
 
 
 
 
1464
  has_generic_layers = any(LAYER_RE.match(k) for k in writer._entries)
1465
 
1466
  h_states_list: list["torch.Tensor"] = []
1467
  target_ids: list[int] = []
1468
  bg_states_list: list["torch.Tensor"] = []
1469
 
1470
- if is_raon or has_generic_layers:
1471
  writer._tmp.flush()
1472
  raw_tokens = ["hello", '"', " is", ' "', "Hello", ",", " World", '!".']
1473
  if im_end_id >= 0:
1474
  raw_tokens.append("<|im_end|>")
 
 
1475
  raw_seq = [tok_id(x) for x in raw_tokens]
 
 
1476
 
1477
  chat_prefix_tokens = [
1478
  "<|im_start|>", "user", "\n", "hello", "<|im_end|>", "\n",
@@ -1502,6 +1510,17 @@ def _write_lm_head_calibrated_chunked(
1502
  outs = _run_exact_reconstructed_forward(
1503
  writer, seqs_to_run, coords, basis_in, scale_in, arch, dev
1504
  )
 
 
 
 
 
 
 
 
 
 
 
1505
  else:
1506
  outs = _run_generic_transformer_forward(
1507
  writer, seqs_to_run, coords, basis_in, scale_in, arch, dev
@@ -2204,10 +2223,23 @@ def recover_from_shard(
2204
  _notify(name, "SYNTHESIZE_CALIBRATED")
2205
  continue
2206
 
2207
- tail_match = (
2208
- by_tail.get(".".join(name.split(".")[-2:]))
2209
- or by_tail.get(".".join(name.split(".")[-3:]))
2210
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
2211
  if tail_match is None and "norm" in name:
2212
  tail_match = next((n for n in present if "norm" in n), None)
2213
  if tail_match is None and "bias" in name:
 
1461
  and "model.layers.0.input_layernorm.weight" in writer._entries
1462
  and "model.layers.0.self_attn_raon_mhc.phi_pre" in writer._entries
1463
  )
1464
+ is_deepseek_v41 = (
1465
+ "layers.0.hc_attn_fn" in writer._entries
1466
+ or "model.layers.0.hc_attn_fn" in writer._entries
1467
+ )
1468
  has_generic_layers = any(LAYER_RE.match(k) for k in writer._entries)
1469
 
1470
  h_states_list: list["torch.Tensor"] = []
1471
  target_ids: list[int] = []
1472
  bg_states_list: list["torch.Tensor"] = []
1473
 
1474
+ if is_raon or is_deepseek_v41 or has_generic_layers:
1475
  writer._tmp.flush()
1476
  raw_tokens = ["hello", '"', " is", ' "', "Hello", ",", " World", '!".']
1477
  if im_end_id >= 0:
1478
  raw_tokens.append("<|im_end|>")
1479
+ elif int(arch.get("eos_token_id", -1)) >= 0:
1480
+ raw_seq_eos = int(arch.get("eos_token_id", 1))
1481
  raw_seq = [tok_id(x) for x in raw_tokens]
1482
+ if im_end_id < 0 and int(arch.get("eos_token_id", -1)) >= 0:
1483
+ raw_seq.append(int(arch["eos_token_id"]))
1484
 
1485
  chat_prefix_tokens = [
1486
  "<|im_start|>", "user", "\n", "hello", "<|im_end|>", "\n",
 
1510
  outs = _run_exact_reconstructed_forward(
1511
  writer, seqs_to_run, coords, basis_in, scale_in, arch, dev
1512
  )
1513
+ elif is_deepseek_v41:
1514
+ outs = _run_deepseek_v41_forward(
1515
+ lambda k: writer.read_tensor_torch(k, dev),
1516
+ lambda k: k in writer._entries,
1517
+ seqs_to_run,
1518
+ coords,
1519
+ basis_in,
1520
+ scale_in,
1521
+ arch,
1522
+ dev,
1523
+ )
1524
  else:
1525
  outs = _run_generic_transformer_forward(
1526
  writer, seqs_to_run, coords, basis_in, scale_in, arch, dev
 
2223
  _notify(name, "SYNTHESIZE_CALIBRATED")
2224
  continue
2225
 
2226
+ tail_match = None
2227
+ if name in _NORM_NAMES or "norm" in name:
2228
+ hidden_sz = int(arch.get("hidden_size", 0))
2229
+ if hidden_sz > 0 and name in _NORM_NAMES:
2230
+ tail_match = next(
2231
+ (
2232
+ n
2233
+ for n in sorted(present)
2234
+ if "norm" in n and tuple(f.get_slice(n).get_shape()) == (hidden_sz,)
2235
+ ),
2236
+ None,
2237
+ )
2238
+ if tail_match is None:
2239
+ tail_match = (
2240
+ by_tail.get(".".join(name.split(".")[-2:]))
2241
+ or by_tail.get(".".join(name.split(".")[-3:]))
2242
+ )
2243
  if tail_match is None and "norm" in name:
2244
  tail_match = next((n for n in present if "norm" in n), None)
2245
  if tail_match is None and "bias" in name: