From 2e34cdd2030fbb5a28e6bfc8d9d75967d25f66f1 Mon Sep 17 00:00:00 2001 From: Seung Hyun Kim Date: Wed, 10 Jul 2024 21:52:33 -0500 Subject: [PATCH] feat: Add bound check during blaze setter --- backend/README.md | 9 +++ .../src/Utilities/Math/Python/BlazeMatrix.cpp | 6 +- .../src/Utilities/Math/Python/BlazeTensor.cpp | 11 ++- .../src/Utilities/Math/Python/BlazeVector.cpp | 3 +- .../src/Utilities/Math/Python/BoundChecks.hpp | 67 +++++++++++++++++++ 5 files changed, 84 insertions(+), 12 deletions(-) create mode 100644 backend/src/Utilities/Math/Python/BoundChecks.hpp diff --git a/backend/README.md b/backend/README.md index 33bbee46..506dfeb2 100644 --- a/backend/README.md +++ b/backend/README.md @@ -33,3 +33,12 @@ For benchmarking various `matmul` implementations, run ``` python3 backend/benchmarking/matmul.py ``` + +## Contributed By + +- Tejaswin Parthasarathy (Teja) +- [Seung Hyun Kim](https://github.com/skim0119) +- Ankith Pai +- [Yashraj Bhosale](https://github.com/bhosale2) +- Arman Tekinalp +- Songyuan Cui diff --git a/backend/src/Utilities/Math/Python/BlazeMatrix.cpp b/backend/src/Utilities/Math/Python/BlazeMatrix.cpp index 4ebe13de..2834471a 100644 --- a/backend/src/Utilities/Math/Python/BlazeMatrix.cpp +++ b/backend/src/Utilities/Math/Python/BlazeMatrix.cpp @@ -2,13 +2,11 @@ // Includes //****************************************************************************** -// -// #include "PythonBindings/BoundChecks.hpp" -// #include "Utilities/DefineTypes.h" #include "Utilities/MakeString.hpp" // #include "Utilities/Math/Python/SliceHelpers.hpp" +#include "Utilities/Math/Python/BoundChecks.hpp" // #include #include @@ -99,7 +97,7 @@ namespace py_bindings { const Real val) { matrix_bounds_check(self, std::get<0>(x), std::get<1>(x)); self(std::get<0>(x), std::get<1>(x)) = val; - }); + }); } //**************************************************************************** diff --git a/backend/src/Utilities/Math/Python/BlazeTensor.cpp b/backend/src/Utilities/Math/Python/BlazeTensor.cpp index 9ed4d01c..779952b7 100644 --- a/backend/src/Utilities/Math/Python/BlazeTensor.cpp +++ b/backend/src/Utilities/Math/Python/BlazeTensor.cpp @@ -3,12 +3,11 @@ //****************************************************************************** -// #include "PythonBindings/BoundChecks.hpp" -// #include "Utilities/DefineTypes.h" #include "Utilities/MakeString.hpp" // #include "Utilities/Math/Python/SliceHelpers.hpp" +#include "Utilities/Math/Python/BoundChecks.hpp" // #include #include @@ -82,8 +81,8 @@ namespace py_bindings { "__getitem__", +[](const type& self, const std::tuple& x) { - // tensor_bounds_check(self, std::get<0>(x), std::get<1>(x), - // std::get<2>(x)); + tensor_bounds_check(self, std::get<0>(x), std::get<1>(x), + std::get<2>(x)); return self(std::get<0>(x), std::get<1>(x), std::get<2>(x)); }) .def( @@ -96,8 +95,8 @@ namespace py_bindings { +[](type& self, const std::tuple& x, const Real val) { - // tensor_bounds_check(self, std::get<0>(x), std::get<1>(x), - // std::get<2>(x)); + tensor_bounds_check(self, std::get<0>(x), std::get<1>(x), + std::get<2>(x)); self(std::get<0>(x), std::get<1>(x), std::get<2>(x)) = val; }) // Need __str__ for converting to string/printing diff --git a/backend/src/Utilities/Math/Python/BlazeVector.cpp b/backend/src/Utilities/Math/Python/BlazeVector.cpp index eed3afa6..1d9494ac 100644 --- a/backend/src/Utilities/Math/Python/BlazeVector.cpp +++ b/backend/src/Utilities/Math/Python/BlazeVector.cpp @@ -2,13 +2,12 @@ // Includes //****************************************************************************** -// #include "PythonBindings/BoundChecks.hpp" // #include "Utilities/DefineTypes.h" -// #include "Utilities/PrettyType.hpp" #include "Utilities/PrintHelpers.hpp" // #include "Utilities/Math/Python/SliceHelpers.hpp" +#include "Utilities/Math/Python/BoundChecks.hpp" // #include #include diff --git a/backend/src/Utilities/Math/Python/BoundChecks.hpp b/backend/src/Utilities/Math/Python/BoundChecks.hpp new file mode 100644 index 00000000..84f91245 --- /dev/null +++ b/backend/src/Utilities/Math/Python/BoundChecks.hpp @@ -0,0 +1,67 @@ +#pragma once + +//****************************************************************************** +// Includes +//****************************************************************************** +#include +#include +#include + +#include "Utilities/PrettyType.hpp" + +namespace py_bindings { + + //**************************************************************************** + /*! \brief Check if a vector-like object access is in bounds. Throws + * std::runtime_error if it is not. + * \ingroup python_bindings + */ + template + void bounds_check(const T& t, const std::size_t i) { + if (i >= t.size()) { + throw std::runtime_error{"Out of bounds access (" + std::to_string(i) + + ") into " + pretty_type::name() + + " of size " + std::to_string(t.size())}; + } + } + //**************************************************************************** + + //**************************************************************************** + /*!\brief Check if a matrix-like object access is in bounds. Throws + * std::runtime_error if it is not. + * \ingroup python_bindings + */ + template + void matrix_bounds_check(const T& matrix, const std::size_t row, + const std::size_t column) { + if (row >= matrix.rows() or column >= matrix.columns()) { + throw std::runtime_error{"Out of bounds access (" + std::to_string(row) + + ", " + std::to_string(column) + + ") into Matrix of size (" + + std::to_string(matrix.rows()) + ", " + + std::to_string(matrix.columns()) + ")"}; + } + } + //**************************************************************************** + + //**************************************************************************** + /*!\brief Check if a tensor-like object access is in bounds. Throws + * std::runtime_error if it is not. + * \ingroup python_bindings + */ + template + void tensor_bounds_check(const T& tensor, const std::size_t page, + const std::size_t row, const std::size_t column) { + if (page >= tensor.pages() or row >= tensor.rows() or + column >= tensor.columns()) { + throw std::runtime_error{ + "Out of bounds access (" + std::to_string(page) + ", " + + std::to_string(row) + ", " + std::to_string(column) + + ") into Tensor of size (" + std::to_string(tensor.pages()) + ", " + + std::to_string(tensor.rows()) + ", " + + std::to_string(tensor.columns()) + ")"}; + } + } + //**************************************************************************** + +} // namespace py_bindings