diff --git a/include/aidge/aidge.hpp b/include/aidge/aidge.hpp index f77ddb3d59a771779fc8362ec0d8ec705a4c2bc2..ec0329dd2db45bed05452c25bf06900b1758d009 100644 --- a/include/aidge/aidge.hpp +++ b/include/aidge/aidge.hpp @@ -36,6 +36,7 @@ #include "aidge/nodeTester/ConditionalInterpreter.hpp" #include "aidge/operator/Add.hpp" +#include "aidge/operator/And.hpp" #include "aidge/operator/ArgMax.hpp" #include "aidge/operator/AvgPooling.hpp" #include "aidge/operator/BatchNorm.hpp" diff --git a/include/aidge/operator/And.hpp b/include/aidge/operator/And.hpp new file mode 100644 index 0000000000000000000000000000000000000000..3739f954fb7660b2643d559eb6d272baac561abe --- /dev/null +++ b/include/aidge/operator/And.hpp @@ -0,0 +1,77 @@ +/******************************************************************************** + * Copyright (c) 2024 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_AND_H_ +#define AIDGE_CORE_OPERATOR_AND_H_ + +#include <memory> +#include <string> +#include <vector> + +#include "aidge/utils/Registrar.hpp" +#include "aidge/operator/OperatorTensor.hpp" +#include "aidge/backend/OperatorImpl.hpp" +#include "aidge/graph/Node.hpp" +#include "aidge/utils/Types.h" + +namespace Aidge { + +/** + * @brief Tensor element-wise logical and operation. + */ +class And_Op : public OperatorTensor, + public Registrable<And_Op, std::string, std::shared_ptr<OperatorImpl>(const And_Op&)> { +public: + static const std::string Type; + + And_Op() : OperatorTensor(Type, {InputCategory::Data, InputCategory::Data}, 1) {} + + /** + * @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. + */ + And_Op(const And_Op& op) + : OperatorTensor(op) + { + if (op.mImpl) { + SET_IMPL_MACRO(And_Op, *this, op.backend()); + } else { + mImpl = nullptr; + } + } + + /** + * @brief Clone the operator using its copy-constructor. + * @see Operator::And_Op + */ + std::shared_ptr<Operator> clone() const override { + return std::make_shared<And_Op>(*this); + } + + bool forwardDims(bool allowDataDependency = false) override final; + + void setBackend(const std::string& name, DeviceIdx_t device = 0) override; + + static const std::vector<std::string> getInputsName(){ + return {"data_input_1", "data_input_2"}; + } + static const std::vector<std::string> getOutputsName(){ + return {"data_output"}; + } +}; + +inline std::shared_ptr<Node> And(const std::string& name = "") { + return std::make_shared<Node>(std::make_shared<And_Op>(), name); +} +} // namespace Aidge + +#endif /* AIDGE_CORE_OPERATOR_AND_H_ */ diff --git a/python_binding/operator/pybind_And.cpp b/python_binding/operator/pybind_And.cpp new file mode 100644 index 0000000000000000000000000000000000000000..29448f73bfcdbc130c8e9566c74d8ebeafd5ff12 --- /dev/null +++ b/python_binding/operator/pybind_And.cpp @@ -0,0 +1,29 @@ +/******************************************************************************** + * Copyright (c) 2024 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/data/Tensor.hpp" +#include "aidge/operator/And.hpp" +#include "aidge/operator/OperatorTensor.hpp" + +namespace py = pybind11; +namespace Aidge { + +void init_And(py::module& m) { + py::class_<And_Op, std::shared_ptr<And_Op>, OperatorTensor>(m, "AndOp", py::multiple_inheritance()) + .def(py::init<>()) + .def_static("get_inputs_name", &And_Op::getInputsName) + .def_static("get_outputs_name", &And_Op::getOutputsName); + declare_registrable<And_Op>(m, "AndOp"); + m.def("And", &And, py::arg("name") = ""); +} +} // namespace Aidge diff --git a/python_binding/pybind_core.cpp b/python_binding/pybind_core.cpp index 9a65449debfb4fe5a0eb82359332f41d1ed3719e..bb83b9d32166b838d50b3b8c2de54520262a9674 100644 --- a/python_binding/pybind_core.cpp +++ b/python_binding/pybind_core.cpp @@ -28,6 +28,7 @@ void init_Operator(py::module&); void init_OperatorTensor(py::module&); void init_Add(py::module&); +void init_And(py::module&); void init_ArgMax(py::module&); void init_AvgPooling(py::module&); void init_BatchNorm(py::module&); @@ -101,6 +102,7 @@ void init_Aidge(py::module& m) { init_Operator(m); init_OperatorTensor(m); init_Add(m); + init_And(m); init_ArgMax(m); init_AvgPooling(m); init_BatchNorm(m); diff --git a/src/operator/And.cpp b/src/operator/And.cpp new file mode 100644 index 0000000000000000000000000000000000000000..43aeebe24ef0e6d0e0b820d1459f25d64e7054a7 --- /dev/null +++ b/src/operator/And.cpp @@ -0,0 +1,58 @@ +/******************************************************************************** + * Copyright (c) 2024 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 <cstddef> // std::size_t +#include <memory> +#include <stdexcept> // std::runtime_error +#include <string> +#include <vector> + +#include "aidge/backend/OperatorImpl.hpp" +#include "aidge/data/Tensor.hpp" +#include "aidge/operator/And.hpp" +#include "aidge/utils/ErrorHandling.hpp" +#include "aidge/utils/Types.h" + +const std::string Aidge::And_Op::Type = "And"; + +bool Aidge::And_Op::forwardDims(bool /*allowDataDependency*/) { + if (inputsAssociated()) { + const std::vector<std::size_t>& inputsDims0 = getInput(0)->dims(); + const std::vector<std::size_t>& inputsDims1 = getInput(1)->dims(); + + std::vector<std::size_t> outDims = (inputsDims0.size() >= inputsDims1.size()) ? inputsDims0 : inputsDims1; + const std::vector<std::size_t>& lowDims = (inputsDims0.size() < inputsDims1.size()) ? inputsDims0 : inputsDims1; + + std::size_t out_id = outDims.size() - 1; + std::size_t low_id = lowDims.size() - 1; + std::size_t i = 0; + while (i++ < lowDims.size()) { + if (outDims[out_id] == 1) { + outDims[out_id] = lowDims[low_id]; + } + else if ((lowDims[low_id] != 1) && (lowDims[low_id] != outDims[out_id])) { + AIDGE_THROW_OR_ABORT(std::runtime_error, "Incompatible Tensor shape for And Operation: {} for input#0 vs {} for input#1", + inputsDims0, inputsDims1); + } + --out_id; + --low_id; + } + mOutputs[0]->resize(outDims); + return true; + } + + return false; +} + +void Aidge::And_Op::setBackend(const std::string& name, Aidge::DeviceIdx_t device) { + SET_IMPL_MACRO(And_Op, *this, name); + mOutputs[0]->setBackend(name, device); +}