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

Added pickle support for lightning.gpu device's DevPool attribute #772

Merged
merged 18 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
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.37.0-dev45"
__version__ = "0.37.0-dev46"
Jaybsoni marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,20 @@ void registerBackendSpecificInfo(py::module_ &m) {
.def("acquireDevice", &DevicePool<int>::acquireDevice)
.def("releaseDevice", &DevicePool<int>::releaseDevice)
.def("syncDevice", &DevicePool<int>::syncDevice)
.def("refresh", &DevicePool<int>::refresh)
.def_static("getTotalDevices", &DevicePool<int>::getTotalDevices)
.def_static("getDeviceUIDs", &DevicePool<int>::getDeviceUIDs)
.def_static("setDeviceID", &DevicePool<int>::setDeviceIdx);
.def_static("setDeviceID", &DevicePool<int>::setDeviceIdx)
.def(py::pickle(
[](const DevicePool<int> &self) { // __getstate__
return py::make_tuple();
},
[](py::tuple &t) { // __setstate__
if (t.size() != 0) {
throw std::runtime_error("Invalid state!");
}
Jaybsoni marked this conversation as resolved.
Show resolved Hide resolved
return DevicePool<int>{};
}));

py::class_<DevTag<int>>(m, "DevTag")
.def(py::init<>())
Expand Down
14 changes: 13 additions & 1 deletion pennylane_lightning/core/src/utils/cuda_utils/DevicePool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,19 @@ namespace Pennylane::LightningGPU {
*/
template <typename DeviceIndexType = int> class DevicePool {
public:
DevicePool() {
DevicePool() { refresh(); }

DevicePool(DevicePool &&dp) { refresh(); }
Jaybsoni marked this conversation as resolved.
Show resolved Hide resolved

/**
* @brief Dynamically refresh the available pool devices.
*
Jaybsoni marked this conversation as resolved.
Show resolved Hide resolved
*/
void refresh() {
std::lock_guard<std::mutex> lg(m_);
available_devices_.clear();
active_devices_.clear();

for (std::size_t i = 0; i < DevicePool::getTotalDevices(); i++) {
available_devices_.push(static_cast<DeviceIndexType>(i));
}
Expand Down
8 changes: 8 additions & 0 deletions pennylane_lightning/core/src/utils/cuda_utils/TSQueue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,12 @@ template <typename T> class TSQueue {
std::lock_guard<std::mutex> lk(m);
return q.empty();
}
/**
* @brief Clear the queue of all items. Thread safe.
Jaybsoni marked this conversation as resolved.
Show resolved Hide resolved
*
Jaybsoni marked this conversation as resolved.
Show resolved Hide resolved
*/
void clear() {
std::lock_guard<std::mutex> lk(m);
q = {};
}
};
Jaybsoni marked this conversation as resolved.
Show resolved Hide resolved
13 changes: 13 additions & 0 deletions tests/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"""
Unit tests for Lightning devices creation.
"""
import pickle as pkl
import sys

import numpy as np
Expand Down Expand Up @@ -67,6 +68,18 @@ def test_create_device_with_unsupported_mpi_buf_size():
pass


@pytest.mark.skipif(
device_name != "lightning.gpu",
reason="Check if the method is pickleable throught the cpp layer",
)
def test_devpool_is_pickleable():
Jaybsoni marked this conversation as resolved.
Show resolved Hide resolved
dev = qml.device(device_name, wires=2)
try:
_ = pkl.dumps(dev._dp)
Jaybsoni marked this conversation as resolved.
Show resolved Hide resolved
except Exception:
Jaybsoni marked this conversation as resolved.
Show resolved Hide resolved
pytest.fail("DevPool should be Pickleable")


@pytest.mark.skipif(
(device_name == "lightning.kokkos" and sys.platform == "win32"),
reason="lightning.kokkos doesn't support 0 wires on Windows.",
Expand Down
Loading