should pytest --forked be supported for GPU unit test runs?

未关闭
#9,075 0 条评论 2 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 2 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
18/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
github-actions, python
领域
testing

调研方向

Start by running pytest --forked tests/unit/ on a GPU host to reproduce the Cannot re-initialize CUDA in forked subprocess error, then check which modules touch the device at import, beginning with tests/unit/simple_model.py and tests/unit/v1/half_precision/test_keep_in_fp32_modules.py. Done means maintainers choose between supporting --forked on GPU (with a collection-time CUDA check in tests/conftest.py) or updating CONTRIBUTING.md, so docs and tests agree. The decision has to come before implementation.

由索引模型根据 Issue 内容生成。

描述

CONTRIBUTING.md tells contributors to run pytest --forked tests/unit/. It says --forked is required to test
CUDA functionality in distributed tests, and that it is safe because import deepspeed no longer initializes CUDA
(#8078, which followed #7918). On a GPU host, that command does not work on master (3fe2d9508):

  • Collecting tests/unit initializes CUDA in the pytest process; 77 test modules do it.
    • Most go through tests/unit/simple_model.py, whose dtype=preferred_dtype() default argument (#5206) queries
      the device at import.
    • The others are boolean skip conditions such as
      @pytest.mark.skipif(not get_accelerator().is_fp16_supported(), ...), module-level pytest.skip() checks, and
      qtorch's JIT build when test_fp_quant.py is imported (when TORCH_CUDA_ARCH_LIST is unset).
  • After that, any test that pytest-forked runs in a forked child and that uses CUDA fails with
    RuntimeError: Cannot re-initialize CUDA in forked subprocess. DistributedTests are not affected, because their
    ranks start from a forkserver (#2141).
  • CI cannot notice. GPU CI (aws-torch-latest, aws-torch-latest-full, modal-torch-latest) runs without
    --forked. CPU CI runs with --forked, but it cannot initialize CUDA.
  • New cases keep arriving: #8843 recently added 7 more import-time skip conditions to
    tests/unit/v1/half_precision/test_keep_in_fp32_modules.py.
  • A check limited to one module does not protect the session.
    test_chunked_cross_entropy.py::test_collection_leaves_cuda_initialization_fork_safe (#8653) checks that importing
    its own module leaves CUDA fork-safe. In the failing run below it passes, while 18 other tests in the same file fail
    because a different module initialized CUDA.

So the documented command is broken on GPU, and the question in #7918 ("It seems the opposite is true: the flag must not be used") is still open in practice.

Describe the solution you'd like

A decision between these two options, so the docs and the tests agree:

  1. Support --forked on GPU hosts. Keep test collection free of device queries, and enforce it.

    • Write device-dependent skip conditions as strings, which pytest evaluates when each test runs, for example
      @pytest.mark.skipif("not get_accelerator().is_fp16_supported()", ...). A pytest_markeval_namespace hook in
      tests/conftest.py provides get_accelerator.
    • Resolve device-dependent defaults and configs inside the tests, and import modules that build kernels at import
      (qtorch) when they are first used.
    • Add a check in tests/conftest.py that fails tests when CUDA was initialized during collection.
      aws-torch-latest runs tests/unit/v1 on GPU for every PR, so it would catch new cases there; the nightly full
      run covers the rest of tests/unit.

    I have a branch that does the first two points.

  2. Do not support --forked on GPU hosts. Update CONTRIBUTING.md instead:

    • --forked is not needed for distributed tests, because their ranks start from a forkserver.
    • GPU runs use plain pytest, optionally with -n.
    • --forked stays for CPU runs.

    No test changes are needed.

Describe alternatives you've considered

  • Keeping the current state: the docs promise a command that fails on GPU, and nothing in CI checks it.
  • Fixing individual tests without a check: it does not last, as #8843 shows.

Additional context

Trade-offs for option 1:

  • Benefit: each test runs in its own process. State such as globals, torch settings, compile caches or an initialized
    CUDA context cannot leak between tests or make results depend on test order.
  • Cost: --forked is slower. On 4x H200 (-n 4), about 920 tests took 58m52s with --forked and 47m36s without.
    Triton-heavy files are hit hardest: test_autoep_expert_activation.py took 342 s with --forked and 25 s without.
  • Contributor burden: skip conditions that query the device must be strings, and nothing may touch the device at
    import time. The conftest.py check would make violations fail with a message that names the cause.

This analysis was done with AI assistance.

主要语言
Python
星标
43.3k
派生
5.1k
平均合并
5 天 10 小时
30 天内合并 PR
85

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

deepspeedai/DeepSpeed 的其他 Issue

查看 deepspeedai/DeepSpeed 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。