Drop redundant allocations and zero-inits in XPU kernels - #4426
Open
cyyever wants to merge 2 commits into
Open
Conversation
unique_template and compute_inverse allocated inverse_indices and counts as
at::empty({num_inp}) and then immediately reassigned them (from compute_inverse
/ compute_unique / inv_loc) without reading, so each num_inp-sized buffer was
allocated and freed for nothing. Declare each tensor at its initialization
point instead, folding num_out into the compute_unique structured binding, so
the wasted allocations are gone without a separate default-construct step.
Test Plan:
Non-behavioral refactor (allocation sites only). lintrunner (clang-format)
reports no issues. A full XPU build and `pytest test/xpu/test_ops_xpu.py -k
unique` require an Intel GPU and were not run in this environment.
Authored with the assistance of Claude, an AI coding assistant.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Several buffers were zero-filled and then completely overwritten before any read, so the memset was wasted work: - im2col / col2im: the output is resized and zeroed, but both kernels write every output element unconditionally (im2col stores the in-bounds input or an explicit 0 for each kh*kw entry; col2im is a gather assigning out_ptr[id] for every id). Drop output.zero_(), matching upstream cuda Im2Col.cu / Col2Im.cu. - inclusive_scan_if scratch: the kernel writes d_first_[i] for all N elements before reading, so at::zeros never shows through; use at::empty. - batch_norm backward reduce: sumn_dy / sum_dy_xmu get one write per channel in both reduce paths, so use at::empty, matching upstream Normalization.cuh. Test Plan: Non-behavioral (each buffer is fully overwritten before use); reasoning corroborated against the upstream CUDA allocators. lintrunner (clang-format) reports no issues. A full XPU build and the relevant op tests (pytest test/xpu/test_ops_xpu.py for unfold/fold, batch_norm, and the unique_dim path exercising inclusive_scan_if) require an Intel GPU and were not run here. Authored with the assistance of Claude, an AI coding assistant. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
cyyever
force-pushed
the
agent/unique-dead-alloc
branch
from
July 23, 2026 10:42
76ca5e2 to
a526184
Compare
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.
Two wasted-work removals in the XPU SYCL kernels (no behavior change):
num_inp-sized allocations in the unique kernels (allocated, thenoverwritten before any read).
output.zero_(), andat::zeros->at::emptyin the pstl segmented scan andbatch_norm backward reduce (matches upstream CUDA, which does not zero these).
Only runtime effect: a few fewer allocations/memsets.
Test: none (non-behavioral; covered by existing test/xpu op tests -- unfold/fold,
batch_norm, unique)
Authored with the assistance of Claude, an AI coding assistant.