Skip to content

Optimize CANN auto padding lookup#29693

Open
GopalakrishnanN wants to merge 1 commit into
microsoft:mainfrom
GopalakrishnanN:gnallasamy-microsoft-optimize-cann-padding
Open

Optimize CANN auto padding lookup#29693
GopalakrishnanN wants to merge 1 commit into
microsoft:mainfrom
GopalakrishnanN:gnallasamy-microsoft-optimize-cann-padding

Conversation

@GopalakrishnanN

@GopalakrishnanN GopalakrishnanN commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Description

Replace the per-execution std::unordered_map construction in CANN Conv, AveragePool, and MaxPool with allocation-free constexpr switch helpers.

  • Preserve Conv auto-pad strings exactly
  • Share the identical pooling auto-pad mapping
  • Keep global pooling's direct VALID mode unchanged
  • Handle every AutoPadType enumerator explicitly
  • Remove unused unordered_map includes

Performance

Optimized MSVC /O2 microbenchmark of the exact map construction/lookup versus the new switch helpers (2,000,000 calls per trial, median of 9 trials):

Path Before After Time saved
Conv 188–218 ns 2.25–2.33 ns ~0.19–0.22 µs/call
Pool 187–218 ns 1.97–2.15 ns ~0.18–0.22 µs/call

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

  • lintrunner passed for all changed files
  • clang-format --dry-run --Werror passed
  • git diff --check passed

CANN 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.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.h with GetConvAutoPadMode and GetPoolAutoPadMode helpers.
  • Updated CANN Conv/AveragePool/MaxPool to use the helpers and removed unordered_map includes.
  • 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;
}
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.

2 participants