From da65006fb845b4ac683ae221d192d499c16bd00e Mon Sep 17 00:00:00 2001
From: cmoineau <cyril.moineau@cea.fr>
Date: Mon, 27 Nov 2023 14:35:27 +0000
Subject: [PATCH] Add Pad operator to python binding.

---
 python_binding/operator/pybind_Pad.cpp | 66 ++++++++++++++++++++++++++
 python_binding/pybind_core.cpp         |  3 ++
 2 files changed, 69 insertions(+)
 create mode 100644 python_binding/operator/pybind_Pad.cpp

diff --git a/python_binding/operator/pybind_Pad.cpp b/python_binding/operator/pybind_Pad.cpp
new file mode 100644
index 000000000..0956d6260
--- /dev/null
+++ b/python_binding/operator/pybind_Pad.cpp
@@ -0,0 +1,66 @@
+/********************************************************************************
+ * 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 <pybind11/stl.h>
+#include <iostream>
+#include <string>
+#include <vector>
+#include <array>
+
+#include "aidge/backend/OperatorImpl.hpp"
+#include "aidge/operator/Pad.hpp"
+#include "aidge/operator/Operator.hpp"
+#include "aidge/utils/Types.h"
+
+namespace py = pybind11;
+namespace Aidge {
+
+template <DimIdx_t DIM> void declare_PadOp(py::module &m) {
+  py::class_<Pad_Op<DIM>, std::shared_ptr<Pad_Op<DIM>>, Operator, Attributes>(
+    m, ("PadOp" + std::to_string(DIM) + "D").c_str(),
+    py::multiple_inheritance())
+  .def(py::init<const std::array<DimSize_t, 2*DIM> &,
+                const PadBorderType &,
+                double>(),
+        py::arg("beginEndTuples"),
+        py::arg("borderType") = PadBorderType::Constant,
+        py::arg("borderValue") = 0.0)
+    .def("get_inputs_name", &Pad_Op<DIM>::getInputsName)
+    .def("get_outputs_name", &Pad_Op<DIM>::getOutputsName)
+    ;
+
+  m.def(("Pad" + std::to_string(DIM) + "D").c_str(), [](const std::vector<DimSize_t>& beginEndTuples,
+                                                        const std::string& name,
+                                                        const PadBorderType &borderType = PadBorderType::Constant,
+                                                        double borderValue = 0.0) {
+        AIDGE_ASSERT(beginEndTuples.size() == 2*DIM, "begin_end_tuples size [%ld] does not match DIM [%d]", beginEndTuples.size(), 2*DIM);
+        return Pad<DIM>(to_array<2*DIM>(beginEndTuples.begin()), name, borderType, borderValue);
+    },
+       py::arg("begin_end_tuples"),
+       py::arg("name") = "",
+       py::arg("border_type") = PadBorderType::Constant,
+       py::arg("border_value") = 0.0);
+}
+
+
+void init_Pad(py::module &m) {
+  py::enum_<PadBorderType>(m, "pad_border_type")
+    .value("Constant", PadBorderType::Constant)
+    .value("Edge",     PadBorderType::Edge)
+    .value("Reflect",  PadBorderType::Reflect)
+    .value("Wrap",     PadBorderType::Wrap)
+    .export_values();
+  declare_PadOp<1>(m);
+  declare_PadOp<2>(m);
+  declare_PadOp<3>(m);
+}
+} // namespace Aidge
diff --git a/python_binding/pybind_core.cpp b/python_binding/pybind_core.cpp
index 9c8d8c61d..09f8f303c 100644
--- a/python_binding/pybind_core.cpp
+++ b/python_binding/pybind_core.cpp
@@ -34,6 +34,7 @@ void init_MaxPooling(py::module&);
 void init_MetaOperatorDefs(py::module&);
 void init_Mul(py::module&);
 void init_Producer(py::module&);
+void init_Pad(py::module&);
 void init_Pow(py::module&);
 void init_ReLU(py::module&);
 void init_Softmax(py::module&);
@@ -79,6 +80,8 @@ void init_Aidge(py::module& m){
     init_MaxPooling(m);
     init_MetaOperatorDefs(m);
     init_Mul(m);
+    init_Pad(m);
+
     init_Pow(m);
     init_ReLU(m);
     init_Softmax(m);
-- 
GitLab