Optimize CANN auto padding lookup#29693
Open
GopalakrishnanN wants to merge 1 commit into
Open
Conversation
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR optimizes CANN operator attribute setup by replacing per-execution std::unordered_map construction/lookup for auto-pad mode strings with allocation-free switch-based helpers, reducing CPU-side overhead in Conv/AveragePool/MaxPool.
Changes:
- Added
padding.hwithGetConvAutoPadModeandGetPoolAutoPadModehelpers. - Updated CANN Conv/AveragePool/MaxPool to use the helpers and removed
unordered_mapincludes. - Preserved special-casing for global pooling (
padding_mode = "VALID").
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| onnxruntime/core/providers/cann/nn/padding.h | Introduces centralized auto-pad → CANN mode string helpers. |
| onnxruntime/core/providers/cann/nn/conv.cc | Uses GetConvAutoPadMode() instead of constructing a map each execution. |
| onnxruntime/core/providers/cann/nn/average_pool.cc | Uses GetPoolAutoPadMode() for non-global pooling padding_mode. |
| onnxruntime/core/providers/cann/nn/max_pool.cc | Uses GetPoolAutoPadMode() for non-global pooling padding_mode. |
Comment on lines
+11
to
+24
| constexpr const char* GetConvAutoPadMode(AutoPadType auto_pad) { | ||
| switch (auto_pad) { | ||
| case AutoPadType::NOTSET: | ||
| return "NOTSET"; | ||
| case AutoPadType::VALID: | ||
| return "VALID"; | ||
| case AutoPadType::SAME_UPPER: | ||
| return "SAME_UPPER"; | ||
| case AutoPadType::SAME_LOWER: | ||
| return "SAME_LOWER"; | ||
| } | ||
|
|
||
| return nullptr; | ||
| } |
Comment on lines
+26
to
+39
| constexpr const char* GetPoolAutoPadMode(AutoPadType auto_pad) { | ||
| switch (auto_pad) { | ||
| case AutoPadType::NOTSET: | ||
| return "CALCULATED"; | ||
| case AutoPadType::VALID: | ||
| return "VALID"; | ||
| case AutoPadType::SAME_UPPER: | ||
| return "SAME"; | ||
| case AutoPadType::SAME_LOWER: | ||
| return "SAME"; | ||
| } | ||
|
|
||
| return nullptr; | ||
| } |
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.
Description
Replace the per-execution
std::unordered_mapconstruction in CANN Conv, AveragePool, and MaxPool with allocation-freeconstexprswitch helpers.VALIDmode unchangedAutoPadTypeenumerator explicitlyunordered_mapincludesPerformance
Optimized MSVC
/O2microbenchmark of the exact map construction/lookup versus the new switch helpers (2,000,000 calls per trial, median of 9 trials):This is approximately a 99% reduction (80–109× faster) for the lookup itself. This is a CPU-side microbenchmark, not an end-to-end inference measurement; overall impact depends on the model and CANN device execution time.
Validation
lintrunnerpassed for all changed filesclang-format --dry-run --Werrorpassedgit diff --checkpassedCANN build and provider tests were not run because the Windows environment does not have the Ascend/CANN SDK configured. No existing CANN-independent test exercises these lookup constants.