From 69d1644ed158b695ea3508beaf2b6ba46d161c80 Mon Sep 17 00:00:00 2001 From: Olivier BICHLER <olivier.bichler@cea.fr> Date: Mon, 31 Mar 2025 18:32:42 +0200 Subject: [PATCH 1/4] Wrong type for axes --- include/aidge/operator/Squeeze.hpp | 10 +++++----- python_binding/operator/pybind_Squeeze.cpp | 2 +- src/operator/Squeeze.cpp | 14 +++++++------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/include/aidge/operator/Squeeze.hpp b/include/aidge/operator/Squeeze.hpp index 03db92a84..b39ecf73c 100644 --- a/include/aidge/operator/Squeeze.hpp +++ b/include/aidge/operator/Squeeze.hpp @@ -27,7 +27,7 @@ #define LIST_SQUEEZE_ATTR(X) \ - X(Axes, "axes", std::vector<std::int8_t>) + X(Axes, "axes", std::vector<std::int64_t>) namespace Aidge { /** @@ -78,7 +78,7 @@ public: Type; // name of the type of the operation (Here "Squeeze") private: - using Attributes_ = StaticAttributes<SqueezeAttr, std::vector<std::int8_t>>; + using Attributes_ = StaticAttributes<SqueezeAttr, std::vector<std::int64_t>>; template <SqueezeAttr e> using attr = typename Attributes_::template attr<e>; const std::shared_ptr<Attributes_> mAttributes; @@ -87,7 +87,7 @@ public: * @brief constructor for Squeeze op * @param[in] axes around which perform the operation */ - Squeeze_Op(const std::vector<std::int8_t> &axes = {}); + Squeeze_Op(const std::vector<std::int64_t> &axes = {}); /** * @brief Copy-constructor. Copy the operator attributes and its output @@ -123,7 +123,7 @@ public: * @brief axes to squeeze, if left empty all 1 sized * dimensions will be removed. */ - inline std::vector<std::int8_t> &axes() const noexcept { + inline std::vector<std::int64_t> &axes() const noexcept { return mAttributes->template getAttr<SqueezeAttr::Axes>(); } @@ -145,7 +145,7 @@ public: // helper with C-style array instead of std::array for kernel_dims to allow // automatic template DIM deduction -std::shared_ptr<Node> Squeeze(const std::vector<std::int8_t> axes = {}, +std::shared_ptr<Node> Squeeze(const std::vector<std::int64_t> axes = {}, const std::string &name = ""); } // namespace Aidge diff --git a/python_binding/operator/pybind_Squeeze.cpp b/python_binding/operator/pybind_Squeeze.cpp index 7808c78da..d5174c98d 100644 --- a/python_binding/operator/pybind_Squeeze.cpp +++ b/python_binding/operator/pybind_Squeeze.cpp @@ -46,7 +46,7 @@ void init_Squeeze(py::module &m) { .def("axes", &Squeeze_Op::axes); declare_registrable<Squeeze_Op>(m, "SqueezeOp"); - m.def("Squeeze", &Squeeze, py::arg("axes") = std::vector<int8_t>({}), + m.def("Squeeze", &Squeeze, py::arg("axes") = std::vector<int64_t>({}), py::arg("name") = "", R"mydelimiter( Initialize a node containing a squeeze operator. diff --git a/src/operator/Squeeze.cpp b/src/operator/Squeeze.cpp index ea3452878..e1c99f88f 100644 --- a/src/operator/Squeeze.cpp +++ b/src/operator/Squeeze.cpp @@ -28,7 +28,7 @@ namespace Aidge { const std::string Squeeze_Op::Type = "Squeeze"; -Squeeze_Op::Squeeze_Op(const std::vector<std::int8_t> &axes) +Squeeze_Op::Squeeze_Op(const std::vector<std::int64_t> &axes) : OperatorTensor( Type, {InputCategory::Data, InputCategory::OptionalData}, @@ -84,7 +84,7 @@ bool Squeeze_Op::forwardDims(bool allowDataDependency) { this->axes().clear(); // If both are provided input would override attrs this->axes().reserve(getInput(1)->size()); const auto &axes = - getInput(1)->refCastFrom(fallback, NativeType_v<std::int8_t>, "cpu"); + getInput(1)->refCastFrom(fallback, NativeType_v<std::int64_t>, "cpu"); if (axes.nbDims() == 0) { this->axes().clear(); } else { @@ -92,7 +92,7 @@ bool Squeeze_Op::forwardDims(bool allowDataDependency) { axes.nbDims() == 1, "Axes input tensor should be of size 1. Received {} dimensions : {}", axes.nbDims(), axes.dims()); - std::copy_n(static_cast<std::int8_t *>(axes.getImpl()->hostPtr()), axes.size(), + std::copy_n(static_cast<std::int64_t *>(axes.getImpl()->hostPtr()), axes.size(), std::back_inserter(this->axes())); } } @@ -107,9 +107,9 @@ bool Squeeze_Op::forwardDims(bool allowDataDependency) { } } } else { - for (std::int8_t axis : this->axes()) { - axis = axis >= 0 ? axis : axis + static_cast<std::int8_t>(inputDims.size()); - if ((axis < 0) || (axis >= static_cast<std::int8_t>(inputDims.size()))) { + for (std::int64_t axis : this->axes()) { + axis = axis >= 0 ? axis : axis + static_cast<std::int64_t>(inputDims.size()); + if ((axis < 0) || (axis >= static_cast<std::int64_t>(inputDims.size()))) { Log::error("{} : Axis index OutOfBounds error, expected value " "within size limits of input tensor : " "[-{},{}], got {}.", @@ -152,7 +152,7 @@ std::set<std::string> Aidge::Squeeze_Op::getAvailableBackends() const { //////////////////////////////////////////////////////////////////////////////// -std::shared_ptr<Node> Squeeze(const std::vector<std::int8_t> axes, +std::shared_ptr<Node> Squeeze(const std::vector<std::int64_t> axes, const std::string &name) { return std::make_shared<Node>(std::make_shared<Squeeze_Op>(axes), name); } -- GitLab From 56166eee204696887ce1dac9d1bc4d11de0112d6 Mon Sep 17 00:00:00 2001 From: Noam ZERAH <noam.zerah@cea.fr> Date: Tue, 1 Apr 2025 09:38:50 +0000 Subject: [PATCH 2/4] Fixing an issue in the ForwardDims of avgPooling when kernel size > input dim size --- src/operator/AvgPooling.cpp | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/operator/AvgPooling.cpp b/src/operator/AvgPooling.cpp index 7561297ba..fe7083c34 100644 --- a/src/operator/AvgPooling.cpp +++ b/src/operator/AvgPooling.cpp @@ -74,11 +74,21 @@ bool Aidge::AvgPooling_Op<DIM>::forwardDims(bool /*allowDataDependency*/) { const auto kernelDim = mAttributes->template getAttr<AvgPoolingAttr::KernelDims>()[dim]; const auto strideDim = mAttributes->template getAttr<AvgPoolingAttr::StrideDims>()[dim]; const auto dilationDim = mAttributes->template getAttr<AvgPoolingAttr::Dilations>()[dim]; - - outputDims[dim+2] = 1 + static_cast<DimSize_t>( - roundingFunction(static_cast<float>(inputDims[dim+2] - - (kernelDim - 1) * dilationDim - 1) / - static_cast<float>(strideDim))); + const auto inputDim = inputDims[dim + 2]; + const auto effective_kernel_size = (kernelDim - 1) * dilationDim + 1; + + if (effective_kernel_size >= inputDim) { + //If the kernel is greater than the input we set it to 1 + outputDims[dim + 2] = 1; + } + else { + outputDims[dim + 2] = 1 + static_cast<DimSize_t>( + roundingFunction( + static_cast<float>(inputDim - effective_kernel_size) / + static_cast<float>(strideDim) + ) + ); + } } outputDims[1] = inputDims[1]; outputDims[0] = inputDims[0]; -- GitLab From c815fccc65d31d7cfc1af5aefead0392596a20c5 Mon Sep 17 00:00:00 2001 From: Noam ZERAH <noam.zerah@cea.fr> Date: Tue, 1 Apr 2025 09:46:27 +0000 Subject: [PATCH 3/4] Rebase on dev version for squeeze --- include/aidge/operator/Squeeze.hpp | 10 +++++----- python_binding/operator/pybind_Squeeze.cpp | 2 +- src/operator/Squeeze.cpp | 14 +++++++------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/include/aidge/operator/Squeeze.hpp b/include/aidge/operator/Squeeze.hpp index b39ecf73c..03db92a84 100644 --- a/include/aidge/operator/Squeeze.hpp +++ b/include/aidge/operator/Squeeze.hpp @@ -27,7 +27,7 @@ #define LIST_SQUEEZE_ATTR(X) \ - X(Axes, "axes", std::vector<std::int64_t>) + X(Axes, "axes", std::vector<std::int8_t>) namespace Aidge { /** @@ -78,7 +78,7 @@ public: Type; // name of the type of the operation (Here "Squeeze") private: - using Attributes_ = StaticAttributes<SqueezeAttr, std::vector<std::int64_t>>; + using Attributes_ = StaticAttributes<SqueezeAttr, std::vector<std::int8_t>>; template <SqueezeAttr e> using attr = typename Attributes_::template attr<e>; const std::shared_ptr<Attributes_> mAttributes; @@ -87,7 +87,7 @@ public: * @brief constructor for Squeeze op * @param[in] axes around which perform the operation */ - Squeeze_Op(const std::vector<std::int64_t> &axes = {}); + Squeeze_Op(const std::vector<std::int8_t> &axes = {}); /** * @brief Copy-constructor. Copy the operator attributes and its output @@ -123,7 +123,7 @@ public: * @brief axes to squeeze, if left empty all 1 sized * dimensions will be removed. */ - inline std::vector<std::int64_t> &axes() const noexcept { + inline std::vector<std::int8_t> &axes() const noexcept { return mAttributes->template getAttr<SqueezeAttr::Axes>(); } @@ -145,7 +145,7 @@ public: // helper with C-style array instead of std::array for kernel_dims to allow // automatic template DIM deduction -std::shared_ptr<Node> Squeeze(const std::vector<std::int64_t> axes = {}, +std::shared_ptr<Node> Squeeze(const std::vector<std::int8_t> axes = {}, const std::string &name = ""); } // namespace Aidge diff --git a/python_binding/operator/pybind_Squeeze.cpp b/python_binding/operator/pybind_Squeeze.cpp index d5174c98d..7808c78da 100644 --- a/python_binding/operator/pybind_Squeeze.cpp +++ b/python_binding/operator/pybind_Squeeze.cpp @@ -46,7 +46,7 @@ void init_Squeeze(py::module &m) { .def("axes", &Squeeze_Op::axes); declare_registrable<Squeeze_Op>(m, "SqueezeOp"); - m.def("Squeeze", &Squeeze, py::arg("axes") = std::vector<int64_t>({}), + m.def("Squeeze", &Squeeze, py::arg("axes") = std::vector<int8_t>({}), py::arg("name") = "", R"mydelimiter( Initialize a node containing a squeeze operator. diff --git a/src/operator/Squeeze.cpp b/src/operator/Squeeze.cpp index e1c99f88f..ea3452878 100644 --- a/src/operator/Squeeze.cpp +++ b/src/operator/Squeeze.cpp @@ -28,7 +28,7 @@ namespace Aidge { const std::string Squeeze_Op::Type = "Squeeze"; -Squeeze_Op::Squeeze_Op(const std::vector<std::int64_t> &axes) +Squeeze_Op::Squeeze_Op(const std::vector<std::int8_t> &axes) : OperatorTensor( Type, {InputCategory::Data, InputCategory::OptionalData}, @@ -84,7 +84,7 @@ bool Squeeze_Op::forwardDims(bool allowDataDependency) { this->axes().clear(); // If both are provided input would override attrs this->axes().reserve(getInput(1)->size()); const auto &axes = - getInput(1)->refCastFrom(fallback, NativeType_v<std::int64_t>, "cpu"); + getInput(1)->refCastFrom(fallback, NativeType_v<std::int8_t>, "cpu"); if (axes.nbDims() == 0) { this->axes().clear(); } else { @@ -92,7 +92,7 @@ bool Squeeze_Op::forwardDims(bool allowDataDependency) { axes.nbDims() == 1, "Axes input tensor should be of size 1. Received {} dimensions : {}", axes.nbDims(), axes.dims()); - std::copy_n(static_cast<std::int64_t *>(axes.getImpl()->hostPtr()), axes.size(), + std::copy_n(static_cast<std::int8_t *>(axes.getImpl()->hostPtr()), axes.size(), std::back_inserter(this->axes())); } } @@ -107,9 +107,9 @@ bool Squeeze_Op::forwardDims(bool allowDataDependency) { } } } else { - for (std::int64_t axis : this->axes()) { - axis = axis >= 0 ? axis : axis + static_cast<std::int64_t>(inputDims.size()); - if ((axis < 0) || (axis >= static_cast<std::int64_t>(inputDims.size()))) { + for (std::int8_t axis : this->axes()) { + axis = axis >= 0 ? axis : axis + static_cast<std::int8_t>(inputDims.size()); + if ((axis < 0) || (axis >= static_cast<std::int8_t>(inputDims.size()))) { Log::error("{} : Axis index OutOfBounds error, expected value " "within size limits of input tensor : " "[-{},{}], got {}.", @@ -152,7 +152,7 @@ std::set<std::string> Aidge::Squeeze_Op::getAvailableBackends() const { //////////////////////////////////////////////////////////////////////////////// -std::shared_ptr<Node> Squeeze(const std::vector<std::int64_t> axes, +std::shared_ptr<Node> Squeeze(const std::vector<std::int8_t> axes, const std::string &name) { return std::make_shared<Node>(std::make_shared<Squeeze_Op>(axes), name); } -- GitLab From a7eb20537e51252e3cab94df6e5518f25778a9ac Mon Sep 17 00:00:00 2001 From: Noam ZERAH <noam.zerah@cea.fr> Date: Tue, 1 Apr 2025 11:56:00 +0000 Subject: [PATCH 4/4] Refactoring the condition in forward dim (using std::max) --- src/operator/AvgPooling.cpp | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/src/operator/AvgPooling.cpp b/src/operator/AvgPooling.cpp index fe7083c34..966063cd0 100644 --- a/src/operator/AvgPooling.cpp +++ b/src/operator/AvgPooling.cpp @@ -74,22 +74,13 @@ bool Aidge::AvgPooling_Op<DIM>::forwardDims(bool /*allowDataDependency*/) { const auto kernelDim = mAttributes->template getAttr<AvgPoolingAttr::KernelDims>()[dim]; const auto strideDim = mAttributes->template getAttr<AvgPoolingAttr::StrideDims>()[dim]; const auto dilationDim = mAttributes->template getAttr<AvgPoolingAttr::Dilations>()[dim]; - const auto inputDim = inputDims[dim + 2]; - const auto effective_kernel_size = (kernelDim - 1) * dilationDim + 1; + + const float effective_size = static_cast<float>(inputDims[dim+2] - (kernelDim - 1) * dilationDim - 1); + const float rounded_val = roundingFunction(effective_size / static_cast<float>(strideDim)); - if (effective_kernel_size >= inputDim) { - //If the kernel is greater than the input we set it to 1 - outputDims[dim + 2] = 1; - } - else { - outputDims[dim + 2] = 1 + static_cast<DimSize_t>( - roundingFunction( - static_cast<float>(inputDim - effective_kernel_size) / - static_cast<float>(strideDim) - ) - ); - } + outputDims[dim+2] = 1 + std::max(0, static_cast<int>(rounded_val)); } + outputDims[1] = inputDims[1]; outputDims[0] = inputDims[0]; getOutput(0)->resize(outputDims); -- GitLab