Avoid ExtraInstructionAttributes allocation on unit="dt"#13078
Merged
Conversation
The default value for `Instruction.unit` is `"dt"`. Previously, the `OperationFromPython` extraction logic would only suppress allocation of the extra instruction attributes if all the contained fields were `None`, but `None` is not actually a valid value of `Instruction.unit` (which must be a string). This meant that `OperationFromPython` would always allocate and store extra attributes, even for the default cases. This did not affect standard gates appended using their corresponding `QuantumCircuit` methods (since no Python-space extraction is performed in that case), but did affect standard calls to `append`, or anything else that entered from Python space. This drastically reduces the memory usage of circuits built by `append`-like methods. Ignoring the inefficiency factor of the heap-allocation implementation, this saves 66 bytes plus small-allocation overhead for 2-byte heap allocations (another 14 bytes on macOS, but will vary depending on the allocator) per standard instruction, which is on the order of 40% memory-usage reduction.
Collaborator
|
One or more of the following people are relevant to this code:
|
Pull Request Test Coverage Report for Build 10680563760Details
💛 - Coveralls |
mtreinish
approved these changes
Sep 3, 2024
Member
mtreinish
left a comment
There was a problem hiding this comment.
LGTM, this is a straightforward improvement and good to see the memory overhead reductions in practice.
| } | ||
|
|
||
| /// Get the Python-space default value for the `unit` field. | ||
| pub fn default_unit(py: Python) -> &Bound<PyString> { |
Member
There was a problem hiding this comment.
I feel like I would put an #[inline] on this since the compiler is less likely to inline a public function. But it doesn't really matter in practice, especially since this is the python path so that won't help anything.
sbrandhsn
pushed a commit
to sbrandhsn/qiskit
that referenced
this pull request
Sep 5, 2024
…13078) The default value for `Instruction.unit` is `"dt"`. Previously, the `OperationFromPython` extraction logic would only suppress allocation of the extra instruction attributes if all the contained fields were `None`, but `None` is not actually a valid value of `Instruction.unit` (which must be a string). This meant that `OperationFromPython` would always allocate and store extra attributes, even for the default cases. This did not affect standard gates appended using their corresponding `QuantumCircuit` methods (since no Python-space extraction is performed in that case), but did affect standard calls to `append`, or anything else that entered from Python space. This drastically reduces the memory usage of circuits built by `append`-like methods. Ignoring the inefficiency factor of the heap-allocation implementation, this saves 66 bytes plus small-allocation overhead for 2-byte heap allocations (another 14 bytes on macOS, but will vary depending on the allocator) per standard instruction, which is on the order of 40% memory-usage reduction.
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.
Summary
The default value for
Instruction.unitis"dt". Previously, theOperationFromPythonextraction logic would only suppress allocation of the extra instruction attributes if all the contained fields wereNone, butNoneis not actually a valid value ofInstruction.unit(which must be a string). This meant thatOperationFromPythonwould always allocate and store extra attributes, even for the default cases. This did not affect standard gates appended using their correspondingQuantumCircuitmethods (since no Python-space extraction is performed in that case), but did affect standard calls toappend, or anything else that entered from Python space.This drastically reduces the memory usage of circuits built by
append-like methods. Ignoring the inefficiency factor of the heap-allocation implementation, this saves 66 bytes plus small-allocation overhead for 2-byte heap allocations (another 14 bytes on macOS, but will vary depending on the allocator) per standard instruction, which is on the order of 40% memory-usage reduction.Details and comments
I'm using the same sort of microbenchmarking script I've been using since #12730, but now modified to use
appendinstead of the special methods onQuantumCircuit:The memory usage of
main_appendfor the parent of this PR is approximately 2.3GB on both macOS, and Linux with glibc, whereas with the PR it drops to 1.35GB on macOS and 1.06GB on Linux/glibc. I suspect there's something additional going on in the macOS one, because while the Linux/glibc one drops to match the memory usage ofmain_methods(as expected), macOS remains 300MB higher. It might have been different Python versions - I used 3.10 on macOS and 3.12 on Linux.