From ea71e7f270eebef8f143f3ede9cde8b77655816e Mon Sep 17 00:00:00 2001 From: Shuli Shu <08cnbj@gmail.com> Date: Mon, 15 Jul 2024 21:16:53 +0000 Subject: [PATCH 01/10] initial commit --- pennylane_lightning/core/src/utils/UtilLinearAlg.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pennylane_lightning/core/src/utils/UtilLinearAlg.hpp b/pennylane_lightning/core/src/utils/UtilLinearAlg.hpp index 818ff1b25d..727222ed34 100644 --- a/pennylane_lightning/core/src/utils/UtilLinearAlg.hpp +++ b/pennylane_lightning/core/src/utils/UtilLinearAlg.hpp @@ -150,7 +150,7 @@ void compute_diagonalizing_gates(int n, int lda, } } else { try { - scipyPathStr = currentPathStr + "../../scipy.libs/"; + scipyPathStr = currentPathStr + "/../../scipy.libs/"; // convert the relative path to absolute path scipyPathStr = std::filesystem::canonical(scipyPathStr).string(); From c84303de5d9bc41b2dcb299b51ab45d262862a88 Mon Sep 17 00:00:00 2001 From: Shuli Shu <08cnbj@gmail.com> Date: Tue, 16 Jul 2024 14:49:58 +0000 Subject: [PATCH 02/10] initial commit --- .../core/src/bindings/BindingsCudaUtils.hpp | 101 ++++++++++++++++++ .../lightning_gpu/bindings/LGPUBindings.hpp | 58 +--------- .../tncuda/bindings/LTensorTNCudaBindings.hpp | 49 +-------- 3 files changed, 106 insertions(+), 102 deletions(-) create mode 100644 pennylane_lightning/core/src/bindings/BindingsCudaUtils.hpp diff --git a/pennylane_lightning/core/src/bindings/BindingsCudaUtils.hpp b/pennylane_lightning/core/src/bindings/BindingsCudaUtils.hpp new file mode 100644 index 0000000000..2a04ad4d63 --- /dev/null +++ b/pennylane_lightning/core/src/bindings/BindingsCudaUtils.hpp @@ -0,0 +1,101 @@ +// Copyright 2024 Xanadu Quantum Technologies Inc. + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 + +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/** + * @file BindingsCudaUtils.hpp + * Defines CUDA device - specific operations to export to Python, other + * utility functions interfacing with Pybind11 and support to agnostic bindings. + */ + +#pragma once + +#include "BindingsBase.hpp" +#include "DevTag.hpp" +#include "DevicePool.hpp" +#include "cuda_helpers.hpp" + +/// @cond DEV +namespace { +using namespace Pennylane; +using namespace Pennylane::Bindings; +} // namespace +/// @endcond + +namespace py = pybind11; + +namespace Pennylane::LightningGPU::Util { +/** + * @brief Register bindings for CUDA utils. + * + * @param m Pybind11 module. + */ +void registerCudaUtils(py::module_ &m) { + m.def("device_reset", &deviceReset, "Reset all GPU devices and contexts."); + m.def("allToAllAccess", []() { + for (int i = 0; i < static_cast(getGPUCount()); i++) { + cudaDeviceEnablePeerAccess(i, 0); + } + }); + + m.def("is_gpu_supported", &isCuQuantumSupported, + py::arg("device_number") = 0, + "Checks if the given GPU device meets the minimum architecture " + "support for the PennyLane-Lightning-GPU device."); + + m.def("get_gpu_arch", &getGPUArch, py::arg("device_number") = 0, + "Returns the given GPU major and minor GPU support."); + py::class_>(m, "DevPool") + .def(py::init<>()) + .def("getActiveDevices", &DevicePool::getActiveDevices) + .def("isActive", &DevicePool::isActive) + .def("isInactive", &DevicePool::isInactive) + .def("acquireDevice", &DevicePool::acquireDevice) + .def("releaseDevice", &DevicePool::releaseDevice) + .def("syncDevice", &DevicePool::syncDevice) + .def("refresh", &DevicePool::refresh) + .def_static("getTotalDevices", &DevicePool::getTotalDevices) + .def_static("getDeviceUIDs", &DevicePool::getDeviceUIDs) + .def_static("setDeviceID", &DevicePool::setDeviceIdx) + .def(py::pickle( + []([[maybe_unused]] const DevicePool &self) { // __getstate__ + return py::make_tuple(); + }, + [](py::tuple &t) { // __setstate__ + if (t.size() != 0) { + throw std::runtime_error("Invalid state!"); + } + return DevicePool{}; + })); + + py::class_>(m, "DevTag") + .def(py::init<>()) + .def(py::init()) + .def(py::init([](int device_id, void *stream_id) { + // Note, streams must be handled externally for now. + // Binding support provided through void* conversion to cudaStream_t + return new DevTag(device_id, + static_cast(stream_id)); + })) + .def(py::init &>()) + .def("getDeviceID", &DevTag::getDeviceID) + .def("getStreamID", + [](DevTag &dev_tag) { + // default stream points to nullptr, so just return void* as + // type + return static_cast(dev_tag.getStreamID()); + }) + .def("refresh", &DevTag::refresh); +} + +} // namespace Pennylane::LightningGPU::Util diff --git a/pennylane_lightning/core/src/simulators/lightning_gpu/bindings/LGPUBindings.hpp b/pennylane_lightning/core/src/simulators/lightning_gpu/bindings/LGPUBindings.hpp index 4ebb907791..5bd92b5520 100644 --- a/pennylane_lightning/core/src/simulators/lightning_gpu/bindings/LGPUBindings.hpp +++ b/pennylane_lightning/core/src/simulators/lightning_gpu/bindings/LGPUBindings.hpp @@ -20,6 +20,7 @@ #include "cuda.h" #include "BindingsBase.hpp" +#include "BindingsCudaUtils.hpp" #include "Constant.hpp" #include "ConstantUtil.hpp" // lookup #include "DevTag.hpp" @@ -362,62 +363,7 @@ auto getBackendInfo() -> py::dict { */ void registerBackendSpecificInfo(py::module_ &m) { m.def("backend_info", &getBackendInfo, "Backend-specific information."); - m.def("device_reset", &deviceReset, "Reset all GPU devices and contexts."); - m.def("allToAllAccess", []() { - for (int i = 0; i < static_cast(getGPUCount()); i++) { - cudaDeviceEnablePeerAccess(i, 0); - } - }); - - m.def("is_gpu_supported", &isCuQuantumSupported, - py::arg("device_number") = 0, - "Checks if the given GPU device meets the minimum architecture " - "support for the PennyLane-Lightning-GPU device."); - - m.def("get_gpu_arch", &getGPUArch, py::arg("device_number") = 0, - "Returns the given GPU major and minor GPU support."); - py::class_>(m, "DevPool") - .def(py::init<>()) - .def("getActiveDevices", &DevicePool::getActiveDevices) - .def("isActive", &DevicePool::isActive) - .def("isInactive", &DevicePool::isInactive) - .def("acquireDevice", &DevicePool::acquireDevice) - .def("releaseDevice", &DevicePool::releaseDevice) - .def("syncDevice", &DevicePool::syncDevice) - .def("refresh", &DevicePool::refresh) - .def_static("getTotalDevices", &DevicePool::getTotalDevices) - .def_static("getDeviceUIDs", &DevicePool::getDeviceUIDs) - .def_static("setDeviceID", &DevicePool::setDeviceIdx) - .def(py::pickle( - []([[maybe_unused]] const DevicePool &self) { // __getstate__ - return py::make_tuple(); - }, - [](py::tuple &t) { // __setstate__ - if (t.size() != 0) { - throw std::runtime_error("Invalid state!"); - } - return DevicePool{}; - })); - - py::class_>(m, "DevTag") - .def(py::init<>()) - .def(py::init()) - .def(py::init([](int device_id, void *stream_id) { - // Note, streams must be handled externally for now. - // Binding support provided through void* conversion to cudaStream_t - return new DevTag(device_id, - static_cast(stream_id)); - })) - .def(py::init &>()) - .def("getDeviceID", &DevTag::getDeviceID) - .def("getStreamID", - [](DevTag &dev_tag) { - // default stream points to nullptr, so just return void* as - // type - return static_cast(dev_tag.getStreamID()); - }) - .def("refresh", &DevTag::refresh); + registerCudaUtils(m); } } // namespace Pennylane::LightningGPU - /// @endcond diff --git a/pennylane_lightning/core/src/simulators/lightning_tensor/tncuda/bindings/LTensorTNCudaBindings.hpp b/pennylane_lightning/core/src/simulators/lightning_tensor/tncuda/bindings/LTensorTNCudaBindings.hpp index 83f2187dc0..39bb9fa61c 100644 --- a/pennylane_lightning/core/src/simulators/lightning_tensor/tncuda/bindings/LTensorTNCudaBindings.hpp +++ b/pennylane_lightning/core/src/simulators/lightning_tensor/tncuda/bindings/LTensorTNCudaBindings.hpp @@ -24,6 +24,7 @@ #include "cuda.h" #include "BindingsBase.hpp" +#include "BindingsCudaUtils.hpp" #include "DevTag.hpp" #include "DevicePool.hpp" #include "Error.hpp" @@ -35,6 +36,7 @@ namespace { using namespace Pennylane; using namespace Pennylane::Bindings; +using namespace Pennylane::LightningGPU::Util; using Pennylane::LightningTensor::TNCuda::MPSTNCuda; } // namespace /// @endcond @@ -88,54 +90,9 @@ auto getBackendInfo() -> py::dict { * * @param m Pybind11 module. */ -// TODO Move this method to a separate module for both LGPU and LTensor usage. void registerBackendSpecificInfo(py::module_ &m) { m.def("backend_info", &getBackendInfo, "Backend-specific information."); - m.def("device_reset", &deviceReset, "Reset all GPU devices and contexts."); - m.def("allToAllAccess", []() { - for (int i = 0; i < static_cast(getGPUCount()); i++) { - cudaDeviceEnablePeerAccess(i, 0); - } - }); - - m.def("is_gpu_supported", &isCuQuantumSupported, - py::arg("device_number") = 0, - "Checks if the given GPU device meets the minimum architecture " - "support for the PennyLane-Lightning-Tensor device."); - - m.def("get_gpu_arch", &getGPUArch, py::arg("device_number") = 0, - "Returns the given GPU major and minor GPU support."); - py::class_>(m, "DevPool") - .def(py::init<>()) - .def("getActiveDevices", &DevicePool::getActiveDevices) - .def("isActive", &DevicePool::isActive) - .def("isInactive", &DevicePool::isInactive) - .def("acquireDevice", &DevicePool::acquireDevice) - .def("releaseDevice", &DevicePool::releaseDevice) - .def("syncDevice", &DevicePool::syncDevice) - .def_static("getTotalDevices", &DevicePool::getTotalDevices) - .def_static("getDeviceUIDs", &DevicePool::getDeviceUIDs) - .def_static("setDeviceID", &DevicePool::setDeviceIdx); - - py::class_>(m, "DevTag") - .def(py::init<>()) - .def(py::init()) - .def(py::init([](int device_id, void *stream_id) { - // Note, streams must be handled externally for now. - // Binding support provided through void* conversion to cudaStream_t - return new DevTag(device_id, - static_cast(stream_id)); - })) - .def(py::init &>()) - .def("getDeviceID", &DevTag::getDeviceID) - .def("getStreamID", - [](DevTag &dev_tag) { - // default stream points to nullptr, so just return void* as - // type - return static_cast(dev_tag.getStreamID()); - }) - .def("refresh", &DevTag::refresh); + registerCudaUtils(m); } } // namespace Pennylane::LightningTensor::TNCuda - /// @endcond From 4163e00fa27fe0c9ba8d1bd794dbebcb793e3fec Mon Sep 17 00:00:00 2001 From: ringo-but-quantum Date: Tue, 16 Jul 2024 14:53:38 +0000 Subject: [PATCH 03/10] Auto update version from '0.38.0-dev6' to '0.38.0-dev8' --- pennylane_lightning/core/_version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pennylane_lightning/core/_version.py b/pennylane_lightning/core/_version.py index 9c61a75da7..02e7041e1a 100644 --- a/pennylane_lightning/core/_version.py +++ b/pennylane_lightning/core/_version.py @@ -16,4 +16,4 @@ Version number (major.minor.patch[-label]) """ -__version__ = "0.38.0-dev6" +__version__ = "0.38.0-dev8" From 590983d12dd62c29093d3cad839bb9b08f0285fa Mon Sep 17 00:00:00 2001 From: Shuli Shu <08cnbj@gmail.com> Date: Tue, 16 Jul 2024 14:57:02 +0000 Subject: [PATCH 04/10] add changelog --- .github/CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/CHANGELOG.md b/.github/CHANGELOG.md index 626b6b988f..9c46333282 100644 --- a/.github/CHANGELOG.md +++ b/.github/CHANGELOG.md @@ -12,6 +12,9 @@ ### Improvements +* Refactor CUDA utils Python bindings to a separate module. + [(#801)](https://github.com/PennyLaneAI/pennylane-lightning/pull/801) + * Enable setting the PennyLane version when invoking, for example, `make docker-build version=master pl_version=master`. [(#791)](https://github.com/PennyLaneAI/pennylane-lightning/pull/791) @@ -38,7 +41,7 @@ This release contains contributions from (in alphabetical order): -Amintor Dusko, Vincent Michaud-Rioux +Amintor Dusko, Vincent Michaud-Rioux, Shuli Shu --- From 8d173be166b861ad06a2daef96c87bb0b287a6e6 Mon Sep 17 00:00:00 2001 From: Shuli Shu <08cnbj@gmail.com> Date: Tue, 16 Jul 2024 14:59:23 +0000 Subject: [PATCH 05/10] revert changes in UtilLinearAlg --- pennylane_lightning/core/src/utils/UtilLinearAlg.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pennylane_lightning/core/src/utils/UtilLinearAlg.hpp b/pennylane_lightning/core/src/utils/UtilLinearAlg.hpp index 727222ed34..818ff1b25d 100644 --- a/pennylane_lightning/core/src/utils/UtilLinearAlg.hpp +++ b/pennylane_lightning/core/src/utils/UtilLinearAlg.hpp @@ -150,7 +150,7 @@ void compute_diagonalizing_gates(int n, int lda, } } else { try { - scipyPathStr = currentPathStr + "/../../scipy.libs/"; + scipyPathStr = currentPathStr + "../../scipy.libs/"; // convert the relative path to absolute path scipyPathStr = std::filesystem::canonical(scipyPathStr).string(); From ffe097efec1f05b543bbd60110b2b42c84b57c9a Mon Sep 17 00:00:00 2001 From: Shuli Shu <08cnbj@gmail.com> Date: Mon, 15 Jul 2024 21:16:53 +0000 Subject: [PATCH 06/10] initial commit --- pennylane_lightning/core/src/utils/UtilLinearAlg.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pennylane_lightning/core/src/utils/UtilLinearAlg.hpp b/pennylane_lightning/core/src/utils/UtilLinearAlg.hpp index 818ff1b25d..727222ed34 100644 --- a/pennylane_lightning/core/src/utils/UtilLinearAlg.hpp +++ b/pennylane_lightning/core/src/utils/UtilLinearAlg.hpp @@ -150,7 +150,7 @@ void compute_diagonalizing_gates(int n, int lda, } } else { try { - scipyPathStr = currentPathStr + "../../scipy.libs/"; + scipyPathStr = currentPathStr + "/../../scipy.libs/"; // convert the relative path to absolute path scipyPathStr = std::filesystem::canonical(scipyPathStr).string(); From def4407fd335bd7683652ae132adedfe42cbf54a Mon Sep 17 00:00:00 2001 From: ringo-but-quantum Date: Tue, 16 Jul 2024 15:02:07 +0000 Subject: [PATCH 07/10] Auto update version from '0.38.0-dev7' to '0.38.0-dev8' --- pennylane_lightning/core/_version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pennylane_lightning/core/_version.py b/pennylane_lightning/core/_version.py index f695fbccc5..02e7041e1a 100644 --- a/pennylane_lightning/core/_version.py +++ b/pennylane_lightning/core/_version.py @@ -16,4 +16,4 @@ Version number (major.minor.patch[-label]) """ -__version__ = "0.38.0-dev7" +__version__ = "0.38.0-dev8" From 275053806b10b300dd70e8595bf29cf8b238664e Mon Sep 17 00:00:00 2001 From: Shuli Shu <08cnbj@gmail.com> Date: Tue, 16 Jul 2024 15:03:35 +0000 Subject: [PATCH 08/10] Trigger CIs From 8a21be2b7cd1e09fdd230fc5fbcc0eacb8008a92 Mon Sep 17 00:00:00 2001 From: Shuli Shu <08cnbj@gmail.com> Date: Tue, 16 Jul 2024 15:46:43 +0000 Subject: [PATCH 09/10] fix changes --- pennylane_lightning/core/src/utils/UtilLinearAlg.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pennylane_lightning/core/src/utils/UtilLinearAlg.hpp b/pennylane_lightning/core/src/utils/UtilLinearAlg.hpp index 727222ed34..818ff1b25d 100644 --- a/pennylane_lightning/core/src/utils/UtilLinearAlg.hpp +++ b/pennylane_lightning/core/src/utils/UtilLinearAlg.hpp @@ -150,7 +150,7 @@ void compute_diagonalizing_gates(int n, int lda, } } else { try { - scipyPathStr = currentPathStr + "/../../scipy.libs/"; + scipyPathStr = currentPathStr + "../../scipy.libs/"; // convert the relative path to absolute path scipyPathStr = std::filesystem::canonical(scipyPathStr).string(); From bfeb2afd1d6f09a6a482bed6965df78c34f33698 Mon Sep 17 00:00:00 2001 From: ringo-but-quantum Date: Wed, 17 Jul 2024 15:25:17 +0000 Subject: [PATCH 10/10] Auto update version from '0.38.0-dev8' to '0.38.0-dev9' --- pennylane_lightning/core/_version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pennylane_lightning/core/_version.py b/pennylane_lightning/core/_version.py index 02e7041e1a..0ddab39d02 100644 --- a/pennylane_lightning/core/_version.py +++ b/pennylane_lightning/core/_version.py @@ -16,4 +16,4 @@ Version number (major.minor.patch[-label]) """ -__version__ = "0.38.0-dev8" +__version__ = "0.38.0-dev9"