fsdp_load
¶
Functions¶
fastvideo.models.loader.fsdp_load.load_model_from_full_model_state_dict
¶
load_model_from_full_model_state_dict(model: FSDPModule | Module, full_sd_iterator: Generator[tuple[str, Tensor], None, None], device: device, param_dtype: dtype, strict: bool = False, cpu_offload: bool = False, param_names_mapping: Callable[[str], tuple[str, Any, Any]] | None = None, training_mode: bool = True) -> _IncompatibleKeys
Converting full state dict into a sharded state dict
and loading it into FSDP model (if training) or normal huggingface model
Args:
model (Union[FSDPModule, torch.nn.Module]): Model to generate fully qualified names for cpu_state_dict
full_sd_iterator (Generator): an iterator yielding (param_name, tensor) pairs
device (torch.device): device used to move full state dict tensors
param_dtype (torch.dtype): dtype used to move full state dict tensors
strict (bool): flag to check if to load the model in strict mode
cpu_offload (bool): flag to check if FSDP offload is enabled
param_names_mapping (Optional[Callable[[str], str]]): a function that maps full param name to sharded param name
training_mode (bool): apply FSDP only for training
Returns:
NamedTuple with missing_keys and unexpected_keys fields:
* missing_keys is a list of str containing the missing keys
* unexpected_keys is a list of str containing the unexpected keys
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If got FSDP with more than 1D. |
Source code in fastvideo/models/loader/fsdp_load.py
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 | |
fastvideo.models.loader.fsdp_load.maybe_load_fsdp_model
¶
maybe_load_fsdp_model(model_cls: type[Module], init_params: dict[str, Any], weight_dir_list: list[str], device: device, hsdp_replicate_dim: int, hsdp_shard_dim: int, default_dtype: dtype, param_dtype: dtype, reduce_dtype: dtype, cpu_offload: bool = False, fsdp_inference: bool = False, output_dtype: dtype | None = None, training_mode: bool = True, pin_cpu_memory: bool = True, enable_torch_compile: bool = False, torch_compile_kwargs: dict[str, Any] | None = None) -> Module
Load the model with FSDP if is training, else load the model without FSDP.
Source code in fastvideo/models/loader/fsdp_load.py
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 | |
fastvideo.models.loader.fsdp_load.set_default_dtype
¶
set_default_dtype(dtype: dtype) -> Generator[None, None, None]
Context manager to set torch's default dtype.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dtype
|
dtype
|
The desired default dtype inside the context manager. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ContextManager |
None
|
context manager for setting default dtype. |
Example
with set_default_dtype(torch.bfloat16): x = torch.tensor([1, 2, 3]) x.dtype torch.bfloat16
Source code in fastvideo/models/loader/fsdp_load.py
fastvideo.models.loader.fsdp_load.shard_model
¶
shard_model(model, *, cpu_offload: bool, reshard_after_forward: bool = True, mp_policy: MixedPrecisionPolicy | None = MixedPrecisionPolicy(), mesh: DeviceMesh | None = None, fsdp_shard_conditions: list[Callable[[str, Module], bool]] = [], pin_cpu_memory: bool = True) -> None
Utility to shard a model with FSDP using the PyTorch Distributed fully_shard API.
This method will over the model's named modules from the bottom-up and apply shard modules based on whether they meet any of the criteria from shard_conditions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
TransformerDecoder
|
Model to shard with FSDP. |
required |
shard_conditions
|
List[Callable[[str, Module], bool]]
|
A list of functions to determine which modules to shard with FSDP. Each function should take module name (relative to root) and the module itself, returning True if FSDP should shard the module and False otherwise. If any of shard_conditions return True for a given module, it will be sharded by FSDP. |
required |
cpu_offload
|
bool
|
If set to True, FSDP will offload parameters, gradients, and optimizer states to CPU. |
required |
reshard_after_forward
|
bool
|
Whether to reshard parameters and buffers after the forward pass. Setting this to True corresponds to the FULL_SHARD sharding strategy from FSDP1, while setting it to False corresponds to the SHARD_GRAD_OP sharding strategy. |
True
|
mesh
|
Optional[DeviceMesh]
|
Device mesh to use for FSDP sharding under multiple parallelism. Default to None. |
None
|
fsdp_shard_conditions
|
List[Callable[[str, Module], bool]]
|
A list of functions to determine which modules to shard with FSDP. |
[]
|
pin_cpu_memory
|
bool
|
If set to True, FSDP will pin the CPU memory of the offloaded parameters. |
True
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If no layer modules were sharded, indicating that no shard_condition was triggered. |
Source code in fastvideo/models/loader/fsdp_load.py
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 | |