(cfg: DictConfig, new_model: nn.Module)
| 333 | |
| 334 | |
| 335 | def init_from_checkpoint(cfg: DictConfig, new_model: nn.Module): |
| 336 | print(f"Initializing model from checkpoint {cfg.checkpoint_run_name}") |
| 337 | checkpoint_cfg = Path(cfg.checkpoint_cfg) |
| 338 | assert checkpoint_cfg.exists(), f"Checkpoint config {checkpoint_cfg} does not exist" |
| 339 | pretrained_cfg = om.load(checkpoint_cfg) |
| 340 | |
| 341 | pretrained_model = build_model(pretrained_cfg.model) |
| 342 | n_params = sum(p.numel() for p in pretrained_model.parameters()) |
| 343 | |
| 344 | checkpoint_filepath = Path(cfg.checkpoint_load_path) / f"{cfg.checkpoint_run_name}" / "latest-rank0.pt" |
| 345 | assert checkpoint_filepath.exists(), f"Checkpoint {checkpoint_filepath} does not exist" |
| 346 | state = torch.load(_ensure_valid_checkpoint(checkpoint_filepath), map_location="cpu") |
| 347 | |
| 348 | state_dict = state.get("state", {}) |
| 349 | model_state = state_dict.get("model", {}) |
| 350 | assert len(model_state) > 0, "Model state is empty, please check the checkpoint and checkpoint path" |
| 351 | |
| 352 | pretrained_model.load_state_dict(model_state) |
| 353 | |
| 354 | if isinstance(pretrained_cfg.model.model_config, DictConfig): |
| 355 | model_config = OmegaConf.to_container(pretrained_cfg.model.model_config, resolve=True) |
| 356 | pretrained_config = FlexBertConfig.from_pretrained(pretrained_cfg.model.pretrained_model_name, **model_config) |
| 357 | |
| 358 | init_mlm_model_from_pretrained( |
| 359 | config=pretrained_config, |
| 360 | pretrained_model=pretrained_model.model, |
| 361 | new_model=new_model.model, |
| 362 | mode=cfg.get("mode", "tile_weights_from_middle"), |
| 363 | ) |
| 364 | print(f"Initalized model from checkpoint {cfg.checkpoint_run_name} with {n_params=:.4e} parameters") |
| 365 | |
| 366 | |
| 367 | def main(cfg: DictConfig, return_trainer: bool = False, do_train: bool = True) -> Optional[Trainer]: |
no test coverage detected