cuStateVecEx backend changes the current CUDA device without restoring it
Maintainer thường phản hồi trong vòng 2 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 56/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- cpp
- Lĩnh vực
- backend, performance
Hướng nghiên cứu
Read lib/vectorspace_custatevecex.h and lib/simulator_custatevecex.h, focusing on the listed cudaSetDevice calls in Vector::assign, Vector::reduce, and ExpectationValue. Check how the scoped guard in pybind_interface/pybind_main.cpp handles device restoration, then reproduce on a machine with at least two visible GPUs using the supplied Python snippet. Done means the caller's current CUDA device is unchanged after these operations, including the multi GPU paths.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Describe the issue
qsim's cuStateVecEx code calls cudaSetDevice in nine places and never saves or restores the caller's current device. There is no cudaGetDevice anywhere in lib/, so whichever GPU was selected last stays selected after the call returns.
lib/vectorspace_custatevecex.h:lib/simulator_custatevecex.h: the callback inExpectationValue, L203
When the switch happens.
- The eight calls in
vectorspace_custatevecex.honly run when the state is split across several GPUs or MPI processes. With a single sub-state, the helpers call the callback without switching (L206, L253, L295, L346). - The call in
ExpectationValueruns every time, even on a single GPU.
Default options trigger it on any machine with two or more visible GPUs. Parameter::num_devices defaults to 0 (L64), so for all but the smallest circuits Create splits the state across the visible GPUs (L412, L443-L455). A plain gpu_mode=2 simulation then runs init_state → SetAllZeros → Vector::assign (pybind_main.cpp L633-L637, statespace_custatevecex.h L86-L97) and returns with the last GPU selected.
Why it matters. Each thread has a current CUDA device that Python code and other libraries rely on. After a gpu_mode=2 simulation, anything that uses the default device silently runs on a different GPU. That includes CuPy, PyTorch, and qsim's own gpu_mode=0/1 backends, which allocate on the current device.
While testing #1100 on 2× A100, before that PR added a workaround, this became a crash. Reading a gpu_mode=0 state that lives on device 0 launched its reorder kernel on device 1:
[FAIL] mode 0: a cuStateVecEx simulation leaves the current device unchanged -- current device 0 -> 1
CUDA error: an illegal memory access was encountered at .../lib/statespace_cuda.h 94
Possible fix. Save the current device on entry and restore it on exit in the four Vector::assign/reduce helpers and in SimulatorCuStateVecEx::ExpectationValue. For example, with a small RAII helper (sketch):
struct CudaDeviceRestorer {
int device = -1;
CudaDeviceRestorer() { if (cudaGetDevice(&device) != cudaSuccess) device = -1; }
~CudaDeviceRestorer() { if (device >= 0) cudaSetDevice(device); }
};
#1100 works around this in the Python bindings with a scoped guard (ScopedCudaDevice in pybind_interface/pybind_main.cpp). Code that uses lib/ directly is still affected.
Tell us how to reproduce the issue
You need a machine with at least two visible GPUs and qsimcirq built with cuQuantum, so that gpu_mode=2 is available.
import cirq
import cupy as cp
import qsimcirq
qubits = cirq.LineQubit.range(5)
circuit = cirq.Circuit(cirq.H.on_each(*qubits))
before = cp.cuda.runtime.getDevice()
qsimcirq.QSimSimulator(qsimcirq.QSimOptions(use_gpu=True, gpu_mode=2)).simulate(circuit)
after = cp.cuda.runtime.getDevice()
print(before, "->", after)
Expected: 0 -> 0. Observed on 2× A100 in an equivalent test: 0 -> 1.
With only one visible GPU there is nothing to switch to, so this only shows up on multi-GPU machines.
Tell us the version of qsim or qsimcirq (if relevant)
main @ 50cac4b (qsimcirq 0.23.0.dev0); the cudaSetDevice calls were added with cuStateVecEx support in #965
Tell us the computing environment (if relevant)
Linux (Ubuntu 22.04 container), 2x NVIDIA A100, CUDA 12.4.1, cuStateVec 1.11.0 (custatevec-cu12), Python 3.10
- Ngôn ngữ chính
- C++
- Star
- 702
- Fork
- 219
- Merge trung bình
- 1 ngày 2 giờ
- Pull request đã merge (30 ngày)
- 8
Chuẩn bị môi trường
- Có Dockerfile hoặc tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của quantumlib/qsim
-
`MPSStateSpace::Sample` accesses results out of bounds when the output vector is emptyCó thể đã có người làm @aadityarshah đã nhận 42 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
quantumlib/qsim#1126 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 57/100
quantumlib/qsim#1146 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
`MPSStateSpace::SampleOnce` crashes for two-qubit statesCó thể đã có người làm @aadityarshah đã nhận 40 ngày trước. Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 58/100
quantumlib/qsim#1129 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Implement expectation values for the MPS SimulatorCó thể đã có người làm @rupasri118 đã nhận 36 ngày trước. Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 55/100
quantumlib/qsim#1122 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 38/100
quantumlib/qsim#1121 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
Tất cả issue của quantumlib/qsim
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
Maintainer thường phản hồi trong vòng 1 ngày
-
ai_p1 comp-sql-syntax
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
ClickHouse/ClickHouse#133888 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Maintainer thường phản hồi trong vòng 1 ngày
-
documentation good first issue
Độ khó 2/5 Nửa ngày Mức phù hợp với người mới 66/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
jbcoe/cc-protocol#507 ·
Maintainer thường phản hồi trong vòng 1 ngày