Skip to content

perf: reduce TensorView metadata allocations#33

Merged
voltjia merged 1 commit into
masterfrom
perf/reduce-tensor-view-allocations
Jul 23, 2026
Merged

perf: reduce TensorView metadata allocations#33
voltjia merged 1 commit into
masterfrom
perf/reduce-tensor-view-allocations

Conversation

@voltjia

@voltjia voltjia commented Jul 22, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Add exact by-value TensorView::Shape / TensorView::Strides constructor overloads so owned metadata can be moved into TensorView without redundant copies.
  • Construct initializer-list metadata directly, and add Linux allocation regression coverage plus focused construction, indexing, and transpose microbenchmarks.

Motivation

TensorView is intended to be a lightweight framework-neutral tensor metadata layer, but common rvalue, initializer-list, operator[], and T() paths created temporary vectors and then copied them into the result. Non-empty rank-2 views therefore performed three or four metadata allocations where two owned buffers are sufficient.

The existing public Shape = std::vector<Size> and Strides = std::vector<Stride> aliases, generic constructors, object data members, and behavior are preserved in this first phase.

Related issue: N/A - this performance work is not linked to an existing issue.

Type of Change

  • feat - new feature / new backend capability / new public API
  • fix - bug fix
  • perf - performance improvement without behavior change
  • refactor - code restructuring without behavior change
  • test - adding or fixing tests only
  • docs - documentation only
  • build / ci - build system or CI configuration
  • chore - tooling, formatting, or other non-code changes
  • Breaking change (requires a ! in the Conventional Commits prefix or a BREAKING CHANGE: footer)

Platforms Affected

  • CPU (WITH_CPU)
  • NVIDIA (WITH_NVIDIA)
  • Iluvatar (WITH_ILUVATAR)
  • Hygon (WITH_HYGON)
  • MetaX (WITH_METAX)
  • Moore (WITH_MOORE)
  • Cambricon (WITH_CAMBRICON)
  • Ascend (WITH_ASCEND)
  • Build system / CMake / generated headers
  • Public headers / installed consumer API
  • Documentation only

Smoke Build and Test Result

CPU Release build on the Linux x86_64 NVIDIA validation host (GCC 11.4.0):

cmake -S . -B build-cpu \
  -DCMAKE_BUILD_TYPE=Release \
  -DWITH_CPU=ON \
  -DINFINI_RT_BUILD_TESTING=ON \
  -DINFINI_RT_BUILD_PERFORMANCE_TESTING=ON
cmake --build build-cpu -j2
ctest --test-dir build-cpu --output-on-failure

100% tests passed, 0 tests failed out of 10

NVIDIA Release build in the existing accelerator-dev/nvidia:latest image on 8x A100-SXM4-80GB (CUDA 13.1.80, GCC 13.3.0):

docker run --rm --gpus all \
  -v "$PWD:/workspace/InfiniRT" -w /workspace/InfiniRT \
  accelerator-dev/nvidia:latest \
  cmake -S . -B build-nvidia \
    -DCMAKE_BUILD_TYPE=Release \
    -DWITH_NVIDIA=ON \
    -DINFINI_RT_BUILD_TESTING=ON \
    -DINFINI_RT_BUILD_PERFORMANCE_TESTING=ON
docker run --rm --gpus all \
  -v "$PWD:/workspace/InfiniRT" -w /workspace/InfiniRT \
  accelerator-dev/nvidia:latest \
  cmake --build build-nvidia -j2
docker run --rm --gpus all --entrypoint ctest \
  -v "$PWD:/workspace/InfiniRT" -w /workspace/InfiniRT \
  accelerator-dev/nvidia:latest \
  --test-dir build-nvidia -E '^perf_' --output-on-failure

100% tests passed, 0 tests failed out of 8

Test Results on Supported Platforms

Platform Affected Build / Smoke Result Full Result / Notes
CPU Yes Full build and tests passed 10/10 passed, including allocation, performance, install, and installed-consumer tests
NVIDIA Yes NVIDIA build and non-performance smoke tests passed 8/8 smoke tests passed; the final clang-format-only amend rebuilt successfully in the same image
Iluvatar Yes Not run - SDK/host unavailable in this pass Backend-specific code is unchanged; maintainer smoke validation requested
Hygon Yes Not run - SDK/host unavailable in this pass Backend-specific code is unchanged; maintainer smoke validation requested
MetaX Yes Not run - SDK/host unavailable in this pass Backend-specific code is unchanged; maintainer smoke validation requested
Moore Yes Not run - SDK/host unavailable in this pass Backend-specific code is unchanged; maintainer smoke validation requested
Cambricon Yes Not run - SDK/host unavailable in this pass Backend-specific code is unchanged; maintainer smoke validation requested
Ascend Yes Not run - SDK/host unavailable in this pass Backend-specific code is unchanged; maintainer smoke validation requested
Full `ctest` output
CPU:
100% tests passed, 0 tests failed out of 10
Total Test time (real) = 2.55 sec

NVIDIA smoke (performance executables excluded; run before the final clang-format-only amend):
100% tests passed, 0 tests failed out of 8
Total Test time (real) = 11.75 sec

The amended head rebuilt successfully in the same NVIDIA image.

Benchmark / Performance Impact

Harness: tests/performance/perf_tensor_view.cc, Release CPU backend on the same Linux x86_64 validation host, GCC 11.4.0, rank 2, shape [32, 64], float32 where applicable, 1,000 warmup iterations, 200,000 measured iterations per sample, and 7 samples. Values below are mean nanoseconds per operation.

Benchmark Baseline Candidate Delta
rvalue full metadata construction 74.86 ns 37.27 ns -50.2%
rvalue default-stride construction 64.61 ns 41.99 ns -35.0%
initializer-list construction 92.58 ns 48.24 ns -47.9%
operator[] 91.90 ns 48.88 ns -46.8%
T() 95.51 ns 49.70 ns -48.0%
numel() control 3.92 ns 3.92 ns 0.0%

The allocation regression test verifies the corresponding metadata allocation reductions: rvalue full metadata, initializer-list, operator[], and T() paths decrease from four allocations to two; rvalue default-stride construction decreases from three to two. Copy and move construction remain at two and zero allocations, respectively.

Notes for Reviewers

  • This is intentionally a first-phase optimization: Shape and Strides remain std::vector, generic tensor-like/range constructors remain available, and TensorView data member types and order are unchanged.
  • The allocation-count test is registered only on Linux because it relies on ELF cross-DSO operator new interposition.
  • Small-vector storage, borrowed metadata views, downstream pass-by-reference changes, and unrelated TensorView correctness fixes are deliberately out of scope.
  • git diff --check and both clang-format 21 CI checks pass.

@voltjia
voltjia force-pushed the perf/reduce-tensor-view-allocations branch from f3cde2a to b1187cc Compare July 22, 2026 23:29
@voltjia
voltjia marked this pull request as ready for review July 23, 2026 02:00
@voltjia voltjia changed the title perf: reduce TensorView metadata allocations perf: reduce TensorView metadata allocations Jul 23, 2026
@voltjia
voltjia merged commit 656b941 into master Jul 23, 2026
6 checks passed
@voltjia
voltjia deleted the perf/reduce-tensor-view-allocations branch July 23, 2026 02:01
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