Resolve externalized call sites by op target, not by recorded node name - #71
Resolve externalized call sites by op target, not by recorded node name#71metascroy wants to merge 3 commits into
Conversation
|
cc @cymbalrush |
|
Hi @metascroy, thanks for the PR.
We would like to understand the use-case or need for renaming the custom ops introduced by coreai-torch (we do so in separate custom op namespace). These APIs were introduced recently and are experimental, we would like to understand how these are getting used. |
The concrete consumer is the ExecuTorch Core AI backend (see sister PR pytorch/executorch#21750). A minimal version of the flow is: ExecuTorch’s standard ATen-to-edge conversion rebuilds the FX graph. For example, I observed: The target remains coreai_torch_ext::n0_rmsnorm_impl_.default; ExecuTorch is not renaming the custom op or changing its namespace. The problem is only that the prepared externalization record refers to the old FX node name. Maybe this isn't the best way to integrate externalization? Any suggestions you have would be appreciated |
|
Hi @metascroy, this PR looks good. I will refactor some of the logic for externalization, so we can remove the need for maintaining this mapping. I will be using this test as a reference (please feel free to update it or add more, so I do not break your use case). |
|
For merging the PR, you must have all the commits to have verified signatures. |
ff06cbd to
7b2c14c
Compare
Problem
add_exported_program(_externalized_exported_programs=...)is documented to emitcomposite graphs "for the patched call sites in
exported_program", but itregisters each lowering under
_ExternalizedExportedProgram.source_nodes— FXnode names captured back when
_subexport_and_restoreran.The two-step API introduced in #53 exists precisely so a caller can do work
between the phases;
_patch_model_for_externalization's own docstring showsep = my_export_or_quantize_pipeline(model). Any pass in that window thatrebuilds or renames nodes leaves the recorded names matching nothing. The
lowerings are then registered under dead keys, and conversion fails later with
an opaque error that names neither the submodule nor the cause:
The call sites are still there — only their names changed. The custom op target
survives any such transform.
Fix
TorchConverter._resolve_source_nodeslocates each call site by op target andpairs it with its
_ExternalizedExportedProgram, falling back to the recordednames when it cannot.
_perform_externalizationuses the resolved names.graph size rather than one graph walk per op name.
call site lives in its parent's program.
order. This assumes a transform preserves the relative order of an op's call
sites, as renaming and the usual lowering passes do.
graph changed shape rather than just its names, so pairing by position would
be meaningless. That op keeps its recorded names and a
UserWarningexplainswhy, instead of silently degrading to the error above.
_utils._externalized_op_nameis the inverse of the existing_find_custom_op_node/_find_all_custom_op_nodeslookups and shares_EXTERNALIZE_NAMESPACEwith them.Tests
test_call_sites_resolved_after_a_renaming_transform— renames everycall_functionnode between_subexport_and_restoreandadd_exported_program, then asserts both call sites still lower. Two callsites, so it covers ordered pairing as well as resolution. Fails without the
change.
test_mismatched_call_site_count_warns_and_falls_back— asserts theUserWarningon a count mismatch.Notes
No behaviour change for
add_pytorch_module: it converts the same program thesubmodules were prepared from, so resolution returns the recorded names.