Repository navigation
Handle strided NumPy inputs in C++ bindings - #5
Open
coderyaz856 wants to merge 1 commit into
Open
coderyaz856 wants to merge 1 commit into
coderyaz856 wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
test_annotate_cells_backed_handles_retryable_open_conflict).git diff --checkpasses.