Id remapping¶
trimbed.remap
¶
Contiguous, order-preserving remapping of token ids.
IdRemap
dataclass
¶
A mapping from a kept subset of old token ids onto 0..n-1.
Old ids are sorted ascending before renumbering, so the surviving tokens keep their
relative order. That is what makes new_to_old usable as-is when pruning the
embeddings: it is the gather index that picks out the rows to keep.
E.g. keeping old ids {0, 5, 9} out of ten gives new_to_old == (0, 5, 9) and
old_to_new == {0: 0, 5: 1, 9: 2}.
from_kept
classmethod
¶
Build a remap from the set of old ids that survive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kept_ids
|
Iterable[int]
|
Old token ids to keep, e.g. |
required |
Returns:
| Type | Description |
|---|---|
Self
|
A remap numbering the sorted kept ids from zero, so that example yields
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If no ids were kept or an id is negative. |
from_vocabularies
classmethod
¶
Build a mapping between two vocabularies, e.g. the original and the trimmed one.
Only the tokens present in both are kept, numbered contiguously from zero.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
old
|
dict[str, int]
|
Token -> id map before trimming, e.g. codefuse-ai/F2LLM-v2-160M's 151,669 entries. |
required |
new
|
dict[str, int]
|
Token -> id map after trimming, numbered contiguously from zero, e.g. the 32,000 entries skeletoken left behind. |
required |
Returns:
| Type | Description |
|---|---|
Self
|
A remap covering every token present in both vocabularies. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the trimmed vocabulary introduces tokens the original lacked, or is not numbered contiguously from zero. |
__len__
¶
Return the number of surviving tokens.
__contains__
¶
Return whether old_id survives the trim.
__iter__
¶
Iterate over the kept old ids in ascending order.
to_new
¶
Map an old id to its new id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
old_id
|
int
|
Id in the original vocabulary. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The corresponding id in the trimmed vocabulary. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
to_old
¶
Map a new id back to the id it had in the original vocabulary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_id
|
int
|
Id in the trimmed vocabulary. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The original id. |
map_sequence
¶
Map a whole sequence or report that it cannot be mapped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
old_ids
|
Iterable[int]
|
Ids produced by the original tokenizer, e.g. |
required |
Returns:
| Type | Description |
|---|---|
list[int] | None
|
The remapped ids, or |