Skip to content

Handle strided NumPy inputs in C++ bindings - #5

Open
coderyaz856 wants to merge 1 commit into
KellisLab:mainfrom
coderyaz856:Yazid-The-Tech-Alchemist-strided-bindings
Open

coderyaz856 wants to merge 1 commit into
KellisLab:mainfrom
coderyaz856:Yazid-The-Tech-Alchemist-strided-bindings

Conversation

@coderyaz856

Copy link
Copy Markdown

Fixes #4

Problem

Several C++ bindings read NumPy array buffers as contiguous even when callers passed sliced or reversed views. This could silently produce incorrect values; a negative-stride view could also cause an out-of-bounds read.

Change

Require contiguous arrays at the affected binding boundaries before reading their buffers. Already-contiguous inputs pass through without a copy. The change covers shared array conversion, sparse matrix buffers, centrality assignments, guide status, row scaling, and visualization indices. It does not change the public API or libactionet.

Validation

  • All 11 focused regression tests pass on the corrected build; 9 fail against the original build.
  • Linux full suite: 657 passed, 2 skipped, 1 previously observed unrelated failure (test_annotate_cells_backed_handles_retryable_open_conflict).
  • git diff --check passes.

Arrays declared as py::array_t<T> or py::array_t<T, forcecast> keep
the caller's strides. Several binding helpers read request().ptr as
if the values sat next to each other in memory. A non-contiguous view
such as x[::2], x[::-1] or M[:, 0] therefore gave wrong values with no
error. With negative strides, the read could also go before the start
of the array.

Examples on the old build:
- labels[::2] passed to compute_feature_specificity_sparse produced
  9999 clusters instead of 3.
- reduce_kernel(precomputed_svd={"d": s[::-1], ...}) got
  sigma = [86.7, 4.94, 1.24, 2e-15, 4e-16] instead of
  [86.8, 4.94, 3.98, 2.94, 1.86].
- Reversed labels raised a RuntimeError.

Add contiguous_array_t<T> (c_style | forcecast) and an
ensure_contiguous<T>() helper in wp_utils.h. pybind11 copies the
input only when it is not already C-contiguous, so contiguous arrays
still go through without a copy. Use the helper in:
- int_array_to_uvec and parse_sigma (wp_utils.h)
- scipy_to_arma_sparse data/indices/indptr (wp_utils.cpp). SciPy
  keeps the arrays given to its constructor without copying them.
- compute_archetype_centrality sample_assignments (wp_network.cpp)
- fit_from_arrays status (wp_tools.cpp)
- optional_row_scale (wp_io.cpp)
- optional_float_vector ai/aj (wp_visualization.cpp)

Add tests/test_strided_inputs.py. Each test passes a view with junk
values in the gaps and checks that the result matches the same values
passed as a normal array. Of its 11 tests, 9 fail on the old build and
all 11 pass with this change. The other two (sparse indices/indptr
views and layout ai/aj views) already pass on the old build and are
kept as regression guards. Without the new tests, the rest of the
suite gives the same results before and after.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

C++ bindings misread strided NumPy arrays

1 participant