MCPcopy
hub / github.com/alibaba/EasyCV / multi_gpu_test

Function multi_gpu_test

easycv/apis/test.py:155–249  ·  view source on GitHub ↗

Test model with multiple gpus. This method tests model with multiple gpus and collects the results under two different modes: gpu and cpu modes. By setting 'gpu_collect=True' it encodes results to gpu tensors and use gpu communication for results collection. On cpu mode it saves the

(model,
                   data_loader,
                   mode='test',
                   tmpdir=None,
                   gpu_collect=False,
                   use_fp16=False,
                   **kwargs)

Source from the content-addressed store, hash-verified

153
154
155def multi_gpu_test(model,
156 data_loader,
157 mode='test',
158 tmpdir=None,
159 gpu_collect=False,
160 use_fp16=False,
161 **kwargs):
162 """Test model with multiple gpus.
163
164 This method tests model with multiple gpus and collects the results
165 under two different modes: gpu and cpu modes. By setting 'gpu_collect=True'
166 it encodes results to gpu tensors and use gpu communication for results
167 collection. On cpu mode it saves the results on different gpus to 'tmpdir'
168 and collects them by the rank 0 worker.
169
170 Args:
171 model (nn.Module): Model to be tested.
172 data_loader (nn.Dataloader): Pytorch data loader.
173 model (str): mode for model to forward
174 tmpdir (str): Path of directory to save the temporary results from
175 different gpus under cpu mode.
176 gpu_collect (bool): Option to use either gpu or cpu to collect results.
177 use_fp16: Use fp16 inference
178
179 Returns:
180 list: The prediction results.
181 """
182 if use_fp16:
183 device = next(model.parameters()).device
184 assert device.type == 'cuda', 'fp16 can only be used in gpu, model is placed on cpu'
185 model.half()
186
187 model.eval()
188 results = {}
189 rank, world_size = get_dist_info()
190
191 if hasattr(data_loader, 'dataset'): # normal dataloader
192 data_len = len(data_loader.dataset)
193 else:
194 data_len = len(data_loader) * data_loader.batch_size * world_size
195
196 if rank == 0:
197 prog_bar = mmcv.ProgressBar(data_len)
198 time.sleep(2) # This line can prevent deadlock problem in some cases.
199
200 for i, data in enumerate(data_loader):
201 with torch.no_grad():
202 result = model(**data, mode=mode)
203 # # encode mask results
204 # if isinstance(result, tuple):
205 # bbox_results, mask_results = result
206 # encoded_mask_results = encode_mask_results(mask_results)
207 # result = bbox_results, encoded_mask_results
208
209 for k, v in result.items():
210 if k not in results:
211 results[k] = []
212

Callers 2

mainFunction · 0.90
after_train_epochMethod · 0.90

Calls 6

ValueErrorClass · 0.90
collect_results_gpuFunction · 0.85
collect_results_cpuFunction · 0.85
sizeMethod · 0.45
updateMethod · 0.45
catMethod · 0.45

Tested by

no test coverage detected