refactor: minimize Python C API usage in tensor conversion - #892
Merged
voltjia merged 3 commits intoAug 7, 2026
Merged
Conversation
voltjia
marked this pull request as ready for review
August 7, 2026 03:37
baominghelly
approved these changes
Aug 7, 2026
voltjia
merged commit Aug 7, 2026
af5d19e
into
perf/optimize-generic-tensor-conversion
6 checks passed
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
pybind11::handleinstead of exposing rawPyObject *values throughout the conversion helpersPyObject_GetAttrwrapper with publicpybind11::getattrMotivation
PR #832 gets most of its speedup from cached attribute names, fused no-argument method calls, and fast metadata conversion. This follow-up narrows direct Python C API usage without giving those gains back.
A broader prototype also replaced
PySequence_Fast_GET_*and direct UTF-8 access with public pybind11 iteration/casters. It regressed the 16x16 GEMM B -> C comparison by 4.25% (95% CI: +1.43% to +6.56%), so this revision deliberately retains those measured hot paths. The remaining direct calls are limited to name interning, fused no-argument calls, integer extraction, fast sequence access, UTF-8 views, and their error handling.Validation
Environment:
cuda:031b2a32c3e0b8eb2ef9da634c3ab934ce87e6af3A is PR #832 base
2cfb526, B is PR #832 head50f979df, and C is this branch.The upper bound of the B -> C regression interval stays below the preselected +2% non-inferiority margin for all three cases.
Checks:
abs,add,gemmpytest tests/test_gemm.py -m smoke -q -n 1: 6 passedruff check tests/conftest.pyclang-format 21.1.8 --dry-run --Werror src/pybind11_utils.hgit diff --check