Skip to content

Fix/14 gpu round trip - #51

Open
LucaLM02 wants to merge 3 commits into
Algorithmiq:mainfrom
LucaLM02:fix/14-gpu-round-trip
Open

LucaLM02 wants to merge 3 commits into
Algorithmiq:mainfrom
LucaLM02:fix/14-gpu-round-trip

Conversation

@LucaLM02

@LucaLM02 LucaLM02 commented Oct 6, 2026

Copy link
Copy Markdown

Summary

Keeps data on the GPU on the device="gpu" path (closes #14 ) :

  • Truncation SVD now runs on the array's own device (xp.linalg.svd) instead of copying R to the host with .get() and calling np.linalg.svd.
  • New to_host: bool = True argument on apply / compress. With to_host=False the result stays backed by CuPy arrays and can be fed straight into the next call without a device/host round trip. The default preserves the previous behaviour, so existing callers are unaffected.
  • CPU path is unchanged.

Performance

Measured on Google Colab:

  • GPU: Tesla T4 (15 GB), driver 580.82.07
  • CPU: Intel Xeon @ 2.00 GHz, 2 vCPU
  • cupy 14.2.0, CUDA runtime 12.9, numpy 2.4.4, Python 3.14.8
  • before = fe1779b (main), after = fbec6ae (this branch)

Method: complex128, seed=0, random MPO × MPS, 20 sites, phys_dim=4, median of 10 runs. Each path is warmed up first, and before/after are timed in separate processes on the same session.

chi_out = 64 (input bond dim 64)

Scenario before (main) after, to_host=True after, to_host=False speedup
single apply 613.5 ms 614.2 ms 610.0 ms 1.01x
5 chained apply 3016.6 ms 3018.3 ms 2978.2 ms 1.01x

chi_out = 128 (input bond dim 128, after only)

Scenario to_host=True to_host=False
single apply 8455.1 ms 8448.3 ms
3 chained apply 24863.1 ms 24830.8 ms

Testing

  • Full test suite on the T4: 40 passed in ~70 s.
  • Chained apply(..., to_host=False) calls accept CuPy inputs and run without host round trips of the result.

Acceptance criteria

  • No .get() / D2H transfer inside the per-site loop
  • Chained GPU apply calls do not round-trip the result through host memory
  • CPU results and numerics unchanged
  • GPU tests pass
  • Before/after timings recorded above, with hardware

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

The two-site fallback still round-trips GPU data through the host, and the GPU SVD path lacks direct coverage.

Review effort: Balanced
Findings: 1 Medium severity · 2 Low severity

Open (3)
What changed in this PR

Moves GPU SVD work and optional outputs fully onto the selected device.

Changes:

  • Adds to_host to public compression APIs.
  • Runs truncated SVD through the active array backend.
  • Adds GPU output and chaining tests.
File Description
src/​src_method/​apply.py Exposes to_host for application results.
src/​src_method/​compress.py Exposes to_host for compression results.
src/​src_method/​stack.py Propagates output-device selection.
src/​src_method/​_sweep.py Avoids unconditional host conversion.
src/​src_method/​utils/​linalg.py Runs truncation SVD on the active backend.
tests/​test_gpu_backend.py Tests GPU-backed outputs and chaining.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/src_method/stack.py
Comment on lines +111 to +112
result = exact_stack(layers, chi_out, kind)
return result if to_host else [xp.asarray(t) for t in result]
Comment thread src/src_method/_sweep.py
Returns:
The site arrays of the compressed train in right-canonical form, as numpy
arrays.
arrays if ``to_host`` is ``True``, otherwise as arrays of ``xp``..
Q, R = xp.linalg.qr(matrix.T if transpose else matrix)
R_np = R.get() if hasattr(R, "get") else np.asarray(R)
U, S, _ = np.linalg.svd(R_np.T if transpose else R_np, full_matrices=False)
U, S, _ = xp.linalg.svd(R.T if transpose else R, full_matrices=False)

This branch has not been deployed

No deployments
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.

GPU path round-trips to host on every call and every site

2 participants