Upload slimshard.py
Browse files- slimshard.py +33 -12
slimshard.py
CHANGED
|
@@ -1480,6 +1480,29 @@ def _write_lm_head_calibrated_chunked(
|
|
| 1480 |
alt = s.replace(" ", "Ġ").replace("\n", "Ċ")
|
| 1481 |
return vocab_id_by_str.get(alt, -1)
|
| 1482 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1483 |
is_raon = (
|
| 1484 |
"model.embed_tokens.weight" in writer._entries
|
| 1485 |
and "model.norm.weight" in writer._entries
|
|
@@ -1499,32 +1522,30 @@ def _write_lm_head_calibrated_chunked(
|
|
| 1499 |
if is_raon or is_deepseek_v41 or has_generic_layers:
|
| 1500 |
writer._tmp.flush()
|
| 1501 |
raw_tokens = ["hello", '"', " is", ' "', "Hello", ",", " World", '!".']
|
|
|
|
| 1502 |
if im_end_id >= 0:
|
| 1503 |
-
|
| 1504 |
-
elif int(arch.get("eos_token_id", -1)) >= 0:
|
| 1505 |
-
raw_seq_eos = int(arch.get("eos_token_id", 1))
|
| 1506 |
-
raw_seq = [tok_id(x) for x in raw_tokens]
|
| 1507 |
-
if im_end_id < 0 and int(arch.get("eos_token_id", -1)) >= 0:
|
| 1508 |
-
raw_seq.append(int(arch["eos_token_id"]))
|
| 1509 |
|
| 1510 |
chat_prefix_tokens = [
|
| 1511 |
"<|im_start|>", "user", "\n", "hello", "<|im_end|>", "\n",
|
| 1512 |
"<|im_start|>", "assistant", "\n", "<think>", "\n\n", "</think>", "\n\n",
|
| 1513 |
]
|
| 1514 |
chat_reply_tokens = ["Hello", "!", " How", " can", " I", " help", " you", " today", "?"]
|
|
|
|
|
|
|
| 1515 |
if im_end_id >= 0:
|
| 1516 |
-
|
| 1517 |
-
chat_seq = [tok_id(x) for x in (chat_prefix_tokens + chat_reply_tokens)]
|
| 1518 |
|
| 1519 |
seqs_to_run = []
|
| 1520 |
seq_transition_slices = []
|
| 1521 |
-
if
|
| 1522 |
seqs_to_run.append(raw_seq[:-1])
|
| 1523 |
seq_transition_slices.append((0, raw_seq[1:]))
|
| 1524 |
-
if
|
|
|
|
| 1525 |
seqs_to_run.append(chat_seq[:-1])
|
| 1526 |
-
start_pos = len(
|
| 1527 |
-
seq_transition_slices.append((start_pos,
|
| 1528 |
|
| 1529 |
bg_seq = [int(x) for x in np.linspace(100, min(max(vocab - 1, 100), 20000), 16, dtype=np.int64)]
|
| 1530 |
seqs_to_run.append(bg_seq)
|
|
|
|
| 1480 |
alt = s.replace(" ", "Ġ").replace("\n", "Ċ")
|
| 1481 |
return vocab_id_by_str.get(alt, -1)
|
| 1482 |
|
| 1483 |
+
def tok_piece_ids(pieces: list[str]) -> list[int]:
|
| 1484 |
+
out_ids: list[int] = []
|
| 1485 |
+
for p in pieces:
|
| 1486 |
+
tid = tok_id(p)
|
| 1487 |
+
if tid >= 0:
|
| 1488 |
+
out_ids.append(tid)
|
| 1489 |
+
continue
|
| 1490 |
+
# Greedy longest-prefix decomposition for multi-token pieces like '!".' -> '!' + '".'
|
| 1491 |
+
s = p.replace(" ", "Ġ").replace("\n", "Ċ")
|
| 1492 |
+
pos = 0
|
| 1493 |
+
while pos < len(s):
|
| 1494 |
+
matched = False
|
| 1495 |
+
for end in range(len(s), pos, -1):
|
| 1496 |
+
sub = s[pos:end]
|
| 1497 |
+
if sub in vocab_id_by_str:
|
| 1498 |
+
out_ids.append(vocab_id_by_str[sub])
|
| 1499 |
+
pos = end
|
| 1500 |
+
matched = True
|
| 1501 |
+
break
|
| 1502 |
+
if not matched:
|
| 1503 |
+
pos += 1
|
| 1504 |
+
return out_ids
|
| 1505 |
+
|
| 1506 |
is_raon = (
|
| 1507 |
"model.embed_tokens.weight" in writer._entries
|
| 1508 |
and "model.norm.weight" in writer._entries
|
|
|
|
| 1522 |
if is_raon or is_deepseek_v41 or has_generic_layers:
|
| 1523 |
writer._tmp.flush()
|
| 1524 |
raw_tokens = ["hello", '"', " is", ' "', "Hello", ",", " World", '!".']
|
| 1525 |
+
raw_seq = tok_piece_ids(raw_tokens)
|
| 1526 |
if im_end_id >= 0:
|
| 1527 |
+
raw_seq.append(im_end_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1528 |
|
| 1529 |
chat_prefix_tokens = [
|
| 1530 |
"<|im_start|>", "user", "\n", "hello", "<|im_end|>", "\n",
|
| 1531 |
"<|im_start|>", "assistant", "\n", "<think>", "\n\n", "</think>", "\n\n",
|
| 1532 |
]
|
| 1533 |
chat_reply_tokens = ["Hello", "!", " How", " can", " I", " help", " you", " today", "?"]
|
| 1534 |
+
chat_pref_ids = tok_piece_ids(chat_prefix_tokens)
|
| 1535 |
+
chat_rep_ids = tok_piece_ids(chat_reply_tokens)
|
| 1536 |
if im_end_id >= 0:
|
| 1537 |
+
chat_rep_ids.append(im_end_id)
|
|
|
|
| 1538 |
|
| 1539 |
seqs_to_run = []
|
| 1540 |
seq_transition_slices = []
|
| 1541 |
+
if len(raw_seq) >= 2:
|
| 1542 |
seqs_to_run.append(raw_seq[:-1])
|
| 1543 |
seq_transition_slices.append((0, raw_seq[1:]))
|
| 1544 |
+
if tok_id("<|im_start|>") >= 0 and len(chat_pref_ids) >= 2 and len(chat_rep_ids) >= 2:
|
| 1545 |
+
chat_seq = chat_pref_ids + chat_rep_ids
|
| 1546 |
seqs_to_run.append(chat_seq[:-1])
|
| 1547 |
+
start_pos = len(chat_pref_ids) - 1
|
| 1548 |
+
seq_transition_slices.append((start_pos, chat_rep_ids))
|
| 1549 |
|
| 1550 |
bg_seq = [int(x) for x in np.linspace(100, min(max(vocab - 1, 100), 20000), 16, dtype=np.int64)]
|
| 1551 |
seqs_to_run.append(bg_seq)
|