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

Function rename

diffusers/scripts/convert_dance_diffusion_to_diffusers.py:161–217  ·  view source on GitHub ↗
(input_string, max_depth=13)

Source from the content-addressed store, hash-verified

159
160
161def rename(input_string, max_depth=13):
162 string = input_string
163
164 if string.split(".")[0] == "timestep_embed":
165 return string.replace("timestep_embed", "time_proj")
166
167 depth = 0
168 if string.startswith("net.3."):
169 depth += 1
170 string = string[6:]
171 elif string.startswith("net."):
172 string = string[4:]
173
174 while string.startswith("main.7."):
175 depth += 1
176 string = string[7:]
177
178 if string.startswith("main."):
179 string = string[5:]
180
181 # mid block
182 if string[:2].isdigit():
183 layer_num = string[:2]
184 string_left = string[2:]
185 else:
186 layer_num = string[0]
187 string_left = string[1:]
188
189 if depth == max_depth:
190 new_layer = MID_NUM_TO_LAYER[layer_num]
191 prefix = "mid_block"
192 elif depth > 0 and int(layer_num) < 7:
193 new_layer = DOWN_NUM_TO_LAYER[layer_num]
194 prefix = f"down_blocks.{depth}"
195 elif depth > 0 and int(layer_num) > 7:
196 new_layer = UP_NUM_TO_LAYER[layer_num]
197 prefix = f"up_blocks.{max_depth - depth - 1}"
198 elif depth == 0:
199 new_layer = DEPTH_0_TO_LAYER[layer_num]
200 prefix = f"up_blocks.{max_depth - 1}" if int(layer_num) > 3 else "down_blocks.0"
201
202 if not string_left.startswith("."):
203 raise ValueError(f"Naming error with {input_string} and string_left: {string_left}.")
204
205 string_left = string_left[1:]
206
207 if "resnets" in new_layer:
208 string_left = convert_resconv_naming(string_left)
209 elif "attentions" in new_layer:
210 new_string_left = convert_attn_naming(string_left)
211 string_left = new_string_left
212
213 if not isinstance(string_left, list):
214 new_string = prefix + "." + new_layer + "." + string_left
215 else:
216 new_string = [prefix + "." + new_layer + "." + s for s in string_left]
217 return new_string
218

Callers 1

rename_orig_weightsFunction · 0.85

Calls 2

convert_resconv_namingFunction · 0.85
convert_attn_namingFunction · 0.85

Tested by

no test coverage detected