Skip to content

Conversation

edgchen1
Copy link
Contributor

@edgchen1 edgchen1 commented Jun 24, 2025

Description

Add IExecutionProvider::ShouldConvertDataLayoutForOp() to allow EPs to customize layout sensitive ops. Move existing hardcoded EP-specific logic out of layout transformer code.

Add OrtEp::ShouldConvertDataLayoutForOp to ABI EP API to allow similar customization by plugin EPs.

Motivation and Context

Enable layout sensitive op customization through internal EP interface and the ABI EP API.

@fs-eire
Copy link
Contributor

fs-eire commented Jun 24, 2025

What is the difference between the newly added interface and the existing GetPreferredLayout()?

@edgchen1
Copy link
Contributor Author

What is the difference between the newly added interface and the existing GetPreferredLayout()?

GetPreferredLayout returns a single preferred layout value for the whole EP. some EPs have further customization of what ops should be considered layout sensitive. this is hardcoded here currently:

// handle special cases
#if defined(USE_JSEP)
// TODO(fs-eire): Remove special case handing of JSEP once NHWC Resize implementation is fixed
if (node.GetExecutionProviderType() == kJsExecutionProvider) {
if (node.OpType() == "Resize") {
// leave Resize as-is pending bugfix for NHWC implementation. this means the node will remain in the ONNX domain
// with the original input layout.
return false;
}
}
#endif
// NHWC for Resize operator is not implemented on kWebGpuExecutionProvider
#if defined(USE_WEBGPU)
if (node.GetExecutionProviderType() == kWebGpuExecutionProvider) {
if (node.OpType() == "Resize") {
return false;
}
}
#endif
// TODO: We don't need to check USE_CUDA || USE_CUDA_PROVIDER_INTERFACE in this function because we're already
// checking if the node is assigned to the desired EP (e.g., CUDA EP). We should only need to check
// ENABLE_CUDA_NHWC_OPS.
#if (defined(USE_CUDA) || defined(USE_CUDA_PROVIDER_INTERFACE)) && ENABLE_CUDA_NHWC_OPS
if (node.GetExecutionProviderType() == kCudaExecutionProvider) {
if (layout_sensitive_ops.count(node.OpType())) {
const auto& cuda_nhwc_ops = GetCUDALayoutSensitiveOps();
if (!cuda_nhwc_ops.count(node.OpType())) {
return false;
}
}
}
#endif
// TODO: We don't really need EP pre-processor macros in this function because we're already checking if the
// node is assigned to the desired EP (e.g., QNN EP). There's nothing about this code that absolutely requires
// conditional compilation.
#if defined(USE_QNN) || defined(USE_QNN_PROVIDER_INTERFACE)
if (node.GetExecutionProviderType() == kQnnExecutionProvider) {
if (node.OpType() == "Upsample") {
// Upsample is translated to QNN's Resize, which requires the NHWC layout for processing.
return true;
}
}
#endif

this new interface provides a way for EPs to specify this originally hardcoded logic.

@edgchen1 edgchen1 marked this pull request as ready for review June 25, 2025 02:55
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

You can commit the suggested changes from lintrunner.

@edgchen1 edgchen1 changed the title Add IExecutionProvider::ShouldConvertNodeLayoutToNhwc() to allow EPs to customize layout sensitive ops Add ShouldConvertNodeLayoutToNhwc() API to allow EPs to customize layout sensitive ops Jun 26, 2025
@edgchen1 edgchen1 requested a review from skottmckay June 26, 2025 17:05
@edgchen1 edgchen1 changed the title Add ShouldConvertNodeLayoutToNhwc() API to allow EPs to customize layout sensitive ops Add ShouldConvertDataLayoutForOp() API to allow EPs to customize layout sensitive ops Jun 27, 2025
Copy link
Contributor

@skottmckay skottmckay left a comment

Choose a reason for hiding this comment

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

:shipit:

@edgchen1 edgchen1 merged commit 6346cdd into main Jun 30, 2025
88 of 90 checks passed
@edgchen1 edgchen1 deleted the edgchen1/ep_layout_sensitive_ops branch June 30, 2025 17:03
daijh pushed a commit to daijh/onnxruntime that referenced this pull request Jul 10, 2025
…yout sensitive ops (microsoft#25147)

### Description
<!-- Describe your changes. -->

Add `IExecutionProvider::ShouldConvertDataLayoutForOp()` to allow EPs to
customize layout sensitive ops. Move existing hardcoded EP-specific
logic out of layout transformer code.

Add `OrtEp::ShouldConvertDataLayoutForOp` to ABI EP API to allow similar
customization by plugin EPs.

### Motivation and Context
<!-- - Why is this change required? What problem does it solve?
- If it fixes an open issue, please link to the issue here. -->

Enable layout sensitive op customization through internal EP interface
and the ABI EP API.
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.

4 participants