Collect results in cpu.
(result_part, size, tmpdir=None)
| 84 | |
| 85 | |
| 86 | def collect_results_cpu(result_part, size, tmpdir=None): |
| 87 | """Collect results in cpu.""" |
| 88 | rank, world_size = get_dist_info() |
| 89 | # create a tmp dir if it is not specified |
| 90 | if tmpdir is None: |
| 91 | MAX_LEN = 512 |
| 92 | # 32 is whitespace |
| 93 | dir_tensor = torch.full((MAX_LEN, ), |
| 94 | 32, |
| 95 | dtype=torch.uint8, |
| 96 | device='cuda') |
| 97 | if rank == 0: |
| 98 | mmcv.mkdir_or_exist('.dist_test') |
| 99 | tmpdir = tempfile.mkdtemp(dir='.dist_test') |
| 100 | tmpdir = torch.tensor( |
| 101 | bytearray(tmpdir.encode()), dtype=torch.uint8, device='cuda') |
| 102 | dir_tensor[:len(tmpdir)] = tmpdir |
| 103 | dist.broadcast(dir_tensor, 0) |
| 104 | tmpdir = dir_tensor.cpu().numpy().tobytes().decode().rstrip() |
| 105 | else: |
| 106 | mmcv.mkdir_or_exist(tmpdir) |
| 107 | # dump the part result to the dir |
| 108 | mmcv.dump(result_part, osp.join(tmpdir, f'part_{rank}.pkl')) |
| 109 | dist.barrier() |
| 110 | # collect all parts |
| 111 | if rank != 0: |
| 112 | return None |
| 113 | else: |
| 114 | # load results of all parts from tmp dir |
| 115 | part_list = [] |
| 116 | for i in range(world_size): |
| 117 | part_file = osp.join(tmpdir, f'part_{i}.pkl') |
| 118 | part_result = mmcv.load(part_file) |
| 119 | part_list.append(part_result) |
| 120 | # sort the results |
| 121 | ordered_results = [] |
| 122 | for res in zip(*part_list): |
| 123 | ordered_results.extend(list(res)) |
| 124 | # the dataloader may pad some samples |
| 125 | ordered_results = ordered_results[:size] |
| 126 | # remove tmp dir |
| 127 | shutil.rmtree(tmpdir) |
| 128 | return ordered_results |
| 129 | |
| 130 | |
| 131 | def collect_results_gpu(result_part, size): |
no test coverage detected