Set pytorch cuda arch list explicitly to get rid of warning. - #44
Conversation
dc9534e to
467b73d
Compare
There was a problem hiding this comment.
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_listhelper 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_listlogic isn’t covered by existing tests. Adding unit tests to validate behavior for varioustorch.cudascenarios (no GPUs, single GPU, multiple GPUs) would help prevent regressions.
def _set_torch_cuda_arch_list(cls) -> None:
| 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_")] |
There was a problem hiding this comment.
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.
| 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 |
There was a problem hiding this comment.
torch.cuda.is_available() 之前判断过,不会空
| 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)) |
There was a problem hiding this comment.
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.
| 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('.'))))) |
| 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)) |
There was a problem hiding this comment.
这里copilot给的提示是对的吧,不像上一个没必要。师兄的版本号是str,字典序不妥,还得是像(sm // 10, sm % 10)那种拆开的叭。虽然排序无妨
There was a problem hiding this comment.
确实,这里排序是为了每次运行他们的顺序是一致的,不然无法有效利用缓存。
另外,我这段代码抄的:
Description
Set pytorch cuda arch list explicitly to get rid of warning.
The pytorch raises warning when compiling before:
Checklist: