Skip to content

Drop redundant allocations and zero-inits in XPU kernels - #4426

Open
cyyever wants to merge 2 commits into
intel:mainfrom
cyyever:agent/unique-dead-alloc
Open

Drop redundant allocations and zero-inits in XPU kernels#4426
cyyever wants to merge 2 commits into
intel:mainfrom
cyyever:agent/unique-dead-alloc

Conversation

@cyyever

@cyyever cyyever commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Two wasted-work removals in the XPU SYCL kernels (no behavior change):

  • Drop dead num_inp-sized allocations in the unique kernels (allocated, then
    overwritten before any read).
  • Skip redundant zero-init on fully-overwritten buffers: im2col/col2im
    output.zero_(), and at::zeros->at::empty in the pstl segmented scan and
    batch_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.

cyyever and others added 2 commits July 23, 2026 13:36
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
cyyever force-pushed the agent/unique-dead-alloc branch from 76ca5e2 to a526184 Compare July 23, 2026 10:42
@cyyever cyyever changed the title Clean up XPU kernels: drop redundant allocations and zero-inits Drop redundant allocations and zero-inits in XPU kernels Jul 23, 2026
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.

1 participant