Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure adjoint allocates memory for max concurrent observables only #221

Merged
merged 33 commits into from
Feb 25, 2022

Conversation

mlxd
Copy link
Member

@mlxd mlxd commented Feb 2, 2022

  • All new features must include a unit test.
    If you've fixed a bug or added code that should be tested, add a test to the
    tests directory!

  • All new functions and code must be clearly commented and documented.
    If you do make documentation changes, make sure that the docs build and
    render correctly by running make docs.

  • Ensure that the test suite passes, by running make test.

  • Add a new entry to the .github/CHANGELOG.md file, summarizing the
    change, and including a link back to the PR.

  • Ensure that code is properly formatted by running make format.

When all the above are checked, delete everything above the dashed
line and fill in the pull request template.


Context: The current implementation of the adjoint Jacobian method in Lightning parallelizes over observables, with each observable handled by a separate OpenMP thread. This works fine in practice unless a large number of observables are required, wherein a new statevector memory block is allocated upfront for the total number of observables. This causes OOM errors for large numbers of observables, even for modest qubit counts. The solution is to restrict the requested number of observables into batches of OMP_NUM_THREADS concurrent executions, where the number of statevectors allocated is limited by the number of executing threads. This imposes a small overhead in requiring additional computation across batches, but ensures the user can reach much higher qubit counts for large numbers of concurrent executions.

Description of the Change: The adjoint Jacobian calculation is now batched at the maximum number of OpenMP threads, and allocates enough memory for a given batch only.

Benefits: Reduces overall memory footprint within a given series of concurrent calculations, allowing larger numbers of qubits in a given calculation, prevents OOM errors encountered for large workflows.

Possible Drawbacks: Imposes additional overheads due to repetition between batches.

Related GitHub Issues:

@github-actions
Copy link
Contributor

github-actions bot commented Feb 2, 2022

Hello. You may have forgotten to update the changelog!
Please edit .github/CHANGELOG.md with:

  • A one-to-two sentence description of the change. You may include a small working example for new features.
  • A link back to this PR.
  • Your name (or GitHub username) in the contributors section.

@codecov
Copy link

codecov bot commented Feb 2, 2022

Codecov Report

Merging #221 (a80fa50) into master (602352b) will increase coverage by 0.01%.
The diff coverage is 100.00%.

❗ Current head a80fa50 differs from pull request most recent head 81064a8. Consider uploading reports for the commit 81064a8 to get more accurate results

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #221      +/-   ##
==========================================
+ Coverage   99.70%   99.72%   +0.01%     
==========================================
  Files           4        4              
  Lines         344      358      +14     
==========================================
+ Hits          343      357      +14     
  Misses          1        1              
Impacted Files Coverage Δ
pennylane_lightning/_version.py 100.00% <100.00%> (ø)
pennylane_lightning/lightning_qubit.py 99.63% <100.00%> (+0.02%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 602352b...81064a8. Read the comment docs.

@github-actions
Copy link
Contributor

github-actions bot commented Feb 2, 2022

Test Report (C++) on Ubuntu

       1 files  ±0         1 suites  ±0   0s ⏱️ ±0s
   555 tests ±0     555 ✔️ ±0  0 💤 ±0  0 ±0 
2 289 runs  ±0  2 289 ✔️ ±0  0 💤 ±0  0 ±0 

Results for commit 81064a8. ± Comparison against base commit 602352b.

♻️ This comment has been updated with latest results.

@@ -65,6 +67,12 @@
)


def _chunk_iterable(it, num_chunks):
"Lazy-evaluted chunking of given iterable from https://stackoverflow.com/a/22045226"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"Lazy-evaluted chunking of given iterable from https://stackoverflow.com/a/22045226"
"Lazy-evaluated chunking of given iterable from https://stackoverflow.com/a/22045226"

tests/test_comparison.py Outdated Show resolved Hide resolved
tests/test_comparison.py Outdated Show resolved Hide resolved
tests/test_comparison.py Show resolved Hide resolved
tests/test_comparison.py Show resolved Hide resolved
tests/test_comparison.py Show resolved Hide resolved
@mlxd mlxd changed the base branch from master to v0.21.0-rc0 February 3, 2022 21:10
Copy link
Contributor

@AmintorDusko AmintorDusko left a comment

Choose a reason for hiding this comment

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

Nice work @mlxd! I'm happy to approve!

Copy link
Member

@maliasadi maliasadi left a comment

Choose a reason for hiding this comment

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

Awesome work! 🚀 I've had only a few comments...

doc/devices.rst Outdated Show resolved Hide resolved
doc/devices.rst Outdated Show resolved Hide resolved
doc/devices.rst Show resolved Hide resolved
doc/devices.rst Show resolved Hide resolved
pennylane_lightning/lightning_qubit.py Outdated Show resolved Hide resolved
pennylane_lightning/lightning_qubit.py Outdated Show resolved Hide resolved
mlxd and others added 2 commits February 3, 2022 17:51
Co-authored-by: Ali Asadi <[email protected]>
Co-authored-by: Ali Asadi <[email protected]>
Copy link
Contributor

@trevor-vincent trevor-vincent left a comment

Choose a reason for hiding this comment

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

Nice work! Don't see any problem beyond what Ali pointed out.

@mlxd mlxd requested a review from maliasadi February 4, 2022 14:22
@mlxd mlxd changed the base branch from v0.21.0-rc0 to master February 4, 2022 14:22
@mlxd mlxd merged commit dbe6e19 into master Feb 25, 2022
@mlxd mlxd deleted the openmp_memory_conc branch February 25, 2022 21:38
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.

5 participants