diff --git a/include/aidge/aidge.hpp b/include/aidge/aidge.hpp index c6d759ce10ddec19341ad226abbccaef5cbab382..8a07b165e5e25ce7d40429d4fe256a57364252e6 100644 --- a/include/aidge/aidge.hpp +++ b/include/aidge/aidge.hpp @@ -38,6 +38,7 @@ #include "aidge/operator/MaxPooling.hpp" #include "aidge/operator/MetaOperator.hpp" #include "aidge/operator/MetaOperatorDefs.hpp" +#include "aidge/operator/Mul.hpp" #include "aidge/operator/Operator.hpp" #include "aidge/operator/Pad.hpp" #include "aidge/operator/Producer.hpp" diff --git a/include/aidge/operator/Mul.hpp b/include/aidge/operator/Mul.hpp new file mode 100644 index 0000000000000000000000000000000000000000..4ea79fe52622b22f8ea8fbd9191d50d45e26acac --- /dev/null +++ b/include/aidge/operator/Mul.hpp @@ -0,0 +1,146 @@ +/******************************************************************************** + * 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_MUL_H_ +#define AIDGE_CORE_OPERATOR_MUL_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 { + +class Mul_Op : public Operator, + public Registrable<Mul_Op, std::string, std::unique_ptr<OperatorImpl>(const Mul_Op&)> { +public: + // FIXME: change accessibility + std::array<std::shared_ptr<Tensor>, 2> mInputs = {std::make_shared<Tensor>(), std::make_shared<Tensor>()}; + const std::shared_ptr<Tensor> mOutput = std::make_shared<Tensor>(); + +public: + static constexpr const char* Type = "Mul"; + + Mul_Op() + : Operator(Type) + { + setDatatype(DataType::Float32); + } + + /** + * @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. + */ + Mul_Op(const Mul_Op& op) + : Operator(Type), + mOutput(std::make_shared<Tensor>(*op.mOutput)) + { + // cpy-ctor + setDatatype(op.mOutput->dataType()); + mImpl = op.mImpl ? Registrar<Mul_Op>::create(mOutput->getImpl()->backend())(*this) : nullptr; + } + + /** + * @brief Clone the operator using its copy-constructor. + * @see Operator::Mul_Op + */ + std::shared_ptr<Operator> clone() const override { + return std::make_shared<Mul_Op>(*this); + } + + void associateInput(const IOIndex_t inputIdx, std::shared_ptr<Data> data) override final { + assert(inputIdx < 2 && "operator supports only 2 inputs"); + (void) inputIdx; // avoid unused warning + assert(strcmp(data->type(), Tensor::Type)==0 && "input data must be of Tensor type"); + mInputs[inputIdx] = std::dynamic_pointer_cast<Tensor>(data); + } + + void computeOutputDims() override final { + if (!mInputs[0]->empty()) + mOutput->resize(mInputs[0]->dims()); + } + + bool outputDimsForwarded() const override final { + return !(mOutput->empty()); + } + + + inline Tensor& input(const IOIndex_t inputIdx) const override final { + assert(static_cast<std::size_t>(inputIdx) < 2 && "wrong inputIdx for Add operator."); + return *(mInputs[inputIdx].get()); + } + inline Tensor& output(const IOIndex_t /*outputIdx*/) const override final { return *(mOutput.get()); } + + + inline std::shared_ptr<Tensor> getInput(const IOIndex_t inputIdx) const override final { + assert((inputIdx < 2) && "Mul Operator has 2 inputs"); + (void) inputIdx; // avoid unused warning + return mInputs[inputIdx]; + } + inline std::shared_ptr<Tensor> getOutput(const IOIndex_t outputIdx) const override final { + assert((outputIdx == 0) && "Mul Operator has only 1 output"); + (void) outputIdx; // avoid unused warning + return mOutput; + } + + + std::shared_ptr<Data> getRawInput(const IOIndex_t inputIdx) const override final { + assert(inputIdx < 2 && "operator supports only 2 inputs"); + (void) inputIdx; // avoid unused warning + return std::static_pointer_cast<Data>(mInputs[inputIdx]); + } + 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>(mOutput); + } + + + void setBackend(const std::string& name) override { + mImpl = Registrar<Mul_Op>::create(name)(*this); + mOutput->setBackend(name); + + // FIXME: temporary workaround + mInputs[0]->setBackend(name); + mInputs[1]->setBackend(name); + } + void setDatatype(const DataType& datatype) override { + mOutput->setDatatype(datatype); + + // FIXME: temporary workaround + mInputs[0]->setDatatype(datatype); + mInputs[1]->setDatatype(datatype); + } + + inline IOIndex_t nbInputs() const noexcept override final { return 2; } + inline IOIndex_t nbDataInputs() const noexcept override final { return 2; } + 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> Mul(const std::string& name = "") { + return std::make_shared<Node>(std::make_shared<Mul_Op>(), name); +} +} + +#endif /* AIDGE_CORE_OPERATOR_MUL_H_ */ diff --git a/python_binding/operator/pybind_Mul.cpp b/python_binding/operator/pybind_Mul.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2627c99005b009769e8fbb97b1f5d79e2424c997 --- /dev/null +++ b/python_binding/operator/pybind_Mul.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/Mul.hpp" +#include "aidge/operator/Operator.hpp" + +namespace py = pybind11; +namespace Aidge { + +void init_Mul(py::module& m) { + py::class_<Mul_Op, std::shared_ptr<Mul_Op>, Operator>(m, "MulOp", py::multiple_inheritance()) + .def("get_inputs_name", &Mul_Op::getInputsName) + .def("get_outputs_name", &Mul_Op::getOutputsName); + + m.def("Mul", &Mul, py::arg("name") = ""); +} +} // namespace Aidge diff --git a/python_binding/pybind_core.cpp b/python_binding/pybind_core.cpp index edd9090a137c2eb1259e659766e58ce220b6a59e..4a1c46d999ced651b8c746042ed96a44c13cadf7 100644 --- a/python_binding/pybind_core.cpp +++ b/python_binding/pybind_core.cpp @@ -32,6 +32,7 @@ void init_LeakyReLU(py::module&); void init_MatMul(py::module&); void init_MaxPooling(py::module&); void init_MetaOperatorDefs(py::module&); +void init_Mul(py::module&); void init_Producer(py::module&); void init_Pow(py::module&); void init_ReLU(py::module&); @@ -76,6 +77,7 @@ void init_Aidge(py::module& m){ init_MatMul(m); init_MaxPooling(m); init_MetaOperatorDefs(m); + init_Mul(m); init_Pow(m); init_ReLU(m); init_Softmax(m);