Add QDP backend detection and pure-PyTorch reference implementations#1189
Open
ryankert01 wants to merge 1 commit intoapache:mainfrom
Open
Add QDP backend detection and pure-PyTorch reference implementations#1189ryankert01 wants to merge 1 commit intoapache:mainfrom
ryankert01 wants to merge 1 commit intoapache:mainfrom
Conversation
- Implemented backend detection and selection logic in _backend.py, prioritizing Rust+CUDA, PyTorch, and fallback to None. - Added pure-PyTorch reference implementations for quantum data encoding methods in torch_ref.py, including amplitude, angle, basis, and IQP encoding. - Created comprehensive tests for fallback mechanisms and pure-PyTorch encodings in test_fallback.py and test_torch_ref.py, ensuring functionality without the Rust extension. - Enhanced error handling and validation across encoding methods to ensure robustness.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1177
Summary
This PR adds a pure-PyTorch reference backend to the QDP Python package to compare with our implementation of GPU kernel~
Benchmark Results
All runs: 100 batches x 64 vectors (except 18-qubit: 50 batches x 64), median of 3 trials.
Amplitude Encoding
Angle Encoding
IQP Encoding
Analysis
encodepath still pays per-batch GPU output allocation + D2H norm validation sync overhead.generate_batch_data+torch.tensor+ H2D transfer.Known Limitations
engine.encodeexpects per-sample basis indices; batch input format differs from PyTorch. Requires Rust API change to support.1 << num_qubitsas sample_size regardless of encoding method, causing a mismatch for IQP (which expectsn + n*(n-1)/2). Pre-existing Rust pipeline bug.Changes
New files (3)
qdp/qdp-python/qumat_qdp/_backend.py— Backend detection (RUST_CUDA/PYTORCH/NONE) with auto-selection,force_backend()for testingtesting/qdp_python/test_torch_ref.py— 396 lines of tests for the PyTorch reference encoderstesting/qdp_python/test_fallback.py— 306 lines of tests for the fallback path (loader, API)Modified files (4)
qdp/qdp-python/qumat_qdp/__init__.py— Graceful degradation when_qdpRust extension is unavailable; exportsBackend,BACKENDqdp/qdp-python/qumat_qdp/api.py—QdpBenchmarknow supports.backend("rust" | "pytorch" | "auto")with PyTorch throughput/latency implementationsqdp/qdp-python/qumat_qdp/loader.py—QuantumDataLoaderfalls back to PyTorch encoding when_qdpis missing; supports synthetic data and.npy/.ptfilestesting/conftest.py—test_torch_ref.pyandtest_fallback.pyare allowed to run without the Rust extensionBenchmark file (1)
qdp/qdp-python/benchmark/benchmark_pytorch_ref.py— Added--modeflag (encode-onlydefault,end-to-end), two new functions (run_mahout_encode_only,run_pytorch_end_to_end), updated banner with mode info and footnotesHow
--modeworksQdpEngine.encode(cuda_tensor)run_throughput_pipeline_py(gen + H2D + encode)Key design decisions
QdpEngine.encode()already accepts CUDA tensors via DLPack zero-copy, enabling the encode-only Mahout benchmark in pure Pythonqumat_qdppackage now works without_qdpcompiled, falling back to PyTorch. This makes the package installable/testable on machines without CUDA_backend.pyprovides a single source of truth for which backend is available, withforce_backend()for testing