Skip to content
Snippets Groups Projects
Commit 1c1faf4b authored by Olivier BICHLER's avatar Olivier BICHLER
Browse files

Merge branch 'lrn' into 'dev'

Add LRN operator

See merge request !255
parents 804f702d 476a99f1
No related branches found
No related tags found
2 merge requests!279v0.4.0,!255Add LRN operator
Pipeline #60127 passed
/********************************************************************************
* 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_ */
/********************************************************************************
* 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
...@@ -52,6 +52,7 @@ void init_GlobalAveragePooling(py::module&); ...@@ -52,6 +52,7 @@ void init_GlobalAveragePooling(py::module&);
void init_GridSample(py::module&); void init_GridSample(py::module&);
void init_Heaviside(py::module&); void init_Heaviside(py::module&);
void init_Identity(py::module&); void init_Identity(py::module&);
void init_LRN(py::module&);
void init_LeakyReLU(py::module&); void init_LeakyReLU(py::module&);
void init_MatMul(py::module&); void init_MatMul(py::module&);
void init_MaxPooling(py::module&); void init_MaxPooling(py::module&);
...@@ -144,6 +145,7 @@ void init_Aidge(py::module& m) { ...@@ -144,6 +145,7 @@ void init_Aidge(py::module& m) {
init_GridSample(m); init_GridSample(m);
init_Heaviside(m); init_Heaviside(m);
init_Identity(m); init_Identity(m);
init_LRN(m);
init_LeakyReLU(m); init_LeakyReLU(m);
init_MatMul(m); init_MatMul(m);
init_MaxPooling(m); init_MaxPooling(m);
......
/********************************************************************************
* 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment