Skip to content
Snippets Groups Projects

[Add] Clip Operator

Merged Noam Zerah requested to merge noamzerah/aidge_core:clipping_node_remove_attr into dev
Files
6
+ 122
0
/********************************************************************************
* 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_CLIP_H_
#define AIDGE_CORE_OPERATOR_CLIP_H_
#include <memory>
#include <vector>
#include <limits>
#include "aidge/backend/OperatorImpl.hpp"
#include "aidge/graph/Node.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 ClipAttr { Min, Max };
class Clip_Op : public OperatorTensor,
public Registrable<Clip_Op, std::string, std::function<std::shared_ptr<OperatorImpl>(const Clip_Op&)>> {
public:
static const std::string Type;
private:
using Attributes_ = StaticAttributes<ClipAttr, float, float>;
template <ClipAttr e> using attr = typename Attributes_::template attr<e>;
const std::shared_ptr<Attributes_> mAttributes;
public:
Clip_Op() = delete;
Clip_Op(float min,float max) :
OperatorTensor(Type, {InputCategory::Data,InputCategory::OptionalData,InputCategory::OptionalData}, 1),
mAttributes(std::make_shared<Attributes_>(attr<ClipAttr::Min>(min), attr<ClipAttr::Max>(max)))
{}
/**
* @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.
*/
Clip_Op(const Clip_Op& op)
: OperatorTensor(op),
mAttributes(op.mAttributes)
{
if (op.mImpl){
SET_IMPL_MACRO(Clip_Op, *this, op.backend());
}else{
mImpl = nullptr;
}
}
/**
* @brief Clone the operator using its copy-constructor.
* @see Operator::Clip_Op
*/
std::shared_ptr<Operator> clone() const override
{
return std::make_shared<Clip_Op>(*this);
}
bool dimsForwarded() const override final;
bool forwardDims(bool allowDataDependency = false) override final;
/**
* @brief Setter to specify the backend to use
*/
void setBackend(const std::string& name, DeviceIdx_t device = 0) override final;
inline std::shared_ptr<Attributes> attributes() const override { return mAttributes; }
/**
* @brief Getter and Setter for min attribute value
* @return float&
*/
inline float& min() const noexcept { return mAttributes->getAttr<ClipAttr::Min>(); }
/**
* @brief Getter and Setter for max attribute value
* @return float&
*/
inline float& max() const noexcept { return mAttributes->getAttr<ClipAttr::Max>(); }
std::set<std::string> getAvailableBackends() const override;
static const std::vector<std::string> getInputsName(){
return {"data_input","min_empty_tensor","max_empty_tensor"};
}
static const std::vector<std::string> getOutputsName(){
return {"data_output"};
}
};
/**
* @brief The Clip operator is a tensor operator that performs a clipping operation on tensor elements.
This class allows limiting tensor values to a specified range, defined by the min
and max parameters (Tensors of empty shapes). Values outside this range are replaced by the corresponding limit values
When ‘min’ is greater than ‘max’, the clip operator sets all the ‘input’ values to the value of ‘max’
* @param[in] name Name of the node
* @param[in] min Min clip value as attribute
* @param[in] max Max clip value as attribute
* @return std::shared_ptr<Node>
*/
std::shared_ptr<Aidge::Node> Clip(
const std::string &name = "",
float min = std::numeric_limits<float>::lowest(),
float max = std::numeric_limits<float>::max()
);
}
namespace {
template <>
const char* const EnumStrings<Aidge::ClipAttr>::data[]
= {"min", "max"};
}
#endif /* AIDGE_CORE_OPERATOR_CLIP_H_ */
Loading