selectively load retrieval models for indexing/retrieving from saved checkpoints
(
model, only_query_model=False, only_context_model=False, custom_load_path=None
)
| 506 | |
| 507 | |
| 508 | def load_biencoder_checkpoint( |
| 509 | model, only_query_model=False, only_context_model=False, custom_load_path=None |
| 510 | ): |
| 511 | """ |
| 512 | selectively load retrieval models for indexing/retrieving |
| 513 | from saved checkpoints |
| 514 | """ |
| 515 | |
| 516 | args = get_args() |
| 517 | |
| 518 | model = utils.unwrap_model(model) |
| 519 | |
| 520 | load_path = custom_load_path if custom_load_path is not None else args.load |
| 521 | |
| 522 | tracker_filename = get_checkpoint_tracker_filename(load_path) |
| 523 | with open(tracker_filename, "r") as f: |
| 524 | iteration = int(f.read().strip()) |
| 525 | |
| 526 | checkpoint_name = get_checkpoint_name(load_path, iteration, False) |
| 527 | if mpu.get_data_parallel_rank() == 0: |
| 528 | print( |
| 529 | "global rank {} is loading checkpoint {}".format( |
| 530 | torch.distributed.get_rank(), checkpoint_name |
| 531 | ) |
| 532 | ) |
| 533 | |
| 534 | state_dict = torch.load(checkpoint_name, map_location="cpu") |
| 535 | ret_state_dict = state_dict["model"] |
| 536 | |
| 537 | if only_query_model: |
| 538 | ret_state_dict.pop("context_model") |
| 539 | if only_context_model: |
| 540 | ret_state_dict.pop("query_model") |
| 541 | |
| 542 | assert len(model) == 1 |
| 543 | model[0].load_state_dict(ret_state_dict) |
| 544 | torch.distributed.barrier() |
| 545 | |
| 546 | if mpu.get_data_parallel_rank() == 0: |
| 547 | print(" successfully loaded {}".format(checkpoint_name)) |
| 548 | |
| 549 | return model |
nothing calls this directly
no test coverage detected