Skip to content

Commit 50da62e

Browse files
authored
[bugfix] always force spawn instead of fork (#852)
1 parent 4f3e875 commit 50da62e

File tree

3 files changed

+8
-34
lines changed

3 files changed

+8
-34
lines changed

fastvideo/envs.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
FASTVIDEO_TRACE_FUNCTION: int = 0
2222
FASTVIDEO_ATTENTION_BACKEND: str | None = None
2323
FASTVIDEO_ATTENTION_CONFIG: str | None = None
24-
FASTVIDEO_WORKER_MULTIPROC_METHOD: str = "fork"
24+
FASTVIDEO_WORKER_MULTIPROC_METHOD: str = "spawn"
2525
FASTVIDEO_TARGET_DEVICE: str = "cuda"
2626
MAX_JOBS: str | None = None
2727
NVCC_THREADS: str | None = None
@@ -212,9 +212,8 @@ def maybe_convert_int(value: str | None) -> int | None:
212212
os.path.expanduser(os.getenv("FASTVIDEO_ATTENTION_CONFIG", "."))),
213213

214214
# Use dedicated multiprocess context for workers.
215-
# Both spawn and fork work
216215
"FASTVIDEO_WORKER_MULTIPROC_METHOD":
217-
lambda: os.getenv("FASTVIDEO_WORKER_MULTIPROC_METHOD", "fork"),
216+
lambda: os.getenv("FASTVIDEO_WORKER_MULTIPROC_METHOD", "spawn"),
218217

219218
# Enables torch profiler if set. Path to the directory where torch profiler
220219
# traces are saved. Note that it must be an absolute path.

fastvideo/utils.py

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,34 +1119,9 @@ def xpu_is_initialized() -> bool:
11191119
return torch.xpu.is_initialized()
11201120

11211121

1122-
def maybe_force_spawn() -> None:
1123-
"""Check if we need to force the use of the `spawn` multiprocessing start
1124-
method.
1125-
"""
1126-
if os.environ.get("FASTVIDEO_WORKER_MULTIPROC_METHOD") == "spawn":
1127-
return
1128-
1129-
reasons = []
1130-
1131-
from fastvideo.worker.ray_utils import is_in_ray_actor
1132-
if is_in_ray_actor():
1133-
# even if we choose to spawn, we need to pass the ray address
1134-
# to the subprocess so that it knows how to connect to the ray cluster.
1135-
# env vars are inherited by subprocesses, even if we use spawn.
1136-
import ray
1137-
os.environ["RAY_ADDRESS"] = ray.get_runtime_context().gcs_address
1138-
reasons.append("In a Ray actor and can only be spawned")
1139-
1140-
if cuda_is_initialized():
1141-
reasons.append("CUDA is initialized")
1142-
elif xpu_is_initialized():
1143-
reasons.append("XPU is initialized")
1144-
1145-
if reasons:
1146-
logger.warning(
1147-
"We must use the `spawn` multiprocessing start method. "
1148-
"Overriding FASTVIDEO_WORKER_MULTIPROC_METHOD to 'spawn'. "
1149-
"Reasons: %s", "; ".join(reasons))
1122+
def force_spawn() -> None:
1123+
if os.environ.get("FASTVIDEO_WORKER_MULTIPROC_METHOD") == "fork":
1124+
logger.warning("We must use the `spawn` multiprocessing start method.")
11501125
os.environ["FASTVIDEO_WORKER_MULTIPROC_METHOD"] = "spawn"
11511126

11521127

@@ -1157,7 +1132,7 @@ def get_mp_context() -> BaseContext:
11571132
certain conditions, we may enforce spawn and override the value of
11581133
FASTVIDEO_WORKER_MULTIPROC_METHOD.
11591134
"""
1160-
maybe_force_spawn()
1135+
force_spawn()
11611136
mp_method = envs.FASTVIDEO_WORKER_MULTIPROC_METHOD
11621137
return multiprocessing.get_context(mp_method)
11631138

fastvideo/worker/multiproc_executor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from fastvideo.fastvideo_args import FastVideoArgs
2020
from fastvideo.logger import init_logger
2121
from fastvideo.pipelines.pipeline_batch_info import ForwardBatch
22-
from fastvideo.utils import decorate_logs, get_distributed_init_method, get_exception_traceback, get_loopback_ip, get_mp_context, get_open_port, kill_itself_when_parent_died, maybe_force_spawn
22+
from fastvideo.utils import decorate_logs, get_distributed_init_method, get_exception_traceback, get_loopback_ip, get_mp_context, get_open_port, kill_itself_when_parent_died, force_spawn
2323
from fastvideo.worker.executor import Executor
2424
from fastvideo.worker.worker_base import WorkerWrapperBase
2525

@@ -503,4 +503,4 @@ def set_multiproc_executor_envs() -> None:
503503
in a multiprocessing environment. This should be called by the parent
504504
process before worker processes are created"""
505505

506-
maybe_force_spawn()
506+
force_spawn()

0 commit comments

Comments
 (0)