Skip to content

perf: optimize cache for tensor conversion - #928

Open
DockeyMint wants to merge 1 commit into
InfiniTensor:masterfrom
DockeyMint:perf/optimize-cache-tensor-conversion
Open

perf: optimize cache for tensor conversion#928
DockeyMint wants to merge 1 commit into
InfiniTensor:masterfrom
DockeyMint:perf/optimize-cache-tensor-conversion

Conversation

@DockeyMint

@DockeyMint DockeyMint commented Aug 13, 2026

Copy link
Copy Markdown

Summary

  • Add TensorCache mechanism for thread-local caching of tensor conversions in pybind11_utils.h, keyed on data_ptr + shape + strides + dtype
  • Skip expensive DeviceFromPybind11HandleImpl and full conversion path on cache hit
  • Merge with existing InternedNames / CallMethodNoArgs optimization (PR perf: optimize generic tensor conversion #832)

Motivation

Tensor conversion from Python to native representation is a hot path. The cache avoids redundant metadata extraction and memory allocation when the same tensor is converted repeatedly, while preserving the low-level Python C API optimizations already on master.

Type of Change

  • feat — new feature / new operator / new platform
  • fix — bug fix
  • perf — performance improvement (no behavioral 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)
  • MetaX (WITH_METAX)
  • Cambricon (WITH_CAMBRICON)
  • Moore (WITH_MOORE)
  • Ascend (WITH_ASCEND)
  • PyTorch C++ bindings (WITH_TORCH)
  • Build system / CMake / CI
  • Python bindings / user-facing API

Smoke Test Result

CUDACXX=/usr/local/cuda/bin/nvcc CMAKE_BUILD_PARALLEL_LEVEL=32 \
SKBUILD_BUILD_DIR=build-pr-smoke-cuda13 \
python -m pip install '.[dev]' --no-build-isolation --no-deps \
  --config-settings=cmake.define.INFINI_RT_ROOT=/data/shared/baoming/workplace/new_ops/infinirt-prefix \
  --config-settings=cmake.define.INFINI_OPS_SMOKE_BUILD=ON \
  --config-settings=cmake.define.WITH_NVIDIA=ON \
  --config-settings=cmake.define.WITH_CPU=OFF

Successfully built InfiniOps
Successfully installed InfiniOps-0.1.0

CUDA_VISIBLE_DEVICES=4 python -m pytest tests -m smoke -q
120 passed, 28 skipped, 13867 deselected in 15.37s

Test Results on Supported Platforms

Platform Affected Build / Smoke Result Full Result / Notes
NVIDIA Yes Build passed; 120 passed, 28 skipped, Full suite not run
Iluvatar
MetaX
Cambricon
Moore
Ascend
Full `pytest` output (optional)

Benchmark / Performance Impact

  • Hardware: NVIDIA A100‑SXM4‑80GB; no foreign GPU process was observed during the alternating A/B run.
  • Workload: FP32 16x16 GEMM, explicit implementation index 0.

Notes for Reviewers

  • The optimization remains framework‑neutral and preserves the existing duck‑typed tensor contract (data_ptr, shape, dtype, device, and stride).

@DockeyMint
DockeyMint requested a review from a team August 13, 2026 08: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