|
19 | 19 | from transformers.models.granite.modeling_granite import eager_attention_forward |
20 | 20 |
|
21 | 21 |
|
| 22 | +def build_control_to_substitute_lut(config) -> torch.Tensor | None: |
| 23 | + """Derive the control->substitute lookup table from *config*. |
| 24 | +
|
| 25 | + Shape ``[max(vocab_size, max_ctrl_id + 1)]``: ``-1`` at every non-control id |
| 26 | + and the substitute id at each control slot. The ``max`` keeps every control |
| 27 | + id addressable even when ``vocab_size`` lags the tokenizer. |
| 28 | +
|
| 29 | + Returns ``None`` when *config* carries no token-exchange mapping, in which |
| 30 | + case the switch leaves ``input_ids`` untouched. |
| 31 | +
|
| 32 | + Single source of truth for the sizing rule: the table is a pure function of |
| 33 | + ``vocab_size``, ``adapter_token_ids`` and ``adapter_substitute_token_ids``, |
| 34 | + so anything that changes those must re-derive it (see |
| 35 | + :meth:`SingleSwitch.rebuild_control_to_substitute_lut`). |
| 36 | + """ |
| 37 | + if config is None: |
| 38 | + return None |
| 39 | + ctrl_ids = getattr(config, "adapter_token_ids", None) |
| 40 | + sub_ids = getattr(config, "adapter_substitute_token_ids", None) |
| 41 | + if not ctrl_ids or not sub_ids: |
| 42 | + return None |
| 43 | + |
| 44 | + lut_size = max(getattr(config, "vocab_size", 0), max(ctrl_ids) + 1) |
| 45 | + lut = torch.full((lut_size,), -1, dtype=torch.long) |
| 46 | + for ctrl_id, sub_id in zip(ctrl_ids, sub_ids): |
| 47 | + lut[ctrl_id] = sub_id |
| 48 | + return lut |
| 49 | + |
| 50 | + |
22 | 51 | class SingleSwitch(nn.Module): |
23 | 52 | """Single-head attention-based switch for adapter selection. |
24 | 53 |
|
@@ -89,22 +118,44 @@ def __init__( |
89 | 118 | # control-token positions carry the substitute id by the time the |
90 | 119 | # decoder embeds them. The decoder is then oblivious — it just calls |
91 | 120 | # embed_tokens(input_ids) and gets the right result by construction. |
92 | | - if ( |
93 | | - config is not None |
94 | | - and getattr(config, "adapter_token_ids", None) is not None |
95 | | - and getattr(config, "adapter_substitute_token_ids", None) is not None |
96 | | - ): |
97 | | - ctrl_ids = config.adapter_token_ids |
98 | | - sub_ids = config.adapter_substitute_token_ids |
99 | | - max_ctrl_id = max(ctrl_ids) |
100 | | - lut_size = max(getattr(config, "vocab_size", 0), max_ctrl_id + 1) |
101 | | - lut = torch.full((lut_size,), -1, dtype=torch.long) |
102 | | - for ctrl_id, sub_id in zip(ctrl_ids, sub_ids): |
103 | | - lut[ctrl_id] = sub_id |
| 121 | + lut = build_control_to_substitute_lut(config) |
| 122 | + if lut is not None: |
104 | 123 | self.register_buffer("control_to_substitute_lut", lut) |
105 | 124 | else: |
106 | 125 | self.control_to_substitute_lut = None |
107 | 126 |
|
| 127 | + def rebuild_control_to_substitute_lut(self, config=None) -> bool: |
| 128 | + """Re-derive the control->substitute table after a vocabulary change. |
| 129 | +
|
| 130 | + ``__init__`` sizes the table from ``config.vocab_size``, so anything that |
| 131 | + grows the vocabulary afterwards — notably |
| 132 | + ``resize_token_embeddings`` when compose adds control and marker tokens — |
| 133 | + leaves the buffer shorter than the config it will be saved alongside. |
| 134 | +
|
| 135 | + That matters because the buffer is persistent. On ``from_pretrained``, |
| 136 | + a stored tensor whose shape disagrees with the freshly-constructed one is |
| 137 | + discarded and the buffer is left as uninitialised memory (there is no |
| 138 | + ``_init_weights`` rule for it), so every id reads as a control id and the |
| 139 | + rewrite sends out-of-range ids into the embedding gather. Re-derive the |
| 140 | + table before saving so the checkpoint and its config agree. |
| 141 | +
|
| 142 | + Returns ``True`` if a table was rebuilt, ``False`` if this switch has no |
| 143 | + token-exchange mapping to rebuild. |
| 144 | + """ |
| 145 | + lut = build_control_to_substitute_lut( |
| 146 | + config if config is not None else self.config |
| 147 | + ) |
| 148 | + if lut is None: |
| 149 | + return False |
| 150 | + |
| 151 | + existing = getattr(self, "control_to_substitute_lut", None) |
| 152 | + if existing is not None: |
| 153 | + lut = lut.to(device=existing.device) |
| 154 | + self.control_to_substitute_lut = lut |
| 155 | + else: |
| 156 | + self.register_buffer("control_to_substitute_lut", lut) |
| 157 | + return True |
| 158 | + |
108 | 159 | @property |
109 | 160 | def num_cache_layers(self) -> int: |
110 | 161 | """Number of cache slots used.""" |
|
0 commit comments