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

Prevent reallocation of aligned memory #572

Merged
merged 28 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
36ec99f
Allow aligned alloc to be more useful
mlxd Dec 7, 2023
b2760ee
Auto update version
github-actions[bot] Dec 7, 2023
ac4c914
Remove comments
mlxd Dec 7, 2023
ebad87f
Merge branch 'improve_lq_memory' of github.com:PennyLaneAI/pennylane-…
mlxd Dec 7, 2023
e1af9a2
Fix testing failures
mlxd Dec 7, 2023
a0e05bf
remove redundant functions from LG & LK
mlxd Dec 7, 2023
a691254
Readd removed import
mlxd Dec 7, 2023
c358396
Merge branch 'master' into improve_lq_memory
mlxd Dec 12, 2023
d827654
Auto update version
github-actions[bot] Dec 12, 2023
1c51a02
Merge branch 'master' into improve_lq_memory
mlxd Dec 12, 2023
7e43211
Auto update version
github-actions[bot] Dec 12, 2023
fbeb14c
Trigger CI
mlxd Dec 12, 2023
27063dc
Ensure dtype conversion happens when using lower prec SV
mlxd Dec 12, 2023
befe032
Remove unused imports
mlxd Dec 12, 2023
fc0fb62
Merge branch 'master' into improve_lq_memory
mlxd Jan 3, 2024
2b72c89
Auto update version
github-actions[bot] Jan 3, 2024
8f75a80
Trigger CI
mlxd Jan 3, 2024
2f10b54
Merge branch 'master' into improve_lq_memory
mlxd Jan 3, 2024
c40d7e9
Auto update version
github-actions[bot] Jan 3, 2024
3dfcb2f
Trigger CI
mlxd Jan 3, 2024
b3d4f2e
Readd missing block to LQ
mlxd Jan 3, 2024
0a1271e
Merge branch 'improve_lq_memory' of github.com:PennyLaneAI/pennylane-…
mlxd Jan 3, 2024
4c18b4c
Merge branch 'master' into improve_lq_memory
mlxd Jan 3, 2024
4dfe6cd
Auto update version
github-actions[bot] Jan 3, 2024
b632ecf
Trigger CI
mlxd Jan 3, 2024
3837f07
Merge branch 'improve_lq_memory' of github.com:PennyLaneAI/pennylane-…
mlxd Jan 3, 2024
5f2fd1f
Add TODO and update changelog
mlxd Jan 3, 2024
70e5b28
Ensure pip installed clang-format on path
mlxd Jan 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pennylane_lightning/core/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
Version number (major.minor.patch[-label])
"""

__version__ = "0.34.0-dev26"
__version__ = "0.34.0-dev27"
20 changes: 12 additions & 8 deletions pennylane_lightning/core/src/bindings/Bindings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,12 @@ auto getNumpyArrayAlignment(const py::array &numpyArray) -> CPUMemoryModel {
* @return Numpy array
*/
template <typename T>
auto alignedNumpyArray(CPUMemoryModel memory_model, size_t size) -> py::array {
auto alignedNumpyArray(CPUMemoryModel memory_model, size_t size,
bool zeroInit = false) -> py::array {
using Pennylane::Util::alignedAlloc;
if (getAlignment<T>(memory_model) > alignof(std::max_align_t)) {
void *ptr =
alignedAlloc(getAlignment<T>(memory_model), sizeof(T) * size);
void *ptr = alignedAlloc(getAlignment<T>(memory_model),
sizeof(T) * size, zeroInit);
auto capsule = py::capsule(ptr, &Util::alignedFree);
return py::array{py::dtype::of<T>(), {size}, {sizeof(T)}, ptr, capsule};
}
Expand All @@ -172,20 +173,23 @@ auto alignedNumpyArray(CPUMemoryModel memory_model, size_t size) -> py::array {
* @param size Size of the array to create
* @param dt Pybind11's datatype object
*/
auto allocateAlignedArray(size_t size, const py::dtype &dt) -> py::array {
auto allocateAlignedArray(size_t size, const py::dtype &dt,
bool zeroInit = false) -> py::array {
auto memory_model = bestCPUMemoryModel();

if (dt.is(py::dtype::of<float>())) {
return alignedNumpyArray<float>(memory_model, size);
return alignedNumpyArray<float>(memory_model, size, zeroInit);
}
if (dt.is(py::dtype::of<double>())) {
return alignedNumpyArray<double>(memory_model, size);
return alignedNumpyArray<double>(memory_model, size, zeroInit);
}
if (dt.is(py::dtype::of<std::complex<float>>())) {
return alignedNumpyArray<std::complex<float>>(memory_model, size);
return alignedNumpyArray<std::complex<float>>(memory_model, size,
zeroInit);
}
if (dt.is(py::dtype::of<std::complex<double>>())) {
return alignedNumpyArray<std::complex<double>>(memory_model, size);
return alignedNumpyArray<std::complex<double>>(memory_model, size,
zeroInit);
}
throw py::type_error("Unsupported datatype.");
}
Expand Down
16 changes: 11 additions & 5 deletions pennylane_lightning/core/src/utils/Memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <memory>
#include <new>
Expand All @@ -35,23 +36,28 @@ namespace Pennylane::Util {
* @param bytes Number of bytes to allocate
* @return Pointer to the allocated memory
*/
inline auto alignedAlloc(uint32_t alignment, size_t bytes) -> void * {
inline auto alignedAlloc(uint32_t alignment, size_t bytes,
bool zero_init = false) -> void * {
if (bytes % alignment != 0) {
bytes = alignment * (bytes / alignment + 1);
}
void *p = nullptr;

#if defined(__clang__) && defined(__APPLE__)
/*
* We use `posix_memalign` for MacOS as Mac does not support
* `std::aligned_alloc` properly yet (even in MacOS 10.15).
*/
void *p;
posix_memalign(&p, alignment, bytes);
return p;
#elif defined(_MSC_VER)
return _aligned_malloc(bytes, alignment);
p = _aligned_malloc(bytes, alignment);
#else
return std::aligned_alloc(alignment, bytes);
p = std::aligned_alloc(alignment, bytes);
#endif
if (zero_init) {
std::memset(p, 0, bytes);
}
return p;
}

/**
Expand Down
11 changes: 0 additions & 11 deletions pennylane_lightning/lightning_gpu/lightning_gpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@

try:
from pennylane_lightning.lightning_gpu_ops import (
allocate_aligned_array,
backend_info,
best_alignment,
get_alignment,
StateVectorC128,
StateVectorC64,
MeasurementsC128,
Expand Down Expand Up @@ -331,14 +328,6 @@ def _asarray(arr, dtype=None):
if not dtype:
dtype = arr.dtype

# We allocate a new aligned memory and copy data to there if alignment
# or dtype mismatches
# Note that get_alignment does not necessarily return CPUMemoryModel(Unaligned) even for
# numpy allocated memory as the memory location happens to be aligned.
if int(get_alignment(arr)) < int(best_alignment()) or arr.dtype != dtype:
new_arr = allocate_aligned_array(arr.size, np.dtype(dtype)).reshape(arr.shape)
np.copyto(new_arr, arr)
arr = new_arr
return arr

# pylint disable=missing-function-docstring
Expand Down
8 changes: 4 additions & 4 deletions pennylane_lightning/lightning_kokkos/lightning_kokkos.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
from pennylane_lightning.lightning_kokkos_ops import (
allocate_aligned_array,
backend_info,
best_alignment,
get_alignment,
InitializationSettings,
MeasurementsC128,
MeasurementsC64,
Expand Down Expand Up @@ -232,8 +230,10 @@ def _asarray(arr, dtype=None):
# or dtype mismatches
# Note that get_alignment does not necessarily return CPUMemoryModel(Unaligned) even for
# numpy allocated memory as the memory location happens to be aligned.
if int(get_alignment(arr)) < int(best_alignment()) or arr.dtype != dtype:
new_arr = allocate_aligned_array(arr.size, np.dtype(dtype)).reshape(arr.shape)
if arr.dtype != dtype:
new_arr = allocate_aligned_array(arr.size, np.dtype(dtype), False).reshape(
arr.shape
)
np.copyto(new_arr, arr)
arr = new_arr
return arr
Expand Down
16 changes: 11 additions & 5 deletions pennylane_lightning/lightning_qubit/lightning_qubit.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ def __init__( # pylint: disable=too-many-arguments
# state as an array of dimension [2]*wires.
self._state = self._create_basis_state(0)
self._pre_rotated_state = self._state
self._c_dtype = c_dtype

self._batch_obs = batch_obs
self._mcmc = mcmc
Expand Down Expand Up @@ -259,8 +260,13 @@ def _asarray(arr, dtype=None):
# Note that get_alignment does not necessarily return CPUMemoryModel(Unaligned)
# numpy allocated memory as the memory location happens to be aligned.
if int(get_alignment(arr)) < int(best_alignment()) or arr.dtype != dtype:
new_arr = allocate_aligned_array(arr.size, np.dtype(dtype)).reshape(arr.shape)
np.copyto(new_arr, arr)
new_arr = allocate_aligned_array(arr.size, np.dtype(dtype), False).reshape(
arr.shape
)
if len(arr.shape):
new_arr[:] = arr
else:
np.copyto(new_arr, arr)
arr = new_arr
return arr

Expand All @@ -273,17 +279,17 @@ def _create_basis_state(self, index):
representing the statevector of the basis state
Note: This function does not support broadcasted inputs yet.
"""
state = np.zeros(2**self.num_wires, dtype=np.complex128)
state = allocate_aligned_array(2**self.num_wires, np.dtype(self.C_DTYPE), True)
state[index] = 1
state = self._asarray(state, dtype=self.C_DTYPE)
return self._reshape(state, [2] * self.num_wires)

def reset(self):
"""Reset the device"""
super().reset()

# init the state vector to |00..0>
self._state = self._create_basis_state(0)
if not self.state[0] == 1.0 + 0j:
self._state = self._create_basis_state(0)
self._pre_rotated_state = self._state

@property
Expand Down
2 changes: 1 addition & 1 deletion tests/test_adjoint_jacobian.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ def test_provide_starting_state(self, tol, dev):

dM1 = dev.adjoint_jacobian(tape)

if device_name != "lightning.gpu":
if device_name == "lightning.kokkos":
dev._pre_rotated_state = dev.state_vector # necessary for lightning.kokkos

qml.execute([tape], dev, None)
Expand Down
17 changes: 11 additions & 6 deletions tests/test_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@
try:
from pennylane_lightning.lightning_qubit_ops import allocate_aligned_array
except (ImportError, ModuleNotFoundError):
try:
from pennylane_lightning.lightning_kokkos_ops import allocate_aligned_array
except (ImportError, ModuleNotFoundError):
pytest.skip("No binary module found. Skipping.", allow_module_level=True)
pytest.skip("No binary module found. Skipping.", allow_module_level=True)


@pytest.mark.skipif(not ld._CPP_BINARY_AVAILABLE, reason="Lightning binary required")
@pytest.mark.parametrize("dt", [np.dtype(np.complex64), np.dtype(np.complex128)])
def test_allocate_aligned_array(dt):
arr = allocate_aligned_array(1024, dt)
def test_allocate_aligned_array_unset(dt):
arr = allocate_aligned_array(1024, dt, False)
assert arr.dtype == dt


@pytest.mark.skipif(not ld._CPP_BINARY_AVAILABLE, reason="Lightning binary required")
@pytest.mark.parametrize("dt", [np.dtype(np.complex64), np.dtype(np.complex128)])
def test_allocate_aligned_array_set(dt):
arr = allocate_aligned_array(1024, dt, True)
assert arr.dtype == dt
assert np.all(arr == 0)