(
self,
hidden_states,
timestep, prompt_emb, pooled_prompt_emb, guidance, text_ids, image_ids=None,
tiled=False, tile_size=128, tile_stride=64, entity_prompt_emb=None, entity_masks=None,
use_gradient_checkpointing=False,
**kwargs
)
| 405 | |
| 406 | |
| 407 | def forward( |
| 408 | self, |
| 409 | hidden_states, |
| 410 | timestep, prompt_emb, pooled_prompt_emb, guidance, text_ids, image_ids=None, |
| 411 | tiled=False, tile_size=128, tile_stride=64, entity_prompt_emb=None, entity_masks=None, |
| 412 | use_gradient_checkpointing=False, |
| 413 | **kwargs |
| 414 | ): |
| 415 | if tiled: |
| 416 | return self.tiled_forward( |
| 417 | hidden_states, |
| 418 | timestep, prompt_emb, pooled_prompt_emb, guidance, text_ids, |
| 419 | tile_size=tile_size, tile_stride=tile_stride, |
| 420 | **kwargs |
| 421 | ) |
| 422 | |
| 423 | if image_ids is None: |
| 424 | image_ids = self.prepare_image_ids(hidden_states) |
| 425 | |
| 426 | conditioning = self.time_embedder(timestep, hidden_states.dtype) + self.pooled_text_embedder(pooled_prompt_emb) |
| 427 | if self.guidance_embedder is not None: |
| 428 | guidance = guidance * 1000 |
| 429 | conditioning = conditioning + self.guidance_embedder(guidance, hidden_states.dtype) |
| 430 | |
| 431 | height, width = hidden_states.shape[-2:] |
| 432 | hidden_states = self.patchify(hidden_states) |
| 433 | hidden_states = self.x_embedder(hidden_states) |
| 434 | |
| 435 | if entity_prompt_emb is not None and entity_masks is not None: |
| 436 | prompt_emb, image_rotary_emb, attention_mask = self.process_entity_masks(hidden_states, prompt_emb, entity_prompt_emb, entity_masks, text_ids, image_ids) |
| 437 | else: |
| 438 | prompt_emb = self.context_embedder(prompt_emb) |
| 439 | image_rotary_emb = self.pos_embedder(torch.cat((text_ids, image_ids), dim=1)) |
| 440 | attention_mask = None |
| 441 | |
| 442 | def create_custom_forward(module): |
| 443 | def custom_forward(*inputs): |
| 444 | return module(*inputs) |
| 445 | return custom_forward |
| 446 | |
| 447 | for block in self.blocks: |
| 448 | if self.training and use_gradient_checkpointing: |
| 449 | hidden_states, prompt_emb = torch.utils.checkpoint.checkpoint( |
| 450 | create_custom_forward(block), |
| 451 | hidden_states, prompt_emb, conditioning, image_rotary_emb, attention_mask, |
| 452 | use_reentrant=False, |
| 453 | ) |
| 454 | else: |
| 455 | hidden_states, prompt_emb = block(hidden_states, prompt_emb, conditioning, image_rotary_emb, attention_mask) |
| 456 | |
| 457 | hidden_states = torch.cat([prompt_emb, hidden_states], dim=1) |
| 458 | for block in self.single_blocks: |
| 459 | if self.training and use_gradient_checkpointing: |
| 460 | hidden_states, prompt_emb = torch.utils.checkpoint.checkpoint( |
| 461 | create_custom_forward(block), |
| 462 | hidden_states, prompt_emb, conditioning, image_rotary_emb, attention_mask, |
| 463 | use_reentrant=False, |
| 464 | ) |
no test coverage detected