perf: avoid redundant operator cache lookups - #858
Open
baominghelly wants to merge 1 commit into
Open
Conversation
baominghelly
force-pushed
the
perf/operator-call-cache-fast-path
branch
from
July 30, 2026 07:27
1f34c14 to
b80f9d7
Compare
baominghelly
marked this pull request as ready for review
July 30, 2026 10:08
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.
Summary
Operator::Callso repeated calls can match cached tensor metadata and scalar arguments without rebuilding a fullCacheKeyor probing the hash table.cache.matchhost-profiling range for the hot-cache probes.cache.lookupnow covers only the hash-tabletry_emplace;cache.keyandcache.constructcontinue to cover full-key materialization and operator construction respectively.CacheKeyBuildercompatibility, cache-generation invalidation, and exception behavior; extend the focused C++ and PR feat: add host range profiling #817 profiling tests for the new path and range boundaries.Motivation
Eager operator calls repeatedly rebuild the same cache key and perform an
unordered_maplookup even when consecutive calls use identical metadata. For small operators this host-side work is measurable relative to kernel launch latency.The hot path compares arguments directly against the two most recently used cache entries. It avoids full key construction and map lookup on common repeated/alternating call patterns while retaining the existing path for misses and legacy custom key builders.
Type of Change
feat— new feature / new operator / new platformfix— bug fixperf— performance improvement (no behavioral change)refactor— code restructuring without behavior changetest— adding or fixing tests onlydocs— documentation onlybuild/ci— build system or CI configurationchore— tooling, formatting, or other non-code changes!in the Conventional Commits prefix or aBREAKING CHANGE:footer)Platforms Affected
WITH_CPU)WITH_NVIDIA)WITH_ILUVATAR)WITH_METAX)WITH_CAMBRICON)WITH_MOORE)WITH_ASCEND)WITH_TORCH)Smoke Test Result
A100-SXM4-80GB; isolated profiling ON/OFF builds used
CMAKE_BUILD_PARALLEL_LEVEL=$(nproc), CPU, NVIDIA, and PyTorch support.Test Results on Supported Platforms
WITH_TORCH=ONbuilds passedPR #817 host-range profiling regression coverage:
The expected range counts are asserted directly: a cold call emits one each of
cache.match,cache.key,cache.lookup, andcache.construct; a hot hit emits only onecache.match. The explicitimplementation_index=0binding case also asserts that nobinding.device_conversionrange is emitted.Benchmark / Performance Impact
Incremental A/B benchmark on one NVIDIA A100-SXM4-80GB:
master+ PR perf: reuse ATen tensors and optimize PyTorch fallback wrappers #798 + PR perf: optimize generic tensor conversion #832.torch out=references.P-C-C-P-P-C; no external process appeared on the selected card.Representative reference-adjusted improvements:
maximum0.768 us,div0.593 us,bitwise_and0.557 us,sub0.504 us,exp0.358 us, andsign0.190 us.mm(0.085 us) andaddmm(0.023 us) are compute-dominated and close to measurement noise.These numbers are the incremental cache-key effect only; they do not attribute the larger combined-stack improvement to this PR. A narrow post-profiling-boundary FP16
addindex-0 check showed no median regression, but was phase-noisy and is not used as the primary performance claim.Notes for Reviewers
CacheKey::Matchescompares tensor metadata directly and hashes only scalar/config arguments; vector lengths are included so grouped tensor arguments remain unambiguous.cache.matchscope wraps both the most-recent and previous-entry probes, so eachOperator::Callemits at most one match range. It is logically separate fromcache.lookup, which is reserved for the hash-table operation.unordered_map. Cache generation changes clear both the map and hot pointers.CacheKeyBuilderspecializations that do not implementMatchesautomatically fall back to the existing full-key/hash-table path.try_emplaceavoids a second lookup on misses; a failed operator construction erases the placeholder before rethrowing.operator.huses unconditional named[[maybe_unused]] HostRangeScopeobjects. With profiling OFF, PR feat: add host range profiling #817's empty scope type optimizes away; the OFF binary symbol scan above confirms that no scope constructor/destructor symbols remain.src/operator.h,src/host_range_profiler.h,src/host_range_profiler.cc,tests/test_cpp_api.py, andtests/test_host_range_profile.py. It does not include PR perf: reuse ATen tensors and optimize PyTorch fallback wrappers #798, PR perf: optimize generic tensor conversion #832, PR perf: avoid redundant tensor conversion in eager bindings #815, benchmark artifacts, or*infinilmoperator-specific changes.