Skip to content

Slice dense block tensors without allocating blocks - #88

Merged
lkdvos merged 1 commit into
mainfrom
ld-dense-slice
Oct 29, 2026
Merged

lkdvos merged 1 commit into
mainfrom
ld-dense-slice

Conversation

@lkdvos

@lkdvos lkdvos commented Oct 29, 2026

Copy link
Copy Markdown
Member

Slicing a dense BlockTensorMap (e.g. t[2:(end - 1)]) allocated every block of the destination through similar, and _copyslice! then replaced each of them by the source block it selects. The result already shares its blocks with the source, so the allocated blocks were thrown away.

This builds a dense slice directly from the sliced parent array. The result is identical to before, including sharing blocks with the source, but no blocks are allocated. Sparse block tensors are unchanged.

In MPSKit, slicing the continuing channels out of an MPO environment is on the hot path of every effective Hamiltonian built from a MPOHamiltonian (GL[2:(end - 1)]). For a chemistry-like Hamiltonian at χ = 64, one such slice drops from 0.93 MiB to 4 KiB.

  • Integer indices keep their dimension, as _copyslice! did.
  • A method for all-integer indices on BlockTensorMap avoids an ambiguity with the existing one that returns a single block.
  • New test: a dense slice shares the source's blocks and allocates less than a quarter of their data.

🤖 Generated with Claude Code

Slicing a `BlockTensorMap` allocated every block of the destination through `similar`,
only for `_copyslice!` to replace each of them by the block of the source it selects.
Build the slice directly from the sliced parent array instead: the result shares the
blocks of the source, as before, without allocating new ones.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
src/tensors/blocktensor.jl 68.81% <100.00%> (+2.15%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lkdvos
lkdvos merged commit 34a27f5 into main Oct 29, 2026
28 checks passed
@lkdvos
lkdvos deleted the ld-dense-slice branch October 29, 2026 13:29
lkdvos referenced this pull request Oct 29, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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