Default weight loader
(fd_config: FDConfig = None)
| 309 | |
| 310 | |
| 311 | def default_weight_loader(fd_config: FDConfig = None) -> None: |
| 312 | """Default weight loader""" |
| 313 | |
| 314 | def fn(param, loaded_weight, shard_id: Optional[Union[int, str]] = None): |
| 315 | """fn""" |
| 316 | output_dim = getattr(param, "output_dim", None) |
| 317 | weight_need_transpose = getattr(param, "weight_need_transpose", False) |
| 318 | if weight_need_transpose: |
| 319 | loaded_weight = loaded_weight.transpose([1, 0]) |
| 320 | # Tensor parallelism splits the weight along the output_dim |
| 321 | if ( |
| 322 | output_dim is not None |
| 323 | and fd_config is not None |
| 324 | and fd_config.parallel_config.tensor_parallel_size > 1 |
| 325 | and not fd_config.load_config.is_pre_sharded |
| 326 | ): |
| 327 | dim = -1 if output_dim else 0 |
| 328 | if isinstance(loaded_weight, paddle.Tensor): |
| 329 | size = loaded_weight.shape[dim] |
| 330 | else: |
| 331 | size = loaded_weight.get_shape()[dim] |
| 332 | block_size = size // fd_config.parallel_config.tensor_parallel_size |
| 333 | shard_offset = fd_config.parallel_config.tensor_parallel_rank * block_size |
| 334 | shard_size = (fd_config.parallel_config.tensor_parallel_rank + 1) * block_size |
| 335 | loaded_weight = slice_fn(loaded_weight, output_dim, shard_offset, shard_size) |
| 336 | |
| 337 | tp_row_bias = getattr(param, "tp_row_bias", None) |
| 338 | if tp_row_bias: |
| 339 | loaded_weight = loaded_weight / fd_config.parallel_config.tensor_parallel_size |
| 340 | |
| 341 | # mlp.gate.weight is precision-sensitive, so we cast it to float32 for computation |
| 342 | loaded_weight = fd_cast(loaded_weight, param) |
| 343 | if param.shape != loaded_weight.shape: |
| 344 | # for e_score_correction_bias |
| 345 | loaded_weight = loaded_weight.reshape(param.shape) |
| 346 | assert param.shape == loaded_weight.shape, ( |
| 347 | f" Attempted to load weight ({loaded_weight.shape}) " f"into parameter ({param.shape})" |
| 348 | ) |
| 349 | loaded_weight = get_tensor(loaded_weight) |
| 350 | param.copy_(loaded_weight, False) |
| 351 | |
| 352 | return fn |
| 353 | |
| 354 | |
| 355 | def is_pre_sliced_weight(model_path): |
no outgoing calls
no test coverage detected