From a5622da95af69e262a6888a7ea3f2b6dffa1f095 Mon Sep 17 00:00:00 2001 From: Noam ZERAH <noam.zerah@cea.fr> Date: Thu, 27 Feb 2025 10:41:14 +0000 Subject: [PATCH 1/3] Updating with new attributes Macro --- include/aidge/operator/BitShift.hpp | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/include/aidge/operator/BitShift.hpp b/include/aidge/operator/BitShift.hpp index c54d6a99f..afe074586 100644 --- a/include/aidge/operator/BitShift.hpp +++ b/include/aidge/operator/BitShift.hpp @@ -23,7 +23,7 @@ #include "aidge/utils/Types.h" #include "aidge/utils/StaticAttributes.hpp" -#define LIST_BITSHIFT_ATTR(X) X(BitShiftdirection, "bit_shift_direction", BitShiftDirection) +#define LIST_BITSHIFT_ATTR(X) X(BitShiftdirection, "bit_shift_direction", BitShiftDirection), X(Rounding,"rounding",bool) namespace Aidge { enum class BitShiftAttr { @@ -87,10 +87,12 @@ public: * @brief Constructor to initialize the `BitShift_Op` with a shift direction. * @param[in] direction The direction of the bitwise shift (left or right). */ - BitShift_Op(BitShiftDirection direction) + BitShift_Op(BitShiftDirection direction, bool rounding = false) : OperatorTensor(Type, {InputCategory::Data, InputCategory::Data}, 1), mAttributes(std::make_shared<Attributes_>( - attr<BitShiftAttr::BitShiftdirection>(direction))) {} + attr<BitShiftAttr::BitShiftdirection>(direction), + attr<BitShiftAttr::Rounding>(rounding))) + {} /** * @brief Copy-constructor. Copies operator attributes and output tensors but not input tensors. @@ -143,6 +145,13 @@ public: inline BitShiftDirection& direction() const noexcept { return mAttributes->template getAttr<BitShiftAttr::BitShiftdirection>(); } + /** + * @brief Retrieve the rounding flag. + * @return A boolean (True: Apply bitshift rounding). + */ + inline bool rounding() const noexcept { + return mAttributes->template getAttr<BitShiftAttr::Rounding>(); + } /** * @brief Get the names of the input tensors. @@ -172,11 +181,12 @@ public: /** * @brief Factory function to create a `BitShift` node. * @param[in] direction The direction of the bitwise shift (`left` or `right`). + * @param[in] rounding Apply rounding * @param[in] name (Optional) Name of the node. * @return A shared pointer to the created 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); +inline std::shared_ptr<Node> BitShift(const BitShift_Op::BitShiftDirection direction,bool rounding = false, const std::string& name = "") { + return std::make_shared<Node>(std::make_shared<BitShift_Op>(direction,rounding), name); } } // namespace Aidge -- GitLab From 608330434b03a9bb4b94d6f45d88fd18e0d5e0d9 Mon Sep 17 00:00:00 2001 From: Noam ZERAH <noam.zerah@cea.fr> Date: Fri, 28 Feb 2025 14:35:26 +0000 Subject: [PATCH 2/3] Adding python binding --- python_binding/operator/pybind_BitShift.cpp | 111 ++++++++++---------- 1 file changed, 57 insertions(+), 54 deletions(-) diff --git a/python_binding/operator/pybind_BitShift.cpp b/python_binding/operator/pybind_BitShift.cpp index f2f4b223d..9313f46c3 100644 --- a/python_binding/operator/pybind_BitShift.cpp +++ b/python_binding/operator/pybind_BitShift.cpp @@ -9,58 +9,61 @@ * ********************************************************************************/ -#include <pybind11/pybind11.h> + #include <pybind11/pybind11.h> -#include <string> -#include "aidge/backend/OperatorImpl.hpp" -#include "aidge/data/Tensor.hpp" -#include "aidge/operator/BitShift.hpp" -#include "aidge/operator/OperatorTensor.hpp" -#include "aidge/utils/Types.h" - -namespace py = pybind11; -namespace Aidge { - -void init_BitShift(py::module &m) { - // Binding for BitShiftOp class - auto pyBitShiftOp = py::class_<BitShift_Op, std::shared_ptr<BitShift_Op>, OperatorTensor>(m, "BitShiftOp", py::multiple_inheritance(),R"mydelimiter( - BitShiftOp is a tensor operator that performs bitwise shifts on tensor elements. - This class allows shifting tensor values either to the left or right based on the - specified direction. The direction can be accessed and controlled using the - BitShiftDirection enum. - :param direction: direction of the bit shift (BitShiftDirection.Left or BitShiftDirection.Right) - :type direction: BitShiftDirection - :param name: name of the node. - )mydelimiter") - .def(py::init<BitShift_Op::BitShiftDirection>(), py::arg("direction")) - .def("direction", &BitShift_Op::direction, "Get the direction of the bit shift (left or right).") - .def_static("get_inputs_name", &BitShift_Op::getInputsName, "Get the names of the input tensors.") - .def_static("get_outputs_name", &BitShift_Op::getOutputsName, "Get the names of the output tensors.") - .def_static("attributes_name", []() { - std::vector<std::string> result; - auto attributes = BitShift_Op::attributesName(); - for (size_t i = 0; i < size(EnumStrings<BitShiftAttr>::data); ++i) { - result.emplace_back(attributes[i]); - } - return result; - }); - - // Enum binding under BitShiftOp class - py::enum_<BitShift_Op::BitShiftDirection>(pyBitShiftOp, "BitShiftDirection") - .value("Right", BitShift_Op::BitShiftDirection::right) - .value("Left", BitShift_Op::BitShiftDirection::left) - .export_values(); - - // Binding for the BitShift function - m.def("BitShift", &BitShift, py::arg("direction") = BitShift_Op::BitShiftDirection::right, py::arg("name") = "", - R"mydelimiter( - BitShiftOp is a tensor operator that performs bitwise shifts on tensor elements. - This class allows shifting tensor values either to the left or right based on the - specified direction. The direction can be accessed and controlled using the - BitShiftDirection enum. - :param direction: direction of the bit shift (BitShiftDirection.Left or BitShiftDirection.Right) - :type direction: BitShiftDirection - :param name: name of the node. - )mydelimiter"); -} -} // namespace Aidge \ No newline at end of file + #include <string> + #include "aidge/backend/OperatorImpl.hpp" + #include "aidge/data/Tensor.hpp" + #include "aidge/operator/BitShift.hpp" + #include "aidge/operator/OperatorTensor.hpp" + #include "aidge/utils/Types.h" + + namespace py = pybind11; + namespace Aidge { + + void init_BitShift(py::module &m) { + // Binding for BitShiftOp class + auto pyBitShiftOp = py::class_<BitShift_Op, std::shared_ptr<BitShift_Op>, OperatorTensor>(m, "BitShiftOp", py::multiple_inheritance(),R"mydelimiter( + BitShiftOp is a tensor operator that performs bitwise shifts on tensor elements. + This class allows shifting tensor values either to the left or right based on the + specified direction. The direction can be accessed and controlled using the + BitShiftDirection enum. + :param direction: direction of the bit shift (BitShiftDirection.Left or BitShiftDirection.Right) + :type direction: BitShiftDirection + :param name: name of the node. + )mydelimiter") + .def(py::init<BitShift_Op::BitShiftDirection>(), py::arg("direction")) + .def("direction", &BitShift_Op::direction, "Get the direction of the bit shift (left or right).") + .def("rounding", &BitShift_Op::rounding, "Apply bitshift rounding") + .def_static("get_inputs_name", &BitShift_Op::getInputsName, "Get the names of the input tensors.") + .def_static("get_outputs_name", &BitShift_Op::getOutputsName, "Get the names of the output tensors.") + .def_static("attributes_name", []() { + std::vector<std::string> result; + auto attributes = BitShift_Op::attributesName(); + for (size_t i = 0; i < size(EnumStrings<BitShiftAttr>::data); ++i) { + result.emplace_back(attributes[i]); + } + return result; + }); + + // Enum binding under BitShiftOp class + py::enum_<BitShift_Op::BitShiftDirection>(pyBitShiftOp, "BitShiftDirection") + .value("Right", BitShift_Op::BitShiftDirection::right) + .value("Left", BitShift_Op::BitShiftDirection::left) + .export_values(); + + // Binding for the BitShift function + m.def("BitShift", &BitShift, py::arg("direction") = BitShift_Op::BitShiftDirection::right,py::arg("rounding") = false, py::arg("name") = "", + R"mydelimiter( + BitShiftOp is a tensor operator that performs bitwise shifts on tensor elements. + This class allows shifting tensor values either to the left or right based on the + specified direction. The direction can be accessed and controlled using the + BitShiftDirection enum. + :param direction: direction of the bit shift (BitShiftDirection.Left or BitShiftDirection.Right) + :type direction: BitShiftDirection + :param rounding: flag to apply bitshift rounding + :type rounding: boolean + :param name: name of the node. + )mydelimiter"); + } + } // namespace Aidge \ No newline at end of file -- GitLab From 16a499fe3a63dd8df1e79f4d2d2ebfed3b7b4040 Mon Sep 17 00:00:00 2001 From: Noam ZERAH <noam.zerah@cea.fr> Date: Wed, 5 Mar 2025 14:45:35 +0000 Subject: [PATCH 3/3] Updating constructor in python binding --- python_binding/operator/pybind_BitShift.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/python_binding/operator/pybind_BitShift.cpp b/python_binding/operator/pybind_BitShift.cpp index 9313f46c3..4efb2c96f 100644 --- a/python_binding/operator/pybind_BitShift.cpp +++ b/python_binding/operator/pybind_BitShift.cpp @@ -30,11 +30,11 @@ BitShiftDirection enum. :param direction: direction of the bit shift (BitShiftDirection.Left or BitShiftDirection.Right) :type direction: BitShiftDirection + :param rounding: flag to apply bitshift rounding + :type rounding: boolean :param name: name of the node. )mydelimiter") - .def(py::init<BitShift_Op::BitShiftDirection>(), py::arg("direction")) - .def("direction", &BitShift_Op::direction, "Get the direction of the bit shift (left or right).") - .def("rounding", &BitShift_Op::rounding, "Apply bitshift rounding") + .def(py::init<BitShift_Op::BitShiftDirection,bool>(), py::arg("direction"),py::arg("rounding")) .def_static("get_inputs_name", &BitShift_Op::getInputsName, "Get the names of the input tensors.") .def_static("get_outputs_name", &BitShift_Op::getOutputsName, "Get the names of the output tensors.") .def_static("attributes_name", []() { -- GitLab