MCPcopy Create free account
hub / github.com/YesianRohn/TextSSR / convert_state_dict

Function convert_state_dict

diffusers/src/diffusers/utils/state_dict_utils.py:141–171  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

139
140
141def convert_state_dict(state_dict, mapping):
142 r"""
143 Simply iterates over the state dict and replaces the patterns in `mapping` with the corresponding values.
144
145 Args:
146 state_dict (`dict[str, torch.Tensor]`):
147 The state dict to convert.
148 mapping (`dict[str, str]`):
149 The mapping to use for conversion, the mapping should be a dictionary with the following structure:
150 - key: the pattern to replace
151 - value: the pattern to replace with
152
153 Returns:
154 converted_state_dict (`dict`)
155 The converted state dict.
156 """
157 converted_state_dict = {}
158 for k, v in state_dict.items():
159 # First, filter out the keys that we always want to replace
160 for pattern in KEYS_TO_ALWAYS_REPLACE.keys():
161 if pattern in k:
162 new_pattern = KEYS_TO_ALWAYS_REPLACE[pattern]
163 k = k.replace(pattern, new_pattern)
164
165 for pattern in mapping.keys():
166 if pattern in k:
167 new_pattern = mapping[pattern]
168 k = k.replace(pattern, new_pattern)
169 break
170 converted_state_dict[k] = v
171 return converted_state_dict
172
173
174def convert_state_dict_to_peft(state_dict, original_type=None, **kwargs):

Calls

no outgoing calls

Tested by

no test coverage detected