selectively load ICT models for indexing/retrieving from ICT or REALM checkpoints
(model, only_query_model=False, only_block_model=False, from_realm_chkpt=False)
| 311 | |
| 312 | |
| 313 | def load_ict_checkpoint(model, only_query_model=False, only_block_model=False, from_realm_chkpt=False): |
| 314 | """selectively load ICT models for indexing/retrieving from ICT or REALM checkpoints""" |
| 315 | |
| 316 | args = get_args() |
| 317 | |
| 318 | if isinstance(model, torchDDP): |
| 319 | model = model.module |
| 320 | |
| 321 | load_path = args.load if from_realm_chkpt else args.ict_load |
| 322 | |
| 323 | tracker_filename = get_checkpoint_tracker_filename(load_path) |
| 324 | with open(tracker_filename, 'r') as f: |
| 325 | iteration = int(f.read().strip()) |
| 326 | |
| 327 | # assert iteration > 0 |
| 328 | checkpoint_name = get_checkpoint_name(load_path, iteration, False) |
| 329 | if mpu.get_data_parallel_rank() == 0: |
| 330 | print('global rank {} is loading checkpoint {}'.format( |
| 331 | torch.distributed.get_rank(), checkpoint_name)) |
| 332 | |
| 333 | state_dict = torch.load(checkpoint_name, map_location='cpu') |
| 334 | ict_state_dict = state_dict['model'] |
| 335 | if from_realm_chkpt and mpu.get_data_parallel_rank() == 0: |
| 336 | print(" loading ICT state dict from REALM", flush=True) |
| 337 | ict_state_dict = ict_state_dict['retriever']['ict_model'] |
| 338 | |
| 339 | if only_query_model: |
| 340 | ict_state_dict.pop('context_model') |
| 341 | if only_block_model: |
| 342 | ict_state_dict.pop('question_model') |
| 343 | |
| 344 | model.load_state_dict(ict_state_dict) |
| 345 | torch.distributed.barrier() |
| 346 | |
| 347 | if mpu.get_data_parallel_rank() == 0: |
| 348 | print(' successfully loaded {}'.format(checkpoint_name)) |
| 349 | |
| 350 | return model |
no test coverage detected