|
| 1 | +from typing import Any, Callable, Dict, Optional, Tuple |
| 2 | +import torch |
| 3 | +from ..helpers.config_helper import ( |
| 4 | + update_config, |
| 5 | + check_hasattr, |
| 6 | + default_num_hidden_layers as nhl, |
| 7 | +) |
| 8 | + |
| 9 | +__TASK__ = "image-to-video" |
| 10 | + |
| 11 | + |
| 12 | +def reduce_model_config(config: Any) -> Dict[str, Any]: |
| 13 | + """Reduces a model size.""" |
| 14 | + if not hasattr(config, "num_hidden_layers") and not hasattr(config, "num_layers"): |
| 15 | + # We cannot reduce. |
| 16 | + return {} |
| 17 | + check_hasattr(config, ("num_hidden_layers", "num_layers")) |
| 18 | + kwargs = {} |
| 19 | + if hasattr(config, "num_layers"): |
| 20 | + kwargs["num_layers"] = min(config.num_layers, nhl()) |
| 21 | + if hasattr(config, "num_hidden_layers"): |
| 22 | + kwargs["num_hidden_layers"] = min(config.num_hidden_layers, nhl()) |
| 23 | + |
| 24 | + update_config(config, kwargs) |
| 25 | + return kwargs |
| 26 | + |
| 27 | + |
| 28 | +def get_inputs( |
| 29 | + model: torch.nn.Module, |
| 30 | + config: Optional[Any], |
| 31 | + text_embed_dim: int, |
| 32 | + latent_channels: int, |
| 33 | + batch_size: int = 2, |
| 34 | + image_height: int = 704, |
| 35 | + image_width: int = 1280, |
| 36 | + latent_frames: int = 1, |
| 37 | + text_maxlen: int = 512, |
| 38 | + add_second_input: int = 1, |
| 39 | + **kwargs, # unused |
| 40 | +): |
| 41 | + """ |
| 42 | + Generates inputs for task ``image-to-video``. |
| 43 | + """ |
| 44 | + assert ( |
| 45 | + "cls_cache" not in kwargs |
| 46 | + ), f"Not yet implemented for cls_cache={kwargs['cls_cache']!r}." |
| 47 | + latent_height = image_height // 8 |
| 48 | + latent_width = image_width // 8 |
| 49 | + dtype = torch.float32 |
| 50 | + |
| 51 | + inputs = dict( |
| 52 | + hidden_states=torch.randn( |
| 53 | + batch_size, |
| 54 | + latent_channels, |
| 55 | + latent_frames, |
| 56 | + latent_height, |
| 57 | + latent_width, |
| 58 | + dtype=dtype, |
| 59 | + ), |
| 60 | + timestep=torch.tensor([1.0] * batch_size, dtype=dtype), |
| 61 | + encoder_hidden_states=torch.randn( |
| 62 | + batch_size, text_maxlen, text_embed_dim, dtype=dtype |
| 63 | + ), |
| 64 | + padding_mask=torch.ones(1, 1, image_height, image_width, dtype=dtype), |
| 65 | + fps=torch.tensor([16] * batch_size, dtype=dtype), |
| 66 | + condition_mask=torch.randn( |
| 67 | + batch_size, 1, latent_frames, latent_height, latent_width, dtype=dtype |
| 68 | + ), |
| 69 | + ) |
| 70 | + shapes = dict( |
| 71 | + hidden_states={ |
| 72 | + 0: "batch_size", |
| 73 | + 2: "latent_frames", |
| 74 | + 3: "latent_height", |
| 75 | + 4: "latent_width", |
| 76 | + }, |
| 77 | + timestep={0: "batch_size"}, |
| 78 | + encoder_hidden_states={0: "batch_size"}, |
| 79 | + padding_mask={0: "batch_size", 2: "height", 3: "width"}, |
| 80 | + fps={0: "batch_size"}, |
| 81 | + condition_mask={ |
| 82 | + 0: "batch_size", |
| 83 | + 2: "latent_frames", |
| 84 | + 3: "latent_height", |
| 85 | + 4: "latent_width", |
| 86 | + }, |
| 87 | + ) |
| 88 | + res = dict(inputs=inputs, dynamic_shapes=shapes) |
| 89 | + |
| 90 | + if add_second_input: |
| 91 | + assert ( |
| 92 | + add_second_input > 0 |
| 93 | + ), f"Not implemented for add_second_input={add_second_input}." |
| 94 | + res["inputs2"] = get_inputs( |
| 95 | + model=model, |
| 96 | + config=config, |
| 97 | + text_embed_dim=text_embed_dim, |
| 98 | + latent_channels=latent_channels, |
| 99 | + batch_size=batch_size, |
| 100 | + image_height=image_height, |
| 101 | + image_width=image_width, |
| 102 | + latent_frames=latent_frames, |
| 103 | + text_maxlen=text_maxlen, |
| 104 | + add_second_input=0, |
| 105 | + **kwargs, |
| 106 | + )["inputs"] |
| 107 | + return res |
| 108 | + |
| 109 | + |
| 110 | +def random_input_kwargs(config: Any) -> Tuple[Dict[str, Any], Callable]: |
| 111 | + """ |
| 112 | + Inputs kwargs. |
| 113 | +
|
| 114 | + If the configuration is None, the function selects typical dimensions. |
| 115 | + """ |
| 116 | + if config is not None: |
| 117 | + check_hasattr(config, "in_channels", "text_embed_dim"), |
| 118 | + kwargs = dict( |
| 119 | + text_embed_dim=1024 if config is None else config.text_embed_dim, |
| 120 | + latent_channels=16 if config is None else config.in_channels - 1, |
| 121 | + batch_size=1, |
| 122 | + image_height=8 * 50, |
| 123 | + image_width=8 * 80, |
| 124 | + latent_frames=1, |
| 125 | + text_maxlen=512, |
| 126 | + ) |
| 127 | + return kwargs, get_inputs |
0 commit comments