MCPcopy Create free account
hub / github.com/adobe-research/custom-diffusion / create_custom_diffusion

Function create_custom_diffusion

src/diffusers_training_sdxl.py:267–297  ·  view source on GitHub ↗
(unet, freeze_model)

Source from the content-addressed store, hash-verified

265
266
267def create_custom_diffusion(unet, freeze_model):
268 for name, params in unet.named_parameters():
269 if freeze_model == 'crossattn':
270 if 'attn2' in name:
271 params.requires_grad = True
272 print(name)
273 else:
274 params.requires_grad = False
275 elif freeze_model == "crossattn_kv":
276 if 'attn2.to_k' in name or 'attn2.to_v' in name:
277 params.requires_grad = True
278 print(name)
279 else:
280 params.requires_grad = False
281 else:
282 raise ValueError(
283 "freeze_model argument only supports crossattn_kv or crossattn"
284 )
285
286 # change attn class
287 def change_attn(unet):
288 for layer in unet.children():
289 if type(layer) is Attention:
290 bound_method = set_use_memory_efficient_attention_xformers.__get__(layer, layer.__class__)
291 setattr(layer, 'set_use_memory_efficient_attention_xformers', bound_method)
292 else:
293 change_attn(layer)
294
295 change_attn(unet)
296 unet.set_attn_processor(CustomDiffusionAttnProcessor())
297 return unet
298
299
300# Adapted from pipelines.StableDiffusionXLPipeline.encode_prompt

Callers 1

mainFunction · 0.70

Calls 2

change_attnFunction · 0.70

Tested by

no test coverage detected