cuStateVecEx Vector move assignment leaks the state vector it overwrites
I maintainer di solito rispondono entro 2 giorni
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 57/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Stack tecnologico
- cpp
- Ambito
- backend, performance
Direzione di ricerca
Start with lib/vectorspace_custatevecex.h, especially Vector::operator=(Vector&&) and its destructor, to trace ownership of the cuStateVecEx descriptor. The issue includes a CUDA reproducer, repro_issue2_leak.cu; build and run it with the stated qsim and cuQuantum paths to check memory before and after assignment. Done when overwriting a live vector releases its old device allocation and the reproducer returns memory after cleanup.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Describe the issue
VectorSpaceCuStateVecEx::Vector::operator=(Vector&&) (lib/vectorspace_custatevecex.h L108-L125) copies r.ptr_ into ptr_ without first destroying the state-vector descriptor that ptr_ already owns:
Vector& operator=(Vector&& r) {
if (this != &r) {
mp_ = r.mp_;
ptr_ = r.ptr_; // the old ptr_ is overwritten without custatevecExStateVectorDestroy
...
The destructor (L102-L106) is the only place that calls custatevecExStateVectorDestroy. So the overwritten state vector's device memory is never released. The move constructor (L91-L100) is fine, because a newly constructed object doesn't own anything yet. The reproducer below shows this directly: every state that gets overwritten stays allocated.
The other GPU backends don't have this problem. VectorSpaceCUDA::Vector, used by gpu_mode=0 and 1, keeps its buffer in a std::unique_ptr (vectorspace_cuda.h L48, L80) and doesn't define its own move assignment. Assigning into it therefore frees the old buffer.
Current callers on main. I couldn't find a code path that triggers this through qsimcirq today:
SimulatorHelperinpybind_interface/pybind_main.cpponly assigns into a state that starts out null.lib/expect.h(L56-L57) only replaces its temporary when it is null or smaller than the state, and qsimcirq always passes one of the right size after the first call.
Any other assignment into a live state leaks, for example state = state_space.Create(n) or state = StateSpace::Null(). #1100 ran into this when an interim version released its states with state = StateSpace::Null(); in a destructor, and repeated simulate-and-free cycles leaked device memory. #1100 now avoids the move assignment.
Possible fix (sketch, not tested): destroy the currently owned descriptor before taking over r's:
Vector& operator=(Vector&& r) {
if (this != &r) {
if (ptr_ != nullptr) {
ErrorCheck(custatevecExStateVectorDestroy(ptr_));
}
mp_ = r.mp_;
ptr_ = r.ptr_;
// ... rest unchanged
Tell us how to reproduce the issue
Save this as repro_issue2_leak.cu:
// Reproducer: VectorSpaceCuStateVecEx::Vector move assignment does not free
// the state vector it overwrites (lib/vectorspace_custatevecex.h).
//
// Build (QSIM = a qsim checkout, CUQUANTUM_ROOT = a cuQuantum install):
// nvcc -std=c++17 -I "$QSIM/lib" -I "$CUQUANTUM_ROOT/include" \
// repro_issue2_leak.cu -L "$CUQUANTUM_ROOT/lib" -lcustatevec -lcublas \
// -Xlinker -rpath -Xlinker "$CUQUANTUM_ROOT/lib" -o repro_issue2_leak
// Run on one GPU:
// CUDA_VISIBLE_DEVICES=0 ./repro_issue2_leak
#include <cstdio>
#include <cuda_runtime.h>
#include "statespace_custatevecex.h"
using StateSpace = qsim::StateSpaceCuStateVecEx<float>;
// Device memory in use, in MiB, relative to the first call.
static double UsedMiB() {
static size_t baseline = 0;
cudaDeviceSynchronize();
size_t free_bytes = 0, total_bytes = 0;
cudaMemGetInfo(&free_bytes, &total_bytes);
if (baseline == 0) baseline = free_bytes;
return (double(baseline) - double(free_bytes)) / (1024.0 * 1024.0);
}
static void Report(const char* step) {
std::printf("%-44s %8.0f MiB in use\n", step, UsedMiB());
}
int main() {
const unsigned num_qubits = 26; // 2^26 complex64 amplitudes = 512 MiB
qsim::MultiProcessCuStateVecEx mp; // single process
StateSpace state_space(mp);
Report("start");
{ // Control: a state freed by its destructor returns its memory.
auto control = state_space.Create(num_qubits);
state_space.SetStateZero(control);
Report("control: create");
}
Report("control: destructor ran");
{ // Bug: move-assigning over a live state leaks the state it replaces.
auto state = state_space.Create(num_qubits);
state_space.SetStateZero(state);
Report("state = Create() (A)");
state = state_space.Create(num_qubits);
state_space.SetStateZero(state);
Report("state = Create() (B replaces A)");
state = StateSpace::Null();
Report("state = StateSpace::Null() (replaces B)");
}
Report("after scope (destructor ran)");
return 0;
}
Build and run it as in its header comment, with QSIM set to a checkout of main and CUQUANTUM_ROOT set to a cuQuantum install:
$ nvcc -std=c++17 -I "$QSIM/lib" -I "$CUQUANTUM_ROOT/include" \
repro_issue2_leak.cu -L "$CUQUANTUM_ROOT/lib" -lcustatevec -lcublas \
-Xlinker -rpath -Xlinker "$CUQUANTUM_ROOT/lib" -o repro_issue2_leak
$ CUDA_VISIBLE_DEVICES=0 ./repro_issue2_leak
start 0 MiB in use
control: create 542 MiB in use
control: destructor ran 2 MiB in use
state = Create() (A) 542 MiB in use
state = Create() (B replaces A) 1080 MiB in use
state = StateSpace::Null() (replaces B) 1080 MiB in use
after scope (destructor ran) 1080 MiB in use
- Control: a state destroyed normally gives its memory back.
- B replaces A: A stays allocated.
Null()replaces B: nothing is freed.- After the scope ends: both state vectors are still allocated.
I got the same numbers on an NVIDIA A100-SXM4-40GB and a Tesla T4.
Tell us the version of qsim or qsimcirq (if relevant)
main @ 50cac4b (qsimcirq 0.23.0.dev0)
Tell us the computing environment (if relevant)
Linux (nvidia/cuda:13.2.0-devel-ubuntu24.04 container), NVIDIA A100-SXM4-40GB and Tesla T4 (identical results), driver 580.159.04, CUDA 13.2, cuQuantum 26.01.0.4 (cuStateVec 1.12.0.4)
- Lingua principale
- C++
- Stelle
- 702
- Fork
- 219
- Merge medio
- 1g 2h
- PR unite (30g)
- 8
Preparare l'ambiente
- Include un Dockerfile o un file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di quantumlib/qsim
-
`MPSStateSpace::Sample` accesses results out of bounds when the output vector is emptyForse già presa @aadityarshah l’ha presa 42 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
quantumlib/qsim#1126 ·
I maintainer di solito rispondono entro 2 giorni
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 56/100
quantumlib/qsim#1145 ·
I maintainer di solito rispondono entro 2 giorni
-
`MPSStateSpace::SampleOnce` crashes for two-qubit statesForse già presa @aadityarshah l’ha presa 40 giorni fa. Aperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 58/100
quantumlib/qsim#1129 ·
I maintainer di solito rispondono entro 2 giorni
-
Implement expectation values for the MPS SimulatorForse già presa @rupasri118 l’ha presa 36 giorni fa. Aperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 55/100
quantumlib/qsim#1122 · 1 commento ·
I maintainer di solito rispondono entro 2 giorni
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 38/100
quantumlib/qsim#1121 · 1 commento ·
I maintainer di solito rispondono entro 2 giorni
Tutte le issue di quantumlib/qsim
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
I maintainer di solito rispondono entro 1 giorno
-
ai_p1 comp-sql-syntax
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
ClickHouse/ClickHouse#133888 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
I maintainer di solito rispondono entro 1 giorno
-
documentation good first issue
Difficoltà 2/5 Mezza giornata Idoneità per principianti 66/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
jbcoe/cc-protocol#507 ·
I maintainer di solito rispondono entro 1 giorno