Skip to content

Adding C++ bindings for DeKA algorithm and restructuring the project. #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
build/
.cache/
.venv/
python/__pycache__/
__pycache__/
35 changes: 35 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
cmake_minimum_required(VERSION 3.15...3.31)

project(
${SKBUILD_PROJECT_NAME}
DESCRIPTION "Deterministic Kaczmarz Algorithm for Online Paramter Estimation."
VERSION ${SKBUILD_PROJECT_VERSION}
LANGUAGES CXX
)

add_definitions(-DHAVE_CBLAS=1)

if (WIN32)
find_package(OpenBLAS REQUIRED)
set(BLAS_LIBRARIES ${CMAKE_INSTALL_PREFIX}${OpenBLAS_LIBRARIES})
else()
find_package(BLAS REQUIRED)
find_package(LAPACK REQUIRED)
endif()

message(STATUS "BLAS VENDOR: " ${BLA_VENDOR})
message(STATUS "BLAS LIBRARIES: " ${BLAS_LIBRARIES})

find_package(Python REQUIRED COMPONENTS Interpreter Development.Module NumPy)
find_package(pybind11 CONFIG REQUIRED)
find_package(xtensor REQUIRED)
find_package(xtensor-python REQUIRED)
find_package(xtensor-blas REQUIRED)

python_add_library(_core MODULE WITH_SOABI src/main.cpp src/estimators.cpp)
target_link_libraries(_core PRIVATE pybind11::headers xtensor xtensor-python Python::NumPy xtensor-blas ${BLAS_LIBRARIES} ${LAPACK_LIBRARIES})

target_compile_definitions(_core PRIVATE VERSION_INFO=${PROJECT_VERSION})

install(TARGETS _core DESTINATION ${SKBUILD_PROJECT_NAME})

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2025 The Accessible and Accelerated Robotics Lab (A²R Lab)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
41 changes: 41 additions & 0 deletions USAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Usage

### Dependencies

To use this project, the main dependencies are:
- Autograd >= 1.7.0,
- NumPy >= 2.2.4,
- Matplotlib >= 3.10.1.
- Python >= 3.10,

### Building Project

To build this project from source, have the following dependencies alongside the ones listed above:

- CMake >= 3.15,
- OpenBLAS/BLAS and LAPACK,
- uv,
- xtl == 0.8.0,
- xtensor == 0.26.0,
- xtensor-blas == 0.22.0,
- xtensor-python == 0.27.0.

The distribution can be built in the project root by just calling:

```bash
uv build
```

### Installing Distribution

After building the project, navigate to the `dist` directory and install the project by running:

```bash
cd dist

# option 1: install tarball
uv pip install deka-0.X.X.tar.gz

# option 2: install wheels
uv pip install deka-0.X.X-[...].whl
```
15 changes: 15 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[build-system]
requires = ["scikit-build-core", "pybind11", "numpy"]
build-backend = "scikit_build_core.build"

[project]
name = "deka"
version = "0.1.0"
description = "The Deterministic Kaczmarz Algorithm with Greedy Selection and Smoothing for Online Inertial Parameter Estimation"
readme = "README.md"
requires-python = ">=3.10"
dependencies = [
"autograd>=1.7.0",
"matplotlib>=3.10.1",
"numpy>=2.2.4",
]
Binary file removed src/__pycache__/LQR_controller.cpython-310.pyc
Binary file not shown.
Binary file removed src/__pycache__/double_pen_LQR.cpython-310.pyc
Binary file not shown.
Binary file not shown.
Binary file removed src/__pycache__/estimation_methods.cpython-310.pyc
Binary file not shown.
Binary file removed src/__pycache__/param_est.cpython-310.pyc
Binary file not shown.
Binary file removed src/__pycache__/quadrotor_dynamics.cpython-310.pyc
Binary file not shown.
Binary file removed src/__pycache__/simulator.cpython-310.pyc
Binary file not shown.
Binary file removed src/__pycache__/utilities.cpython-310.pyc
Binary file not shown.
File renamed without changes.
4 changes: 4 additions & 0 deletions src/deka/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from __future__ import annotations

from ._core import __doc__, __version__, DEKA

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
98 changes: 98 additions & 0 deletions src/estimators.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include <optional>
#include <pybind11/pybind11.h>
#include <xtensor.hpp>
#include <xtensor-python/pyarray.hpp>
#include <xtensor-blas/xlinalg.hpp>

namespace py = pybind11;

struct Estimator {
virtual xt::pyarray<double> iterate(
xt::pyarray<double>& A,
xt::pyarray<double>& b,
std::optional<xt::pyarray<double>> x0,
int num_iters) = 0;

virtual ~Estimator() {}
};

struct DEKA : public Estimator {
int n;
double damping;
double reg;
double smoothing;
double tol_min;
double tol;
xt::pyarray<double> x;
xt::pyarray<double> x_smooth;

DEKA(int n, double damping, double reg, double smoothing,
double tol_min, double tol_max)
: n(n),
damping(damping),
reg(reg),
smoothing(smoothing),
tol_min(tol_min),
tol(tol_max),
x(xt::zeros<double>({n, 1})),
x_smooth(xt::zeros<double>({n, 1})) {}

xt::pyarray<double> iterate(
xt::pyarray<double>& A,
xt::pyarray<double>& b,
std::optional<xt::pyarray<double>> x0,
int num_iters) override {

if (x0.has_value()) x = x0.value();

// TODO: add mask to ignore rows where A is all zeros

for (int k = 0; k < num_iters; k++) {
xt::pyarray<double> residual = xt::squeeze(b - xt::linalg::dot(A, x));
double res_norm = xt::linalg::norm(residual, 2);

py::print("Residual:", residual);
py::print("Residual norm:", res_norm);

if (res_norm < tol) break;

double res_norm_sq = xt::square(xt::linalg::norm(residual, 2));
xt::pyarray<double> A_row_norms_sq = xt::squeeze(xt::sum(xt::square(A), 1)) + 1e-10;
double max_ratio = xt::amax(xt::square(xt::abs(residual)) / A_row_norms_sq)();
double A_frob_norm_sq = xt::square(xt::linalg::norm(A, 2));
double epsilon = 0.5 * (max_ratio / res_norm_sq + 1 / A_frob_norm_sq);

py::print("Residual norm squared:", res_norm_sq);
py::print("A row norms squared:", A_row_norms_sq);
py::print("Max ratio:", max_ratio);
py::print("A frob norm squared:", A_frob_norm_sq);
py::print("Epsilon:", epsilon);

auto mask = (xt::square(residual) / A_row_norms_sq) >= epsilon * res_norm_sq;
xt::pyarray<double> eta = xt::where(mask, residual, 0.0);
eta.reshape({eta.size(), 1});

py::print("Eta:", eta);

if (xt::sum(eta)() == 0) break;

xt::pyarray<double> A_T_eta = xt::linalg::dot(xt::transpose(A), eta);
double numer = xt::linalg::dot(xt::transpose(eta), residual)(0);
double denom = xt::square(xt::linalg::norm(A_T_eta, 2))() + reg;
xt::pyarray<double> raw_update = numer / denom * A_T_eta;
x = x + raw_update * damping;

py::print("A_T_eta:", A_T_eta);
py::print("num:", numer, "denom:", denom);
py::print("Raw update:", raw_update);
py::print("x:", x);
}

// apply exponential smoothing to blend raw estimates
x_smooth = smoothing * x_smooth + (1 - smoothing) * x;

return x_smooth;
}

~DEKA() override = default;
};
52 changes: 52 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <xtensor.hpp>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#define FORCE_IMPORT_ARRAY
#include <xtensor-python/pyarray.hpp>
#include <optional>

// use "unity build" by directly inlining other source files
#include "estimators.cpp"

#define STRINGIFY(x) #x
#define MACRO_STRINGIFY(x) STRINGIFY(x)

// using namespace pybind11::literals;
namespace py = pybind11;

PYBIND11_MODULE(_core, m) {
xt::import_numpy();

m.doc() = R"pbdoc(
DEKA Estimation Methods
-----------------------

.. currentmodule:: deka

.. autosummary::
:toctree: _generate

DEKA
)pbdoc";

py::class_<DEKA>(m, "DEKA")
.def(
py::init<const int, const double, const double, const double,
const double, const double>(),
py::arg("n"),
py::arg("damping") = 0.1,
py::arg("reg") = 1e-6,
py::arg("smoothing") = 0.9,
py::arg("tol_max") = 1e-4,
py::arg("tol_min") = 1e-7
)
.def_readwrite("x", &DEKA::x)
.def("iterate", &DEKA::iterate, py::arg("A"), py::arg("b"),
py::arg("x0") = std::nullopt, py::arg("num_iters") = 1000);

#ifdef VERSION_INFO
m.attr("__version__") = MACRO_STRINGIFY(VERSION_INFO);
#else
m.attr("__version__") = "dev";
#endif
}
4 changes: 4 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import deka


print(deka.add(1, 2));
Loading