Skip to content

Commit 6280ce8

Browse files
committed
fix multiple calls to predict in a row #222
1 parent 04bc742 commit 6280ce8

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

comet/models/base.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@ def predict(
558558
accelerator: str = "auto",
559559
num_workers: int = None,
560560
length_batching: bool = True,
561+
destroy_ddp_workers: bool = True,
561562
) -> Prediction:
562563
"""Method that receives a list of samples (dictionaries with translations,
563564
sources and/or references) and returns segment-level scores, system level score
@@ -579,7 +580,13 @@ def predict(
579580
data. Defaults to None
580581
length_batching (bool): If set to true, reduces padding by sorting samples
581582
by sequence length. Defaults to True.
582-
583+
destroy_ddp_workers (bool): Whether to terminate the additional worker processes
584+
spawned during multi-GPU prediction. When `True` (default), all processes
585+
except the main process (rank 0) are terminated after `predict` completes,
586+
and execution continues only on the main process. When `False`, no worker
587+
processes are terminated, so `predict` can be called multiple times in a
588+
row when using multi-GPU environments, but the user is responsible for
589+
handling any multiprocessing behavior in the calling code.
583590
Return:
584591
Prediction object with `scores`, `system_score` and any metadata returned
585592
by the model.
@@ -665,9 +672,12 @@ def predict(
665672
pred_writer.cleanup()
666673
return predictions
667674

675+
# Destroy or keep the processes after predict
668676
elif gpus > 1 and not trainer.is_global_zero:
669-
# If we are not in the GLOBAL RANK we will return None
670-
exit()
677+
if destroy_ddp_workers:
678+
exit()
679+
else:
680+
return None
671681

672682
scores = torch.cat([pred["scores"] for pred in predictions], dim=0).tolist()
673683
if "metadata" in predictions[0]:

0 commit comments

Comments
 (0)