From f570c78dfafcabc82f0e1219452842a127e0f5ee Mon Sep 17 00:00:00 2001 From: cmoineau <cyril.moineau@cea.fr> Date: Tue, 29 Oct 2024 08:51:57 +0000 Subject: [PATCH] Add pybind support for GridSample. --- .../operator/pybind_DepthToSpace.cpp | 59 +++++++++++++++++++ python_binding/pybind_core.cpp | 2 + 2 files changed, 61 insertions(+) create mode 100644 python_binding/operator/pybind_DepthToSpace.cpp diff --git a/python_binding/operator/pybind_DepthToSpace.cpp b/python_binding/operator/pybind_DepthToSpace.cpp new file mode 100644 index 000000000..efb8a7406 --- /dev/null +++ b/python_binding/operator/pybind_DepthToSpace.cpp @@ -0,0 +1,59 @@ +/******************************************************************************** + * Copyright (c) 2023 CEA-List + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * SPDX-License-Identifier: EPL-2.0 + * + ********************************************************************************/ + +#include <pybind11/pybind11.h> + +#include "aidge/backend/OperatorImpl.hpp" +#include "aidge/data/Tensor.hpp" +#include "aidge/operator/DepthToSpace.hpp" +#include "aidge/operator/OperatorTensor.hpp" +#include "aidge/utils/Attributes.hpp" +#include "aidge/utils/Types.h" + +static typename Aidge::DepthToSpace_Op::Mode stringToMode(const std::string& mode) { +static std::unordered_map<std::string, typename Aidge::DepthToSpace_Op::Mode> map = { + {"DCR", Aidge::DepthToSpace_Op::Mode::DCR}, + {"CRD", Aidge::DepthToSpace_Op::Mode::CRD} +}; +return map[mode]; +} + +namespace py = pybind11; +namespace Aidge { + +void declare_DepthToSpace(py::module &m) { + + py::class_<DepthToSpace_Op, std::shared_ptr<DepthToSpace_Op>, OperatorTensor> (m, "DepthToSpaceOp", py::multiple_inheritance()) + .def(py::init([](const std::uint32_t blockSize, const std::string& mode) { + return new DepthToSpace_Op(blockSize, stringToMode(mode)); + }), py::arg("block_size"), py::arg("mode") = "CRD") + .def_static("get_inputs_name", &DepthToSpace_Op::getInputsName) + .def_static("get_outputs_name", &DepthToSpace_Op::getOutputsName) + .def_readonly_static("Type", &DepthToSpace_Op::Type) + .def("__repr__", [](DepthToSpace_Op& b) { + return fmt::format("Operator(type='{}')", b.Type); + }); + + declare_registrable<DepthToSpace_Op>(m, "DepthToSpaceOp"); + + m.def("DepthToSpace", []( + const std::uint32_t blockSize, + const std::string& mode, + const std::string& name) { + return DepthToSpace(blockSize, stringToMode(mode), name); + }, py::arg("block_size"), py::arg("mode") = "CRD", py::arg("name") = ""); +} + +void init_DepthToSpace(py::module &m) { + declare_DepthToSpace(m); +} + +} // namespace Aidge diff --git a/python_binding/pybind_core.cpp b/python_binding/pybind_core.cpp index fe471d122..53e5ec0fe 100644 --- a/python_binding/pybind_core.cpp +++ b/python_binding/pybind_core.cpp @@ -40,6 +40,7 @@ void init_Concat(py::module&); void init_ConstantOfShape(py::module&); void init_Conv(py::module&); void init_ConvDepthWise(py::module&); +void init_DepthToSpace(py::module&); void init_Div(py::module&); void init_Erf(py::module&); void init_FC(py::module&); @@ -126,6 +127,7 @@ void init_Aidge(py::module& m) { init_Conv(m); init_ConvDepthWise(m); init_ConstantOfShape(m); + init_DepthToSpace(m); init_Div(m); init_Erf(m); init_FC(m); -- GitLab