About
The d9d.module.block.embedding package provides enhanced embedding layers.
Features
Currently, this package provides only SplitTokenEmbeddings module. You can use this module:
- Regular Token Embedding Layer: Specify a single split with global vocab size.
- For Prompt Tuning: Add additional tokens to your Tokenizer and specify two splits - first one will be original token embeddings, second one will be newly added learnable prompt tokens. Unfreeze only
nn.Embeddingmodule that is related to the second split.
d9d.module.block.embedding
Package providing various embedding layer implementations
SplitTokenEmbeddings
Bases: Module, ModuleLateInit
A token embedding layer composed of multiple named, independent embedding tables.
This class maintains a dictionary of embedding layers, mapping contiguous ranges of global vocabulary indices to specific named splits (e.g., 'orig', 'special', 'prompt_prefix'). This is useful for model adaptation strategies where different sets of tokens require different initialization training behaviors.
Source code in d9d/module/block/embedding/shard_token_embedding.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | |
__init__(split_vocab_size, split_order, hidden_size)
Constructs the SplitTokenEmbeddings object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
split_vocab_size
|
dict[str, int]
|
A dictionary mapping split names to their vocabulary sizes. |
required |
split_order
|
Sequence[str]
|
A sequence defining the order in which splits are concatenated to form the global vocabulary. Keys provided here must exist in split_vocab_size. |
required |
hidden_size
|
int
|
The dimensionality of the embedding vectors. |
required |
Source code in d9d/module/block/embedding/shard_token_embedding.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |
forward(input_ids)
Retrieves embeddings for the input indices by routing them to appropriate internal layers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_ids
|
Tensor
|
Tensor of arbitrary shape containing global vocabulary indices. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Tensor of same shape as input_ids plus a last dimension of hidden_size. |
Source code in d9d/module/block/embedding/shard_token_embedding.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | |
reset_parameters()
Resets parameters for all registered embedding splits.
Source code in d9d/module/block/embedding/shard_token_embedding.py
93 94 95 96 97 98 99 | |