From c994acd5fc7e065e5e355d61172707b7578f22bf Mon Sep 17 00:00:00 2001
From: cmoineau <cyril.moineau@cea.fr>
Date: Wed, 15 Nov 2023 08:31:59 +0000
Subject: [PATCH] Add an Identity cell + bind it.

---
 include/aidge/operator/Identity.hpp         | 143 ++++++++++++++++++++
 python_binding/operator/pybind_Identity.cpp |  27 ++++
 python_binding/pybind_core.cpp              |   2 +
 3 files changed, 172 insertions(+)
 create mode 100644 include/aidge/operator/Identity.hpp
 create mode 100644 python_binding/operator/pybind_Identity.cpp

diff --git a/include/aidge/operator/Identity.hpp b/include/aidge/operator/Identity.hpp
new file mode 100644
index 000000000..003990ecc
--- /dev/null
+++ b/include/aidge/operator/Identity.hpp
@@ -0,0 +1,143 @@
+/********************************************************************************
+ * 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
+ *
+ ********************************************************************************/
+
+#ifndef AIDGE_CORE_OPERATOR_IDENTITY_H_
+#define AIDGE_CORE_OPERATOR_IDENTITY_H_
+
+#include <cassert>
+#include <memory>
+#include <vector>
+
+#include "aidge/utils/Registrar.hpp"
+#include "aidge/operator/Operator.hpp"
+#include "aidge/backend/OperatorImpl.hpp"
+#include "aidge/data/Tensor.hpp"
+#include "aidge/data/Data.hpp"
+#include "aidge/graph/Node.hpp"
+#include "aidge/utils/Types.h"
+
+namespace Aidge {
+
+/**
+ * @brief Indentity_Op is an helper operator made to ease the declaration of MetaNodes.
+ * This Operator has no Implementation, it just forward its input Tensor.
+ * Note: Error may occur if new methods are added in Operator which use an implementation.
+ * Has we need to update this class to remove the use of Impl.
+ *
+ */
+class Identity_Op : public Operator,
+    public Registrable<Identity_Op, std::string, std::unique_ptr<OperatorImpl>(const Identity_Op&)> {
+public:
+    // FIXME: change accessibility
+    std::shared_ptr<Tensor> mInput = std::make_shared<Tensor>();
+
+public:
+    static constexpr const char* Type = "Identity";
+
+    Identity_Op()
+            : Operator(Type)
+    {
+        setDatatype(DataType::Float32);
+        mImpl = std::make_shared<OperatorImpl>(*this);
+    }
+
+    /**
+     * @brief Copy-constructor. Copy the operator attributes and its output tensor(s), but not its input tensors (the new operator has no input associated).
+     * @param op Operator to copy.
+     */
+    Identity_Op(const Identity_Op& op)
+        : Operator(Type)
+    {
+        // cpy-ctor
+        setDatatype(op.mInput->dataType());
+        mImpl = std::make_shared<OperatorImpl>(*this);
+    }
+
+    /**
+     * @brief Clone the operator using its copy-constructor.
+     * @see Operator::Identity_Op
+     */
+    std::shared_ptr<Operator> clone() const override {
+        return std::make_shared<Identity_Op>(*this);
+    }
+
+    void associateInput(const IOIndex_t inputIdx, std::shared_ptr<Data> data) override final {
+        assert(inputIdx == 0 && "operator supports only 1 input");
+        (void) inputIdx; // avoid unused warning
+        assert(strcmp(data->type(), Tensor::Type)==0 && "input data must be of Tensor type");
+        mInput = std::dynamic_pointer_cast<Tensor>(data);
+    }
+
+    void computeOutputDims() override final {} // Do nothing
+
+    bool outputDimsForwarded() const override final {
+        return true;
+    }
+
+
+    void forward() override { runHooks(); }
+
+    void backward() override { }
+
+    inline Tensor& input(const IOIndex_t /*inputIdx*/) const override final { return *(mInput.get()); }
+    // output = input
+    inline Tensor& output(const IOIndex_t /*outputIdx*/) const override final { return *(mInput.get()); }
+
+
+    inline std::shared_ptr<Tensor> getInput(const IOIndex_t inputIdx) const override final {
+        assert((inputIdx == 0) && "ReLU Operator has only 1 input");
+        (void) inputIdx; // avoid unused warning
+        return mInput;
+    }
+    inline std::shared_ptr<Tensor> getOutput(const IOIndex_t outputIdx) const override final {
+        assert((outputIdx == 0) && "Identity Operator has only 1 output");
+        (void) outputIdx; // avoid unused warning
+        return mInput;
+    }
+
+
+    std::shared_ptr<Data> getRawInput(const IOIndex_t inputIdx) const override final {
+        assert(inputIdx == 0 && "operator supports only 1 input");
+        (void) inputIdx; // avoid unused warning
+        return std::static_pointer_cast<Data>(mInput);
+    }
+    std::shared_ptr<Data> getRawOutput(const IOIndex_t outputIdx) const override final {
+        assert(outputIdx == 0 && "operator supports only 1 output");
+        (void) outputIdx; // avoid unused warning
+        return std::static_pointer_cast<Data>(mInput);
+    }
+
+
+
+    void setBackend(const std::string& name) override {
+        // setBackend do nothing, Identity node has no backend it just pass the same Tensor
+    }
+    void setDatatype(const DataType& datatype) override {
+        // setDatatype do nothing, Identity node has no backend it just pass the same Tensor
+    }
+
+    inline IOIndex_t nbInputs() const noexcept override final { return 1; }
+    inline IOIndex_t nbDataInputs() const noexcept override final { return 1; }
+    inline IOIndex_t nbOutputs() const noexcept override final { return 1; }
+    static const std::vector<std::string> getInputsName(){
+        return {"data_input"};
+    }
+    static const std::vector<std::string> getOutputsName(){
+        return {"data_output"};
+    }
+};
+
+inline std::shared_ptr<Node> Identity(const std::string& name = "") {
+    return std::make_shared<Node>(std::make_shared<Identity_Op>(), name);
+}
+}
+
+#endif /* AIDGE_CORE_OPERATOR_IDENTITY_H_ */
diff --git a/python_binding/operator/pybind_Identity.cpp b/python_binding/operator/pybind_Identity.cpp
new file mode 100644
index 000000000..b1b1e8888
--- /dev/null
+++ b/python_binding/operator/pybind_Identity.cpp
@@ -0,0 +1,27 @@
+/********************************************************************************
+ * 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/operator/Identity.hpp"
+#include "aidge/operator/Operator.hpp"
+
+namespace py = pybind11;
+namespace Aidge {
+
+void init_Identity(py::module& m) {
+    py::class_<Identity_Op, std::shared_ptr<Identity_Op>, Operator>(m, "IdentityOp", py::multiple_inheritance())
+    .def("get_inputs_name", &Identity_Op::getInputsName)
+    .def("get_outputs_name", &Identity_Op::getOutputsName);
+
+    m.def("Identity", &Identity, py::arg("name") = "");
+}
+}  // namespace Aidge
diff --git a/python_binding/pybind_core.cpp b/python_binding/pybind_core.cpp
index 6cc597b5e..9c8d8c61d 100644
--- a/python_binding/pybind_core.cpp
+++ b/python_binding/pybind_core.cpp
@@ -39,6 +39,7 @@ void init_ReLU(py::module&);
 void init_Softmax(py::module&);
 void init_Sqrt(py::module&);
 void init_Sub(py::module&);
+void init_Identity(py::module&);
 
 void init_Node(py::module&);
 void init_GraphView(py::module&);
@@ -83,6 +84,7 @@ void init_Aidge(py::module& m){
     init_Softmax(m);
     init_Sqrt(m);
     init_Sub(m);
+    init_Identity(m);
 
     init_Producer(m);
     init_GraphRegex(m);
-- 
GitLab