r""" Simply iterates over the state dict and replaces the patterns in `mapping` with the corresponding values. Args: state_dict (`dict[str, torch.Tensor]`): The state dict to convert. mapping (`dict[str, str]`): The mapping to use for conversion, the
(state_dict, mapping)
| 175 | |
| 176 | |
| 177 | def convert_state_dict(state_dict, mapping): |
| 178 | r""" |
| 179 | Simply iterates over the state dict and replaces the patterns in `mapping` with the corresponding values. |
| 180 | |
| 181 | Args: |
| 182 | state_dict (`dict[str, torch.Tensor]`): |
| 183 | The state dict to convert. |
| 184 | mapping (`dict[str, str]`): |
| 185 | The mapping to use for conversion, the mapping should be a dictionary with the following structure: |
| 186 | - key: the pattern to replace |
| 187 | - value: the pattern to replace with |
| 188 | |
| 189 | Returns: |
| 190 | converted_state_dict (`dict`) |
| 191 | The converted state dict. |
| 192 | """ |
| 193 | converted_state_dict = {} |
| 194 | for k, v in state_dict.items(): |
| 195 | # First, filter out the keys that we always want to replace |
| 196 | for pattern in KEYS_TO_ALWAYS_REPLACE.keys(): |
| 197 | if pattern in k: |
| 198 | new_pattern = KEYS_TO_ALWAYS_REPLACE[pattern] |
| 199 | k = k.replace(pattern, new_pattern) |
| 200 | |
| 201 | for pattern in mapping.keys(): |
| 202 | if pattern in k: |
| 203 | new_pattern = mapping[pattern] |
| 204 | k = k.replace(pattern, new_pattern) |
| 205 | break |
| 206 | converted_state_dict[k] = v |
| 207 | return converted_state_dict |
| 208 | |
| 209 | |
| 210 | def convert_state_dict_to_peft(state_dict, original_type=None, **kwargs): |
no outgoing calls
no test coverage detected
searching dependent graphs…