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

Function apply_freeu

diffusers/src/diffusers/utils/torch_utils.py:124–148  ·  view source on GitHub ↗

Applies the FreeU mechanism as introduced in https: //arxiv.org/abs/2309.11497. Adapted from the official code repository: https://github.com/ChenyangSi/FreeU. Args: resolution_idx (`int`): Integer denoting the UNet block where FreeU is being applied. hidden_states (`torch.T

(
    resolution_idx: int, hidden_states: "torch.Tensor", res_hidden_states: "torch.Tensor", **freeu_kwargs
)

Source from the content-addressed store, hash-verified

122
123
124def apply_freeu(
125 resolution_idx: int, hidden_states: "torch.Tensor", res_hidden_states: "torch.Tensor", **freeu_kwargs
126) -> Tuple["torch.Tensor", "torch.Tensor"]:
127 """Applies the FreeU mechanism as introduced in https:
128 //arxiv.org/abs/2309.11497. Adapted from the official code repository: https://github.com/ChenyangSi/FreeU.
129
130 Args:
131 resolution_idx (`int`): Integer denoting the UNet block where FreeU is being applied.
132 hidden_states (`torch.Tensor`): Inputs to the underlying block.
133 res_hidden_states (`torch.Tensor`): Features from the skip block corresponding to the underlying block.
134 s1 (`float`): Scaling factor for stage 1 to attenuate the contributions of the skip features.
135 s2 (`float`): Scaling factor for stage 2 to attenuate the contributions of the skip features.
136 b1 (`float`): Scaling factor for stage 1 to amplify the contributions of backbone features.
137 b2 (`float`): Scaling factor for stage 2 to amplify the contributions of backbone features.
138 """
139 if resolution_idx == 0:
140 num_half_channels = hidden_states.shape[1] // 2
141 hidden_states[:, :num_half_channels] = hidden_states[:, :num_half_channels] * freeu_kwargs["b1"]
142 res_hidden_states = fourier_filter(res_hidden_states, threshold=1, scale=freeu_kwargs["s1"])
143 if resolution_idx == 1:
144 num_half_channels = hidden_states.shape[1] // 2
145 hidden_states[:, :num_half_channels] = hidden_states[:, :num_half_channels] * freeu_kwargs["b2"]
146 res_hidden_states = fourier_filter(res_hidden_states, threshold=1, scale=freeu_kwargs["s2"])
147
148 return hidden_states, res_hidden_states

Callers 9

forwardMethod · 0.85
forwardMethod · 0.85
forwardMethod · 0.85
forwardMethod · 0.85
forwardMethod · 0.85
forwardMethod · 0.85
forwardMethod · 0.85
forwardMethod · 0.85

Calls 1

fourier_filterFunction · 0.85

Tested by

no test coverage detected