diff --git a/include/aidge/operator/LRN.hpp b/include/aidge/operator/LRN.hpp new file mode 100644 index 0000000000000000000000000000000000000000..669564d4a8fb0da2fac4f67e164fc08653131472 --- /dev/null +++ b/include/aidge/operator/LRN.hpp @@ -0,0 +1,84 @@ +/******************************************************************************** + * 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_LRN_H_ +#define AIDGE_CORE_OPERATOR_LRN_H_ + +#include <memory> +#include <vector> + +#include "aidge/backend/OperatorImpl.hpp" +#include "aidge/graph/Node.hpp" +#include "aidge/operator/OperatorTensor.hpp" +#include "aidge/operator/Producer.hpp" +#include "aidge/utils/Registrar.hpp" +#include "aidge/utils/StaticAttributes.hpp" +#include "aidge/utils/Types.h" + +namespace Aidge { +enum class LRNAttr { Alpha, Beta, Bias, Size }; + +class LRN_Op : public OperatorTensor, + public Registrable<LRN_Op, + std::string, + std::function<std::shared_ptr<OperatorImpl>(const LRN_Op&)>> { + +public: + static const std::string Type; + +private: + using Attributes_ = StaticAttributes<LRNAttr, float, float, float, std::int32_t>; + template <LRNAttr e> using attr = typename Attributes_::template attr<e>; + const std::shared_ptr<Attributes_> mAttributes; + +public: + LRN_Op() = delete; + + LRN_Op(std::int32_t size); + + /** + * @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. + */ + LRN_Op(const LRN_Op& op); + + /** + * @brief Clone the operator using its copy-constructor. + * @see Operator::LRN_Op + */ + std::shared_ptr<Operator> clone() const override; + + void setBackend(const std::string& name, DeviceIdx_t device = 0) override final; + std::set<std::string> getAvailableBackends() const override; + + inline std::shared_ptr<Attributes> attributes() const override { return mAttributes; } + inline float& alpha() const noexcept { return mAttributes -> getAttr<LRNAttr::Alpha>(); } + inline float& beta() const noexcept { return mAttributes -> getAttr<LRNAttr::Beta>(); } + inline float& bias() const noexcept { return mAttributes -> getAttr<LRNAttr::Bias>(); } + inline std::int32_t& size() const noexcept { return mAttributes -> getAttr<LRNAttr::Size>(); } + + static const std::vector<std::string> getInputsName(){ + return {"data_input"}; + } + static const std::vector<std::string> getOutputsName(){ + return {"data_output"}; + } +}; + +std::shared_ptr<Node> LRN(std::int32_t size, const std::string& name = ""); +} // namespace Aidge + +namespace { +template <> +const char *const EnumStrings<Aidge::LRNAttr>::data[] = {"alpha", "beta", "bias", "size"}; +} + +#endif /* AIDGE_CORE_OPERATOR_LRN_H_ */ diff --git a/python_binding/operator/pybind_LRN.cpp b/python_binding/operator/pybind_LRN.cpp new file mode 100644 index 0000000000000000000000000000000000000000..178af540b3efb280eaba67572e07cb673e563b22 --- /dev/null +++ b/python_binding/operator/pybind_LRN.cpp @@ -0,0 +1,31 @@ +/******************************************************************************** + * 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 <string> + +#include "aidge/data/Tensor.hpp" +#include "aidge/operator/LRN.hpp" +#include "aidge/operator/OperatorTensor.hpp" + +namespace py = pybind11; +namespace Aidge { + +void init_LRN(py::module& m) { + py::class_<LRN_Op, std::shared_ptr<LRN_Op>, OperatorTensor>(m, "LRNOp", py::multiple_inheritance()) + .def(py::init<std::int32_t>(), py::arg("size")) + .def_static("get_inputs_name", &LRN_Op::getInputsName) + .def_static("get_outputs_name", &LRN_Op::getOutputsName) + .def_readonly_static("Type", &LRN_Op::Type); + declare_registrable<LRN_Op>(m, "LRNOp"); + m.def("LRN", &LRN, py::arg("size"), py::arg("name") = ""); +} +} // namespace Aidge diff --git a/python_binding/pybind_core.cpp b/python_binding/pybind_core.cpp index 2602108adf55c39f1fa86d5687286713e197bc82..7a88f61f518777e9b2e359f9c89eb1f3ebcd53d0 100644 --- a/python_binding/pybind_core.cpp +++ b/python_binding/pybind_core.cpp @@ -52,6 +52,7 @@ void init_GlobalAveragePooling(py::module&); void init_GridSample(py::module&); void init_Heaviside(py::module&); void init_Identity(py::module&); +void init_LRN(py::module&); void init_LeakyReLU(py::module&); void init_MatMul(py::module&); void init_MaxPooling(py::module&); @@ -144,6 +145,7 @@ void init_Aidge(py::module& m) { init_GridSample(m); init_Heaviside(m); init_Identity(m); + init_LRN(m); init_LeakyReLU(m); init_MatMul(m); init_MaxPooling(m); diff --git a/src/operator/LRN.cpp b/src/operator/LRN.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c5ce243bd6a48ae4b1ce8461924b498c804b53e6 --- /dev/null +++ b/src/operator/LRN.cpp @@ -0,0 +1,60 @@ +/******************************************************************************** + * 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 "aidge/operator/LRN.hpp" + +#include <memory> +#include <string> + +#include "aidge/data/Tensor.hpp" +#include "aidge/utils/Registrar.hpp" +#include "aidge/utils/Types.h" + +const std::string Aidge::LRN_Op::Type = "LRN"; + +Aidge::LRN_Op::LRN_Op(std::int32_t size) + : OperatorTensor(Type, {InputCategory::Data}, 1), + mAttributes(std::make_shared<Attributes_>( + attr<LRNAttr::Alpha>(0.0001), + attr<LRNAttr::Beta>(0.75), + attr<LRNAttr::Bias>(1.0), + attr<LRNAttr::Size>(size))) +{} + +Aidge::LRN_Op::LRN_Op(const Aidge::LRN_Op& op) + : OperatorTensor(op), + mAttributes(op.mAttributes) +{ + if (op.mImpl){ + SET_IMPL_MACRO(LRN_Op, *this, op.backend()); + }else{ + mImpl = nullptr; + } +} + +std::shared_ptr<Aidge::Operator> Aidge::LRN_Op::clone() const { + return std::make_shared<LRN_Op>(*this); +} + +void Aidge::LRN_Op::setBackend(const std::string& name, Aidge::DeviceIdx_t device) { + mImpl = Registrar<LRN_Op>::create(name)(*this); + mOutputs[0]->setBackend(name, device); +} + +std::set<std::string> Aidge::LRN_Op::getAvailableBackends() const { + return Registrar<LRN_Op>::getKeys(); +} + +//////////////////////////////////////////////// + +std::shared_ptr<Aidge::Node> Aidge::LRN(std::int32_t size, const std::string& name) { + return std::make_shared<Node>(std::make_shared<LRN_Op>(size), name); +} \ No newline at end of file