Skip to content
Snippets Groups Projects

[Add] Heaviside Operator

Merged Jerome Hue requested to merge jeromeh/aidge_core:heaviside-operator into dev
Files
6
+ 107
0
/********************************************************************************
* 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_HEAVISIDE_H_
#define AIDGE_CORE_OPERATOR_HEAVISIDE_H_
#include <memory>
#include <string>
#include <vector>
#include "aidge/backend/OperatorImpl.hpp"
#include "aidge/graph/Node.hpp"
#include "aidge/operator/Operator.hpp"
#include "aidge/operator/OperatorTensor.hpp"
#include "aidge/utils/Registrar.hpp"
#include "aidge/utils/StaticAttributes.hpp"
#include "aidge/utils/Types.h"
namespace Aidge {
enum class HeavisideAttr {
/**
* @brief The value used in the output tensor when the input is 0.
*/
Value
};
class Heaviside_Op
: public OperatorTensor,
public Registrable<
Heaviside_Op,
std::string,
std::function<std::shared_ptr<OperatorImpl>(const Heaviside_Op &)>> {
private:
using Attributes_ = StaticAttributes<HeavisideAttr, float>;
template <HeavisideAttr e>
using attr = typename Attributes_::template attr<e>;
const std::shared_ptr<Attributes_> mAttributes;
public:
static const std::string Type;
/*
* Compute the Heaviside step function for each element of the first input.
* Heaviside step function is defined as :
*
* \f[
* heaviside(input, values) = \begin{cases}
* 0 & \text{if input } < 0 \\
* values & \text{if input } = 0 \\
* 1 & \text{if input } > 0
* \end{cases}
* \f]
* */
Heaviside_Op(float value);
Heaviside_Op(const Heaviside_Op &op);
std::shared_ptr<Operator> clone() const override;
void setBackend(const std::string &name, DeviceIdx_t device = 0) override;
std::set<std::string> getAvailableBackends() const override;
static const std::vector<std::string> getInputsName() {
return {"data_input", "data_values"};
}
static const std::vector<std::string> getOutputsName() {
return {"output"};
}
inline std::shared_ptr<Attributes> attributes() const override {
return mAttributes;
}
inline float &value() const {
return mAttributes->template getAttr<HeavisideAttr::Value>();
}
};
/**
* @brief Create a Heaviside node.
*
* Initializes a Heaviside node that computes the Heaviside step function for each element
* of the input tensor, using the specified value for inputs equal to zero.
*
* @param value The value used in the output tensor when the input is 0.
* @param name Optional. The name of the node.
*
* @return A shared pointer to a Node representing the Heaviside operation.
*/
std::shared_ptr<Node> Heaviside(float value, const std::string &name = "");
} // namespace Aidge
namespace {
template <>
const char *const EnumStrings<Aidge::HeavisideAttr>::data[] = {"value"};
}
#endif /* AIDGE_CORE_OPERATOR_HEAVISIDE_H_ */
Loading