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

Method __call__

diffusers/examples/community/hd_painter.py:28–97  ·  view source on GitHub ↗
(
        self,
        attn: Attention,
        hidden_states: torch.Tensor,
        encoder_hidden_states: Optional[torch.Tensor] = None,
        attention_mask: Optional[torch.Tensor] = None,
        temb: Optional[torch.Tensor] = None,
        scale: float = 1.0,
    )

Source from the content-addressed store, hash-verified

26 self.mask_resoltuion = mask.shape[-1] * mask.shape[-2] # 64 x 64 if the image is 512x512
27
28 def __call__(
29 self,
30 attn: Attention,
31 hidden_states: torch.Tensor,
32 encoder_hidden_states: Optional[torch.Tensor] = None,
33 attention_mask: Optional[torch.Tensor] = None,
34 temb: Optional[torch.Tensor] = None,
35 scale: float = 1.0,
36 ) -> torch.Tensor:
37 # Same as the default AttnProcessor up untill the part where similarity matrix gets saved
38 downscale_factor = self.mask_resoltuion // hidden_states.shape[1]
39 residual = hidden_states
40
41 if attn.spatial_norm is not None:
42 hidden_states = attn.spatial_norm(hidden_states, temb)
43
44 input_ndim = hidden_states.ndim
45
46 if input_ndim == 4:
47 batch_size, channel, height, width = hidden_states.shape
48 hidden_states = hidden_states.view(batch_size, channel, height * width).transpose(1, 2)
49
50 batch_size, sequence_length, _ = (
51 hidden_states.shape if encoder_hidden_states is None else encoder_hidden_states.shape
52 )
53 attention_mask = attn.prepare_attention_mask(attention_mask, sequence_length, batch_size)
54
55 if attn.group_norm is not None:
56 hidden_states = attn.group_norm(hidden_states.transpose(1, 2)).transpose(1, 2)
57
58 query = attn.to_q(hidden_states)
59
60 if encoder_hidden_states is None:
61 encoder_hidden_states = hidden_states
62 elif attn.norm_cross:
63 encoder_hidden_states = attn.norm_encoder_hidden_states(encoder_hidden_states)
64
65 key = attn.to_k(encoder_hidden_states)
66 value = attn.to_v(encoder_hidden_states)
67
68 query = attn.head_to_batch_dim(query)
69 key = attn.head_to_batch_dim(key)
70 value = attn.head_to_batch_dim(value)
71
72 # Automatically recognize the resolution and save the attention similarity values
73 # We need to use the values before the softmax function, hence the rewritten get_attention_scores function.
74 if downscale_factor == self.scale_factor**2:
75 self.attention_scores = get_attention_scores(attn, query, key, attention_mask)
76 attention_probs = self.attention_scores.softmax(dim=-1)
77 attention_probs = attention_probs.to(query.dtype)
78 else:
79 attention_probs = attn.get_attention_scores(query, key, attention_mask) # Original code
80
81 hidden_states = torch.bmm(attention_probs, value)
82 hidden_states = attn.batch_to_head_dim(hidden_states)
83
84 # linear proj
85 hidden_states = attn.to_out[0](hidden_states)

Callers

nothing calls this directly

Calls 8

get_attention_scoresFunction · 0.85
transposeMethod · 0.80
head_to_batch_dimMethod · 0.80
get_attention_scoresMethod · 0.80
batch_to_head_dimMethod · 0.80
toMethod · 0.45

Tested by

no test coverage detected