(self, video, device, tile_size, tile_stride)
| 723 | |
| 724 | |
| 725 | def tiled_encode(self, video, device, tile_size, tile_stride): |
| 726 | _, _, T, H, W = video.shape |
| 727 | size_h, size_w = tile_size |
| 728 | stride_h, stride_w = tile_stride |
| 729 | |
| 730 | # Split tasks |
| 731 | tasks = [] |
| 732 | for h in range(0, H, stride_h): |
| 733 | if (h-stride_h >= 0 and h-stride_h+size_h >= H): continue |
| 734 | for w in range(0, W, stride_w): |
| 735 | if (w-stride_w >= 0 and w-stride_w+size_w >= W): continue |
| 736 | h_, w_ = h + size_h, w + size_w |
| 737 | tasks.append((h, h_, w, w_)) |
| 738 | |
| 739 | data_device = "cpu" |
| 740 | computation_device = device |
| 741 | |
| 742 | out_T = (T + 3) // 4 |
| 743 | weight = torch.zeros((1, 1, out_T, H // self.upsampling_factor, W // self.upsampling_factor), dtype=video.dtype, device=data_device) |
| 744 | values = torch.zeros((1, 16, out_T, H // self.upsampling_factor, W // self.upsampling_factor), dtype=video.dtype, device=data_device) |
| 745 | |
| 746 | for h, h_, w, w_ in tqdm(tasks, desc="VAE encoding"): |
| 747 | hidden_states_batch = video[:, :, :, h:h_, w:w_].to(computation_device) |
| 748 | hidden_states_batch = self.model.encode(hidden_states_batch, self.scale).to(data_device) |
| 749 | |
| 750 | mask = self.build_mask( |
| 751 | hidden_states_batch, |
| 752 | is_bound=(h==0, h_>=H, w==0, w_>=W), |
| 753 | border_width=((size_h - stride_h) // self.upsampling_factor, (size_w - stride_w) // self.upsampling_factor) |
| 754 | ).to(dtype=video.dtype, device=data_device) |
| 755 | |
| 756 | target_h = h // self.upsampling_factor |
| 757 | target_w = w // self.upsampling_factor |
| 758 | values[ |
| 759 | :, |
| 760 | :, |
| 761 | :, |
| 762 | target_h:target_h + hidden_states_batch.shape[3], |
| 763 | target_w:target_w + hidden_states_batch.shape[4], |
| 764 | ] += hidden_states_batch * mask |
| 765 | weight[ |
| 766 | :, |
| 767 | :, |
| 768 | :, |
| 769 | target_h: target_h + hidden_states_batch.shape[3], |
| 770 | target_w: target_w + hidden_states_batch.shape[4], |
| 771 | ] += mask |
| 772 | values = values / weight |
| 773 | return values |
| 774 | |
| 775 | |
| 776 | def single_encode(self, video, device): |
no test coverage detected