MCPcopy Create free account
hub / github.com/apple/ml-stable-diffusion / UNet2DConditionModelXL

Class UNet2DConditionModelXL

python_coreml_stable_diffusion/unet.py:1041–1142  ·  view source on GitHub ↗

UNet2DConditionModel variant for Stable Diffusion XL with an extended forward() signature

Source from the content-addressed store, hash-verified

1039
1040
1041class UNet2DConditionModelXL(UNet2DConditionModel):
1042 """ UNet2DConditionModel variant for Stable Diffusion XL
1043 with an extended forward() signature
1044 """
1045 def forward(
1046 self,
1047 sample,
1048 timestep,
1049 encoder_hidden_states,
1050 time_ids,
1051 text_embeds,
1052 *additional_residuals,
1053 ):
1054 # 0. Project time embeddings
1055 t_emb = self.time_proj(timestep)
1056 emb = self.time_embedding(t_emb)
1057
1058 aug_emb = None
1059
1060 if self.config.addition_embed_type == "text":
1061 raise NotImplementedError
1062 elif self.config.addition_embed_type == "text_image":
1063 raise NotImplementedError
1064 elif self.config.addition_embed_type == "text_time":
1065 assert time_ids is not None
1066 assert text_embeds is not None
1067
1068 time_embeds = self.add_time_proj(time_ids.flatten())
1069 time_embeds = time_embeds.reshape((text_embeds.shape[0], -1))
1070
1071 add_embeds = torch.concat([text_embeds, time_embeds], dim=-1)
1072 aug_emb = self.add_embedding(add_embeds)
1073 elif self.config.addition_embed_type == "image":
1074 raise NotImplementedError
1075 elif self.config.addition_embed_type == "image_hint":
1076 raise NotImplementedError
1077
1078 emb = emb + aug_emb if aug_emb is not None else emb
1079
1080 # 1. center input if necessary
1081 if self.config.center_input_sample:
1082 sample = 2 * sample - 1.0
1083
1084 # 2. pre-process
1085 sample = self.conv_in(sample)
1086
1087 # 3. down
1088 down_block_res_samples = (sample, )
1089 for downsample_block in self.down_blocks:
1090 if hasattr(
1091 downsample_block,
1092 "attentions") and downsample_block.attentions is not None:
1093 sample, res_samples = downsample_block(
1094 hidden_states=sample,
1095 temb=emb,
1096 encoder_hidden_states=encoder_hidden_states)
1097 else:
1098 sample, res_samples = downsample_block(hidden_states=sample,

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected