Skip to content

Fork safety #7918

Closed
Closed

Description

@Flamefire

It looks like deepspeed is not fork-safe:

  • During import it checks builder.is_compatible() for all ops
  • Some ops use torch.cuda.get_device_properties
  • This initializes the CUDA context

See

compatible_ops[op_name] = op_compatible

When the process then forks to run in parallel any access to CUDA through PyTorch will fail:

RuntimeError: Cannot re-initialize CUDA in forked subprocess. To use CUDA with multiprocessing, you must use the 'spawn' start method

I'm not sure why this doesn't seem to be fully consistent but I can reproduce it with:
pytest --forked tests/unit/ops/deepspeed4science/test_DS4Sci_EvoformerAttention.py -k 'test_DS4Sci_EvoformerAttention[tensor_shape1-dtype1]' -s

Where it will fail when invoking skip_on_arch(8 if dtype == torch.bfloat16 else 7) which calls torch.cuda.get_device_properties, now in the forked subprocess.

This is an issue in general: Fork-multiprocessing can not be used after importing deepspeed

But it also contradicts the documentation:

Note that pytest-forked and the --forked flag are required to test CUDA functionality in distributed tests.

It seems the opposite is true: The flag must not be used.

Or am I missing anything?

Activity

  1. vibeyclaw commented on Mar 29, 2026

    @vibeyclaw

    Ran into similar CUDA fork issues before. The root cause is clear from your trace: any torch.cuda.* call during import locks the CUDA context to the parent process, making fork() unusable.

    One workaround that helped us: lazy-wrap the compatibility checks so they only fire when the op is actually used, not at import time. Something like caching is_compatible() behind a property that defers the CUDA call.

    For the test suite specifically, switching pytest to --forked with mp.set_start_method("spawn") in conftest avoids it, though that is more of a band-aid.

    Might be worth checking if CUDA_VISIBLE_DEVICES="" during import + deferred re-check could work as a less invasive fix.

  2. Achyuthan-S commented on Jun 19, 2026

    @Achyuthan-S
    Contributor

    Thanks for the detailed report and repro — I opened #8078 to fix this. The fix gates the get_device_properties probe on torch.cuda.is_initialized() so import deepspeed no longer creates a CUDA context. Would appreciate a look if you have a GPU handy to confirm the --forked Evoformer test passes on your setup.

  3. added a commit that references this issue on Jun 29, 2026
    5aef6d8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions