Speed up in-place vector-to-C-contiguous-matrix broadcast on CPU#2981
Open
vchamarthi wants to merge 3 commits into
Open
Speed up in-place vector-to-C-contiguous-matrix broadcast on CPU#2981vchamarthi wants to merge 3 commits into
vchamarthi wants to merge 3 commits into
Conversation
In-place binary elementwise ops broadcasting a vector against a
C-contiguous matrix (m += row, m += col[:, None]) fell through to the
general strided kernel on CPU, although a vectorized row-broadcast
kernel already exists and is used by the out-of-place path.
- Add the missing C-contiguous row-broadcast dispatch branch to
py_binary_inplace_ufunc (reuses the existing
BinaryInplaceRowMatrixBroadcastingFunctor); the in-place template
previously only had the F-style {1,0} branch while the out-of-place
path already handled the {0,1} C-contiguous case.
- Add BinaryInplaceColMatrixBroadcastingFunctor for the column case
(mat[gid] += vec[gid / n1]) and wire it for add via a defaulted extra
template parameter, keeping all other in-place ufuncs unchanged.
Both paths are guarded by exact simplified-stride checks and fall back
to the strided kernel otherwise. Results are bitwise-identical.
Adds TestAdd::test_inplace_row_broadcast and
TestAdd::test_inplace_column_broadcast covering several shapes (incl.
row lengths not a multiple of the sub-group size) across dtypes.
Collaborator
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.
Speed up in-place vector-to-C-contiguous-matrix broadcast on CPU
In-place binary elementwise ops that broadcast a vector against a C-contiguous matrix
(
m += row,m += col[:, None]) fell through to the general strided kernel (scalar, oneelement per work-item) on the CPU device, even though a vectorized broadcast kernel already
exists and is used by the out-of-place path.
Changes
m += row): add the missing C-contiguous dispatch branch topy_binary_inplace_ufunc(the out-of-place path already had it; the in-place template onlyhad the F-style
{1,0}branch). Reuses the existingBinaryInplaceRowMatrixBroadcastingFunctor,so it benefits all in-place binary ufuncs — no new kernel.
m += col[:, None]): addBinaryInplaceColMatrixBroadcastingFunctor(
mat[gid] += vec[gid / n1]) and wire it foraddvia a defaulted extra templateparameter, leaving all other in-place ufuncs unchanged.
Both paths are guarded by exact simplified-stride checks and fall back to the strided kernel
otherwise; results are bitwise-identical.
Results
13th Gen Intel Core i5-13400 (CPU/OpenCL), float32, dpnp built from source. Same op, before/after
this change:
D= 21846x21846)D += rowD += col[:, None]This was the only op category where dpnp-on-CPU lost to NumPy in dpbench
pairwise_distance(M16Gb, single). End-to-end via the dpbench CLI (
--repeat 30,--validatepassing):0.92x → 1.52x vs stock NumPy (731 ms → 445 ms; NumPy 678 ms).
Checklist
(
TestAdd::test_inplace_row_broadcast,TestAdd::test_inplace_column_broadcast)bitwise-equal to NumPy on
opencl:cpuandlevel_zero:gpu, across float32/64 + int32/64 andshapes incl. non-sub-group-multiple row lengths)
icpx 2026.0,
-Wall -Wextra: 0 warnings)