Qualcomm AI Engine Direct - Support Debug Handle and Integrate IntermediateOutputCapturer - #16316

Closed
winskuo-quic wants to merge 3 commits into
pytorch:mainfrom
CodeLinaro:dev1/winskuo/debug_handle
Closed

Qualcomm AI Engine Direct - Support Debug Handle and Integrate IntermediateOutputCapturer#16316
winskuo-quic wants to merge 3 commits into
pytorch:mainfrom
CodeLinaro:dev1/winskuo/debug_handle

Conversation

@winskuo-quic

Copy link
Copy Markdown
Collaborator

Summary

  • AOT Debug Handle Enablement. Also supported debug_handle map, although debugger did not use it. Enable this because of community request.
  • Runtime Debug Handle Enablement: Parse debug handle in runtime and use debug_handle and tensors key when storing result into etdump.
  • Reuse ExecuTorch Debugger feature and reduce redundancy between QNN ExecuTorch Debugger and ExecuTorch Debugger Utils.

Additional Topics:

  • What is the official way of retrieving an edge module that does not carry backend info?

Test plan

  • E2E example script test
    • python backends/qualcomm/tests/test_qnn_delegate.py -k TestExampleUtilsScript.test_intermediate_debugger -s $DEVICE --model SM8650 --build_folder build-android/ --executorch_root . --image_dataset ../imagenet-mini/val/ --artifact ./e2e_test_debug
  • Simple model test
    • python backends/qualcomm/tests/test_qnn_delegate.py -k TestQNNQuantizedUtils.test_qnn_backend_dump_intermediate_outputs_simple_model --model SM8550 --device $DEVICE --build_folder build-android
    • python backends/qualcomm/tests/test_qnn_delegate.py -k TestQNNQuantizedUtils.test_qnn_backend_dump_intermediate_outputs_topk --model SM8550 --device $DEVICE --build_folder build-android

@pytorch-bot

pytorch-botBot commented Dec 18, 2025

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/16316

Note: Links to docs will display an error until the docs builds have been completed.

✅ You can merge normally! (1 Unrelated Failure)

As of commit cc92ab4 with merge base 8e8d97e (image):

UNSTABLE - The following job is marked as unstable, possibly due to flakiness on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-clameta-claBot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 18, 2025
@github-actions

Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@winskuo-quic
winskuo-quic marked this pull request as draft December 18, 2025 09:51
@Gasoonjia

Copy link
Copy Markdown
Contributor

Is the PR ready to be reviewed now?

Comment threadbackends/qualcomm/_passes/resolve_debug_handle.py
@winskuo-quic
winskuo-quic marked this pull request as ready for review December 19, 2025 01:36
@winskuo-quic
winskuo-quicforce-pushed the dev1/winskuo/debug_handle branch 2 times, most recently from 9a7ca59 to dc72614CompareDecember 19, 2025 01:45
@winskuo-quic

Copy link
Copy Markdown
CollaboratorAuthor

Is the PR ready to be reviewed now?

Hi @Gasoonjia,
I was rebasing previously, so I set it to draft. This PR should now be ready for review. Thanks.

@winskuo-quic

Copy link
Copy Markdown
CollaboratorAuthor

Hi @cccclai, @Gasoonjia, @kimishpatel,
I have supported the debug_handle in this PR, which has been mentioned in #5310 and #15735.
We will now be using the ExecuTorch's official Intermediate_Output_Capturer to capture CPU's intermediate result.

Also, I would also like to get some suggestions on the official API to retrieve an edge IR. The current way of retrieving an edge IR is through:

edge_module=lower_module.original_module.module()

However, I encountered following issues when retrieving edge IR using the above method.

  1. If there are partitions, I'll be getting a graph that fuses supported nodes to delegate node(s). However, it would be helpful for debugging if we could get the edge IR graph that does not fuse the backend supported nodes to delegate node(s).
  2. I noticed that by using the edge IR graph above, the input order might have changed. This can be easily reproduced by using model that has more than 1 input (e.g., Roberta). I would like to know how if there's any way I can get a graph that has the correct input order.

Thanks

@Gasoonjia

Copy link
Copy Markdown
Contributor

hi @winskuo-quic

I think instead of using the edge graph IR as the ground truth for comparsion, it will be great if we can use the export program ET stack get at the first place (e.g. the export graph of model variable

here), since it should be the source graph ET stack take and our job is making sure our intermediate output the same as the input graph as much as possible.

You can see how we calculate intermediate output numercal descrepancy:

defcalculate_numeric_gap(

https://github.com/pytorch/executorch/blob/0fb422f9c59e0e5526c0082352a583baf0510fb7/exir/passes/debug_handle_generator_pass.py here's pass for debug handle generation, where the debug handle of a node is the same as the node sharing the same greatest ancestor node in the export flow.

@Gasoonjia

Gasoonjia commented Dec 19, 2025

Copy link
Copy Markdown
Contributor

Here's an example of how our current API works on VIT model on xnnpack backend: https://gist.github.com/Gasoonjia/db6285ac39ad5759b95c7a92d37cd4f8

and below is the expected output. For some ops like layernorm there're still some issue i need to fix.

idxaot_opsaot_intermediate_outputruntime_opsruntime_intermediate_outputgap
0[conv2d][[[tensor(0.0253, -0.0287, -0.0042, 0.0118, …)]][DELEGATE_CALL][[[tensor(0.0253, -0.0287, -0.0042, 0.0118, …)]]3.2825094945114346e-15
1[permute, cat, add, dropout][[[tensor(-0.0024), tensor(0.0054), tensor(0.0…), …]][DELEGATE_CALL][[[tensor(-0.0024), tensor(0.0054), tensor(0.0…), …]]3.281230918554512e-15
2[expand][[[tensor(-0.0012), tensor(0.0027), tensor(0.0…), …]][native_call_expand_copy.out][[[tensor(-0.0012), tensor(0.0027), tensor(0.0…), …]]0.0
3[layer_norm][[[tensor(-0.0001), tensor(0.0009), tensor(-0.…), …]][native_call_native_layer_norm.out][[[tensor(31.1172)], [tensor(4.3549)], [tensor(…)…]]19.7299543374596
4[transpose, linear, unflatten, …, transpose][[[tensor(0.0027), tensor(-0.0032), tensor(0.0…), …]][DELEGATE_CALL, DELEGATE_CALL, DELEGATE_…][[[tensor(0.0027), tensor(-0.0032), tensor(0.0…), …]]9.381436078525961e-05
61[layer_norm_23][[[tensor(-0.8604), tensor(-0.1713), tensor(-0.…),…]][native_call_native_layer_norm.out][[[tensor(2.2180)], [tensor(1.8462)], [tensor(…)…]]2.8061147356332854
62[linear_46, gelu_11, dropout_35, …, dropout_36][[[tensor(-0.6561), tensor(-0.0496), tensor(-0.…),…]][DELEGATE_CALL][[[tensor(-0.6561), tensor(-0.0496), tensor(-0.…),…]]1.0872256686587983e-11
63[layer_norm_24][[[tensor(-0.9040), tensor(-0.1004), tensor(-0.…),…]][native_call_native_layer_norm.out][[[tensor(1.9138)], [tensor(1.9031)], [tensor(…)…]]3.104443617092582
64[select_36][[tensor(-0.9040), tensor(-0.1004), tensor(-0.…),…]][native_call_select_copy.int_out][[tensor(-0.9040), tensor(-0.1004), tensor(-0.…),…]]1.1178469901123618e-12
65[linear_48][[tensor(-0.9624), tensor(0.7285), tensor(0.79…),…]][DELEGATE_CALL][[tensor(-0.9624), tensor(0.7285), tensor(0.79…),…]]1.7864835786911282e-12

I would love to chat with you regarding how we can make the pipeline works on qualcomm backend!
Hope it can help you!

@winskuo-quic
winskuo-quicforce-pushed the dev1/winskuo/debug_handle branch from dc72614 to 00c4f7eCompareDecember 19, 2025 05:25
@winskuo-quic
winskuo-quic marked this pull request as draft December 19, 2025 05:26
@winskuo-quic

Copy link
Copy Markdown
CollaboratorAuthor

Hi @Gasoonjia,
I have turned this PR back to draft for now.
I would love to learn more about ExecuTorch's Debugger Framework and let's move this conversation to email first.
I would also love to chat with you to discuss more in details.

@GasoonjiaGasoonjia 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.

thx for the work!

Comment threadbackends/qualcomm/_passes/resolve_debug_handle.py Outdated
def call(self, graph_module: torch.fx.GraphModule):
handle_counter = 1
visited = set()
for node in graph_module.graph.nodes:

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.

not sure if Qualcomm can handle conditional graph. If so i think the way you are adding debug handle might not able to equip debug handle to all branches. You can follow what i'm doing here:
https://github.com/pytorch/executorch/blob/main/exir/passes/debug_handle_generator_pass.py#L14

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

I would like to confirm on definition of conditional graph. Initially, I thought we can't have conditional graph (e.g., if else) in Aten. The only way to do it is through operations such as where op, which still makes it a single graph. We actually use a lot of for loop for graph traversing in our passes. If you find this being a concern, please let me know and I'll take a look at all our other passes.

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.

conditiion graph refers to some branch like torch.cond. https://github.com/pytorch/executorch/blob/main/exir/tests/test_passes.py#L1275 is an example.
For conditional graph like torch.cond we will capture the graph for both branch.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Thanks for sharing the operation. I think we currently does not have this op. I think I can add a TODO or some checks for now and actually made the change if QNN supports this operation in future. This will probably require a bigger refactor since all our passes are now using for loops.

assert (
source_node.name in visited
), "Graph is not traversed in topological order, unexpected behavior."
node.meta[QCOM_DEBUG_HANDLE] = source_node.meta[QCOM_DEBUG_HANDLE]

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.

Curious why do we need to set the get_item node the same debug handle as the soruce node? Since it will introduce duplicate debug handle in the graph and im a little bit worried if it could cause any issue in the down stream,

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

I am actually just following the same behavior prior to this pass. For node's that has multi-output, it will have get_item node as users. I noticed that the debug_handle for all get_item node and node itself all have the same debug_handle, as shown below.
image

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.

I feel like the reason of get_item and its user sharing the same debug handle is because of exporting in strict mode and both aten_topk and its get_item function are from same source.
For here i don't have strong preference of whether or not we should make get_item's debug handle the same as source node, as long as it can support Qualcomm's requirement.

tensor_name = f"{node.name}_{wrapper_idx}"

# Only append special namings when enable tensor dump, since longer name results bigger .pte
if (handle_id := node.meta.get(QCOM_DEBUG_HANDLE)) and self.enable_tensor_dump:

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.

wondering if we still need this file since we will migrate to devtool infra?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

I believe this can be eventually deprecated as we discussed offline. However, since currently we don't have an official API to bring debug_handle to runtime, we will need to stick with parsing tensor name during runtime to extract debug_handle ID.

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.

oh will the tensor name here be part of runtime?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

During runtime, we will get tensor name like relu__debugID_1, where 1 here is the debug handle. We use regex in runtime to parse the ID 1 and save it to ETDUMP as the key.
I agree we will migrate to devtool infra, but due to some of the limitations where we don't really have an official flow to pass debug_handle ID related info to runtime, we need to keep it this way so the debugger can still function after this PR is merged.

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.

Thx for detailed explanation! Mind share me the ptr of how you retreive the op name during runtime?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Of course! Please refer to the message below.

Comment threadbackends/qualcomm/debugger/qnn_intermediate_debugger.py
@winskuo-quic
winskuo-quicforce-pushed the dev1/winskuo/debug_handle branch from 00c4f7e to 7ec377fCompareDecember 29, 2025 07:48
@winskuo-quic
winskuo-quic marked this pull request as ready for review December 29, 2025 07:48
std::string qnn_tensor_name =
std::string(QNN_TENSOR_VER_PTR(output_tensor)->name);
if (std::regex_search(qnn_tensor_name, match, re)) {
debug_handle_id = static_cast<uint32_t>(std::stoul(match[1].str()));

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Hi @Gasoonjia,
This is where we parse the qnn_tensor_name and get the debug_handle id.

@winskuo-quic
winskuo-quicforce-pushed the dev1/winskuo/debug_handle branch from 403300d to cc92ab4CompareJanuary 8, 2026 05:00
@github-actions

Copy link
Copy Markdown

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
If you want the bot to never mark this PR stale again, add the no-stale label.
Stale pull requests will automatically be closed after 30 days of inactivity.

@github-actionsgithub-actionsBot added the Stale PRs inactive for over 60 days label Mar 10, 2026
@Gasoonjia

Copy link
Copy Markdown
Contributor

hi @winskuo-quic any related PR recently need my support or review?

@winskuo-quic

Copy link
Copy Markdown
CollaboratorAuthor

hi @winskuo-quic any related PR recently need my support or review?

Hi @Gasoonjia,
It would be appreciated if you can have a look at this PR: #18834
It is the 2nd part of the PR, which is reusing all dev tools feature. After this PR is approved, I will have a 3rd PR for claude SKILL.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA SignedThis label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.StalePRs inactive for over 60 days

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@winskuo-quic@Gasoonjia
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Qualcomm AI Engine Direct - Support Debug Handle and Integrate IntermediateOutputCapturer - #16316

Closed
winskuo-quic wants to merge 3 commits into
pytorch:mainfrom
CodeLinaro:dev1/winskuo/debug_handle
Closed

Qualcomm AI Engine Direct - Support Debug Handle and Integrate IntermediateOutputCapturer#16316
winskuo-quic wants to merge 3 commits into
pytorch:mainfrom
CodeLinaro:dev1/winskuo/debug_handle

Conversation

@winskuo-quic

Copy link
Copy Markdown
Collaborator

Summary

  • AOT Debug Handle Enablement. Also supported debug_handle map, although debugger did not use it. Enable this because of community request.
  • Runtime Debug Handle Enablement: Parse debug handle in runtime and use debug_handle and tensors key when storing result into etdump.
  • Reuse ExecuTorch Debugger feature and reduce redundancy between QNN ExecuTorch Debugger and ExecuTorch Debugger Utils.

Additional Topics:

  • What is the official way of retrieving an edge module that does not carry backend info?

Test plan

  • E2E example script test
    • python backends/qualcomm/tests/test_qnn_delegate.py -k TestExampleUtilsScript.test_intermediate_debugger -s $DEVICE --model SM8650 --build_folder build-android/ --executorch_root . --image_dataset ../imagenet-mini/val/ --artifact ./e2e_test_debug
  • Simple model test
    • python backends/qualcomm/tests/test_qnn_delegate.py -k TestQNNQuantizedUtils.test_qnn_backend_dump_intermediate_outputs_simple_model --model SM8550 --device $DEVICE --build_folder build-android
    • python backends/qualcomm/tests/test_qnn_delegate.py -k TestQNNQuantizedUtils.test_qnn_backend_dump_intermediate_outputs_topk --model SM8550 --device $DEVICE --build_folder build-android

@pytorch-bot

pytorch-botBot commented Dec 18, 2025

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/16316

Note: Links to docs will display an error until the docs builds have been completed.

✅ You can merge normally! (1 Unrelated Failure)

As of commit cc92ab4 with merge base 8e8d97e (image):

UNSTABLE - The following job is marked as unstable, possibly due to flakiness on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-clameta-claBot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 18, 2025
@github-actions

Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@winskuo-quic
winskuo-quic marked this pull request as draft December 18, 2025 09:51
@Gasoonjia

Copy link
Copy Markdown
Contributor

Is the PR ready to be reviewed now?

Comment threadbackends/qualcomm/_passes/resolve_debug_handle.py
@winskuo-quic
winskuo-quic marked this pull request as ready for review December 19, 2025 01:36
@winskuo-quic
winskuo-quicforce-pushed the dev1/winskuo/debug_handle branch 2 times, most recently from 9a7ca59 to dc72614CompareDecember 19, 2025 01:45
@winskuo-quic

Copy link
Copy Markdown
CollaboratorAuthor

Is the PR ready to be reviewed now?

Hi @Gasoonjia,
I was rebasing previously, so I set it to draft. This PR should now be ready for review. Thanks.

@winskuo-quic

Copy link
Copy Markdown
CollaboratorAuthor

Hi @cccclai, @Gasoonjia, @kimishpatel,
I have supported the debug_handle in this PR, which has been mentioned in #5310 and #15735.
We will now be using the ExecuTorch's official Intermediate_Output_Capturer to capture CPU's intermediate result.

Also, I would also like to get some suggestions on the official API to retrieve an edge IR. The current way of retrieving an edge IR is through:

edge_module=lower_module.original_module.module()

However, I encountered following issues when retrieving edge IR using the above method.

  1. If there are partitions, I'll be getting a graph that fuses supported nodes to delegate node(s). However, it would be helpful for debugging if we could get the edge IR graph that does not fuse the backend supported nodes to delegate node(s).
  2. I noticed that by using the edge IR graph above, the input order might have changed. This can be easily reproduced by using model that has more than 1 input (e.g., Roberta). I would like to know how if there's any way I can get a graph that has the correct input order.

Thanks

@Gasoonjia

Copy link
Copy Markdown
Contributor

hi @winskuo-quic

I think instead of using the edge graph IR as the ground truth for comparsion, it will be great if we can use the export program ET stack get at the first place (e.g. the export graph of model variable

here), since it should be the source graph ET stack take and our job is making sure our intermediate output the same as the input graph as much as possible.

You can see how we calculate intermediate output numercal descrepancy:

defcalculate_numeric_gap(

https://github.com/pytorch/executorch/blob/0fb422f9c59e0e5526c0082352a583baf0510fb7/exir/passes/debug_handle_generator_pass.py here's pass for debug handle generation, where the debug handle of a node is the same as the node sharing the same greatest ancestor node in the export flow.

@Gasoonjia

Gasoonjia commented Dec 19, 2025

Copy link
Copy Markdown
Contributor

Here's an example of how our current API works on VIT model on xnnpack backend: https://gist.github.com/Gasoonjia/db6285ac39ad5759b95c7a92d37cd4f8

and below is the expected output. For some ops like layernorm there're still some issue i need to fix.

idxaot_opsaot_intermediate_outputruntime_opsruntime_intermediate_outputgap
0[conv2d][[[tensor(0.0253, -0.0287, -0.0042, 0.0118, …)]][DELEGATE_CALL][[[tensor(0.0253, -0.0287, -0.0042, 0.0118, …)]]3.2825094945114346e-15
1[permute, cat, add, dropout][[[tensor(-0.0024), tensor(0.0054), tensor(0.0…), …]][DELEGATE_CALL][[[tensor(-0.0024), tensor(0.0054), tensor(0.0…), …]]3.281230918554512e-15
2[expand][[[tensor(-0.0012), tensor(0.0027), tensor(0.0…), …]][native_call_expand_copy.out][[[tensor(-0.0012), tensor(0.0027), tensor(0.0…), …]]0.0
3[layer_norm][[[tensor(-0.0001), tensor(0.0009), tensor(-0.…), …]][native_call_native_layer_norm.out][[[tensor(31.1172)], [tensor(4.3549)], [tensor(…)…]]19.7299543374596
4[transpose, linear, unflatten, …, transpose][[[tensor(0.0027), tensor(-0.0032), tensor(0.0…), …]][DELEGATE_CALL, DELEGATE_CALL, DELEGATE_…][[[tensor(0.0027), tensor(-0.0032), tensor(0.0…), …]]9.381436078525961e-05
61[layer_norm_23][[[tensor(-0.8604), tensor(-0.1713), tensor(-0.…),…]][native_call_native_layer_norm.out][[[tensor(2.2180)], [tensor(1.8462)], [tensor(…)…]]2.8061147356332854
62[linear_46, gelu_11, dropout_35, …, dropout_36][[[tensor(-0.6561), tensor(-0.0496), tensor(-0.…),…]][DELEGATE_CALL][[[tensor(-0.6561), tensor(-0.0496), tensor(-0.…),…]]1.0872256686587983e-11
63[layer_norm_24][[[tensor(-0.9040), tensor(-0.1004), tensor(-0.…),…]][native_call_native_layer_norm.out][[[tensor(1.9138)], [tensor(1.9031)], [tensor(…)…]]3.104443617092582
64[select_36][[tensor(-0.9040), tensor(-0.1004), tensor(-0.…),…]][native_call_select_copy.int_out][[tensor(-0.9040), tensor(-0.1004), tensor(-0.…),…]]1.1178469901123618e-12
65[linear_48][[tensor(-0.9624), tensor(0.7285), tensor(0.79…),…]][DELEGATE_CALL][[tensor(-0.9624), tensor(0.7285), tensor(0.79…),…]]1.7864835786911282e-12

I would love to chat with you regarding how we can make the pipeline works on qualcomm backend!
Hope it can help you!

@winskuo-quic
winskuo-quicforce-pushed the dev1/winskuo/debug_handle branch from dc72614 to 00c4f7eCompareDecember 19, 2025 05:25
@winskuo-quic
winskuo-quic marked this pull request as draft December 19, 2025 05:26
@winskuo-quic

Copy link
Copy Markdown
CollaboratorAuthor

Hi @Gasoonjia,
I have turned this PR back to draft for now.
I would love to learn more about ExecuTorch's Debugger Framework and let's move this conversation to email first.
I would also love to chat with you to discuss more in details.

@GasoonjiaGasoonjia 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.

thx for the work!

Comment threadbackends/qualcomm/_passes/resolve_debug_handle.py Outdated
def call(self, graph_module: torch.fx.GraphModule):
handle_counter = 1
visited = set()
for node in graph_module.graph.nodes:

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.

not sure if Qualcomm can handle conditional graph. If so i think the way you are adding debug handle might not able to equip debug handle to all branches. You can follow what i'm doing here:
https://github.com/pytorch/executorch/blob/main/exir/passes/debug_handle_generator_pass.py#L14

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

I would like to confirm on definition of conditional graph. Initially, I thought we can't have conditional graph (e.g., if else) in Aten. The only way to do it is through operations such as where op, which still makes it a single graph. We actually use a lot of for loop for graph traversing in our passes. If you find this being a concern, please let me know and I'll take a look at all our other passes.

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.

conditiion graph refers to some branch like torch.cond. https://github.com/pytorch/executorch/blob/main/exir/tests/test_passes.py#L1275 is an example.
For conditional graph like torch.cond we will capture the graph for both branch.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Thanks for sharing the operation. I think we currently does not have this op. I think I can add a TODO or some checks for now and actually made the change if QNN supports this operation in future. This will probably require a bigger refactor since all our passes are now using for loops.

assert (
source_node.name in visited
), "Graph is not traversed in topological order, unexpected behavior."
node.meta[QCOM_DEBUG_HANDLE] = source_node.meta[QCOM_DEBUG_HANDLE]

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.

Curious why do we need to set the get_item node the same debug handle as the soruce node? Since it will introduce duplicate debug handle in the graph and im a little bit worried if it could cause any issue in the down stream,

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

I am actually just following the same behavior prior to this pass. For node's that has multi-output, it will have get_item node as users. I noticed that the debug_handle for all get_item node and node itself all have the same debug_handle, as shown below.
image

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.

I feel like the reason of get_item and its user sharing the same debug handle is because of exporting in strict mode and both aten_topk and its get_item function are from same source.
For here i don't have strong preference of whether or not we should make get_item's debug handle the same as source node, as long as it can support Qualcomm's requirement.

tensor_name = f"{node.name}_{wrapper_idx}"

# Only append special namings when enable tensor dump, since longer name results bigger .pte
if (handle_id := node.meta.get(QCOM_DEBUG_HANDLE)) and self.enable_tensor_dump:

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.

wondering if we still need this file since we will migrate to devtool infra?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

I believe this can be eventually deprecated as we discussed offline. However, since currently we don't have an official API to bring debug_handle to runtime, we will need to stick with parsing tensor name during runtime to extract debug_handle ID.

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.

oh will the tensor name here be part of runtime?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

During runtime, we will get tensor name like relu__debugID_1, where 1 here is the debug handle. We use regex in runtime to parse the ID 1 and save it to ETDUMP as the key.
I agree we will migrate to devtool infra, but due to some of the limitations where we don't really have an official flow to pass debug_handle ID related info to runtime, we need to keep it this way so the debugger can still function after this PR is merged.

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.

Thx for detailed explanation! Mind share me the ptr of how you retreive the op name during runtime?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Of course! Please refer to the message below.

Comment threadbackends/qualcomm/debugger/qnn_intermediate_debugger.py
@winskuo-quic
winskuo-quicforce-pushed the dev1/winskuo/debug_handle branch from 00c4f7e to 7ec377fCompareDecember 29, 2025 07:48
@winskuo-quic
winskuo-quic marked this pull request as ready for review December 29, 2025 07:48
std::string qnn_tensor_name =
std::string(QNN_TENSOR_VER_PTR(output_tensor)->name);
if (std::regex_search(qnn_tensor_name, match, re)) {
debug_handle_id = static_cast<uint32_t>(std::stoul(match[1].str()));

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Hi @Gasoonjia,
This is where we parse the qnn_tensor_name and get the debug_handle id.

@winskuo-quic
winskuo-quicforce-pushed the dev1/winskuo/debug_handle branch from 403300d to cc92ab4CompareJanuary 8, 2026 05:00
@github-actions

Copy link
Copy Markdown

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
If you want the bot to never mark this PR stale again, add the no-stale label.
Stale pull requests will automatically be closed after 30 days of inactivity.

@github-actionsgithub-actionsBot added the Stale PRs inactive for over 60 days label Mar 10, 2026
@Gasoonjia

Copy link
Copy Markdown
Contributor

hi @winskuo-quic any related PR recently need my support or review?

@winskuo-quic

Copy link
Copy Markdown
CollaboratorAuthor

hi @winskuo-quic any related PR recently need my support or review?

Hi @Gasoonjia,
It would be appreciated if you can have a look at this PR: #18834
It is the 2nd part of the PR, which is reusing all dev tools feature. After this PR is approved, I will have a 3rd PR for claude SKILL.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA SignedThis label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.StalePRs inactive for over 60 days

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@winskuo-quic@Gasoonjia
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Qualcomm AI Engine Direct - Support Debug Handle and Integrate IntermediateOutputCapturer - #16316

Closed
winskuo-quic wants to merge 3 commits into
pytorch:mainfrom
CodeLinaro:dev1/winskuo/debug_handle
Closed

Qualcomm AI Engine Direct - Support Debug Handle and Integrate IntermediateOutputCapturer#16316
winskuo-quic wants to merge 3 commits into
pytorch:mainfrom
CodeLinaro:dev1/winskuo/debug_handle

Conversation

@winskuo-quic

Copy link
Copy Markdown
Collaborator

Summary

  • AOT Debug Handle Enablement. Also supported debug_handle map, although debugger did not use it. Enable this because of community request.
  • Runtime Debug Handle Enablement: Parse debug handle in runtime and use debug_handle and tensors key when storing result into etdump.
  • Reuse ExecuTorch Debugger feature and reduce redundancy between QNN ExecuTorch Debugger and ExecuTorch Debugger Utils.

Additional Topics:

  • What is the official way of retrieving an edge module that does not carry backend info?

Test plan

  • E2E example script test
    • python backends/qualcomm/tests/test_qnn_delegate.py -k TestExampleUtilsScript.test_intermediate_debugger -s $DEVICE --model SM8650 --build_folder build-android/ --executorch_root . --image_dataset ../imagenet-mini/val/ --artifact ./e2e_test_debug
  • Simple model test
    • python backends/qualcomm/tests/test_qnn_delegate.py -k TestQNNQuantizedUtils.test_qnn_backend_dump_intermediate_outputs_simple_model --model SM8550 --device $DEVICE --build_folder build-android
    • python backends/qualcomm/tests/test_qnn_delegate.py -k TestQNNQuantizedUtils.test_qnn_backend_dump_intermediate_outputs_topk --model SM8550 --device $DEVICE --build_folder build-android

@pytorch-bot

pytorch-botBot commented Dec 18, 2025

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/16316

Note: Links to docs will display an error until the docs builds have been completed.

✅ You can merge normally! (1 Unrelated Failure)

As of commit cc92ab4 with merge base 8e8d97e (image):

UNSTABLE - The following job is marked as unstable, possibly due to flakiness on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-clameta-claBot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 18, 2025
@github-actions

Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@winskuo-quic
winskuo-quic marked this pull request as draft December 18, 2025 09:51
@Gasoonjia

Copy link
Copy Markdown
Contributor

Is the PR ready to be reviewed now?

Comment threadbackends/qualcomm/_passes/resolve_debug_handle.py
@winskuo-quic
winskuo-quic marked this pull request as ready for review December 19, 2025 01:36
@winskuo-quic
winskuo-quicforce-pushed the dev1/winskuo/debug_handle branch 2 times, most recently from 9a7ca59 to dc72614CompareDecember 19, 2025 01:45
@winskuo-quic

Copy link
Copy Markdown
CollaboratorAuthor

Is the PR ready to be reviewed now?

Hi @Gasoonjia,
I was rebasing previously, so I set it to draft. This PR should now be ready for review. Thanks.

@winskuo-quic

Copy link
Copy Markdown
CollaboratorAuthor

Hi @cccclai, @Gasoonjia, @kimishpatel,
I have supported the debug_handle in this PR, which has been mentioned in #5310 and #15735.
We will now be using the ExecuTorch's official Intermediate_Output_Capturer to capture CPU's intermediate result.

Also, I would also like to get some suggestions on the official API to retrieve an edge IR. The current way of retrieving an edge IR is through:

edge_module=lower_module.original_module.module()

However, I encountered following issues when retrieving edge IR using the above method.

  1. If there are partitions, I'll be getting a graph that fuses supported nodes to delegate node(s). However, it would be helpful for debugging if we could get the edge IR graph that does not fuse the backend supported nodes to delegate node(s).
  2. I noticed that by using the edge IR graph above, the input order might have changed. This can be easily reproduced by using model that has more than 1 input (e.g., Roberta). I would like to know how if there's any way I can get a graph that has the correct input order.

Thanks

@Gasoonjia

Copy link
Copy Markdown
Contributor

hi @winskuo-quic

I think instead of using the edge graph IR as the ground truth for comparsion, it will be great if we can use the export program ET stack get at the first place (e.g. the export graph of model variable

here), since it should be the source graph ET stack take and our job is making sure our intermediate output the same as the input graph as much as possible.

You can see how we calculate intermediate output numercal descrepancy:

defcalculate_numeric_gap(

https://github.com/pytorch/executorch/blob/0fb422f9c59e0e5526c0082352a583baf0510fb7/exir/passes/debug_handle_generator_pass.py here's pass for debug handle generation, where the debug handle of a node is the same as the node sharing the same greatest ancestor node in the export flow.

@Gasoonjia

Gasoonjia commented Dec 19, 2025

Copy link
Copy Markdown
Contributor

Here's an example of how our current API works on VIT model on xnnpack backend: https://gist.github.com/Gasoonjia/db6285ac39ad5759b95c7a92d37cd4f8

and below is the expected output. For some ops like layernorm there're still some issue i need to fix.

idxaot_opsaot_intermediate_outputruntime_opsruntime_intermediate_outputgap
0[conv2d][[[tensor(0.0253, -0.0287, -0.0042, 0.0118, …)]][DELEGATE_CALL][[[tensor(0.0253, -0.0287, -0.0042, 0.0118, …)]]3.2825094945114346e-15
1[permute, cat, add, dropout][[[tensor(-0.0024), tensor(0.0054), tensor(0.0…), …]][DELEGATE_CALL][[[tensor(-0.0024), tensor(0.0054), tensor(0.0…), …]]3.281230918554512e-15
2[expand][[[tensor(-0.0012), tensor(0.0027), tensor(0.0…), …]][native_call_expand_copy.out][[[tensor(-0.0012), tensor(0.0027), tensor(0.0…), …]]0.0
3[layer_norm][[[tensor(-0.0001), tensor(0.0009), tensor(-0.…), …]][native_call_native_layer_norm.out][[[tensor(31.1172)], [tensor(4.3549)], [tensor(…)…]]19.7299543374596
4[transpose, linear, unflatten, …, transpose][[[tensor(0.0027), tensor(-0.0032), tensor(0.0…), …]][DELEGATE_CALL, DELEGATE_CALL, DELEGATE_…][[[tensor(0.0027), tensor(-0.0032), tensor(0.0…), …]]9.381436078525961e-05
61[layer_norm_23][[[tensor(-0.8604), tensor(-0.1713), tensor(-0.…),…]][native_call_native_layer_norm.out][[[tensor(2.2180)], [tensor(1.8462)], [tensor(…)…]]2.8061147356332854
62[linear_46, gelu_11, dropout_35, …, dropout_36][[[tensor(-0.6561), tensor(-0.0496), tensor(-0.…),…]][DELEGATE_CALL][[[tensor(-0.6561), tensor(-0.0496), tensor(-0.…),…]]1.0872256686587983e-11
63[layer_norm_24][[[tensor(-0.9040), tensor(-0.1004), tensor(-0.…),…]][native_call_native_layer_norm.out][[[tensor(1.9138)], [tensor(1.9031)], [tensor(…)…]]3.104443617092582
64[select_36][[tensor(-0.9040), tensor(-0.1004), tensor(-0.…),…]][native_call_select_copy.int_out][[tensor(-0.9040), tensor(-0.1004), tensor(-0.…),…]]1.1178469901123618e-12
65[linear_48][[tensor(-0.9624), tensor(0.7285), tensor(0.79…),…]][DELEGATE_CALL][[tensor(-0.9624), tensor(0.7285), tensor(0.79…),…]]1.7864835786911282e-12

I would love to chat with you regarding how we can make the pipeline works on qualcomm backend!
Hope it can help you!

@winskuo-quic
winskuo-quicforce-pushed the dev1/winskuo/debug_handle branch from dc72614 to 00c4f7eCompareDecember 19, 2025 05:25
@winskuo-quic
winskuo-quic marked this pull request as draft December 19, 2025 05:26
@winskuo-quic

Copy link
Copy Markdown
CollaboratorAuthor

Hi @Gasoonjia,
I have turned this PR back to draft for now.
I would love to learn more about ExecuTorch's Debugger Framework and let's move this conversation to email first.
I would also love to chat with you to discuss more in details.

@GasoonjiaGasoonjia 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.

thx for the work!

Comment threadbackends/qualcomm/_passes/resolve_debug_handle.py Outdated
def call(self, graph_module: torch.fx.GraphModule):
handle_counter = 1
visited = set()
for node in graph_module.graph.nodes:

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.

not sure if Qualcomm can handle conditional graph. If so i think the way you are adding debug handle might not able to equip debug handle to all branches. You can follow what i'm doing here:
https://github.com/pytorch/executorch/blob/main/exir/passes/debug_handle_generator_pass.py#L14

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

I would like to confirm on definition of conditional graph. Initially, I thought we can't have conditional graph (e.g., if else) in Aten. The only way to do it is through operations such as where op, which still makes it a single graph. We actually use a lot of for loop for graph traversing in our passes. If you find this being a concern, please let me know and I'll take a look at all our other passes.

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.

conditiion graph refers to some branch like torch.cond. https://github.com/pytorch/executorch/blob/main/exir/tests/test_passes.py#L1275 is an example.
For conditional graph like torch.cond we will capture the graph for both branch.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Thanks for sharing the operation. I think we currently does not have this op. I think I can add a TODO or some checks for now and actually made the change if QNN supports this operation in future. This will probably require a bigger refactor since all our passes are now using for loops.

assert (
source_node.name in visited
), "Graph is not traversed in topological order, unexpected behavior."
node.meta[QCOM_DEBUG_HANDLE] = source_node.meta[QCOM_DEBUG_HANDLE]

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.

Curious why do we need to set the get_item node the same debug handle as the soruce node? Since it will introduce duplicate debug handle in the graph and im a little bit worried if it could cause any issue in the down stream,

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

I am actually just following the same behavior prior to this pass. For node's that has multi-output, it will have get_item node as users. I noticed that the debug_handle for all get_item node and node itself all have the same debug_handle, as shown below.
image

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.

I feel like the reason of get_item and its user sharing the same debug handle is because of exporting in strict mode and both aten_topk and its get_item function are from same source.
For here i don't have strong preference of whether or not we should make get_item's debug handle the same as source node, as long as it can support Qualcomm's requirement.

tensor_name = f"{node.name}_{wrapper_idx}"

# Only append special namings when enable tensor dump, since longer name results bigger .pte
if (handle_id := node.meta.get(QCOM_DEBUG_HANDLE)) and self.enable_tensor_dump:

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.

wondering if we still need this file since we will migrate to devtool infra?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

I believe this can be eventually deprecated as we discussed offline. However, since currently we don't have an official API to bring debug_handle to runtime, we will need to stick with parsing tensor name during runtime to extract debug_handle ID.

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.

oh will the tensor name here be part of runtime?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

During runtime, we will get tensor name like relu__debugID_1, where 1 here is the debug handle. We use regex in runtime to parse the ID 1 and save it to ETDUMP as the key.
I agree we will migrate to devtool infra, but due to some of the limitations where we don't really have an official flow to pass debug_handle ID related info to runtime, we need to keep it this way so the debugger can still function after this PR is merged.

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.

Thx for detailed explanation! Mind share me the ptr of how you retreive the op name during runtime?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Of course! Please refer to the message below.

Comment threadbackends/qualcomm/debugger/qnn_intermediate_debugger.py
@winskuo-quic
winskuo-quicforce-pushed the dev1/winskuo/debug_handle branch from 00c4f7e to 7ec377fCompareDecember 29, 2025 07:48
@winskuo-quic
winskuo-quic marked this pull request as ready for review December 29, 2025 07:48
std::string qnn_tensor_name =
std::string(QNN_TENSOR_VER_PTR(output_tensor)->name);
if (std::regex_search(qnn_tensor_name, match, re)) {
debug_handle_id = static_cast<uint32_t>(std::stoul(match[1].str()));

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Hi @Gasoonjia,
This is where we parse the qnn_tensor_name and get the debug_handle id.

@winskuo-quic
winskuo-quicforce-pushed the dev1/winskuo/debug_handle branch from 403300d to cc92ab4CompareJanuary 8, 2026 05:00
@github-actions

Copy link
Copy Markdown

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
If you want the bot to never mark this PR stale again, add the no-stale label.
Stale pull requests will automatically be closed after 30 days of inactivity.

@github-actionsgithub-actionsBot added the Stale PRs inactive for over 60 days label Mar 10, 2026
@Gasoonjia

Copy link
Copy Markdown
Contributor

hi @winskuo-quic any related PR recently need my support or review?

@winskuo-quic

Copy link
Copy Markdown
CollaboratorAuthor

hi @winskuo-quic any related PR recently need my support or review?

Hi @Gasoonjia,
It would be appreciated if you can have a look at this PR: #18834
It is the 2nd part of the PR, which is reusing all dev tools feature. After this PR is approved, I will have a 3rd PR for claude SKILL.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA SignedThis label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.StalePRs inactive for over 60 days

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@winskuo-quic@Gasoonjia
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Qualcomm AI Engine Direct - Support Debug Handle and Integrate IntermediateOutputCapturer - #16316

Closed
winskuo-quic wants to merge 3 commits into
pytorch:mainfrom
CodeLinaro:dev1/winskuo/debug_handle
Closed

Qualcomm AI Engine Direct - Support Debug Handle and Integrate IntermediateOutputCapturer#16316
winskuo-quic wants to merge 3 commits into
pytorch:mainfrom
CodeLinaro:dev1/winskuo/debug_handle

Conversation

@winskuo-quic

Copy link
Copy Markdown
Collaborator

Summary

  • AOT Debug Handle Enablement. Also supported debug_handle map, although debugger did not use it. Enable this because of community request.
  • Runtime Debug Handle Enablement: Parse debug handle in runtime and use debug_handle and tensors key when storing result into etdump.
  • Reuse ExecuTorch Debugger feature and reduce redundancy between QNN ExecuTorch Debugger and ExecuTorch Debugger Utils.

Additional Topics:

  • What is the official way of retrieving an edge module that does not carry backend info?

Test plan

  • E2E example script test
    • python backends/qualcomm/tests/test_qnn_delegate.py -k TestExampleUtilsScript.test_intermediate_debugger -s $DEVICE --model SM8650 --build_folder build-android/ --executorch_root . --image_dataset ../imagenet-mini/val/ --artifact ./e2e_test_debug
  • Simple model test
    • python backends/qualcomm/tests/test_qnn_delegate.py -k TestQNNQuantizedUtils.test_qnn_backend_dump_intermediate_outputs_simple_model --model SM8550 --device $DEVICE --build_folder build-android
    • python backends/qualcomm/tests/test_qnn_delegate.py -k TestQNNQuantizedUtils.test_qnn_backend_dump_intermediate_outputs_topk --model SM8550 --device $DEVICE --build_folder build-android

@pytorch-bot

pytorch-botBot commented Dec 18, 2025

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/16316

Note: Links to docs will display an error until the docs builds have been completed.

✅ You can merge normally! (1 Unrelated Failure)

As of commit cc92ab4 with merge base 8e8d97e (image):

UNSTABLE - The following job is marked as unstable, possibly due to flakiness on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-clameta-claBot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 18, 2025
@github-actions

Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@winskuo-quic
winskuo-quic marked this pull request as draft December 18, 2025 09:51
@Gasoonjia

Copy link
Copy Markdown
Contributor

Is the PR ready to be reviewed now?

Comment threadbackends/qualcomm/_passes/resolve_debug_handle.py
@winskuo-quic
winskuo-quic marked this pull request as ready for review December 19, 2025 01:36
@winskuo-quic
winskuo-quicforce-pushed the dev1/winskuo/debug_handle branch 2 times, most recently from 9a7ca59 to dc72614CompareDecember 19, 2025 01:45
@winskuo-quic

Copy link
Copy Markdown
CollaboratorAuthor

Is the PR ready to be reviewed now?

Hi @Gasoonjia,
I was rebasing previously, so I set it to draft. This PR should now be ready for review. Thanks.

@winskuo-quic

Copy link
Copy Markdown
CollaboratorAuthor

Hi @cccclai, @Gasoonjia, @kimishpatel,
I have supported the debug_handle in this PR, which has been mentioned in #5310 and #15735.
We will now be using the ExecuTorch's official Intermediate_Output_Capturer to capture CPU's intermediate result.

Also, I would also like to get some suggestions on the official API to retrieve an edge IR. The current way of retrieving an edge IR is through:

edge_module=lower_module.original_module.module()

However, I encountered following issues when retrieving edge IR using the above method.

  1. If there are partitions, I'll be getting a graph that fuses supported nodes to delegate node(s). However, it would be helpful for debugging if we could get the edge IR graph that does not fuse the backend supported nodes to delegate node(s).
  2. I noticed that by using the edge IR graph above, the input order might have changed. This can be easily reproduced by using model that has more than 1 input (e.g., Roberta). I would like to know how if there's any way I can get a graph that has the correct input order.

Thanks

@Gasoonjia

Copy link
Copy Markdown
Contributor

hi @winskuo-quic

I think instead of using the edge graph IR as the ground truth for comparsion, it will be great if we can use the export program ET stack get at the first place (e.g. the export graph of model variable

here), since it should be the source graph ET stack take and our job is making sure our intermediate output the same as the input graph as much as possible.

You can see how we calculate intermediate output numercal descrepancy:

defcalculate_numeric_gap(

https://github.com/pytorch/executorch/blob/0fb422f9c59e0e5526c0082352a583baf0510fb7/exir/passes/debug_handle_generator_pass.py here's pass for debug handle generation, where the debug handle of a node is the same as the node sharing the same greatest ancestor node in the export flow.

@Gasoonjia

Gasoonjia commented Dec 19, 2025

Copy link
Copy Markdown
Contributor

Here's an example of how our current API works on VIT model on xnnpack backend: https://gist.github.com/Gasoonjia/db6285ac39ad5759b95c7a92d37cd4f8

and below is the expected output. For some ops like layernorm there're still some issue i need to fix.

idxaot_opsaot_intermediate_outputruntime_opsruntime_intermediate_outputgap
0[conv2d][[[tensor(0.0253, -0.0287, -0.0042, 0.0118, …)]][DELEGATE_CALL][[[tensor(0.0253, -0.0287, -0.0042, 0.0118, …)]]3.2825094945114346e-15
1[permute, cat, add, dropout][[[tensor(-0.0024), tensor(0.0054), tensor(0.0…), …]][DELEGATE_CALL][[[tensor(-0.0024), tensor(0.0054), tensor(0.0…), …]]3.281230918554512e-15
2[expand][[[tensor(-0.0012), tensor(0.0027), tensor(0.0…), …]][native_call_expand_copy.out][[[tensor(-0.0012), tensor(0.0027), tensor(0.0…), …]]0.0
3[layer_norm][[[tensor(-0.0001), tensor(0.0009), tensor(-0.…), …]][native_call_native_layer_norm.out][[[tensor(31.1172)], [tensor(4.3549)], [tensor(…)…]]19.7299543374596
4[transpose, linear, unflatten, …, transpose][[[tensor(0.0027), tensor(-0.0032), tensor(0.0…), …]][DELEGATE_CALL, DELEGATE_CALL, DELEGATE_…][[[tensor(0.0027), tensor(-0.0032), tensor(0.0…), …]]9.381436078525961e-05
61[layer_norm_23][[[tensor(-0.8604), tensor(-0.1713), tensor(-0.…),…]][native_call_native_layer_norm.out][[[tensor(2.2180)], [tensor(1.8462)], [tensor(…)…]]2.8061147356332854
62[linear_46, gelu_11, dropout_35, …, dropout_36][[[tensor(-0.6561), tensor(-0.0496), tensor(-0.…),…]][DELEGATE_CALL][[[tensor(-0.6561), tensor(-0.0496), tensor(-0.…),…]]1.0872256686587983e-11
63[layer_norm_24][[[tensor(-0.9040), tensor(-0.1004), tensor(-0.…),…]][native_call_native_layer_norm.out][[[tensor(1.9138)], [tensor(1.9031)], [tensor(…)…]]3.104443617092582
64[select_36][[tensor(-0.9040), tensor(-0.1004), tensor(-0.…),…]][native_call_select_copy.int_out][[tensor(-0.9040), tensor(-0.1004), tensor(-0.…),…]]1.1178469901123618e-12
65[linear_48][[tensor(-0.9624), tensor(0.7285), tensor(0.79…),…]][DELEGATE_CALL][[tensor(-0.9624), tensor(0.7285), tensor(0.79…),…]]1.7864835786911282e-12

I would love to chat with you regarding how we can make the pipeline works on qualcomm backend!
Hope it can help you!

@winskuo-quic
winskuo-quicforce-pushed the dev1/winskuo/debug_handle branch from dc72614 to 00c4f7eCompareDecember 19, 2025 05:25
@winskuo-quic
winskuo-quic marked this pull request as draft December 19, 2025 05:26
@winskuo-quic

Copy link
Copy Markdown
CollaboratorAuthor

Hi @Gasoonjia,
I have turned this PR back to draft for now.
I would love to learn more about ExecuTorch's Debugger Framework and let's move this conversation to email first.
I would also love to chat with you to discuss more in details.

@GasoonjiaGasoonjia 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.

thx for the work!

Comment threadbackends/qualcomm/_passes/resolve_debug_handle.py Outdated
def call(self, graph_module: torch.fx.GraphModule):
handle_counter = 1
visited = set()
for node in graph_module.graph.nodes:

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.

not sure if Qualcomm can handle conditional graph. If so i think the way you are adding debug handle might not able to equip debug handle to all branches. You can follow what i'm doing here:
https://github.com/pytorch/executorch/blob/main/exir/passes/debug_handle_generator_pass.py#L14

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

I would like to confirm on definition of conditional graph. Initially, I thought we can't have conditional graph (e.g., if else) in Aten. The only way to do it is through operations such as where op, which still makes it a single graph. We actually use a lot of for loop for graph traversing in our passes. If you find this being a concern, please let me know and I'll take a look at all our other passes.

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.

conditiion graph refers to some branch like torch.cond. https://github.com/pytorch/executorch/blob/main/exir/tests/test_passes.py#L1275 is an example.
For conditional graph like torch.cond we will capture the graph for both branch.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Thanks for sharing the operation. I think we currently does not have this op. I think I can add a TODO or some checks for now and actually made the change if QNN supports this operation in future. This will probably require a bigger refactor since all our passes are now using for loops.

assert (
source_node.name in visited
), "Graph is not traversed in topological order, unexpected behavior."
node.meta[QCOM_DEBUG_HANDLE] = source_node.meta[QCOM_DEBUG_HANDLE]

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.

Curious why do we need to set the get_item node the same debug handle as the soruce node? Since it will introduce duplicate debug handle in the graph and im a little bit worried if it could cause any issue in the down stream,

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

I am actually just following the same behavior prior to this pass. For node's that has multi-output, it will have get_item node as users. I noticed that the debug_handle for all get_item node and node itself all have the same debug_handle, as shown below.
image

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.

I feel like the reason of get_item and its user sharing the same debug handle is because of exporting in strict mode and both aten_topk and its get_item function are from same source.
For here i don't have strong preference of whether or not we should make get_item's debug handle the same as source node, as long as it can support Qualcomm's requirement.

tensor_name = f"{node.name}_{wrapper_idx}"

# Only append special namings when enable tensor dump, since longer name results bigger .pte
if (handle_id := node.meta.get(QCOM_DEBUG_HANDLE)) and self.enable_tensor_dump:

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.

wondering if we still need this file since we will migrate to devtool infra?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

I believe this can be eventually deprecated as we discussed offline. However, since currently we don't have an official API to bring debug_handle to runtime, we will need to stick with parsing tensor name during runtime to extract debug_handle ID.

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.

oh will the tensor name here be part of runtime?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

During runtime, we will get tensor name like relu__debugID_1, where 1 here is the debug handle. We use regex in runtime to parse the ID 1 and save it to ETDUMP as the key.
I agree we will migrate to devtool infra, but due to some of the limitations where we don't really have an official flow to pass debug_handle ID related info to runtime, we need to keep it this way so the debugger can still function after this PR is merged.

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.

Thx for detailed explanation! Mind share me the ptr of how you retreive the op name during runtime?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Of course! Please refer to the message below.

Comment threadbackends/qualcomm/debugger/qnn_intermediate_debugger.py
@winskuo-quic
winskuo-quicforce-pushed the dev1/winskuo/debug_handle branch from 00c4f7e to 7ec377fCompareDecember 29, 2025 07:48
@winskuo-quic
winskuo-quic marked this pull request as ready for review December 29, 2025 07:48
std::string qnn_tensor_name =
std::string(QNN_TENSOR_VER_PTR(output_tensor)->name);
if (std::regex_search(qnn_tensor_name, match, re)) {
debug_handle_id = static_cast<uint32_t>(std::stoul(match[1].str()));

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Hi @Gasoonjia,
This is where we parse the qnn_tensor_name and get the debug_handle id.

@winskuo-quic
winskuo-quicforce-pushed the dev1/winskuo/debug_handle branch from 403300d to cc92ab4CompareJanuary 8, 2026 05:00
@github-actions

Copy link
Copy Markdown

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
If you want the bot to never mark this PR stale again, add the no-stale label.
Stale pull requests will automatically be closed after 30 days of inactivity.

@github-actionsgithub-actionsBot added the Stale PRs inactive for over 60 days label Mar 10, 2026
@Gasoonjia

Copy link
Copy Markdown
Contributor

hi @winskuo-quic any related PR recently need my support or review?

@winskuo-quic

Copy link
Copy Markdown
CollaboratorAuthor

hi @winskuo-quic any related PR recently need my support or review?

Hi @Gasoonjia,
It would be appreciated if you can have a look at this PR: #18834
It is the 2nd part of the PR, which is reusing all dev tools feature. After this PR is approved, I will have a 3rd PR for claude SKILL.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA SignedThis label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.StalePRs inactive for over 60 days

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@winskuo-quic@Gasoonjia
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Qualcomm AI Engine Direct - Support Debug Handle and Integrate IntermediateOutputCapturer - #16316

Closed
winskuo-quic wants to merge 3 commits into
pytorch:mainfrom
CodeLinaro:dev1/winskuo/debug_handle
Closed

Qualcomm AI Engine Direct - Support Debug Handle and Integrate IntermediateOutputCapturer#16316
winskuo-quic wants to merge 3 commits into
pytorch:mainfrom
CodeLinaro:dev1/winskuo/debug_handle

Conversation

@winskuo-quic

Copy link
Copy Markdown
Collaborator

Summary

  • AOT Debug Handle Enablement. Also supported debug_handle map, although debugger did not use it. Enable this because of community request.
  • Runtime Debug Handle Enablement: Parse debug handle in runtime and use debug_handle and tensors key when storing result into etdump.
  • Reuse ExecuTorch Debugger feature and reduce redundancy between QNN ExecuTorch Debugger and ExecuTorch Debugger Utils.

Additional Topics:

  • What is the official way of retrieving an edge module that does not carry backend info?

Test plan

  • E2E example script test
    • python backends/qualcomm/tests/test_qnn_delegate.py -k TestExampleUtilsScript.test_intermediate_debugger -s $DEVICE --model SM8650 --build_folder build-android/ --executorch_root . --image_dataset ../imagenet-mini/val/ --artifact ./e2e_test_debug
  • Simple model test
    • python backends/qualcomm/tests/test_qnn_delegate.py -k TestQNNQuantizedUtils.test_qnn_backend_dump_intermediate_outputs_simple_model --model SM8550 --device $DEVICE --build_folder build-android
    • python backends/qualcomm/tests/test_qnn_delegate.py -k TestQNNQuantizedUtils.test_qnn_backend_dump_intermediate_outputs_topk --model SM8550 --device $DEVICE --build_folder build-android

@pytorch-bot

pytorch-botBot commented Dec 18, 2025

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/16316

Note: Links to docs will display an error until the docs builds have been completed.

✅ You can merge normally! (1 Unrelated Failure)

As of commit cc92ab4 with merge base 8e8d97e (image):

UNSTABLE - The following job is marked as unstable, possibly due to flakiness on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-clameta-claBot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 18, 2025
@github-actions

Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@winskuo-quic
winskuo-quic marked this pull request as draft December 18, 2025 09:51
@Gasoonjia

Copy link
Copy Markdown
Contributor

Is the PR ready to be reviewed now?

Comment threadbackends/qualcomm/_passes/resolve_debug_handle.py
@winskuo-quic
winskuo-quic marked this pull request as ready for review December 19, 2025 01:36
@winskuo-quic
winskuo-quicforce-pushed the dev1/winskuo/debug_handle branch 2 times, most recently from 9a7ca59 to dc72614CompareDecember 19, 2025 01:45
@winskuo-quic

Copy link
Copy Markdown
CollaboratorAuthor

Is the PR ready to be reviewed now?

Hi @Gasoonjia,
I was rebasing previously, so I set it to draft. This PR should now be ready for review. Thanks.

@winskuo-quic

Copy link
Copy Markdown
CollaboratorAuthor

Hi @cccclai, @Gasoonjia, @kimishpatel,
I have supported the debug_handle in this PR, which has been mentioned in #5310 and #15735.
We will now be using the ExecuTorch's official Intermediate_Output_Capturer to capture CPU's intermediate result.

Also, I would also like to get some suggestions on the official API to retrieve an edge IR. The current way of retrieving an edge IR is through:

edge_module=lower_module.original_module.module()

However, I encountered following issues when retrieving edge IR using the above method.

  1. If there are partitions, I'll be getting a graph that fuses supported nodes to delegate node(s). However, it would be helpful for debugging if we could get the edge IR graph that does not fuse the backend supported nodes to delegate node(s).
  2. I noticed that by using the edge IR graph above, the input order might have changed. This can be easily reproduced by using model that has more than 1 input (e.g., Roberta). I would like to know how if there's any way I can get a graph that has the correct input order.

Thanks

@Gasoonjia

Copy link
Copy Markdown
Contributor

hi @winskuo-quic

I think instead of using the edge graph IR as the ground truth for comparsion, it will be great if we can use the export program ET stack get at the first place (e.g. the export graph of model variable

here), since it should be the source graph ET stack take and our job is making sure our intermediate output the same as the input graph as much as possible.

You can see how we calculate intermediate output numercal descrepancy:

defcalculate_numeric_gap(

https://github.com/pytorch/executorch/blob/0fb422f9c59e0e5526c0082352a583baf0510fb7/exir/passes/debug_handle_generator_pass.py here's pass for debug handle generation, where the debug handle of a node is the same as the node sharing the same greatest ancestor node in the export flow.

@Gasoonjia

Gasoonjia commented Dec 19, 2025

Copy link
Copy Markdown
Contributor

Here's an example of how our current API works on VIT model on xnnpack backend: https://gist.github.com/Gasoonjia/db6285ac39ad5759b95c7a92d37cd4f8

and below is the expected output. For some ops like layernorm there're still some issue i need to fix.

idxaot_opsaot_intermediate_outputruntime_opsruntime_intermediate_outputgap
0[conv2d][[[tensor(0.0253, -0.0287, -0.0042, 0.0118, …)]][DELEGATE_CALL][[[tensor(0.0253, -0.0287, -0.0042, 0.0118, …)]]3.2825094945114346e-15
1[permute, cat, add, dropout][[[tensor(-0.0024), tensor(0.0054), tensor(0.0…), …]][DELEGATE_CALL][[[tensor(-0.0024), tensor(0.0054), tensor(0.0…), …]]3.281230918554512e-15
2[expand][[[tensor(-0.0012), tensor(0.0027), tensor(0.0…), …]][native_call_expand_copy.out][[[tensor(-0.0012), tensor(0.0027), tensor(0.0…), …]]0.0
3[layer_norm][[[tensor(-0.0001), tensor(0.0009), tensor(-0.…), …]][native_call_native_layer_norm.out][[[tensor(31.1172)], [tensor(4.3549)], [tensor(…)…]]19.7299543374596
4[transpose, linear, unflatten, …, transpose][[[tensor(0.0027), tensor(-0.0032), tensor(0.0…), …]][DELEGATE_CALL, DELEGATE_CALL, DELEGATE_…][[[tensor(0.0027), tensor(-0.0032), tensor(0.0…), …]]9.381436078525961e-05
61[layer_norm_23][[[tensor(-0.8604), tensor(-0.1713), tensor(-0.…),…]][native_call_native_layer_norm.out][[[tensor(2.2180)], [tensor(1.8462)], [tensor(…)…]]2.8061147356332854
62[linear_46, gelu_11, dropout_35, …, dropout_36][[[tensor(-0.6561), tensor(-0.0496), tensor(-0.…),…]][DELEGATE_CALL][[[tensor(-0.6561), tensor(-0.0496), tensor(-0.…),…]]1.0872256686587983e-11
63[layer_norm_24][[[tensor(-0.9040), tensor(-0.1004), tensor(-0.…),…]][native_call_native_layer_norm.out][[[tensor(1.9138)], [tensor(1.9031)], [tensor(…)…]]3.104443617092582
64[select_36][[tensor(-0.9040), tensor(-0.1004), tensor(-0.…),…]][native_call_select_copy.int_out][[tensor(-0.9040), tensor(-0.1004), tensor(-0.…),…]]1.1178469901123618e-12
65[linear_48][[tensor(-0.9624), tensor(0.7285), tensor(0.79…),…]][DELEGATE_CALL][[tensor(-0.9624), tensor(0.7285), tensor(0.79…),…]]1.7864835786911282e-12

I would love to chat with you regarding how we can make the pipeline works on qualcomm backend!
Hope it can help you!

@winskuo-quic
winskuo-quicforce-pushed the dev1/winskuo/debug_handle branch from dc72614 to 00c4f7eCompareDecember 19, 2025 05:25
@winskuo-quic
winskuo-quic marked this pull request as draft December 19, 2025 05:26
@winskuo-quic

Copy link
Copy Markdown
CollaboratorAuthor

Hi @Gasoonjia,
I have turned this PR back to draft for now.
I would love to learn more about ExecuTorch's Debugger Framework and let's move this conversation to email first.
I would also love to chat with you to discuss more in details.

@GasoonjiaGasoonjia 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.

thx for the work!

Comment threadbackends/qualcomm/_passes/resolve_debug_handle.py Outdated
def call(self, graph_module: torch.fx.GraphModule):
handle_counter = 1
visited = set()
for node in graph_module.graph.nodes:

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.

not sure if Qualcomm can handle conditional graph. If so i think the way you are adding debug handle might not able to equip debug handle to all branches. You can follow what i'm doing here:
https://github.com/pytorch/executorch/blob/main/exir/passes/debug_handle_generator_pass.py#L14

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

I would like to confirm on definition of conditional graph. Initially, I thought we can't have conditional graph (e.g., if else) in Aten. The only way to do it is through operations such as where op, which still makes it a single graph. We actually use a lot of for loop for graph traversing in our passes. If you find this being a concern, please let me know and I'll take a look at all our other passes.

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.

conditiion graph refers to some branch like torch.cond. https://github.com/pytorch/executorch/blob/main/exir/tests/test_passes.py#L1275 is an example.
For conditional graph like torch.cond we will capture the graph for both branch.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Thanks for sharing the operation. I think we currently does not have this op. I think I can add a TODO or some checks for now and actually made the change if QNN supports this operation in future. This will probably require a bigger refactor since all our passes are now using for loops.

assert (
source_node.name in visited
), "Graph is not traversed in topological order, unexpected behavior."
node.meta[QCOM_DEBUG_HANDLE] = source_node.meta[QCOM_DEBUG_HANDLE]

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.

Curious why do we need to set the get_item node the same debug handle as the soruce node? Since it will introduce duplicate debug handle in the graph and im a little bit worried if it could cause any issue in the down stream,

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

I am actually just following the same behavior prior to this pass. For node's that has multi-output, it will have get_item node as users. I noticed that the debug_handle for all get_item node and node itself all have the same debug_handle, as shown below.
image

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.

I feel like the reason of get_item and its user sharing the same debug handle is because of exporting in strict mode and both aten_topk and its get_item function are from same source.
For here i don't have strong preference of whether or not we should make get_item's debug handle the same as source node, as long as it can support Qualcomm's requirement.

tensor_name = f"{node.name}_{wrapper_idx}"

# Only append special namings when enable tensor dump, since longer name results bigger .pte
if (handle_id := node.meta.get(QCOM_DEBUG_HANDLE)) and self.enable_tensor_dump:

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.

wondering if we still need this file since we will migrate to devtool infra?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

I believe this can be eventually deprecated as we discussed offline. However, since currently we don't have an official API to bring debug_handle to runtime, we will need to stick with parsing tensor name during runtime to extract debug_handle ID.

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.

oh will the tensor name here be part of runtime?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

During runtime, we will get tensor name like relu__debugID_1, where 1 here is the debug handle. We use regex in runtime to parse the ID 1 and save it to ETDUMP as the key.
I agree we will migrate to devtool infra, but due to some of the limitations where we don't really have an official flow to pass debug_handle ID related info to runtime, we need to keep it this way so the debugger can still function after this PR is merged.

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.

Thx for detailed explanation! Mind share me the ptr of how you retreive the op name during runtime?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Of course! Please refer to the message below.

Comment threadbackends/qualcomm/debugger/qnn_intermediate_debugger.py
@winskuo-quic
winskuo-quicforce-pushed the dev1/winskuo/debug_handle branch from 00c4f7e to 7ec377fCompareDecember 29, 2025 07:48
@winskuo-quic
winskuo-quic marked this pull request as ready for review December 29, 2025 07:48
std::string qnn_tensor_name =
std::string(QNN_TENSOR_VER_PTR(output_tensor)->name);
if (std::regex_search(qnn_tensor_name, match, re)) {
debug_handle_id = static_cast<uint32_t>(std::stoul(match[1].str()));

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Hi @Gasoonjia,
This is where we parse the qnn_tensor_name and get the debug_handle id.

@winskuo-quic
winskuo-quicforce-pushed the dev1/winskuo/debug_handle branch from 403300d to cc92ab4CompareJanuary 8, 2026 05:00
@github-actions

Copy link
Copy Markdown

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
If you want the bot to never mark this PR stale again, add the no-stale label.
Stale pull requests will automatically be closed after 30 days of inactivity.

@github-actionsgithub-actionsBot added the Stale PRs inactive for over 60 days label Mar 10, 2026
@Gasoonjia

Copy link
Copy Markdown
Contributor

hi @winskuo-quic any related PR recently need my support or review?

@winskuo-quic

Copy link
Copy Markdown
CollaboratorAuthor

hi @winskuo-quic any related PR recently need my support or review?

Hi @Gasoonjia,
It would be appreciated if you can have a look at this PR: #18834
It is the 2nd part of the PR, which is reusing all dev tools feature. After this PR is approved, I will have a 3rd PR for claude SKILL.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA SignedThis label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.StalePRs inactive for over 60 days

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@winskuo-quic@Gasoonjia
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Qualcomm AI Engine Direct - Support Debug Handle and Integrate IntermediateOutputCapturer - #16316

Closed
winskuo-quic wants to merge 3 commits into
pytorch:mainfrom
CodeLinaro:dev1/winskuo/debug_handle
Closed

Qualcomm AI Engine Direct - Support Debug Handle and Integrate IntermediateOutputCapturer#16316
winskuo-quic wants to merge 3 commits into
pytorch:mainfrom
CodeLinaro:dev1/winskuo/debug_handle

Conversation

@winskuo-quic

Copy link
Copy Markdown
Collaborator

Summary

  • AOT Debug Handle Enablement. Also supported debug_handle map, although debugger did not use it. Enable this because of community request.
  • Runtime Debug Handle Enablement: Parse debug handle in runtime and use debug_handle and tensors key when storing result into etdump.
  • Reuse ExecuTorch Debugger feature and reduce redundancy between QNN ExecuTorch Debugger and ExecuTorch Debugger Utils.

Additional Topics:

  • What is the official way of retrieving an edge module that does not carry backend info?

Test plan

  • E2E example script test
    • python backends/qualcomm/tests/test_qnn_delegate.py -k TestExampleUtilsScript.test_intermediate_debugger -s $DEVICE --model SM8650 --build_folder build-android/ --executorch_root . --image_dataset ../imagenet-mini/val/ --artifact ./e2e_test_debug
  • Simple model test
    • python backends/qualcomm/tests/test_qnn_delegate.py -k TestQNNQuantizedUtils.test_qnn_backend_dump_intermediate_outputs_simple_model --model SM8550 --device $DEVICE --build_folder build-android
    • python backends/qualcomm/tests/test_qnn_delegate.py -k TestQNNQuantizedUtils.test_qnn_backend_dump_intermediate_outputs_topk --model SM8550 --device $DEVICE --build_folder build-android

@pytorch-bot

pytorch-botBot commented Dec 18, 2025

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/16316

Note: Links to docs will display an error until the docs builds have been completed.

✅ You can merge normally! (1 Unrelated Failure)

As of commit cc92ab4 with merge base 8e8d97e (image):

UNSTABLE - The following job is marked as unstable, possibly due to flakiness on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-clameta-claBot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 18, 2025
@github-actions

Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@winskuo-quic
winskuo-quic marked this pull request as draft December 18, 2025 09:51
@Gasoonjia

Copy link
Copy Markdown
Contributor

Is the PR ready to be reviewed now?

Comment threadbackends/qualcomm/_passes/resolve_debug_handle.py
@winskuo-quic
winskuo-quic marked this pull request as ready for review December 19, 2025 01:36
@winskuo-quic
winskuo-quicforce-pushed the dev1/winskuo/debug_handle branch 2 times, most recently from 9a7ca59 to dc72614CompareDecember 19, 2025 01:45
@winskuo-quic

Copy link
Copy Markdown
CollaboratorAuthor

Is the PR ready to be reviewed now?

Hi @Gasoonjia,
I was rebasing previously, so I set it to draft. This PR should now be ready for review. Thanks.

@winskuo-quic

Copy link
Copy Markdown
CollaboratorAuthor

Hi @cccclai, @Gasoonjia, @kimishpatel,
I have supported the debug_handle in this PR, which has been mentioned in #5310 and #15735.
We will now be using the ExecuTorch's official Intermediate_Output_Capturer to capture CPU's intermediate result.

Also, I would also like to get some suggestions on the official API to retrieve an edge IR. The current way of retrieving an edge IR is through:

edge_module=lower_module.original_module.module()

However, I encountered following issues when retrieving edge IR using the above method.

  1. If there are partitions, I'll be getting a graph that fuses supported nodes to delegate node(s). However, it would be helpful for debugging if we could get the edge IR graph that does not fuse the backend supported nodes to delegate node(s).
  2. I noticed that by using the edge IR graph above, the input order might have changed. This can be easily reproduced by using model that has more than 1 input (e.g., Roberta). I would like to know how if there's any way I can get a graph that has the correct input order.

Thanks

@Gasoonjia

Copy link
Copy Markdown
Contributor

hi @winskuo-quic

I think instead of using the edge graph IR as the ground truth for comparsion, it will be great if we can use the export program ET stack get at the first place (e.g. the export graph of model variable

here), since it should be the source graph ET stack take and our job is making sure our intermediate output the same as the input graph as much as possible.

You can see how we calculate intermediate output numercal descrepancy:

defcalculate_numeric_gap(

https://github.com/pytorch/executorch/blob/0fb422f9c59e0e5526c0082352a583baf0510fb7/exir/passes/debug_handle_generator_pass.py here's pass for debug handle generation, where the debug handle of a node is the same as the node sharing the same greatest ancestor node in the export flow.

@Gasoonjia

Gasoonjia commented Dec 19, 2025

Copy link
Copy Markdown
Contributor

Here's an example of how our current API works on VIT model on xnnpack backend: https://gist.github.com/Gasoonjia/db6285ac39ad5759b95c7a92d37cd4f8

and below is the expected output. For some ops like layernorm there're still some issue i need to fix.

idxaot_opsaot_intermediate_outputruntime_opsruntime_intermediate_outputgap
0[conv2d][[[tensor(0.0253, -0.0287, -0.0042, 0.0118, …)]][DELEGATE_CALL][[[tensor(0.0253, -0.0287, -0.0042, 0.0118, …)]]3.2825094945114346e-15
1[permute, cat, add, dropout][[[tensor(-0.0024), tensor(0.0054), tensor(0.0…), …]][DELEGATE_CALL][[[tensor(-0.0024), tensor(0.0054), tensor(0.0…), …]]3.281230918554512e-15
2[expand][[[tensor(-0.0012), tensor(0.0027), tensor(0.0…), …]][native_call_expand_copy.out][[[tensor(-0.0012), tensor(0.0027), tensor(0.0…), …]]0.0
3[layer_norm][[[tensor(-0.0001), tensor(0.0009), tensor(-0.…), …]][native_call_native_layer_norm.out][[[tensor(31.1172)], [tensor(4.3549)], [tensor(…)…]]19.7299543374596
4[transpose, linear, unflatten, …, transpose][[[tensor(0.0027), tensor(-0.0032), tensor(0.0…), …]][DELEGATE_CALL, DELEGATE_CALL, DELEGATE_…][[[tensor(0.0027), tensor(-0.0032), tensor(0.0…), …]]9.381436078525961e-05
61[layer_norm_23][[[tensor(-0.8604), tensor(-0.1713), tensor(-0.…),…]][native_call_native_layer_norm.out][[[tensor(2.2180)], [tensor(1.8462)], [tensor(…)…]]2.8061147356332854
62[linear_46, gelu_11, dropout_35, …, dropout_36][[[tensor(-0.6561), tensor(-0.0496), tensor(-0.…),…]][DELEGATE_CALL][[[tensor(-0.6561), tensor(-0.0496), tensor(-0.…),…]]1.0872256686587983e-11
63[layer_norm_24][[[tensor(-0.9040), tensor(-0.1004), tensor(-0.…),…]][native_call_native_layer_norm.out][[[tensor(1.9138)], [tensor(1.9031)], [tensor(…)…]]3.104443617092582
64[select_36][[tensor(-0.9040), tensor(-0.1004), tensor(-0.…),…]][native_call_select_copy.int_out][[tensor(-0.9040), tensor(-0.1004), tensor(-0.…),…]]1.1178469901123618e-12
65[linear_48][[tensor(-0.9624), tensor(0.7285), tensor(0.79…),…]][DELEGATE_CALL][[tensor(-0.9624), tensor(0.7285), tensor(0.79…),…]]1.7864835786911282e-12

I would love to chat with you regarding how we can make the pipeline works on qualcomm backend!
Hope it can help you!

@winskuo-quic
winskuo-quicforce-pushed the dev1/winskuo/debug_handle branch from dc72614 to 00c4f7eCompareDecember 19, 2025 05:25
@winskuo-quic
winskuo-quic marked this pull request as draft December 19, 2025 05:26
@winskuo-quic

Copy link
Copy Markdown
CollaboratorAuthor

Hi @Gasoonjia,
I have turned this PR back to draft for now.
I would love to learn more about ExecuTorch's Debugger Framework and let's move this conversation to email first.
I would also love to chat with you to discuss more in details.

@GasoonjiaGasoonjia 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.

thx for the work!

Comment threadbackends/qualcomm/_passes/resolve_debug_handle.py Outdated
def call(self, graph_module: torch.fx.GraphModule):
handle_counter = 1
visited = set()
for node in graph_module.graph.nodes:

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.

not sure if Qualcomm can handle conditional graph. If so i think the way you are adding debug handle might not able to equip debug handle to all branches. You can follow what i'm doing here:
https://github.com/pytorch/executorch/blob/main/exir/passes/debug_handle_generator_pass.py#L14

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

I would like to confirm on definition of conditional graph. Initially, I thought we can't have conditional graph (e.g., if else) in Aten. The only way to do it is through operations such as where op, which still makes it a single graph. We actually use a lot of for loop for graph traversing in our passes. If you find this being a concern, please let me know and I'll take a look at all our other passes.

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.

conditiion graph refers to some branch like torch.cond. https://github.com/pytorch/executorch/blob/main/exir/tests/test_passes.py#L1275 is an example.
For conditional graph like torch.cond we will capture the graph for both branch.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Thanks for sharing the operation. I think we currently does not have this op. I think I can add a TODO or some checks for now and actually made the change if QNN supports this operation in future. This will probably require a bigger refactor since all our passes are now using for loops.

assert (
source_node.name in visited
), "Graph is not traversed in topological order, unexpected behavior."
node.meta[QCOM_DEBUG_HANDLE] = source_node.meta[QCOM_DEBUG_HANDLE]

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.

Curious why do we need to set the get_item node the same debug handle as the soruce node? Since it will introduce duplicate debug handle in the graph and im a little bit worried if it could cause any issue in the down stream,

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

I am actually just following the same behavior prior to this pass. For node's that has multi-output, it will have get_item node as users. I noticed that the debug_handle for all get_item node and node itself all have the same debug_handle, as shown below.
image

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.

I feel like the reason of get_item and its user sharing the same debug handle is because of exporting in strict mode and both aten_topk and its get_item function are from same source.
For here i don't have strong preference of whether or not we should make get_item's debug handle the same as source node, as long as it can support Qualcomm's requirement.

tensor_name = f"{node.name}_{wrapper_idx}"

# Only append special namings when enable tensor dump, since longer name results bigger .pte
if (handle_id := node.meta.get(QCOM_DEBUG_HANDLE)) and self.enable_tensor_dump:

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.

wondering if we still need this file since we will migrate to devtool infra?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

I believe this can be eventually deprecated as we discussed offline. However, since currently we don't have an official API to bring debug_handle to runtime, we will need to stick with parsing tensor name during runtime to extract debug_handle ID.

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.

oh will the tensor name here be part of runtime?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

During runtime, we will get tensor name like relu__debugID_1, where 1 here is the debug handle. We use regex in runtime to parse the ID 1 and save it to ETDUMP as the key.
I agree we will migrate to devtool infra, but due to some of the limitations where we don't really have an official flow to pass debug_handle ID related info to runtime, we need to keep it this way so the debugger can still function after this PR is merged.

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.

Thx for detailed explanation! Mind share me the ptr of how you retreive the op name during runtime?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Of course! Please refer to the message below.

Comment threadbackends/qualcomm/debugger/qnn_intermediate_debugger.py
@winskuo-quic
winskuo-quicforce-pushed the dev1/winskuo/debug_handle branch from 00c4f7e to 7ec377fCompareDecember 29, 2025 07:48
@winskuo-quic
winskuo-quic marked this pull request as ready for review December 29, 2025 07:48
std::string qnn_tensor_name =
std::string(QNN_TENSOR_VER_PTR(output_tensor)->name);
if (std::regex_search(qnn_tensor_name, match, re)) {
debug_handle_id = static_cast<uint32_t>(std::stoul(match[1].str()));

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Hi @Gasoonjia,
This is where we parse the qnn_tensor_name and get the debug_handle id.

@winskuo-quic
winskuo-quicforce-pushed the dev1/winskuo/debug_handle branch from 403300d to cc92ab4CompareJanuary 8, 2026 05:00
@github-actions

Copy link
Copy Markdown

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
If you want the bot to never mark this PR stale again, add the no-stale label.
Stale pull requests will automatically be closed after 30 days of inactivity.

@github-actionsgithub-actionsBot added the Stale PRs inactive for over 60 days label Mar 10, 2026
@Gasoonjia

Copy link
Copy Markdown
Contributor

hi @winskuo-quic any related PR recently need my support or review?

@winskuo-quic

Copy link
Copy Markdown
CollaboratorAuthor

hi @winskuo-quic any related PR recently need my support or review?

Hi @Gasoonjia,
It would be appreciated if you can have a look at this PR: #18834
It is the 2nd part of the PR, which is reusing all dev tools feature. After this PR is approved, I will have a 3rd PR for claude SKILL.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA SignedThis label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.StalePRs inactive for over 60 days

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@winskuo-quic@Gasoonjia
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Qualcomm AI Engine Direct - Support Debug Handle and Integrate IntermediateOutputCapturer - #16316

Closed
winskuo-quic wants to merge 3 commits into
pytorch:mainfrom
CodeLinaro:dev1/winskuo/debug_handle
Closed

Qualcomm AI Engine Direct - Support Debug Handle and Integrate IntermediateOutputCapturer#16316
winskuo-quic wants to merge 3 commits into
pytorch:mainfrom
CodeLinaro:dev1/winskuo/debug_handle

Conversation

@winskuo-quic

Copy link
Copy Markdown
Collaborator

Summary

  • AOT Debug Handle Enablement. Also supported debug_handle map, although debugger did not use it. Enable this because of community request.
  • Runtime Debug Handle Enablement: Parse debug handle in runtime and use debug_handle and tensors key when storing result into etdump.
  • Reuse ExecuTorch Debugger feature and reduce redundancy between QNN ExecuTorch Debugger and ExecuTorch Debugger Utils.

Additional Topics:

  • What is the official way of retrieving an edge module that does not carry backend info?

Test plan

  • E2E example script test
    • python backends/qualcomm/tests/test_qnn_delegate.py -k TestExampleUtilsScript.test_intermediate_debugger -s $DEVICE --model SM8650 --build_folder build-android/ --executorch_root . --image_dataset ../imagenet-mini/val/ --artifact ./e2e_test_debug
  • Simple model test
    • python backends/qualcomm/tests/test_qnn_delegate.py -k TestQNNQuantizedUtils.test_qnn_backend_dump_intermediate_outputs_simple_model --model SM8550 --device $DEVICE --build_folder build-android
    • python backends/qualcomm/tests/test_qnn_delegate.py -k TestQNNQuantizedUtils.test_qnn_backend_dump_intermediate_outputs_topk --model SM8550 --device $DEVICE --build_folder build-android

@pytorch-bot

pytorch-botBot commented Dec 18, 2025

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/16316

Note: Links to docs will display an error until the docs builds have been completed.

✅ You can merge normally! (1 Unrelated Failure)

As of commit cc92ab4 with merge base 8e8d97e (image):

UNSTABLE - The following job is marked as unstable, possibly due to flakiness on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-clameta-claBot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 18, 2025
@github-actions

Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@winskuo-quic
winskuo-quic marked this pull request as draft December 18, 2025 09:51
@Gasoonjia

Copy link
Copy Markdown
Contributor

Is the PR ready to be reviewed now?

Comment threadbackends/qualcomm/_passes/resolve_debug_handle.py
@winskuo-quic
winskuo-quic marked this pull request as ready for review December 19, 2025 01:36
@winskuo-quic
winskuo-quicforce-pushed the dev1/winskuo/debug_handle branch 2 times, most recently from 9a7ca59 to dc72614CompareDecember 19, 2025 01:45
@winskuo-quic

Copy link
Copy Markdown
CollaboratorAuthor

Is the PR ready to be reviewed now?

Hi @Gasoonjia,
I was rebasing previously, so I set it to draft. This PR should now be ready for review. Thanks.

@winskuo-quic

Copy link
Copy Markdown
CollaboratorAuthor

Hi @cccclai, @Gasoonjia, @kimishpatel,
I have supported the debug_handle in this PR, which has been mentioned in #5310 and #15735.
We will now be using the ExecuTorch's official Intermediate_Output_Capturer to capture CPU's intermediate result.

Also, I would also like to get some suggestions on the official API to retrieve an edge IR. The current way of retrieving an edge IR is through:

edge_module=lower_module.original_module.module()

However, I encountered following issues when retrieving edge IR using the above method.

  1. If there are partitions, I'll be getting a graph that fuses supported nodes to delegate node(s). However, it would be helpful for debugging if we could get the edge IR graph that does not fuse the backend supported nodes to delegate node(s).
  2. I noticed that by using the edge IR graph above, the input order might have changed. This can be easily reproduced by using model that has more than 1 input (e.g., Roberta). I would like to know how if there's any way I can get a graph that has the correct input order.

Thanks

@Gasoonjia

Copy link
Copy Markdown
Contributor

hi @winskuo-quic

I think instead of using the edge graph IR as the ground truth for comparsion, it will be great if we can use the export program ET stack get at the first place (e.g. the export graph of model variable

here), since it should be the source graph ET stack take and our job is making sure our intermediate output the same as the input graph as much as possible.

You can see how we calculate intermediate output numercal descrepancy:

defcalculate_numeric_gap(

https://github.com/pytorch/executorch/blob/0fb422f9c59e0e5526c0082352a583baf0510fb7/exir/passes/debug_handle_generator_pass.py here's pass for debug handle generation, where the debug handle of a node is the same as the node sharing the same greatest ancestor node in the export flow.

@Gasoonjia

Gasoonjia commented Dec 19, 2025

Copy link
Copy Markdown
Contributor

Here's an example of how our current API works on VIT model on xnnpack backend: https://gist.github.com/Gasoonjia/db6285ac39ad5759b95c7a92d37cd4f8

and below is the expected output. For some ops like layernorm there're still some issue i need to fix.

idxaot_opsaot_intermediate_outputruntime_opsruntime_intermediate_outputgap
0[conv2d][[[tensor(0.0253, -0.0287, -0.0042, 0.0118, …)]][DELEGATE_CALL][[[tensor(0.0253, -0.0287, -0.0042, 0.0118, …)]]3.2825094945114346e-15
1[permute, cat, add, dropout][[[tensor(-0.0024), tensor(0.0054), tensor(0.0…), …]][DELEGATE_CALL][[[tensor(-0.0024), tensor(0.0054), tensor(0.0…), …]]3.281230918554512e-15
2[expand][[[tensor(-0.0012), tensor(0.0027), tensor(0.0…), …]][native_call_expand_copy.out][[[tensor(-0.0012), tensor(0.0027), tensor(0.0…), …]]0.0
3[layer_norm][[[tensor(-0.0001), tensor(0.0009), tensor(-0.…), …]][native_call_native_layer_norm.out][[[tensor(31.1172)], [tensor(4.3549)], [tensor(…)…]]19.7299543374596
4[transpose, linear, unflatten, …, transpose][[[tensor(0.0027), tensor(-0.0032), tensor(0.0…), …]][DELEGATE_CALL, DELEGATE_CALL, DELEGATE_…][[[tensor(0.0027), tensor(-0.0032), tensor(0.0…), …]]9.381436078525961e-05
61[layer_norm_23][[[tensor(-0.8604), tensor(-0.1713), tensor(-0.…),…]][native_call_native_layer_norm.out][[[tensor(2.2180)], [tensor(1.8462)], [tensor(…)…]]2.8061147356332854
62[linear_46, gelu_11, dropout_35, …, dropout_36][[[tensor(-0.6561), tensor(-0.0496), tensor(-0.…),…]][DELEGATE_CALL][[[tensor(-0.6561), tensor(-0.0496), tensor(-0.…),…]]1.0872256686587983e-11
63[layer_norm_24][[[tensor(-0.9040), tensor(-0.1004), tensor(-0.…),…]][native_call_native_layer_norm.out][[[tensor(1.9138)], [tensor(1.9031)], [tensor(…)…]]3.104443617092582
64[select_36][[tensor(-0.9040), tensor(-0.1004), tensor(-0.…),…]][native_call_select_copy.int_out][[tensor(-0.9040), tensor(-0.1004), tensor(-0.…),…]]1.1178469901123618e-12
65[linear_48][[tensor(-0.9624), tensor(0.7285), tensor(0.79…),…]][DELEGATE_CALL][[tensor(-0.9624), tensor(0.7285), tensor(0.79…),…]]1.7864835786911282e-12

I would love to chat with you regarding how we can make the pipeline works on qualcomm backend!
Hope it can help you!

@winskuo-quic
winskuo-quicforce-pushed the dev1/winskuo/debug_handle branch from dc72614 to 00c4f7eCompareDecember 19, 2025 05:25
@winskuo-quic
winskuo-quic marked this pull request as draft December 19, 2025 05:26
@winskuo-quic

Copy link
Copy Markdown
CollaboratorAuthor

Hi @Gasoonjia,
I have turned this PR back to draft for now.
I would love to learn more about ExecuTorch's Debugger Framework and let's move this conversation to email first.
I would also love to chat with you to discuss more in details.

@GasoonjiaGasoonjia 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.

thx for the work!

Comment threadbackends/qualcomm/_passes/resolve_debug_handle.py Outdated
def call(self, graph_module: torch.fx.GraphModule):
handle_counter = 1
visited = set()
for node in graph_module.graph.nodes:

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.

not sure if Qualcomm can handle conditional graph. If so i think the way you are adding debug handle might not able to equip debug handle to all branches. You can follow what i'm doing here:
https://github.com/pytorch/executorch/blob/main/exir/passes/debug_handle_generator_pass.py#L14

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

I would like to confirm on definition of conditional graph. Initially, I thought we can't have conditional graph (e.g., if else) in Aten. The only way to do it is through operations such as where op, which still makes it a single graph. We actually use a lot of for loop for graph traversing in our passes. If you find this being a concern, please let me know and I'll take a look at all our other passes.

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.

conditiion graph refers to some branch like torch.cond. https://github.com/pytorch/executorch/blob/main/exir/tests/test_passes.py#L1275 is an example.
For conditional graph like torch.cond we will capture the graph for both branch.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Thanks for sharing the operation. I think we currently does not have this op. I think I can add a TODO or some checks for now and actually made the change if QNN supports this operation in future. This will probably require a bigger refactor since all our passes are now using for loops.

assert (
source_node.name in visited
), "Graph is not traversed in topological order, unexpected behavior."
node.meta[QCOM_DEBUG_HANDLE] = source_node.meta[QCOM_DEBUG_HANDLE]

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.

Curious why do we need to set the get_item node the same debug handle as the soruce node? Since it will introduce duplicate debug handle in the graph and im a little bit worried if it could cause any issue in the down stream,

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

I am actually just following the same behavior prior to this pass. For node's that has multi-output, it will have get_item node as users. I noticed that the debug_handle for all get_item node and node itself all have the same debug_handle, as shown below.
image

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.

I feel like the reason of get_item and its user sharing the same debug handle is because of exporting in strict mode and both aten_topk and its get_item function are from same source.
For here i don't have strong preference of whether or not we should make get_item's debug handle the same as source node, as long as it can support Qualcomm's requirement.

tensor_name = f"{node.name}_{wrapper_idx}"

# Only append special namings when enable tensor dump, since longer name results bigger .pte
if (handle_id := node.meta.get(QCOM_DEBUG_HANDLE)) and self.enable_tensor_dump:

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.

wondering if we still need this file since we will migrate to devtool infra?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

I believe this can be eventually deprecated as we discussed offline. However, since currently we don't have an official API to bring debug_handle to runtime, we will need to stick with parsing tensor name during runtime to extract debug_handle ID.

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.

oh will the tensor name here be part of runtime?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

During runtime, we will get tensor name like relu__debugID_1, where 1 here is the debug handle. We use regex in runtime to parse the ID 1 and save it to ETDUMP as the key.
I agree we will migrate to devtool infra, but due to some of the limitations where we don't really have an official flow to pass debug_handle ID related info to runtime, we need to keep it this way so the debugger can still function after this PR is merged.

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.

Thx for detailed explanation! Mind share me the ptr of how you retreive the op name during runtime?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Of course! Please refer to the message below.

Comment threadbackends/qualcomm/debugger/qnn_intermediate_debugger.py
@winskuo-quic
winskuo-quicforce-pushed the dev1/winskuo/debug_handle branch from 00c4f7e to 7ec377fCompareDecember 29, 2025 07:48
@winskuo-quic
winskuo-quic marked this pull request as ready for review December 29, 2025 07:48
std::string qnn_tensor_name =
std::string(QNN_TENSOR_VER_PTR(output_tensor)->name);
if (std::regex_search(qnn_tensor_name, match, re)) {
debug_handle_id = static_cast<uint32_t>(std::stoul(match[1].str()));

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Hi @Gasoonjia,
This is where we parse the qnn_tensor_name and get the debug_handle id.

@winskuo-quic
winskuo-quicforce-pushed the dev1/winskuo/debug_handle branch from 403300d to cc92ab4CompareJanuary 8, 2026 05:00
@github-actions

Copy link
Copy Markdown

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
If you want the bot to never mark this PR stale again, add the no-stale label.
Stale pull requests will automatically be closed after 30 days of inactivity.

@github-actionsgithub-actionsBot added the Stale PRs inactive for over 60 days label Mar 10, 2026
@Gasoonjia

Copy link
Copy Markdown
Contributor

hi @winskuo-quic any related PR recently need my support or review?

@winskuo-quic

Copy link
Copy Markdown
CollaboratorAuthor

hi @winskuo-quic any related PR recently need my support or review?

Hi @Gasoonjia,
It would be appreciated if you can have a look at this PR: #18834
It is the 2nd part of the PR, which is reusing all dev tools feature. After this PR is approved, I will have a 3rd PR for claude SKILL.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA SignedThis label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.StalePRs inactive for over 60 days

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@winskuo-quic@Gasoonjia
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Qualcomm AI Engine Direct - Support Debug Handle and Integrate IntermediateOutputCapturer - #16316

Closed
winskuo-quic wants to merge 3 commits into
pytorch:mainfrom
CodeLinaro:dev1/winskuo/debug_handle
Closed

Qualcomm AI Engine Direct - Support Debug Handle and Integrate IntermediateOutputCapturer#16316
winskuo-quic wants to merge 3 commits into
pytorch:mainfrom
CodeLinaro:dev1/winskuo/debug_handle

Conversation

@winskuo-quic

Copy link
Copy Markdown
Collaborator

Summary

  • AOT Debug Handle Enablement. Also supported debug_handle map, although debugger did not use it. Enable this because of community request.
  • Runtime Debug Handle Enablement: Parse debug handle in runtime and use debug_handle and tensors key when storing result into etdump.
  • Reuse ExecuTorch Debugger feature and reduce redundancy between QNN ExecuTorch Debugger and ExecuTorch Debugger Utils.

Additional Topics:

  • What is the official way of retrieving an edge module that does not carry backend info?

Test plan

  • E2E example script test
    • python backends/qualcomm/tests/test_qnn_delegate.py -k TestExampleUtilsScript.test_intermediate_debugger -s $DEVICE --model SM8650 --build_folder build-android/ --executorch_root . --image_dataset ../imagenet-mini/val/ --artifact ./e2e_test_debug
  • Simple model test
    • python backends/qualcomm/tests/test_qnn_delegate.py -k TestQNNQuantizedUtils.test_qnn_backend_dump_intermediate_outputs_simple_model --model SM8550 --device $DEVICE --build_folder build-android
    • python backends/qualcomm/tests/test_qnn_delegate.py -k TestQNNQuantizedUtils.test_qnn_backend_dump_intermediate_outputs_topk --model SM8550 --device $DEVICE --build_folder build-android

@pytorch-bot

pytorch-botBot commented Dec 18, 2025

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/16316

Note: Links to docs will display an error until the docs builds have been completed.

✅ You can merge normally! (1 Unrelated Failure)

As of commit cc92ab4 with merge base 8e8d97e (image):

UNSTABLE - The following job is marked as unstable, possibly due to flakiness on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-clameta-claBot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 18, 2025
@github-actions

Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@winskuo-quic
winskuo-quic marked this pull request as draft December 18, 2025 09:51
@Gasoonjia

Copy link
Copy Markdown
Contributor

Is the PR ready to be reviewed now?

Comment threadbackends/qualcomm/_passes/resolve_debug_handle.py
@winskuo-quic
winskuo-quic marked this pull request as ready for review December 19, 2025 01:36
@winskuo-quic
winskuo-quicforce-pushed the dev1/winskuo/debug_handle branch 2 times, most recently from 9a7ca59 to dc72614CompareDecember 19, 2025 01:45
@winskuo-quic

Copy link
Copy Markdown
CollaboratorAuthor

Is the PR ready to be reviewed now?

Hi @Gasoonjia,
I was rebasing previously, so I set it to draft. This PR should now be ready for review. Thanks.

@winskuo-quic

Copy link
Copy Markdown
CollaboratorAuthor

Hi @cccclai, @Gasoonjia, @kimishpatel,
I have supported the debug_handle in this PR, which has been mentioned in #5310 and #15735.
We will now be using the ExecuTorch's official Intermediate_Output_Capturer to capture CPU's intermediate result.

Also, I would also like to get some suggestions on the official API to retrieve an edge IR. The current way of retrieving an edge IR is through:

edge_module=lower_module.original_module.module()

However, I encountered following issues when retrieving edge IR using the above method.

  1. If there are partitions, I'll be getting a graph that fuses supported nodes to delegate node(s). However, it would be helpful for debugging if we could get the edge IR graph that does not fuse the backend supported nodes to delegate node(s).
  2. I noticed that by using the edge IR graph above, the input order might have changed. This can be easily reproduced by using model that has more than 1 input (e.g., Roberta). I would like to know how if there's any way I can get a graph that has the correct input order.

Thanks

@Gasoonjia

Copy link
Copy Markdown
Contributor

hi @winskuo-quic

I think instead of using the edge graph IR as the ground truth for comparsion, it will be great if we can use the export program ET stack get at the first place (e.g. the export graph of model variable

here), since it should be the source graph ET stack take and our job is making sure our intermediate output the same as the input graph as much as possible.

You can see how we calculate intermediate output numercal descrepancy:

defcalculate_numeric_gap(

https://github.com/pytorch/executorch/blob/0fb422f9c59e0e5526c0082352a583baf0510fb7/exir/passes/debug_handle_generator_pass.py here's pass for debug handle generation, where the debug handle of a node is the same as the node sharing the same greatest ancestor node in the export flow.

@Gasoonjia

Gasoonjia commented Dec 19, 2025

Copy link
Copy Markdown
Contributor

Here's an example of how our current API works on VIT model on xnnpack backend: https://gist.github.com/Gasoonjia/db6285ac39ad5759b95c7a92d37cd4f8

and below is the expected output. For some ops like layernorm there're still some issue i need to fix.

idxaot_opsaot_intermediate_outputruntime_opsruntime_intermediate_outputgap
0[conv2d][[[tensor(0.0253, -0.0287, -0.0042, 0.0118, …)]][DELEGATE_CALL][[[tensor(0.0253, -0.0287, -0.0042, 0.0118, …)]]3.2825094945114346e-15
1[permute, cat, add, dropout][[[tensor(-0.0024), tensor(0.0054), tensor(0.0…), …]][DELEGATE_CALL][[[tensor(-0.0024), tensor(0.0054), tensor(0.0…), …]]3.281230918554512e-15
2[expand][[[tensor(-0.0012), tensor(0.0027), tensor(0.0…), …]][native_call_expand_copy.out][[[tensor(-0.0012), tensor(0.0027), tensor(0.0…), …]]0.0
3[layer_norm][[[tensor(-0.0001), tensor(0.0009), tensor(-0.…), …]][native_call_native_layer_norm.out][[[tensor(31.1172)], [tensor(4.3549)], [tensor(…)…]]19.7299543374596
4[transpose, linear, unflatten, …, transpose][[[tensor(0.0027), tensor(-0.0032), tensor(0.0…), …]][DELEGATE_CALL, DELEGATE_CALL, DELEGATE_…][[[tensor(0.0027), tensor(-0.0032), tensor(0.0…), …]]9.381436078525961e-05
61[layer_norm_23][[[tensor(-0.8604), tensor(-0.1713), tensor(-0.…),…]][native_call_native_layer_norm.out][[[tensor(2.2180)], [tensor(1.8462)], [tensor(…)…]]2.8061147356332854
62[linear_46, gelu_11, dropout_35, …, dropout_36][[[tensor(-0.6561), tensor(-0.0496), tensor(-0.…),…]][DELEGATE_CALL][[[tensor(-0.6561), tensor(-0.0496), tensor(-0.…),…]]1.0872256686587983e-11
63[layer_norm_24][[[tensor(-0.9040), tensor(-0.1004), tensor(-0.…),…]][native_call_native_layer_norm.out][[[tensor(1.9138)], [tensor(1.9031)], [tensor(…)…]]3.104443617092582
64[select_36][[tensor(-0.9040), tensor(-0.1004), tensor(-0.…),…]][native_call_select_copy.int_out][[tensor(-0.9040), tensor(-0.1004), tensor(-0.…),…]]1.1178469901123618e-12
65[linear_48][[tensor(-0.9624), tensor(0.7285), tensor(0.79…),…]][DELEGATE_CALL][[tensor(-0.9624), tensor(0.7285), tensor(0.79…),…]]1.7864835786911282e-12

I would love to chat with you regarding how we can make the pipeline works on qualcomm backend!
Hope it can help you!

@winskuo-quic
winskuo-quicforce-pushed the dev1/winskuo/debug_handle branch from dc72614 to 00c4f7eCompareDecember 19, 2025 05:25
@winskuo-quic
winskuo-quic marked this pull request as draft December 19, 2025 05:26
@winskuo-quic

Copy link
Copy Markdown
CollaboratorAuthor

Hi @Gasoonjia,
I have turned this PR back to draft for now.
I would love to learn more about ExecuTorch's Debugger Framework and let's move this conversation to email first.
I would also love to chat with you to discuss more in details.

@GasoonjiaGasoonjia 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.

thx for the work!

Comment threadbackends/qualcomm/_passes/resolve_debug_handle.py Outdated
def call(self, graph_module: torch.fx.GraphModule):
handle_counter = 1
visited = set()
for node in graph_module.graph.nodes:

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.

not sure if Qualcomm can handle conditional graph. If so i think the way you are adding debug handle might not able to equip debug handle to all branches. You can follow what i'm doing here:
https://github.com/pytorch/executorch/blob/main/exir/passes/debug_handle_generator_pass.py#L14

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

I would like to confirm on definition of conditional graph. Initially, I thought we can't have conditional graph (e.g., if else) in Aten. The only way to do it is through operations such as where op, which still makes it a single graph. We actually use a lot of for loop for graph traversing in our passes. If you find this being a concern, please let me know and I'll take a look at all our other passes.

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.

conditiion graph refers to some branch like torch.cond. https://github.com/pytorch/executorch/blob/main/exir/tests/test_passes.py#L1275 is an example.
For conditional graph like torch.cond we will capture the graph for both branch.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Thanks for sharing the operation. I think we currently does not have this op. I think I can add a TODO or some checks for now and actually made the change if QNN supports this operation in future. This will probably require a bigger refactor since all our passes are now using for loops.

assert (
source_node.name in visited
), "Graph is not traversed in topological order, unexpected behavior."
node.meta[QCOM_DEBUG_HANDLE] = source_node.meta[QCOM_DEBUG_HANDLE]

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.

Curious why do we need to set the get_item node the same debug handle as the soruce node? Since it will introduce duplicate debug handle in the graph and im a little bit worried if it could cause any issue in the down stream,

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

I am actually just following the same behavior prior to this pass. For node's that has multi-output, it will have get_item node as users. I noticed that the debug_handle for all get_item node and node itself all have the same debug_handle, as shown below.
image

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.

I feel like the reason of get_item and its user sharing the same debug handle is because of exporting in strict mode and both aten_topk and its get_item function are from same source.
For here i don't have strong preference of whether or not we should make get_item's debug handle the same as source node, as long as it can support Qualcomm's requirement.

tensor_name = f"{node.name}_{wrapper_idx}"

# Only append special namings when enable tensor dump, since longer name results bigger .pte
if (handle_id := node.meta.get(QCOM_DEBUG_HANDLE)) and self.enable_tensor_dump:

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.

wondering if we still need this file since we will migrate to devtool infra?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

I believe this can be eventually deprecated as we discussed offline. However, since currently we don't have an official API to bring debug_handle to runtime, we will need to stick with parsing tensor name during runtime to extract debug_handle ID.

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.

oh will the tensor name here be part of runtime?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

During runtime, we will get tensor name like relu__debugID_1, where 1 here is the debug handle. We use regex in runtime to parse the ID 1 and save it to ETDUMP as the key.
I agree we will migrate to devtool infra, but due to some of the limitations where we don't really have an official flow to pass debug_handle ID related info to runtime, we need to keep it this way so the debugger can still function after this PR is merged.

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.

Thx for detailed explanation! Mind share me the ptr of how you retreive the op name during runtime?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Of course! Please refer to the message below.

Comment threadbackends/qualcomm/debugger/qnn_intermediate_debugger.py
@winskuo-quic
winskuo-quicforce-pushed the dev1/winskuo/debug_handle branch from 00c4f7e to 7ec377fCompareDecember 29, 2025 07:48
@winskuo-quic
winskuo-quic marked this pull request as ready for review December 29, 2025 07:48
std::string qnn_tensor_name =
std::string(QNN_TENSOR_VER_PTR(output_tensor)->name);
if (std::regex_search(qnn_tensor_name, match, re)) {
debug_handle_id = static_cast<uint32_t>(std::stoul(match[1].str()));

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Hi @Gasoonjia,
This is where we parse the qnn_tensor_name and get the debug_handle id.

@winskuo-quic
winskuo-quicforce-pushed the dev1/winskuo/debug_handle branch from 403300d to cc92ab4CompareJanuary 8, 2026 05:00
@github-actions

Copy link
Copy Markdown

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
If you want the bot to never mark this PR stale again, add the no-stale label.
Stale pull requests will automatically be closed after 30 days of inactivity.

@github-actionsgithub-actionsBot added the Stale PRs inactive for over 60 days label Mar 10, 2026
@Gasoonjia

Copy link
Copy Markdown
Contributor

hi @winskuo-quic any related PR recently need my support or review?

@winskuo-quic

Copy link
Copy Markdown
CollaboratorAuthor

hi @winskuo-quic any related PR recently need my support or review?

Hi @Gasoonjia,
It would be appreciated if you can have a look at this PR: #18834
It is the 2nd part of the PR, which is reusing all dev tools feature. After this PR is approved, I will have a 3rd PR for claude SKILL.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA SignedThis label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.StalePRs inactive for over 60 days

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@winskuo-quic@Gasoonjia