Skip to content
Snippets Groups Projects

Added Bitshift Operator

Merged Noam Zerah requested to merge noamzerah/aidge_core:feat_operator_bitshift into dev
Files
6
+ 124
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_BITSHIFT_H_
#define AIDGE_CORE_OPERATOR_BITSHIFT_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"
#include "aidge/utils/StaticAttributes.hpp"
namespace Aidge {
enum class BitShiftAttr { BitShiftdirection };
/**
* @brief Tensor BitShift Operator
*/
class BitShift_Op : public OperatorTensor,
public Registrable<BitShift_Op, std::string, std::shared_ptr<OperatorImpl>(const BitShift_Op&)> {
public:
enum BitShiftDirection {left,right};
static const std::string Type;
private:
using Attributes_ = StaticAttributes<BitShiftAttr,BitShiftDirection>;
template <BitShiftAttr e> using attr = typename Attributes_::template attr<e>;
const std::shared_ptr<Attributes_> mAttributes;
public:
BitShift_Op(BitShiftDirection direction)
: OperatorTensor(Type, {InputCategory::Data, InputCategory::Data}, 1),
mAttributes(std::make_shared<Attributes_>(
attr<BitShiftAttr::BitShiftdirection>(direction)))
{}
/**¨PPPP
* @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.
*/
BitShift_Op(const BitShift_Op& op)
: OperatorTensor(op),mAttributes(op.mAttributes)
{
if (op.mImpl) {
SET_IMPL_MACRO(BitShift_Op, *this, op.backend());
} else {
mImpl = nullptr;
}
}
/**
* @brief Clone the operator using its copy-constructor.
* @see Operator::BitShift_Op
*/
std::shared_ptr<Operator> clone() const override {
return std::make_shared<BitShift_Op>(*this);
}
bool forwardDims(bool allowDataDependency = false) override final;
/**
* @brief Setter to specify which backend to use
*
* @return Boolean
*/
void setBackend(const std::string& name, DeviceIdx_t device = 0) override;
/**
* @brief Getter to retrieve Attributes of the bitshift class
*
* @return Attributes
*/
inline std::shared_ptr<Attributes> attributes() const override { return mAttributes; }
/**
* @brief Retrieve the direction in which the shift should be applied (right or left)
*
* @return BitShiftDirection
*/
inline BitShiftDirection& direction() const noexcept { return mAttributes ->template getAttr<BitShiftAttr::BitShiftdirection>(); }
static const std::vector<std::string> getInputsName(){
return {"InputTensor", "ShiftAmount"};
}
static const std::vector<std::string> getOutputsName(){
return {"OutputTensor"};
}
};
/**
* @brief The bitwise shift operator performs an element-wise operation between the input tensor and the shift tensor in
the direction specified by "direction"
* @param[in] direction Direction of the bitshift (Left or Right)
* @param[in] name Name of the node
* @return std::shared_ptr<Node>
*/
inline std::shared_ptr<Node> BitShift(const BitShift_Op::BitShiftDirection direction, const std::string& name = "") {
return std::make_shared<Node>(std::make_shared<BitShift_Op>(direction), name);
}
} // namespace Aidge
namespace {
template <>
const char *const EnumStrings<Aidge::BitShiftAttr>::data[] = {"BitShiftdirection"};
}
#endif /* AIDGE_CORE_OPERATOR_BITSHIFT_H_ */
Loading