Skip to content
Merged
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
4 changes: 3 additions & 1 deletion .github/workflows/build-and-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ jobs:
CIBW_SKIP: "*-win32 *-manylinux_i686 *-musllinux*"
CIBW_ARCHS_MACOS: "x86_64 universal2"
CIBW_ARCHS_LINUX: "auto aarch64"

CIBW_BEFORE_BUILD_LINUX: "yum install -y openblas-devel || apt-get install -y libopenblas-dev"
CIBW_BEFORE_BUILD_WINDOWS: "vcpkg install openblas:x64-windows"
CIBW_ENVIRONMENT_WINDOWS: "CMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake"
- name: Check wheels
shell: bash
run: |
Expand Down
29 changes: 29 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
cmake_minimum_required(VERSION 3.15)
project(SparseDiffPy LANGUAGES C)

add_subdirectory(SparseDiffEngine)

find_package(Python3 REQUIRED COMPONENTS Interpreter Development.Module NumPy)

# Linux: CMake find_package(BLAS) doesn't set include dirs;
# openblas puts cblas.h in /usr/include/openblas/
if(UNIX AND NOT APPLE)
find_path(OPENBLAS_INCLUDE_DIR cblas.h PATHS /usr/include/openblas /usr/include)
if(OPENBLAS_INCLUDE_DIR)
target_include_directories(dnlp_diff PRIVATE ${OPENBLAS_INCLUDE_DIR})
endif()
endif()

python3_add_library(_sparsediffengine MODULE
sparsediffpy/_bindings/bindings.c
)

target_include_directories(_sparsediffengine PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/SparseDiffEngine/include
${CMAKE_CURRENT_SOURCE_DIR}/SparseDiffEngine/src
${CMAKE_CURRENT_SOURCE_DIR}/sparsediffpy/_bindings
)

target_link_libraries(_sparsediffengine PRIVATE dnlp_diff Python3::NumPy)

install(TARGETS _sparsediffengine DESTINATION sparsediffpy)
2 changes: 1 addition & 1 deletion SparseDiffEngine
Submodule SparseDiffEngine updated 83 files
+14 −2 .github/workflows/cmake.yml
+4 −0 .github/workflows/sanitizer.yml
+1 −1 .github/workflows/valgrind.yml
+13 −0 CMakeLists.txt
+7 −1 include/bivariate.h
+4 −3 include/subexpr.h
+11 −0 include/utils/cblas_wrapper.h
+69 −0 include/utils/matrix.h
+73 −39 src/bivariate/left_matmul.c
+21 −2 src/bivariate/right_matmul.c
+197 −0 src/utils/dense_matrix.c
+5 −11 src/utils/linalg_sparse_matmuls.c
+75 −0 src/utils/sparse_matrix.c
+10 −0 tests/all_tests.c
+1 −1 tests/forward_pass/affine/test_add.h
+3 −3 tests/forward_pass/affine/test_broadcast.h
+2 −2 tests/forward_pass/affine/test_hstack.h
+1 −1 tests/forward_pass/affine/test_linear_op.h
+3 −3 tests/forward_pass/affine/test_sum.h
+2 −2 tests/forward_pass/affine/test_variable_constant.h
+1 −1 tests/forward_pass/composite/test_composite.h
+1 −1 tests/forward_pass/elementwise/test_exp.h
+1 −1 tests/forward_pass/elementwise/test_log.h
+53 −0 tests/forward_pass/test_left_matmul_dense.h
+1 −1 tests/forward_pass/test_matmul.h
+1 −1 tests/forward_pass/test_prod_axis_one.h
+1 −1 tests/forward_pass/test_prod_axis_zero.h
+1 −1 tests/jacobian_tests/test_broadcast.h
+2 −2 tests/jacobian_tests/test_composite.h
+2 −2 tests/jacobian_tests/test_const_scalar_mult.h
+2 −2 tests/jacobian_tests/test_const_vector_mult.h
+4 −4 tests/jacobian_tests/test_elementwise_mult.h
+2 −2 tests/jacobian_tests/test_hstack.h
+3 −3 tests/jacobian_tests/test_left_matmul.h
+2 −2 tests/jacobian_tests/test_log.h
+1 −1 tests/jacobian_tests/test_matmul.h
+4 −3 tests/jacobian_tests/test_prod.h
+2 −2 tests/jacobian_tests/test_prod_axis_one.h
+1 −1 tests/jacobian_tests/test_prod_axis_zero.h
+2 −2 tests/jacobian_tests/test_quad_form.h
+5 −5 tests/jacobian_tests/test_quad_over_lin.h
+3 −3 tests/jacobian_tests/test_rel_entr.h
+1 −1 tests/jacobian_tests/test_rel_entr_scalar_vector.h
+1 −1 tests/jacobian_tests/test_rel_entr_vector_scalar.h
+2 −2 tests/jacobian_tests/test_right_matmul.h
+5 −5 tests/jacobian_tests/test_sum.h
+2 −2 tests/jacobian_tests/test_trace.h
+1 −1 tests/jacobian_tests/test_transpose.h
+1 −1 tests/profiling/profile_left_matmul.h
+18 −0 tests/utils/test_cblas.h
+3 −3 tests/utils/test_coo_matrix.h
+7 −7 tests/utils/test_csc_matrix.h
+4 −4 tests/utils/test_csr_csc_conversion.h
+10 −10 tests/utils/test_csr_matrix.h
+9 −9 tests/utils/test_linalg_sparse_matmuls.h
+136 −0 tests/utils/test_matrix.h
+1 −1 tests/wsum_hess/elementwise/test_entr.h
+1 −1 tests/wsum_hess/elementwise/test_exp.h
+4 −4 tests/wsum_hess/elementwise/test_hyperbolic.h
+2 −2 tests/wsum_hess/elementwise/test_log.h
+1 −1 tests/wsum_hess/elementwise/test_logistic.h
+1 −1 tests/wsum_hess/elementwise/test_power.h
+3 −3 tests/wsum_hess/elementwise/test_trig.h
+1 −1 tests/wsum_hess/elementwise/test_xexp.h
+3 −3 tests/wsum_hess/test_broadcast.h
+2 −2 tests/wsum_hess/test_const_scalar_mult.h
+2 −2 tests/wsum_hess/test_const_vector_mult.h
+2 −2 tests/wsum_hess/test_hstack.h
+3 −3 tests/wsum_hess/test_left_matmul.h
+2 −2 tests/wsum_hess/test_matmul.h
+4 −4 tests/wsum_hess/test_multiply.h
+4 −4 tests/wsum_hess/test_prod.h
+4 −4 tests/wsum_hess/test_prod_axis_one.h
+3 −3 tests/wsum_hess/test_prod_axis_zero.h
+1 −1 tests/wsum_hess/test_quad_form.h
+2 −2 tests/wsum_hess/test_quad_over_lin.h
+3 −3 tests/wsum_hess/test_rel_entr.h
+1 −1 tests/wsum_hess/test_rel_entr_scalar_vector.h
+1 −1 tests/wsum_hess/test_rel_entr_vector_scalar.h
+2 −2 tests/wsum_hess/test_right_matmul.h
+3 −3 tests/wsum_hess/test_sum.h
+3 −3 tests/wsum_hess/test_trace.h
+1 −1 tests/wsum_hess/test_transpose.h
12 changes: 5 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[build-system]
requires = ["numpy >= 2.0.0", "setuptools >= 68.1.0", "wheel"]
build-backend = "setuptools.build_meta"
requires = ["scikit-build-core >= 0.10", "numpy >= 2.0.0"]
build-backend = "scikit_build_core.build"

[project]
name = "sparsediffpy"
Expand All @@ -14,8 +14,6 @@ license = "Apache-2.0"
file = "README.md"
content-type = "text/markdown"

[tool.setuptools]
include-package-data = false

[tool.setuptools.packages.find]
include = ["sparsediffpy*"]
[tool.scikit-build]
cmake.minimum-version = "3.15"
wheel.packages = ["sparsediffpy"]
72 changes: 0 additions & 72 deletions setup.py

This file was deleted.

101 changes: 101 additions & 0 deletions sparsediffpy/_bindings/atoms/dense_matmul.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#ifndef ATOM_DENSE_MATMUL_H
#define ATOM_DENSE_MATMUL_H

#include "bivariate.h"
#include "common.h"

/* Dense left matrix multiplication: A @ f(x) where A is a dense matrix.
*
* Python signature:
* make_dense_left_matmul(child, A_data_flat, m, n)
*
* - child: the child expression capsule f(x).
* - A_data_flat: contiguous row-major numpy float64 array of size m*n.
* - m, n: dimensions of matrix A. */
static PyObject *py_make_dense_left_matmul(PyObject *self, PyObject *args)
{
PyObject *child_capsule;
PyObject *data_obj;
int m, n;
if (!PyArg_ParseTuple(args, "OOii", &child_capsule, &data_obj, &m, &n))
{
return NULL;
}

expr *child = (expr *) PyCapsule_GetPointer(child_capsule, EXPR_CAPSULE_NAME);
if (!child)
{
PyErr_SetString(PyExc_ValueError, "invalid child capsule");
return NULL;
}

PyArrayObject *data_array =
(PyArrayObject *) PyArray_FROM_OTF(data_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
if (!data_array)
{
return NULL;
}

double *A_data = (double *) PyArray_DATA(data_array);

expr *node = new_left_matmul_dense(child, m, n, A_data);
Py_DECREF(data_array);

if (!node)
{
PyErr_SetString(PyExc_RuntimeError,
"failed to create dense_left_matmul node");
return NULL;
}
expr_retain(node);
return PyCapsule_New(node, EXPR_CAPSULE_NAME, expr_capsule_destructor);
}

/* Dense right matrix multiplication: f(x) @ A where A is a dense matrix.
*
* Python signature:
* make_dense_right_matmul(child, A_data_flat, m, n)
*
* - child: the child expression capsule f(x).
* - A_data_flat: contiguous row-major numpy float64 array of size m*n.
* - m, n: dimensions of matrix A. */
static PyObject *py_make_dense_right_matmul(PyObject *self, PyObject *args)
{
PyObject *child_capsule;
PyObject *data_obj;
int m, n;
if (!PyArg_ParseTuple(args, "OOii", &child_capsule, &data_obj, &m, &n))
{
return NULL;
}

expr *child = (expr *) PyCapsule_GetPointer(child_capsule, EXPR_CAPSULE_NAME);
if (!child)
{
PyErr_SetString(PyExc_ValueError, "invalid child capsule");
return NULL;
}

PyArrayObject *data_array =
(PyArrayObject *) PyArray_FROM_OTF(data_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
if (!data_array)
{
return NULL;
}

double *A_data = (double *) PyArray_DATA(data_array);

expr *node = new_right_matmul_dense(child, m, n, A_data);
Py_DECREF(data_array);

if (!node)
{
PyErr_SetString(PyExc_RuntimeError,
"failed to create dense_right_matmul node");
return NULL;
}
expr_retain(node);
return PyCapsule_New(node, EXPR_CAPSULE_NAME, expr_capsule_destructor);
}

#endif /* ATOM_DENSE_MATMUL_H */
2 changes: 1 addition & 1 deletion sparsediffpy/_bindings/atoms/left_matmul.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "common.h"

/* Left matrix multiplication: A @ f(x) where A is a constant matrix */
static PyObject *py_make_left_matmul(PyObject *self, PyObject *args)
static PyObject *py_make_sparse_left_matmul(PyObject *self, PyObject *args)
{
PyObject *child_capsule;
PyObject *data_obj, *indices_obj, *indptr_obj;
Expand Down
2 changes: 1 addition & 1 deletion sparsediffpy/_bindings/atoms/right_matmul.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "common.h"

/* Right matrix multiplication: f(x) @ A where A is a constant matrix */
static PyObject *py_make_right_matmul(PyObject *self, PyObject *args)
static PyObject *py_make_sparse_right_matmul(PyObject *self, PyObject *args)
{
PyObject *child_capsule;
PyObject *data_obj, *indices_obj, *indptr_obj;
Expand Down
13 changes: 9 additions & 4 deletions sparsediffpy/_bindings/bindings.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "atoms/const_vector_mult.h"
#include "atoms/constant.h"
#include "atoms/cos.h"
#include "atoms/dense_matmul.h"
#include "atoms/diag_vec.h"
#include "atoms/entr.h"
#include "atoms/exp.h"
Expand Down Expand Up @@ -109,10 +110,14 @@ static PyMethodDef DNLPMethods[] = {
{"make_entr", py_make_entr, METH_VARARGS, "Create entr node"},
{"make_logistic", py_make_logistic, METH_VARARGS, "Create logistic node"},
{"make_xexp", py_make_xexp, METH_VARARGS, "Create xexp node"},
{"make_left_matmul", py_make_left_matmul, METH_VARARGS,
"Create left matmul node (A @ f(x))"},
{"make_right_matmul", py_make_right_matmul, METH_VARARGS,
"Create right matmul node (f(x) @ A)"},
{"make_sparse_left_matmul", py_make_sparse_left_matmul, METH_VARARGS,
"Create sparse left matmul node (A @ f(x))"},
{"make_dense_left_matmul", py_make_dense_left_matmul, METH_VARARGS,
"Create dense left matmul node (A @ f(x)) where A is dense"},
{"make_sparse_right_matmul", py_make_sparse_right_matmul, METH_VARARGS,
"Create sparse right matmul node (f(x) @ A)"},
{"make_dense_right_matmul", py_make_dense_right_matmul, METH_VARARGS,
"Create dense right matmul node (f(x) @ A) where A is dense"},
{"make_quad_form", py_make_quad_form, METH_VARARGS,
"Create quadratic form node (x' * Q * x)"},
{"make_quad_over_lin", py_make_quad_over_lin, METH_VARARGS,
Expand Down
Loading