MCPcopy Create free account
hub / github.com/OpenImagingLab/FlashVSR / tiled_forward

Method tiled_forward

diffsynth/models/tiler.py:192–234  ·  view source on GitHub ↗
(
        self,
        forward_fn,
        model_input,
        tile_size, tile_stride,
        tile_device="cpu", tile_dtype=torch.float32,
        computation_device="cuda", computation_dtype=torch.float32,
        border_width=None, scales=[1, 1, 1, 1],
        progress_bar=lambda x:x
    )

Source from the content-addressed store, hash-verified

190
191
192 def tiled_forward(
193 self,
194 forward_fn,
195 model_input,
196 tile_size, tile_stride,
197 tile_device="cpu", tile_dtype=torch.float32,
198 computation_device="cuda", computation_dtype=torch.float32,
199 border_width=None, scales=[1, 1, 1, 1],
200 progress_bar=lambda x:x
201 ):
202 B, C, T, H, W = model_input.shape
203 scale_C, scale_T, scale_H, scale_W = scales
204 tile_size_H, tile_size_W = tile_size
205 tile_stride_H, tile_stride_W = tile_stride
206
207 value = torch.zeros((B, int(C*scale_C), int(T*scale_T), int(H*scale_H), int(W*scale_W)), dtype=tile_dtype, device=tile_device)
208 weight = torch.zeros((1, 1, int(T*scale_T), int(H*scale_H), int(W*scale_W)), dtype=tile_dtype, device=tile_device)
209
210 # Split tasks
211 tasks = []
212 for h in range(0, H, tile_stride_H):
213 for w in range(0, W, tile_stride_W):
214 if (h-tile_stride_H >= 0 and h-tile_stride_H+tile_size_H >= H) or (w-tile_stride_W >= 0 and w-tile_stride_W+tile_size_W >= W):
215 continue
216 h_, w_ = h + tile_size_H, w + tile_size_W
217 if h_ > H: h, h_ = max(H - tile_size_H, 0), H
218 if w_ > W: w, w_ = max(W - tile_size_W, 0), W
219 tasks.append((h, h_, w, w_))
220
221 # Run
222 for hl, hr, wl, wr in progress_bar(tasks):
223 mask = self.build_mask(
224 int(T*scale_T), int((hr-hl)*scale_H), int((wr-wl)*scale_W),
225 tile_dtype, tile_device,
226 is_bound=(True, True, hl==0, hr>=H, wl==0, wr>=W),
227 border_width=border_width
228 )
229 grid_input = model_input[:, :, :, hl:hr, wl:wr].to(dtype=computation_dtype, device=computation_device)
230 grid_output = forward_fn(grid_input).to(dtype=tile_dtype, device=tile_device)
231 value[:, :, :, int(hl*scale_H):int(hr*scale_H), int(wl*scale_W):int(wr*scale_W)] += grid_output * mask
232 weight[:, :, :, int(hl*scale_H):int(hr*scale_H), int(wl*scale_W):int(wr*scale_W)] += mask
233 value = value / weight
234 return value

Callers

nothing calls this directly

Calls 2

build_maskMethod · 0.95
toMethod · 0.45

Tested by

no test coverage detected