Spaces:
Runtime error
Runtime error
Update modules/model.py
Browse files25 unmodified lines26272829303132333 unmodified lines37383940414222 unmodified lines65666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351369 unmodified lines1461471481491501511521531541551561573 unmodified lines1611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162 unmodified lines21922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025135 unmodified lines2872882892902912922932946 unmodified lines3013023033043053063073083093103113123133144 unmodified lines3193203213223233243253263273283293303313328 unmodified lines34134234334434534634734834935035135235335435535635735835936036136236336436536665 unmodified lines43243343443543643743894 unmodified lines53353453553653753853916 unmodified lines55655755855956056156256317 unmodified lines581582583584585586587588589590591592593594595596597146 unmodified lines74474574674774874975075175275375475575675775875976076176239 unmodified lines8028038048058068078087 unmodified lines8168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918 unmodified lines9009019029039049059068 unmodified lines91591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296325 unmodified linesfrom safetensors.torch import load_filexformers_available = Falsetry:import xformersxformers_available = Trueexcept ImportError:pass3 unmodified linesdefault = lambda val, d: val if exists(val) else dlogger = logging.get_logger(__name__) # pylint: disable=invalid-namedef get_attention_scores(attn, query, key, attention_mask=None):if attn.upcast_attention:22 unmodified linesreturn attention_scoresdef load_lora_attn_procs(model_file, unet, scale=1.0):if Path(model_file).suffix == ".pt":state_dict = torch.load(model_file, map_location="cpu")else:state_dict = load_file(model_file, device="cpu")# 'lora_unet_down_blocks_1_attentions_0_transformer_blocks_0_attn1_to_q.lora_down.weight'# 'down_blocks.0.attentions.0.transformer_blocks.0.attn1.processor.to_q_lora.down.weight'if any("lora_unet_down_blocks"in k for k in state_dict.keys()):# extract ldm format loradf_lora = {}attn_numlayer = re.compile(r'_attn(\d)_to_([qkv]|out).lora_')alpha_numlayer = re.compile(r'_attn(\d)_to_([qkv]|out).alpha')for k, v in state_dict.items():if "attn" not in k or "lora_te" in k:# currently not support: ff, clip-attncontinuek = k.replace("lora_unet_down_blocks_", "down_blocks.")k = k.replace("lora_unet_up_blocks_", "up_blocks.")k = k.replace("lora_unet_mid_block_", "mid_block_")k = k.replace("_attentions_", ".attentions.")k = k.replace("_transformer_blocks_", ".transformer_blocks.")k = k.replace("to_out_0", "to_out")k = attn_numlayer.sub(r'.attn\1.processor.to_\2_lora.', k)k = alpha_numlayer.sub(r'.attn\1.processor.to_\2_lora.alpha', k)df_lora[k] = vstate_dict = df_lora# fill attn processorsattn_processors = {}is_lora = all("lora" in k for k in state_dict.keys())if is_lora:lora_grouped_dict = defaultdict(dict)for key, value in state_dict.items():if "alpha" in key:attn_processor_key, sub_key = ".".join(key.split(".")[:-2]), ".".join(key.split(".")[-2:])else:attn_processor_key, sub_key = ".".join(key.split(".")[:-3]), ".".join(key.split(".")[-3:])lora_grouped_dict[attn_processor_key][sub_key] = valuefor key, value_dict in lora_grouped_dict.items():rank = value_dict["to_k_lora.down.weight"].shape[0]cross_attention_dim = value_dict["to_k_lora.down.weight"].shape[1]hidden_size = value_dict["to_k_lora.up.weight"].shape[0]attn_processors[key] = LoRACrossAttnProcessor(hidden_size=hidden_size, cross_attention_dim=cross_attention_dim, rank=rank, scale=scale)attn_processors[key].load_state_dict(value_dict, strict=False)else:raise ValueError(f"{model_file} does not seem to be in the correct format expected by LoRA training.")# set correct dtype & deviceattn_processors = {k: v.to(device=unet.device, dtype=unet.dtype) for k, v in attn_processors.items()}# set layersunet.set_attn_processor(attn_processors)class CrossAttnProcessor(nn.Module):def __call__(self, attn, hidden_states, encoder_hidden_states=None, attention_mask=None, qkvo_bias=None):batch_size, sequence_length, _ = hidden_states.shapeattention_mask = attn.prepare_attention_mask(attention_mask, sequence_length)9 unmodified linesquery = attn.to_q(hidden_states)key = attn.to_k(encoder_states)value = attn.to_v(encoder_states)if qkvo_bias is not None:query += qkvo_bias["q"](hidden_states)key += qkvo_bias["k"](encoder_states)value += qkvo_bias["v"](encoder_states)query = attn.head_to_batch_dim(query)key = attn.head_to_batch_dim(key)value = attn.head_to_batch_dim(value)3 unmodified linesattention_scores = get_attention_scores(attn, query, key, attention_mask)w = img_state[sequence_length].to(query.device)cross_attention_weight = weight_func(w, sigma, attention_scores)attention_scores += torch.repeat_interleave(cross_attention_weight, repeats=attn.heads, dim=0)# calc probsattention_probs = attention_scores.softmax(dim=-1)attention_probs = attention_probs.to(query.dtype)hidden_states = torch.bmm(attention_probs, value)elif xformers_available:hidden_states = xformers.ops.memory_efficient_attention(query.contiguous(), key.contiguous(), value.contiguous(), attn_bias=attention_mask)hidden_states = hidden_states.to(query.dtype)else:q_bucket_size = 512k_bucket_size = 1024# use flash-attentionhidden_states = FlashAttentionFunction(query.contiguous(), key.contiguous(), value.contiguous(),attention_mask, causal=False, q_bucket_size=q_bucket_size, k_bucket_size=k_bucket_size)hidden_states = hidden_states.to(query.dtype)hidden_states = attn.batch_to_head_dim(hidden_states)# linear projhidden_states = attn.to_out[0](hidden_states)if qkvo_bias is not None:hidden_states += qkvo_bias["o"](hidden_states)# dropouthidden_states = attn.to_out[1](hidden_states)return hidden_statesclass LoRACrossAttnProcessor(CrossAttnProcessor):def __init__(self, hidden_size, cross_attention_dim=None, rank=4, scale=1.0):super().__init__()self.to_q_lora = LoRALinearLayer(hidden_size, hidden_size, rank)self.to_k_lora = LoRALinearLayer(cross_attention_dim or hidden_size, hidden_size, rank)self.to_v_lora = LoRALinearLayer(cross_attention_dim or hidden_size, hidden_size, rank)self.to_out_lora = LoRALinearLayer(hidden_size, hidden_size, rank)self.scale = scaledef __call__(self, attn, hidden_states, encoder_hidden_states=None, attention_mask=None,):scale = self.scaleqkvo_bias = {2 unmodified lines"v": lambda inputs: scale * self.to_v_lora(inputs),"o": lambda inputs: scale * self.to_out_lora(inputs),}return super().__call__(attn, hidden_states, encoder_hidden_states, attention_mask, qkvo_bias)class LoRALinearLayer(nn.Module):def __init__(self, in_features, out_features, rank=4):super().__init__()if rank > min(in_features, out_features):raise ValueError(f"LoRA rank {rank} must be less or equal than {min(in_features, out_features)}")self.down = nn.Linear(in_features, rank, bias=False)self.up = nn.Linear(rank, out_features, bias=False)self.scale = 1.0self.alpha = ranknn.init.normal_(self.down.weight, std=1 / rank)nn.init.zeros_(self.up.weight)def forward(self, hidden_states):orig_dtype = hidden_states.dtypedtype = self.down.weight.dtyperank = self.down.out_featuresdown_hidden_states = self.down(hidden_states.to(dtype))up_hidden_states = self.up(down_hidden_states) * (self.alpha / rank)return up_hidden_states.to(orig_dtype)class ModelWrapper:35 unmodified linesscheduler=scheduler,)self.setup_unet(self.unet)self.prompt_parser = FrozenCLIPEmbedderWithCustomWords(self.tokenizer, self.text_encoder)def setup_unet(self, unet):unet = unet.to(self.device)model = ModelWrapper(unet, self.scheduler.alphas_cumprod)6 unmodified lineslibrary = importlib.import_module("k_diffusion")sampling = getattr(library, "sampling")return getattr(sampling, scheduler_type)def encode_sketchs(self, state, scale_ratio=8, g_strength=1.0, text_ids=None):uncond, cond = text_ids[0], text_ids[1]img_state = []if state is None:return torch.FloatTensor(0)for k, v in state.items():if v["map"] is None:continue4 unmodified linestruncation=True,add_special_tokens=False,).input_idsdotmap = v["map"] < 255arr = torch.from_numpy(dotmap.astype(float) * float(v["weight"]) * g_strength)img_state.append((v_input, arr))if len(img_state) == 0:return torch.FloatTensor(0)w_tensors = dict()cond = cond.tolist()uncond = uncond.tolist()8 unmodified linesfor v_as_tokens, img_where_color in img_state:is_in = 0ret = F.interpolate(img_where_color.unsqueeze(0).unsqueeze(1),scale_factor=1 / scale_ratio,mode="bilinear",align_corners=True,).squeeze().reshape(-1, 1).repeat(1, len(v_as_tokens))for idx, tok in enumerate(cond):if cond[idx : idx + len(v_as_tokens)] == v_as_tokens:is_in = 1ret_cond_tensor[0, :, idx : idx + len(v_as_tokens)] += (ret)for idx, tok in enumerate(uncond):if uncond[idx : idx + len(v_as_tokens)] == v_as_tokens:is_in = 1ret_uncond_tensor[0, :, idx : idx + len(v_as_tokens)] += (ret)if not is_in == 1:print(f"tokens {v_as_tokens} not found in text")w_tensors[w_r * h_r] = torch.cat([ret_uncond_tensor, ret_cond_tensor])scale_ratio *= 265 unmodified lines):return torch.device(module._hf_hook.execution_device)return self.devicedef decode_latents(self, latents):latents = latents.to(self.device, dtype=self.vae.dtype)latents = 1 / 0.18215 * latents94 unmodified linespww_attn_weight=1.0,sampler_name="",sampler_opt={},scale_ratio=8.0):sampler = self.get_scheduler(sampler_name)if image is not None:16 unmodified lines# 3. Encode input prompttext_ids, text_embeddings = self.prompt_parser([negative_prompt, prompt])text_embeddings = text_embeddings.to(self.unet.dtype)init_timestep = int(num_inference_steps / min(strength, 0.999)) if strength > 0 else 0sigmas = self.get_sigmas(init_timestep, sampler_opt).to(text_embeddings.device, dtype=text_embeddings.dtype)17 unmodified lines)img_state = self.encode_sketchs(pww_state,g_strength=pww_attn_weight,text_ids=text_ids,)def model_fn(x, sigma):latent_model_input = torch.cat([x] * 2)weight_func = (lambda w, sigma, qk: w * math.log(1 + sigma) * qk.max())encoder_state = {"img_state": img_state,"states": text_embeddings,146 unmodified linesself.k_diffusion_model.log_sigmas = self.k_diffusion_model.log_sigmas.to(latents.device)img_state = self.encode_sketchs(pww_state,g_strength=pww_attn_weight,text_ids=text_ids,)def model_fn(x, sigma):latent_model_input = torch.cat([x] * 2)weight_func = (lambda w, sigma, qk: w * math.log(1 + sigma) * qk.max())encoder_state = {"img_state": img_state,"states": text_embeddings,39 unmodified linessampler_name=sampler_name,sampler_opt=sampler_opt,pww_state=None,pww_attn_weight=pww_attn_weight/2,)# 8. Post-processing7 unmodified linesclass FlashAttentionFunction(Function):@staticmethod@torch.no_grad()def forward(ctx, q, k, v, mask, causal, q_bucket_size, k_bucket_size):""" Algorithm 2 in the paper """device = q.devicemax_neg_value = -torch.finfo(q.dtype).maxqk_len_diff = max(k.shape[-2] - q.shape[-2], 0)o = torch.zeros_like(q)all_row_sums = torch.zeros((*q.shape[:-1], 1), device = device)all_row_maxes = torch.full((*q.shape[:-1], 1), max_neg_value, device = device)scale = (q.shape[-1] ** -0.5)if not exists(mask):mask = (None,) * math.ceil(q.shape[-2] / q_bucket_size)else:mask = rearrange(mask, 'b n -> b 1 1 n')mask = mask.split(q_bucket_size, dim = -1)row_splits = zip(q.split(q_bucket_size, dim = -2),o.split(q_bucket_size, dim = -2),mask,all_row_sums.split(q_bucket_size, dim = -2),all_row_maxes.split(q_bucket_size, dim = -2),)for ind, (qc, oc, row_mask, row_sums, row_maxes) in enumerate(row_splits):q_start_index = ind * q_bucket_size - qk_len_diffcol_splits = zip(k.split(k_bucket_size, dim = -2),v.split(k_bucket_size, dim = -2),)for k_ind, (kc, vc) in enumerate(col_splits):k_start_index = k_ind * k_bucket_sizeattn_weights = einsum('... i d, ... j d -> ... i j', qc, kc) * scaleif exists(row_mask):attn_weights.masked_fill_(~row_mask, max_neg_value)if causal and q_start_index < (k_start_index + k_bucket_size - 1):causal_mask = torch.ones((qc.shape[-2], kc.shape[-2]), dtype = torch.bool, device = device).triu(q_start_index - k_start_index + 1)attn_weights.masked_fill_(causal_mask, max_neg_value)block_row_maxes = attn_weights.amax(dim = -1, keepdims = True)attn_weights -= block_row_maxesexp_weights = torch.exp(attn_weights)if exists(row_mask):exp_weights.masked_fill_(~row_mask, 0.)block_row_sums = exp_weights.sum(dim = -1, keepdims = True).clamp(min = EPSILON)new_row_maxes = torch.maximum(block_row_maxes, row_maxes)exp_values = einsum('... i j, ... j d -> ... i d', exp_weights, vc)exp_row_max_diff = torch.exp(row_maxes - new_row_maxes)exp_block_row_max_diff = torch.exp(block_row_maxes - new_row_maxes)new_row_sums = exp_row_max_diff * row_sums + exp_block_row_max_diff * block_row_sumsoc.mul_((row_sums / new_row_sums) * exp_row_max_diff).add_((exp_block_row_max_diff / new_row_sums) * exp_values)row_maxes.copy_(new_row_maxes)row_sums.copy_(new_row_sums)8 unmodified lines@staticmethod@torch.no_grad()def backward(ctx, do):""" Algorithm 4 in the paper """causal, scale, mask, q_bucket_size, k_bucket_size = ctx.argsq, k, v, o, lse = ctx.saved_tensors8 unmodified linesdv = torch.zeros_like(v)row_splits = zip(q.split(q_bucket_size, dim = -2),o.split(q_bucket_size, dim = -2),do.split(q_bucket_size, dim = -2),mask,lse.split(q_bucket_size, dim = -2),dq.split(q_bucket_size, dim = -2))for ind, (qc, oc, doc, row_mask, lsec, dqc) in enumerate(row_splits):q_start_index = ind * q_bucket_size - qk_len_diffcol_splits = zip(k.split(k_bucket_size, dim = -2),v.split(k_bucket_size, dim = -2),dk.split(k_bucket_size, dim = -2),dv.split(k_bucket_size, dim = -2),)for k_ind, (kc, vc, dkc, dvc) in enumerate(col_splits):k_start_index = k_ind * k_bucket_sizeattn_weights = einsum('... i d, ... j d -> ... i j', qc, kc) * scaleif causal and q_start_index < (k_start_index + k_bucket_size - 1):causal_mask = torch.ones((qc.shape[-2], kc.shape[-2]), dtype = torch.bool, device = device).triu(q_start_index - k_start_index + 1)attn_weights.masked_fill_(causal_mask, max_neg_value)p = torch.exp(attn_weights - lsec)if exists(row_mask):p.masked_fill_(~row_mask, 0.)dv_chunk = einsum('... i j, ... i d -> ... j d', p, doc)dp = einsum('... i d, ... j d -> ... i j', doc, vc)D = (doc * oc).sum(dim = -1, keepdims = True)ds = p * scale * (dp - D)dq_chunk = einsum('... i j, ... j d -> ... i d', ds, kc)dk_chunk = einsum('... i j, ... i d -> ... j d', ds, qc)dqc.add_(dq_chunk)dkc.add_(dk_chunk)dvc.add_(dv_chunk)return dq, dk, dv, None, None, None, None25 unmodified lines2627282930313233343 unmodified lines3839404142434422 unmodified lines6768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541559 unmodified lines1651661671681691701711721731741751763 unmodified lines1801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532 unmodified lines25625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129235 unmodified lines3283293303313323333343353363373383393406 unmodified lines3473483493503513523533543553563573583593604 unmodified lines3653663673683693703713723733743753763773783793808 unmodified lines38939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841965 unmodified lines48548648748848949049194 unmodified lines58658758858959059159216 unmodified lines60961061161261361461561661761817 unmodified lines636637638639640641642643644645646647648649650146 unmodified lines79779879980080180280380480580680780880981081181281339 unmodified lines8538548558568578588597 unmodified lines8678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489498 unmodified lines9589599609619629639648 unmodified lines9739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102525 unmodified linesfrom safetensors.torch import load_filexformers_available = Falsetry:import xformersxformers_available = Trueexcept ImportError:pass3 unmodified linesdefault = lambda val, d: val if exists(val) else dlogger = logging.get_logger(__name__) # pylint: disable=invalid-namedef get_attention_scores(attn, query, key, attention_mask=None):if attn.upcast_attention:22 unmodified linesreturn attention_scoresdef load_lora_attn_procs(model_file, unet, scale=1.0):if Path(model_file).suffix == ".pt":state_dict = torch.load(model_file, map_location="cpu")else:state_dict = load_file(model_file, device="cpu")if any("lora_unet_down_blocks" in k for k in state_dict.keys()):# convert ldm format loradf_lora = {}attn_numlayer = re.compile(r"_attn(\d)_to_([qkv]|out).lora_")alpha_numlayer = re.compile(r"_attn(\d)_to_([qkv]|out).alpha")for k, v in state_dict.items():if "attn" not in k or "lora_te" in k:# currently not support: ff, clip-attncontinuek = k.replace("lora_unet_down_blocks_", "down_blocks.")k = k.replace("lora_unet_up_blocks_", "up_blocks.")k = k.replace("lora_unet_mid_block_", "mid_block_")k = k.replace("_attentions_", ".attentions.")k = k.replace("_transformer_blocks_", ".transformer_blocks.")k = k.replace("to_out_0", "to_out")k = attn_numlayer.sub(r".attn\1.processor.to_\2_lora.", k)k = alpha_numlayer.sub(r".attn\1.processor.to_\2_lora.alpha", k)df_lora[k] = vstate_dict = df_lora# fill attn processorsattn_processors = {}is_lora = all("lora" in k for k in state_dict.keys())if is_lora:lora_grouped_dict = defaultdict(dict)for key, value in state_dict.items():if "alpha" in key:attn_processor_key, sub_key = ".".join(key.split(".")[:-2]), ".".join(key.split(".")[-2:])else:attn_processor_key, sub_key = ".".join(key.split(".")[:-3]), ".".join(key.split(".")[-3:])lora_grouped_dict[attn_processor_key][sub_key] = valuefor key, value_dict in lora_grouped_dict.items():rank = value_dict["to_k_lora.down.weight"].shape[0]cross_attention_dim = value_dict["to_k_lora.down.weight"].shape[1]hidden_size = value_dict["to_k_lora.up.weight"].shape[0]attn_processors[key] = LoRACrossAttnProcessor(hidden_size=hidden_size,cross_attention_dim=cross_attention_dim,rank=rank,scale=scale,)attn_processors[key].load_state_dict(value_dict, strict=False)else:raise ValueError(f"{model_file} does not seem to be in the correct format expected by LoRA training.")# set correct dtype & deviceattn_processors = {k: v.to(device=unet.device, dtype=unet.dtype)for k, v in attn_processors.items()}# set layersunet.set_attn_processor(attn_processors)class CrossAttnProcessor(nn.Module):def __call__(self,attn,hidden_states,encoder_hidden_states=None,attention_mask=None,qkvo_bias=None,):batch_size, sequence_length, _ = hidden_states.shapeattention_mask = attn.prepare_attention_mask(attention_mask, sequence_length)9 unmodified linesquery = attn.to_q(hidden_states)key = attn.to_k(encoder_states)value = attn.to_v(encoder_states)if qkvo_bias is not None:query += qkvo_bias["q"](hidden_states)key += qkvo_bias["k"](encoder_states)value += qkvo_bias["v"](encoder_states)query = attn.head_to_batch_dim(query)key = attn.head_to_batch_dim(key)value = attn.head_to_batch_dim(value)3 unmodified linesattention_scores = get_attention_scores(attn, query, key, attention_mask)w = img_state[sequence_length].to(query.device)cross_attention_weight = weight_func(w, sigma, attention_scores)attention_scores += torch.repeat_interleave(cross_attention_weight, repeats=attn.heads, dim=0)# calc probsattention_probs = attention_scores.softmax(dim=-1)attention_probs = attention_probs.to(query.dtype)hidden_states = torch.bmm(attention_probs, value)elif xformers_available:hidden_states = xformers.ops.memory_efficient_attention(query.contiguous(),key.contiguous(),value.contiguous(),attn_bias=attention_mask,)hidden_states = hidden_states.to(query.dtype)else:q_bucket_size = 512k_bucket_size = 1024# use flash-attentionhidden_states = FlashAttn.apply(query.contiguous(),key.contiguous(),value.contiguous(),attention_mask,causal=False,q_bucket_size=q_bucket_size,k_bucket_size=k_bucket_size,)hidden_states = hidden_states.to(query.dtype)hidden_states = attn.batch_to_head_dim(hidden_states)# linear projhidden_states = attn.to_out[0](hidden_states)if qkvo_bias is not None:hidden_states += qkvo_bias["o"](hidden_states)# dropouthidden_states = attn.to_out[1](hidden_states)return hidden_statesclass LoRACrossAttnProcessor(CrossAttnProcessor):def __init__(self, hidden_size, cross_attention_dim=None, rank=4, scale=1.0):super().__init__()self.to_q_lora = LoRALinearLayer(hidden_size, hidden_size, rank)self.to_k_lora = LoRALinearLayer(cross_attention_dim or hidden_size, hidden_size, rank)self.to_v_lora = LoRALinearLayer(cross_attention_dim or hidden_size, hidden_size, rank)self.to_out_lora = LoRALinearLayer(hidden_size, hidden_size, rank)self.scale = scaledef __call__(self,attn,hidden_states,encoder_hidden_states=None,attention_mask=None,):scale = self.scaleqkvo_bias = {2 unmodified lines"v": lambda inputs: scale * self.to_v_lora(inputs),"o": lambda inputs: scale * self.to_out_lora(inputs),}return super().__call__(attn, hidden_states, encoder_hidden_states, attention_mask, qkvo_bias)class LoRALinearLayer(nn.Module):def __init__(self, in_features, out_features, rank=4):super().__init__()if rank > min(in_features, out_features):raise ValueError(f"LoRA rank {rank} must be less or equal than {min(in_features, out_features)}")self.down = nn.Linear(in_features, rank, bias=False)self.up = nn.Linear(rank, out_features, bias=False)self.scale = 1.0self.alpha = ranknn.init.normal_(self.down.weight, std=1 / rank)nn.init.zeros_(self.up.weight)def forward(self, hidden_states):orig_dtype = hidden_states.dtypedtype = self.down.weight.dtyperank = self.down.out_featuresdown_hidden_states = self.down(hidden_states.to(dtype))up_hidden_states = self.up(down_hidden_states) * (self.alpha / rank)return up_hidden_states.to(orig_dtype)class ModelWrapper:35 unmodified linesscheduler=scheduler,)self.setup_unet(self.unet)self.prompt_parser = FrozenCLIPEmbedderWithCustomWords(self.tokenizer, self.text_encoder)def set_clip_skip(self, n):self.prompt_parser.CLIP_stop_at_last_layers = ndef setup_unet(self, unet):unet = unet.to(self.device)model = ModelWrapper(unet, self.scheduler.alphas_cumprod)6 unmodified lineslibrary = importlib.import_module("k_diffusion")sampling = getattr(library, "sampling")return getattr(sampling, scheduler_type)def encode_sketchs(self, state, scale_ratio=8, g_strength=1.0, text_ids=None):uncond, cond = text_ids[0], text_ids[1]img_state = []if state is None:return torch.FloatTensor(0)for k, v in state.items():if v["map"] is None:continue4 unmodified linestruncation=True,add_special_tokens=False,).input_idsdotmap = v["map"] < 255arr = torch.from_numpy(dotmap.astype(float) * float(v["weight"]) * g_strength)img_state.append((v_input, arr))if len(img_state) == 0:return torch.FloatTensor(0)w_tensors = dict()cond = cond.tolist()uncond = uncond.tolist()8 unmodified linesfor v_as_tokens, img_where_color in img_state:is_in = 0ret = (F.interpolate(img_where_color.unsqueeze(0).unsqueeze(1),scale_factor=1 / scale_ratio,mode="bilinear",align_corners=True,).squeeze().reshape(-1, 1).repeat(1, len(v_as_tokens)))for idx, tok in enumerate(cond):if cond[idx : idx + len(v_as_tokens)] == v_as_tokens:is_in = 1ret_cond_tensor[0, :, idx : idx + len(v_as_tokens)] += retfor idx, tok in enumerate(uncond):if uncond[idx : idx + len(v_as_tokens)] == v_as_tokens:is_in = 1ret_uncond_tensor[0, :, idx : idx + len(v_as_tokens)] += retif not is_in == 1:print(f"tokens {v_as_tokens} not found in text")w_tensors[w_r * h_r] = torch.cat([ret_uncond_tensor, ret_cond_tensor])scale_ratio *= 265 unmodified lines):return torch.device(module._hf_hook.execution_device)return self.devicedef decode_latents(self, latents):latents = latents.to(self.device, dtype=self.vae.dtype)latents = 1 / 0.18215 * latents94 unmodified linespww_attn_weight=1.0,sampler_name="",sampler_opt={},scale_ratio=8.0,):sampler = self.get_scheduler(sampler_name)if image is not None:16 unmodified lines# 3. Encode input prompttext_ids, text_embeddings = self.prompt_parser([negative_prompt, prompt])text_embeddings = text_embeddings.to(self.unet.dtype)init_timestep = (int(num_inference_steps / min(strength, 0.999)) if strength > 0 else 0)sigmas = self.get_sigmas(init_timestep, sampler_opt).to(text_embeddings.device, dtype=text_embeddings.dtype)17 unmodified lines)img_state = self.encode_sketchs(pww_state,g_strength=pww_attn_weight,text_ids=text_ids,)def model_fn(x, sigma):latent_model_input = torch.cat([x] * 2)weight_func = lambda w, sigma, qk: w * math.log(1 + sigma) * qk.max()encoder_state = {"img_state": img_state,"states": text_embeddings,146 unmodified linesself.k_diffusion_model.log_sigmas = self.k_diffusion_model.log_sigmas.to(latents.device)img_state = self.encode_sketchs(pww_state,g_strength=pww_attn_weight,text_ids=text_ids,)def model_fn(x, sigma):latent_model_input = torch.cat([x] * 2)weight_func = lambda w, sigma, qk: w * math.log(1 + sigma) * qk.max()encoder_state = {"img_state": img_state,"states": text_embeddings,39 unmodified linessampler_name=sampler_name,sampler_opt=sampler_opt,pww_state=None,pww_attn_weight=pww_attn_weight / 2,)# 8. Post-processing7 unmodified linesclass FlashAttentionFunction(Function):@staticmethod@torch.no_grad()def forward(ctx, q, k, v, mask, causal, q_bucket_size, k_bucket_size):"""Algorithm 2 in the paper"""device = q.devicemax_neg_value = -torch.finfo(q.dtype).maxqk_len_diff = max(k.shape[-2] - q.shape[-2], 0)o = torch.zeros_like(q)all_row_sums = torch.zeros((*q.shape[:-1], 1), device=device)all_row_maxes = torch.full((*q.shape[:-1], 1), max_neg_value, device=device)scale = q.shape[-1] ** -0.5if not exists(mask):mask = (None,) * math.ceil(q.shape[-2] / q_bucket_size)else:mask = rearrange(mask, "b n -> b 1 1 n")mask = mask.split(q_bucket_size, dim=-1)row_splits = zip(q.split(q_bucket_size, dim=-2),o.split(q_bucket_size, dim=-2),mask,all_row_sums.split(q_bucket_size, dim=-2),all_row_maxes.split(q_bucket_size, dim=-2),)for ind, (qc, oc, row_mask, row_sums, row_maxes) in enumerate(row_splits):q_start_index = ind * q_bucket_size - qk_len_diffcol_splits = zip(k.split(k_bucket_size, dim=-2),v.split(k_bucket_size, dim=-2),)for k_ind, (kc, vc) in enumerate(col_splits):k_start_index = k_ind * k_bucket_sizeattn_weights = einsum("... i d, ... j d -> ... i j", qc, kc) * scaleif exists(row_mask):attn_weights.masked_fill_(~row_mask, max_neg_value)if causal and q_start_index < (k_start_index + k_bucket_size - 1):causal_mask = torch.ones((qc.shape[-2], kc.shape[-2]), dtype=torch.bool, device=device).triu(q_start_index - k_start_index + 1)attn_weights.masked_fill_(causal_mask, max_neg_value)block_row_maxes = attn_weights.amax(dim=-1, keepdims=True)attn_weights -= block_row_maxesexp_weights = torch.exp(attn_weights)if exists(row_mask):exp_weights.masked_fill_(~row_mask, 0.0)block_row_sums = exp_weights.sum(dim=-1, keepdims=True).clamp(min=EPSILON)new_row_maxes = torch.maximum(block_row_maxes, row_maxes)exp_values = einsum("... i j, ... j d -> ... i d", exp_weights, vc)exp_row_max_diff = torch.exp(row_maxes - new_row_maxes)exp_block_row_max_diff = torch.exp(block_row_maxes - new_row_maxes)new_row_sums = (exp_row_max_diff * row_sums+ exp_block_row_max_diff * block_row_sums)oc.mul_((row_sums / new_row_sums) * exp_row_max_diff).add_((exp_block_row_max_diff / new_row_sums) * exp_values)row_maxes.copy_(new_row_maxes)row_sums.copy_(new_row_sums)8 unmodified lines@staticmethod@torch.no_grad()def backward(ctx, do):"""Algorithm 4 in the paper"""causal, scale, mask, q_bucket_size, k_bucket_size = ctx.argsq, k, v, o, lse = ctx.saved_tensors8 unmodified linesdv = torch.zeros_like(v)row_splits = zip(q.split(q_bucket_size, dim=-2),o.split(q_bucket_size, dim=-2),do.split(q_bucket_size, dim=-2),mask,lse.split(q_bucket_size, dim=-2),dq.split(q_bucket_size, dim=-2),)for ind, (qc, oc, doc, row_mask, lsec, dqc) in enumerate(row_splits):q_start_index = ind * q_bucket_size - qk_len_diffcol_splits = zip(k.split(k_bucket_size, dim=-2),v.split(k_bucket_size, dim=-2),dk.split(k_bucket_size, dim=-2),dv.split(k_bucket_size, dim=-2),)for k_ind, (kc, vc, dkc, dvc) in enumerate(col_splits):k_start_index = k_ind * k_bucket_sizeattn_weights = einsum("... i d, ... j d -> ... i j", qc, kc) * scaleif causal and q_start_index < (k_start_index + k_bucket_size - 1):causal_mask = torch.ones((qc.shape[-2], kc.shape[-2]), dtype=torch.bool, device=device).triu(q_start_index - k_start_index + 1)attn_weights.masked_fill_(causal_mask, max_neg_value)p = torch.exp(attn_weights - lsec)if exists(row_mask):p.masked_fill_(~row_mask, 0.0)dv_chunk = einsum("... i j, ... i d -> ... j d", p, doc)dp = einsum("... i d, ... j d -> ... i j", doc, vc)D = (doc * oc).sum(dim=-1, keepdims=True)ds = p * scale * (dp - D)dq_chunk = einsum("... i j, ... j d -> ... i d", ds, kc)dk_chunk = einsum("... i j, ... i d -> ... j d", ds, qc)dqc.add_(dq_chunk)dkc.add_(dk_chunk)dvc.add_(dv_chunk)return dq, dk, dv, None, None, None, NoneFlashAttn = FlashAttentionFunction()
modules/model.py+238-176