Skip to content

Commit

Permalink
feat: Add bound check during blaze setter
Browse files Browse the repository at this point in the history
  • Loading branch information
skim0119 committed Jul 11, 2024
1 parent a61a445 commit 2e34cdd
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 12 deletions.
9 changes: 9 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 2 additions & 4 deletions backend/src/Utilities/Math/Python/BlazeMatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <array>
#include <cstddef>
Expand Down Expand Up @@ -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;
});
});
}
//****************************************************************************

Expand Down
11 changes: 5 additions & 6 deletions backend/src/Utilities/Math/Python/BlazeTensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <array>
#include <cstddef>
Expand Down Expand Up @@ -82,8 +81,8 @@ namespace py_bindings {
"__getitem__",
+[](const type& self,
const std::tuple<std::size_t, std::size_t, std::size_t>& 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(
Expand All @@ -96,8 +95,8 @@ namespace py_bindings {
+[](type& self,
const std::tuple<std::size_t, std::size_t, std::size_t>& 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
Expand Down
3 changes: 1 addition & 2 deletions backend/src/Utilities/Math/Python/BlazeVector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <algorithm>
#include <memory>
Expand Down
67 changes: 67 additions & 0 deletions backend/src/Utilities/Math/Python/BoundChecks.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#pragma once

//******************************************************************************
// Includes
//******************************************************************************
#include <cstddef>
#include <stdexcept>
#include <string>

#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 <typename T>
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<T>() +
" 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 <typename T>
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 <typename T>
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

0 comments on commit 2e34cdd

Please sign in to comment.