File size: 13,223 Bytes
37a82b7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | # Copyright (c) 2025 SandAI. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import os
from abc import ABC, abstractmethod
from typing import Literal
import torch
from diffusers import ConfigMixin, ModelMixin
from diffusers.configuration_utils import register_to_config
from inference.infra.parallelism import TileProcessor
from .vae_module import DiagonalGaussianDistribution, ViTDecoder, ViTEncoder
class VideoTokenizerABC(ABC):
"""
Abstract base class for video tokenizers.
This class defines the interface for video tokenizers and provides common methods and properties.
"""
@property
@abstractmethod
def spatial_downsample_factor(self):
"""
Property representing the spatial downsample factor.
Returns:
int: The spatial downsample factor.
"""
raise NotImplementedError
@property
@abstractmethod
def temporal_downsample_factor(self):
"""
Property representing the temporal downsample factor.
Returns:
int: The temporal downsample factor.
"""
raise NotImplementedError
@property
def first_frame_as_image(self):
"""
Property representing the first frame as image.
For tokenizer like CausalVAE, Omnitokenizer, the first frame is treated as image.
in this case if the temporal downsample factor is 4, the input should be 4*x+1, and encoded tensor would be x+1.
for example encode 65 frames to 17 frames. and decode 17 frames to 65 frames.
Returns:
bool: The first frame as image.
"""
return False
@property
def allow_spatial_tiling(self):
"""
Determines whether spatial tiling is allowed or not.
Returns:
bool: True if spatial tiling is allowed, False otherwise.
"""
return True
@abstractmethod
def encode(self, x) -> torch.Tensor:
"""
Abstract method for encoding the input tensor.
Args:
x (torch.Tensor [N C T H W] range[-1, 1]): The input tensor to be encoded.
Returns:
torch.Tensor: The encoded tensor.
"""
raise NotImplementedError
@abstractmethod
def decode(self, x) -> torch.Tensor:
"""
Abstract method for decoding the input tensor.
Args:
x (torch.Tensor [N C T H W]): The input tensor to be decoded.
Returns:
torch.Tensor [N C T H W] range[-1, 1]: The decoded tensor.
"""
raise NotImplementedError
def tile_processor(
self,
tile_sample_min_height=256,
tile_sample_min_width=256,
tile_sample_min_length=16,
spatial_tile_overlap_factor: float = 0.25,
temporal_tile_overlap_factor: float = 0,
parallel_group: torch.distributed.ProcessGroup = None,
) -> TileProcessor:
"""
Property representing the tiled encoder or decoder.
Returns:
TileProcessor: The tiled encoder or decoder.
"""
return TileProcessor(
encode_fn=self.encode,
decode_fn=self.decode,
tile_sample_min_height=tile_sample_min_height,
tile_sample_min_width=tile_sample_min_width,
tile_sample_min_length=tile_sample_min_length,
spatial_tile_overlap_factor=spatial_tile_overlap_factor,
temporal_tile_overlap_factor=temporal_tile_overlap_factor,
sr_ratio=getattr(self, 'sr_ratio', 1),
spatial_downsample_factor=self.spatial_downsample_factor,
temporal_downsample_factor=self.temporal_downsample_factor,
first_frame_as_image=self.first_frame_as_image,
parallel_group=parallel_group,
)
@torch.inference_mode()
def tiled_encode_3d(
self,
x,
tile_sample_min_height=256,
tile_sample_min_width=256,
tile_sample_min_length: int = 16,
spatial_tile_overlap_factor: float = 0.25,
temporal_tile_overlap_factor: float = 0,
allow_spatial_tiling: bool = None,
verbose: bool = False,
parallel_group: torch.distributed.ProcessGroup = None,
) -> torch.Tensor:
"""
Encodes the input tensor `x` using tiled encoding.
Args:
x (torch.Tensor shape:[N C T H W]): The input tensor to be encoded.
tile_sample_min_height (int, optional): The minimum height of each tile sample. Defaults to 256.
tile_sample_min_width (int, optional): The minimum width of each tile sample. Defaults to 256.
tile_sample_min_length (int, optional): The minimum length of each tile sample. Defaults to 16.
spatial_tile_overlap_factor (float, optional): Overlap factor for spatial tiles. Defaults to 0.25.
temporal_tile_overlap_factor (float, optional): Overlap factor for temporal tiles. Defaults to 0.
allow_spatial_tiling (bool, optional): Whether spatial tiling is allowed. Defaults to None.
verbose (bool, optional): Whether to print verbose information. Defaults to False.
parallel_group (torch.distributed.ProcessGroup, optional): Distributed encoding group. Defaults to None.
Returns:
torch.Tensor: The encoded tensor.
"""
allow_spatial_tiling = allow_spatial_tiling if allow_spatial_tiling is not None else self.allow_spatial_tiling
if not allow_spatial_tiling:
tile_sample_min_height = 100000
tile_sample_min_width = 100000
return self.tile_processor(
tile_sample_min_height=tile_sample_min_height,
tile_sample_min_width=tile_sample_min_width,
tile_sample_min_length=tile_sample_min_length,
spatial_tile_overlap_factor=spatial_tile_overlap_factor,
temporal_tile_overlap_factor=temporal_tile_overlap_factor,
parallel_group=parallel_group,
).tiled_encode(x, verbose)
@torch.inference_mode()
def tiled_decode_3d(
self,
x,
tile_sample_min_height=256,
tile_sample_min_width=256,
tile_sample_min_length: int = 16,
spatial_tile_overlap_factor: float = 0.25,
temporal_tile_overlap_factor: float = 0,
allow_spatial_tiling: bool = None,
verbose: bool = False,
parallel_group: torch.distributed.ProcessGroup = None,
) -> torch.Tensor:
"""
Decodes the input tensor using the tile autoencoder.
Args:
x (torch.Tensor): The input tensor to be decoded.
tile_sample_min_height (int, optional): The minimum height of each tile sample. Defaults to 256.
tile_sample_min_width (int, optional): The minimum width of each tile sample. Defaults to 256.
tile_sample_min_length (int, optional): The minimum length of each tile sample. Defaults to 16.
spatial_tile_overlap_factor (float, optional): Overlap factor for spatial tiles. Defaults to 0.25.
temporal_tile_overlap_factor (float, optional): Overlap factor for temporal tiles. Defaults to 0.
allow_spatial_tiling (bool, optional): Whether spatial tiling is allowed. Defaults to None.
verbose (bool, optional): Whether to print verbose information. Defaults to False.
parallel_group (torch.distributed.ProcessGroup, optional): Distributed decoding group. Defaults to None.
Returns:
torch.Tensor shape:[N C T H W]: The decoded tensor.
"""
allow_spatial_tiling = allow_spatial_tiling if allow_spatial_tiling is not None else self.allow_spatial_tiling
if not allow_spatial_tiling:
tile_sample_min_height = 100000
tile_sample_min_width = 100000
return self.tile_processor(
tile_sample_min_height=tile_sample_min_height,
tile_sample_min_width=tile_sample_min_width,
tile_sample_min_length=tile_sample_min_length,
spatial_tile_overlap_factor=spatial_tile_overlap_factor,
temporal_tile_overlap_factor=temporal_tile_overlap_factor,
parallel_group=parallel_group,
).tiled_decode(x, verbose)
class ViTVAE(ModelMixin, ConfigMixin, VideoTokenizerABC):
@register_to_config
def __init__(self, ddconfig: dict, model_type: Literal['vit', 'vit_ncthw'] = 'vit'):
super().__init__()
if model_type == 'vit':
self.encoder = ViTEncoder(**ddconfig)
self.decoder = ViTDecoder(**ddconfig)
elif model_type == 'vit_ncthw':
from videotokenizer.modules.vit_ncthw import ViTDecoderNCTHW, ViTEncoderNCTHW
self.encoder = ViTEncoderNCTHW(**ddconfig)
self.decoder = ViTDecoderNCTHW(**ddconfig)
else:
raise ValueError(f"model_type {model_type} not supported")
if 'patch_length' in ddconfig:
self._temporal_downsample_factor = ddconfig['patch_length']
else:
self._temporal_downsample_factor = 1
if 'patch_size' in ddconfig:
self._spatial_downsample_factor = ddconfig['patch_size']
else:
self._spatial_downsample_factor = 8
@property
def spatial_downsample_factor(self):
return self._spatial_downsample_factor
@property
def temporal_downsample_factor(self):
return self._temporal_downsample_factor
def init_from_ckpt(self, path, ignore_keys=list()):
raise NotImplementedError
def encode(self, x, sample_posterior=True):
"""
Encode the input video.
Args:
x (torch.Tensor): Input video tensor has shape N C T H W
Returns:
tuple: Tuple containing the quantized tensor, embedding loss, and additional information.
"""
N, C, T, H, W = x.shape
if T == 1 and self._temporal_downsample_factor > 1:
x = x.expand(-1, -1, 4, -1, -1)
x = self.encoder(x)
posterior = DiagonalGaussianDistribution(x)
if sample_posterior:
z = posterior.sample()
else:
z = posterior.mode()
return z[:, :, :1, :, :].type(x.dtype)
else:
x = self.encoder(x)
posterior = DiagonalGaussianDistribution(x)
if sample_posterior:
z = posterior.sample()
else:
z = posterior.mode()
return z.type(x.dtype)
def decode(self, x):
"""
Decode the quantized tensor.
Args:
quant (torch.Tensor): Quantized tensor.
Returns:
torch.Tensor: Decoded tensor.
"""
N, C, T, H, W = x.shape
if T == 1:
x = x.expand(-1, -1, 1, -1, -1)
x = self.decoder(x)
x = x[:, :, :1, :, :]
return x
else:
x = self.decoder(x)
return x
def forward(self, x, sample_posterior=True):
x = self.encoder(x)
posterior = DiagonalGaussianDistribution(x)
if sample_posterior:
z = posterior.sample()
else:
z = posterior.mode()
dec = self.decoder(z)
return dec, posterior
def get_last_layer(self):
"""
Get the last layer of the decoder.
Returns:
torch.Tensor: Last layer of the decoder.
"""
return self.decoder.last_layer.weight
@property
def allow_spatial_tiling(self):
return False
class AutoModel:
r"""
:class:`~models.AutoModel` is a generic model class
that will be instantiated as one of the base model classes of the library
when created with the `AutoModel.from_pretrained(pretrained_model_name_or_path)`
This class cannot be instantiated using `__init__()` (throws an error).
"""
def __init__(self):
raise EnvironmentError(
"AutoModel is designed to be instantiated "
"using the `AutoModel.from_pretrained(pretrained_model_name_or_path)` method."
)
@classmethod
def from_pretrained(cls, pretrained_model_name_or_path, *args, **kwargs) -> VideoTokenizerABC:
config = os.path.join(pretrained_model_name_or_path, 'config.json')
if not os.path.exists(config):
raise ValueError("Can't find a model config file at {}.".format(config))
# Load config
with open(config, 'r') as json_file:
config_dict = json.load(json_file)
assert config_dict['_class_name'] == 'ViTVAE'
return ViTVAE.from_pretrained(pretrained_model_name_or_path, *args, **kwargs)
|