perf(torch): reuse ATen wrappers in generated operators#816
Draft
voltjia wants to merge 1 commit into
Draft
Conversation
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
TargetTensorPool<Adapter>with stable entries and move-only call-scoped leases.Tensor.libinfiniops.so.Motivation
Generated Torch fallback operators currently call
at::from_blobfor every tensor argument on every invocation. This does not copy tensor data, but it repeatedly constructsTensorImpl/Storagewrappers. The Python binding also obtains tensor metadata through repeated Python attribute calls.PR #798 avoids most of that cost by retaining and reusing the caller's ATen handle. This PR instead caches target-native wrappers behind a compile-time adapter. An
X -> InfiniOps -> ATenpath can therefore reuse ATen wrappers without putting anX-specific owner or handle into the genericTensor/operator cache.Related: #798
Type of Change
feat- new feature / new operator / new platformfix- bug fixperf- performance improvement (no intended 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)The generated path also covers Hygon, which is not listed in the template.
Smoke Test Result
Final commit:
2671579bec529394d343b74fbce6bc889dfe75cbImage:
accelerator-dev/nvidia:latest(nvcr.io/nvidia/pytorch:25.12-py3)Hardware: NVIDIA A100-SXM4-80GB
python -m pip install --force-reinstall --no-build-isolation --no-deps '.[dev]' \ --config-settings=cmake.define.WITH_CPU=ON \ --config-settings=cmake.define.WITH_NVIDIA=ON \ --config-settings=cmake.define.WITH_TORCH=ON \ --config-settings=cmake.define.INFINI_RT_ROOT=/home/huangjiacheng/infiniops-pr798-perf-20260715/infinirt-prefix \ --config-settings=cmake.define.INFINI_OPS_SMOKE_BUILD=ON \ --config-settings=cmake.define.INFINI_OPS_OPS=add,mul,cast,cat,gemm,matmul,linear,rms_norm,swiglu,silu_and_mul,causal_softmax,causal_softmax_infinilm,abs,clamp,exp,cutlass_scaled_mm,moe_sum,flash_attn_varlen_func,scaled_dot_product_attention,stack \ --config-settings=cmake.define.INFINI_OPS_TORCH_OPS=abs,clamp,exp,stack python -m pytest tests -m smoke -qTest Results on Supported Platforms
The same smoke build included CPU. Additional final-SHA checks:
python -m pytest tests/test_target_tensor_pool.py tests/test_generate_torch_ops.py tests/test_generate_wrappers.py -q:30 passed in 1.83s.0.15.22check and format check passed for all changed Python files.21.1.8passed for all changed C++ files.data_ptr/shape/dtype/device/stride()protocol passed required Abs, optional Clamp, and list Stack calls in the Release wheel.uint8.import infini.opsbeforeimport torchpassed.141312allocated-byte baseline after releasing 64 MiB (Abs), 128 MiB (Clamp), and 192 MiB (Stack) payloads.readelf -dconfirmed thatlibinfiniops.sohas nolibtorch_python.sodependency.The full final-SHA suite and non-NVIDIA platform smoke suites were not run. This PR remains a draft for those CI/platform checks.
Benchmark / Performance Impact
Benchmark: generated
absTorch fallback,torch.float16, shapes[1, 4096],[8, 4096], and[1, 8192]on A100-SXM4-80GB. The harness used two independent GPU/CPU placements, four Williams-balanced panels per round, seven timed blocks per shape, and alternating data-pointer correctness checks.The performance panel used the pre-publication implementation commit
c856ae9; the tested float16 hot path is unchanged in the final commit. These numbers are post-hoc directional sensitivity results. The preregistered primary analysis failed its control-stability checks, so the exact ratios are not presented as definitive latency estimates.Notes for Reviewers
Rebindchanges only the borrowed data pointer.at::Tensorcontainers so the native wrappers are destroyed before their leases are released.AtenTensorAdapterinstalls a non-owningc10::DataPtr; no source tensor or storage owner is retained.DataPtrownership, nested-call lease behavior, list-pool high-water allocation, and the MetaX/Moore system-compiler bridge path.