Skip to content

Set pytorch cuda arch list explicitly to get rid of warning. - #44

Merged
hzhangxyz merged 1 commit into
mainfrom
dev/set-cuda-arch
Jun 26, 2025
Merged

Set pytorch cuda arch list explicitly to get rid of warning.#44
hzhangxyz merged 1 commit into
mainfrom
dev/set-cuda-arch

Conversation

@hzhangxyz

@hzhangxyz hzhangxyz commented Jun 17, 2025

Copy link
Copy Markdown
Member

Description

Set pytorch cuda arch list explicitly to get rid of warning.

The pytorch raises warning when compiling before:

.../site-packages/torch/utils/cpp_extension.py:2059: UserWarning: TORCH_CUDA_ARCH_LIST is not set, all archs for visible cards are included for compilation.
If this is not desired, please set os.environ['TORCH_CUDA_ARCH_LIST'].
  warnings.warn(

Checklist:

@hzhangxyz
hzhangxyz marked this pull request as ready for review June 26, 2025 04:18
Copilot AI review requested due to automatic review settings June 26, 2025 04:18

This comment was marked as outdated.

@hzhangxyz
hzhangxyz force-pushed the dev/set-cuda-arch branch from dc9534e to 467b73d Compare June 26, 2025 04:38
@hzhangxyz
hzhangxyz requested review from Copilot and msg-bq and removed request for Copilot June 26, 2025 04:39

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds logic to explicitly set TORCH_CUDA_ARCH_LIST based on available GPU capabilities to remove the warning when building PyTorch extensions.

  • Introduce _set_torch_cuda_arch_list helper to compute and export the CUDA arch list.
  • Invoke this helper at the start of _load_module.
Comments suppressed due to low confidence (1)

qmb/hamiltonian.py:20

  • The new _set_torch_cuda_arch_list logic isn’t covered by existing tests. Adding unit tests to validate behavior for various torch.cuda scenarios (no GPUs, single GPU, multiple GPUs) would help prevent regressions.
    def _set_torch_cuda_arch_list(cls) -> None:

Comment thread qmb/hamiltonian.py
return
if "TORCH_CUDA_ARCH_LIST" in os.environ:
return
supported_sm = [int(arch[3:]) for arch in torch.cuda.get_arch_list() if arch.startswith("sm_")]

Copilot AI Jun 26, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If supported_sm is empty, calling max() will raise a ValueError. Consider guarding against an empty list (e.g. return early) to avoid a crash when no arch entries are found.

Suggested change
supported_sm = [int(arch[3:]) for arch in torch.cuda.get_arch_list() if arch.startswith("sm_")]
supported_sm = [int(arch[3:]) for arch in torch.cuda.get_arch_list() if arch.startswith("sm_")]
if not supported_sm:
return # No supported architectures, exit early

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

torch.cuda.is_available() 之前判断过,不会空

Comment thread qmb/hamiltonian.py
capability = min(max_supported_sm, torch.cuda.get_device_capability(i))
arch = f'{capability[0]}.{capability[1]}'
arch_list.add(arch)
os.environ["TORCH_CUDA_ARCH_LIST"] = ";".join(sorted(arch_list))

Copilot AI Jun 26, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorting the version strings lexicographically can misorder multi-digit majors (e.g. "10.2" vs "9.1"). Use a numeric key (e.g. sorted(arch_list, key=lambda v: tuple(map(int, v.split('.'))))) to ensure correct ascending order.

Suggested change
os.environ["TORCH_CUDA_ARCH_LIST"] = ";".join(sorted(arch_list))
os.environ["TORCH_CUDA_ARCH_LIST"] = ";".join(sorted(arch_list, key=lambda v: tuple(map(int, v.split('.')))))

Copilot uses AI. Check for mistakes.
Comment thread qmb/hamiltonian.py
capability = min(max_supported_sm, torch.cuda.get_device_capability(i))
arch = f'{capability[0]}.{capability[1]}'
arch_list.add(arch)
os.environ["TORCH_CUDA_ARCH_LIST"] = ";".join(sorted(arch_list))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里copilot给的提示是对的吧,不像上一个没必要。师兄的版本号是str,字典序不妥,还得是像(sm // 10, sm % 10)那种拆开的叭。虽然排序无妨

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

确实,这里排序是为了每次运行他们的顺序是一致的,不然无法有效利用缓存。

另外,我这段代码抄的:

https://github.com/pytorch/pytorch/blob/0d01bafc34fc99a0b3e46cbf1ecfd8f97563efa6/torch/utils/cpp_extension.py#L2428-L2444

@hzhangxyz
hzhangxyz merged commit d9fc9c8 into main Jun 26, 2025
2 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants